coupons.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. function debounce(handle, delay) {
  5. let time = null;
  6. return function () {
  7. let self = this,
  8. arg = arguments;
  9. clearTimeout(time);
  10. time = setTimeout(function () {
  11. handle.apply(self, arg);
  12. }, delay)
  13. }
  14. }
  15. var indexPage = new Vue({
  16. el: "#indexPage",
  17. data() {
  18. return {
  19. data: [],
  20. multipleSelection: [],
  21. searchKey: '',
  22. offset: 0,
  23. limit: 10,
  24. totalPage: 0,
  25. currentPage: 1,
  26. }
  27. },
  28. created() {
  29. this.getData();
  30. },
  31. methods: {
  32. getData() {
  33. let that = this;
  34. Fast.api.ajax({
  35. url: 'shopro/coupons/index',
  36. loading: true,
  37. type: 'GET',
  38. data: {
  39. searchWhere: that.searchKey,
  40. offset: that.offset,
  41. limit: that.limit,
  42. },
  43. }, function (ret, res) {
  44. that.data = res.data.rows;
  45. that.totalPage = res.data.total;
  46. return false;
  47. })
  48. },
  49. operation(type, id) {
  50. let that = this;
  51. switch (type) {
  52. case 'create':
  53. Fast.api.open('shopro/coupons/add', '新增', {
  54. callback() {
  55. that.getData();
  56. }
  57. })
  58. break;
  59. case 'edit':
  60. Fast.api.open("shopro/coupons/edit?ids=" + id, '编辑', {
  61. callback() {
  62. that.getData();
  63. }
  64. })
  65. break;
  66. case 'del':
  67. let ids;
  68. if (id) {
  69. ids = id;
  70. } else {
  71. let idArr = []
  72. if (that.multipleSelection.length > 0) {
  73. that.multipleSelection.forEach(i => {
  74. idArr.push(i.id)
  75. })
  76. ids = idArr.join(',')
  77. }
  78. }
  79. if (ids) {
  80. that.$confirm('此操作将删除优惠券, 是否继续?', '提示', {
  81. confirmButtonText: '确定',
  82. cancelButtonText: '取消',
  83. type: 'warning'
  84. }).then(() => {
  85. Fast.api.ajax({
  86. url: 'shopro/coupons/del/ids/' + ids,
  87. loading: true,
  88. type: 'POST'
  89. }, function (ret, res) {
  90. that.getData();
  91. return false;
  92. })
  93. }).catch(() => {
  94. that.$message({
  95. type: 'info',
  96. message: '已取消删除'
  97. });
  98. });
  99. }
  100. break;
  101. case 'recyclebin':
  102. Fast.api.open('shopro/coupons/recyclebin', '查看回收站', {
  103. callback() {
  104. that.getData();
  105. }
  106. })
  107. break;
  108. }
  109. },
  110. handleSelectionChange(val) {
  111. this.multipleSelection = val;
  112. },
  113. handleSizeChange(val) {
  114. this.offset = 0
  115. this.limit = val;
  116. this.currentPage = 1;
  117. this.getData()
  118. },
  119. handleCurrentChange(val) {
  120. this.currentPage = val;
  121. this.offset = (val - 1) * this.limit;
  122. this.getData()
  123. },
  124. tableRowClassName({
  125. rowIndex
  126. }) {
  127. if (rowIndex % 2 == 1) {
  128. return 'bg-color';
  129. }
  130. return '';
  131. },
  132. tableCellClassName({
  133. columnIndex
  134. }) {
  135. if (columnIndex == 1 || columnIndex == 2 || columnIndex == 9) {
  136. return 'cell-left';
  137. }
  138. return '';
  139. },
  140. debounceFilter: debounce(function () {
  141. this.getData()
  142. }, 1000),
  143. },
  144. watch: {
  145. searchKey(newVal, oldVal) {
  146. if (newVal != oldVal) {
  147. this.offset = 0;
  148. this.limit = 10;
  149. this.currentPage = 1;
  150. this.debounceFilter();
  151. }
  152. },
  153. },
  154. })
  155. },
  156. recyclebin: function () {
  157. // 初始化表格参数配置
  158. Table.api.init({
  159. extend: {
  160. 'dragsort_url': ''
  161. }
  162. });
  163. var table = $("#table");
  164. // 初始化表格
  165. table.bootstrapTable({
  166. url: 'shopro/coupons/recyclebin' + location.search,
  167. pk: 'id',
  168. sortName: 'id',
  169. columns: [
  170. [{
  171. checkbox: true
  172. },
  173. {
  174. field: 'id',
  175. title: __('Id')
  176. },
  177. {
  178. field: 'name',
  179. title: __('Name'),
  180. align: 'left'
  181. },
  182. {
  183. field: 'deletetime',
  184. title: __('Deletetime'),
  185. operate: 'RANGE',
  186. addclass: 'datetimerange',
  187. formatter: Table.api.formatter.datetime
  188. },
  189. {
  190. field: 'operate',
  191. width: '130px',
  192. title: __('Operate'),
  193. table: table,
  194. events: Table.api.events.operate,
  195. buttons: [{
  196. name: 'Restore',
  197. text: __('Restore'),
  198. classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
  199. icon: 'fa fa-rotate-left',
  200. url: 'shopro/coupons/restore',
  201. refresh: true
  202. },
  203. {
  204. name: 'Destroy',
  205. text: __('Destroy'),
  206. classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
  207. icon: 'fa fa-times',
  208. url: 'shopro/coupons/destroy',
  209. refresh: true
  210. }
  211. ],
  212. formatter: Table.api.formatter.operate
  213. }
  214. ]
  215. ]
  216. });
  217. // 为表格绑定事件
  218. Table.api.bindevent(table);
  219. },
  220. select: function () {
  221. // 初始化表格参数配置
  222. Table.api.init({
  223. extend: {
  224. index_url: 'shopro/coupons/select',
  225. }
  226. });
  227. var idArr = [];
  228. var selectArr = [];
  229. var table = $("#table");
  230. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
  231. if (e.type == 'check' || e.type == 'uncheck') {
  232. row = [row];
  233. } else {
  234. idArr = [];
  235. selectArr = [];
  236. }
  237. $.each(row, function (i, j) {
  238. if (e.type.indexOf("uncheck") > -1) {
  239. var index = idArr.indexOf(j.id);
  240. if (index > -1) {
  241. idArr.splice(index, 1);
  242. }
  243. if (indexall > -1) {
  244. selectArr.splice(index, 1);
  245. }
  246. } else {
  247. idArr.indexOf(j.id) == -1 && idArr.push(j.id);
  248. selectArr.indexOf(j) == -1 && selectArr.push(j);
  249. }
  250. });
  251. });
  252. // 初始化表格
  253. table.bootstrapTable({
  254. url: $.fn.bootstrapTable.defaults.extend.index_url,
  255. pk: 'id',
  256. sortName: 'id',
  257. showToggle: false,
  258. showExport: false,
  259. columns: [
  260. [{
  261. checkbox: true
  262. },
  263. {
  264. field: 'id',
  265. title: __('Id')
  266. },
  267. {
  268. field: 'name',
  269. title: __('Name')
  270. },
  271. {
  272. field: 'goods_ids',
  273. title: __('Goods_ids')
  274. },
  275. {
  276. field: 'amount',
  277. title: __('Amount'),
  278. operate: 'BETWEEN'
  279. },
  280. {
  281. field: 'enough',
  282. title: __('Enough'),
  283. operate: 'BETWEEN'
  284. },
  285. {
  286. field: 'stock',
  287. title: __('Stock')
  288. },
  289. {
  290. field: 'limit',
  291. title: __('Limit')
  292. },
  293. {
  294. field: 'gettime',
  295. title: __('Gettime')
  296. },
  297. {
  298. field: 'usetime',
  299. title: __('Usetime')
  300. },
  301. {
  302. field: 'description',
  303. title: __('Description')
  304. },
  305. {
  306. field: 'createtime',
  307. title: __('Createtime'),
  308. operate: 'RANGE',
  309. addclass: 'datetimerange',
  310. formatter: Table.api.formatter.datetime
  311. },
  312. {
  313. field: 'updatetime',
  314. title: __('Updatetime'),
  315. operate: 'RANGE',
  316. addclass: 'datetimerange',
  317. formatter: Table.api.formatter.datetime
  318. },
  319. {
  320. field: 'operate',
  321. title: __('Operate'),
  322. events: {
  323. 'click .btn-chooseone': function (e, value, row, index) {
  324. var multiple = Backend.api.query('multiple');
  325. multiple = multiple == 'true' ? true : false;
  326. row.ids = row.id.toString()
  327. Fast.api.close({
  328. data: row,
  329. multiple: multiple
  330. });
  331. },
  332. },
  333. formatter: function () {
  334. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  335. }
  336. }
  337. ]
  338. ]
  339. });
  340. // 选中多个
  341. $(document).on("click", ".btn-choose-multi", function () {
  342. if (Backend.api.query('type') == 'decorate') {
  343. var multiple = Backend.api.query('multiple');
  344. multiple = multiple == 'true' ? true : false;
  345. Fast.api.close({
  346. data: selectArr,
  347. multiple: multiple
  348. });
  349. } else {
  350. let row = {}
  351. var multiple = Backend.api.query('multiple');
  352. multiple = multiple == 'true' ? true : false;
  353. row.ids = idArr.join(",")
  354. Fast.api.close({
  355. data: row,
  356. multiple: multiple
  357. });
  358. }
  359. // var multiple = Backend.api.query('multiple');
  360. // multiple = multiple == 'true' ? true : false;
  361. // let row = {}
  362. // row.ids = idArr.join(",")
  363. // Fast.api.close({
  364. // data: row,
  365. // multiple: multiple
  366. // });
  367. });
  368. // 为表格绑定事件
  369. Table.api.bindevent(table);
  370. require(['upload'], function (Upload) {
  371. Upload.api.plupload($("#toolbar .plupload"), function () {
  372. $(".btn-refresh").trigger("click");
  373. });
  374. });
  375. },
  376. add: function () {
  377. Controller.detailInit('add');
  378. },
  379. edit: function () {
  380. Controller.detailInit('edit');
  381. },
  382. detailInit: function (type) {
  383. Vue.directive('enterNumber', {
  384. inserted: function (el) {
  385. let changeValue = (el, type) => {
  386. const e = document.createEvent('HTMLEvents')
  387. e.initEvent(type, true, true)
  388. el.dispatchEvent(e)
  389. }
  390. el.addEventListener("keyup", function (e) {
  391. let input = e.target;
  392. let reg = new RegExp('^((?:(?:[1-9]{1}\\d*)|(?:[0]{1}))(?:\\.(?:\\d){0,2})?)(?:\\d*)?$');
  393. let matchRes = input.value.match(reg);
  394. if (matchRes === null) {
  395. input.value = "";
  396. } else {
  397. if (matchRes[1] !== matchRes[0]) {
  398. input.value = matchRes[1];
  399. }
  400. }
  401. changeValue(input, 'input')
  402. });
  403. }
  404. });
  405. Vue.directive('positiveInteger', {
  406. inserted: function (el) {
  407. el.addEventListener("keypress", function (e) {
  408. e = e || window.event;
  409. let charcode = typeof e.charCode == 'number' ? e.charCode : e.keyCode;
  410. let re = /\d/;
  411. if (!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey) {
  412. if (e.preventDefault) {
  413. e.preventDefault();
  414. } else {
  415. e.returnValue = false;
  416. }
  417. }
  418. });
  419. }
  420. });
  421. var pageDetail = new Vue({
  422. el: "#pageDetail",
  423. computed: {
  424. enoughAmount() {
  425. return this.detailData.enough - this.detailData.amount
  426. }
  427. },
  428. data() {
  429. var checkEnough = (rule, value, callback) => {
  430. if (value && this.enoughAmount > 0) {
  431. callback();
  432. } else {
  433. callback(new Error('使用门槛必须大于减免金额'));
  434. }
  435. }
  436. var checkAmount = (rule, value, callback) => {
  437. if (value && this.enoughAmount > 0) {
  438. callback();
  439. } else {
  440. callback(new Error('减免金额必须小于使用门槛'));
  441. }
  442. }
  443. var checkTime = (rule, value, callback) => {
  444. if(value){
  445. if (value.length > 0 && value[0]) {
  446. callback();
  447. } else {
  448. callback(new Error('领取结束时间不能大于使用结束时间'));
  449. }
  450. }
  451. }
  452. return {
  453. optType: type,
  454. detailData: {},
  455. detailDataInit: {
  456. amount: "",
  457. description: "",
  458. enough: "",
  459. gettime: "",
  460. goods_ids: "",
  461. limit: 1,
  462. name: "",
  463. stock: 0,
  464. usetime: "",
  465. type: 'cash',
  466. goods_ids: '',
  467. goods_type: 'all'
  468. },
  469. detail_id: null,
  470. rules: {
  471. name: [{
  472. required: true,
  473. message: '请输入优惠券名称',
  474. trigger: 'blur'
  475. }],
  476. type: [{
  477. required: true,
  478. message: '请选择优惠券类型',
  479. trigger: 'blur'
  480. }],
  481. gettime: [{
  482. required: true,
  483. message: '请选择有效时间',
  484. trigger: 'change'
  485. }, {
  486. validator: checkTime,
  487. trigger: 'change'
  488. }],
  489. usetime: [{
  490. required: true,
  491. message: '请选择用券时间',
  492. trigger: 'change'
  493. }, {
  494. validator: checkTime,
  495. trigger: 'change'
  496. }],
  497. enough: [{
  498. required: true,
  499. message: '请输入使用门槛',
  500. trigger: 'change'
  501. }, {
  502. validator: checkEnough,
  503. trigger: 'change'
  504. }],
  505. amount: [{
  506. required: true,
  507. message: '请输入减免金额',
  508. trigger: 'change'
  509. }, {
  510. validator: checkAmount,
  511. trigger: 'change'
  512. }],
  513. goods_ids: [{
  514. required: true,
  515. message: '请选择商品',
  516. trigger: 'change'
  517. }],
  518. },
  519. goods_arr: []
  520. }
  521. },
  522. created() {
  523. this.detailData = JSON.parse(JSON.stringify(this.detailDataInit))
  524. if (this.optType == 'edit') {
  525. this.detail_id = Config.row.id;
  526. for (key in this.detailData) {
  527. this.detailData[key] = Config.row[key]
  528. }
  529. this.detailData.gettime = this.detailData.gettime.split(' - ');
  530. this.detailData.usetime = this.detailData.usetime.split(' - ');
  531. if (this.detailData.goods_ids == 0) {
  532. this.detailData.goods_type = 'all'
  533. } else {
  534. this.detailData.goods_type = 'part'
  535. }
  536. if (Config.row.goods) {
  537. this.goods_arr = Config.row.goods;
  538. this.goods_arr.forEach(i => {
  539. i.selected = false;
  540. })
  541. }
  542. }
  543. },
  544. methods: {
  545. operation(type, id) {
  546. let that = this;
  547. switch (type) {
  548. case 'selected':
  549. let idsArr = [];
  550. if (that.goods_arr.length > 0) {
  551. that.goods_arr.forEach(i => {
  552. idsArr.push(i.id)
  553. })
  554. }
  555. let ids = idsArr.join(',')
  556. parent.Fast.api.open('shopro/goods/goods/select?multiple=true&type=activity&ids=' + ids, '选择商品', {
  557. callback(data) {
  558. that.goods_arr = data.data
  559. that.goods_arr.forEach(i => {
  560. i.selected = false;
  561. })
  562. that.$forceUpdate();
  563. }
  564. })
  565. break;
  566. case 'selectedDel':
  567. that.goods_arr.forEach(i => {
  568. i.selected = false;
  569. })
  570. if (that.goods_arr[id]) {
  571. that.$set(that.goods_arr[id], 'selected', true)
  572. that.$forceUpdate()
  573. }
  574. break;
  575. case 'clear':
  576. that.goods_arr = []
  577. break;
  578. case 'delete':
  579. that.goods_arr.splice(id, 1)
  580. break;
  581. case 'edit':
  582. Fast.api.open("shopro/coupons/edit/?ids=" + that.detail_id, '编辑', {
  583. callback() {
  584. that.getData();
  585. }
  586. })
  587. break;
  588. }
  589. },
  590. changeTime(type) {
  591. if (this.detailData.usetime && this.detailData.gettime) {
  592. if (this.detailData.usetime[1] < this.detailData.gettime[1]) {
  593. this.detailData[type] = [false]
  594. }
  595. }
  596. },
  597. submit(type, check) {
  598. let that = this;
  599. if (type == 'yes') {
  600. this.$refs[check].validate((valid) => {
  601. if (valid) {
  602. let subData = JSON.parse(JSON.stringify(that.detailData));
  603. subData.gettime = subData.gettime.join(' - ');
  604. subData.usetime = subData.usetime.join(' - ');
  605. if (subData.goods_type == 'all') {
  606. subData.goods_ids = 0
  607. } else {
  608. if (that.goods_arr.length == 0) {
  609. return false;
  610. }
  611. let goodsArr = []
  612. that.goods_arr.forEach(i => {
  613. goodsArr.push(i.id)
  614. })
  615. subData.goods_ids = goodsArr.join(',')
  616. }
  617. delete subData.goods_type
  618. if (that.optType != 'add') {
  619. Fast.api.ajax({
  620. url: 'shopro/coupons/edit?ids=' + that.detail_id,
  621. loading: true,
  622. type: "POST",
  623. data: {
  624. data: JSON.stringify(subData)
  625. }
  626. }, function (ret, res) {
  627. Fast.api.close({
  628. data: true
  629. })
  630. })
  631. } else {
  632. Fast.api.ajax({
  633. url: 'shopro/coupons/add',
  634. loading: true,
  635. type: "POST",
  636. data: {
  637. data: JSON.stringify(subData)
  638. }
  639. }, function (ret, res) {
  640. Fast.api.close({
  641. data: true
  642. })
  643. })
  644. }
  645. } else {
  646. return false;
  647. }
  648. });
  649. } else {
  650. Fast.api.close()
  651. that.storeForm = JSON.parse(JSON.stringify(that.storeFormInit[that.store_type]));
  652. }
  653. },
  654. },
  655. })
  656. },
  657. api: {
  658. bindevent: function () {
  659. Form.api.bindevent($("form[role=form]"));
  660. }
  661. }
  662. };
  663. return Controller;
  664. });