You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

113 lines
3.4 KiB

  1. const request = require('./utils/request')
  2. import { timestampToTime } from './utils/util'
  3. App({
  4. globalData: {
  5. sessionKey: "",
  6. openId: "",
  7. token: "",
  8. userInfo: null,
  9. promotContentCount: "",
  10. completionContentCount: "",
  11. noticeText: ""
  12. },
  13. onLaunch() {
  14. const that = this
  15. // 登录
  16. wx.login({
  17. success: res => {
  18. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  19. console.log(res, 'wx.login')
  20. doLogin(res.code)
  21. },
  22. })
  23. /**
  24. * @description 登录
  25. * @param {*} loginData {code , appid}
  26. * @returns token , sessionKey , openId
  27. */
  28. const doLogin = code => {
  29. const data = {
  30. code,
  31. appId: request.appId
  32. }
  33. request.post({
  34. url: '/api/user/login',
  35. data
  36. }).then(res => {
  37. console.log(res, 'loginSuccess');
  38. // 存储数据
  39. request.setHead(res.data.token, res.data.tenantId)
  40. wx.setStorageSync('sessionKey', res.data.sessionKey)
  41. that.globalData.sessionKey = res.data.sessionKey
  42. wx.setStorageSync('openId', res.data.openId)
  43. that.globalData.openId = res.data.openId
  44. wx.setStorageSync('token', res.data.token)
  45. that.globalData.token = res.data.token
  46. // 获取通知栏信息文本
  47. getNoticeText()
  48. }).catch(err => {
  49. console.log(err);
  50. wx.showToast({
  51. title: '网络错误,请稍后再试',
  52. icon: 'none'
  53. })
  54. })
  55. }
  56. /**
  57. * @description 检查用户信息
  58. * @returns userInfo
  59. */
  60. const checkUserInfo = () => {
  61. const that = this
  62. request.get({
  63. url: '/api/user/userinfo'
  64. }).then(res => {
  65. console.log(res, 'userinfo');
  66. res.data.createDate = res.data.createDate ? timestampToTime(res.data.createDate, 'YYYY-MM-DD hh:mm:ss') : ''
  67. res.data.updateDate = res.data.updateDate ? timestampToTime(res.data.updateDate, 'YYYY-MM-DD hh:mm:ss') : ''
  68. res.data.validStartTime = res.data.validStartTime ? timestampToTime(res.data.validStartTime, 'YYYY-MM-DD hh:mm:ss') : ''
  69. res.data.validEndTime = res.data.validEndTime ? timestampToTime(res.data.validEndTime, 'YYYY-MM-DD hh:mm:ss') : ''
  70. res.data.coverPhone = res.data.phone ? (res.data.phone.slice(0, 3) + `****` + res.data.phone.slice(7)) : ''
  71. const userInfo = res.data
  72. that.globalData.userInfo = userInfo
  73. // 请求完成后的回调,在index.js中调用防止异步
  74. that.userInfoCallback(res)
  75. wx.setStorageSync('promotContentCount', res.data.promotContentCount)
  76. that.globalData.promotContentCount = res.data.promotContentCount
  77. wx.setStorageSync('completionContentCount', res.data.completionContentCount)
  78. that.globalData.completionContentCount = res.data.completionContentCount
  79. }).catch(err => {
  80. console.log(err, 'err');
  81. })
  82. }
  83. /**
  84. * @description 获取通知栏信息文本
  85. * @returns list
  86. */
  87. const getNoticeText = () => {
  88. const that = this
  89. request.get({
  90. url: '/api/notice/list'
  91. }).then(res => {
  92. console.log(res, 'getNoticeText');
  93. let noticeText = ''
  94. res.data.forEach((item, index) => {
  95. noticeText += index + 1 + '、' + item.content + ' '
  96. })
  97. that.globalData.noticeText = noticeText
  98. // 检查用户信息
  99. checkUserInfo()
  100. }).catch(err => {
  101. console.log(err, 'err');
  102. })
  103. }
  104. },
  105. })