You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
4.4 KiB
200 lines
4.4 KiB
import {
|
|
appConfig
|
|
} from '../config'
|
|
|
|
const openid_info_key = appConfig.openidInfoKey
|
|
const user_info_key = 'userInfo'
|
|
|
|
export const weixin = {
|
|
throttle: function(fn, gapTime) {
|
|
if (gapTime == null || gapTime == undefined) {
|
|
gapTime = 1500
|
|
}
|
|
|
|
let _lastTime = null
|
|
|
|
// 返回新的函数
|
|
return function() {
|
|
let _nowTime = +new Date()
|
|
if (_nowTime - _lastTime > gapTime || !_lastTime) {
|
|
fn.apply(this, arguments) //将this和参数传给原函数
|
|
_lastTime = _nowTime
|
|
}
|
|
}
|
|
},
|
|
getParam: function(query, variable) {
|
|
var vars = query.split("&");
|
|
for (var i = 0; i < vars.length; i++) {
|
|
var pair = vars[i].split("=");
|
|
if (pair[0] == variable) {
|
|
return pair[1];
|
|
}
|
|
}
|
|
return (false);
|
|
},
|
|
request: options => {
|
|
if (!options.customLoading) {
|
|
uni.showLoading({
|
|
title: '正在加载'
|
|
});
|
|
} else {
|
|
// 当前页面请求数量+1
|
|
if (options.bindThis) {
|
|
options.bindThis.setData({
|
|
//loadingCount: options.bindThis.data.loadingCount + 1
|
|
});
|
|
}
|
|
}
|
|
if (options.newUrl) {
|
|
options.url = appConfig.newBaseUrl + options.api
|
|
} else {
|
|
options.url = appConfig.baseUrl + options.api;
|
|
}
|
|
|
|
options.header = {
|
|
...options.header,
|
|
//'Accept': 'application/json',
|
|
//'Connection': 'keep-alive'
|
|
//'content-type': 'application/json'
|
|
}; // 如果已登录,请求中拼openId
|
|
var access_token = uni.getStorageSync("userInfo").access_token;
|
|
|
|
if (!weixin.isNull(access_token)) {
|
|
options.data = {
|
|
...options.data,
|
|
'token': access_token
|
|
};
|
|
} // 如果是POST方法
|
|
|
|
|
|
if (options.method == 'POST' && !weixin.isNull(access_token)) {
|
|
// 拼时间戳
|
|
options.data.ts = new Date().getTime();
|
|
}
|
|
|
|
uni.request({
|
|
...options,
|
|
success: function(res) {
|
|
if (res.statusCode != 200) {
|
|
if (options.utilFail != undefined) {
|
|
if (res.statusCode == 401) {
|
|
uni.clearStorageSync();
|
|
|
|
let isFirst = true;
|
|
if (isFirst) {
|
|
uni.redirectTo({
|
|
url: '/pages/login/index',
|
|
success: () => {
|
|
isFirst = false
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
//重新静默登录
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} else {
|
|
options.utilFail('TODO: 特殊处理非200错误(' + res.statusCode + ')');
|
|
}
|
|
}
|
|
} else {
|
|
console.log(res)
|
|
if (res.data.hasOwnProperty('errcode')) {
|
|
if (options.utilFail != undefined) {
|
|
options.utilFail(res.data.errmsg || '接口发生未知错误');
|
|
} else {
|
|
options.utilFail(res.data.errmsg);
|
|
}
|
|
} else {
|
|
if (options.utilSuccess != undefined) {
|
|
options.utilSuccess(res.data);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
fail: options.utilFail,
|
|
complete: function(res) {
|
|
if (!options.customLoading) {
|
|
uni.hideNavigationBarLoading();
|
|
uni.hideLoading();
|
|
} else {
|
|
// 当前页面请求数量-1
|
|
if (options.bindThis) {
|
|
options.bindThis.setData({
|
|
loadingCount: options.bindThis.data.loadingCount - 1
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|
|
},
|
|
getOpenidInfo: (cb, refresh) => {
|
|
cb = cb || function() {}
|
|
refresh = refresh || false
|
|
if (!refresh) {
|
|
let user_info = uni.getStorageSync(user_info_key)
|
|
if (!weixin.isNull(user_info)) {
|
|
cb(user_info)
|
|
return
|
|
}
|
|
}
|
|
|
|
uni.login({
|
|
provider: 'weixin',
|
|
success: (res) => {
|
|
console.log(res.code)
|
|
uni.request({
|
|
url: appConfig.baseUrl + '/api/member/login-by-code',
|
|
method: 'POST',
|
|
data: {
|
|
code: res.code
|
|
},
|
|
success: result => {
|
|
console.log(result)
|
|
const user_info1 = result.data.data.user_info
|
|
result.data.data.user_info.openid = user_info1.wechat_openid
|
|
uni.setStorageSync(user_info_key, result.data.data)
|
|
cb(result.data.data)
|
|
}
|
|
});
|
|
}
|
|
});
|
|
},
|
|
getUserProfile: (cb) => {
|
|
cb = cb || function() {}
|
|
|
|
wx.getUserProfile({
|
|
desc: '用于完善会员资料',
|
|
success: (res) => {
|
|
uni.setStorageSync('user_profile', res.userInfo)
|
|
cb(res.userInfo)
|
|
}
|
|
})
|
|
},
|
|
getUserInfoCache: () => {
|
|
return uni.getStorageSync(user_info_key)
|
|
},
|
|
isNull: p => {
|
|
return p == '' || p == undefined || p == null || p == 'undefined' || p == 'null';
|
|
} // 正则
|
|
,
|
|
getBaseUrl: () => {
|
|
return appConfig.baseUrl
|
|
},
|
|
alert: (msg) => {
|
|
uni.showModal({
|
|
content: msg,
|
|
showCancel: false
|
|
|
|
})
|
|
}
|
|
}
|
|
|
|
export function login() {
|
|
|
|
}
|