auth.js 1.9 KB

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