takePhoto.js 14 KB

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