Vue.component('band-label-link-goods', { template: `
搜索
搜索 清空已选
未选择商品
`, props: { 'show': { type: Boolean, default: false, }, 'operateType': { type: Number, default: 2, },//1商户 2门店 'labelId': { type: String, required: true, }, 'storeId': { type: String, default: '', }, 'requestUrl': { type: Object, default() { return {} } },//请求地址 }, data() { return { searchForm: { search_type: 2, search_text: '', firstClass: '', secondClass: '', thirdClass: '', page: 1, limit: 10, },//待选商品列表搜索选项 currentLen: 0,//待选商品总数量 loading: false, goodsList: [],//待选商品列表 chooseGoodsList: [],//已选商品列表 showChooseGoodsList: [],//展示的已选商品列表 select_keyword: '', chooseGoodsObj: {},//已选商品id对象 subLoading: false, } }, methods: { //关闭弹框 closeBand() { this.searchForm = { search_type: 2, search_text: '', firstClass: '', secondClass: '', thirdClass: '', page: 1, limit: 10, }; this.chooseGoodsList = []; this.showChooseGoodsList = []; this.chooseGoodsObj = {}; this.select_keyword = ''; this.$nextTick(() => { this.$emit('close'); }) }, //即将清空已选 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.$nextTick(() => { this.goodsList = goodsList; }) }).catch(() => { }) }, //确认选择商品 sureChooseGoods() { this.$confirm("确认把已选商品与该标签绑定?", "温馨提示", { confirmButtonText: '确认', cancelButtonText: '取消', }).then(() => { let ids = []; this.chooseGoodsList.forEach((item) => { ids.push(item.id); }); this.sendRequest(this.requestUrl.bandGoods,{ data: { ids: ids.join(','), label_id: this.labelId, operate_type: this.operateType, }, success: res => { if (res.status == 1) { this.$message.success(res.message); this.closeBand(); } else { this.$message.error(res.message); } } }); }).catch(() => { }) }, //清空已选搜索关键词 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) { if (name.indexOf(arr[i]) != -1) { len++; } } if (len == arr.length) { return true; } else { return false; } }, //切换每页数量 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; this.sendRequest('Retail/LabelInventory/getGoodsList',{ data: { label_id: this.labelId, operate_type: this.operateType, ...this.searchForm, }, success: (res) => { this.loading = false; if (res.status == 1) { this.goodsList = res.data.list; this.currentLen = res.data.count; } }, }) }, }, watch: { show(n, o) { //监听show的变化,重新渲染列表 if (n) { this.getData(); } } }, created() { // console.log('created'); // this.getData(); }, })