utils.js 3.69 KB
Newer Older
zhangpeng committed
1 2
import Config from '../static/config/index.js';
import WXBizDataCrypt from "./WXBizDataCrypt.js"; 
zhangcheng committed
3
import {$EventBus} from './EventBus'
4
import Store from '@/store'
zhangpeng committed
5
export default{
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	// 组装提交订单数据
	AssemblyOrder(totalPrice, totalNum) {
         let shopCarInfo = uni.getStorageSync('shopCarInfo').filter(v => v.flag == true);
         if (shopCarInfo && totalPrice > 0 && totalNum > 0) {
         	let orderDetails = [];
         	for (let i = 0; i < shopCarInfo.length; i++) {
         		let item = shopCarInfo[i];
         		let res = {
         			goodsId: item.goodsId, //商品id
         			goodsName: item.name, //商品名称
         			machineId: undefined, //点单屏机器ID
         			num: item.num, //当前sku数量
         			realAmount: item.skus[0].price * item.num, //实付金额
         			specRuleDetail: JSON.stringify(item.skus[0].rules), //规格选项详情
         			skuId: item.skus[0].skuId, //"sku ID"
         			goodsCategory: item.categoryId //商品分类
         		};
         		orderDetails.push(res);
         	}
         	let DAta = {
         		amount: totalPrice, //商品总金额
         		goodsNum: totalNum, //商品总数量
         		shopId: Store.getters.shopInfo[0].id, //店铺
         		machineId: undefined, //机器ID
         		source: 3, //小程序固定传3
         		orderDetails: orderDetails
         	}
         	return DAta;
         } else {
         	uni.showToast({
         		title: '请选择商品'
         	})
         }
	},
	// 加入购物车数据
zhangcheng committed
41
	getallNum(Obj){
zhangcheng committed
42
		let shopCarInfo = uni.getStorageSync('shopCarInfo');
zhangcheng committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
		console.log(shopCarInfo,'oooooo')
		    if (shopCarInfo){
				let currentGoods = shopCarInfo.find(v => v.goodsId == Obj.goodsId && v.skuId == Obj.skuId);
				if (currentGoods){
					console.log(currentGoods,'pppppp')
					currentGoods.num+=1;
				}else{
					shopCarInfo.push(Obj);
				}
				console.log(shopCarInfo,7777)
               
			}else{
				shopCarInfo = [Obj];
			}
			uni.setStorageSync('shopCarInfo', shopCarInfo);
             $EventBus.$emit('updateCar');
zhangcheng committed
59
	},
zhangpeng committed
60 61 62 63 64 65 66 67
	// 解密手机
	async onGetPhoneNumber(appid,session_key,encryptedData,iv) {
	 let pc = await new WXBizDataCrypt(appid,session_key);
	 let data = await pc.decryptData(encryptedData ,iv);
	 return data;
	},
	// 扫码方法
	scanCode() {
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		uni.scanCode({
			success:(res)=> {
				this.scaninfo=res;
				if(res.result){//res.result
				if(res.result.includes(Config.AVScanUrl)){
					uni.reLaunch({
					url:`/pages/index/index?q=${res.result}`
					})
				}else{
					uni.showToast({
					title:'二维码错误',
					icon:'none'
					})
				}
				// '0016429'||
				
				// `../pages/index/index`
			//    console.log(res.result,'内部扫码')
				}
			},
			fail() {
				uni.showToast({
					icon:'none',
					title:'扫码失败'
				})
			}
		});
zhangpeng committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	},
	formatDate(value) {
	                var date = new Date();
	                date.setTime(value);
	                var month = date.getMonth() + 1;
					if (month < 10)
					    month = "0" + month;
					var cdate = date.getDate();
					if (cdate < 10)
					    cdate = "0" + cdate;
	                var hours = date.getHours();
	                if (hours < 10)
	                    hours = "0" + hours;
	                var minutes = date.getMinutes();
	                if (minutes < 10)
	                    minutes = "0" + minutes;
	                var time = date.getFullYear() + "-" + month + "-" + cdate +
	                    " " + hours + ":" + minutes;
	                return time;
	            },
    numberFixed(data,fixed=2){
		if(data==undefined){
			return undefined
		}else if(data == 0){
			return '0'
		}else{
			return (Math.round(Number(data) * Math.pow(10,fixed)) / Math.pow(10,fixed)).toFixed(fixed);
		}
	}
}