auth.js 2.4 KB

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