const req = require('../utils/request.js') const env = { NODE_ENV: req.env.NODE_ENV=='dev'?'dev':req.env.NODE_ENV=='prd'?'product':'lo', dev: { //测试环境 apiUrl: 'https://apitest.zhiqiyun.com', }, product: { //生产环境 apiUrl: 'https://apimall.zhiqiyun.com', }, lo: { //生产环境 apiUrl: 'http://192.168.110.180:8078', } } class ReqBase { /** * 构造函数 * @param {Object} options 接口参数,appId 为必选参数 */ constructor(options) { console.log('ReqBase sdk:', options); // if (!options.appId) { // throw Error('appId值不能为空'); // } // this.appId = options.appId; this.appId = options.appId; this.getSystemInfo(); } /* * 获取token */ getToken(url, success) { let token = ''; var dataP = {}; var header = {}; token = this.getStorage('ACTION_TOKEN'); dataP = {}; header.appId = this.appId; header['content-type'] = 'application/json;charset=UTF-8'; if (token) { header.authorityToken = token; success.call(this, token); return false; } else { this.loadToken(header, dataP, data => { success.call(this, data); }); } } loadToken(header, dataP, success) { let that = this uni.request({ url: env[env.NODE_ENV].apiUrl + '/api/access_token', data: dataP, method: 'get', header: header, success(json) { if (json.statusCode !== 200) { console.log('ACTION_TOKEN 获取失败>>>>>', json.data.message); return; } var token = json.data.data; that.setStorage('ACTION_TOKEN', token); success.call(that, token); }, fail(res) { console.log('ACTION_TOKEN 获取失败>>>>>', res); } }) } clearValueEmpty(data) { let keyValue = {}; for (let key in data) { let value = typeof data[key]; if (value == 'string' && value) { if (data[key] != 'undefined' || data[key] != " " || data[key] != undefined || data[key] != null) { if (data[key] != '') { keyValue[key] = data[key]; } } if (data[key] != '') { keyValue[key] = data[key]; } } else if (value == 'number' && value != null) { keyValue[key] = data[key]; } else if (value == 'boolean') { keyValue[key] = data[key]; } else { if (data[key]) keyValue[key] = data[key]; } } return keyValue; } /** * 公共请求 * * @param {*} url 请求url * @param {*} data 请求参数 * @param {*} method 请求方法 * @param {*} success 成功函数 * @param {*} isLoad 是否显示加载提示 */ baseRequest(url, data, method, success) { var header = {}; var deviceInfo = this.getStorage('deviceInfo'); // #ifdef APP-PLUS var sysMac = this.getStorage('sysMac'); // #endif var device_info = {}; if (deviceInfo) { data.browser = deviceInfo.browserName; data.browserVersion = deviceInfo.browserVersion; data.manufacturer = deviceInfo.deviceBrand; data.model = deviceInfo.deviceModel; data.deviceId = deviceInfo.deviceId; data.os = deviceInfo.osName; data.osVersion = deviceInfo.osVersion; // #ifdef APP-PLUS if (sysMac) { data.sysMac = sysMac; } // #endif } var dataP = { uri: url, code: 'sys.behavior.service', body: data } this.getToken(url, token => { header.authorityToken = token; header.appId = this.appId; header['content-type'] = 'application/json;charset=UTF-8'; uni.request({ url: env[env.NODE_ENV].apiUrl + '/api/v3/application/public', data: this.clearValueEmpty(dataP), method: method, header: header, success(json) { if (json.statusCode !== 200) { console.log('action sdk error:', json.data.message); success.call(this, -1);//接口异常返回-1,不影响其他应用 return; } if (json.data.code !== 0) { console.log('action sdk error:', json.data.msg); success.call(this, -1);//接口异常返回-1,不影响其他应用 return; } let dataR = json.data.data; if (typeof dataR === 'string' && dataR.indexOf("{") === 0) { dataR = JSON.parse(dataR); } success.call(this, dataR); }, fail() { } }) }) } /**GET请求 */ getRequest(url, data, success) { this.baseRequest(url, data, 'get', success); } /**POST请求 */ postRequest(url, data, success) { this.baseRequest(url, data, 'post', success); } /**GET请求 请求api项目 */ getRequestAPI(url, data, success, isLoad){ this.baseRequestAPI(url, data, 'get', success, isLoad); } /** * 公共请求 请求api项目 * * @param {*} url 请求url * @param {*} data 请求参数 * @param {*} method 请求方法 * @param {*} success 成功函数 * @param {*} isLoad 是否显示加载提示 */ baseRequestAPI(url, data, method, success) { var header = {}; this.getToken(url, token => { header.authorityToken = token; header.appId = this.appId; if (url.indexOf('/v3') != -1 || url.indexOf('/paper/create') != -1) { header['content-type'] = 'application/json;charset=UTF-8' } else { header['content-type'] = 'application/x-www-form-urlencoded' } uni.request({ url: env[env.NODE_ENV].apiUrl + url, data: this.clearValueEmpty(data), method: method, header: header, success(json) { if (json.statusCode !== 200) { console.log('action sdk error:', json.data.message); success.call(this, -1);//接口异常返回-1,不影响其他应用 return; } if (json.data.code !== 0) { console.log('action sdk error:', json.data.msg); success.call(this, -1);//接口异常返回-1,不影响其他应用 return; } let dataR = json.data.data; if (typeof dataR === 'string' && dataR.indexOf("{") === 0) { dataR = JSON.parse(dataR); } success.call(this, dataR); }, fail() { } }) }) } setStorage(key, value, expire = 0) { let obj = { data: value, //存储的数据 time: Date.now() / 1000, //记录存储的时间戳 expire: expire //记录过期时间,单位秒 } uni.setStorageSync(env.NODE_ENV + "_" + key, JSON.stringify(obj)) } getStorage(key) { let val = uni.getStorageSync(env.NODE_ENV + "_" + key) if (!val) { return null } try { val = JSON.parse(val) if (val.expire && Date.now() / 1000 - val.time > val.expire) { this.removeStorage(key) return null } else { return val.data } } catch (e) { return uni.getStorageSync(env.NODE_ENV + "_" + key) } return uni.getStorageSync(env.NODE_ENV + "_" + key) } removeStorage(key) { return uni.removeStorageSync(env.NODE_ENV + "_" + key); } getSystemInfo() { var that = this; console.log('getSystemInfo>>>>>>>'); uni.getSystemInfo({ success: function(res) { that.setStorage('deviceInfo', res); // #ifdef APP-PLUS that.getDeviceMac(); // #endif } }); }; getDeviceMac() { var deviceMac = "" if (this.isAndroidSys()) { var net = plus.android.importClass("java.net.NetworkInterface"); var wl0 = net.getByName('wlan0'); var macByte = wl0.getHardwareAddress(); deviceMac = ''; for (var i = 0; i < macByte.length; i++) { var tmp = ""; var num = macByte[i]; if (num < 0) { tmp = (255 + num + 1).toString(16); } else { tmp = num.toString(16); } if (tmp.length == 1) { tmp = "0" + tmp; } deviceMac += tmp; } } this.setStorage('sysMac', deviceMac); }; /** * 安卓环境 */ isAndroidSys() { return uni.getSystemInfoSync().platform == 'android'; } } module.exports = ReqBase