takePhoto.js 14 KB

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