wxLogin.js 2.7 KB

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