auth.js 2.2 KB

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