takePhoto.js 8.8 KB

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