cachePage.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // pages/cachePage/cachePage.js
  2. import cloudApi from "../../apis/cloudApi"
  3. import {
  4. postApi
  5. } from "../../apis/api"
  6. import {
  7. uploadFile
  8. } from "../../utils/upload"
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. type: "",
  15. file: "",
  16. latitude: "",
  17. longitude: "",
  18. shipName: "",
  19. shipMmsi: "",
  20. agreeModal: false,
  21. agreeText: false
  22. },
  23. goBack() {
  24. wx.redirectTo({
  25. url: '/pages/index/index',
  26. })
  27. },
  28. checkout() {
  29. if (!this.data.shipName) {
  30. wx.showToast({
  31. title: '请输入船名!',
  32. icon: "error"
  33. })
  34. return
  35. }
  36. if (!this.data.shipMmsi) {
  37. wx.showToast({
  38. title: '请输入MMSI!',
  39. icon: "error"
  40. })
  41. return
  42. }
  43. return true
  44. },
  45. agree() {
  46. this.setData({
  47. agreeText: false,
  48. })
  49. },
  50. showAgeeeText() {
  51. this.setData({
  52. agreeText: true
  53. })
  54. },
  55. hideAgreeText() {
  56. this.setData({
  57. agreeText: false
  58. })
  59. },
  60. hideAgreeModal() {
  61. this.setData({
  62. agreeModal: false
  63. })
  64. },
  65. showAgreeModal() {
  66. // if (!this.checkout()) return
  67. this.setData({
  68. agreeModal: true
  69. })
  70. },
  71. async getPhoneNumber(e) {
  72. try {
  73. if (e.detail.errMsg == "getPhoneNumber:ok") {
  74. wx.showLoading({
  75. title: '正在登录...',
  76. mask: true
  77. })
  78. let session_key = wx.getStorageSync('session_key')
  79. let {
  80. result
  81. } = await cloudApi("getWxPhoneNumber", {
  82. ...e.detail,
  83. session_key
  84. })
  85. let {
  86. phone,
  87. } = result
  88. if (phone) {
  89. wx.setStorageSync('phone', phone)
  90. let {
  91. shipName,
  92. shipMmsi
  93. } = this.data
  94. try {
  95. let res = await uploadFile(wx.getStorageSync('file'), {
  96. openId: wx.getStorageSync('openId'),
  97. phone,
  98. shipName,
  99. shipMmsi,
  100. type: wx.getStorageSync('type'),
  101. location: `${this.data.longitude},${this.data.latitude}`
  102. }, 1)
  103. if (res.status == 0) {
  104. wx.setStorageSync('shareImageUrl', res.result.mediaInfo.viewUrl)
  105. let {
  106. shipInfo,
  107. userInfo
  108. } = res.result
  109. let data = {
  110. ...shipInfo,
  111. ...userInfo
  112. }
  113. Object.keys(data).forEach(function (key) {
  114. wx.setStorageSync(key, data[key])
  115. })
  116. wx.showToast({
  117. title: res.msg,
  118. })
  119. wx.downloadFile({
  120. url: res.result.mediaInfo.viewUrl,
  121. success: e => {
  122. console.log("下载调用!", e)
  123. wx.setStorageSync('cacheImage', e.tempFilePath)
  124. wx.saveImageToPhotosAlbum({
  125. filePath: e.tempFilePath,
  126. success: e => {
  127. console.log(5, e)
  128. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  129. wx.showToast({
  130. title: '保存成功!',
  131. })
  132. wx.removeStorageSync('cacheImage')
  133. }
  134. },
  135. fail: async e => {
  136. console.log("6", e)
  137. wx.hideLoading({})
  138. wx.showToast({
  139. title: '保存失败!',
  140. duration: 5000
  141. })
  142. await cloudApi('sendError', {
  143. e,
  144. flag: 4,
  145. msg: "保存失败"
  146. })
  147. this.setData({
  148. authModal: true,
  149. modalText: "文件存储"
  150. })
  151. }
  152. })
  153. wx.navigateTo({
  154. url: '/pages/takePhoto/success/success',
  155. })
  156. },
  157. fail: async e => {
  158. wx.hideLoading({})
  159. wx.showToast({
  160. title: '下载失败!',
  161. duration: 5000
  162. })
  163. await cloudApi('sendError', {
  164. e,
  165. msg: "下载失败",
  166. flag: 3
  167. })
  168. }
  169. })
  170. } else {
  171. wx.hideLoading({})
  172. await cloudApi('sendError', {
  173. res,
  174. flag: 2
  175. })
  176. wx.showToast({
  177. title: res.msg,
  178. icon: "error"
  179. })
  180. }
  181. } catch (error) {
  182. await cloudApi('sendError', {
  183. error,
  184. flag: 1
  185. })
  186. }
  187. } else {
  188. wx.hideLoading({})
  189. wx.showToast({
  190. title: '获取手机号失败',
  191. duration: 5000
  192. })
  193. await cloudApi('sendError', {
  194. msg: "获取手机号失败"
  195. })
  196. }
  197. } else {
  198. wx.showToast({
  199. title: '请授权以登录',
  200. icon: "error"
  201. })
  202. await cloudApi('sendError', {
  203. msg: "未授权手机号"
  204. })
  205. }
  206. } catch (error) {
  207. await cloudApi('sendError', {
  208. msg: "全局trycatch",
  209. error
  210. })
  211. }
  212. },
  213. /**
  214. * 生命周期函数--监听页面加载
  215. */
  216. onLoad: function (options) {
  217. this.setData({
  218. type: wx.getStorageSync('type'),
  219. file: wx.getStorageSync('file'),
  220. latitude: wx.getStorageSync('latitude'),
  221. longitude: wx.getStorageSync('longitude')
  222. })
  223. },
  224. onShareAppMessage: function () {
  225. }
  226. })