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