takePhoto.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import { uploadFile } from "../../utils/upload";
  2. import { subMsg } from "../../utils/wxUtils";
  3. import { postApi, getApi } from "../../apis/api";
  4. Page({
  5. data: {
  6. sign: "https://frontend-1255802371.cos.ap-shanghai.myqcloud.com/sign.png",
  7. avater_exp:
  8. "https://frontend-1255802371.cos.ap-shanghai.myqcloud.com/miniapp-static/avatar-icon.jpg?sign=f5c66c94d189436f82353eb48cb01f08&t=1634538864",
  9. cameraIcon:
  10. "https://frontend-1255802371.cos.ap-shanghai.myqcloud.com/miniapp-static/camera-icon.png?sign=11a65871a2800cd04ecaa8991687fccd&t=1634607415",
  11. newCameraIcon:
  12. "https://frontend-1255802371.cos.ap-shanghai.myqcloud.com/miniapp-static/camera.png?sign=ad0fe8bead6a46cb20f45f792d4bed67&t=1645502416",
  13. userName: "",
  14. phone: "",
  15. shipName: "",
  16. shipMmsi: "",
  17. authModal: false,
  18. modalText: "位置",
  19. shipId: "",
  20. locked: false,
  21. certs: [],
  22. contacts: [],
  23. oilPriceList: [],
  24. },
  25. async getCerts() {
  26. let { data } = await postApi("/ship/cert/list", {
  27. userId: wx.getStorageSync("userId"),
  28. });
  29. if (data.status === 0) {
  30. data.result.forEach((cert) => {
  31. cert.endValidTime = cert.endValidTime.split(" ")[0];
  32. });
  33. this.setData({
  34. certs: data.result,
  35. });
  36. } else {
  37. this.setData({
  38. certs: [],
  39. });
  40. wx.showToast({
  41. title: data.msg,
  42. icon: "none",
  43. });
  44. }
  45. },
  46. async getContacts() {
  47. let { data } = await getApi("/ship/cert/operation/contacts");
  48. if (data.status === 0) {
  49. this.setData({
  50. contacts: data.result,
  51. });
  52. } else {
  53. this.setData({
  54. contacts: [],
  55. });
  56. wx.showToast({
  57. title: data.msg,
  58. icon: "none",
  59. });
  60. }
  61. },
  62. makePhoneCall(e) {
  63. wx.makePhoneCall({
  64. phoneNumber: e.currentTarget.dataset.phone,
  65. });
  66. },
  67. copy(e) {
  68. wx.setClipboardData({
  69. data: e.currentTarget.dataset.phone,
  70. success(res) {
  71. wx.showToast({
  72. title: "复制成功",
  73. });
  74. },
  75. });
  76. },
  77. async getOilPriceList() {
  78. let { data } = await postApi("/fuel/list", {});
  79. if (data.status === 0) {
  80. this.setData({
  81. oilPriceList: data.result.map((item, index) => {
  82. return {
  83. id: index,
  84. latitude: item.fuelShipLatitude,
  85. longitude: item.fuelShipLongitude,
  86. iconPath:
  87. "https://frontend-1255802371.cos.ap-shanghai.myqcloud.com/miniapp-static%2Fgas-station.png",
  88. width: 40,
  89. height: 40,
  90. ...item,
  91. callout: {
  92. content: item.vendorName,
  93. display: "ALWAYS",
  94. bgColor: "none",
  95. },
  96. };
  97. }),
  98. });
  99. } else {
  100. this.setData({
  101. oilPriceList: [],
  102. });
  103. wx.showToast({
  104. title: data.msg,
  105. icon: "none",
  106. });
  107. }
  108. },
  109. openSetting() {
  110. this.setData({
  111. authModal: false,
  112. });
  113. wx.openSetting({
  114. complete: (e) => {
  115. console.log(e);
  116. if (e.authSetting["scope.writePhotosAlbum"]) {
  117. if (wx.getStorageSync("cacheImage")) {
  118. wx.saveImageToPhotosAlbum({
  119. filePath: wx.getStorageSync("cacheImage"),
  120. success: (e) => {
  121. wx.showToast({
  122. title: "保存成功!",
  123. });
  124. wx.removeStorageSync("cacheImage");
  125. },
  126. complete: (e) => {
  127. console.log(e);
  128. },
  129. });
  130. }
  131. }
  132. },
  133. });
  134. },
  135. takeBill() {
  136. if (this.data.locked) return;
  137. wx.redirectTo({
  138. url: "/pages/takeBill/takeBill",
  139. });
  140. },
  141. async registerShip() {
  142. if (!this.checkout()) return;
  143. let res = await postApi("/user/wx/register", {
  144. userId: wx.getStorageSync("userId"),
  145. shipName: this.data.shipName,
  146. shipMmsi: this.data.shipMmsi,
  147. });
  148. if (res.data.status == 0) {
  149. let { shipInfo, userInfo } = res.data.result;
  150. let data = {
  151. ...shipInfo,
  152. ...userInfo,
  153. };
  154. Object.keys(data).forEach(function (key) {
  155. wx.setStorageSync(key, data[key]);
  156. });
  157. wx.showToast({
  158. title: res.data.msg,
  159. });
  160. this.setData({
  161. ...data,
  162. });
  163. } else {
  164. wx.showToast({
  165. title: res.data.msg,
  166. });
  167. }
  168. },
  169. checkout() {
  170. if (!this.data.shipName) {
  171. wx.showToast({
  172. title: "请输入船名!",
  173. icon: "error",
  174. });
  175. return;
  176. }
  177. if (!this.data.shipMmsi) {
  178. wx.showToast({
  179. title: "请输入MMSI!",
  180. icon: "error",
  181. });
  182. return;
  183. }
  184. return true;
  185. },
  186. async takePhoto(e) {
  187. if (this.data.locked) {
  188. wx.showLoading({
  189. title: "高精度定位中...",
  190. });
  191. return;
  192. }
  193. this.data.locked = true;
  194. let { mediatype } = e.currentTarget.dataset;
  195. wx.getLocation({
  196. type: "gcj02",
  197. isHighAccuracy: true,
  198. success: (e) => {
  199. let { latitude, longitude } = e;
  200. console.log("获取定位成功!", e);
  201. this.data.latitude = latitude;
  202. this.data.longitude = longitude;
  203. wx.setStorageSync("latitude", latitude);
  204. wx.setStorageSync("longitude", longitude);
  205. wx.chooseMedia({
  206. mediaType: ["image"],
  207. sourceType: ["camera"],
  208. success: (e) => {
  209. console.log("获取媒体成功!", e);
  210. let src = e.tempFiles[0].tempFilePath;
  211. if (e.type == "video") {
  212. wx.showLoading({
  213. title: "正在压缩...",
  214. });
  215. wx.compressVideo({
  216. src,
  217. quality: "high",
  218. bitrate: "",
  219. fps: "",
  220. resolution: "",
  221. success: async (e) => {
  222. if (wx.getStorageSync("userName")) {
  223. wx.showLoading({
  224. title: "正在上传...",
  225. });
  226. let res = await uploadFile(e.tempFilePath, {
  227. type: 4,
  228. userId: wx.getStorageSync("userId"),
  229. location: `${this.data.longitude},${this.data.latitude}`,
  230. });
  231. if (res.status == 0) {
  232. console.log(res);
  233. wx.showToast({
  234. title: res.msg,
  235. });
  236. wx.redirectTo({
  237. url: "/pages/takePhoto/success/success",
  238. });
  239. } else {
  240. wx.showToast({
  241. title: res.msg,
  242. });
  243. }
  244. } else {
  245. // 新用户视频
  246. wx.hideLoading({
  247. success: (res) => {},
  248. });
  249. console.log("新用户视频", e);
  250. wx.setStorageSync("type", 2);
  251. wx.setStorageSync("file", e.tempFilePath);
  252. wx.redirectTo({
  253. url: `/pages/newCachePage/newCachePage`,
  254. });
  255. }
  256. },
  257. fail: (e) => {
  258. console.log(e);
  259. },
  260. });
  261. } else {
  262. wx.compressImage({
  263. src,
  264. quality: 80, // 压缩质量
  265. success: async (e) => {
  266. console.log("图片压缩成功!", e);
  267. wx.hideLoading({
  268. success: (res) => {},
  269. });
  270. if (wx.getStorageSync("userName")) {
  271. wx.showLoading({
  272. title: "正在上传...",
  273. });
  274. let postData = {
  275. type: mediatype,
  276. userId: wx.getStorageSync("userId"),
  277. };
  278. if (mediatype == 3) {
  279. postData.location = `${this.data.longitude},${this.data.latitude}`;
  280. } else {
  281. postData.location = "";
  282. }
  283. let res = await uploadFile(e.tempFilePath, postData);
  284. console.log("上传结束", res);
  285. if (res.status == 0) {
  286. wx.showToast({
  287. title: res.msg,
  288. });
  289. wx.setStorageSync("shareImageUrl", res.result.viewUrl);
  290. console.log(wx.getStorageSync("shareImageUrl"));
  291. wx.downloadFile({
  292. url: res.result.viewUrl,
  293. success: (e) => {
  294. console.log("下载调用!", e);
  295. wx.setStorageSync("cacheImage", e.tempFilePath);
  296. if (mediatype == 3) {
  297. wx.saveImageToPhotosAlbum({
  298. filePath: e.tempFilePath,
  299. success: (e) => {
  300. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  301. wx.showToast({
  302. title: "保存成功!",
  303. });
  304. wx.removeStorageSync("cacheImage");
  305. }
  306. },
  307. fail: (e) => {
  308. console.log("失败4", e);
  309. this.setData({
  310. authModal: true,
  311. modalText: "文件存储",
  312. });
  313. },
  314. });
  315. }
  316. wx.redirectTo({
  317. url: "/pages/takePhoto/success/success",
  318. });
  319. },
  320. fail: (e) => {
  321. console.log("失败3", e);
  322. },
  323. });
  324. } else {
  325. wx.showToast({
  326. title: res.msg,
  327. });
  328. }
  329. } else {
  330. // 新用户图片
  331. console.log("新用户图片", e);
  332. wx.setStorageSync("type", 1);
  333. wx.setStorageSync("file", e.tempFilePath);
  334. wx.redirectTo({
  335. url: `/pages/newCachePage/newCachePage`,
  336. });
  337. }
  338. },
  339. fail: (e) => {
  340. console.log("失败2", e);
  341. },
  342. });
  343. }
  344. },
  345. fail: (e) => {
  346. console.log("失败1", e);
  347. },
  348. });
  349. },
  350. fail: (e) => {
  351. this.setData({
  352. authModal: true,
  353. modalText: "位置信息",
  354. });
  355. },
  356. complete: (e) => {
  357. wx.hideLoading({
  358. success: (res) => {},
  359. });
  360. this.data.locked = false;
  361. },
  362. });
  363. },
  364. async msg() {
  365. let res = await subMsg();
  366. },
  367. async checkCheckStatus() {
  368. let { data } = await postApi("/user/wx/checkin/status", {
  369. userId: wx.getStorageSync("userId"),
  370. });
  371. this.setData(data.result);
  372. },
  373. async check() {
  374. if (this.data.checkinStatus == 1) return;
  375. if (this.data.locked) {
  376. wx.showLoading({
  377. title: "高精度定位中...",
  378. });
  379. return;
  380. }
  381. this.data.locked = true;
  382. wx.getLocation({
  383. type: "gcj02",
  384. isHighAccuracy: true,
  385. success: async (e) => {
  386. let { latitude, longitude } = e;
  387. let { data } = await postApi("/user/wx/checkin", {
  388. userId: wx.getStorageSync("userId"),
  389. latitude,
  390. longitude,
  391. });
  392. this.setData(data.result);
  393. if (data.status == 0) {
  394. wx.showToast({
  395. title: data.msg,
  396. icon: "success",
  397. duration: 2000,
  398. mask: true,
  399. });
  400. }
  401. },
  402. complete: (e) => {
  403. wx.hideLoading({});
  404. this.data.locked = false;
  405. },
  406. });
  407. },
  408. onShow() {
  409. if (typeof this.getTabBar === "function" && this.getTabBar()) {
  410. this.getTabBar().setData({
  411. selected: 0,
  412. });
  413. }
  414. let userName = wx.getStorageSync("userName");
  415. let userId = wx.getStorageSync("userId");
  416. let phone = wx.getStorageSync("phone");
  417. let shipId = wx.getStorageSync("shipId");
  418. if (userId && shipId) this.checkCheckStatus();
  419. this.getCerts();
  420. this.getContacts();
  421. this.getOilPriceList();
  422. this.setData({
  423. userName,
  424. phone,
  425. userId,
  426. shipId,
  427. });
  428. },
  429. });