cachePage.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. await cloudApi('sendError', {
  138. e,
  139. flag: 4,
  140. msg: "保存失败"
  141. })
  142. this.setData({
  143. authModal: true,
  144. modalText: "文件存储"
  145. })
  146. }
  147. })
  148. wx.navigateTo({
  149. url: '/pages/takePhoto/success/success',
  150. })
  151. },
  152. fail: async e => {
  153. await cloudApi('sendError', {
  154. e,
  155. msg: "下载失败",
  156. flag: 3
  157. })
  158. }
  159. })
  160. } else {
  161. await cloudApi('sendError', {
  162. res,
  163. flag: 2
  164. })
  165. wx.showToast({
  166. title: res.msg,
  167. icon: "error"
  168. })
  169. }
  170. } catch (error) {
  171. await cloudApi('sendError', {
  172. error,
  173. flag: 1
  174. })
  175. }
  176. } else {
  177. wx.showToast({
  178. title: '获取手机号失败',
  179. duration: 5000
  180. })
  181. await cloudApi('sendError', {
  182. msg: "获取手机号失败"
  183. })
  184. }
  185. } else {
  186. wx.showToast({
  187. title: '请授权以登录',
  188. icon: "error"
  189. })
  190. await cloudApi('sendError', {
  191. msg: "未授权手机号"
  192. })
  193. }
  194. } catch (error) {
  195. await cloudApi('sendError', {
  196. msg: "全局trycatch",
  197. error
  198. })
  199. }
  200. },
  201. /**
  202. * 生命周期函数--监听页面加载
  203. */
  204. onLoad: function (options) {
  205. this.setData({
  206. type: wx.getStorageSync('type'),
  207. file: wx.getStorageSync('file'),
  208. latitude: wx.getStorageSync('latitude'),
  209. longitude: wx.getStorageSync('longitude')
  210. })
  211. },
  212. onShareAppMessage: function () {
  213. }
  214. })