takePhoto.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import { uploadFile } from "../../utils/upload";
  2. import { subMsg } from "../../utils/wxUtils";
  3. import { postApi } from "../../apis/api";
  4. Page({
  5. data: {
  6. sign: "https://frontend-1255802371.cos.ap-shanghai.myqcloud.com/sign.png",
  7. avater_exp:
  8. "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/avatar-icon.jpg?sign=f5c66c94d189436f82353eb48cb01f08&t=1634538864",
  9. cameraIcon:
  10. "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/camera-icon.png?sign=11a65871a2800cd04ecaa8991687fccd&t=1634607415",
  11. newCameraIcon:
  12. "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/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. },
  22. openSetting() {
  23. this.setData({
  24. authModal: false,
  25. });
  26. wx.openSetting({
  27. complete: (e) => {
  28. console.log(e);
  29. if (e.authSetting["scope.writePhotosAlbum"]) {
  30. if (wx.getStorageSync("cacheImage")) {
  31. wx.saveImageToPhotosAlbum({
  32. filePath: wx.getStorageSync("cacheImage"),
  33. success: (e) => {
  34. wx.showToast({
  35. title: "保存成功!",
  36. });
  37. wx.removeStorageSync("cacheImage");
  38. },
  39. complete: (e) => {
  40. console.log(e);
  41. },
  42. });
  43. }
  44. }
  45. },
  46. });
  47. },
  48. takeBill() {
  49. if (this.data.locked) return;
  50. wx.redirectTo({
  51. url: "/pages/takeBill/takeBill",
  52. });
  53. },
  54. async registerShip() {
  55. if (!this.checkout()) return;
  56. let res = await postApi("/user/wx/register", {
  57. userId: wx.getStorageSync("userId"),
  58. shipName: this.data.shipName,
  59. shipMmsi: this.data.shipMmsi,
  60. });
  61. if (res.data.status == 0) {
  62. let { shipInfo, userInfo } = res.data.result;
  63. let data = {
  64. ...shipInfo,
  65. ...userInfo,
  66. };
  67. Object.keys(data).forEach(function (key) {
  68. wx.setStorageSync(key, data[key]);
  69. });
  70. wx.showToast({
  71. title: res.data.msg,
  72. });
  73. this.setData({
  74. ...data,
  75. });
  76. } else {
  77. wx.showToast({
  78. title: res.data.msg,
  79. });
  80. }
  81. },
  82. checkout() {
  83. if (!this.data.shipName) {
  84. wx.showToast({
  85. title: "请输入船名!",
  86. icon: "error",
  87. });
  88. return;
  89. }
  90. if (!this.data.shipMmsi) {
  91. wx.showToast({
  92. title: "请输入MMSI!",
  93. icon: "error",
  94. });
  95. return;
  96. }
  97. return true;
  98. },
  99. async takePhoto(e) {
  100. if (this.data.locked) {
  101. wx.showLoading({
  102. title: "高精度定位中...",
  103. });
  104. return;
  105. }
  106. this.data.locked = true;
  107. let { mediatype } = e.currentTarget.dataset;
  108. wx.getLocation({
  109. type: "gcj02",
  110. isHighAccuracy: true,
  111. success: (e) => {
  112. let { latitude, longitude } = e;
  113. console.log("获取定位成功!", e);
  114. this.data.latitude = latitude;
  115. this.data.longitude = longitude;
  116. wx.setStorageSync("latitude", latitude);
  117. wx.setStorageSync("longitude", longitude);
  118. wx.chooseMedia({
  119. mediaType: ["image"],
  120. sourceType: ["camera"],
  121. success: (e) => {
  122. console.log("获取媒体成功!", e);
  123. let src = e.tempFiles[0].tempFilePath;
  124. if (e.type == "video") {
  125. wx.showLoading({
  126. title: "正在压缩...",
  127. });
  128. wx.compressVideo({
  129. src,
  130. quality: "high",
  131. bitrate: "",
  132. fps: "",
  133. resolution: "",
  134. success: async (e) => {
  135. if (wx.getStorageSync("userName")) {
  136. wx.showLoading({
  137. title: "正在上传...",
  138. });
  139. let res = await uploadFile(e.tempFilePath, {
  140. type: 4,
  141. userId: wx.getStorageSync("userId"),
  142. location: `${this.data.longitude},${this.data.latitude}`,
  143. });
  144. if (res.status == 0) {
  145. console.log(res);
  146. wx.showToast({
  147. title: res.msg,
  148. });
  149. wx.redirectTo({
  150. url: "/pages/takePhoto/success/success",
  151. });
  152. } else {
  153. wx.showToast({
  154. title: res.msg,
  155. });
  156. }
  157. } else {
  158. // 新用户视频
  159. wx.hideLoading({
  160. success: (res) => {},
  161. });
  162. console.log("新用户视频", e);
  163. wx.setStorageSync("type", 2);
  164. wx.setStorageSync("file", e.tempFilePath);
  165. wx.redirectTo({
  166. url: `/pages/newCachePage/newCachePage`,
  167. });
  168. }
  169. },
  170. fail: (e) => {
  171. console.log(e);
  172. },
  173. });
  174. } else {
  175. wx.compressImage({
  176. src,
  177. quality: 80, // 压缩质量
  178. success: async (e) => {
  179. console.log("图片压缩成功!", e);
  180. wx.hideLoading({
  181. success: (res) => {},
  182. });
  183. if (wx.getStorageSync("userName")) {
  184. wx.showLoading({
  185. title: "正在上传...",
  186. });
  187. let postData = {
  188. type: mediatype,
  189. userId: wx.getStorageSync("userId"),
  190. };
  191. if (mediatype == 3) {
  192. postData.location = `${this.data.longitude},${this.data.latitude}`;
  193. } else {
  194. postData.location = "";
  195. }
  196. let res = await uploadFile(e.tempFilePath, postData);
  197. console.log("上传结束", res);
  198. if (res.status == 0) {
  199. wx.showToast({
  200. title: res.msg,
  201. });
  202. wx.setStorageSync("shareImageUrl", res.result.viewUrl);
  203. console.log(wx.getStorageSync("shareImageUrl"));
  204. wx.downloadFile({
  205. url: res.result.viewUrl,
  206. success: (e) => {
  207. console.log("下载调用!", e);
  208. wx.setStorageSync("cacheImage", e.tempFilePath);
  209. if (mediatype == 3) {
  210. wx.saveImageToPhotosAlbum({
  211. filePath: e.tempFilePath,
  212. success: (e) => {
  213. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  214. wx.showToast({
  215. title: "保存成功!",
  216. });
  217. wx.removeStorageSync("cacheImage");
  218. }
  219. },
  220. fail: (e) => {
  221. console.log("失败4", e);
  222. this.setData({
  223. authModal: true,
  224. modalText: "文件存储",
  225. });
  226. },
  227. });
  228. }
  229. wx.redirectTo({
  230. url: "/pages/takePhoto/success/success",
  231. });
  232. },
  233. fail: (e) => {
  234. console.log("失败3", e);
  235. },
  236. });
  237. } else {
  238. wx.showToast({
  239. title: res.msg,
  240. });
  241. }
  242. } else {
  243. // 新用户图片
  244. console.log("新用户图片", e);
  245. wx.setStorageSync("type", 1);
  246. wx.setStorageSync("file", e.tempFilePath);
  247. wx.redirectTo({
  248. url: `/pages/newCachePage/newCachePage`,
  249. });
  250. }
  251. },
  252. fail: (e) => {
  253. console.log("失败2", e);
  254. },
  255. });
  256. }
  257. },
  258. fail: (e) => {
  259. console.log("失败1", e);
  260. },
  261. });
  262. },
  263. fail: (e) => {
  264. this.setData({
  265. authModal: true,
  266. modalText: "位置信息",
  267. });
  268. },
  269. complete: (e) => {
  270. wx.hideLoading({
  271. success: (res) => {},
  272. });
  273. this.data.locked = false;
  274. },
  275. });
  276. },
  277. async msg() {
  278. let res = await subMsg();
  279. },
  280. async checkCheckStatus() {
  281. let { data } = await postApi("/user/wx/checkin/status", {
  282. userId: wx.getStorageSync("userId"),
  283. });
  284. this.setData(data.result);
  285. },
  286. async check() {
  287. if (this.data.checkinStatus == 1) return;
  288. if (this.data.locked) {
  289. wx.showLoading({
  290. title: "高精度定位中...",
  291. });
  292. return;
  293. }
  294. this.data.locked = true;
  295. wx.getLocation({
  296. type: "gcj02",
  297. isHighAccuracy: true,
  298. success: async (e) => {
  299. let { latitude, longitude } = e;
  300. let { data } = await postApi("/user/wx/checkin", {
  301. userId: wx.getStorageSync("userId"),
  302. latitude,
  303. longitude,
  304. });
  305. this.setData(data.result);
  306. if (data.status == 0) {
  307. wx.showToast({
  308. title: data.msg,
  309. icon: "success",
  310. duration: 2000,
  311. mask: true,
  312. });
  313. }
  314. },
  315. complete: (e) => {
  316. this.data.locked = false;
  317. },
  318. });
  319. },
  320. onShow() {
  321. let userName = wx.getStorageSync("userName");
  322. let userId = wx.getStorageSync("userId");
  323. let phone = wx.getStorageSync("phone");
  324. let shipId = wx.getStorageSync("shipId");
  325. if (userId && shipId) this.checkCheckStatus();
  326. this.setData({
  327. userName,
  328. phone,
  329. userId,
  330. shipId,
  331. });
  332. },
  333. });