takePhoto.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // pages/takePhoto/takePhoto.js
  2. import { uploadFile } from "../../utils/upload";
  3. import { postApi, getApi } from "../../apis/api";
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. checkinStatus: 0, // 签到状态
  10. checkinDatetime: "",
  11. checkinDesc: "",
  12. shipOwnerId: 0,
  13. locked: false, // 锁定状态
  14. terminalServices: [], // 码头服务列表
  15. terminalSearchWords: "", // 搜索关键词
  16. loading: false, // 加载状态
  17. waterLevels: [], // 水位数据
  18. demolitionPolicy: [], // 拆解政策
  19. shipbuildingPolicy: [], // 造船政策
  20. shipAnnualInspection: [],
  21. shipCertificate: [],
  22. shipCrewCertificate: [],
  23. certOperationContact: [],
  24. palletList: [
  25. {
  26. id: 5,
  27. cargoName: "222",
  28. loadPortName: "2",
  29. dischargePortName: "3",
  30. cargoTons: 4,
  31. contactName: "5",
  32. contactPhone: "13666666666",
  33. status: 1,
  34. releaseTime: "2025/05/08 19:52:33",
  35. createTime: "2025/05/07 23:36:26",
  36. },
  37. {
  38. id: 6,
  39. cargoName: "测试1",
  40. loadPortName: "上海",
  41. dischargePortName: "张家港",
  42. cargoTons: 111,
  43. contactName: "11",
  44. contactPhone: "11111111111",
  45. status: 1,
  46. releaseTime: "2025/05/08 19:52:14",
  47. createTime: "2025/05/08 11:06:03",
  48. },
  49. ],
  50. palletListSearchWords: "", // 货盘搜索关键词
  51. },
  52. /**
  53. * 生命周期函数--监听页面加载
  54. */
  55. onLoad(options) {
  56. this.getTerminalServices();
  57. this.getWaterLevel();
  58. this.getNewEnergyPolicy();
  59. this.getIntelligentService();
  60. this.getPalletList();
  61. this.getCheckinStatus();
  62. },
  63. /**
  64. * 生命周期函数--监听页面初次渲染完成
  65. */
  66. onReady() {},
  67. /**
  68. * 生命周期函数--监听页面显示
  69. */
  70. onShow() {
  71. if (typeof this.getTabBar === "function" && this.getTabBar()) {
  72. this.getTabBar().setData({
  73. selected: 0,
  74. });
  75. }
  76. },
  77. /**
  78. * 生命周期函数--监听页面隐藏
  79. */
  80. onHide() {},
  81. /**
  82. * 生命周期函数--监听页面卸载
  83. */
  84. onUnload() {},
  85. /**
  86. * 页面相关事件处理函数--监听用户下拉动作
  87. */
  88. onPullDownRefresh() {},
  89. /**
  90. * 页面上拉触底事件的处理函数
  91. */
  92. onReachBottom() {},
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage() {},
  97. /**
  98. * 获取码头服务数据
  99. */
  100. getTerminalServices(terminalSearchWords = "") {
  101. this.setData({
  102. loading: true,
  103. });
  104. postApi("/pallet/terminal/services", {
  105. searchWords: terminalSearchWords,
  106. })
  107. .then((res) => {
  108. if (res.data && res.data.status === 0) {
  109. this.setData({
  110. terminalServices: res.data.result || [],
  111. loading: false,
  112. });
  113. } else {
  114. this.setData({
  115. loading: false,
  116. });
  117. }
  118. })
  119. .catch((res) => {
  120. this.setData({
  121. loading: false,
  122. });
  123. });
  124. },
  125. /**
  126. * 搜索码头服务
  127. */
  128. searchTerminalServices() {
  129. this.getTerminalServices(this.data.terminalSearchWords);
  130. },
  131. /**
  132. * 输入框内容变化事件
  133. */
  134. onSearchInput(e) {
  135. this.setData({
  136. terminalSearchWords: e.detail.value,
  137. });
  138. },
  139. /**
  140. * 拨打电话
  141. */
  142. makePhoneCall(e) {
  143. const phone = e.currentTarget.dataset.phone;
  144. if (phone) {
  145. wx.makePhoneCall({
  146. phoneNumber: phone,
  147. });
  148. } else {
  149. wx.showToast({
  150. title: "电话号码不存在",
  151. icon: "none",
  152. });
  153. }
  154. },
  155. async getWaterLevel() {
  156. let { data } = await getApi("/pallet/water/level");
  157. if (data.status === 0) {
  158. // 判断data.result是数组内对象waterLevelFluctuation字段的第一个字符如果是负号,新增change字段的值为down,如果是正号,新增change字段的值为up,如果是0,新增change字段的值为zero
  159. data.result.forEach((item) => {
  160. if (item.waterLevelFluctuation[0] === "-") {
  161. item.change = "falling";
  162. item.changeIcon = "↓";
  163. }
  164. if (item.waterLevelFluctuation[0] === "+") {
  165. item.change = "rising";
  166. item.changeIcon = "↑";
  167. }
  168. if (item.waterLevelFluctuation[0] === "0") {
  169. item.change = "stable";
  170. }
  171. });
  172. this.setData({
  173. waterLevels: data.result,
  174. });
  175. } else {
  176. this.setData({
  177. waterLevels: [],
  178. });
  179. }
  180. },
  181. async getNewEnergyPolicy() {
  182. let { data } = await getApi("/pallet/new/energy/subsidy/policy");
  183. if (data.status === 0) {
  184. this.setData({
  185. demolitionPolicy: data.result.demolitionPolicy,
  186. shipbuildingPolicy: data.result.shipbuildingPolicy,
  187. });
  188. } else {
  189. this.setData({
  190. demolitionPolicy: [],
  191. shipbuildingPolicy: [],
  192. });
  193. }
  194. },
  195. // intelligent/service获取智能服务
  196. async getIntelligentService() {
  197. let { data } = await postApi("/pallet/intelligent/service", {});
  198. if (data.status === 0) {
  199. this.setData(data.result);
  200. } else {
  201. this.setData({
  202. shipAnnualInspection: [],
  203. shipCertificate: [],
  204. shipCrewCertificate: [],
  205. certOperationContact: [],
  206. });
  207. }
  208. },
  209. /**
  210. * 获取货盘列表数据
  211. */
  212. async getPalletList(palletListSearchWords = "") {
  213. this.setData({
  214. loading: true,
  215. });
  216. try {
  217. let { data } = await postApi("/pallet/list", {
  218. searchWords: palletListSearchWords,
  219. });
  220. if (data && data.status === 0) {
  221. this.setData({
  222. palletList: data.result || [],
  223. loading: false,
  224. });
  225. } else {
  226. this.setData({
  227. loading: false,
  228. palletList: [],
  229. });
  230. }
  231. } catch (error) {
  232. console.log(error);
  233. this.setData({
  234. loading: false,
  235. });
  236. }
  237. },
  238. /**
  239. * 搜索货盘列表
  240. */
  241. searchPalletList() {
  242. this.getPalletList(this.data.palletListSearchWords);
  243. },
  244. /**
  245. * 货盘搜索输入框内容变化事件
  246. */
  247. onPalletSearchInput(e) {
  248. this.setData({
  249. palletListSearchWords: e.detail.value,
  250. });
  251. },
  252. /**
  253. * 获取签到状态
  254. */
  255. async getCheckinStatus() {
  256. try {
  257. let { data } = await getApi("/checkin/wx/checkin/status");
  258. if (data && data.status === 0) {
  259. this.setData(data.result);
  260. }
  261. } catch (error) {}
  262. },
  263. checkToken() {
  264. if (!wx.getStorageSync("accessToken")) {
  265. wx.showToast({
  266. title: "尚未登录,正在体验",
  267. icon: "none",
  268. });
  269. return false;
  270. }
  271. return true;
  272. },
  273. async onSignIn() {
  274. if (!this.checkToken()) return;
  275. if (this.data.checkinStatus == 1) return;
  276. if (this.data.locked) {
  277. wx.showLoading({
  278. title: "高精度定位中...",
  279. });
  280. return;
  281. }
  282. this.data.locked = true;
  283. wx.getLocation({
  284. type: "gcj02",
  285. isHighAccuracy: true,
  286. success: async (e) => {
  287. let { latitude, longitude } = e;
  288. let { data } = await postApi("/checkin", {
  289. latitude,
  290. longitude,
  291. });
  292. this.setData(data.result);
  293. if (data.status == 0) {
  294. wx.showToast({
  295. title: data.msg,
  296. icon: "success",
  297. duration: 2000,
  298. mask: true,
  299. });
  300. // 更新签到状态
  301. this.getCheckinStatus();
  302. }
  303. },
  304. complete: (e) => {
  305. wx.hideLoading({});
  306. this.data.locked = false;
  307. },
  308. });
  309. },
  310. async takePhoto(e) {
  311. if (this.data.locked) {
  312. wx.showLoading({
  313. title: "高精度定位中...",
  314. });
  315. return;
  316. }
  317. this.data.locked = true;
  318. let { mediatype } = e.currentTarget.dataset;
  319. wx.getLocation({
  320. type: "gcj02",
  321. isHighAccuracy: true,
  322. success: (e) => {
  323. let { latitude, longitude } = e;
  324. console.log("获取定位成功!", e);
  325. this.data.latitude = latitude;
  326. this.data.longitude = longitude;
  327. wx.setStorageSync("latitude", latitude);
  328. wx.setStorageSync("longitude", longitude);
  329. wx.chooseMedia({
  330. mediaType: ["image"],
  331. sourceType: ["camera"],
  332. success: (e) => {
  333. console.log("获取媒体成功!", e);
  334. let src = e.tempFiles[0].tempFilePath;
  335. if (e.type == "video") {
  336. wx.showLoading({
  337. title: "正在压缩...",
  338. });
  339. wx.compressVideo({
  340. src,
  341. quality: "high",
  342. bitrate: "",
  343. fps: "",
  344. resolution: "",
  345. success: async (e) => {
  346. if (wx.getStorageSync("userName")) {
  347. wx.showLoading({
  348. title: "正在上传...",
  349. });
  350. let res = await uploadFile(e.tempFilePath, {
  351. type: 4,
  352. userId: wx.getStorageSync("userId"),
  353. location: `${this.data.longitude},${this.data.latitude}`,
  354. });
  355. if (res.status == 0) {
  356. console.log(res);
  357. wx.showToast({
  358. title: res.msg,
  359. });
  360. wx.redirectTo({
  361. url: "/pages/takePhoto/success/success",
  362. });
  363. } else {
  364. wx.showToast({
  365. title: res.msg,
  366. });
  367. }
  368. } else {
  369. // 新用户视频
  370. wx.hideLoading({
  371. success: (res) => {},
  372. });
  373. console.log("新用户视频", e);
  374. wx.setStorageSync("type", 2);
  375. wx.setStorageSync("file", e.tempFilePath);
  376. wx.redirectTo({
  377. url: `/pages/newCachePage/newCachePage`,
  378. });
  379. }
  380. },
  381. fail: (e) => {
  382. console.log(e);
  383. },
  384. });
  385. } else {
  386. wx.compressImage({
  387. src,
  388. quality: 80, // 压缩质量
  389. success: async (e) => {
  390. console.log("图片压缩成功!", e);
  391. wx.hideLoading({
  392. success: (res) => {},
  393. });
  394. if (wx.getStorageSync("userName")) {
  395. wx.showLoading({
  396. title: "正在上传...",
  397. });
  398. let postData = {
  399. type: mediatype,
  400. userId: wx.getStorageSync("userId"),
  401. };
  402. if (mediatype == 3) {
  403. postData.location = `${this.data.longitude},${this.data.latitude}`;
  404. } else {
  405. postData.location = "";
  406. }
  407. let res = await uploadFile(e.tempFilePath, postData);
  408. console.log("上传结束", res);
  409. if (res.status == 0) {
  410. wx.showToast({
  411. title: res.msg,
  412. });
  413. wx.setStorageSync("shareImageUrl", res.result.viewUrl);
  414. console.log(wx.getStorageSync("shareImageUrl"));
  415. wx.downloadFile({
  416. url: res.result.viewUrl,
  417. success: (e) => {
  418. console.log("下载调用!", e);
  419. wx.setStorageSync("cacheImage", e.tempFilePath);
  420. if (mediatype == 3) {
  421. wx.saveImageToPhotosAlbum({
  422. filePath: e.tempFilePath,
  423. success: (e) => {
  424. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  425. wx.showToast({
  426. title: "保存成功!",
  427. });
  428. wx.removeStorageSync("cacheImage");
  429. }
  430. },
  431. fail: (e) => {
  432. console.log("失败4", e);
  433. this.setData({
  434. authModal: true,
  435. modalText: "文件存储",
  436. });
  437. },
  438. });
  439. }
  440. wx.redirectTo({
  441. url: "/pages/takePhoto/success/success",
  442. });
  443. },
  444. fail: (e) => {
  445. console.log("失败3", e);
  446. },
  447. });
  448. } else {
  449. wx.showToast({
  450. title: res.msg,
  451. });
  452. }
  453. } else {
  454. // 新用户图片
  455. console.log("新用户图片", e);
  456. wx.setStorageSync("type", 1);
  457. wx.setStorageSync("file", e.tempFilePath);
  458. wx.redirectTo({
  459. url: `/pages/newCachePage/newCachePage`,
  460. });
  461. }
  462. },
  463. fail: (e) => {
  464. console.log("失败2", e);
  465. },
  466. });
  467. }
  468. },
  469. fail: (e) => {
  470. console.log("失败1", e);
  471. },
  472. });
  473. },
  474. fail: (e) => {
  475. this.setData({
  476. authModal: true,
  477. modalText: "位置信息",
  478. });
  479. },
  480. complete: (e) => {
  481. wx.hideLoading({
  482. success: (res) => {},
  483. });
  484. this.data.locked = false;
  485. },
  486. });
  487. },
  488. takeBill() {
  489. if (this.data.locked) return;
  490. wx.redirectTo({
  491. url: "/pages/takeBill/takeBill",
  492. });
  493. },
  494. });