Commit d1c04132 authored by 肖健's avatar 肖健

更新js

parent d36ebe78
This diff is collapsed.
let plugin = requirePlugin('loginPlugin'); let plugin = requirePlugin("loginPlugin");
(function () { (function () {
if (console.jdLoginLog) { if (console.jdLoginLog) return
return;
}
let normalLog = console.log; let normalLog = console.log;
console.jdLoginLog = (...args) => { console.jdLoginLog = (...args) => {
args.unshift('-------登录插件-------'); args.unshift('-------登录插件-------')
if (normalLog) { normalLog && normalLog(...args);
normalLog(...args);
} }
}; })()
})();
const utils = { const utils = {
redirectPage(url) { redirectPage(url) {
uni.redirectTo({ wx.redirectTo({
url url
}); });
}, },
redirectToH5({ page, wvroute }) { redirectToH5({ page, wvroute }) {
let url = plugin.formH5Url({ let url = plugin.formH5Url({ page: decodeURIComponent(page), wvroute })
page: decodeURIComponent(page), utils.redirectPage(url)
wvroute
});
utils.redirectPage(url);
}, },
navigateToH5({ page, wvroute }) { navigateToH5({ page, wvroute }) {
let url = plugin.formH5Url({ let url = plugin.formH5Url({ page: decodeURIComponent(page), wvroute })
page: decodeURIComponent(page), wx.navigateTo({ url })
wvroute
});
uni.navigateTo({
url
});
}, },
setLoginParamsStorage(obj = {}) { setLoginParamsStorage(obj = {}) {
if (obj.appid && isNaN(Number(obj.appid))) { if(obj.appid && isNaN(Number(obj.appid))){
delete obj.appid; delete obj.appid
} }
plugin.setLoginStorageSync(utils.getLoginConfig(obj)); plugin.setLoginStorageSync(utils.getLoginConfig(obj));
}, },
...@@ -45,157 +35,114 @@ const utils = { ...@@ -45,157 +35,114 @@ const utils = {
*/ */
getLoginConfig(obj = {}) { getLoginConfig(obj = {}) {
//兼容缓存中有returnPage, 传递的参数中无,塞缓存时会用缓存中的值,导致不匹配 //兼容缓存中有returnPage, 传递的参数中无,塞缓存时会用缓存中的值,导致不匹配
const handleUndefinedType = (o = {}) => { const handleUndefinedType = (o={})=>{
let { pageType = 'redirectTo' } = o; let { pageType = 'redirectTo' } = o
o.pageType = pageType; o.pageType = pageType;
return o; return o
}; }
let storageConfig = plugin.getLoginParams(); let storageConfig = plugin.getLoginParams();
let config = handleUndefinedType(utils.getDefaultConfig()); let config = handleUndefinedType(utils.getDefaultConfig());
let loginParams = { let loginParams = { ...storageConfig, ...config };
...storageConfig,
...config
};
if (plugin.isObject(obj)) { if (plugin.isObject(obj)) {
loginParams = { loginParams = { ...loginParams, ...handleUndefinedType(obj) }
...loginParams,
...handleUndefinedType(obj)
};
} else { } else {
console.jdLoginLog('登录参数必须为对象'); console.jdLoginLog('登录参数必须为对象')
} }
return loginParams; return loginParams
}, },
getDefaultConfig() { getDefaultConfig() {
let lgConfig; let lgConfig;
try { try {
lgConfig = require('./config.js'); lgConfig = require("./config.js");
} catch (err) { } catch (err) {
console.log('CatchClause', err);
console.log('CatchClause', err);
lgConfig = {}; lgConfig = {};
} }
return lgConfig.config || {}; return lgConfig.config || {}
}, },
handleJump(p = {}) { handleJump(p = {}) {
let { goback, pluginUrl, riskUrl } = p; let { goback, pluginUrl, riskUrl } = p;
if (goback) { if (goback) {
utils.goBack(); utils.goBack();
return; return
} }
if (pluginUrl) { if (pluginUrl) {
utils.redirectPage(pluginUrl); utils.redirectPage(pluginUrl);
return; return
}
if (riskUrl) {
utils.redirectToH5({
page: riskUrl
});
} }
riskUrl && utils.redirectToH5({ page: riskUrl })
}, },
goBack() { goBack() {
let params = plugin.getLoginParams(); let params = plugin.getLoginParams(),
let { returnPage, pageType } = params; { returnPage, pageType } = params;
if (pageType == 'navigateBack') { if(pageType == 'navigateBack'){
uni[pageType](); wx[pageType]();
return; return;
} }
if (!returnPage) { if (!returnPage) {
uni.showToast({ wx.showToast({
title: '没有returnPage,无法跳转', title: '没有returnPage,无法跳转',
icon: 'none' icon: 'none'
}); })
return; return
} }
if (pageType !== 'h5') { if (pageType !== 'h5') {
returnPage = decodeURIComponent(returnPage); returnPage = decodeURIComponent(returnPage);
if (pageType && pageType != 'rediretTo') { if(pageType && pageType != 'rediretTo'){
uni[pageType]({ wx[pageType]({url: returnPage})
url: returnPage }else{
}); utils.redirectPage(returnPage)
} else {
utils.redirectPage(returnPage);
} }
return; return;
} else { } else {
utils.redirectToH5({ utils.redirectToH5({ page: returnPage })
page: returnPage
});
return; return;
} }
}, },
h5Init(options) { h5Init(options) {
let p = plugin.getLoginParams(); let p = plugin.getLoginParams();
if (plugin.isEmptyObj(p)) { if (plugin.isEmptyObj(p)) utils.setLoginParamsStorage(options)
utils.setLoginParamsStorage(options);
}
}, },
setCustomNavigation() { setCustomNavigation() {
let { navigationBarColor, navigationBarTitle } = plugin.getLoginParams(); let { navigationBarColor, navigationBarTitle } = plugin.getLoginParams();
if (plugin.isObject(navigationBarColor)) { plugin.isObject(navigationBarColor) && wx.setNavigationBarColor(navigationBarColor);
uni.setNavigationBarColor(navigationBarColor); plugin.isObject(navigationBarTitle) && wx.setNavigationBarTitle(navigationBarTitle);
}
if (plugin.isObject(navigationBarTitle)) {
uni.setNavigationBarTitle(navigationBarTitle);
}
}, },
requestWithLoginStatus(obj = {}) { requestWithLoginStatus(obj = {}) {
obj.header = obj.header || {}; obj.header = obj.header || {};
let [GUID = '', KEY = '', TOKEN = '', PIN = ''] = plugin.getJdListStorage(['guid', 'pt_key', 'pt_token', 'pt_pin']); let [GUID = '', KEY = '', TOKEN = '', PIN = ''] = plugin.getJdListStorage(['guid', 'pt_key', 'pt_token', 'pt_pin']),
let _cookie = `guid=${GUID}; pt_pin=${encodeURIComponent(PIN)}; pt_key=${KEY}; pt_token=${TOKEN}`; _cookie = `guid=${GUID}; pt_pin=${encodeURIComponent(PIN)}; pt_key=${KEY}; pt_token=${TOKEN}`,
let { cookie } = obj.header; { cookie } = obj.header ;
obj.header.cookie = cookie ? `${cookie};${_cookie}` : _cookie; obj.header.cookie = cookie ? `${cookie};${_cookie}` : _cookie;
uni.request(obj); wx.request(obj)
}, },
silentauthlogin(options, goToLogin, callback) { silentauthlogin(options, goToLogin, callback) {
uni.login({ wx.login({
success(res = {}) { success(res = {}) {
let { code } = res; let { code } = res;
if (code) { if (code) {
utils.setLoginParamsStorage(options); utils.setLoginParamsStorage(options);
plugin plugin.silentauthlogin({ ...options, code }, goToLogin)
.silentauthlogin(
{
...options,
code
},
goToLogin
)
.then((res) => { .then((res) => {
if (callback) { callback && callback({isSuccess: res.err_code == 0});
callback({ if(goToLogin && res.err_code == 0){
isSuccess: res.err_code == 0 utils.handleJump(res)
});
}
if (goToLogin && res.err_code == 0) {
utils.handleJump(res);
} }
}) })
.catch((res) => { .catch((res) => {
if (callback) { callback && callback({isSuccess: false});
callback({
isSuccess: false
});
}
console.jdLoginLog('请重试一次'); console.jdLoginLog('请重试一次');
}); });
} else { } else {
if (callback) { callback && callback({isSuccess: false});
callback({
isSuccess: false
});
}
console.jdLoginLog('wx.login 获取code失败'); console.jdLoginLog('wx.login 获取code失败');
} }
}, },
fail() { fail() {
if (callback) { callback && callback({isSuccess: false});
callback({ },
isSuccess: false
});
}
}
}); });
} }
}; }
export default utils;
export default utils
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment