| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import {
- CACHE_USERINFO,
- CACHE_TOKEN,
- CACHE_EXPIRES_TIME
- } from '../config.js';
- import {
- login_simple,
- } from '../api/user.js';
- // 新的登录逻辑实现文件
- // 新的逻辑要求尽量减少用户授权确认,用户只有在首次注册时和以后定期不定期的才需要确认(用于获取用户微信头像等资料)上传资料用于注册、系统同步更新用户资料。
- // 静默登录,不再需要用户授权。
- // 用于小程序启动后,token 过期的情况下,后台静默登录。如果失败,则不用处理,等待后续的用户授权过程。
- export function loginSilently() {
- return new Promise((resolve, reject) => {
- wx.login({
- success: (login_res) => {
- login_simple({
- code: login_res.code
- }).then((simple_res) => {
- if (simple_res.data.status == 1) {
- // 需要提交用户资料,此时必须有 cache_key
- if (simple_res.data.cache_key) {
- wx.setStorage({
- key: 'cache_key',
- data: simple_res.data.cache_key
- });
- } else {
- reject({
- err: '登录失败'
- });
- }
- reject({
- status: 1,
- err: ''
- });
- } else if (simple_res.data.status == 0) {
- // todo: clear cache_key
- wx.removeStorage({
- key: 'cache_key',
- });
- var app = getApp();
- app.globalData.token = simple_res.data.token;
- app.globalData.isLog = true;
- app.globalData.userInfo = simple_res.data.userInfo;
- app.globalData.expiresTime = simple_res.data.expires_time;
- wx.setStorageSync(CACHE_TOKEN, simple_res.data.token);
- wx.setStorageSync(CACHE_EXPIRES_TIME, simple_res.data.expires_time);
- wx.setStorageSync(CACHE_USERINFO, JSON.stringify(simple_res.data.userInfo));
- resolve(simple_res.data);
- }
- }).catch((err) => {
- reject({
- err: err
- });
- })
- },
- fail: (err) => {
- reject({
- err: err
- })
- }
- })
- });
- }
|