takePhoto.js 14 KB

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