newCachePage.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 {
  166. data
  167. } = await postApi('/wx/getPhoneNumber', {
  168. code: e.detail.code
  169. })
  170. let {
  171. phoneNumber: phone
  172. } = data.result
  173. if (phone) {
  174. wx.setStorageSync('phone', phone)
  175. try {
  176. let res = await postApi('/user/wx/login', {
  177. openId: wx.getStorageSync('openId'),
  178. phone: 18988988889
  179. })
  180. console.log(res)
  181. if (res.data.status == 0) {
  182. let {
  183. shipInfo,
  184. userInfo
  185. } = res.data.result
  186. let data = {
  187. ...shipInfo,
  188. ...userInfo
  189. }
  190. Object.keys(data).forEach(function (key) {
  191. wx.setStorageSync(key, data[key])
  192. })
  193. wx.compressImage({
  194. src: wx.getStorageSync('file'),
  195. quality: 80, // 压缩质量
  196. success: async e => {
  197. console.log("图片压缩成功!", e)
  198. wx.hideLoading({
  199. success: (res) => {},
  200. })
  201. wx.showLoading({
  202. title: '正在上传...',
  203. })
  204. let res = await uploadFile(e.tempFilePath, {
  205. type: 3,
  206. userId: wx.getStorageSync('userId'),
  207. location: `${this.data.longitude},${this.data.latitude}`
  208. })
  209. console.log("上传结束", res)
  210. if (res.status == 0) {
  211. wx.showToast({
  212. title: res.msg
  213. })
  214. wx.setStorageSync('shareImageUrl', res.result.viewUrl)
  215. console.log(wx.getStorageSync('shareImageUrl'))
  216. wx.downloadFile({
  217. url: res.result.viewUrl,
  218. success: e => {
  219. console.log("下载调用!", e)
  220. wx.setStorageSync('cacheImage', e.tempFilePath)
  221. wx.saveImageToPhotosAlbum({
  222. filePath: e.tempFilePath,
  223. success: e => {
  224. if (e.errMsg == "saveImageToPhotosAlbum:ok") {
  225. wx.showToast({
  226. title: '保存成功!',
  227. })
  228. wx.removeStorageSync('cacheImage')
  229. }
  230. },
  231. fail: e => {
  232. console.log("失败4", e)
  233. this.setData({
  234. authModal: true,
  235. modalText: "文件存储"
  236. })
  237. }
  238. })
  239. wx.navigateTo({
  240. url: '/pages/takePhoto/success/success',
  241. })
  242. },
  243. fail: e => {
  244. console.log("失败33", e)
  245. }
  246. })
  247. } else {
  248. wx.showToast({
  249. title: res.msg
  250. })
  251. }
  252. },
  253. fail: e => {
  254. console.log("失败2", e)
  255. }
  256. })
  257. } else {
  258. let {
  259. userInfo
  260. } = res.data.result
  261. let data = {
  262. ...userInfo
  263. }
  264. Object.keys(data).forEach(function (key) {
  265. wx.setStorageSync(key, data[key])
  266. })
  267. this.setData({
  268. isWxRegister: true,
  269. agreeModal: false
  270. })
  271. }
  272. } catch (error) {
  273. console.log(error)
  274. } finally {
  275. wx.hideLoading({
  276. success: (res) => {},
  277. })
  278. }
  279. } else {
  280. wx.hideLoading({})
  281. wx.showToast({
  282. title: '获取手机号失败',
  283. duration: 5000
  284. })
  285. }
  286. } else {
  287. wx.showToast({
  288. title: '请授权以登录',
  289. icon: "error"
  290. })
  291. }
  292. } catch (error) {
  293. }
  294. },
  295. /**
  296. * 生命周期函数--监听页面加载
  297. */
  298. onLoad: function (options) {
  299. this.setData({
  300. type: wx.getStorageSync('type'),
  301. file: wx.getStorageSync('file'),
  302. latitude: wx.getStorageSync('latitude'),
  303. longitude: wx.getStorageSync('longitude')
  304. })
  305. },
  306. onShareAppMessage: function () {
  307. }
  308. })