jazz
2024-01-03 12a2f18a968c29b3502a50c4f0a9f49c27c9f3f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * 登录
 */
// const DEBUG = require('../config').isConsole
// const ismock = require('../config').ismock
import fn from './fn'
import Req from './jun_httpInstall'
import Store from '../store'
import Config from '../config'
// import { Message } from 'element-ui'
 
const config = {
  // 登录请求接口链接
  url: 'weixin!ajaxGetInfoByCode',
  // 2小时过期
  expire: 7.2e6
}
 
/**
 * 检查登录缓存过期,默认不过期
 * @param {object} we_session 缓存对象
 */
function checkExpire(we_session) {
  // if ((we_session && Date.now() >= we_session.expire) || !we_session) {
  //     // 删除store缓存
  //  Store.commit('unLogined')
  //     // wx.removeStorageSync('we_session')
  //     localStorage.removeItem('we_session')
  //     return true
  // }
  return false
}
 
/**
 * 进入微信长链,可获取code
 */
function toLongUrl() {
  // fn.urlReplace(Config.codeUrl)
  // location.href = Config.codeUrl
  location.href = Config.createCodeUrl()
}
 
/**
 * 登录
 * @param {object}   option 登录选项
 * @param {boolean}  option.force 是否强制重新登录
 * @param {function} option.callback 登录成功后回调
 */
function checkLogin(option = {}) {
  loginReq(option).then(() => {
    if (typeof option.callback === 'function') {
      option.callback()
    }
  })
}
 
/**
 * 登录请求
 * @param {object} option
 * @param {object} option.data 请求参数
 */
function loginReq(option) {
  if (Config.ismock || Config.istest) {
    option.data.code = '011h5c000ySdaK1lbw000ubdtm2h5c00'
  }
 
  return new Promise((resolve, reject) => {
    // 前置判断
    const userData = Store.getters.getUserData
    if (userData.key) {
      resolve(userData)
      return
    }
 
    if (!option || !option.data) {
      reject()
      console.error('调用loginReq,缺失 option')
      return
    }
    if (!option.data.code) {
      console.error('调用loginReq,缺失 code')
      reject()
      return
    }
    Req.http3.post({
      url: config.url,
      data: option.data
    }).then((res) => {
      // 缓存key值
      fn.setLocalStorage('we_session', {
        we_session: res.inf.key,
        expire: Date.now() + config.expire
      })
      // 缓存用户数据
      Store.commit('setUserData', res.inf)
      console.log(res.inf)
 
      // if (res.inf.name) {
      //     Store.commit('setNickName', res.inf.name)
      // }
 
      // 处理登录完成 store
      // getUserInfo().then(_res=>{
      //     // Store.commit('setLogined', _res.inf)
      //     resolve(res)
      // })
      // Store.commit('setLogined')
 
      resolve(res)
    }).catch((res) => {
      console.log(res)
      reject(res)
    })
  })
}
 
export default {
  checkLogin,
  loginReq,
  toLongUrl
}