takePhoto.js 6.9 KB

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