takePhoto.js 6.2 KB

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