auth.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {
  2. imgServerUrl
  3. } from '../../config/config.js'
  4. import {
  5. decodePhone
  6. } from '../../services/wx.js'
  7. import {
  8. updataStorageData
  9. } from '../../utils/storage.js'
  10. var app = getApp()
  11. Page({
  12. data: {
  13. canIUse: wx.canIUse('button.open-type.getPhoneNumber'),
  14. imgServerUrl: imgServerUrl,
  15. isShow: true
  16. },
  17. onLoad: function(options) {
  18. app.globalData.noPhone = true
  19. var pages = getCurrentPages();
  20. var currPage = pages[pages.length - 1]; //当前页面
  21. var prevPage = pages[pages.length - 2]; //上一个页面
  22. prevPage.setData({
  23. noPhone: false
  24. })
  25. },
  26. getPhoneNumber: function(e) {
  27. let iv = e.detail.iv || ''
  28. let encryptedData = e.detail.encryptedData || ''
  29. if (iv) {
  30. //用户按了允许授权按钮
  31. this.decodePhoneCallback({
  32. iv: iv,
  33. encryptedData: encryptedData,
  34. }).then(data => {
  35. app.globalData.noPhone = false
  36. updataStorageData('phone', data.data.hpUser.phone);
  37. wx.navigateBack()
  38. })
  39. } else {
  40. //用户按了拒绝按钮
  41. wx.showModal({
  42. title: '警告',
  43. content: '未经授权会导致系统部分功能无法使用。',
  44. confirmText: '返回授权',
  45. showCancel: false,
  46. success: function(res) {
  47. if (res.confirm) {
  48. console.log('用户点击了“返回授权”')
  49. }
  50. }
  51. })
  52. }
  53. },
  54. // 手机号解密回调 返回成功失败
  55. decodePhoneCallback(params) {
  56. return new Promise((resolve, reject) => {
  57. decodePhone({
  58. 'encryptedData': encodeURIComponent(params.encryptedData),
  59. 'iv': encodeURIComponent(params.iv),
  60. 'user_id': app.globalData.userId,
  61. 'user_token': app.globalData.userToken
  62. }).then(data => {
  63. resolve(data)
  64. }).catch(data => {
  65. reject(false)
  66. })
  67. })
  68. }
  69. })