product.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. }
  540. });
  541. $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function () {
  542. return "请输入产品名称";
  543. };
  544. // 合并操作方法
  545. Table.api.events.operate = $.extend(Table.api.events.operate,
  546. {
  547. 'click .btn-evaluate': function (e, value, row, index) {
  548. e.stopPropagation();
  549. e.preventDefault();
  550. var table = $(this).closest('table');
  551. var options = table.bootstrapTable('getOptions');
  552. var ids = row[options.pk];
  553. row = $.extend({}, row ? row : {}, {ids: ids});
  554. var url = options.extend.evaluate_url + '?product_id=' + row.id;
  555. Fast.api.open(Table.api.replaceurl(url, row, table), __('Evaluate'), $(this).data() || {});
  556. }
  557. },
  558. {
  559. 'click .btn-copy': function (e, value, row, index) {
  560. e.stopPropagation();
  561. e.preventDefault();
  562. var table = $(this).closest('table');
  563. var options = table.bootstrapTable('getOptions');
  564. var ids = row[options.pk];
  565. row = $.extend({}, row ? row : {}, {ids: ids});
  566. var url = options.extend.copy_url;
  567. Fast.api.open(Table.api.replaceurl(url, row, table), __('Copy'), $(this).data() || {});
  568. }
  569. }
  570. );
  571. var table = $("#table");
  572. // 初始化表格
  573. table.bootstrapTable({
  574. url: $.fn.bootstrapTable.defaults.extend.index_url,
  575. pk: 'id',
  576. sortName: 'weigh',
  577. placeholder: '请输入产品名称',
  578. fixedColumns:true,
  579. fixedRightNumber:1,
  580. columns: [
  581. [
  582. {checkbox: true},
  583. {field: 'id', title: __('Id')},
  584. // {field: 'category_id', title: __('Category_id'), visible: false},
  585. /* {
  586. field: 'category', title: __('Category name'), formatter: function (value, row, index) {
  587. if (value) {
  588. if (value.parent) {
  589. return value.parent.name + ' / ' + value.name;
  590. } else {
  591. return value.name;
  592. }
  593. }
  594. return value;
  595. }
  596. },*/
  597. {field: 'title', title: __('Title')},
  598. {
  599. field: 'image',
  600. title: __('Image'),
  601. events: Table.api.events.image,
  602. formatter: Table.api.formatter.image
  603. },
  604. {field: 'activetime', title: __('Activetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  605. {
  606. field: 'images',
  607. title: __('Images'),
  608. events: Table.api.events.image,
  609. formatter: Table.api.formatter.images,
  610. visible: false
  611. },
  612. {field: 'sales_price', title: __('Lower price')},
  613. {field: 'stock', title: __('Stock')},
  614. // {field: 'look', title: __('Look')},
  615. // {field: 'sales', title: __('Sales')},
  616. {field: 'real_sales', title: __('Real sales')},
  617. // {field: 'no_buy_yet', title: __('No buy yet')},
  618. // {field: 'real_look', title: __('Real look')},
  619. // {field: 'delivery_id', title: __('Delivery_id'), visible: false},
  620. // {field: 'delivery.name', title: __('Delivery_id'), visible: false},
  621. /*{
  622. field: 'updatetime',
  623. title: __('Updatetime'),
  624. operate: 'RANGE',
  625. addclass: 'datetimerange',
  626. formatter: Table.api.formatter.datetime,
  627. visible: false
  628. },*/
  629. {
  630. field: 'createtime',
  631. title: __('Createtime'),
  632. operate: 'RANGE',
  633. addclass: 'datetimerange',
  634. formatter: Table.api.formatter.datetime
  635. },
  636. {field: 'weigh', title: __('Weigh')},
  637. {field: 'switch', title: __('Switch'), formatter: Table.api.formatter.toggle},
  638. {
  639. field: 'operate',
  640. title: __('Operate'),
  641. table: table,
  642. events: Table.api.events.operate,
  643. formatter: Table.api.formatter.operate,
  644. /*buttons:[
  645. {
  646. name: 'evaluate',
  647. text: __('Evaluate'),
  648. classname: 'btn btn-xs btn-info btn-evaluate',
  649. extend: 'data-toggle="tooltip"',
  650. icon: 'fa fa-commenting'
  651. },
  652. {
  653. name: 'copy',
  654. text: __('Copy'),
  655. classname: 'btn btn-xs btn-info btn-copy',
  656. extend: 'data-toggle="tooltip"',
  657. icon: 'fa fa-copy'
  658. }
  659. ]*/
  660. }
  661. ]
  662. ]
  663. });
  664. // 为表格绑定事件
  665. Table.api.bindevent(table);
  666. },
  667. recyclebin: function () {
  668. // 初始化表格参数配置
  669. Table.api.init({
  670. extend: {
  671. 'dragsort_url': ''
  672. }
  673. });
  674. var table = $("#table");
  675. // 初始化表格
  676. table.bootstrapTable({
  677. url: 'unishop/product/recyclebin' + location.search,
  678. pk: 'id',
  679. sortName: 'id',
  680. columns: [
  681. [
  682. {checkbox: true},
  683. {field: 'id', title: __('Id')},
  684. {field: 'title', title: __('Title'), align: 'left'},
  685. {
  686. field: 'deletetime',
  687. title: __('Deletetime'),
  688. operate: 'RANGE',
  689. addclass: 'datetimerange',
  690. formatter: Table.api.formatter.datetime
  691. },
  692. {
  693. field: 'operate',
  694. width: '130px',
  695. title: __('Operate'),
  696. table: table,
  697. events: Table.api.events.operate,
  698. buttons: [
  699. {
  700. name: 'Restore',
  701. text: __('Restore'),
  702. classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
  703. icon: 'fa fa-rotate-left',
  704. url: 'unishop/product/restore',
  705. refresh: true
  706. },
  707. /* {
  708. name: 'Destroy',
  709. text: __('Destroy'),
  710. classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
  711. icon: 'fa fa-times',
  712. url: 'unishop/product/destroy',
  713. refresh: true
  714. }*/
  715. ],
  716. formatter: Table.api.formatter.operate
  717. }
  718. ]
  719. ]
  720. });
  721. // 为表格绑定事件
  722. Table.api.bindevent(table);
  723. },
  724. add: function () {
  725. Controller.api.bindevent();
  726. var component = new specComponent().$mount();
  727. // 插入页面
  728. document.getElementById('spec').appendChild(component.$el);
  729. $('#spec').on('click', function () {
  730. // 选择图片
  731. let chooseImageButtons = $(".fachoose-spec", $("#spec"));
  732. if (chooseImageButtons) {
  733. //console.log(chooseImageButtons);
  734. for (let i = 0; i < chooseImageButtons.length; i++) {
  735. let events = $._data(chooseImageButtons[i], 'events');
  736. if (events && events['click']) {
  737. // 已绑定事件
  738. //console.log('获取图片不重新绑定事件');
  739. } else {
  740. $(chooseImageButtons[i]).on('click', function () {
  741. var that = this;
  742. var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
  743. var mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : '';
  744. parent.Fast.api.open("general/attachment/select?element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype , __('Choose'), {
  745. callback: function (data) {
  746. var button = $("#" + $(that).attr("id"));
  747. var preview_id = $(button).data("preview-id") ? $(button).data("preview-id") : "";
  748. var input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
  749. if (input_id) {
  750. $("#" + input_id).val(data.url).trigger("change");
  751. //TODO 出发一下input事件,让vue的v-model接收到数据
  752. document.getElementById(input_id).dispatchEvent(new Event('input'));
  753. // 插入图片
  754. if (preview_id && input_id) {
  755. var inputStr = data.url;
  756. var inputArr = inputStr.split(/\,/);
  757. $("#" + preview_id).empty();
  758. var tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  759. $.each(inputArr, function (i, j) {
  760. if (!j) {
  761. return true;
  762. }
  763. var data = {
  764. url: j,
  765. fullurl: Fast.api.cdnurl(j),
  766. data: $(that).data()
  767. };
  768. var html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  769. let preview = $("#" + preview_id);
  770. preview.append(html);
  771. $('.btn-trash', preview).on('click', function () {
  772. preview.empty();
  773. })
  774. });
  775. }
  776. }
  777. }
  778. });
  779. return false;
  780. });
  781. }
  782. }
  783. }
  784. });
  785. },
  786. edit: function () {
  787. Controller.api.bindevent();
  788. var component = new specComponent().$mount();
  789. // 插入页面
  790. document.getElementById('spec').appendChild(component.$el);
  791. $('#spec').on('click', function () {
  792. // 选择图片
  793. let chooseImageButtons = $(".fachoose-spec", $("#spec"));
  794. if (chooseImageButtons) {
  795. for (let i = 0; i < chooseImageButtons.length; i++) {
  796. let events = $._data(chooseImageButtons[i], 'events');
  797. let that = chooseImageButtons[i];
  798. let button = $("#" + $(that).attr("id"))
  799. let maxcount = $(button).data("maxcount");
  800. let preview_id = $(button).data("preview-id") ? $(button).data("preview-id") : "";
  801. let input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
  802. if (events && events['click']) {
  803. // 已绑定事件
  804. //console.log('获取图片不重新绑定事件');
  805. } else {
  806. $(chooseImageButtons[i]).on('click', function () {
  807. let multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
  808. let mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : '';
  809. parent.Fast.api.open("general/attachment/select?element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype , __('Choose'), {
  810. callback: function (data) {
  811. maxcount = typeof maxcount !== "undefined" ? maxcount : 0;
  812. if (input_id) {
  813. $("#" + input_id).val(data.url).trigger("change");
  814. //TODO 出发一下input事件,让vue的v-model接收到数据
  815. document.getElementById(input_id).dispatchEvent(new Event('input'));
  816. // 插入图片
  817. if (preview_id && input_id) {
  818. let inputStr = data.url;
  819. let inputArr = inputStr.split(/\,/);
  820. $("#" + preview_id).empty();
  821. let tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  822. $.each(inputArr, function (i, j) {
  823. if (!j) {
  824. return true;
  825. }
  826. let data = {
  827. url: j,
  828. fullurl: Fast.api.cdnurl(j),
  829. data: $(that).data()
  830. };
  831. let html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  832. let preview = $("#" + preview_id);
  833. preview.append(html);
  834. $('.btn-trash', preview).on('click', function(){
  835. preview.empty();
  836. $("#" + input_id).val('');
  837. //TODO 出发一下input事件,让vue的v-model接收到数据
  838. document.getElementById(input_id).dispatchEvent(new Event('input'));
  839. })
  840. });
  841. }
  842. }
  843. }
  844. });
  845. return false;
  846. });
  847. }
  848. // 首次加载的时候用到, 加载数据
  849. let inputStr = $('#' + input_id).val();
  850. if (inputStr && input_id) {
  851. let inputArr = inputStr.split(/\,/);
  852. $("#" + preview_id).empty();
  853. let tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  854. $.each(inputArr, function (i, j) {
  855. if (!j) {
  856. return true;
  857. }
  858. let data = {
  859. url: j,
  860. fullurl: Fast.api.cdnurl(j),
  861. data: $(that).data()
  862. };
  863. let html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  864. //console.log(preview_id);
  865. let preview = $("#" + preview_id);
  866. preview.append(html);
  867. $('.btn-trash', preview).on('click', function(){
  868. $("#" + preview_id).empty();
  869. $('#' + input_id).val('');
  870. //TODO 出发一下input事件,让vue的v-model接收到数据
  871. document.getElementById(input_id).dispatchEvent(new Event('input'));
  872. })
  873. });
  874. }
  875. }
  876. }
  877. });
  878. $('#spec').click();
  879. },
  880. copy:function(){
  881. Controller.api.bindevent();
  882. var component = new specComponent().$mount();
  883. // 插入页面
  884. document.getElementById('spec').appendChild(component.$el);
  885. $('#spec').on('click', function () {
  886. // 选择图片
  887. let chooseImageButtons = $(".fachoose-spec", $("#spec"));
  888. if (chooseImageButtons) {
  889. for (let i = 0; i < chooseImageButtons.length; i++) {
  890. let events = $._data(chooseImageButtons[i], 'events');
  891. let that = chooseImageButtons[i];
  892. let button = $("#" + $(that).attr("id"))
  893. let maxcount = $(button).data("maxcount");
  894. let preview_id = $(button).data("preview-id") ? $(button).data("preview-id") : "";
  895. let input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
  896. if (events && events['click']) {
  897. // 已绑定事件
  898. //console.log('获取图片不重新绑定事件');
  899. } else {
  900. $(chooseImageButtons[i]).on('click', function () {
  901. let multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
  902. let mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : '';
  903. parent.Fast.api.open("general/attachment/select?element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype , __('Choose'), {
  904. callback: function (data) {
  905. maxcount = typeof maxcount !== "undefined" ? maxcount : 0;
  906. if (input_id) {
  907. $("#" + input_id).val(data.url).trigger("change");
  908. //TODO 出发一下input事件,让vue的v-model接收到数据
  909. document.getElementById(input_id).dispatchEvent(new Event('input'));
  910. // 插入图片
  911. if (preview_id && input_id) {
  912. let inputStr = data.url;
  913. let inputArr = inputStr.split(/\,/);
  914. $("#" + preview_id).empty();
  915. let tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  916. $.each(inputArr, function (i, j) {
  917. if (!j) {
  918. return true;
  919. }
  920. let data = {
  921. url: j,
  922. fullurl: Fast.api.cdnurl(j),
  923. data: $(that).data()
  924. };
  925. let html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  926. let preview = $("#" + preview_id);
  927. preview.append(html);
  928. $('.btn-trash', preview).on('click', function(){
  929. preview.empty();
  930. $("#" + input_id).val('');
  931. //TODO 出发一下input事件,让vue的v-model接收到数据
  932. document.getElementById(input_id).dispatchEvent(new Event('input'));
  933. })
  934. });
  935. }
  936. }
  937. }
  938. });
  939. return false;
  940. });
  941. }
  942. // 首次加载的时候用到, 加载数据
  943. let inputStr = $('#' + input_id).val();
  944. if (inputStr && input_id) {
  945. let inputArr = inputStr.split(/\,/);
  946. $("#" + preview_id).empty();
  947. let tpl = $("#" + preview_id).data("template") ? $("#" + preview_id).data("template") : "";
  948. $.each(inputArr, function (i, j) {
  949. if (!j) {
  950. return true;
  951. }
  952. let data = {
  953. url: j,
  954. fullurl: Fast.api.cdnurl(j),
  955. data: $(that).data()
  956. };
  957. let html = tpl ? Template(tpl, data) : Template.render(Upload.config.previewtpl, data);
  958. //console.log(preview_id);
  959. let preview = $("#" + preview_id);
  960. preview.append(html);
  961. $('.btn-trash', preview).on('click', function(){
  962. $("#" + preview_id).empty();
  963. $('#' + input_id).val('');
  964. //TODO 出发一下input事件,让vue的v-model接收到数据
  965. document.getElementById(input_id).dispatchEvent(new Event('input'));
  966. })
  967. });
  968. }
  969. }
  970. }
  971. });
  972. $('#spec').click();
  973. },
  974. api: {
  975. bindevent: function () {
  976. Form.api.bindevent($("form[role=form]"));
  977. }
  978. },
  979. select: function () {
  980. // 初始化表格参数配置
  981. Table.api.init({
  982. extend: {
  983. index_url: 'unishop/product/index',
  984. }
  985. });
  986. $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function () {
  987. return "请输入产品名称";
  988. };
  989. var table = $("#table");
  990. // 初始化表格
  991. table.bootstrapTable({
  992. url: $.fn.bootstrapTable.defaults.extend.index_url,
  993. sortName: 'id',
  994. placeholder: '请输入产品名称',
  995. columns: [
  996. [
  997. {field: 'state', checkbox: true,},
  998. {field: 'id', title: __('Id'), sortable: true},
  999. {field: 'title', title: __('Title')},
  1000. {field: 'image', title: __('Image'), operate: false, formatter: Table.api.formatter.image},
  1001. {field: 'stock', title: __('Stock')},
  1002. {field: 'category.name', title: __('Category name')},
  1003. {
  1004. field: 'switch',
  1005. title: __('Switch'),
  1006. formatter: Table.api.formatter.status,
  1007. searchList: {"1": __('Yes'), "0": __('No')}
  1008. },
  1009. {
  1010. field: 'createtime',
  1011. title: __('Createtime'),
  1012. formatter: Table.api.formatter.datetime,
  1013. operate: 'RANGE',
  1014. addclass: 'datetimerange',
  1015. sortable: true
  1016. },
  1017. {
  1018. field: 'operate', title: __('Operate'), events: {
  1019. 'click .btn-chooseone': function (e, value, row, index) {
  1020. var dataArr = new Array();
  1021. dataArr.push(row);
  1022. Fast.api.close({data: dataArr, multiple: false});
  1023. },
  1024. }, formatter: function () {
  1025. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  1026. }
  1027. }
  1028. ]
  1029. ]
  1030. });
  1031. // 选中多个
  1032. $(document).on("click", ".btn-choose-multi", function () {
  1033. var dataArr = new Array();
  1034. $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
  1035. dataArr.push(j);
  1036. });
  1037. var multiple = Backend.api.query('multiple');
  1038. multiple = multiple == 'true' ? true : false;
  1039. Fast.api.close({data: dataArr, multiple: true});
  1040. });
  1041. // 为表格绑定事件
  1042. //Table.api.bindevent(table);
  1043. }
  1044. };
  1045. return Controller;
  1046. });