takePhoto.js 12 KB

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