auth2.js 2.8 KB

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