newCachePage.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // pages/newCachePage/newCahePage.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. this.setData({
  67. agreeModal: true
  68. })
  69. },
  70. async getPhoneNumber(e) {
  71. try {
  72. if (e.detail.errMsg == "getPhoneNumber:ok") {
  73. wx.showLoading({
  74. title: '正在登录...',
  75. mask: true
  76. })
  77. let session_key = wx.getStorageSync('session_key')
  78. let {
  79. result
  80. } = await cloudApi("getWxPhoneNumber", {
  81. ...e.detail,
  82. session_key
  83. })
  84. let {
  85. phone,
  86. } = result
  87. if (phone) {
  88. wx.setStorageSync('phone', phone)
  89. let {
  90. shipName,
  91. shipMmsi
  92. } = this.data
  93. try {
  94. let res = await uploadFile(wx.getStorageSync('file'), {
  95. openId: wx.getStorageSync('openId'),
  96. phone,
  97. shipName,
  98. shipMmsi,
  99. type: wx.getStorageSync('type'),
  100. location: `${this.data.longitude},${this.data.latitude}`
  101. }, 1)
  102. if (res.status == 0) {
  103. wx.setStorageSync('shareImageUrl', res.result.mediaInfo.viewUrl)
  104. let {
  105. shipInfo,
  106. userInfo
  107. } = res.result
  108. let data = {
  109. ...shipInfo,
  110. ...userInfo
  111. }
  112. Object.keys(data).forEach(function (key) {
  113. wx.setStorageSync(key, data[key])
  114. })
  115. wx.showToast({
  116. title: res.msg,
  117. })
  118. wx.downloadFile({
  119. url: res.result.mediaInfo.viewUrl,
  120. success: e => {
  121. console.log("下载调用!", e)
  122. wx.setStorageSync('cacheImage', e.tempFilePath)
  123. wx.saveImageToPhotosAlbum({
  124. filePath: e.tempFilePath,
  125. success: e => {
  126. console.log(5, e)
  127. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  128. wx.showToast({
  129. title: '保存成功!',
  130. })
  131. wx.removeStorageSync('cacheImage')
  132. }
  133. },
  134. fail: async e => {
  135. console.log("6", e)
  136. wx.hideLoading({})
  137. wx.showToast({
  138. title: '保存失败!',
  139. duration: 5000
  140. })
  141. await cloudApi('sendError', {
  142. e,
  143. flag: 4,
  144. msg: "保存失败"
  145. })
  146. this.setData({
  147. authModal: true,
  148. modalText: "文件存储"
  149. })
  150. }
  151. })
  152. wx.navigateTo({
  153. url: '/pages/takePhoto/success/success',
  154. })
  155. },
  156. fail: async e => {
  157. wx.hideLoading({})
  158. wx.showToast({
  159. title: '下载失败!',
  160. duration: 5000
  161. })
  162. await cloudApi('sendError', {
  163. e,
  164. msg: "下载失败",
  165. flag: 3
  166. })
  167. }
  168. })
  169. } else {
  170. wx.hideLoading({})
  171. await cloudApi('sendError', {
  172. res,
  173. flag: 2
  174. })
  175. wx.showToast({
  176. title: res.msg,
  177. icon: "error"
  178. })
  179. }
  180. } catch (error) {
  181. await cloudApi('sendError', {
  182. error,
  183. flag: 1
  184. })
  185. }
  186. } else {
  187. wx.hideLoading({})
  188. wx.showToast({
  189. title: '获取手机号失败',
  190. duration: 5000
  191. })
  192. await cloudApi('sendError', {
  193. msg: "获取手机号失败"
  194. })
  195. }
  196. } else {
  197. wx.showToast({
  198. title: '请授权以登录',
  199. icon: "error"
  200. })
  201. await cloudApi('sendError', {
  202. msg: "未授权手机号"
  203. })
  204. }
  205. } catch (error) {
  206. await cloudApi('sendError', {
  207. msg: "全局trycatch",
  208. error
  209. })
  210. }
  211. },
  212. /**
  213. * 生命周期函数--监听页面加载
  214. */
  215. onLoad: function (options) {
  216. this.setData({
  217. type: wx.getStorageSync('type'),
  218. file: wx.getStorageSync('file'),
  219. latitude: wx.getStorageSync('latitude'),
  220. longitude: wx.getStorageSync('longitude')
  221. })
  222. },
  223. onShareAppMessage: function () {
  224. }
  225. })