product.js 58 KB

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