utils.js 4.42 KB
Newer Older
zhangpeng committed
1
import Config from '../static/config/index.js';
张成 committed
2 3
import WXBizDataCrypt from './WXBizDataCrypt.js';
import { $EventBus } from './EventBus';
张成 committed
4

张成 committed
5 6
export default {
  // 组装提交订单数据
张成 committed
7
  AssemblyOrder(totalPrice, totalNum, buyType, shopCarInfo) {
张成 committed
8 9 10 11
    if (shopCarInfo && totalPrice > 0 && totalNum > 0) {
      let orderDetails = [];
      for (let i = 0; i < shopCarInfo.length; i++) {
        let item = shopCarInfo[i];
张成 committed
12

张成 committed
13 14 15 16 17 18
        let res = {
          goodsId: item.goodsId, //商品id
          goodsName: item.name, //商品名称
          machineId: undefined, //点单屏机器ID
          num: item.num, //当前sku数量
          realAmount: item.sku.discount * item.num, //实付金额
张成 committed
19
          amount: item.sku.price * item.num, //总价格
张成 committed
20 21 22 23 24 25 26 27
          specRuleDetail: JSON.stringify(item.sku.rules), //规格选项详情
          specRuleIds: item.sku.rules.map((item) => item.ruleId).join(','), //规格选项详情
          skuId: item.skuId, //"sku ID"
          goodsCategory: item.categoryId, //商品分类
        };

        orderDetails.push(res);
      }
张成 committed
28
      const shopData = uni.getStorageSync('shopData');
张成 committed
29 30 31
      let DAta = {
        amount: totalPrice, //商品总金额
        goodsNum: totalNum, //商品总数量
张成 committed
32
        shopId: shopData.id, //店铺f
张成 committed
33
        machineId: undefined, //机器ID
张成 committed
34
        source: buyType, //小程序固定传3
张成 committed
35 36 37 38 39 40 41 42 43 44
        orderDetails: orderDetails,
      };
      return DAta;
    } else {
      uni.showToast({
        title: '请选择商品',
      });
    }
  },
  // 加入购物车数据
张成 committed
45 46
  async getallNum(Obj, callback) {
    console.log(Obj);
张成 committed
47 48
    let shopCarInfo = uni.getStorageSync('shopCarInfo') || [];
    let size = 0;
张成 committed
49
    shopCarInfo.forEach((item) => (size += item.num));
张成 committed
50 51 52 53 54
    if (size >= 9) {
      uni.showToast({
        title: '最多可一次购买9杯',
        icon: 'none',
      });
张成 committed
55
      return false;
张成 committed
56
    }
张成 committed
57

张成 committed
58 59 60 61 62 63 64 65 66 67
    if (shopCarInfo) {
      let currentGoods = shopCarInfo.find((v) => v.goodsId == Obj.goodsId && v.skuId == Obj.skuId);
      if (currentGoods) {
        currentGoods.num += 1;
      } else {
        shopCarInfo.push(Obj);
      }
    } else {
      shopCarInfo = [Obj];
    }
张成 committed
68
    console.log(Obj);
张成 committed
69 70
    uni.setStorageSync('shopCarInfo', shopCarInfo);
    $EventBus.$emit('updateCar');
张成 committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    if (callback) {
      callback();
    }
  },
  // 商品详情立即购买
  async addGoods(Obj) {
    let goodsList = [];
    let size = 0;
    goodsList.forEach((item) => (size += item.num));
    if (size >= 9) {
      uni.showToast({
        title: '最多可一次购买9杯',
        icon: 'none',
      });
      return false;
    }
    if (goodsList) {
      let currentGoods = goodsList.find(
        (item) => item.goodsId == Obj.goodsId && item.skuId == Obj.skuId
      );
      if (currentGoods) {
        currentGoods.num += 1;
      } else {
        goodsList.push(Obj);
      }
    } else {
      goodsList = [Obj];
    }
    uni.setStorageSync('goodsList', goodsList);
张成 committed
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  },

  // 解密手机
  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() {
    uni.scanCode({
      success: (res) => {
        this.scaninfo = res;
        if (res.result) {
          if (res.result.includes(Config.AVScanUrl)) {
            uni.reLaunch({
              url: `/pages/index/index?q=${res.result}`,
            });
          } else {
            uni.showToast({
              title: '二维码错误',
              icon: 'none',
            });
          }
        }
      },
      fail() {
        uni.showToast({
          icon: 'none',
          title: '扫码失败',
        });
      },
    });
  },
  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);
    }
  },
};