takePhoto.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. wx.showToast({
  118. title: res.data.msg || "获取数据失败",
  119. icon: "none",
  120. });
  121. }
  122. })
  123. .catch((res) => {
  124. console.log(res);
  125. this.setData({
  126. loading: false,
  127. });
  128. wx.showToast({
  129. title: "网络请求失败",
  130. icon: "none",
  131. });
  132. });
  133. },
  134. /**
  135. * 搜索码头服务
  136. */
  137. searchTerminalServices() {
  138. this.getTerminalServices(this.data.terminalSearchWords);
  139. },
  140. /**
  141. * 输入框内容变化事件
  142. */
  143. onSearchInput(e) {
  144. this.setData({
  145. terminalSearchWords: e.detail.value,
  146. });
  147. },
  148. /**
  149. * 拨打电话
  150. */
  151. makePhoneCall(e) {
  152. const phone = e.currentTarget.dataset.phone;
  153. if (phone) {
  154. wx.makePhoneCall({
  155. phoneNumber: phone,
  156. });
  157. } else {
  158. wx.showToast({
  159. title: "电话号码不存在",
  160. icon: "none",
  161. });
  162. }
  163. },
  164. async getWaterLevel() {
  165. let { data } = await getApi("/pallet/water/level");
  166. if (data.status === 0) {
  167. // 判断data.result是数组内对象waterLevelFluctuation字段的第一个字符如果是负号,新增change字段的值为down,如果是正号,新增change字段的值为up,如果是0,新增change字段的值为zero
  168. data.result.forEach((item) => {
  169. if (item.waterLevelFluctuation[0] === "-") {
  170. item.change = "falling";
  171. item.changeIcon = "↓";
  172. }
  173. if (item.waterLevelFluctuation[0] === "+") {
  174. item.change = "rising";
  175. item.changeIcon = "↑";
  176. }
  177. if (item.waterLevelFluctuation[0] === "0") {
  178. item.change = "stable";
  179. }
  180. });
  181. this.setData({
  182. waterLevels: data.result,
  183. });
  184. } else {
  185. this.setData({
  186. waterLevels: [],
  187. });
  188. wx.showToast({
  189. title: data.msg,
  190. icon: "none",
  191. });
  192. }
  193. },
  194. async getNewEnergyPolicy() {
  195. let { data } = await getApi("/pallet/new/energy/subsidy/policy");
  196. if (data.status === 0) {
  197. this.setData({
  198. demolitionPolicy: data.result.demolitionPolicy,
  199. shipbuildingPolicy: data.result.shipbuildingPolicy,
  200. });
  201. } else {
  202. this.setData({
  203. demolitionPolicy: [],
  204. shipbuildingPolicy: [],
  205. });
  206. wx.showToast({
  207. title: data.msg,
  208. icon: "none",
  209. });
  210. }
  211. },
  212. // intelligent/service获取智能服务
  213. async getIntelligentService() {
  214. let { data } = await postApi("/pallet/intelligent/service", {});
  215. if (data.status === 0) {
  216. this.setData(data.result);
  217. } else {
  218. this.setData({
  219. shipAnnualInspection: [],
  220. shipCertificate: [],
  221. shipCrewCertificate: [],
  222. certOperationContact: [],
  223. });
  224. wx.showToast({
  225. title: data.msg,
  226. });
  227. }
  228. },
  229. /**
  230. * 获取货盘列表数据
  231. */
  232. async getPalletList(palletListSearchWords = "") {
  233. this.setData({
  234. loading: true,
  235. });
  236. try {
  237. let { data } = await postApi("/pallet/list", {
  238. searchWords: palletListSearchWords,
  239. });
  240. if (data && data.status === 0) {
  241. this.setData({
  242. palletList: data.result || [],
  243. loading: false,
  244. });
  245. } else {
  246. this.setData({
  247. loading: false,
  248. palletList: [],
  249. });
  250. wx.showToast({
  251. title: data.msg || "获取数据失败",
  252. icon: "none",
  253. });
  254. }
  255. } catch (error) {
  256. console.log(error);
  257. this.setData({
  258. loading: false,
  259. });
  260. wx.showToast({
  261. title: "网络请求失败",
  262. icon: "none",
  263. });
  264. }
  265. },
  266. /**
  267. * 搜索货盘列表
  268. */
  269. searchPalletList() {
  270. this.getPalletList(this.data.palletListSearchWords);
  271. },
  272. /**
  273. * 货盘搜索输入框内容变化事件
  274. */
  275. onPalletSearchInput(e) {
  276. this.setData({
  277. palletListSearchWords: e.detail.value,
  278. });
  279. },
  280. /**
  281. * 获取签到状态
  282. */
  283. async getCheckinStatus() {
  284. try {
  285. let { data } = await getApi("/checkin/wx/checkin/status");
  286. if (data && data.status === 0) {
  287. this.setData(data.result);
  288. } else {
  289. wx.showToast({
  290. title: data.msg || "获取签到状态失败",
  291. icon: "none",
  292. });
  293. }
  294. } catch (error) {
  295. console.log(error);
  296. wx.showToast({
  297. title: "网络请求失败",
  298. icon: "none",
  299. });
  300. }
  301. },
  302. async onSignIn() {
  303. if (this.data.checkinStatus == 1) return;
  304. if (this.data.locked) {
  305. wx.showLoading({
  306. title: "高精度定位中...",
  307. });
  308. return;
  309. }
  310. this.data.locked = true;
  311. wx.getLocation({
  312. type: "gcj02",
  313. isHighAccuracy: true,
  314. success: async (e) => {
  315. let { latitude, longitude } = e;
  316. let { data } = await postApi("/checkin", {
  317. latitude,
  318. longitude,
  319. });
  320. this.setData(data.result);
  321. if (data.status == 0) {
  322. wx.showToast({
  323. title: data.msg,
  324. icon: "success",
  325. duration: 2000,
  326. mask: true,
  327. });
  328. // 更新签到状态
  329. this.getCheckinStatus();
  330. }
  331. },
  332. complete: (e) => {
  333. wx.hideLoading({});
  334. this.data.locked = false;
  335. },
  336. });
  337. },
  338. async takePhoto(e) {
  339. if (this.data.locked) {
  340. wx.showLoading({
  341. title: "高精度定位中...",
  342. });
  343. return;
  344. }
  345. this.data.locked = true;
  346. let { mediatype } = e.currentTarget.dataset;
  347. wx.getLocation({
  348. type: "gcj02",
  349. isHighAccuracy: true,
  350. success: (e) => {
  351. let { latitude, longitude } = e;
  352. console.log("获取定位成功!", e);
  353. this.data.latitude = latitude;
  354. this.data.longitude = longitude;
  355. wx.setStorageSync("latitude", latitude);
  356. wx.setStorageSync("longitude", longitude);
  357. wx.chooseMedia({
  358. mediaType: ["image"],
  359. sourceType: ["camera"],
  360. success: (e) => {
  361. console.log("获取媒体成功!", e);
  362. let src = e.tempFiles[0].tempFilePath;
  363. if (e.type == "video") {
  364. wx.showLoading({
  365. title: "正在压缩...",
  366. });
  367. wx.compressVideo({
  368. src,
  369. quality: "high",
  370. bitrate: "",
  371. fps: "",
  372. resolution: "",
  373. success: async (e) => {
  374. if (wx.getStorageSync("userName")) {
  375. wx.showLoading({
  376. title: "正在上传...",
  377. });
  378. let res = await uploadFile(e.tempFilePath, {
  379. type: 4,
  380. userId: wx.getStorageSync("userId"),
  381. location: `${this.data.longitude},${this.data.latitude}`,
  382. });
  383. if (res.status == 0) {
  384. console.log(res);
  385. wx.showToast({
  386. title: res.msg,
  387. });
  388. wx.redirectTo({
  389. url: "/pages/takePhoto/success/success",
  390. });
  391. } else {
  392. wx.showToast({
  393. title: res.msg,
  394. });
  395. }
  396. } else {
  397. // 新用户视频
  398. wx.hideLoading({
  399. success: (res) => {},
  400. });
  401. console.log("新用户视频", e);
  402. wx.setStorageSync("type", 2);
  403. wx.setStorageSync("file", e.tempFilePath);
  404. wx.redirectTo({
  405. url: `/pages/newCachePage/newCachePage`,
  406. });
  407. }
  408. },
  409. fail: (e) => {
  410. console.log(e);
  411. },
  412. });
  413. } else {
  414. wx.compressImage({
  415. src,
  416. quality: 80, // 压缩质量
  417. success: async (e) => {
  418. console.log("图片压缩成功!", e);
  419. wx.hideLoading({
  420. success: (res) => {},
  421. });
  422. if (wx.getStorageSync("userName")) {
  423. wx.showLoading({
  424. title: "正在上传...",
  425. });
  426. let postData = {
  427. type: mediatype,
  428. userId: wx.getStorageSync("userId"),
  429. };
  430. if (mediatype == 3) {
  431. postData.location = `${this.data.longitude},${this.data.latitude}`;
  432. } else {
  433. postData.location = "";
  434. }
  435. let res = await uploadFile(e.tempFilePath, postData);
  436. console.log("上传结束", res);
  437. if (res.status == 0) {
  438. wx.showToast({
  439. title: res.msg,
  440. });
  441. wx.setStorageSync("shareImageUrl", res.result.viewUrl);
  442. console.log(wx.getStorageSync("shareImageUrl"));
  443. wx.downloadFile({
  444. url: res.result.viewUrl,
  445. success: (e) => {
  446. console.log("下载调用!", e);
  447. wx.setStorageSync("cacheImage", e.tempFilePath);
  448. if (mediatype == 3) {
  449. wx.saveImageToPhotosAlbum({
  450. filePath: e.tempFilePath,
  451. success: (e) => {
  452. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  453. wx.showToast({
  454. title: "保存成功!",
  455. });
  456. wx.removeStorageSync("cacheImage");
  457. }
  458. },
  459. fail: (e) => {
  460. console.log("失败4", e);
  461. this.setData({
  462. authModal: true,
  463. modalText: "文件存储",
  464. });
  465. },
  466. });
  467. }
  468. wx.redirectTo({
  469. url: "/pages/takePhoto/success/success",
  470. });
  471. },
  472. fail: (e) => {
  473. console.log("失败3", e);
  474. },
  475. });
  476. } else {
  477. wx.showToast({
  478. title: res.msg,
  479. });
  480. }
  481. } else {
  482. // 新用户图片
  483. console.log("新用户图片", e);
  484. wx.setStorageSync("type", 1);
  485. wx.setStorageSync("file", e.tempFilePath);
  486. wx.redirectTo({
  487. url: `/pages/newCachePage/newCachePage`,
  488. });
  489. }
  490. },
  491. fail: (e) => {
  492. console.log("失败2", e);
  493. },
  494. });
  495. }
  496. },
  497. fail: (e) => {
  498. console.log("失败1", e);
  499. },
  500. });
  501. },
  502. fail: (e) => {
  503. this.setData({
  504. authModal: true,
  505. modalText: "位置信息",
  506. });
  507. },
  508. complete: (e) => {
  509. wx.hideLoading({
  510. success: (res) => {},
  511. });
  512. this.data.locked = false;
  513. },
  514. });
  515. },
  516. takeBill() {
  517. if (this.data.locked) return;
  518. wx.redirectTo({
  519. url: "/pages/takeBill/takeBill",
  520. });
  521. },
  522. });