goods.spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. (function () {
  2. // 配置信息
  3. var setting = {
  4. el: 'many-app',
  5. baseData: null,
  6. isSpecLocked: false,
  7. batchData: {
  8. goods_no: '',
  9. goods_price: '',
  10. sharing_price: '',
  11. line_price: '',
  12. stock_num: '',
  13. goods_weight: ''
  14. }
  15. };
  16. /**
  17. * 构造方法
  18. * @param options
  19. * @param baseData
  20. * @constructor
  21. */
  22. function GoodsSpec(options, baseData) {
  23. // 配置信息
  24. setting = $.extend(true, {}, setting, options);
  25. // 初始化
  26. this.initialize();
  27. }
  28. GoodsSpec.prototype = {
  29. // vue组件句柄
  30. appVue: null,
  31. /**
  32. * 初始化
  33. */
  34. initialize: function () {
  35. // 已存在的规格数据
  36. var spec_attr = [], spec_list = [];
  37. if (typeof setting.baseData !== 'undefined' && setting.baseData !== null) {
  38. spec_attr = setting.baseData['spec_attr'];
  39. spec_list = setting.baseData['spec_list'];
  40. }
  41. // 实例化vue对象
  42. this.appVue = new Vue({
  43. el: setting.el,
  44. data: {
  45. // 规格组/值
  46. spec_attr: spec_attr,
  47. // sku列表
  48. spec_list: spec_list,
  49. // 显示添加规格组按钮
  50. showAddGroupBtn: true,
  51. // 显示添加规格组表单
  52. showAddGroupForm: false,
  53. // 新增规格组值
  54. addGroupFrom: {
  55. specName: '',
  56. specValue: ''
  57. },
  58. // 当前规格属性是否锁定
  59. isSpecLocked: setting.isSpecLocked,
  60. // 批量设置sku属性
  61. batchData: setting.batchData
  62. },
  63. methods: {
  64. /**
  65. * 显示/隐藏添加规则组
  66. */
  67. onToggleAddGroupForm: function () {
  68. this.showAddGroupBtn = !this.showAddGroupBtn;
  69. this.showAddGroupForm = !this.showAddGroupForm;
  70. },
  71. /**
  72. * 表单提交:新增规格组
  73. * @returns {boolean}
  74. */
  75. onSubmitAddGroup: function () {
  76. var _this = this;
  77. if (_this.addGroupFrom.specName === '' || _this.addGroupFrom.specValue === '') {
  78. layer.msg('请填写规则名或规则值');
  79. return false;
  80. }
  81. // 添加到数据库
  82. var load = layer.load();
  83. $.post(STORE_URL + '/goods.spec/addSpec', {
  84. spec_name: _this.addGroupFrom.specName,
  85. spec_value: _this.addGroupFrom.specValue
  86. }, function (result) {
  87. layer.close(load);
  88. if (result.code !== 1) {
  89. layer.msg(result.msg);
  90. return false;
  91. }
  92. // 记录规格数据
  93. _this.spec_attr.push({
  94. group_id: result.data['spec_id'],
  95. group_name: _this.addGroupFrom.specName,
  96. spec_items: [{
  97. item_id: result.data['spec_value_id'],
  98. spec_value: _this.addGroupFrom.specValue
  99. }],
  100. tempValue: ''
  101. });
  102. // 清空输入内容
  103. _this.addGroupFrom.specName = '';
  104. _this.addGroupFrom.specValue = '';
  105. // 隐藏添加规则组
  106. _this.onToggleAddGroupForm();
  107. // 构建规格组合列表
  108. _this.buildSkulist();
  109. });
  110. },
  111. /**
  112. * 新增规格值
  113. * @param index
  114. */
  115. onSubmitAddValue: function (index) {
  116. var _this = this
  117. , specAttr = _this.spec_attr[index];
  118. // 验证新增规格值
  119. if (!this.verifyAddValue(specAttr)) {
  120. return false;
  121. }
  122. // 添加到数据库
  123. var load = layer.load();
  124. $.post(STORE_URL + '/goods.spec/addSpecValue', {
  125. spec_id: specAttr.group_id,
  126. spec_value: specAttr.tempValue
  127. }, function (result) {
  128. layer.close(load);
  129. if (result.code !== 1) {
  130. layer.msg(result.msg);
  131. return false;
  132. }
  133. // 记录规格数据
  134. specAttr.spec_items.push({
  135. item_id: result.data['spec_value_id'],
  136. spec_value: specAttr.tempValue
  137. });
  138. // 清空输入内容
  139. specAttr.tempValue = '';
  140. // 构建规格组合列表
  141. _this.buildSkulist();
  142. });
  143. },
  144. /**
  145. * 验证新增规格值
  146. * @param specAttr
  147. * @returns {boolean}
  148. */
  149. verifyAddValue(specAttr) {
  150. if (!specAttr.tempValue || specAttr.tempValue === '') {
  151. layer.msg('规格值不能为空');
  152. return false
  153. }
  154. // 验证规格值是否重复
  155. var specValues = []
  156. specAttr.spec_items.forEach(function (item) {
  157. specValues.push(item.spec_value)
  158. })
  159. if (specValues.indexOf(specAttr.tempValue) > -1) {
  160. layer.msg('规格值不能重复');
  161. return false
  162. }
  163. return true
  164. },
  165. /**
  166. * 构建规格组合列表
  167. */
  168. buildSkulist: function () {
  169. var _this = this;
  170. // 计算skuList总数 (table行数)
  171. var skuDataCount = 1;
  172. for (var i = 0; i < _this.spec_attr.length; i++) {
  173. skuDataCount *= _this.spec_attr[i].spec_items.length;
  174. }
  175. // 遍历skuList列表,生成tr(行)
  176. var skuList = [];
  177. for (i = 0; i < skuDataCount; i++) {
  178. // skuItem的数据
  179. var skuItemRows = [],
  180. rowCount = 1, specSkuIdAttr = [];
  181. // 遍历规格属性, 生成td row(列)
  182. for (var j = 0; j < _this.spec_attr.length; j++) {
  183. // 每组规格的值
  184. var specValues = _this.spec_attr[j].spec_items;
  185. // 合并后的规格值显示的区块数量
  186. rowCount = rowCount * specValues.length;
  187. // 合并后的规格值的rowspan
  188. var anInterBankNum = skuDataCount / rowCount;
  189. var point = (i / anInterBankNum) % specValues.length;
  190. // rowspan能被i整除 才写入skuItemRows
  191. // i % anInterBankNum
  192. if (0 === (i % anInterBankNum)) {
  193. skuItemRows.push({
  194. rowspan: anInterBankNum,
  195. item_id: specValues[point].item_id,
  196. spec_value: specValues[point].spec_value
  197. });
  198. }
  199. const pointInt = parseInt(point.toString());
  200. specSkuIdAttr.push(specValues[pointInt].item_id);
  201. }
  202. skuList.push({
  203. spec_sku_id: specSkuIdAttr.join('_'),
  204. rows: skuItemRows,
  205. form: {}
  206. });
  207. }
  208. // return false;
  209. // 合并旧sku数据
  210. if (_this.spec_list.length > 0 && skuList.length > 0) {
  211. for (i = 0; i < skuList.length; i++) {
  212. var overlap = _this.spec_list.filter(function (val) {
  213. return val.spec_sku_id === skuList[i].spec_sku_id;
  214. });
  215. if (overlap.length > 0) skuList[i].form = overlap[0].form;
  216. }
  217. }
  218. _this.spec_list = skuList;
  219. // 注册上传sku图片事件
  220. _this.onSelectImagesEvent();
  221. },
  222. /**
  223. * 删除规则组事件
  224. * @param index
  225. */
  226. onDeleteGroup: function (index) {
  227. var _this = this;
  228. layer.confirm('确定要删除该规则吗?确认后不可恢复请谨慎操作'
  229. , function (layerIndex) {
  230. // 删除指定规则组
  231. _this.spec_attr.splice(index, 1);
  232. // 构建规格组合列表
  233. _this.buildSkulist();
  234. layer.close(layerIndex);
  235. });
  236. },
  237. /**
  238. * 删除规则值事件
  239. * @param index
  240. * @param itemIndex
  241. */
  242. onDeleteValue: function (index, itemIndex) {
  243. var _this = this;
  244. layer.confirm('确定要删除该规则吗?确认后不可恢复请谨慎操作'
  245. , function (layerIndex) {
  246. // 删除指定规则组
  247. _this.spec_attr[index].spec_items.splice(itemIndex, 1);
  248. // 构建规格组合列表
  249. _this.buildSkulist();
  250. layer.close(layerIndex);
  251. });
  252. },
  253. /**
  254. * 批量设置sku属性
  255. */
  256. onSubmitBatchData: function () {
  257. var _this = this;
  258. _this.spec_list.forEach(function (value) {
  259. for (var key in _this.batchData) {
  260. if (_this.batchData.hasOwnProperty(key) && _this.batchData[key]) {
  261. _this.$set(value.form, key, _this.batchData[key]);
  262. }
  263. }
  264. });
  265. },
  266. /**
  267. * 注册上传sku图片事件
  268. */
  269. onSelectImagesEvent: function () {
  270. var _this = this;
  271. // 注册上传sku图片
  272. _this.$nextTick(function () {
  273. $(_this.$el).find('.j-selectImg').selectImages({
  274. done: function (data, $addon) {
  275. var index = $addon.data('index');
  276. _this.$set(_this.spec_list[index].form, 'image_id', data[0]['file_id']);
  277. _this.$set(_this.spec_list[index].form, 'image_path', data[0]['file_path']);
  278. }
  279. });
  280. });
  281. },
  282. /**
  283. * 删除sku图片
  284. */
  285. onDeleteSkuImage: function (index) {
  286. this.spec_list[index].form['image_id'] = 0;
  287. this.spec_list[index].form['image_path'] = '';
  288. },
  289. /**
  290. * 获取当前data
  291. */
  292. getData: function () {
  293. return this.$data;
  294. },
  295. /**
  296. * sku列表是否为空
  297. * @returns {boolean}
  298. */
  299. isEmptySkuList: function () {
  300. return !this.spec_list.length;
  301. }
  302. }
  303. });
  304. // 初始化生成sku列表
  305. spec_list.length > 0 && this.appVue.buildSkulist();
  306. }
  307. };
  308. window.GoodsSpec = GoodsSpec;
  309. })();