全新UI视觉体验,移动端API优化降低重复调用,提高并发6倍,修复N多Bug
This commit is contained in:
24
app/libs/apps.js
Normal file
24
app/libs/apps.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import { appAuth } from '../api/public';
|
||||
|
||||
class Apps{
|
||||
/**
|
||||
* 授权登录获取token
|
||||
* @param {Object} code
|
||||
*/
|
||||
authApp(code) {
|
||||
return new Promise((resolve, reject) => {
|
||||
appAuth(code,{'spread_spid': 0})
|
||||
.then(({
|
||||
data
|
||||
}) => {
|
||||
resolve(data);
|
||||
Cache.set(WX_AUTH, code);
|
||||
Cache.clear(STATE_KEY);
|
||||
loginType && Cache.clear(LOGINTYPE);
|
||||
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
export default new Apps();
|
||||
@@ -1,11 +1,12 @@
|
||||
import store from "../store";
|
||||
import Cache from '../utils/cache';
|
||||
// #ifdef H5 || APP-PLUS
|
||||
import { Debounce } from '@/utils/validate.js'
|
||||
// #ifdef H5
|
||||
import { isWeixin } from "../utils";
|
||||
import auth from './wechat';
|
||||
// #endif
|
||||
|
||||
import { LOGIN_STATUS, USER_INFO, EXPIRES_TIME, STATE_R_KEY} from './../config/cache';
|
||||
import { LOGIN_STATUS, USER_INFO, EXPIRES_TIME, STATE_R_KEY, BACK_URL} from './../config/cache';
|
||||
|
||||
function prePage(){
|
||||
let pages = getCurrentPages();
|
||||
@@ -13,28 +14,55 @@ function prePage(){
|
||||
return prePage.route;
|
||||
}
|
||||
|
||||
export function toLogin(push, pathLogin) {
|
||||
export const toLogin = Debounce(_toLogin,800)
|
||||
|
||||
export function _toLogin(push, pathLogin) {
|
||||
|
||||
store.commit("LOGOUT");
|
||||
let path = prePage();
|
||||
let login_back_url = Cache.get(BACK_URL);
|
||||
// #ifdef H5
|
||||
path = location.href;
|
||||
// path = location.href;
|
||||
path = location.pathname + location.search;
|
||||
// #endif
|
||||
if(!pathLogin)
|
||||
if(!pathLogin){
|
||||
pathLogin = '/page/users/login/index'
|
||||
Cache.set('login_back_url',path);
|
||||
// #ifdef H5 || APP-PLUS
|
||||
Cache.set('login_back_url',path);
|
||||
}
|
||||
|
||||
// #ifdef H5
|
||||
if (isWeixin()) {
|
||||
auth.oAuth();
|
||||
let urlData = location.pathname + location.search
|
||||
if (urlData.indexOf('?') !== -1) {
|
||||
urlData += '&go_longin=1';
|
||||
} else {
|
||||
urlData += '?go_longin=1';
|
||||
}
|
||||
if (!Cache.has('snsapiKey')) {
|
||||
auth.oAuth('snsapi_base', urlData);
|
||||
} else {
|
||||
if (['/pages/user/index'].indexOf(login_back_url) == -1) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/users/wechat_login/index'
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (path !== pathLogin) {
|
||||
push ? uni.navigateTo({
|
||||
url:'/pages/users/login/index'
|
||||
}) : uni.reLaunch({
|
||||
url: '/pages/users/login/index'
|
||||
});
|
||||
if (['/pages/user/index'].indexOf(login_back_url) == -1) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/users/login/index'
|
||||
})
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
|
||||
if (['pages/user/index','/pages/user/index'].indexOf(login_back_url) == -1) {
|
||||
// #ifdef MP
|
||||
uni.navigateTo({
|
||||
url: '/pages/users/wechat_login/index'
|
||||
})
|
||||
// #endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,4 +86,4 @@ export function checkLogin()
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
export function goShopDetail(item,uid) {
|
||||
import {
|
||||
preOrderApi
|
||||
} from '@/api/order.js';
|
||||
import util from 'utils/util'
|
||||
|
||||
/**
|
||||
* 去商品详情
|
||||
*/
|
||||
export function goShopDetail(item, uid) {
|
||||
return new Promise(resolve => {
|
||||
if (item.activityH5 && item.activityH5.type === "1") {
|
||||
uni.navigateTo({
|
||||
url: `/pages/activity/goods_seckill_details/index?id=${item.activityH5.id}&time=${item.activityH5.time}&status=2`
|
||||
url: `/pages/activity/goods_seckill_details/index?id=${item.activityH5.id}`
|
||||
})
|
||||
} else if (item.activityH5 && item.activityH5.type === "2") {
|
||||
uni.navigateTo({
|
||||
url: `/pages/activity/goods_bargain_details/index?id=${item.activityH5.id}&bargain=${uid}`
|
||||
url: `/pages/activity/goods_bargain_details/index?id=${item.activityH5.id}&startBargainUid=${uid}`
|
||||
})
|
||||
} else if (item.activityH5 && item.activityH5.type === "3") {
|
||||
uni.navigateTo({
|
||||
@@ -17,3 +25,23 @@ export function goShopDetail(item,uid) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动商品、普通商品、购物车、再次购买预下单
|
||||
*/
|
||||
export function getPreOrder(preOrderType, orderDetails) {
|
||||
return new Promise((resolve, reject) => {
|
||||
preOrderApi({
|
||||
"preOrderType": preOrderType,
|
||||
"orderDetails": orderDetails
|
||||
}).then(res => {
|
||||
uni.navigateTo({
|
||||
url: '/pages/users/order_confirm/index?preOrderNo=' + res.data.preOrderNo
|
||||
});
|
||||
}).catch(err => {
|
||||
return util.Tips({
|
||||
title: err
|
||||
});
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ import { checkLogin } from './login';
|
||||
import { login } from '../api/public';
|
||||
import Cache from '../utils/cache';
|
||||
import { STATE_R_KEY, USER_INFO, EXPIRES_TIME, LOGIN_STATUS} from './../config/cache';
|
||||
|
||||
class Routine
|
||||
{
|
||||
|
||||
@@ -25,16 +24,6 @@ class Routine
|
||||
getUserProfile(){
|
||||
let that = this , code = this.getUserCode();
|
||||
return new Promise( (resolve,reject) => {
|
||||
// uni.getUserInfo({
|
||||
// lang: 'zh_CN',
|
||||
// success(user) {
|
||||
// if(code) user.code = code;
|
||||
// resolve({userInfo:user,islogin:false});
|
||||
// },
|
||||
// fail(res){
|
||||
// reject(res);
|
||||
// }
|
||||
// })
|
||||
uni.getUserProfile({
|
||||
lang: 'zh_CN',
|
||||
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
|
||||
@@ -76,9 +65,6 @@ class Routine
|
||||
async getCode(){
|
||||
let provider = await this.getProvider();
|
||||
return new Promise((resolve,reject)=>{
|
||||
if(Cache.has(STATE_R_KEY)){
|
||||
return resolve(Cache.get(STATE_R_KEY));
|
||||
}
|
||||
uni.login({
|
||||
provider:provider,
|
||||
success(res) {
|
||||
@@ -130,17 +116,19 @@ class Routine
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序登录
|
||||
*/
|
||||
authUserInfo(code,data)
|
||||
{
|
||||
return new Promise((resolve, reject)=>{
|
||||
login(code,data).then(res=>{
|
||||
// let time = res.data.expiresTime - Cache.time();
|
||||
store.commit('UPDATE_USERINFO', res.data.user);
|
||||
store.commit('LOGIN', {token:res.data.token});
|
||||
store.commit('SETUID', res.data.user.uid);
|
||||
// Cache.set(EXPIRES_TIME,res.data.expiresTime,time);
|
||||
Cache.set(USER_INFO,res.data.user);
|
||||
if(res.data.type==='login'){
|
||||
store.commit('LOGIN', {
|
||||
token: res.data.token
|
||||
});
|
||||
store.commit("SETUID", res.data.uid);
|
||||
}
|
||||
return resolve(res);
|
||||
}).catch(res=>{
|
||||
return reject(res);
|
||||
|
||||
@@ -112,6 +112,21 @@ class AuthWechat {
|
||||
});
|
||||
}
|
||||
|
||||
// 使用微信内置地图查看位置接口;
|
||||
seeLocation(config){
|
||||
return new Promise((resolve, reject) => {
|
||||
this.wechat().then(wx => {
|
||||
this.toPromise(wx.openLocation, config).then(res => {
|
||||
resolve(res);
|
||||
}).catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
}).catch(err => {
|
||||
reject(err);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
* @param {Object} config
|
||||
@@ -153,12 +168,37 @@ class AuthWechat {
|
||||
/**
|
||||
* 自动去授权
|
||||
*/
|
||||
oAuth() {
|
||||
if (uni.getStorageSync(WX_AUTH) && store.state.app.token) return;
|
||||
oAuth(snsapiBase,url) {
|
||||
if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase == 'snsapi_base') return;
|
||||
const {
|
||||
code
|
||||
} = parseQuery();
|
||||
if (!code) return this.toAuth();
|
||||
if (!code || code == uni.getStorageSync('snsapiCode')){
|
||||
return this.toAuth(snsapiBase,url);
|
||||
}else{
|
||||
if(Cache.has('snsapiKey'))
|
||||
return this.auth(code).catch(error=>{
|
||||
uni.showToast({
|
||||
title:error,
|
||||
icon:'none'
|
||||
})
|
||||
})
|
||||
}
|
||||
// if (uni.getStorageSync(WX_AUTH) && store.state.app.token) return;
|
||||
// const {
|
||||
// code
|
||||
// } = parseQuery();
|
||||
// if (!code){
|
||||
// return this.toAuth(snsapiBase,url);
|
||||
// }else{
|
||||
// if(Cache.has('snsapiKey'))
|
||||
// return this.auth(code).catch(error=>{
|
||||
// uni.showToast({
|
||||
// title:error,
|
||||
// icon:'none'
|
||||
// })
|
||||
// })
|
||||
// }
|
||||
}
|
||||
|
||||
clearAuthStatus() {
|
||||
@@ -171,24 +211,16 @@ class AuthWechat {
|
||||
*/
|
||||
auth(code) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let loginType = Cache.get(LOGINTYPE);
|
||||
wechatAuth(code, Cache.get("spread"), loginType)
|
||||
wechatAuth(code, Cache.get("spread"))
|
||||
.then(({
|
||||
data
|
||||
}) => {
|
||||
// let expires_time = data.expires_time.substring(0, 19);
|
||||
// expires_time = expires_time.replace(/-/g, '/');
|
||||
// expires_time = new Date(expires_time).getTime();
|
||||
// let newTime = Math.round(new Date() / 1000);
|
||||
store.commit("LOGIN", {
|
||||
token: data.token
|
||||
// time: expires_time - newTime
|
||||
});
|
||||
resolve(data);
|
||||
Cache.set(WX_AUTH, code);
|
||||
Cache.clear(STATE_KEY);
|
||||
// Cache.clear('spread');
|
||||
loginType && Cache.clear(LOGINTYPE);
|
||||
resolve();
|
||||
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
@@ -198,32 +230,43 @@ class AuthWechat {
|
||||
* 获取跳转授权后的地址
|
||||
* @param {Object} appId
|
||||
*/
|
||||
getAuthUrl(appId) {
|
||||
const redirect_uri = encodeURIComponent(
|
||||
`${location.origin}/pages/auth/index?back_url=` +
|
||||
encodeURIComponent(
|
||||
encodeURIComponent(
|
||||
uni.getStorageSync(BACK_URL) ?
|
||||
uni.getStorageSync(BACK_URL) :
|
||||
location.pathname + location.search
|
||||
)
|
||||
)
|
||||
);
|
||||
uni.removeStorageSync(BACK_URL);
|
||||
const state = encodeURIComponent(
|
||||
("" + Math.random()).split(".")[1] + "authorizestate"
|
||||
);
|
||||
uni.setStorageSync(STATE_KEY, state);
|
||||
return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
|
||||
}
|
||||
|
||||
getAuthUrl(appId,snsapiBase,backUrl) {
|
||||
let url = `${location.origin}${backUrl}`
|
||||
if(url.indexOf('?') == -1){
|
||||
url = url+'?'
|
||||
}else{
|
||||
url = url+'&'
|
||||
}
|
||||
const redirect_uri = encodeURIComponent(
|
||||
`${url}scope=${snsapiBase}&back_url=` +
|
||||
encodeURIComponent(
|
||||
encodeURIComponent(
|
||||
uni.getStorageSync(BACK_URL) ?
|
||||
uni.getStorageSync(BACK_URL) :
|
||||
location.pathname + location.search
|
||||
)
|
||||
)
|
||||
);
|
||||
uni.removeStorageSync(BACK_URL);
|
||||
const state = encodeURIComponent(
|
||||
("" + Math.random()).split(".")[1] + "authorizestate"
|
||||
);
|
||||
uni.setStorageSync(STATE_KEY, state);
|
||||
return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
|
||||
// if(snsapiBase==='snsapi_base'){
|
||||
// return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
|
||||
// }else{
|
||||
// return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转自动登录
|
||||
*/
|
||||
toAuth() {
|
||||
toAuth(snsapiBase,backUrl) {
|
||||
let that = this;
|
||||
this.wechat().then(wx => {
|
||||
location.href = this.getAuthUrl(that.initConfig.appId);
|
||||
location.href = this.getAuthUrl(that.initConfig.appId,snsapiBase,backUrl);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -272,4 +315,4 @@ class AuthWechat {
|
||||
}
|
||||
|
||||
export default new AuthWechat();
|
||||
// #endif
|
||||
// #endif
|
||||
|
||||
Reference in New Issue
Block a user