|
- const request = require('./utils/request')
- import { timestampToTime } from './utils/util'
-
- App({
- globalData: {
- sessionKey: "",
- openId: "",
- token: "",
- userInfo: null,
- promotContentCount: "",
- completionContentCount: "",
- noticeText: ""
- },
- onLaunch() {
- const that = this
- // 登录
- wx.login({
- success: res => {
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- console.log(res, 'wx.login')
- doLogin(res.code)
- },
- })
-
- /**
- * @description 登录
- * @param {*} loginData {code , appid}
- * @returns token , sessionKey , openId
- */
- const doLogin = code => {
- const data = {
- code,
- appId: request.appId
- }
- request.post({
- url: '/api/user/login',
- data
- }).then(res => {
- console.log(res, 'loginSuccess');
- // 存储数据
- request.setHead(res.data.token, res.data.tenantId)
- wx.setStorageSync('sessionKey', res.data.sessionKey)
- that.globalData.sessionKey = res.data.sessionKey
- wx.setStorageSync('openId', res.data.openId)
- that.globalData.openId = res.data.openId
- wx.setStorageSync('token', res.data.token)
- that.globalData.token = res.data.token
- // 获取通知栏信息文本
- getNoticeText()
- }).catch(err => {
- console.log(err);
- wx.showToast({
- title: '网络错误,请稍后再试',
- icon: 'none'
- })
- })
- }
-
- /**
- * @description 检查用户信息
- * @returns userInfo
- */
- const checkUserInfo = () => {
- const that = this
- request.get({
- url: '/api/user/userinfo'
- }).then(res => {
- console.log(res, 'userinfo');
- res.data.createDate = res.data.createDate ? timestampToTime(res.data.createDate, 'YYYY-MM-DD hh:mm:ss') : ''
- res.data.updateDate = res.data.updateDate ? timestampToTime(res.data.updateDate, 'YYYY-MM-DD hh:mm:ss') : ''
- res.data.validStartTime = res.data.validStartTime ? timestampToTime(res.data.validStartTime, 'YYYY-MM-DD hh:mm:ss') : ''
- res.data.validEndTime = res.data.validEndTime ? timestampToTime(res.data.validEndTime, 'YYYY-MM-DD hh:mm:ss') : ''
- res.data.coverPhone = res.data.phone ? (res.data.phone.slice(0, 3) + `****` + res.data.phone.slice(7)) : ''
- const userInfo = res.data
- that.globalData.userInfo = userInfo
-
- // 请求完成后的回调,在index.js中调用防止异步
- that.userInfoCallback(res)
-
- wx.setStorageSync('promotContentCount', res.data.promotContentCount)
- that.globalData.promotContentCount = res.data.promotContentCount
-
- wx.setStorageSync('completionContentCount', res.data.completionContentCount)
- that.globalData.completionContentCount = res.data.completionContentCount
-
- }).catch(err => {
- console.log(err, 'err');
- })
- }
-
- /**
- * @description 获取通知栏信息文本
- * @returns list
- */
- const getNoticeText = () => {
- const that = this
- request.get({
- url: '/api/notice/list'
- }).then(res => {
- console.log(res, 'getNoticeText');
- let noticeText = ''
- res.data.forEach((item, index) => {
- noticeText += index + 1 + '、' + item.content + ' '
- })
- that.globalData.noticeText = noticeText
- // 检查用户信息
- checkUserInfo()
- }).catch(err => {
- console.log(err, 'err');
- })
- }
- },
- })
|