import * as config from '@/config' import WXBizDataCrypt from './WXBizDataCrypt.js' let baseUrl = config.def().baseUrlNew // 定义基础请求路径(后端服务器地址) const baseRequest = (_gp, _mt, data = {}, failCallback) => { //异步请求数据 return new Promise(resolve => { let baseUrl = config.def().baseUrlNew uni.request({ url: baseUrl + '/m.api', data: { ...data, _gp, _mt }, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', // 'ACCESSTOKEN': accessToken }, success: (res) => { if (res.statusCode === 200) { if (res.data.errno === 200) { resolve(res.data); } else { if (failCallback) { failCallback(res.data) } else { uni.showToast({ title: res.data.errmsg, icon: 'none' }) } } } } }) }) } //带Token请求 const TokenRequest = (method, url, data, header) => { var contentheader = 'application/json' if (header) { contentheader = header } let ac_token = ""; uni.getStorage({ key: 'userInfo', success: function(res) { ac_token = res.data.accessToken } }); //此token是登录成功后后台返回保存在storage中的 let DefaultOpts = { url: baseUrl + url, data: data, method: method, header: { 'content-type': contentheader, 'Token': ac_token, } } let promise = new Promise(function(resolve, reject) { uni.request(DefaultOpts).then( (res) => { console.log(JSON.stringify(res[1].data)) if (res[1].data.code == '200' || res[1].data.code == 200) { // 后端返回的状态码100为成功状态,成功则返回请求结果,在app调试时可以通过console.log(JSON.stringify(res[1].data))来查看返回值(以项目实际情况为准) resolve(res[1].data) } if (res[1].data.code == '105' || res[1].data.code == 105) { // 后端返回状态码为105则为未登录状态(以项目实际情况为准) uni.showToast({ icon: 'none', title: '尚未登录', duration: 2000 }); // 尚未登录的逻辑处理 return false } } ).catch( (response) => { reject(response) } ) }) return promise } //微信登录 const wxlogin = () => { let promise = new Promise(function(resolve, reject) { uni.login({ "provider": "weixin", success: function(wxres) { console.log("wxlogin",wxres) baseRequest('commonUserApp', 'commonUserLogin', { loginType: 1, raw: JSON.stringify(wxres) }, failres => { uni.showToast({ icon: "none", title: failres.errmsg, duration: 3000 }); uni.hideLoading() }).then(res => { console.log(res.data) resolve(res.data) }) }, fail: function(err) { reject(err.code) } }) }) return promise } //获取手机号 const getPhone = (e, userInfo) => { console.log(e,userInfo) let promise = new Promise(function(resolve, reject) { if (e.mp.detail.errMsg === "getPhoneNumber:ok") { let appId = 'wxa88225ca7712d558' let sessionKey = userInfo.sessionKey let encryptedData = e.mp.detail.encryptedData let iv = e.mp.detail.iv let data ='' try { console.log('解密前 encryptedData: ', encryptedData) console.log('解密前 iv: ', iv) console.log('解密前 appId: ', appId) console.log('解密前 sessionKey: ', sessionKey) data= WXBizDataCrypt.prototype.decryptData(encryptedData, iv, appId, sessionKey) console.log('解密后 data: ', data) resolve(data.phoneNumber) } catch (e) { uni.showToast({ icon: "none", title: e, duration: 3000 }); } } }) return promise } //同步信息 const syncInfo = (userInfo) => { let promise = new Promise(function(resolve, reject) { if (!userInfo.head) userInfo.head ='https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/card_head.png' if (!userInfo.nickname) { let code = "" for (var i = 0; i < 6; i++) { code += parseInt(Math.random() * 10); } userInfo.nickname = 'user' + code } baseRequest('commonUserApp', 'edit', { commonUserInfo: JSON.stringify(userInfo) }, failres => { uni.showToast({ icon: "none", title: failres.errmsg, duration: 3000 }); reject(err.code) }).then(res => { resolve(res.data) }) }) return promise } export default { baseUrl, baseRequest, TokenRequest, wxlogin, getPhone, syncInfo }