您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

70 行
1.8 KiB

  1. const request = require('./utils/request')
  2. App({
  3. globalData: {
  4. },
  5. onLaunch() {
  6. const that = this
  7. // 登录
  8. wx.login({
  9. success: res => {
  10. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  11. console.log(res.code)
  12. doLogin(res.code)
  13. },
  14. })
  15. /**
  16. * @description 登录
  17. * @param {*} loginData {code , appid}
  18. * @returns token , sessionKey , openId
  19. */
  20. const doLogin = code => {
  21. const data = {
  22. code,
  23. appId: request.appId
  24. }
  25. request.post({
  26. url: '/api/user/login',
  27. data
  28. }).then(res => {
  29. console.log(res, 'loginSuccess');
  30. // 存储数据
  31. request.setHead(res.data.token, res.data.tenantId)
  32. wx.setStorageSync('sessionKey', res.data.sessionKey)
  33. that.globalData.sessionKey = res.data.sessionKey
  34. wx.setStorageSync('openId', res.data.openId)
  35. that.globalData.openId = res.data.openId
  36. wx.setStorageSync('AccessToken', res.data.token)
  37. that.globalData.AccessToken = res.data.token
  38. // 检查用户信息
  39. checkUserInfo()
  40. }).catch(err => {
  41. console.log(err);
  42. wx.showToast({
  43. title: '网络错误,请稍后再试',
  44. icon: 'none'
  45. })
  46. })
  47. }
  48. /**
  49. * @description 检查用户信息
  50. * @returns userInfo
  51. */
  52. const checkUserInfo = () => {
  53. request.get({
  54. url: '/api/user/userinfo'
  55. }).then(res => {
  56. console.log(res, 'userinfo');
  57. const userInfo = res.data
  58. that.globalData.userInfo = userInfo
  59. // 请求完成后的回调,在index.js中调用防止异步
  60. that.userInfoCallback(res)
  61. }).catch(err => {
  62. console.log(err, 'err');
  63. })
  64. }
  65. },
  66. })