auth.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // pages/components/auth/auth.js
  2. const api = require('../../../config');
  3. const getReq = require('../../../config').getReq;
  4. let app = getApp();
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. },
  11. /**
  12. * 组件的初始数据
  13. */
  14. data: {
  15. phone_iv: '',
  16. phone_encryptedData: '',
  17. userInfoE:{},
  18. showAuthName: true,
  19. showAuthPhone: false,
  20. },
  21. lifetimes: {
  22. attached: function () {
  23. if(!app.globalData.userInfo) {
  24. this.setData({ showAuthName: true, showAuthPhone: false })
  25. }
  26. else if(!app.globalData.hasmobile) {
  27. this.setData({ showAuthName: false, showAuthPhone: true })
  28. }
  29. },
  30. detached: function () {
  31. },
  32. },
  33. /**
  34. * 组件的方法列表
  35. */
  36. methods: {
  37. userPhoneHandler: function(e) {
  38. if (e.detail.errMsg == "getPhoneNumber:ok"){
  39. this.phone_encryptedData = e.detail.encryptedData
  40. this.phone_iv = e.detail.iv
  41. this.wxauthen(this.userInfoE)
  42. }
  43. },
  44. userInfoHandler(e)
  45. {
  46. if (e.detail.errMsg == "getUserInfo:ok")
  47. {
  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
  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. })