wxLogin.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. uni.setStorageSync('token',response.data.token)
  19. uni.setStorageSync('unionId',response.data.unionId)
  20. })
  21. .catch(error =>{
  22. uni.setStorageSync('unionId',error.data.unionId)
  23. store.commit('logout',error.data)
  24. store.commit('updateStatus',error.data)
  25. })
  26. }
  27. const wxLoginQuick = async function(){// 根据微信的code获取用户登录状态:1已登录过 -1未登录过跳转
  28. const wechatCode = await authorize.getCode('weixin')// 根据微信的code获取用户登录状态:1已登录过 -1未登录过
  29. const getUserInfo = await authorize.getUserInfo('weixin')
  30. newUserService.UserLoginAuthApplets({
  31. code:wechatCode,
  32. encryptedData:getUserInfo.encryptedData,
  33. iv:getUserInfo.iv
  34. })
  35. .then(response =>{
  36. console.log(response)
  37. store.commit('updateStatus',response.data)
  38. store.commit('login',response.data)
  39. uni.setStorageSync('token',response.data.token)
  40. uni.setStorageSync('unionId',response.data.unionId)
  41. uni.switchTab({url:'/pages/tabBar/user/user'})
  42. })
  43. .catch(error =>{
  44. uni.setStorageSync('unionId',error.data.unionId)
  45. store.commit('logout',error.data)
  46. store.commit('updateStatus',error.data)
  47. })
  48. }
  49. export default{
  50. wxLoginAuthorize,
  51. wxLoginQuick
  52. }