auth.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // pages/components/auth/auth.js
  2. const api = require('../../../config');
  3. const getReq = require('../../../config').getReq;
  4. const ald = require('../../../utils/ald-stat.js')
  5. let app = getApp();
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. },
  12. /**
  13. * 组件的初始数据
  14. */
  15. data: {
  16. phone_iv: '',
  17. phone_encryptedData: '',
  18. userInfoE: {},
  19. showAuthName: true,
  20. showAuthPhone: false,
  21. },
  22. lifetimes: {
  23. attached: function () {
  24. if (!app.globalData.userInfo) {
  25. this.setData({ showAuthName: true, showAuthPhone: false })
  26. }
  27. else if (!app.globalData.hasmobile) {
  28. this.setData({ showAuthName: false, showAuthPhone: true })
  29. }
  30. },
  31. detached: function () {
  32. },
  33. },
  34. /**
  35. * 组件的方法列表
  36. */
  37. methods: {
  38. userPhoneHandler: function (e) {
  39. if (e.detail.errMsg == "getPhoneNumber:ok") {
  40. wx.aldstat.sendEvent('wxAuthen', { phone: true })
  41. this.phone_encryptedData = e.detail.encryptedData
  42. this.phone_iv = e.detail.iv
  43. this.wxauthen(this.userInfoE)
  44. }
  45. },
  46. userInfoHandler(e) {
  47. if (e.detail.errMsg == "getUserInfo:ok") {
  48. app.globalData.userInfo = e.detail.userInfo;
  49. app.globalData.userInfo.nickname = e.detail.userInfo.nickName;
  50. this.userInfoE = e
  51. this.wxauthen(e)
  52. }
  53. },
  54. wxauthen: function (eInfo) {
  55. let userInfo;
  56. if (app.globalData.userId.unionid) {
  57. userInfo = Object.assign({}, app.globalData.userId, app.globalData.userInfo);
  58. }
  59. else {
  60. userInfo = eInfo.detail.userInfo;
  61. let { encryptedData, iv, signature } = eInfo.detail
  62. userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
  63. }
  64. let phoneInfo = { iv: this.phone_iv, encryptedData: this.phone_encryptedData }
  65. let params = {
  66. user_info: userInfo,
  67. phone_info: phoneInfo,
  68. act: "login",
  69. op: "wxauthen",
  70. relay_id: app.globalData.relay_id,
  71. channel: app.globalData.channel_num
  72. }
  73. let self = this;
  74. getReq(app, params, function (res) {
  75. if (res.code == 200) {
  76. let datas = res.datas;
  77. app.globalData.member_id = datas.member_id
  78. app.globalData.hasmobile = datas.hasmobile
  79. if (datas.hasmobile == false) {
  80. self.setData({ showAuthName: false, showAuthPhone: true })
  81. }
  82. else {
  83. self.triggerEvent('getAuth', {
  84. userInfo
  85. })
  86. }
  87. }
  88. else {
  89. app.relogin();
  90. wx.showToast({
  91. icon: 'none',
  92. title: '登陆遇到一点麻烦,请重试~~~',
  93. duration: 2000
  94. })
  95. }
  96. })
  97. }
  98. }
  99. })