class.mp.login.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {service as dibootApi} from '@/utils/dibootApi.js'
  2. import constant from '@/utils/constant.js'
  3. import Member from './class.member.js'
  4. /**
  5. * 微信公众号登陆
  6. */
  7. export default class MpLogin extends Member {
  8. constructor() {
  9. super()
  10. }
  11. /**
  12. * 微信公众号登陆
  13. */
  14. redirect(bindWp = false) {
  15. let authtoken = uni.getStorageSync("authtoken")
  16. let redirect = uni.getStorageSync("redirect")
  17. if(bindWp) {
  18. // 获取是否绑定标记
  19. let bindWpTag = uni.getStorageSync("bindWpTag")
  20. if (!redirect && !bindWpTag) {
  21. this.buildOauth2Url(bindWp)
  22. }
  23. } else {
  24. if (!authtoken && !redirect) {
  25. this.buildOauth2Url(bindWp)
  26. }
  27. }
  28. }
  29. /**
  30. * 获取授权URL
  31. */
  32. async buildOauth2Url(bindWp) {
  33. const res = await dibootApi.get(`/wx-mp/auth/buildOAuthUrl?url=${encodeURIComponent(constant.frontIndex())}`)
  34. if (res.code === 0) {
  35. window.location.href = res.data
  36. uni.setStorageSync('redirect', true)
  37. if(bindWp) {
  38. uni.setStorageSync('bindWpTag', true)
  39. }
  40. } else {
  41. console.log('buildOauth2Url错误:', res)
  42. }
  43. }
  44. /**
  45. * 登陆
  46. */
  47. go() {
  48. return new Promise(async (reslove, reject) => {
  49. try {
  50. uni.showLoading({title: '登陆中'})
  51. const res = await dibootApi.get('/wx-mp/auth/apply', {
  52. params: {
  53. code: this.getQueryString4hash('code'),
  54. state: this.getQueryString4hash('state')
  55. }
  56. })
  57. if(res.code === 0) {
  58. uni.setStorageSync("authtoken", res.data)
  59. uni.removeStorageSync("redirect")
  60. let tipMsg = { title: '登录成功', type: 'success' }
  61. this.$tip ? this.$tip.show(tipMsg) : uni.showToast(tipMsg)
  62. reslove({code: true})
  63. } else {
  64. uni.removeStorageSync("redirect")
  65. this.$tip ? this.$tip.show({ title: res.msg, type: 'error', duration: '3000'}) : uni.showToast({ title: res.msg, icon: 'error'})
  66. }
  67. } catch(e) {
  68. console.log(e)
  69. uni.removeStorageSync("redirect")
  70. this.$tip ? this.$tip.show({ title: e.errMsg, type: 'error', duration: '3000'}) : uni.showToast({ title: '网络异常', icon: 'error'})
  71. } finally {
  72. uni.hideLoading()
  73. }
  74. })
  75. }
  76. /**
  77. * 绑定微信
  78. * @param {Object} data
  79. */
  80. async bindWxMp() {
  81. return new Promise(async (reslove, reject) => {
  82. try {
  83. uni.showLoading({title: '绑定中'})
  84. const res = await dibootApi.get('/wx-mp/bindMp', {
  85. params: {
  86. code: this.getQueryString4hash('code'),
  87. state: this.getQueryString4hash('state')
  88. }
  89. })
  90. if(res.code === 0) {
  91. uni.setStorageSync("member", JSON.stringify(res.data))
  92. let tipMsg = { title: '绑定成功', type: 'success' }
  93. this.$tip ? this.$tip.show(tipMsg) : uni.showToast(tipMsg)
  94. reslove({code: true})
  95. } else {
  96. this.$tip ? this.$tip.show({ title: res.msg, type: 'error', duration: '3000'}) : uni.showToast({ title: res.msg, icon: 'error'})
  97. reslove({code: true})
  98. }
  99. } catch(e) {
  100. console.log(e)
  101. this.$tip ? this.$tip.show({ title: e.errMsg, type: 'error', duration: '3000'}) : uni.showToast({ title: '网络异常', icon: 'error'})
  102. reslove({code: true})
  103. } finally {
  104. uni.removeStorageSync("redirect")
  105. uni.removeStorageSync("bindWpTag")
  106. uni.hideLoading()
  107. }
  108. })
  109. }
  110. /**
  111. * 获取code和state值
  112. * @param {Object} name
  113. */
  114. getQueryString4hash(name) {
  115. const href = window.location.href
  116. const arr1 = href.split('?')
  117. if (arr1 && arr1.length > 1) {
  118. const paramStr = arr1[1]
  119. const params = paramStr.split('&')
  120. for (let i = 0; i < params.length; i++) {
  121. const paramObjs = params[i].split('=')
  122. if (paramObjs && paramObjs.length > 1 && paramObjs[0] === name) {
  123. return paramObjs[1]
  124. }
  125. }
  126. }
  127. return ''
  128. }
  129. }