| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721 |
- import { getGoodsDataApi,getProductDataApi,getPxClassDataApi } from "@/api/product/index";
- import { mapGetters, mapActions } from "vuex";
- import { copyObject } from "@/utils/util";
- export default {
- data() {
- return {
- pageType: "bill", //选择商品页面类别 用于区分不同功能的数据
- suitInfo: [], //成品-商品信息
- suitClassIndex: 0, //成品-当前商品类别下标
- productInfo: [], //商品信息
- pxClassInfo: [], //培训班开单列表
- classIndex: 0, //当前商品类别下标
- py: "", //拼音搜索条件
- type: "", //库存预警时,固定为waring,其他情况不传或者传空
- autoLoad: true, //自动获取商品信息
- isLimit: true //选择数量不能大于库存限制
- };
- },
- onLoad(options) {
- const { pageType } = options;
- if (pageType) {
- this.pageType = pageType;
- }
- if (this.autoLoad) {
- this.initGoodsData();//成品
- this.initData();//组合
- this.initPxClassData();//课程
- }
- },
- onUnload() {},
- computed: {
- ...mapGetters(["getSelectInfo"]),
- filterProductInfo() {
- let list = copyObject(this.productInfo);
- if (this.py) {
- let commonList = list.find(ele => {
- //在常用中过滤
- return ele.classId == -2;
- });
- if (commonList) {
- let commonChildList = [];
- if (!this.$util.isEmpty(commonList.child)) {
- commonList.child.forEach(ele => {
- let hasName = ele.itemName && ele.itemName.indexOf(this.py) >= 0;
- let elePy = (ele.py && ele.py.toLocaleUpperCase()) || "";
- let hasPy = elePy.indexOf(this.py.toLocaleUpperCase()) >= 0;
- if (hasName || hasPy) {
- commonChildList.push(ele);
- }
- });
- }
- commonList.child = commonChildList;
- list = [commonList];
- }
- }
- return list;
- },
- /**
- * 搜索、初始化数据
- * */
- suitFilterProductInfo() {
- let list = copyObject(this.suitInfo);
- if (this.py) {
- let commonList = list.find(ele => {
- //在常用中过滤
- return ele.classId == -2;
- });
- if (commonList) {
- let commonChildList = [];
- if (!this.$util.isEmpty(commonList.child)) {
- commonList.child.forEach(ele => {
- let hasName = ele.itemName && ele.itemName.indexOf(this.py) >= 0;
- let elePy = (ele.py && ele.py.toLocaleUpperCase()) || "";
- let hasPy = elePy.indexOf(this.py.toLocaleUpperCase()) >= 0;
- if (hasName || hasPy) {
- commonChildList.push(ele);
- }
- });
- }
- commonList.child = commonChildList;
- list = [commonList];
- }
- }
- return list;
- },
- /**
- * 当前页面选中的商品列表
- */
- selectList() {
- return this.getSelectInfo[this.pageType] || [];
- },
- scrollClassId() {
- const curClass = this.productInfo[this.classIndex];
- if (curClass) {
- return `class_${curClass.classId}`;
- }
- return "";
- },
- //选择商品总价
- allPrice() {
- let price = 0;
- if (this.selectList) {
- for (const item of this.selectList) {
- const itemInfo = this.getSelectItemById(item.id, item.classId);
- if (
- itemInfo &&
- !this.$util.isEmpty(itemInfo.bigPrice) &&
- !this.$util.isEmpty(itemInfo.smallPrice)
- ) {
- price += item.bigCount * Number(itemInfo.bigPrice);
- price += item.smallCount * Number(itemInfo.smallPrice);
- }
- }
- }
- return Math.round(price * 100) / 100;
- },
- //选择商品总数
- allCount() {
- // console.log(444444, this.selectList);
- return this.selectList.length;
- },
- /**
- * 描点-定位分类
- * */
- suitScrollClassId() {
- const curClass = this.suitInfo[this.suitClassIndex];
- if (curClass) {
- return `class_${curClass.classId}`;
- }
- return "";
- },
- /**
- * 多风格-选择商品总价
- * */
- allGoodsPrice() {
- let price = 0;
- if (this.selectList.goodsStyleList) {
- for (const item of this.selectList) {
- if(item.goodsStyle==0){
- price += item.bigCount * Number(item.price);
- }else{
- price += item.bigCount * Number(item.goodsStyleList[item.goodsStyleSelectIndex].price);
- }
- }
- }
- return Math.round(price * 100) / 100;
- },
- /**
- * 多风格-选择商品总数
- * */
- allGoodsCount() {
- let length = 0;
- this.selectList.forEach(element => {
- length += Number(element.bigCount)
- })
- return length;
- },
- /**
- * 当前选中商品列表中是否有没有库存的商品
- */
- hasNoStock() {
- let has = false;
- if (this.selectList) {
- for (const item of this.selectList) {
- const {
- bigCount,
- smallCount,
- bigNum,
- bigUnit,
- smallNum,
- smallUnit,
- ratio
- } = item;
- const ratioNum = Number(ratio);
- if (
- Number(bigCount) * ratioNum + Number(smallCount) >
- Number(bigNum) * ratioNum + Number(smallNum)
- ) {
- has = true;
- break;
- }
- }
- }
- return has;
- }
- },
- methods: {
- ...mapActions(["setSelectInfoByType", "resetSelectInfoByType"]),
- /**
- * 请求商品数据
- */
- async initData(extendInfo) {
- //extendInfo调用时的扩展参数 如 shopId
- try {
- let params = {
- py: this.py,
- type: this.type
- };
- if (extendInfo) {
- params = { ...params, ...extendInfo };
- }
- const { data } = await getProductDataApi(params);
- data.forEach(element => {
- element.child &&
- element.child.forEach(child => {
- child.classId = element.classId;
- child.className = element.className;
- });
- });
- this.productInfo = data;
- this.classIndex = 0;
- } catch (error) {
- } finally {
- uni.hideLoading();
- }
- },
- /**
- * 获取当前选中商品总价
- */
- getSelectItemPrice(item) {
- let price = 0;
- const { bigCount, smallCount, bigPrice, smallPrice } = item;
- let sum =
- Number(bigCount) * Number(bigPrice) +
- Number(smallCount) * Number(smallPrice);
- price = Math.round(sum * 100) / 100;
- return price;
- },
- /**
- * 成品-请求商品数据
- */
- async initGoodsData(extendInfo) {
- //extendInfo调用时的扩展参数 如 shopId
- try {
- let params = {
- py: this.py,
- type: this.type
- };
- if (extendInfo) {
- params = { ...params, ...extendInfo };
- }
- const { data } = await getGoodsDataApi(params);
- data.forEach(element => {
- element.child &&
- element.child.forEach(child => {
- child.classId = element.classId;
- child.className = element.className;
- });
- });
- this.suitInfo = data;
- this.suitClassIndex = 0;
- } catch (error) {
- } finally {
- uni.hideLoading();
- }
- },
- /**
- * 获取当前选中商品总价
- */
- getSelectGoodsPrice(item) {
- let price = 0;
- if(item.goodsStyle==0){
- price = item.bigCount * Number(item.price);
- }else{
- price = item.bigCount * Number(item.goodsStyleList[item.goodsStyleSelectIndex].price);
- }
- // let sum = 0;
- // if()
- // Number(bigCount) * Number(bigPrice) +
- // Number(smallCount) * Number(smallPrice);
- // price = Math.round(sum * 100) / 100;
- return price;
- },
- /**
- * 通过id获取当前选中的商品信息
- */
- getSelectItemById(id, classId) {
- let info = {
- bigCount: 0, //扎数
- smallCount: 0, //支数
- autoPrice: 0, //调价价格
- itemPrice: 0 //调价价格
- };
- const item =
- this.selectList &&
- this.selectList.find(ele => {
- return ele.id == id && ele.classId == classId;
- });
- if (item) {
- info = {
- ...item,
- bigCount: Number(item.bigCount),
- smallCount: Number(item.smallCount),
- autoPrice: Number(item.autoPrice),
- itemPrice: Number(item.itemPrice),
- };
- }
- return info;
- },
- /**
- * 通过id获取当前选中的商品类别信息 左边菜单角标 当前选中该类别商品多少件
- */
- getSelectClassById(classId) {
- let info = {
- bigCount: 0, //扎数
- smallCount: 0 //支数
- };
- const productItem =
- this.productInfo &&
- this.productInfo.find(ele => {
- return ele.classId == classId;
- });
- if (productItem && productItem.child) {
- for (const item of productItem.child) {
- for (const selectItem of this.selectList) {
- if (item.id == selectItem.id && item.classId == selectItem.classId) {
- info.bigCount += Number(selectItem.bigCount);
- info.smallCount += Number(selectItem.smallCount);
- info.itemPrice += selectItem.itemPrice?Number(selectItem.itemPrice):'';
- }
- }
- }
- }
- return info;
- },
- /**
- * 添加商品
- */
- addEvent(item) {
- const list = this.selectList;
- let has = false;
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- if (element.id == item.id && element.classId == item.classId) {
- has = true;
- if (this.isLimit) {
- if (element.bigCount < item.bigNum) {
- list[index] = { ...element, bigCount: ++element.bigCount };
- }
- // else if (element.smallCount < item.smallNum) {
- // list[index] = { ...element, smallCount: ++element.smallCount };
- // }
- } else {
- list[index] = { ...element, bigCount: ++element.bigCount };
- }
- if (this.isLimit) {
- const { bigCount, smallCount } = list[index];
- //超过库存时 设置为库存最大值
- const ratio = Number(item.ratio);
- if (
- Number(bigCount) * ratio + Number(smallCount) >
- Number(item.bigNum) * ratio + Number(item.smallNum)
- ) {
- list[index].bigCount = Number(item.bigNum);
- list[index].smallCount = Number(item.smallNum);
- this.$msg("库存不足");
- }
- }
- break;
- }
- }
- if (!has) {
- let info = { classId: item.classId, id: item.id };
- const classInfo =
- this.productInfo &&
- this.productInfo.find(element => {
- return element.classId == item.classId && element.child;
- });
- if (classInfo) {
- info = classInfo.child.find(ele => {
- return ele.id == item.id;
- });
- }
- let params = {
- ...info,
- bigCount: 1, //扎数
- smallCount: 0, //支数
- autoPrice: 0 //调价价格
- };
- if (info.bigNum && info.bigNum > 0) {
- list.push(params);
- } else {
- this.$msg("库存不足");
- }
- }
- this.setSelectInfoByType({ type: this.pageType, info: list });
- // this.$forceUpdate();
- },
- addCustomNumEvent(item,params1,type) {
- const list = this.selectList;
- // if (!has) {}
- let info = { classId: item.classId, id: item.id };
- const classInfo =
- this.productInfo &&
- this.productInfo.find(element => {
- return element.classId == item.classId && element.child;
- });
- if (classInfo) {
- info = classInfo.child.find(ele => {
- return ele.id == item.id;
- });
- }
- let params = {
- ...info,
- bigCount: 0, //扎数
- smallCount: 0, //支数
- autoPrice: 0 //调价价格
- };
- list.push(params);
- if(type!='noAdd'){
- this.setSelectInfoByType({ type: this.pageType, info: list });
- }
- this.$nextTick(()=>{
- this.customNum(params1)
- })
- // this.$forceUpdate();
- },
- /**
- * 减去商品
- */
- delEvent(item) {
- const list = this.selectList;
- let has = false;
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- if (element.id == item.id && element.classId == item.classId) {
- has = true;
- if (element.bigCount > 0) {
- list[index] = { ...element, bigCount: --element.bigCount };
- } else if (element.smallCount > 0) {
- list[index] = { ...element, smallCount: 0 };
- }
- if (this.isLimit) {
- const { bigCount, smallCount } = list[index];
- //超过库存时 设置为库存最大值
- const ratio = Number(item.ratio);
- if (
- Number(bigCount) * ratio + Number(smallCount) >
- Number(item.bigNum) * ratio + Number(item.smallNum)
- ) {
- list[index].bigCount = Number(item.bigNum);
- list[index].smallCount = Number(item.smallNum);
- }
- }
- if (element.bigCount == 0 && element.smallCount == 0) {
- list.splice(index, 1);
- }
- break;
- }
- }
- if (!has) {
- let info = { classId: item.classId, id: item.id };
- const classInfo =
- this.productInfo &&
- this.productInfo.find(element => {
- return element.classId == item.classId && element.child;
- });
- if (classInfo) {
- info = classInfo.child.find(ele => {
- return ele.id == item.id;
- });
- }
- let params = {
- ...info,
- bigCount: 1, //扎数
- smallCount: 0, //支数
- autoPrice: 0 //调价价格
- };
- list.push(params);
- }
- this.setSelectInfoByType({ type: this.pageType, info: list });
- // this.$forceUpdate();
- },
- //减去所有商品
- delAllEvent(item) {
- const list = this.selectList;
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- if (element.id == item.id ) {
- list.splice(index, 1);
- break;
- }
- }
- this.setSelectInfoByType({ type: this.pageType, info: list });
- },
- /**
- * 通过id获取当前选中的商品类别信息 左边菜单角标 当前选中该类别商品多少件
- */
- getSuitSelectClassById(classId) {
- let info = {
- bigCount: 0, //扎数
- smallCount: 0 //支数
- };
- const productItem =
- this.suitInfo &&
- this.suitInfo.find(ele => {
- return ele.classId == classId;
- });
- if (productItem && productItem.child) {
- for (const item of productItem.child) {
- for (const selectItem of this.selectList) {
- if (item.id == selectItem.id && item.classId == selectItem.classId) {
- info.bigCount += Number(selectItem.bigCount);
- info.smallCount += Number(selectItem.smallCount);
- info.itemPrice += selectItem.itemPrice?Number(selectItem.itemPrice):'';
- }
- }
- }
- }
- return info;
- },
- /**
- * 多风格-添加商品
- */
- addGoodsEvent(item) {
- const list = this.selectList;
- //过滤空数据
- if(item.bigCount==0||!item.bigCount){return}
- let has = false;
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- if (element.id == item.id && element.classId == item.classId) {
- has = true;
- list[index] = { ...element, bigCount: item.bigCount,goodsStyleSelectIndex: item.goodsStyleSelectIndex };
- break;
- }
- }
- if (!has) {
- let info = { classId: item.classId, id: item.id };
- const classInfo =
- this.suitInfo &&
- this.suitInfo.find(element => {
- return element.classId == item.classId && element.child;
- });
- if (classInfo) {
- info = classInfo.child.find(ele => {
- return ele.id == item.id;
- });
- }
- let params = {
- ...info,
- bigCount: item.bigCount, //扎数
- goodsStyleSelectIndex: item.goodsStyleSelectIndex, //选中的风格
- };
- list.push(params);
- }
- this.setSelectInfoByType({ type: this.pageType, info: list });
- // this.$forceUpdate();
- },
- /**
- * 多风格-添加商品
- */
- addGoods(item) {
- const list = this.selectList;
- let has = false;
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- if (element.id == item.id && element.classId == item.classId) {
- has = true;
- list[index] = { ...element, bigCount: ++element.bigCount };
- break;
- }
- }
- if (!has) {
- let info = { classId: item.classId, id: item.id };
- const classInfo =
- this.suitInfo &&
- this.suitInfo.find(element => {
- return element.classId == item.classId && element.child;
- });
- if (classInfo) {
- info = classInfo.child.find(ele => {
- return ele.id == item.id;
- });
- }
- let params = {
- ...info,
- bigCount: 1, //扎数
- smallCount: 0, //支数
- autoPrice: 0 //调价价格
- };
- list.push(params);
- }
- this.setSelectInfoByType({ type: this.pageType, info: list });
- // this.$forceUpdate();
- },
- /**
- * 多风格-减去商品
- */
- delGoodsEvent(item) {
- const list = this.selectList;
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- if (element.id == item.id && element.classId == item.classId) {
- if (element.bigCount > 0) {
- list[index] = { ...element, bigCount: --element.bigCount };
- }
- if (element.bigCount == 0 || !element.bigCount) {
- list.splice(index, 1);
- }
- break;
- }
- }
- this.setSelectInfoByType({ type: this.pageType, info: list });
- },
- /**
- * 更新商品
- */
- updateItemEvent(item) {
- const list = this.selectList;
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- if (element.id == item.id && element.classId == item.classId) {
- const { bigCount, smallCount } = list[index];
- //超过库存时 设置为库存最大值
- const ratio = Number(item.ratio);
- if (this.isLimit) {
- if (
- Number(bigCount) * ratio + Number(smallCount) >
- Number(item.bigNum) * ratio + Number(item.smallNum)
- ) {
- list[index].bigCount = Number(item.bigNum);
- list[index].smallCount = Number(item.smallNum);
- list[index].itemPrice = item.itemPrice?Number(item.itemPrice):'';
- }
- }
- list[index] = {
- ...element,
- bigCount: Number(item.bigCount),
- smallCount: Number(item.smallCount),
- itemPrice: item.itemPrice?Number(item.itemPrice):''
- };
- break;
- }
- }
- this.setSelectInfoByType({ type: this.pageType, info: list });
- // this.$forceUpdate();
- },
- /**
- * 切换左侧商品类别
- */
- swichClass(e) {
- let cur = e.currentTarget.dataset.current;
- if (this.classIndex == cur) {
- return false;
- } else {
- this.classIndex = cur;
- }
- },
- /**
- * 多风格-切换左侧商品类别
- */
- suitSwichClass(e) {
- let cur = e.currentTarget.dataset.current;
- if (this.suitClassIndex == cur) {
- return false;
- } else {
- this.suitClassIndex = cur;
- }
- },
- /**
- * 搜索对应花材
- */
- searchFn() {},
- /**
- * 请求课程数据
- */
- async initPxClassData() {
- try {
- const { data } = await getPxClassDataApi({pageSize:50});
- this.pxClassInfo = data.list;
- } catch (error) {
- } finally {
- uni.hideLoading();
- }
- },
- }
- };
|