123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- export default {
- /*
- * 合并数组
- */
- SplitArray(list, sp) {
- if (typeof list != 'object') return [];
- if (sp === undefined) sp = [];
- for (var i = 0; i < list.length; i++) {
- sp.push(list[i]);
- }
- return sp;
- },
- trim(str) {
- return String.prototype.trim.call(str);
- },
- $h: {
- //除法函数,用来得到精确的除法结果
- //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
- //调用:$h.Div(arg1,arg2)
- //返回值:arg1除以arg2的精确结果
- Div: function(arg1, arg2) {
- arg1 = parseFloat(arg1);
- arg2 = parseFloat(arg2);
- var t1 = 0,
- t2 = 0,
- r1, r2;
- try {
- t1 = arg1.toString().split(".")[1].length;
- } catch (e) {}
- try {
- t2 = arg2.toString().split(".")[1].length;
- } catch (e) {}
- r1 = Number(arg1.toString().replace(".", ""));
- r2 = Number(arg2.toString().replace(".", ""));
- return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
- },
- //加法函数,用来得到精确的加法结果
- //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
- //调用:$h.Add(arg1,arg2)
- //返回值:arg1加上arg2的精确结果
- Add: function(arg1, arg2) {
- arg2 = parseFloat(arg2);
- var r1, r2, m;
- try {
- r1 = arg1.toString().split(".")[1].length
- } catch (e) {
- r1 = 0
- }
- try {
- r2 = arg2.toString().split(".")[1].length
- } catch (e) {
- r2 = 0
- }
- m = Math.pow(100, Math.max(r1, r2));
- return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
- },
- //减法函数,用来得到精确的减法结果
- //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
- //调用:$h.Sub(arg1,arg2)
- //返回值:arg1减去arg2的精确结果
- Sub: function(arg1, arg2) {
- arg1 = parseFloat(arg1);
- arg2 = parseFloat(arg2);
- var r1, r2, m, n;
- try {
- r1 = arg1.toString().split(".")[1].length
- } catch (e) {
- r1 = 0
- }
- try {
- r2 = arg2.toString().split(".")[1].length
- } catch (e) {
- r2 = 0
- }
- m = Math.pow(10, Math.max(r1, r2));
- //动态控制精度长度
- n = (r1 >= r2) ? r1 : r2;
- return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
- },
- //乘法函数,用来得到精确的乘法结果
- //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
- //调用:$h.Mul(arg1,arg2)
- //返回值:arg1乘以arg2的精确结果
- Mul: function(arg1, arg2) {
- arg1 = parseFloat(arg1);
- arg2 = parseFloat(arg2);
- var m = 0,
- s1 = arg1.toString(),
- s2 = arg2.toString();
- try {
- m += s1.split(".")[1].length
- } catch (e) {}
- try {
- m += s2.split(".")[1].length
- } catch (e) {}
- return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
- },
- },
- // 获取地理位置;
- $L: {
- async getLocation() {
- // #ifdef APP-PLUS
- let status = await this.checkPermission();
- if (status !== 1) {
- return;
- }
- // #endif
- // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
- let status = await this.getSetting();
- if (status === 2) {
- this.openSetting();
- return;
- }
- // #endif
- this.doGetLocation();
- },
- doGetLocation() {
- uni.getLocation({
- type:'gcj02',
- success: (res) => {
- uni.removeStorageSync('CACHE_LONGITUDE');
- uni.removeStorageSync('CACHE_LATITUDE');
- uni.setStorageSync('CACHE_LONGITUDE', res.longitude);
- uni.setStorageSync('CACHE_LATITUDE', res.latitude);
- },
- fail: (err) => {
- // #ifdef MP-BAIDU
- if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
- this.openSetting();
- }
- // #endif
- // #ifndef MP-BAIDU
- if (err.errMsg.indexOf("auth deny") >= 0) {
- uni.showToast({
- title: "访问位置被拒绝"
- })
- } else {
- uni.showToast({
- title: err.errMsg
- })
- }
- // #endif
- }
- })
- },
- getSetting: function() {
- return new Promise((resolve, reject) => {
- uni.getSetting({
- success: (res) => {
- if (res.authSetting['scope.userLocation'] === undefined) {
- resolve(0);
- return;
- }
- if (res.authSetting['scope.userLocation']) {
- resolve(1);
- } else {
- resolve(2);
- }
- }
- });
- });
- },
- openSetting: function() {
- uni.openSetting({
- success: (res) => {
- if (res.authSetting && res.authSetting['scope.userLocation']) {
- this.doGetLocation();
- }
- },
- fail: (err) => {}
- })
- },
- // #ifdef APP-PLUS
- async checkPermission() {
- let status = permision.isIOS ? await permision.requestIOS('location') :
- await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
- if (status === null || status === 1) {
- status = 1;
- } else if (status === 2) {
- uni.showModal({
- content: "系统定位已关闭",
- confirmText: "确定",
- showCancel: false,
- success: function(res) {}
- })
- } else if (status.code) {
- uni.showModal({
- content: status.message
- })
- } else {
- uni.showModal({
- content: "需要定位权限",
- confirmText: "设置",
- success: function(res) {
- if (res.confirm) {
- permision.gotoAppSetting();
- }
- }
- })
- }
- return status;
- },
- // #endif
- }
- }
|