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) // let _json = {"access_token":"134207|a3d07M2xJFhCQK3YJ5JzZ8L15sYOwo0vWMy5meMR","token_type":"bearer","expires_in":86400,"user_info":{"id":14521,"name":"微信用户123","phone":"18914071203","due_date":"2023-07-10","hospital":null,"wechat":null,"wechat_nickname":"微信用户","address":null,"source":null,"user_source":null,"wechat_openid":"oEgEM0QhlSnJfVUI0qWIK3JM8O7g","wechat_unionid":null,"visit_count":null,"last_login_time":null,"lon":null,"lat":null,"province":null,"city":null,"area":"吴江区","operating":null,"ip":null,"machine":null,"pid":null,"site_id":null,"created_at":"2023-02-07T01:57:06.000000Z","updated_at":"2023-07-10T10:35:53.000000Z","deleted_at":null,"password":null,"promotion":1,"promotion_type":null,"promotion_id":null,"avatar":"https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132","session_key":"E34k9RRqji6kETr3v7B1/g==","openid":"oEgEM0QhlSnJfVUI0qWIK3JM8O7g"}} // uni.setStorageSync(user_info_key, _json) 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() { }