auth.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. console.log(e)
  28. let iv = e.detail.iv || ''
  29. let encryptedData = e.detail.encryptedData || ''
  30. if (iv) {
  31. //用户按了允许授权按钮
  32. this.decodePhoneCallback({
  33. iv: iv,
  34. encryptedData: encryptedData,
  35. }).then(data => {
  36. app.globalData.noPhone = false
  37. console.log('手机号解密:', data)
  38. updataStorageData('phone', data.data.hpUser.phone);
  39. wx.navigateBack()
  40. })
  41. } else {
  42. wx.setStorageSync('resumeUrl', '')
  43. //用户按了拒绝按钮
  44. wx.showModal({
  45. title: '警告',
  46. content: '未经授权会导致系统部分功能无法使用。',
  47. confirmText: '返回授权',
  48. showCancel: false,
  49. success: function(res) {
  50. if (res.confirm) {
  51. console.log('用户点击了“返回授权”')
  52. }
  53. }
  54. })
  55. }
  56. },
  57. // 手机号解密回调 返回成功失败
  58. decodePhoneCallback(params) {
  59. return new Promise((resolve, reject) => {
  60. decodePhone({
  61. 'encryptedData': encodeURIComponent(params.encryptedData),
  62. 'iv': encodeURIComponent(params.iv),
  63. 'user_id': app.globalData.userId,
  64. 'user_token': app.globalData.userToken
  65. }).then(data => {
  66. resolve(data)
  67. }).catch(data => {
  68. reject(false)
  69. })
  70. })
  71. }
  72. })