takePhoto.js 7.0 KB

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