wxLogin.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import store from '@/store/index.js'
  2. import authorize from '@/common/config/authorize.js'
  3. import {
  4. userInfoLogin
  5. } from '@/services/user.service.js'
  6. // 根据微信的code获取用户登录状态:1已登录过 -1未登录过
  7. const wxLoginAuthorize = function() {
  8. authorize.getCode('weixin').then(wechatcode => {
  9. console.log('code:' + wechatcode)
  10. authorize.getUserInfo('weixin').then(wxResponse => {
  11. userInfoLogin({
  12. code: wechatcode
  13. }).then(response => {
  14. store.commit('updateStatus', response.data)
  15. store.commit('login', response.data)
  16. }).catch(error => {
  17. console.log(error)
  18. store.commit('logout', error.data)
  19. store.commit('updateStatus', error.data)
  20. })
  21. })
  22. })
  23. }
  24. const wxLoginQuick = function() { // 根据微信的code获取用户登录状态:1已登录过 -1未登录过跳转
  25. authorize.getCode('weixin').then(wechatcode => {
  26. // 根据微信的code获取用户登录状态:1已登录过 -1未登录过
  27. authorize.getUserInfo('weixin').then(wxResponse => {
  28. userInfoLogin({
  29. code: wechatcode,
  30. encryptedData: wxResponse.encryptedData,
  31. iv: wxResponse.iv
  32. }).then(response => {
  33. console.log(response)
  34. store.commit('updateStatus', response.data)
  35. store.commit('login', response.data)
  36. store.commit('wxLogin', wxResponse.userInfo)
  37. if (response.data.userIdentity == 1) {
  38. uni.navigateTo({
  39. url: '/seller/pages/index/index'
  40. })
  41. } else if (response.data.userIdentity === 3) {
  42. uni.navigateTo({
  43. url: '/supplier/pages/index/index'
  44. })
  45. } else {
  46. uni.switchTab({
  47. url: '/pages/tabBar/user/user'
  48. })
  49. }
  50. }).catch(error => {
  51. store.commit('logout', error.data)
  52. store.commit('updateStatus', error.data)
  53. store.commit('wxLogin', wxResponse.userInfo)
  54. })
  55. })
  56. })
  57. }
  58. export default {
  59. wxLoginAuthorize,
  60. wxLoginQuick
  61. }