This repository has been archived on 2024-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
yoshop/web/assets/store/js/goods.spec.js

339 lines
14 KiB
JavaScript
Raw Permalink Normal View History

2020-04-25 22:20:29 +08:00
(function () {
// 配置信息
var setting = {
el: 'many-app',
baseData: null,
isSpecLocked: false,
batchData: {
goods_no: '',
goods_price: '',
sharing_price: '',
line_price: '',
stock_num: '',
goods_weight: ''
}
};
/**
* 构造方法
* @param options
* @param baseData
* @constructor
*/
function GoodsSpec(options, baseData) {
// 配置信息
setting = $.extend(true, {}, setting, options);
// 初始化
this.initialize();
}
GoodsSpec.prototype = {
// vue组件句柄
appVue: null,
/**
* 初始化
*/
initialize: function () {
// 已存在的规格数据
var spec_attr = [], spec_list = [];
if (typeof setting.baseData !== 'undefined' && setting.baseData !== null) {
spec_attr = setting.baseData['spec_attr'];
spec_list = setting.baseData['spec_list'];
}
// 实例化vue对象
this.appVue = new Vue({
el: setting.el,
data: {
2021-07-16 16:26:32 +08:00
// 规格组/值
2020-04-25 22:20:29 +08:00
spec_attr: spec_attr,
2021-07-16 16:26:32 +08:00
// sku列表
2020-04-25 22:20:29 +08:00
spec_list: spec_list,
// 显示添加规格组按钮
showAddGroupBtn: true,
// 显示添加规格组表单
showAddGroupForm: false,
// 新增规格组值
addGroupFrom: {
specName: '',
specValue: ''
},
// 当前规格属性是否锁定
isSpecLocked: setting.isSpecLocked,
// 批量设置sku属性
batchData: setting.batchData
},
methods: {
/**
* 显示/隐藏添加规则组
*/
onToggleAddGroupForm: function () {
this.showAddGroupBtn = !this.showAddGroupBtn;
this.showAddGroupForm = !this.showAddGroupForm;
},
/**
* 表单提交新增规格组
* @returns {boolean}
*/
onSubmitAddGroup: function () {
var _this = this;
if (_this.addGroupFrom.specName === '' || _this.addGroupFrom.specValue === '') {
layer.msg('请填写规则名或规则值');
return false;
}
// 添加到数据库
var load = layer.load();
$.post(STORE_URL + '/goods.spec/addSpec', {
spec_name: _this.addGroupFrom.specName,
spec_value: _this.addGroupFrom.specValue
}, function (result) {
layer.close(load);
if (result.code !== 1) {
layer.msg(result.msg);
return false;
}
// 记录规格数据
_this.spec_attr.push({
group_id: result.data['spec_id'],
group_name: _this.addGroupFrom.specName,
spec_items: [{
item_id: result.data['spec_value_id'],
spec_value: _this.addGroupFrom.specValue
}],
tempValue: ''
});
// 清空输入内容
_this.addGroupFrom.specName = '';
_this.addGroupFrom.specValue = '';
// 隐藏添加规则组
_this.onToggleAddGroupForm();
// 构建规格组合列表
_this.buildSkulist();
});
},
/**
* 新增规格值
* @param index
*/
onSubmitAddValue: function (index) {
var _this = this
, specAttr = _this.spec_attr[index];
2021-07-16 16:26:32 +08:00
// 验证新增规格值
if (!this.verifyAddValue(specAttr)) {
2020-04-25 22:20:29 +08:00
return false;
}
// 添加到数据库
var load = layer.load();
$.post(STORE_URL + '/goods.spec/addSpecValue', {
spec_id: specAttr.group_id,
spec_value: specAttr.tempValue
}, function (result) {
layer.close(load);
if (result.code !== 1) {
layer.msg(result.msg);
return false;
}
// 记录规格数据
specAttr.spec_items.push({
item_id: result.data['spec_value_id'],
spec_value: specAttr.tempValue
});
// 清空输入内容
specAttr.tempValue = '';
// 构建规格组合列表
_this.buildSkulist();
});
},
2021-07-16 16:26:32 +08:00
/**
* 验证新增规格值
* @param specAttr
* @returns {boolean}
*/
verifyAddValue(specAttr) {
if (!specAttr.tempValue || specAttr.tempValue === '') {
layer.msg('规格值不能为空');
return false
}
// 验证规格值是否重复
var specValues = []
specAttr.spec_items.forEach(function (item) {
specValues.push(item.spec_value)
})
if (specValues.indexOf(specAttr.tempValue) > -1) {
layer.msg('规格值不能重复');
return false
}
return true
},
2020-04-25 22:20:29 +08:00
/**
* 构建规格组合列表
*/
buildSkulist: function () {
var _this = this;
2021-07-16 16:26:32 +08:00
// 计算skuList总数 (table行数)
var skuDataCount = 1;
2020-04-25 22:20:29 +08:00
for (var i = 0; i < _this.spec_attr.length; i++) {
2021-07-16 16:26:32 +08:00
skuDataCount *= _this.spec_attr[i].spec_items.length;
2020-04-25 22:20:29 +08:00
}
2021-07-16 16:26:32 +08:00
// 遍历skuList列表生成tr(行)
var skuList = [];
for (i = 0; i < skuDataCount; i++) {
// skuItem的数据
var skuItemRows = [],
rowCount = 1, specSkuIdAttr = [];
// 遍历规格属性, 生成td row(列)
2020-04-25 22:20:29 +08:00
for (var j = 0; j < _this.spec_attr.length; j++) {
2021-07-16 16:26:32 +08:00
// 每组规格的值
var specValues = _this.spec_attr[j].spec_items;
// 合并后的规格值显示的区块数量
rowCount = rowCount * specValues.length;
// 合并后的规格值的rowspan
var anInterBankNum = skuDataCount / rowCount;
var point = (i / anInterBankNum) % specValues.length;
// rowspan能被i整除 才写入skuItemRows
// i % anInterBankNum
2020-04-25 22:20:29 +08:00
if (0 === (i % anInterBankNum)) {
2021-07-16 16:26:32 +08:00
skuItemRows.push({
2020-04-25 22:20:29 +08:00
rowspan: anInterBankNum,
2021-07-16 16:26:32 +08:00
item_id: specValues[point].item_id,
spec_value: specValues[point].spec_value
2020-04-25 22:20:29 +08:00
});
}
2021-07-16 16:26:32 +08:00
const pointInt = parseInt(point.toString());
specSkuIdAttr.push(specValues[pointInt].item_id);
2020-04-25 22:20:29 +08:00
}
2021-07-16 16:26:32 +08:00
skuList.push({
2020-04-25 22:20:29 +08:00
spec_sku_id: specSkuIdAttr.join('_'),
2021-07-16 16:26:32 +08:00
rows: skuItemRows,
2020-04-25 22:20:29 +08:00
form: {}
});
2021-07-16 16:26:32 +08:00
2020-04-25 22:20:29 +08:00
}
2021-07-16 16:26:32 +08:00
2020-04-25 22:20:29 +08:00
// return false;
// 合并旧sku数据
2021-07-16 16:26:32 +08:00
if (_this.spec_list.length > 0 && skuList.length > 0) {
for (i = 0; i < skuList.length; i++) {
2020-04-25 22:20:29 +08:00
var overlap = _this.spec_list.filter(function (val) {
2021-07-16 16:26:32 +08:00
return val.spec_sku_id === skuList[i].spec_sku_id;
2020-04-25 22:20:29 +08:00
});
2021-07-16 16:26:32 +08:00
if (overlap.length > 0) skuList[i].form = overlap[0].form;
2020-04-25 22:20:29 +08:00
}
}
2021-07-16 16:26:32 +08:00
_this.spec_list = skuList;
2020-04-25 22:20:29 +08:00
// 注册上传sku图片事件
_this.onSelectImagesEvent();
},
/**
* 删除规则组事件
* @param index
*/
onDeleteGroup: function (index) {
var _this = this;
layer.confirm('确定要删除该规则吗?确认后不可恢复请谨慎操作'
, function (layerIndex) {
// 删除指定规则组
_this.spec_attr.splice(index, 1);
// 构建规格组合列表
_this.buildSkulist();
layer.close(layerIndex);
});
},
/**
* 删除规则值事件
* @param index
* @param itemIndex
*/
onDeleteValue: function (index, itemIndex) {
var _this = this;
layer.confirm('确定要删除该规则吗?确认后不可恢复请谨慎操作'
, function (layerIndex) {
// 删除指定规则组
_this.spec_attr[index].spec_items.splice(itemIndex, 1);
// 构建规格组合列表
_this.buildSkulist();
layer.close(layerIndex);
});
},
/**
* 批量设置sku属性
*/
onSubmitBatchData: function () {
var _this = this;
_this.spec_list.forEach(function (value) {
2021-07-16 16:26:32 +08:00
for (var key in _this.batchData) {
2020-04-25 22:20:29 +08:00
if (_this.batchData.hasOwnProperty(key) && _this.batchData[key]) {
_this.$set(value.form, key, _this.batchData[key]);
}
}
});
},
/**
* 注册上传sku图片事件
*/
onSelectImagesEvent: function () {
var _this = this;
// 注册上传sku图片
_this.$nextTick(function () {
$(_this.$el).find('.j-selectImg').selectImages({
done: function (data, $addon) {
var index = $addon.data('index');
_this.$set(_this.spec_list[index].form, 'image_id', data[0]['file_id']);
_this.$set(_this.spec_list[index].form, 'image_path', data[0]['file_path']);
}
});
});
},
/**
* 删除sku图片
*/
onDeleteSkuImage: function (index) {
this.spec_list[index].form['image_id'] = 0;
this.spec_list[index].form['image_path'] = '';
},
/**
* 获取当前data
*/
getData: function () {
return this.$data;
},
/**
* sku列表是否为空
* @returns {boolean}
*/
isEmptySkuList: function () {
return !this.spec_list.length;
}
}
});
// 初始化生成sku列表
spec_list.length > 0 && this.appVue.buildSkulist();
}
};
window.GoodsSpec = GoodsSpec;
})();