takePhoto.js 9.2 KB

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