Vue.component('template-choose-store-goods', {
template: `
`,
props: {
'max_choose_num': {
type: Number,
default: 1,
},//最大可选商品数量
'show': {
type: Boolean,
default: false,
},//为了重新渲染数据
'store_id': {
type: Number,
default: 0,
},//门店ID,可不传
'online_state': {
type: String,
default: '',
},//商城出售状态 0非商城出售 1商城出售
'status': {
type: String,
default: '',
},//商品上下架状态 0上架 1下架
'input_field': {
type: Array,
default: () => {
return []
},
},//其他显示字段
'distribution': {
type: Boolean,
default: false,
}
},
data: function () {
return {
searchForm: {
keyword: '',
page: 1,
limit: 10,
},//待选商品列表搜索选项
currentLen: 0,//待选商品总数量
loading: false,
goodsList: [],//待选商品列表
chooseGoodsList: [],//已选商品列表
showChooseGoodsList: [],//展示的已选商品列表
select_keyword: '',
chooseGoodsObj: {},//已选商品id对象
}
},
methods: {
//即将清空已选
toClearChoose() {
this.$confirm("确认清空已选商品列表?", "关闭提示", {
confirmButtonText: '清空',
cancelButtonText: '取消',
type: 'info'
}).then(() => {
this.chooseGoodsList = [];
this.showChooseGoodsList = [];
this.chooseGoodsObj = {};
var goodsList = JSON.parse(JSON.stringify(this.goodsList));
this.goodsList = [];
this.goodsList = goodsList;
}).catch(() => {
})
},
//确认选择商品
sureChooseGoods() {
let chooseGoodsList = JSON.parse(JSON.stringify(this.chooseGoodsList));
let reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d{0,2})$/;
if (this.input_field.length) {
for (let i = 0; i < chooseGoodsList.length; i++) {
for (let j = 0; j < this.input_field.length; j++) {
let a = chooseGoodsList[i][this.input_field[j].key];
switch (this.input_field[j].type) {
case 'rate':
if (((this.input_field[j].required || a != ''
&& a != undefined)
&& !reg.test(a))
|| (a && a * 1 < 0.01 || a * 1 > 99)) {
return this.$message.error(`${this.input_field[j].title}设置有误`);
}
break;
}
}
}
}
this.chooseGoodsList = [];
this.showChooseGoodsList = [];
this.chooseGoodsObj = {};
this.select_keyword = '';
this.currentLen = 0;
this.$nextTick(function () {
this.$emit('select-goods', chooseGoodsList);
}.bind(this))
},
//清空已选搜索关键词
goodsNull(e) {
if (!e) {
this.showChooseGoodsList = this.chooseGoodsList
}
},
//判断商品是否可选
checkGoods(goods_id) {
return !this.chooseGoodsObj[goods_id];
},
//点击了选择商品
chooseGoods(item) {
item = JSON.parse(JSON.stringify(item));
this.chooseGoodsObj[item.id] = true;
this.chooseGoodsList.unshift(item);
if (this.select_keyword) {
if (this.fuzzySearch(item.goods_name, this.select_keyword)) {
this.showChooseGoodsList.unshift(item);
}
} else {
this.showChooseGoodsList.unshift(item);
}
var goodsList = JSON.parse(JSON.stringify(this.goodsList));
this.goodsList = [];
this.goodsList = goodsList;
},
//删除已选商品
deleteSelected(index) {
let goods_id = this.showChooseGoodsList[index].id;
delete this.chooseGoodsObj[goods_id];
this.showChooseGoodsList.splice(index, 1);
for (var i in this.chooseGoodsList) {
if (this.chooseGoodsList[i].id == goods_id) {
this.chooseGoodsList.splice(i, 1);
break;
}
}
delete goods_id;
var goodsList = JSON.parse(JSON.stringify(this.goodsList));
this.goodsList = [];
this.goodsList = goodsList;
},
//已选列表搜索
searchChooseGoods() {
this.showChooseGoodsList = [];
if (this.select_keyword == '') {
this.showChooseGoodsList = this.chooseGoodsList;
} else {
for (let i = 0; i < this.chooseGoodsList.length; i++) {
if (this.fuzzySearch(this.chooseGoodsList[i].goods_name, this.select_keyword)) {
this.showChooseGoodsList.push(this.chooseGoodsList[i]);
}
}
}
},
//模糊搜索商品名称
fuzzySearch(name, keyword) {
if (keyword === '' || keyword === undefined) {
return true;
}
let len = 0;
let arr = String(keyword).split('');
for (let i in arr) {
console.log(arr[i]);
if (name.indexOf(arr[i]) != -1) {
len++;
}
}
if (len == arr.length) {
return true;
} else {
return false;
}
},
//准备搜索
searchGoods() {
this.searchForm.page = 1;
this.getCount();
this.getData();
},
//切换每页数量
handleSizeChange(val) {
this.searchForm.limit = val;
this.searchForm.page = 1;
this.getData();
},
//切换页数
handleCurrentChange(val) {
this.searchForm.page = val;
this.getData();
},
//获取商品列表
getData() {
this.goodsList = [];
this.loading = true;
$.ajax({
url: "/index.php/Retail/Component/getStoreGoodsList",
type: 'post',
data: {
store_id: this.store_id,
online_state: this.online_state,
status: this.status,
distribution: this.distribution ? 1 : 0,
...this.searchForm,
},
success: (res) => {
this.loading = false;
if (res.status == 1) {
this.goodsList = res.data;
}
},
error: (data) => {
console.log(data);
this.loading = false;
}
})
},
//获取商品总数量
getCount() {
$.ajax({
url: "/index.php/Retail/Component/getStoreGoodsCount",
type: 'post',
data: {
store_id: this.store_id,
online_state: this.online_state,
status: this.status,
...this.searchForm,
},
success: (res) => {
if (res.status == 1) {
this.currentLen = res.count;
}
},
error: (data) => {
console.log(data);
}
})
},
},
watch: {
show(n, o) {
//监听show的变化,重新渲染列表
if (n) {
this.getCount();
this.getData();
}
}
},
created () {
// console.log('created');
this.getCount();
this.getData();
},
});