takePhoto.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. takePhoto(e) {
  43. let {
  44. mediatype
  45. } = e.currentTarget.dataset
  46. wx.getLocation({
  47. type: 'gcj02',
  48. isHighAccuracy: true,
  49. success: e => {
  50. let {
  51. latitude,
  52. longitude
  53. } = e
  54. console.log("获取定位成功!", e)
  55. this.data.latitude = latitude
  56. this.data.longitude = longitude
  57. wx.setStorageSync('latitude', latitude)
  58. wx.setStorageSync('longitude', longitude)
  59. wx.chooseMedia({
  60. mediaType: ["image"],
  61. sourceType: ["camera"],
  62. success: e => {
  63. console.log("获取媒体成功!", e)
  64. let src = e.tempFiles[0].tempFilePath
  65. if (e.type == "video") {
  66. wx.showLoading({
  67. title: '正在压缩...',
  68. })
  69. wx.compressVideo({
  70. src,
  71. quality: "high",
  72. bitrate: "",
  73. fps: "",
  74. resolution: "",
  75. success: async e => {
  76. if (wx.getStorageSync('userName')) {
  77. wx.showLoading({
  78. title: '正在上传...',
  79. })
  80. let res = await uploadFile(e.tempFilePath, {
  81. type: 4,
  82. userId: wx.getStorageSync('userId'),
  83. location: `${this.data.longitude},${this.data.latitude}`
  84. })
  85. if (res.status == 0) {
  86. console.log(res)
  87. wx.showToast({
  88. title: res.msg
  89. })
  90. wx.redirectTo({
  91. url: '/pages/takePhoto/success/success',
  92. })
  93. } else {
  94. wx.showToast({
  95. title: res.msg
  96. })
  97. }
  98. } else {
  99. // 新用户视频
  100. wx.hideLoading({
  101. success: (res) => {},
  102. })
  103. console.log("新用户视频", e)
  104. wx.setStorageSync('type', 2)
  105. wx.setStorageSync('file', e.tempFilePath)
  106. wx.redirectTo({
  107. url: `/pages/newCachePage/newCachePage`,
  108. })
  109. }
  110. },
  111. fail: e => {
  112. console.log(e)
  113. }
  114. })
  115. } else {
  116. wx.compressImage({
  117. src,
  118. quality: 80, // 压缩质量
  119. success: async e => {
  120. console.log("图片压缩成功!", e)
  121. wx.hideLoading({
  122. success: (res) => {},
  123. })
  124. if (wx.getStorageSync('userName')) {
  125. wx.showLoading({
  126. title: '正在上传...',
  127. })
  128. let postData = {
  129. type: mediatype,
  130. userId: wx.getStorageSync('userId'),
  131. }
  132. if (mediatype == 3) {
  133. postData.location = `${this.data.longitude},${this.data.latitude}`
  134. } else {
  135. postData.location = ''
  136. }
  137. let res = await uploadFile(e.tempFilePath, postData)
  138. console.log("上传结束", res)
  139. if (res.status == 0) {
  140. wx.showToast({
  141. title: res.msg
  142. })
  143. wx.setStorageSync('shareImageUrl', res.result.viewUrl)
  144. console.log(wx.getStorageSync('shareImageUrl'))
  145. wx.downloadFile({
  146. url: res.result.viewUrl,
  147. success: e => {
  148. console.log("下载调用!", e)
  149. wx.setStorageSync('cacheImage', e.tempFilePath)
  150. if (mediatype == 3) {
  151. wx.saveImageToPhotosAlbum({
  152. filePath: e.tempFilePath,
  153. success: e => {
  154. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  155. wx.showToast({
  156. title: '保存成功!',
  157. })
  158. wx.removeStorageSync('cacheImage')
  159. }
  160. },
  161. fail: e => {
  162. console.log("失败4", e)
  163. this.setData({
  164. authModal: true,
  165. modalText: "文件存储"
  166. })
  167. }
  168. })
  169. }
  170. wx.redirectTo({
  171. url: '/pages/takePhoto/success/success',
  172. })
  173. },
  174. fail: e => {
  175. console.log("失败3", e)
  176. }
  177. })
  178. } else {
  179. wx.showToast({
  180. title: res.msg
  181. })
  182. }
  183. } else {
  184. // 新用户图片
  185. console.log("新用户图片", e)
  186. wx.setStorageSync('type', 1)
  187. wx.setStorageSync('file', e.tempFilePath)
  188. wx.redirectTo({
  189. url: `/pages/newCachePage/newCachePage`,
  190. })
  191. }
  192. },
  193. fail: e => {
  194. console.log("失败2", e)
  195. }
  196. })
  197. }
  198. },
  199. fail: e => {
  200. console.log("失败1", e)
  201. }
  202. })
  203. },
  204. fail: e => {
  205. this.setData({
  206. authModal: true,
  207. modalText: "位置信息"
  208. })
  209. }
  210. })
  211. },
  212. onLoad() {
  213. let userName = wx.getStorageSync('userName')
  214. let userId = wx.getStorageSync('userId')
  215. let phone = wx.getStorageSync('phone')
  216. let shipId = wx.getStorageSync('shipId')
  217. this.setData({
  218. userName,
  219. phone,
  220. userId,
  221. shipId
  222. })
  223. }
  224. })