product.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. //加载Vue 主要用作规格
  2. require.config({
  3. paths: {
  4. "vue": "../addons/unishop/js/vue",
  5. //"vue": "https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue",
  6. }
  7. });
  8. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'upload', 'vue'], function ($, undefined, Backend, Table, Form, Upload, Vue) {
  9. // 规格模板
  10. //使用基础 Vue 构造器,创建一个“子类”。参数是一个包含组件选项的对象
  11. var specComponent = Vue.extend({
  12. template: '<div>' +
  13. ' <div style="display: none" class="form-group">\n' +
  14. ' <label class="control-label col-xs-12 col-sm-2">' + __('use_spec') + '</label>\n' +
  15. ' <div class="col-xs-12 col-sm-8">\n' +
  16. ' <input type="radio" name="row[use_spec]" value="0" v-model="use_spec"/>否\n' +
  17. ' <input type="radio" name="row[use_spec]" value="1" v-model="use_spec"/>是\n' +
  18. ' </div>\n' +
  19. ' </div>\n' +
  20. '\n' +
  21. ' <div class="form-group" v-if="use_spec == 1">\n' +
  22. ' <label class="control-label col-xs-12 col-sm-2">' + __('Spec') + '</label>\n' +
  23. ' <div class="col-xs-12 col-sm-8">\n' +
  24. ' <input id="c-specList" type="hidden" name="row[specList]" v-model="specListJSON"/>\n' +
  25. ' <input id="c-specTableList" type="hidden" name="row[specTableList]" v-model="specTableListJSON"/>\n' +
  26. '\n' +
  27. ' <table class="table table-hover">\n' +
  28. ' <thead>\n' +
  29. ' <tr>\n' +
  30. ' <th class="col-sm-4">\n' +
  31. ' <a href="javascript:;" class="btn btn-success btn-add" title="添加" data-toggle="modal"\n' +
  32. ' data-target="#addSpec"><i class="fa fa-plus"></i> 添加属性</a>\n' +
  33. ' <!-- 输入规格名称 -->\n' +
  34. ' <div class="modal fade" id="addSpec" tabindex="-1" role="dialog">\n' +
  35. ' <div class="modal-dialog modal-sm" role="document">\n' +
  36. ' <div class="modal-content">\n' +
  37. ' <div class="modal-body">\n' +
  38. ' <label>输入规格名称</label>\n' +
  39. ' <input type="text" class="form-control" v-model="specName">\n' +
  40. ' </div>\n' +
  41. ' <div class="modal-footer">\n' +
  42. ' <button type="button" class="btn btn-default" data-dismiss="modal">\n' +
  43. __('Close') +
  44. ' </button>\n' +
  45. ' <button type="button" class="btn btn-primary" data-dismiss="modal"\n' +
  46. ' v-on:click="addSpecList">' + __('Add') + '\n' +
  47. ' </button>\n' +
  48. ' </div>\n' +
  49. ' </div>\n' +
  50. ' </div>\n' +
  51. ' </div>\n' +
  52. ' <!-- 输入规格值 -->\n' +
  53. ' <div class="modal fade" id="addSpecValue" tabindex="-1" role="dialog">\n' +
  54. ' <div class="modal-dialog modal-sm" role="document">\n' +
  55. ' <div class="modal-content">\n' +
  56. ' <div class="modal-body">\n' +
  57. ' <label>输入规格 {{specFatherName}} 的值</label>\n' +
  58. ' <input type="text" class="form-control" v-model="specValue">\n' +
  59. ' </div>\n' +
  60. ' <div class="modal-footer">\n' +
  61. ' <button type="button" class="btn btn-default" data-dismiss="modal">\n' +
  62. __('Close') +
  63. ' </button>\n' +
  64. ' <button type="button" class="btn btn-primary" data-dismiss="modal"\n' +
  65. ' v-on:click="addSpecChildList">' + __('Add') + '\n' +
  66. ' </button>\n' +
  67. ' </div>\n' +
  68. ' </div>\n' +
  69. ' </div>\n' +
  70. ' </div>\n' +
  71. ' <!-- 批量设置值 -->\n' +
  72. ' <div class="modal fade" id="addMultiValue" tabindex="-1" role="dialog">\n' +
  73. ' <div class="modal-dialog modal-sm" role="document">\n' +
  74. ' <div class="modal-content">\n' +
  75. ' <div class="modal-body">\n' +
  76. ' <label>批量设置 {{multiName_}} 的值</label>\n' +
  77. ' <input type="number" class="form-control" v-model="multiValue">\n' +
  78. ' </div>\n' +
  79. ' <div class="modal-footer">\n' +
  80. ' <button type="button" class="btn btn-default" data-dismiss="modal">\n' +
  81. __('Close') +
  82. ' </button>\n' +
  83. ' <button type="button" class="btn btn-primary" data-dismiss="modal"\n' +
  84. ' v-on:click="addMultiValue">' + __('Multi setting') + '\n' +
  85. ' </button>\n' +
  86. ' </div>\n' +
  87. ' </div>\n' +
  88. ' </div>\n' +
  89. ' </div>\n' +
  90. ' </th>\n' +
  91. ' <th style=" height: 50px;\n' +
  92. ' display: block;\n' +
  93. ' line-height: 36px;\n' +
  94. ' text-align: center;">\n' +
  95. __('Specchildlist') + '\n' +
  96. ' </th>\n' +
  97. ' </tr>\n' +
  98. ' </thead>\n' +
  99. ' <tbody>\n' +
  100. ' <tr v-for="(item,index) in specList">\n' +
  101. ' <td>\n' +
  102. ' <div class="input-group">\n' +
  103. ' <input type="text" class="form-control" v-model="item.name">\n' +
  104. ' <span class="input-group-btn">\n' +
  105. ' <button class="btn btn-default" type="button" v-on:click="delSpec(index)">\n' +
  106. ' <span aria-hidden="true" class="tab-danger">&times;</span>\n' +
  107. ' </button>\n' +
  108. ' </span>\n' +
  109. ' </div>\n' +
  110. ' </td>\n' +
  111. ' <td>\n' +
  112. ' <div class="input-group" v-for="(childItem, childIndex) in item.child">\n' +
  113. ' <input type="text" class="form-control" disabled v-model="item.child[childIndex]">\n' +
  114. ' <span class="input-group-btn">\n' +
  115. ' <button class="btn btn-default" type="button"\n' +
  116. ' v-on:click="delSpecChild(index,childIndex)">\n' +
  117. ' <span aria-hidden="true" class="tab-danger">&times;</span>\n' +
  118. ' </button>\n' +
  119. ' </span>\n' +
  120. ' </div>\n' +
  121. ' <a href="javascript:;" v-on:click="herFatherSpec(index)" class="btn btn-success btn-add"\n' +
  122. ' title="添加" data-toggle="modal" data-target="#addSpecValue"><i class="fa fa-plus"></i>\n' +
  123. ' 添加属性值</a>\n' +
  124. ' </td>\n' +
  125. ' </tr>\n' +
  126. ' </tbody>\n' +
  127. ' </table>\n' +
  128. ' </div>\n' +
  129. ' </div>\n' +
  130. ' <div class="form-group" v-if="use_spec == 1">\n' +
  131. ' <div class="col-xs-12 col-sm-1"></div>\n' +
  132. ' <div class="col-xs-12 col-sm-10">\n' +
  133. ' <table class="table table-bordered table-hover">\n' +
  134. ' <thead>\n' +
  135. ' <tr>\n' +
  136. ' <th>图片</th>\n' +
  137. ' <th v-for="(item) in specList" v-if="item.child.length > 0">\n' +
  138. ' <span>\n' +
  139. ' {{item.name}}\n' +
  140. ' </span>\n' +
  141. ' </th>\n' +
  142. ' <th><i class="fa fa-plus btn-success" data-toggle="modal" data-target="#addMultiValue" v-on:click="addMultiType(\'code\')"></i>' + __('Code') + '</th>\n' +
  143. ' <th><i class="fa fa-plus btn-success" data-toggle="modal" data-target="#addMultiValue" v-on:click="addMultiType(\'market_price\')"></i>' + __('Market price') + '</th>\n' +
  144. ' <th><i class="fa fa-plus btn-success" data-toggle="modal" data-target="#addMultiValue" v-on:click="addMultiType(\'sales_price\')"></i>' + __('Sales price') + '</th>\n' +
  145. ' <th><i class="fa fa-plus btn-success" data-toggle="modal" data-target="#addMultiValue" v-on:click="addMultiType(\'stock\')"></i>' + __('Stock') + '</th>\n' +
  146. ' <th><i class="fa fa-plus btn-success" data-toggle="modal" data-target="#addMultiValue" v-on:click="addMultiType(\'sales\')"></i>' + __('Sales') + '</th>\n' +
  147. ' </tr>\n' +
  148. ' </thead>\n' +
  149. ' <tbody>\n' +
  150. ' <tr v-for="(tItem,index) in specTableList">\n' +
  151. ' <td>\n' +
  152. ' <input v-bind:id="\'c-logoSpec\' + index" type="hidden" v-model="tItem.image" >\n' +
  153. '\n' +
  154. ' <button type="button" v-bind:id="\'fachoose-logoSpec\'+index" class="btn btn-primary fachoose-spec"\n' +
  155. ' v-bind:data-input-id="\'c-logoSpec\'+index" v-bind:data-preview-id="\'p-logoSpec\'+index" data-mimetype="image/*"\n' +
  156. ' data-multiple="false">' + __('Choose') +
  157. ' </button>\n' +
  158. ' <button style="display: none;" type="button" v-bind:id="\'plupload-logoSpec\'+index"\n' +
  159. ' class="btn btn-danger plupload-spec" v-bind:data-input-id="\'c-logoSpec\'+index"\n' +
  160. ' data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp"\n' +
  161. ' data-multiple="false" v-bind:data-preview-id="\'p-logoSpec\'+index">\n' +
  162. __('Upload') +
  163. ' </button>\n' +
  164. ' <ul class="row list-inline plupload-preview width100" v-bind:id="\'p-logoSpec\'+index">\n' +
  165. '\n' +
  166. ' </ul>\n' +
  167. ' </td>\n' +
  168. ' <td v-for="vItem in tItem.value">\n' +
  169. ' <input class="form-control" disabled v-bind:value="vItem" />\n' +
  170. ' </td>\n' +
  171. ' <td><input class="form-control" data-rule="required" min="0" type="number" v-model="tItem.code"></td>\n' +
  172. ' <td><input class="form-control" data-rule="required" min="0" type="number" v-model="tItem.market_price"></td>\n' +
  173. ' <td><input class="form-control" data-rule="required" min="0" type="number" v-model="tItem.sales_price"></td>\n' +
  174. ' <td><input class="form-control" data-rule="required" min="0" type="number" v-model="tItem.stock"></td>\n' +
  175. ' <td><input class="form-control" data-rule="required" min="0" type="number" v-model="tItem.sales"></td>\n' +
  176. ' </tr>\n' +
  177. ' </tbody>\n' +
  178. ' </table>\n' +
  179. ' </div>\n' +
  180. ' </div>\n' +
  181. '\n' +
  182. ' <div class="form-group">\n' +
  183. ' <label class="control-label col-xs-12 col-sm-2">' + __('Lower market price') + ':</label>\n' +
  184. ' <div class="col-xs-12 col-sm-8">\n' +
  185. ' <input id="c-market_price" data-rule="required" v-bind:readonly="use_spec == 1" class="form-control"\n' +
  186. ' name="row[market_price]" min="0" type="number" v-model="noSpecValue.market_price">\n' +
  187. ' </div>\n' +
  188. ' </div>\n' +
  189. ' <div class="form-group">\n' +
  190. ' <label class="control-label col-xs-12 col-sm-2">' + __('Lower sales price') + ':</label>\n' +
  191. ' <div class="col-xs-12 col-sm-8">\n' +
  192. ' <input id="c-sales_price" data-rule="required" v-bind:readonly="use_spec == 1" class="form-control"\n' +
  193. ' name="row[sales_price]" min="0" type="number" v-model="noSpecValue.sales_price">\n' +
  194. ' </div>\n' +
  195. ' </div>\n' +
  196. '\n' +
  197. ' <div class="form-group">\n' +
  198. ' <label class="control-label col-xs-12 col-sm-2">' + __('Total stock') + ':</label>\n' +
  199. ' <div class="col-xs-12 col-sm-8">\n' +
  200. ' <input id="c-stock" data-rule="required" v-bind:readonly="use_spec == 1" class="form-control"\n' +
  201. ' name="row[stock]" min="0" type="number" v-model="noSpecValue.stock">\n' +
  202. ' </div>\n' +
  203. ' </div>\n' +
  204. ' <div class="form-group">\n' +
  205. ' <label class="control-label col-xs-12 col-sm-2">' + __('Total sales') + ':</label>\n' +
  206. ' <div class="col-xs-12 col-sm-8">\n' +
  207. ' <input id="c-sales" data-rule="required" v-bind:readonly="use_spec == 1" class="form-control"\n' +
  208. ' name="row[sales]" min="0" type="number" v-model="noSpecValue.sales">\n' +
  209. ' </div>\n' +
  210. ' </div>\n' +
  211. '</div>\n',
  212. data() {
  213. return {
  214. specList: _specList ? _specList : '',
  215. //[ //规格列表
  216. // {
  217. // name: '尺寸',
  218. // child: ['XS']
  219. // }, {
  220. // name: '大小',
  221. // child: ['大', '小']
  222. // },
  223. //],
  224. specTableList: _specTableList ? _specTableList : '',
  225. //[
  226. // {
  227. // value: ['XS', '大'],
  228. // code: 0,
  229. // market_price: 0.00,
  230. // sales_price: 0.00,
  231. // stock: 0,
  232. // sales: 0,
  233. // image: ''
  234. // },
  235. // {
  236. // value: ['XS', '小'],
  237. // code: 0,
  238. // market_price: 0.00,
  239. // sales_price: 0.00,
  240. // stock: 0,
  241. // sales: 0,
  242. // image: ''
  243. // }
  244. //],
  245. use_spec: _use_spec ? _use_spec : 0, //是否使用
  246. specName: '', //要添加的规格健名
  247. specValue: '', //要添加的规格值
  248. specChildValue: '', //要添加的规格值
  249. specFatherIndex: 0,
  250. market_price: _market_price ? _market_price : 0.00,
  251. sales_price: _sales_price ? _sales_price : 0.00,
  252. stock: _stock ? _stock : 0,
  253. sales: _sales ? _sales : 0,
  254. multiType:'code', // 当前批量添加的类型
  255. multiValue: '', // 当前批量编辑的值
  256. multi: [{ // 批量类型数组
  257. 'type': 'code',
  258. 'name': __('Code')
  259. }, {
  260. 'type': 'market_price',
  261. 'name': __('Market price')
  262. },{
  263. 'type': 'sales_price',
  264. 'name': __('Sales price')
  265. },{
  266. 'type': 'stock',
  267. 'name': __('Stock')
  268. },{
  269. 'type': 'sales',
  270. 'name': __('Sales')
  271. }]
  272. }
  273. },
  274. computed: {
  275. specListJSON() {
  276. return JSON.stringify(this.specList);
  277. },
  278. specTableListJSON() {
  279. let data = [];
  280. // 按字母排序
  281. for (let i in this.specTableList) {
  282. let newData = {};
  283. Object.keys(this.specTableList[i]).sort().map(key => {
  284. //newData[i][key]=this.specTableList[i].key;
  285. newData[key] = this.specTableList[i][key];
  286. });
  287. data[i] = newData;
  288. }
  289. return JSON.stringify(data);
  290. },
  291. specFatherName() {
  292. return this.specList[this.specFatherIndex] ? this.specList[this.specFatherIndex].name : '';
  293. },
  294. noSpecValue() {
  295. if (this.specTableList.length > 0) {
  296. let value = this.specTableList[0];
  297. let market_price = value.market_price;
  298. let sales_price = value.sales_price;
  299. let stock = 0;
  300. let sales = 0;
  301. for (let item of this.specTableList) {
  302. market_price = parseFloat(item.market_price) < parseFloat(market_price) ? parseFloat(item.market_price) : market_price;
  303. sales_price = parseFloat(item.sales_price) < parseFloat(sales_price) ? parseFloat(item.sales_price) : sales_price;
  304. stock = parseFloat(stock) + parseFloat(item.stock);
  305. sales = parseFloat(sales) + parseFloat(item.sales);
  306. }
  307. return {market_price, sales_price, stock, sales};
  308. } else {
  309. return {
  310. market_price: this.market_price,
  311. sales_price: this.sales_price,
  312. stock: this.stock,
  313. sales: this.sales,
  314. }
  315. }
  316. },
  317. multiName_() {
  318. let res = '';
  319. for (let i in this.multi) {
  320. if (this.multi[i].type == this.multiType) {
  321. res = this.multi[i].name;
  322. }
  323. }
  324. return res;
  325. }
  326. },
  327. watch: {
  328. multiValue(val) {
  329. switch (this.multiType) {
  330. case 'code':
  331. case 'stock':
  332. case 'sales':
  333. this.multiValue = parseInt(val);
  334. break;
  335. }
  336. }
  337. },
  338. methods: {
  339. //渲染到表格
  340. render() {
  341. let arr = this.specList;
  342. if (arr.length == 0) {
  343. //空
  344. return;
  345. }
  346. let specTableList = this.specTableList;
  347. this.specTableList = [];
  348. let td = []; //tbody
  349. var xNum = [];
  350. // 自创笛卡尔乘积算法 @author: mingwei zheng
  351. // 主逻辑
  352. for (var i = 0; i < arr.length; i++) {
  353. var valueLenght = arr[i]['child'].length; //是一个数组
  354. var cols = cols ? cols : valueLenght;//行数
  355. xNum[i] = 1;
  356. for (var ii = i + 1; ii < arr.length; ii++) {
  357. if (arr[ii]) {
  358. let length = arr[ii]['child'].length ? arr[ii]['child'].length : 1;
  359. xNum[i] *= length;
  360. }
  361. if (i == 0) {
  362. let length = arr[ii]['child'].length ? arr[ii]['child'].length : 1;
  363. cols *= length;
  364. }
  365. }
  366. //console.log(xNum[i]);//每个值填多少个行
  367. for (var ii = 0; ii < cols;) {
  368. if (arr[i]['child'].length == 0) {
  369. ii++;
  370. }
  371. for (var v = 0; v < arr[i]['child'].length; v++) {
  372. for (var x = 0; x < xNum[i]; x++) {
  373. td[ii] = td[ii] ? td[ii] : {
  374. value: [],
  375. code: 0,
  376. market_price: 0.00,
  377. sales_price: 0.00,
  378. stock: 0,
  379. sales: 0,
  380. image: ''
  381. };
  382. td[ii].value.push(arr[i]['child'][v]);
  383. //保留原来大值(针对添加的情况)
  384. for (let tItem of specTableList) {
  385. if (
  386. tItem.value.toString() + ',' + arr[i]['child'][v] == td[ii].value.toString()
  387. ||
  388. tItem.value.toString() == td[ii].value.toString()
  389. ) {
  390. let tempValue = td[ii].value;
  391. td[ii] = tItem;
  392. td[ii].value = tempValue;
  393. break;
  394. }
  395. }
  396. ii++;
  397. }
  398. }
  399. }
  400. }
  401. this.specTableList = td;
  402. },
  403. //添加规格名
  404. addSpecList() {
  405. if (this.specName == '') {
  406. Toastr.error('不能为空');
  407. return
  408. }
  409. for (let item of this.specList) {
  410. if (item.name == this.specName) {
  411. Toastr.error('不能重复');
  412. this.specName = '';
  413. return
  414. }
  415. }
  416. if (this.specList === '') {
  417. this.specList = [];
  418. }
  419. this.specList.push({
  420. name: this.specName,
  421. child: []
  422. });
  423. this.specName = '';
  424. },
  425. //添加规格值
  426. addSpecChildList() {
  427. if (this.specValue == '') {
  428. Toastr.error('不能为空');
  429. return
  430. }
  431. for (let item of this.specList) {
  432. if (item.name == this.specFatherName) {
  433. for (let Citem of item.child) {
  434. if (Citem == this.specValue) {
  435. Toastr.error('不能重复的');
  436. this.specValue = '';
  437. return
  438. }
  439. }
  440. item.child.push(this.specValue)
  441. }
  442. }
  443. this.specValue = '';
  444. //更新到表格
  445. this.render();
  446. },
  447. //设置父亲规格名
  448. herFatherSpec(index) {
  449. this.specFatherIndex = index;
  450. },
  451. //删除规格属性
  452. delSpec(index) {
  453. let confir = confirm('是否删除属性:' + this.specList[index].name + '。若有属性值则一带删除。');
  454. if (confir) {
  455. for (var i = this.specList[index].child.length - 1; i >= 0; i--) {
  456. this.delSpecChild(index, i, true);
  457. }
  458. this.specList.splice(index, 1);
  459. this.render();
  460. }
  461. },
  462. //删除规格属性值
  463. delSpecChild(index, childIndex, noconfir = false) {
  464. if (!noconfir)
  465. var confir = confirm('是否删除属性值:' + this.specList[index].child[childIndex] + '。');
  466. if (confir || noconfir) {
  467. var value = this.specList[index].child.splice(childIndex, 1)[0];
  468. //更新到表格
  469. //console.log(value)
  470. this.renderDel(index, value)
  471. }
  472. return;
  473. },
  474. //删除渲染
  475. renderDel(index, value) {
  476. const that = this;
  477. if (that.specList[index].child.length == 0) {
  478. //删除最后一个的情况。只删除specTableList.value的
  479. for (let item of this.specTableList) {
  480. for (let i in item.value) {
  481. if (item.value[i] == value) {
  482. item.value.splice(i, 1);
  483. break;
  484. }
  485. }
  486. }
  487. this.render();
  488. } else {
  489. //还有属性值的情况。直接删除跟value相关的specTableList
  490. for (var i = this.specTableList.length - 1; i >= 0; i--) {
  491. for (let item of that.specTableList[i].value) {
  492. if (item == value) {
  493. this.specTableList.splice(i, 1);
  494. break;
  495. }
  496. }
  497. }
  498. }
  499. return;
  500. },
  501. // 批量添加规格值
  502. addMultiType(type) {
  503. this.multiType = type;
  504. this.multiValue = '';
  505. },
  506. addMultiValue() {
  507. this.multiType; // 当前批量添加的类型
  508. this.multiValue; // 当前批量编辑的值
  509. if (this.multiValue === '') {
  510. Toastr.error('不能为空');
  511. return;
  512. }
  513. switch (this.multiType) {
  514. case 'market_price':
  515. case 'sales_price':
  516. this.multiValue = parseFloat(this.multiValue).toFixed(2);
  517. break;
  518. }
  519. for (let i in this.specTableList) {
  520. this.specTableList[i][this.multiType] = this.multiValue;
  521. }
  522. this.render();
  523. }
  524. }
  525. });
  526. var Controller = {
  527. index: function () {
  528. // 初始化表格参数配置
  529. Table.api.init({
  530. extend: {
  531. index_url: 'unishop/product/index' + location.search,
  532. add_url: 'unishop/product/add',
  533. edit_url: 'unishop/product/edit',
  534. del_url: 'unishop/product/del',
  535. multi_url: 'unishop/product/multi',
  536. evaluate_url: 'unishop/evaluate/index',
  537. copy_url: 'unishop/product/copy',
  538. table: 'unishop_product',
  539. dragsort_url: false,
  540. }
  541. });
  542. $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function () {
  543. return "请输入产品名称";
  544. };
  545. // 合并操作方法
  546. Table.api.events.operate = $.extend(Table.api.events.operate,
  547. {
  548. 'click .btn-evaluate': function (e, value, row, index) {
  549. e.stopPropagation();
  550. e.preventDefault();
  551. var table = $(this).closest('table');
  552. var options = table.bootstrapTable('getOptions');
  553. var ids = row[options.pk];
  554. row = $.extend({}, row ? row : {}, {ids: ids});
  555. var url = options.extend.evaluate_url + '?product_id=' + row.id;
  556. Fast.api.open(Table.api.replaceurl(url, row, table), __('Evaluate'), $(this).data() || {});
  557. }
  558. },
  559. {
  560. 'click .btn-copy': function (e, value, row, index) {
  561. e.stopPropagation();
  562. e.preventDefault();
  563. var table = $(this).closest('table');
  564. var options = table.bootstrapTable('getOptions');
  565. var ids = row[options.pk];
  566. row = $.extend({}, row ? row : {}, {ids: ids});
  567. var url = options.extend.copy_url;
  568. Fast.api.open(Table.api.replaceurl(url, row, table), __('Copy'), $(this).data() || {});
  569. }
  570. }
  571. );
  572. var table = $("#table");
  573. // 初始化表格
  574. table.bootstrapTable({
  575. url: $.fn.bootstrapTable.defaults.extend.index_url,
  576. pk: 'id',
  577. sortName: 'weigh',
  578. placeholder: '请输入产品名称',
  579. fixedColumns:true,
  580. fixedRightNumber:1,
  581. columns: [
  582. [
  583. {checkbox: true},
  584. {field: 'id', title: __('Id')},
  585. // {field: 'category_id', title: __('Category_id'), visible: false},
  586. /* {
  587. field: 'category', title: __('Category name'), formatter: function (value, row, index) {
  588. if (value) {
  589. if (value.parent) {
  590. return value.parent.name + ' / ' + value.name;
  591. } else {
  592. return value.name;
  593. }
  594. }
  595. return value;
  596. }
  597. },*/
  598. {field: 'title', title: __('Title')},
  599. {
  600. field: 'image',
  601. title: __('Image'),
  602. events: Table.api.events.image,
  603. formatter: Table.api.formatter.image
  604. },
  605. {field: 'activetime', title: __('Activetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  606. {
  607. field: 'images',
  608. title: __('Images'),
  609. events: Table.api.events.image,
  610. formatter: Table.api.formatter.images,
  611. visible: false
  612. },
  613. {field: 'sales_price', title: __('Lower price')},
  614. {field: 'stock', title: __('Stock')},
  615. // {field: 'look', title: __('Look')},
  616. // {field: 'sales', title: __('Sales')},
  617. {field: 'real_sales', title: __('Real sales')},
  618. // {field: 'no_buy_yet', title: __('No buy yet')},
  619. // {field: 'real_look', title: __('Real look')},
  620. // {field: 'delivery_id', title: __('Delivery_id'), visible: false},
  621. // {field: 'delivery.name', title: __('Delivery_id'), visible: false},
  622. /*{
  623. field: 'updatetime',
  624. title: __('Updatetime'),
  625. operate: 'RANGE',
  626. addclass: 'datetimerange',
  627. formatter: Table.api.formatter.datetime,
  628. visible: false
  629. },*/
  630. {
  631. field: 'createtime',
  632. title: __('Createtime'),
  633. operate: 'RANGE',
  634. addclass: 'datetimerange',
  635. formatter: Table.api.formatter.datetime
  636. },
  637. {field: 'weigh', title: __('Weigh')},
  638. {field: 'switch', title: __('Switch'), formatter: Table.api.formatter.toggle},
  639. {
  640. field: 'operate',
  641. title: __('Operate'),
  642. table: table,
  643. events: Table.api.events.operate,
  644. formatter: Table.api.formatter.operate,
  645. /*buttons:[
  646. {
  647. name: 'evaluate',
  648. text: __('Evaluate'),
  649. classname: 'btn btn-xs btn-info btn-evaluate',
  650. extend: 'data-toggle="tooltip"',
  651. icon: 'fa fa-commenting'
  652. },
  653. {
  654. name: 'copy',
  655. text: __('Copy'),
  656. classname: 'btn btn-xs btn-info btn-copy',
  657. extend: 'data-toggle="tooltip"',
  658. icon: 'fa fa-copy'
  659. }
  660. ]*/
  661. }
  662. ]
  663. ]
  664. });
  665. // 为表格绑定事件
  666. Table.api.bindevent(table);
  667. },
  668. recyclebin: function () {
  669. // 初始化表格参数配置
  670. Table.api.init({
  671. extend: {
  672. 'dragsort_url': ''
  673. }
  674. });
  675. var table = $("#table");
  676. // 初始化表格
  677. table.bootstrapTable({
  678. url: 'unishop/product/recyclebin' + location.search,
  679. pk: 'id',
  680. sortName: 'id',
  681. columns: [
  682. [
  683. {checkbox: true},
  684. {field: 'id', title: __('Id')},
  685. {field: 'title', title: __('Title'), align: 'left'},
  686. {
  687. field: 'deletetime',
  688. title: __('Deletetime'),
  689. operate: 'RANGE',
  690. addclass: 'datetimerange',
  691. formatter: Table.api.formatter.datetime
  692. },
  693. {
  694. field: 'operate',
  695. width: '130px',
  696. title: __('Operate'),
  697. table: table,
  698. events: Table.api.events.operate,
  699. buttons: [
  700. {
  701. name: 'Restore',
  702. text: __('Restore'),
  703. classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
  704. icon: 'fa fa-rotate-left',
  705. url: 'unishop/product/restore',
  706. refresh: true
  707. },
  708. /* {
  709. name: 'Destroy',
  710. text: __('Destroy'),
  711. classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
  712. icon: 'fa fa-times',
  713. url: 'unishop/product/destroy',
  714. refresh: true
  715. }*/
  716. ],
  717. formatter: Table.api.formatter.operate
  718. }
  719. ]
  720. ]
  721. });
  722. // 为表格绑定事件
  723. Table.api.bindevent(table);
  724. },
  725. add: function () {
  726. Controller.api.bindevent();
  727. var component = new specComponent().$mount();
  728. // 插入页面
  729. document.getElementById('spec').appendChild(component.$el);
  730. $('#spec').on('click', function () {
  731. // 选择图片
  732. let chooseImageButtons = $(".fachoose-spec", $("#spec"));
  733. if (chooseImageButtons) {
  734. //console.log(chooseImageButtons);
  735. for (let i = 0; i < chooseImageButtons.length; i++) {
  736. let events = $._data(chooseImageButtons[i], 'events');
  737. if (events && events['click']) {
  738. // 已绑定事件
  739. //console.log('获取图片不重新绑定事件');
  740. } else {
  741. $(chooseImageButtons[i]).on('click', function () {
  742. var that = this;
  743. var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
  744. var mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : '';
  745. parent.Fast.api.open("general/attachment/select?element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype , __('Choose'), {
  746. callback: function (data) {
  747. var button = $("#" + $(that).attr("id"));
  748. var preview_id = $(button).data("preview-id") ? $(button).data("preview-id") : "";
  749. var input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
  750. if (input_id) {
  751. $("#" + input_id).val(data.url).trigger("change");
  752. //TODO 出发一下input事件,让vue的v-model接收到数据
  753. document.getElementById(input_id).dispatchEvent(new Event('input'));
  754. // 插入图片
  755. if (preview_id && input_id) {
  756. var inputStr = data.url;
  757. var inputArr = inputStr.split(/\,/);
  758. $("#" + preview_id).empty();
  759. var tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  760. $.each(inputArr, function (i, j) {
  761. if (!j) {
  762. return true;
  763. }
  764. var data = {
  765. url: j,
  766. fullurl: Fast.api.cdnurl(j),
  767. data: $(that).data()
  768. };
  769. var html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  770. let preview = $("#" + preview_id);
  771. preview.append(html);
  772. $('.btn-trash', preview).on('click', function () {
  773. preview.empty();
  774. })
  775. });
  776. }
  777. }
  778. }
  779. });
  780. return false;
  781. });
  782. }
  783. }
  784. }
  785. });
  786. },
  787. edit: function () {
  788. Controller.api.bindevent();
  789. var component = new specComponent().$mount();
  790. // 插入页面
  791. document.getElementById('spec').appendChild(component.$el);
  792. $('#spec').on('click', function () {
  793. // 选择图片
  794. let chooseImageButtons = $(".fachoose-spec", $("#spec"));
  795. if (chooseImageButtons) {
  796. for (let i = 0; i < chooseImageButtons.length; i++) {
  797. let events = $._data(chooseImageButtons[i], 'events');
  798. let that = chooseImageButtons[i];
  799. let button = $("#" + $(that).attr("id"))
  800. let maxcount = $(button).data("maxcount");
  801. let preview_id = $(button).data("preview-id") ? $(button).data("preview-id") : "";
  802. let input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
  803. if (events && events['click']) {
  804. // 已绑定事件
  805. //console.log('获取图片不重新绑定事件');
  806. } else {
  807. $(chooseImageButtons[i]).on('click', function () {
  808. let multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
  809. let mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : '';
  810. parent.Fast.api.open("general/attachment/select?element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype , __('Choose'), {
  811. callback: function (data) {
  812. maxcount = typeof maxcount !== "undefined" ? maxcount : 0;
  813. if (input_id) {
  814. $("#" + input_id).val(data.url).trigger("change");
  815. //TODO 出发一下input事件,让vue的v-model接收到数据
  816. document.getElementById(input_id).dispatchEvent(new Event('input'));
  817. // 插入图片
  818. if (preview_id && input_id) {
  819. let inputStr = data.url;
  820. let inputArr = inputStr.split(/\,/);
  821. $("#" + preview_id).empty();
  822. let tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  823. $.each(inputArr, function (i, j) {
  824. if (!j) {
  825. return true;
  826. }
  827. let data = {
  828. url: j,
  829. fullurl: Fast.api.cdnurl(j),
  830. data: $(that).data()
  831. };
  832. let html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  833. let preview = $("#" + preview_id);
  834. preview.append(html);
  835. $('.btn-trash', preview).on('click', function(){
  836. preview.empty();
  837. $("#" + input_id).val('');
  838. //TODO 出发一下input事件,让vue的v-model接收到数据
  839. document.getElementById(input_id).dispatchEvent(new Event('input'));
  840. })
  841. });
  842. }
  843. }
  844. }
  845. });
  846. return false;
  847. });
  848. }
  849. // 首次加载的时候用到, 加载数据
  850. let inputStr = $('#' + input_id).val();
  851. if (inputStr && input_id) {
  852. let inputArr = inputStr.split(/\,/);
  853. $("#" + preview_id).empty();
  854. let tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  855. $.each(inputArr, function (i, j) {
  856. if (!j) {
  857. return true;
  858. }
  859. let data = {
  860. url: j,
  861. fullurl: Fast.api.cdnurl(j),
  862. data: $(that).data()
  863. };
  864. let html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  865. //console.log(preview_id);
  866. let preview = $("#" + preview_id);
  867. preview.append(html);
  868. $('.btn-trash', preview).on('click', function(){
  869. $("#" + preview_id).empty();
  870. $('#' + input_id).val('');
  871. //TODO 出发一下input事件,让vue的v-model接收到数据
  872. document.getElementById(input_id).dispatchEvent(new Event('input'));
  873. })
  874. });
  875. }
  876. }
  877. }
  878. });
  879. $('#spec').click();
  880. },
  881. copy:function(){
  882. Controller.api.bindevent();
  883. var component = new specComponent().$mount();
  884. // 插入页面
  885. document.getElementById('spec').appendChild(component.$el);
  886. $('#spec').on('click', function () {
  887. // 选择图片
  888. let chooseImageButtons = $(".fachoose-spec", $("#spec"));
  889. if (chooseImageButtons) {
  890. for (let i = 0; i < chooseImageButtons.length; i++) {
  891. let events = $._data(chooseImageButtons[i], 'events');
  892. let that = chooseImageButtons[i];
  893. let button = $("#" + $(that).attr("id"))
  894. let maxcount = $(button).data("maxcount");
  895. let preview_id = $(button).data("preview-id") ? $(button).data("preview-id") : "";
  896. let input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
  897. if (events && events['click']) {
  898. // 已绑定事件
  899. //console.log('获取图片不重新绑定事件');
  900. } else {
  901. $(chooseImageButtons[i]).on('click', function () {
  902. let multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
  903. let mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : '';
  904. parent.Fast.api.open("general/attachment/select?element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype , __('Choose'), {
  905. callback: function (data) {
  906. maxcount = typeof maxcount !== "undefined" ? maxcount : 0;
  907. if (input_id) {
  908. $("#" + input_id).val(data.url).trigger("change");
  909. //TODO 出发一下input事件,让vue的v-model接收到数据
  910. document.getElementById(input_id).dispatchEvent(new Event('input'));
  911. // 插入图片
  912. if (preview_id && input_id) {
  913. let inputStr = data.url;
  914. let inputArr = inputStr.split(/\,/);
  915. $("#" + preview_id).empty();
  916. let tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  917. $.each(inputArr, function (i, j) {
  918. if (!j) {
  919. return true;
  920. }
  921. let data = {
  922. url: j,
  923. fullurl: Fast.api.cdnurl(j),
  924. data: $(that).data()
  925. };
  926. let html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  927. let preview = $("#" + preview_id);
  928. preview.append(html);
  929. $('.btn-trash', preview).on('click', function(){
  930. preview.empty();
  931. $("#" + input_id).val('');
  932. //TODO 出发一下input事件,让vue的v-model接收到数据
  933. document.getElementById(input_id).dispatchEvent(new Event('input'));
  934. })
  935. });
  936. }
  937. }
  938. }
  939. });
  940. return false;
  941. });
  942. }
  943. // 首次加载的时候用到, 加载数据
  944. let inputStr = $('#' + input_id).val();
  945. if (inputStr && input_id) {
  946. let inputArr = inputStr.split(/\,/);
  947. $("#" + preview_id).empty();
  948. let tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  949. $.each(inputArr, function (i, j) {
  950. if (!j) {
  951. return true;
  952. }
  953. let data = {
  954. url: j,
  955. fullurl: Fast.api.cdnurl(j),
  956. data: $(that).data()
  957. };
  958. let html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  959. //console.log(preview_id);
  960. let preview = $("#" + preview_id);
  961. preview.append(html);
  962. $('.btn-trash', preview).on('click', function(){
  963. $("#" + preview_id).empty();
  964. $('#' + input_id).val('');
  965. //TODO 出发一下input事件,让vue的v-model接收到数据
  966. document.getElementById(input_id).dispatchEvent(new Event('input'));
  967. })
  968. });
  969. }
  970. }
  971. }
  972. });
  973. $('#spec').click();
  974. },
  975. api: {
  976. bindevent: function () {
  977. Form.api.bindevent($("form[role=form]"));
  978. }
  979. },
  980. select: function () {
  981. // 初始化表格参数配置
  982. Table.api.init({
  983. extend: {
  984. index_url: 'unishop/product/index',
  985. }
  986. });
  987. $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function () {
  988. return "请输入产品名称";
  989. };
  990. var table = $("#table");
  991. // 初始化表格
  992. table.bootstrapTable({
  993. url: $.fn.bootstrapTable.defaults.extend.index_url,
  994. sortName: 'id',
  995. placeholder: '请输入产品名称',
  996. columns: [
  997. [
  998. {field: 'state', checkbox: true,},
  999. {field: 'id', title: __('Id'), sortable: true},
  1000. {field: 'title', title: __('Title')},
  1001. {field: 'image', title: __('Image'), operate: false, formatter: Table.api.formatter.image},
  1002. {field: 'stock', title: __('Stock')},
  1003. {field: 'category.name', title: __('Category name')},
  1004. {
  1005. field: 'switch',
  1006. title: __('Switch'),
  1007. formatter: Table.api.formatter.status,
  1008. searchList: {"1": __('Yes'), "0": __('No')}
  1009. },
  1010. {
  1011. field: 'createtime',
  1012. title: __('Createtime'),
  1013. formatter: Table.api.formatter.datetime,
  1014. operate: 'RANGE',
  1015. addclass: 'datetimerange',
  1016. sortable: true
  1017. },
  1018. {
  1019. field: 'operate', title: __('Operate'), events: {
  1020. 'click .btn-chooseone': function (e, value, row, index) {
  1021. var dataArr = new Array();
  1022. dataArr.push(row);
  1023. Fast.api.close({data: dataArr, multiple: false});
  1024. },
  1025. }, formatter: function () {
  1026. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  1027. }
  1028. }
  1029. ]
  1030. ]
  1031. });
  1032. // 选中多个
  1033. $(document).on("click", ".btn-choose-multi", function () {
  1034. var dataArr = new Array();
  1035. $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
  1036. dataArr.push(j);
  1037. });
  1038. var multiple = Backend.api.query('multiple');
  1039. multiple = multiple == 'true' ? true : false;
  1040. Fast.api.close({data: dataArr, multiple: true});
  1041. });
  1042. // 为表格绑定事件
  1043. //Table.api.bindevent(table);
  1044. }
  1045. };
  1046. return Controller;
  1047. });