takePhoto.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import {
  2. uploadFile
  3. } from "../../utils/upload"
  4. Page({
  5. data: {
  6. avatar: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/avatar-icon.jpg?sign=f5c66c94d189436f82353eb48cb01f08&t=1634538864",
  7. cameraIcon: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/camera-icon.png?sign=11a65871a2800cd04ecaa8991687fccd&t=1634607415",
  8. newCameraIcon: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/camera.png?sign=ad0fe8bead6a46cb20f45f792d4bed67&t=1645502416",
  9. userName: "",
  10. phone: "",
  11. shipName: "",
  12. shipMmsi: "",
  13. authModal: false,
  14. modalText: "位置"
  15. },
  16. openSetting() {
  17. this.setData({
  18. authModal: false,
  19. })
  20. wx.openSetting({
  21. complete: e => {
  22. console.log(e)
  23. if (e.authSetting["scope.writePhotosAlbum"]) {
  24. if (wx.getStorageSync('cacheImage')) {
  25. wx.saveImageToPhotosAlbum({
  26. filePath: wx.getStorageSync('cacheImage'),
  27. success: e => {
  28. wx.showToast({
  29. title: '保存成功!',
  30. })
  31. wx.removeStorageSync('cacheImage')
  32. },
  33. complete: e => {
  34. console.log(e)
  35. }
  36. })
  37. }
  38. }
  39. }
  40. })
  41. },
  42. takeBill() {
  43. wx.redirectTo({
  44. url: '/pages/takeBill/takeBill',
  45. })
  46. },
  47. takePhoto(e) {
  48. let {
  49. mediatype
  50. } = e.currentTarget.dataset
  51. wx.getLocation({
  52. type: 'gcj02',
  53. isHighAccuracy: true,
  54. success: e => {
  55. let {
  56. latitude,
  57. longitude
  58. } = e
  59. console.log("获取定位成功!", e)
  60. this.data.latitude = latitude
  61. this.data.longitude = longitude
  62. wx.setStorageSync('latitude', latitude)
  63. wx.setStorageSync('longitude', longitude)
  64. wx.chooseMedia({
  65. mediaType: ["image"],
  66. sourceType: ["camera"],
  67. success: e => {
  68. console.log("获取媒体成功!", e)
  69. let src = e.tempFiles[0].tempFilePath
  70. if (e.type == "video") {
  71. wx.showLoading({
  72. title: '正在压缩...',
  73. })
  74. wx.compressVideo({
  75. src,
  76. quality: "high",
  77. bitrate: "",
  78. fps: "",
  79. resolution: "",
  80. success: async e => {
  81. if (wx.getStorageSync('userName')) {
  82. wx.showLoading({
  83. title: '正在上传...',
  84. })
  85. let res = await uploadFile(e.tempFilePath, {
  86. type: 4,
  87. userId: wx.getStorageSync('userId'),
  88. location: `${this.data.longitude},${this.data.latitude}`
  89. })
  90. if (res.status == 0) {
  91. console.log(res)
  92. wx.showToast({
  93. title: res.msg
  94. })
  95. wx.redirectTo({
  96. url: '/pages/takePhoto/success/success',
  97. })
  98. } else {
  99. wx.showToast({
  100. title: res.msg
  101. })
  102. }
  103. } else {
  104. // 新用户视频
  105. wx.hideLoading({
  106. success: (res) => {},
  107. })
  108. console.log("新用户视频", e)
  109. wx.setStorageSync('type', 2)
  110. wx.setStorageSync('file', e.tempFilePath)
  111. wx.redirectTo({
  112. url: `/pages/newCachePage/newCachePage`,
  113. })
  114. }
  115. },
  116. fail: e => {
  117. console.log(e)
  118. }
  119. })
  120. } else {
  121. wx.compressImage({
  122. src,
  123. quality: 80, // 压缩质量
  124. success: async e => {
  125. console.log("图片压缩成功!", e)
  126. wx.hideLoading({
  127. success: (res) => {},
  128. })
  129. if (wx.getStorageSync('userName')) {
  130. wx.showLoading({
  131. title: '正在上传...',
  132. })
  133. let postData = {
  134. type: mediatype,
  135. userId: wx.getStorageSync('userId'),
  136. }
  137. if (mediatype == 3) {
  138. postData.location = `${this.data.longitude},${this.data.latitude}`
  139. } else {
  140. postData.location = ''
  141. }
  142. let res = await uploadFile(e.tempFilePath, postData)
  143. console.log("上传结束", res)
  144. if (res.status == 0) {
  145. wx.showToast({
  146. title: res.msg
  147. })
  148. wx.setStorageSync('shareImageUrl', res.result.viewUrl)
  149. console.log(wx.getStorageSync('shareImageUrl'))
  150. wx.downloadFile({
  151. url: res.result.viewUrl,
  152. success: e => {
  153. console.log("下载调用!", e)
  154. wx.setStorageSync('cacheImage', e.tempFilePath)
  155. if (mediatype == 3) {
  156. wx.saveImageToPhotosAlbum({
  157. filePath: e.tempFilePath,
  158. success: e => {
  159. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  160. wx.showToast({
  161. title: '保存成功!',
  162. })
  163. wx.removeStorageSync('cacheImage')
  164. }
  165. },
  166. fail: e => {
  167. console.log("失败4", e)
  168. this.setData({
  169. authModal: true,
  170. modalText: "文件存储"
  171. })
  172. }
  173. })
  174. }
  175. wx.redirectTo({
  176. url: '/pages/takePhoto/success/success',
  177. })
  178. },
  179. fail: e => {
  180. console.log("失败3", e)
  181. }
  182. })
  183. } else {
  184. wx.showToast({
  185. title: res.msg
  186. })
  187. }
  188. } else {
  189. // 新用户图片
  190. console.log("新用户图片", e)
  191. wx.setStorageSync('type', 1)
  192. wx.setStorageSync('file', e.tempFilePath)
  193. wx.redirectTo({
  194. url: `/pages/newCachePage/newCachePage`,
  195. })
  196. }
  197. },
  198. fail: e => {
  199. console.log("失败2", e)
  200. }
  201. })
  202. }
  203. },
  204. fail: e => {
  205. console.log("失败1", e)
  206. }
  207. })
  208. },
  209. fail: e => {
  210. this.setData({
  211. authModal: true,
  212. modalText: "位置信息"
  213. })
  214. }
  215. })
  216. },
  217. onLoad() {
  218. let userName = wx.getStorageSync('userName')
  219. let userId = wx.getStorageSync('userId')
  220. let phone = wx.getStorageSync('phone')
  221. let shipId = wx.getStorageSync('shipId')
  222. this.setData({
  223. userName,
  224. phone,
  225. userId,
  226. shipId
  227. })
  228. }
  229. })