newCachePage.js 8.4 KB

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