newCachePage.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. isWxRegister: false
  23. },
  24. goBack() {
  25. wx.redirectTo({
  26. url: '/pages/index/index',
  27. })
  28. },
  29. async register() {
  30. if (!this.checkout()) return
  31. let res = await postApi('/user/wx/register', {
  32. userId: wx.getStorageSync('userId'),
  33. shipName: this.data.shipName,
  34. shipMmsi: this.data.shipMmsi,
  35. })
  36. console.log(res)
  37. if (res.data.status == 0) {
  38. let {
  39. shipInfo,
  40. userInfo
  41. } = res.data.result
  42. let data = {
  43. ...shipInfo,
  44. ...userInfo
  45. }
  46. Object.keys(data).forEach(function (key) {
  47. wx.setStorageSync(key, data[key])
  48. })
  49. wx.compressImage({
  50. src: wx.getStorageSync('file'),
  51. quality: 80, // 压缩质量
  52. success: async e => {
  53. console.log("图片压缩成功!", e)
  54. wx.hideLoading({
  55. success: (res) => {},
  56. })
  57. wx.showLoading({
  58. title: '正在上传...',
  59. })
  60. let res = await uploadFile(e.tempFilePath, {
  61. type: 3,
  62. userId: wx.getStorageSync('userId'),
  63. location: `${this.data.longitude},${this.data.latitude}`
  64. })
  65. console.log("上传结束", res)
  66. if (res.status == 0) {
  67. wx.showToast({
  68. title: res.msg
  69. })
  70. wx.setStorageSync('shareImageUrl', res.result.viewUrl)
  71. console.log(wx.getStorageSync('shareImageUrl'))
  72. wx.downloadFile({
  73. url: res.result.viewUrl,
  74. success: e => {
  75. console.log("下载调用!", e)
  76. wx.setStorageSync('cacheImage', e.tempFilePath)
  77. wx.saveImageToPhotosAlbum({
  78. filePath: e.tempFilePath,
  79. success: e => {
  80. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  81. wx.showToast({
  82. title: '保存成功!',
  83. })
  84. wx.removeStorageSync('cacheImage')
  85. }
  86. },
  87. fail: e => {
  88. console.log("失败44", e)
  89. this.setData({
  90. authModal: true,
  91. modalText: "文件存储"
  92. })
  93. }
  94. })
  95. wx.navigateTo({
  96. url: '/pages/takePhoto/success/success',
  97. })
  98. },
  99. fail: e => {
  100. console.log("失败3", e)
  101. }
  102. })
  103. } else {
  104. wx.showToast({
  105. title: res.msg
  106. })
  107. }
  108. },
  109. fail: e => {
  110. console.log("失败22", e)
  111. }
  112. })
  113. } else {
  114. }
  115. },
  116. checkout() {
  117. if (!this.data.shipName) {
  118. wx.showToast({
  119. title: '请输入船名!',
  120. icon: "error"
  121. })
  122. return
  123. }
  124. if (!this.data.shipMmsi) {
  125. wx.showToast({
  126. title: '请输入MMSI!',
  127. icon: "error"
  128. })
  129. return
  130. }
  131. return true
  132. },
  133. agree() {
  134. this.setData({
  135. agreeText: false,
  136. })
  137. },
  138. showAgeeeText() {
  139. this.setData({
  140. agreeText: true
  141. })
  142. },
  143. hideAgreeText() {
  144. this.setData({
  145. agreeText: false
  146. })
  147. },
  148. hideAgreeModal() {
  149. this.setData({
  150. agreeModal: false
  151. })
  152. },
  153. showAgreeModal() {
  154. this.setData({
  155. agreeModal: true
  156. })
  157. },
  158. async getPhoneNumber(e) {
  159. try {
  160. if (e.detail.errMsg == "getPhoneNumber:ok") {
  161. wx.showLoading({
  162. title: '正在登录...',
  163. mask: true
  164. })
  165. let session_key = wx.getStorageSync('session_key')
  166. let {
  167. result
  168. } = await cloudApi("getWxPhoneNumber", {
  169. ...e.detail,
  170. session_key
  171. })
  172. let {
  173. phone,
  174. } = result
  175. if (phone) {
  176. wx.setStorageSync('phone', phone)
  177. try {
  178. let res = await postApi('/user/wx/login', {
  179. openId: wx.getStorageSync('openId'),
  180. phone
  181. })
  182. console.log(res)
  183. if (res.data.status == 0) {
  184. let {
  185. shipInfo,
  186. userInfo
  187. } = res.data.result
  188. let data = {
  189. ...shipInfo,
  190. ...userInfo
  191. }
  192. Object.keys(data).forEach(function (key) {
  193. wx.setStorageSync(key, data[key])
  194. })
  195. wx.compressImage({
  196. src: wx.getStorageSync('file'),
  197. quality: 80, // 压缩质量
  198. success: async e => {
  199. console.log("图片压缩成功!", e)
  200. wx.hideLoading({
  201. success: (res) => {},
  202. })
  203. wx.showLoading({
  204. title: '正在上传...',
  205. })
  206. let res = await uploadFile(e.tempFilePath, {
  207. type: 3,
  208. userId: wx.getStorageSync('userId'),
  209. location: `${this.data.longitude},${this.data.latitude}`
  210. })
  211. console.log("上传结束", res)
  212. if (res.status == 0) {
  213. wx.showToast({
  214. title: res.msg
  215. })
  216. wx.setStorageSync('shareImageUrl', res.result.viewUrl)
  217. console.log(wx.getStorageSync('shareImageUrl'))
  218. wx.downloadFile({
  219. url: res.result.viewUrl,
  220. success: e => {
  221. console.log("下载调用!", e)
  222. wx.setStorageSync('cacheImage', e.tempFilePath)
  223. wx.saveImageToPhotosAlbum({
  224. filePath: e.tempFilePath,
  225. success: e => {
  226. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  227. wx.showToast({
  228. title: '保存成功!',
  229. })
  230. wx.removeStorageSync('cacheImage')
  231. }
  232. },
  233. fail: e => {
  234. console.log("失败4", e)
  235. this.setData({
  236. authModal: true,
  237. modalText: "文件存储"
  238. })
  239. }
  240. })
  241. wx.navigateTo({
  242. url: '/pages/takePhoto/success/success',
  243. })
  244. },
  245. fail: e => {
  246. console.log("失败33", e)
  247. }
  248. })
  249. } else {
  250. wx.showToast({
  251. title: res.msg
  252. })
  253. }
  254. },
  255. fail: e => {
  256. console.log("失败2", e)
  257. }
  258. })
  259. } else {
  260. let {
  261. userInfo
  262. } = res.data.result
  263. let data = {
  264. ...userInfo
  265. }
  266. Object.keys(data).forEach(function (key) {
  267. wx.setStorageSync(key, data[key])
  268. })
  269. this.setData({
  270. isWxRegister: true,
  271. agreeModal: false
  272. })
  273. }
  274. } catch (error) {
  275. console.log(error)
  276. } finally {
  277. wx.hideLoading({
  278. success: (res) => {},
  279. })
  280. }
  281. } else {
  282. wx.hideLoading({})
  283. wx.showToast({
  284. title: '获取手机号失败',
  285. duration: 5000
  286. })
  287. await cloudApi('sendError', {
  288. msg: "获取手机号失败"
  289. })
  290. }
  291. } else {
  292. wx.showToast({
  293. title: '请授权以登录',
  294. icon: "error"
  295. })
  296. await cloudApi('sendError', {
  297. msg: "未授权手机号"
  298. })
  299. }
  300. } catch (error) {
  301. await cloudApi('sendError', {
  302. msg: "全局trycatch",
  303. error
  304. })
  305. }
  306. },
  307. /**
  308. * 生命周期函数--监听页面加载
  309. */
  310. onLoad: function (options) {
  311. this.setData({
  312. type: wx.getStorageSync('type'),
  313. file: wx.getStorageSync('file'),
  314. latitude: wx.getStorageSync('latitude'),
  315. longitude: wx.getStorageSync('longitude')
  316. })
  317. },
  318. onShareAppMessage: function () {
  319. }
  320. })