wxLogin.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import store from '@/store/index.js'
  2. import authorize from '@/common/config/authorize.js'
  3. import { userInfoLogin } from "@/api/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{
  40. uni.switchTab({url:'/pages/tabBar/user/user'})
  41. }
  42. }).catch(error =>{
  43. uni.removeStorageSync('sessionid')
  44. uni.setStorageSync('sessionid','JSESSIONID='+error.data)
  45. store.commit('logout',error.data)
  46. store.commit('updateStatus',error.data)
  47. store.commit('wxLogin',wxResponse.userInfo);
  48. })
  49. })
  50. })
  51. }
  52. export default{
  53. wxLoginAuthorize,
  54. wxLoginQuick
  55. }