class.mini.login.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {service as dibootApi} from '@/utils/dibootApi.js'
  2. import Member from './class.member.js'
  3. export default class MiniLogin extends Member {
  4. constructor() {
  5. super()
  6. this.$path = null
  7. this.bindWx = false
  8. }
  9. setUrlPath(path){
  10. this.$path = path
  11. return this
  12. }
  13. setBindWx(bindWx){
  14. this.bindWx = bindWx
  15. return this
  16. }
  17. /**
  18. * 微信小程序授权用户信息
  19. */
  20. go() {
  21. let _this = this
  22. // 微信登陆授权
  23. wx.getUserProfile({
  24. desc : '用于完善用户资料',
  25. lang : 'zh_CN',
  26. success : function( res ){
  27. uni.login({
  28. provider: 'weixin',
  29. success: function (loginRes) {
  30. // 微信登陆
  31. _this.miniAuthLogin(loginRes.code, res.userInfo)
  32. }
  33. });
  34. },
  35. fail : function( res ){
  36. console.log('wx.getUserProfile=>获取用户失败', res);
  37. }
  38. })
  39. }
  40. /**
  41. * 微信小程序登陆
  42. * @param {Object} code 微信code,
  43. * @param {Object} encodePhone 加密的手机号信息
  44. */
  45. async miniAuthLogin(code, infoRes) {
  46. let msg = this.bindWx ? '绑定中' : '登录中'
  47. let msgFail = this.bindWx ? '绑定失败' : '登陆失败'
  48. uni.showLoading({
  49. title: msg
  50. });
  51. try {
  52. // 调用登陆接口
  53. const res = await dibootApi.get('/wx-ma/auth/getSessionInfo', {params: {code}})
  54. if(res.code === 0) {
  55. const {sessionKey, openid} = res.data
  56. // 存储sessionKey
  57. uni.setStorageSync("sessionKey", sessionKey)
  58. if(this.bindWx) {
  59. this.bindWxMa({sessionKey, openid, ...infoRes})
  60. } else {
  61. // 存储用户信息
  62. this.wxStorageUserInfo({sessionKey, openid, ...infoRes})
  63. }
  64. } else {
  65. this.$tip ? this.$tip.show({ title: msgFail, type: 'error', duration: '3000'}) : uni.showToast({ title: res.msg, icon: 'error'})
  66. uni.hideLoading()
  67. }
  68. } catch(e) {
  69. console.log(e)
  70. this.$tip ? this.$tip.show({ title: e.errMsg, type: 'error', duration: '3000'}) : uni.showToast({ title: '网络异常', icon: 'error'})
  71. uni.hideLoading()
  72. }
  73. }
  74. /**
  75. * 存储用户信息
  76. *
  77. * @param {Object} data
  78. * sessionKey, openid, signature, rawData, encryptedData, iv
  79. */
  80. async wxStorageUserInfo(data) {
  81. const saveRes = await dibootApi.post('/wx-ma/auth/getAndSaveWxMember', data)
  82. if(saveRes.code === 0 ) {
  83. uni.setStorageSync("member", JSON.stringify(saveRes.data))
  84. // 调用iam登陆接口
  85. const loginForm = {authAccount: saveRes.data.openid, authType: 'WX_MP'}
  86. const loginRes = await dibootApi.post('/wx-ma/auth/login', loginForm)
  87. if(loginRes.code === 0) {
  88. uni.setStorageSync("authtoken", loginRes.data)
  89. this.$tip ? this.$tip.show({ title: '登录成功', type: 'success' }) : uni.showToast({ title: '登录成功', icon: 'success' })
  90. uni.hideLoading()
  91. // 跳转到首页
  92. uni.switchTab({
  93. url: this.$path
  94. })
  95. } else {
  96. this.$tip ? this.$tip.show({ title: '登录失败', type: 'error', duration: '3000'}) : uni.showToast({ title: '登录失败', icon: 'error'})
  97. uni.hideLoading()
  98. }
  99. } else {
  100. this.$tip ? this.$tip.show({ title: saveRes.msg, type: 'error', duration: '3000'}) : uni.showToast({ title: saveRes.msg, icon: 'error'})
  101. uni.hideLoading()
  102. }
  103. }
  104. /**
  105. * 绑定微信
  106. * @param {Object} data
  107. */
  108. async bindWxMa(data) {
  109. const bindRes = await dibootApi.post('/wx-ma/bindMa', data)
  110. if(bindRes.code === 0 ) {
  111. uni.setStorageSync("member", JSON.stringify(bindRes.data))
  112. this.$tip ? this.$tip.show({ title: '绑定成功', type: 'success' }) : uni.showToast({ title: '绑定成功', icon: 'success' })
  113. uni.hideLoading()
  114. // 跳转到首页
  115. uni.reLaunch({
  116. url: this.$path
  117. })
  118. } else {
  119. this.$tip ? this.$tip.show({ title: bindRes.msg, type: 'error', duration: '3000'}) : uni.showToast({ title: bindRes.msg, icon: 'error'})
  120. uni.hideLoading()
  121. }
  122. }
  123. }