import gcoord from "./gcoord.js"; import {jsonp} from 'vue-jsonp' const HOSTARR = { // 'development': 'http://safety.115.langye.net', // 'production': 'http://safety.115.langye.net', 'development': 'http://192.168.60.99:9001', 'production': 'http://192.168.60.99:9001' }; // 审核状态 const HOST = HOSTARR[process.env.NODE_ENV]; const formatTime = date => { const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); return [year, month, day].map(formatNumber).join('-'); }; const user_info_key = "user_info_key"; const user_key = "userInfo"; const getExpireKey = key => key + '_expire_time' const setStorageSync = (key, value, expire) => { if (expire) { let time = new Date().getTime() + expire * 1000 time && uni.setStorageSync(getExpireKey(key), time) } uni.setStorageSync(key, value) } const getStorageSync = key => { let expire = uni.getStorageSync(getExpireKey(key)) if (expire && expire < new Date().getTime()) { uni.removeStorageSync(getExpireKey(key)) uni.removeStorageSync(key) } return uni.getStorageSync(key) } const formatTimeMin = date => { const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); const hour = date.getHours(); const minute = date.getMinutes(); const second = date.getSeconds(); return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join( ':'); }; const formatTimeAll = date => { const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); const hour = date.getHours(); const minute = date.getMinutes(); const second = date.getSeconds(); return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join( ':'); }; const formatNumber = n => { n = n.toString(); return n[1] ? n : '0' + n; }; // 快捷显示alert const alert = msg => { uni.showModal({ title: '', content: msg, showCancel: false }); }; // 订单状态 const toast = msg => { uni.showModal({ title: '', content: msg, showCancel: false }); }; // 订单状态 const request = options => { if (!options.customLoading) { uni.showLoading({ title: '正在加载' }); } else { } options.url = HOST + options.api; options.header = { ...options.header, //'Accept': 'application/json', //'Connection': 'keep-alive' //'content-type': 'application/json' }; // 如果已登录,请求中拼openId var access_token = uni.getStorageSync(user_info_key).token; var request_token = access_token // var request_token = access_bd_token?access_bd_token:access_token // console.log(request_token) if (isLogin()) { options.data = { ...options.data, 'token': request_token }; }// 如果是POST方法s if (options.method == 'POST' && !isNull(request_token)) { // 拼时间戳 options.data.ts = new Date().getTime(); } uni.request({ ...options, success: function(res) { uni.hideLoading(); console.log("res",res) if (res.statusCode != 200) { if (options.utilFail != undefined) { if (res.statusCode == 401) { uni.clearStorageSync(); uni.navigateTo({ url: '/pages/index/login' }); } else { options.utilFail('TODO: 特殊处理非200错误(' + res.statusCode + ')'); } } } else { if (res.data.hasOwnProperty("errcode")) { if (options.utilFail != undefined) { options.utilFail(res.data.errmsg || '接口发生未知错误'); if(res.data.errcode==40001){ console.log("http://localhost:8080/h5/#/",res.data.hasOwnProperty("errcode")) setTimeout(function(){ uni.removeStorageSync("user_info_key") uni.navigateTo({ url: '/pages/index/login' }); },1500) } } 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(); } else { // 当前页面请求数量-1 // if (options.bindThis) { // options.bindThis.setData({ // loadingCount: options.bindThis.data.loadingCount - 1 // }); // } } } }); }; // 提交formID封装 const isLogin = () => { var userInfo = uni.getStorageSync(user_info_key); return !isNull(userInfo.token) }; // 判断是否为空 const isNull = p => { return p == '' || p == undefined || p == null || p == 'undefined' || p == 'null'; }; // 正则 const phoneRegex = /^1[3456789]\d{9}$/; const idCardRegex = /^\d{17}([0-9]|X)$/; // 去掉特殊字符 const filterSpecialChars = str => { return str.replace(/[^\u4e00-\u9fa5a-zA-Z0-9\w]/g, ''); }; // 生成随机字符串 const randomStr = length => { var ret = ''; var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ]; for (var i = 0; i < length; i++) { var randomIndex = Math.round(Math.random() * (arr.length - 1)); ret += arr[randomIndex]; } return ret; }; // proType对应的中文描述 const weekdayArr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']; // 是否合法的身份证 const isValidCardID = cardID => { // Step1: 先判断格式 if (!idCardRegex.test(cardID)) return false; // Step2: 校验最后一位 var code = cardID.split(''); //加权因子 var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //校验位 var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]; var sum = 0; var ai = 0; var wi = 0; for (var i = 0; i < 17; i++) { ai = code[i]; wi = factor[i]; sum += ai * wi; } var last = parity[sum % 11]; return parity[sum % 11] == code[17]; }; const shareInfo = { title:"", imageUrl:"/static/share.jpg" } /** * UTF16和UTF8转换对照表 * U+00000000 – U+0000007F 0xxxxxxx * U+00000080 – U+000007FF 110xxxxx 10xxxxxx * U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx * U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx * U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx * U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */ var Base64 = { // 转码表 table: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' ], UTF16ToUTF8: function(str) { var res = [], len = str.length; for (var i = 0; i < len; i++) { var code = str.charCodeAt(i); if (code > 0x0000 && code <= 0x007F) { // 单字节,这里并不考虑0x0000,因为它是空字节 // U+00000000 – U+0000007F 0xxxxxxx res.push(str.charAt(i)); } else if (code >= 0x0080 && code <= 0x07FF) { // 双字节 // U+00000080 – U+000007FF 110xxxxx 10xxxxxx // 110xxxxx var byte1 = 0xC0 | code >> 6 & 0x1F; // 10xxxxxx var byte2 = 0x80 | code & 0x3F; res.push(String.fromCharCode(byte1), String.fromCharCode(byte2)); } else if (code >= 0x0800 && code <= 0xFFFF) { // 三字节 // U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx // 1110xxxx var byte1 = 0xE0 | code >> 12 & 0x0F; // 10xxxxxx var byte2 = 0x80 | code >> 6 & 0x3F; // 10xxxxxx var byte3 = 0x80 | code & 0x3F; res.push(String.fromCharCode(byte1), String.fromCharCode(byte2), String.fromCharCode( byte3)); } else if (code >= 0x00010000 && code <= 0x001FFFFF) { // 四字节 // U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (code >= 0x00200000 && code <= 0x03FFFFFF) { // 五字节 // U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ { // 六字节 // U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return res.join(''); }, UTF8ToUTF16: function(str) { var res = [], len = str.length; var i = 0; for (var i = 0; i < len; i++) { var code = str.charCodeAt(i); // 对第一个字节进行判断 if ((code >> 7 & 0xFF) == 0x0) { // 单字节 // 0xxxxxxx res.push(str.charAt(i)); } else if ((code >> 5 & 0xFF) == 0x6) { // 双字节 // 110xxxxx 10xxxxxx var code2 = str.charCodeAt(++i); var byte1 = (code & 0x1F) << 6; var byte2 = code2 & 0x3F; var utf16 = byte1 | byte2; res.push(Sting.fromCharCode(utf16)); } else if ((code >> 4 & 0xFF) == 0xE) { // 三字节 // 1110xxxx 10xxxxxx 10xxxxxx var code2 = str.charCodeAt(++i); var code3 = str.charCodeAt(++i); var byte1 = code << 4 | code2 >> 2 & 0x0F; var byte2 = (code2 & 0x03) << 6 | code3 & 0x3F; utf16 = (byte1 & 0x00FF) << 8 | byte2; res.push(String.fromCharCode(utf16)); } else if ((code >> 3 & 0xFF) == 0x1E) { // 四字节 // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if ((code >> 2 & 0xFF) == 0x3E) { // 五字节 // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ { // 六字节 // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return res.join(''); }, encode: function(str) { if (!str) { return ''; } var utf8 = this.UTF16ToUTF8(str); // 转成UTF8 var i = 0; // 遍历索引 var len = utf8.length; var res = []; while (i < len) { var c1 = utf8.charCodeAt(i++) & 0xFF; res.push(this.table[c1 >> 2]); // 需要补2个= if (i == len) { res.push(this.table[(c1 & 0x3) << 4]); res.push('=='); break; } var c2 = utf8.charCodeAt(i++); // 需要补1个= if (i == len) { res.push(this.table[(c1 & 0x3) << 4 | c2 >> 4 & 0x0F]); res.push(this.table[(c2 & 0x0F) << 2]); res.push('='); break; } var c3 = utf8.charCodeAt(i++); res.push(this.table[(c1 & 0x3) << 4 | c2 >> 4 & 0x0F]); res.push(this.table[(c2 & 0x0F) << 2 | (c3 & 0xC0) >> 6]); res.push(this.table[c3 & 0x3F]); } return res.join(''); }, decode: function(str) { if (!str) { return ''; } var len = str.length; var i = 0; var res = []; while (i < len) { var code1 = this.table.indexOf(str.charAt(i++)); var code2 = this.table.indexOf(str.charAt(i++)); var code3 = this.table.indexOf(str.charAt(i++)); var code4 = this.table.indexOf(str.charAt(i++)); var c1 = code1 << 2 | code2 >> 4; var c2 = (code2 & 0xF) << 4 | code3 >> 2; var c3 = (code3 & 0x3) << 6 | code4; res.push(String.fromCharCode(c1)); if (code3 != 64) { res.push(String.fromCharCode(c2)); } if (code4 != 64) { res.push(String.fromCharCode(c3)); } } return this.UTF8ToUTF16(res.join('')); } }; const initAddress = callback => { var that = this var addInfo = { latitude:"", longitude:"", address:"" } uni.getLocation({ type: "wgs84", geocode: true, success(res) { var loc = null console.log(typeof(window.android)) if (typeof(window.android) != undefined && typeof(window.android) != "undefined") { loc = window.android.getLocInfo(); } if (loc) { addInfo.latitude = JSON.parse(loc).lat; addInfo.longitude = JSON.parse(loc).lon; } else { addInfo.latitude = res.latitude; addInfo.longitude = res.longitude; } let _latlog = gcoord.transformFromWGSToGCJ(addInfo.latitude, addInfo.longitude); console.log(_latlog) const url = "https://apis.map.qq.com/ws/geocoder/v1/?key=PCBBZ-2Y4L5-V6AIY-Q2LHL-FSGLT-ZRBL6&location=" + _latlog.latitude + "," + _latlog.longitude + "&output=jsonp" jsonp( url ).then(re => { addInfo.address = re.result.formatted_addresses.recommend callback(addInfo) }) } }) } module.exports = { HOST: HOST, formatTime: formatTime, formatTimeAll: formatTimeAll, formatNumber: formatNumber, formatTimeMin:formatTimeMin, alert: alert, request: request, isLogin: isLogin, isNull: isNull, phoneRegex: phoneRegex, idCardRegex: idCardRegex, filterSpecialChars: filterSpecialChars, randomStr: randomStr, weekdayArr: weekdayArr, Base64: Base64, toast: toast, isValidCardID:isValidCardID, shareInfo:shareInfo, initAddress:initAddress, setStorageSync:setStorageSync, getStorageSync:getStorageSync };