takePhoto.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. locationModal: false
  13. },
  14. openSetting() {
  15. this.setData({
  16. locationModal: false
  17. })
  18. wx.openSetting({})
  19. },
  20. takePhoto() {
  21. wx.getLocation({
  22. success: e => {
  23. let {
  24. latitude,
  25. longitude
  26. } = e
  27. this.data.latitude = latitude
  28. this.data.longitude = longitude
  29. wx.setStorageSync('latitude', latitude)
  30. wx.setStorageSync('longitude', longitude)
  31. wx.chooseMedia({
  32. // sourceType: ["camera", "album"],
  33. sourceType: ["camera"],
  34. success: e => {
  35. let src = e.tempFiles[0].tempFilePath
  36. if (e.type == "video") {
  37. wx.showLoading({
  38. title: '正在压缩...',
  39. })
  40. wx.compressVideo({
  41. src,
  42. quality: "high",
  43. bitrate: "",
  44. fps: "",
  45. resolution: "",
  46. success: async e => {
  47. if (wx.getStorageSync('userName')) {
  48. wx.showLoading({
  49. title: '正在上传...',
  50. })
  51. let res = await uploadFile(e.tempFilePath, {
  52. type: 4,
  53. userId: wx.getStorageSync('userId'),
  54. location: `${this.data.longitude},${this.data.latitude}`
  55. })
  56. if (res.status == 0) {
  57. wx.showToast({
  58. title: res.msg
  59. })
  60. wx.navigateTo({
  61. url: '/pages/takePhoto/success/success',
  62. })
  63. } else {
  64. wx.showToast({
  65. title: res.msg
  66. })
  67. }
  68. } else {
  69. // 新用户视频
  70. wx.hideLoading({
  71. success: (res) => {},
  72. })
  73. console.log("新用户视频", e)
  74. wx.setStorageSync('type', 2)
  75. wx.setStorageSync('file', e.tempFilePath)
  76. wx.navigateTo({
  77. url: `/pages/cachePage/cachePage`,
  78. })
  79. }
  80. },
  81. fail: e => {
  82. console.log(e)
  83. }
  84. })
  85. } else {
  86. wx.compressImage({
  87. src,
  88. quality: 80, // 压缩质量
  89. success: async e => {
  90. wx.hideLoading({
  91. success: (res) => {},
  92. })
  93. if (wx.getStorageSync('userName')) {
  94. wx.showLoading({
  95. title: '正在上传...',
  96. })
  97. let res = await uploadFile(e.tempFilePath, {
  98. type: 3,
  99. userId: wx.getStorageSync('userId'),
  100. location: `${this.data.longitude},${this.data.latitude}`
  101. })
  102. if (res.status == 0) {
  103. wx.showToast({
  104. title: res.msg
  105. })
  106. wx.navigateTo({
  107. url: '/pages/takePhoto/success/success',
  108. })
  109. } else {
  110. wx.showToast({
  111. title: res.msg
  112. })
  113. }
  114. } else {
  115. // 新用户图片
  116. console.log("新用户图片", e)
  117. wx.setStorageSync('type', 1)
  118. wx.setStorageSync('file', e.tempFilePath)
  119. wx.navigateTo({
  120. url: `/pages/cachePage/cachePage`,
  121. })
  122. }
  123. },
  124. fail: e => {
  125. console.log(e)
  126. }
  127. })
  128. }
  129. }
  130. })
  131. },
  132. fail: e => {
  133. this.setData({
  134. locationModal: true
  135. })
  136. }
  137. })
  138. },
  139. onLoad() {
  140. let userName = wx.getStorageSync('userName')
  141. let phone = wx.getStorageSync('phone')
  142. this.setData({
  143. userName,
  144. phone
  145. })
  146. }
  147. })