takePhoto.js 6.4 KB

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