wxLogin.js 2.3 KB

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