import { payH5API, alipayH5API } from '@/request/index/index.js'; import { WXpayAPI, WXpayDYPAPI } from '@/request/restaurant/index.js'; import { WXpayH5API } from '@/request/recharge/index.js'; import { mapMutations, } from 'vuex'; // 客服链接 import { RXSERVICE } from '@/static/js/serviceurl.js'; // 公共mixins export default { computed: {}, // 方法 methods: { ...mapMutations(['GetState']), // 七牛云地址拼接 qnyurl(src, address = 'v2') { // 瑞幸咖啡图片链接 return `https://img.agrimedia.cn/chwl/${address}/${src}`; // switch (address) { // case 'rx': // // 瑞幸咖啡图片链接 // return `https://img.agrimedia.cn/chwl/rx/${src}`; // case 'xbk': // // 瑞幸咖啡图片链接 // return `https://img.agrimedia.cn/chwl/xbk/${src}`; // case 'mdl': // // 麦当劳图片链接 // return `https://img.agrimedia.cn/chwl/mdl/${src}`; // case 'fuel': // // 加油图片连接地址 // return `https://img.agrimedia.cn/chwl/fuel/${src}`; // case 'dyp': // // 电影票图片连接地址 // return `https://img.agrimedia.cn/chwl/dyp/${src}`; // default: // // 默认返回链接 // return `https://img.agrimedia.cn/chwl/v2/${src}`; // } // // 注意七牛云上传地址 // return `https://img.agrimedia.cn/chwl/v2/${src}` }, // 保留小数点 RetainDecimalPoint(num = 0, iscarry = true) { // 保留小数点后几位 let r = 2; // 因为该函数分为用户手动输入和后台传入方式 // 当用户手动输入的价格则不需要除以100 // 后台传入的价格是以分传入,所以需要除以一百 if (iscarry) { num = num / 100; } // 转换之后的num let s = '' + num; // 检测传入参数是否为带小数点参数 if (s.indexOf('.') != -1) { // 传入参数小数点不足所指定位数则补位 // 获取小数点位置 let index = s.indexOf('.'); // 拿到小数点之后有几位 let t = s.substring(index + 1, s.length); // 如果小数点之后位数小于指定位数则补齐 if (t['length'] < r) { for (let i = 0; i < r - t['length']; i++) { s += '0'; } } else { return s.substring(0, index + 3); } return s; } else { // 是否正数 let number = /(^[1-9]\d*$)/; // 是否为正数 if (number.test(num)) { // 定义数组 let a = []; // 结果 let n = ''; // 将初始值循环至数组 for (let key = 0; key < s['length']; key++) { a.push(s[key]); }; // 取最终值 for (let i = 0; i < a.length; i++) { n += a[i]; if (i >= a.length - 1) { n += '.' for (let i = 0; i < r; i++) { n += '0'; } } }; return n } else { return s } } }, // 保留小数点2.0 // 前一个方法局限性太低,但又因为很多地方使用所以再开一个方法 /** * @param {String} num需要处理的数据(不管是数字还是字符串最终都会先被处理成字符串) * @param {Boolean} iscarry是否需要对数据经行除以一百(此处属性用于传入的数据是价格且为【分】单位) * @param {Number} toFixed是否需要对数据经行小数点保留处理(保留的位数) * 注意:若使用了try catch,则将不会显示错误信息 */ RetainDecimalPoints({ num = 0, iscarry = true, toFixed = 2 }) { console.log(num.__proto__.constructor.name, 'numnum') if (num.__proto__.constructor.name !== 'string' && typeof num !== 'string') { throw '所传参数不是字符串'; return; } // 保留小数点后几位 toFixed = Number(toFixed) // 因为该函数分为用户手动输入和后台传入方式 // 当用户手动输入的价格则不需要除以100 // 后台传入的价格是以分传入,所以需要除以一百 if (iscarry) { num = '' + (num / 100); }; // 检测传入参数是否为带小数点参数 let index = num.indexOf('.'); // 传入数据携带小数点 if (index != -1) { // 返回数据是否需要携带小数点 if (toFixed > 0) { // 拿到现有数据小数点之后存在几位 let _l = num.substring(index + 1, num.length); // 如果当前数据小数点之后现存位数大于给定返回位数则截取 if (_l['length'] > toFixed) { num = num.substring(0, (index + 1) + toFixed); } else { // 如果当前数据小数点之后现存位数小于给定返回位数则循环补位 for (let _i = 0; _i < toFixed - _l['length']; _i++) { num += '0'; } } } else { num = num.substring(0, index); } } else { // 是否正数 let number = /(^[1-9]\d*$)/; // 是否为正数 if (number.test(num)) { // 定义数组 let a = []; // 结果 let n = ''; // 将初始数值赋值至数组 a = num.split(""); // 取最终值 for (let i = 0; i < a.length; i++) { n += a[i]; if (i >= a.length - 1) { n += '.'; for (let i = 0; i < toFixed; i++) { n += '0'; } } }; num = n; } }; return num; }, // 佣金格式化 _commission(num) { return this.RetainDecimalPoints({ num: String(num), iscarry: false, toFixed: 4 }) }, _fixed(num) { return num.toFixed(2) }, // 金额格式化(iscarry:true) _amount(num) { return this.RetainDecimalPoints({ num: String(num), iscarry: true, toFixed: 2 }) }, // 金额格式化(iscarry:false) __amount(num) { return this.RetainDecimalPoints({ num: String(num), iscarry: false, toFixed: 2 }) }, _deduction_prop_(deduction_price, price, key) { // console.log(deduction_price,price,'接收'); // 补贴价格(补贴了多少钱) const Subsidyprice = this.RetainDecimalPoints({ num: String(price * (deduction_price / 100)), iscarry: false, toFixed: 0 }); console.log(Subsidyprice, '补贴了多少'); // console.log(Subsidyprice,this.RetainDecimalPoints({num:Subsidyprice,iscarry:true,toFixed:2}),'Subsidyprice'); // 补贴后的价格 const AfterSubsidy = this.RetainDecimalPoints({ num: String(price - Subsidyprice), iscarry: true, toFixed: 2 }); console.log(AfterSubsidy, '补贴后的价格'); // console.log(AfterSubsidy,'AfterSubsidy'); const _D = { Subsidyprice: this.RetainDecimalPoints({ num: String(Subsidyprice), iscarry: true, toFixed: 2 }), AfterSubsidy } if (key) return _D[key]; return _D; }, // 解决小数点精度丢失问题 calc(num1, num2, calcStr) { var str1, // 转换为字符串的数字 str2, ws1 = 0, // ws1,ws2 用来存储传入的num的小数点后的数字的位数 ws2 = 0, // 赋默认值,解决当整数和小数运算时倍数计算错误导致的结果误差 bigger, // bigger和smaller用于加,减,除法找出小的那个数字,给后面补0,解决位数不对从而造成的计算错误的问题;乘法需要将结果除两个数字的倍数之和 smaller, // 例如:加减除法中1.001 + 2.03 ,如果不给2.03进行补0,最后会变成1001+203,数字错位导致结果错误;乘法中1.12*1.1会放大为112*11,所以结果需要除以1000才会是正确的结果,112*11/1000=1.232 zeroCount, // 需要补充0的个数 isExistDot1, // 传入的数字是否存在小数点 isExistDot2, sum, beishu = 1; // 将数字转换为字符串 str1 = num1.toString(); str2 = num2.toString(); // 是否存在小数点(判断需要计算的数字是不是包含小数) isExistDot1 = str1.indexOf('.') != -1 ? true : false; isExistDot2 = str2.indexOf('.') != -1 ? true : false; // 取小数点后面的位数 if (isExistDot1) { ws1 = str1.split('.')[1].length; } if (isExistDot2) { ws2 = str2.split('.')[1].length; } // 如ws1 和 ws2 无默认值,如果num1 或 num2 不是小数的话则 ws1 或 ws2 的值将为 undefined // bigger 和 smaller 的值会和预期不符 bigger = ws1 > ws2 ? ws1 : ws2; smaller = ws1 < ws2 ? ws1 : ws2; switch (calcStr) { // 加减法找出小的那个数字,给后面补0,解决位数不对从而造成的计算错误的问题 // 例如:1.001 + 2.03 ,如果不给2.03进行补0,最后会变成1001+203,数字错位导致结果错误 case "+": case "-": case "/": zeroCount = bigger - smaller; for (var i = 0; i < zeroCount; i++) { if (ws1 == smaller) { str1 += "0"; } else { str2 += "0"; } } break; case "*": // 乘法需要将结果除两个数字的倍数之和 bigger = bigger + smaller; break; default: return "暂不支持的计算类型,现已支持的有加法、减法、乘法、除法"; break; } // 去除数字中的小数点 str1 = str1.replace('.', ''); str2 = str2.replace('.', ''); // 计算倍数,例如:1.001小数点后有三位,则需要乘 1000 变成 1001,变成整数后精度丢失问题则不会存在 for (var i = 0; i < bigger; i++) { beishu *= 10; // 等价于beishu = beishu * 10; } num1 = parseInt(str1); num2 = parseInt(str2); // 进行最终计算并除相应倍数 switch (calcStr) { case "+": sum = (num1 + num2) / beishu; break; case "-": sum = (num1 - num2) / beishu; break; case "*": sum = (num1 * num2) / beishu; break; case "/": sum = num1 / num2; /* 除数与被除数同时放大一定倍数,不影响结果, 所以对数字进行放大对应倍数并进行补0操作后不用另对倍数做处理 */ break; default: return "暂不支持的计算类型,现已支持的有加法、减法、乘法、除法"; } return sum; }, // 返回页面 leftClick(page, url) { if (page) { uni.redirectTo({ url }); } else { uni.navigateBack({ delta: 1 }); } }, // H5客服连接 location() { const isWechat = getApp()['globalData']['parameters']?.isWechat; console.log('是否小程序环境', getApp()['globalData']['parameters']); if (isWechat) { wx.miniProgram.navigateTo({ url: `/pages/wxpay/openCustomerServiceChat?url=${RXSERVICE}` }); return; }; location.href = `https://work.weixin.qq.com/kfid/kfc2f4d0cb26562ffe1`; }, // 用于无前缀图片拼接 ImgSplicing(name) { return `https://img.agrimedia.cn/chwl/H5card/${name}`; }, // 点击店铺导航 openLocation() { uni.showToast({ title: '定位功能正在维护', icon: 'none' }) }, // 支付 payment(data) { uni.showLoading({ title: '加载中' }); /** * @param {String} order_sn订单号 * @param {Number} type订单类型 * @param {String} redirect_url回调地址 * @param {Object} money微信支付时需要的参数(该参数只在微信环境进入H5时才会存在) * @payment方法的回调地址是查询完订单状态之后要进入的页面 */ let { order_sn, type, redirect_url = '', } = data; console.log(order_sn, type, redirect_url, '支付方法参数1'); // 微信跳转H5下单支付时需要先获取支付参数然后跳转微信小程序支付 if (getApp()['globalData']['parameters']?.isWechat) { console.log("呼呼1") this._H5WXPAY(data).then(res => { uni.hideLoading(); console.log(res['data'], '微信支付参数'); const miniPayRequest = { timeStamp: encodeURIComponent(res['data']['result']['timestamp']), nonceStr: encodeURIComponent(res['data']['result']['nonceStr']), package: encodeURIComponent(res['data']['result']['package']), signType: encodeURIComponent(res['data']['result']['signType']), paySign: encodeURIComponent(res['data']['result']['paySign']), order_sn: res['data']['out_trade_no'], type, business: 'H5Environment', // 微信结算付款页面需要return_url字段 return_url: redirect_url }; console.log(encodeURIComponent(JSON.stringify(miniPayRequest)), '编码'); // return wx.miniProgram.navigateTo({ url: `/pages/wxpay/wxpay?data=${encodeURIComponent(JSON.stringify(miniPayRequest))}&isencodeURL=true` }); }) // // 电影票支付与点餐支付接口不同此处做出区分 // if(type == 4){ // WXpayDYPAPI({ // orderNo:order_sn, // }) // }else{ // WXpayAPI({ // out_order_num:order_sn, // money // }).then(res => { // uni.hideLoading(); // console.log(res['data'],'微信支付参数'); // const miniPayRequest = { // timeStamp: encodeURIComponent(res['data']['result']['timestamp']), // nonceStr: encodeURIComponent(res['data']['result']['nonceStr']), // package: encodeURIComponent(res['data']['result']['package']), // signType: encodeURIComponent(res['data']['result']['signType']), // paySign: encodeURIComponent(res['data']['result']['paySign']), // order_sn:res['data']['out_trade_no'], // type, // business:'restaurant', // // 微信结算付款页面需要return_url字段 // return_url:redirect_url // }; // console.log(encodeURIComponent(JSON.stringify(miniPayRequest)),'编码'); // wx.miniProgram.navigateTo({ // url: `/pages/wxpay/wxpay?data=${encodeURIComponent(JSON.stringify(miniPayRequest))}&isencodeURL=true` // }); // }) // } } else { console.log("呼呼2") // 测试 // location.href = `/pages/pay/index?order_sn=${order_sn}&type=${type}&redirect_url=${redirect_url}`; // return // 反之直接获取支付链接 // 回调地址添加标识。用于点击返回上一页 // redirect_url = `${redirect_url}`; WXpayH5API({ order_sn, type, redirect_url: `${getApp()['globalData']['urlfileName']}/pages/pay/index?order_sn=${order_sn}&type=${type}&redirect_url=${redirect_url}` }).then(res => { console.log( `${getApp()['globalData']['urlfileName']}/pages/pay/index?order_sn=${order_sn}&type=${type}&redirect_url=${redirect_url}` ) console.log(res['data']['redirect_url'], '支付方法地址'); // location.href = ; window.location.replace(res['data']['redirect_url']) }) } }, // H5在小程序环境下支付时每个接口不同所以封装为统一方法 _H5WXPAY(PayPara) { // 点餐微信支付接口 if (PayPara['type'] == 3) { let { order_sn: out_order_num, money } = PayPara; return WXpayAPI({ out_order_num, money }); }; // 电影票微信支付接口 if (PayPara['type'] == 4) { let { order_sn: orderNo } = PayPara; return WXpayDYPAPI({ orderNo }); } //vip充值 if (PayPara['type'] == 7) { let { order_sn, type, redirect_url } = PayPara; return WXpayH5API({ order_sn, type, redirect_url }); } }, /** * @支付回调统一管理方法 * @查询订单状态完成之后的回调地址redirect_url */ pay_statepage(parameters, redirect_url) { // console.log(order_sn,'pay_statepage方法参数'); let url = ''; url += redirect_url; console.log(url, '判断跳转链接'); /** * @拼接参数 */ console.log('拼接参数') if (typeof parameters !== 'undefined') { for (let key in parameters) { if (url.indexOf('?') == -1) { url += `?${key}=${parameters[key]}`; continue; }; url += `&${key}=${parameters[key]}` } }; console.log(url, '地址'); if (redirect_url) { // location.href = url; uni.redirectTo({ url }) } // uni.redirectTo({ // url // }); }, // 获取除去指定元素之外的高度 // bottomlb 底部留白距离 initScrollView({ el, bottomlb = 0 } = data) { let self = this; return new Promise((resolve, reject) => { uni.getSystemInfo({ success: (res) => { if (typeof el === 'string') { uni.createSelectorQuery().in(self).select(`.${el}`) .boundingClientRect( data => { console.log(data, self['NAVHEIGHT'], bottomlb, '元素高度'); // 有时候无法获取到订单顶部的切换类型元素高度,所以当获取不到时在此处直接赋值 if (data['height'] == 0 && el == 'OrderToggleType') { bottomlb = 45; } // 注意这里获得的高度宽度都是px,需要转换rpx // 可使用窗口高度,将px转换rpx resolve(((res.windowHeight - (data['height'] + self[ 'NAVHEIGHT'] + bottomlb)) * (750 / res[ 'windowWidth']))) }).exec() } else if (typeof el === 'object') { let hs = []; for (let i = 0; i < el['length']; i++) { uni.createSelectorQuery().in(self).select(`.${el[i]}`) .boundingClientRect( data => { hs.push(data['height']) console.log(hs, '高度'); }).exec() }; // // 宏任务执行完毕之后执行微任务 // setTimeout(() => { // console.log(this.sum(hs), '求和'); // resolve(((res.windowHeight - this.sum(hs)) * (750 / // res['windowWidth']))) // }, 0) } } }) // setTimeout(() => { // uni.getSystemInfo({ // success: (res) => { // // 因为支付宝平台不支持createSelectorQuery // // #ifdef MP-WEIXIN // uni.createSelectorQuery().in(self).select(".tabbars") // .boundingClientRect( // data => { // console.log(data, '高度'); // // 注意这里获得的高度宽度都是px,需要转换rpx // // 可使用窗口高度,将px转换rpx // self.scrollHeight = ((res.windowHeight) * (750 / // res['windowWidth'])); // resolve(); // }).exec() // // #endif // // 支付宝通过组件中获取到的元素信息经行计算 // // #ifdef MP-ALIPAY // // - // self.scrollHeight = ((res.windowHeight - self['$refs'][ // 'tabbars' // ]['tabbarsHeight']) * (750 / res // .windowWidth)); // resolve(); // // #endif // } // }) // }, 500) }); }, // 深拷贝 deepClone(source) { if (!source || typeof source !== 'object') { throw new Error('error arguments', 'shallowClone'); } var targetObj = source.constructor === Array ? [] : {}; for (var keys in source) { if (source.hasOwnProperty(keys)) { if (source[keys] && typeof source[keys] === 'object') { targetObj[keys] = source[keys].constructor === Array ? [] : {}; targetObj[keys] = deepClone(source[keys]); } else { targetObj[keys] = source[keys]; } } } return targetObj; }, // 复制到粘贴版 Copy(e) { uni.setClipboardData({ data: e, success: function() { uni.showToast({ title: '复制成功', icon: 'none' }) } }) }, // /** * @选择地址之后返回用户位置信息 */ GetUserchooseLocation() { return new Promise((resolve, reject) => { uni.chooseLocation({ success: function(res) { getCityName2(res['latitude'], res['longitude']).then(e => { console.log(e, 'eeeeeeeeeee'); let { result: { address, ad_info: { province, city, district, location, city_code, name }, formatted_addresses: { recommend } } } = e; resolve({ province, city, district, location, city_code, name, recommend, address }); }).catch(e => { reject(e); }) } }); }) }, // H5打开地图 handleOpen() { }, //元转分 regYuanToFen(yuan, digit) { var m = 0, s1 = yuan.toString(), s2 = digit.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) }, //获取url参数 getUrlParams(url) { try { // 通过 ? 分割获取后面的参数字符串 let urlStr = url.split('?')[1] // 创建空对象存储参数 let obj = {}; // 再通过 & 将每一个参数单独分割出来 let paramsArr = urlStr.split('&') for (let i = 0, len = paramsArr.length; i < len; i++) { // 再通过 = 将每一个参数分割为 key:value 的形式 let arr = paramsArr[i].split('=') obj[arr[0]] = arr[1]; } return obj } catch { return false } }, }, filters: { OrderState(v) { switch (v) { case 0: return '待支付'; case 1: return '待发货'; case 2: return '待收货'; case 3: return '已退款'; case 4: return '已取消'; case 5: return '完成'; } } } }