takePhoto.js 8.9 KB

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