takePhoto.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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.checkToken()) return;
  312. if (this.data.locked) {
  313. wx.showLoading({
  314. title: "高精度定位中...",
  315. });
  316. return;
  317. }
  318. this.data.locked = true;
  319. let { mediatype } = e.currentTarget.dataset;
  320. wx.getLocation({
  321. type: "gcj02",
  322. isHighAccuracy: true,
  323. success: (e) => {
  324. let { latitude, longitude } = e;
  325. console.log("获取定位成功!", e);
  326. this.data.latitude = latitude;
  327. this.data.longitude = longitude;
  328. wx.setStorageSync("latitude", latitude);
  329. wx.setStorageSync("longitude", longitude);
  330. wx.chooseMedia({
  331. mediaType: ["image"],
  332. sourceType: ["camera"],
  333. success: (e) => {
  334. console.log("获取媒体成功!", e);
  335. let src = e.tempFiles[0].tempFilePath;
  336. if (e.type == "video") {
  337. wx.showLoading({
  338. title: "正在压缩...",
  339. });
  340. wx.compressVideo({
  341. src,
  342. quality: "high",
  343. bitrate: "",
  344. fps: "",
  345. resolution: "",
  346. success: async (e) => {
  347. if (wx.getStorageSync("userName")) {
  348. wx.showLoading({
  349. title: "正在上传...",
  350. });
  351. let res = await uploadFile(e.tempFilePath, {
  352. type: 4,
  353. userId: wx.getStorageSync("userId"),
  354. location: `${this.data.longitude},${this.data.latitude}`,
  355. });
  356. if (res.status == 0) {
  357. console.log(res);
  358. wx.showToast({
  359. title: res.msg,
  360. });
  361. wx.redirectTo({
  362. url: "/pages/takePhoto/success/success",
  363. });
  364. } else {
  365. wx.showToast({
  366. title: res.msg,
  367. });
  368. }
  369. } else {
  370. // 新用户视频
  371. wx.hideLoading({
  372. success: (res) => {},
  373. });
  374. console.log("新用户视频", e);
  375. wx.setStorageSync("type", 2);
  376. wx.setStorageSync("file", e.tempFilePath);
  377. wx.redirectTo({
  378. url: `/pages/newCachePage/newCachePage`,
  379. });
  380. }
  381. },
  382. fail: (e) => {
  383. console.log(e);
  384. },
  385. });
  386. } else {
  387. wx.compressImage({
  388. src,
  389. quality: 80, // 压缩质量
  390. success: async (e) => {
  391. console.log("图片压缩成功!", e);
  392. wx.hideLoading({
  393. success: (res) => {},
  394. });
  395. if (wx.getStorageSync("userName")) {
  396. wx.showLoading({
  397. title: "正在上传...",
  398. });
  399. let postData = {
  400. type: mediatype,
  401. userId: wx.getStorageSync("userId"),
  402. };
  403. if (mediatype == 3) {
  404. postData.location = `${this.data.longitude},${this.data.latitude}`;
  405. } else {
  406. postData.location = "";
  407. }
  408. let res = await uploadFile(e.tempFilePath, postData);
  409. console.log("上传结束", res);
  410. if (res.status == 0) {
  411. wx.showToast({
  412. title: res.msg,
  413. });
  414. wx.setStorageSync("shareImageUrl", res.result.viewUrl);
  415. console.log(wx.getStorageSync("shareImageUrl"));
  416. wx.downloadFile({
  417. url: res.result.viewUrl,
  418. success: (e) => {
  419. console.log("下载调用!", e);
  420. wx.setStorageSync("cacheImage", e.tempFilePath);
  421. if (mediatype == 3) {
  422. wx.saveImageToPhotosAlbum({
  423. filePath: e.tempFilePath,
  424. success: (e) => {
  425. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  426. wx.showToast({
  427. title: "保存成功!",
  428. });
  429. wx.removeStorageSync("cacheImage");
  430. }
  431. },
  432. fail: (e) => {
  433. console.log("失败4", e);
  434. this.setData({
  435. authModal: true,
  436. modalText: "文件存储",
  437. });
  438. },
  439. });
  440. }
  441. wx.redirectTo({
  442. url: "/pages/takePhoto/success/success",
  443. });
  444. },
  445. fail: (e) => {
  446. console.log("失败3", e);
  447. },
  448. });
  449. } else {
  450. wx.showToast({
  451. title: res.msg,
  452. });
  453. }
  454. } else {
  455. // 新用户图片
  456. console.log("新用户图片", e);
  457. wx.setStorageSync("type", 1);
  458. wx.setStorageSync("file", e.tempFilePath);
  459. wx.redirectTo({
  460. url: `/pages/newCachePage/newCachePage`,
  461. });
  462. }
  463. },
  464. fail: (e) => {
  465. console.log("失败2", e);
  466. },
  467. });
  468. }
  469. },
  470. fail: (e) => {
  471. console.log("失败1", e);
  472. },
  473. });
  474. },
  475. fail: (e) => {
  476. this.setData({
  477. authModal: true,
  478. modalText: "位置信息",
  479. });
  480. },
  481. complete: (e) => {
  482. wx.hideLoading({
  483. success: (res) => {},
  484. });
  485. this.data.locked = false;
  486. },
  487. });
  488. },
  489. takeBill() {
  490. if (!this.checkToken()) return;
  491. if (this.data.locked) return;
  492. wx.redirectTo({
  493. url: "/pages/takeBill/takeBill",
  494. });
  495. },
  496. });