coupon.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. use yii\helpers\Json;
  3. use common\helpers\Url;
  4. ?>
  5. <div id="<?= $box_id ?>">
  6. <a class="blue" href="javascript:void(0)" @click="selectCouponType()">选择优惠券</a>
  7. <table class="table table-hover table-bordered" style="margin-top: 10px">
  8. <thead>
  9. <tr>
  10. <th>ID</th>
  11. <th>优惠券信息</th>
  12. <th>优惠内容</th>
  13. <th>参与商品</th>
  14. <th v-for="(column, index) in columns" :style="column.options.style">
  15. <span v-html="column.label"></span>
  16. </th>
  17. <th style="width: 50px">操作</th>
  18. </tr>
  19. </thead>
  20. <tbody>
  21. <tr v-for="(couponType, index) in couponTypes">
  22. <td>{{couponType.id}}</td>
  23. <td>
  24. {{couponType.title}} <br>
  25. <small>库存:{{couponType.stock}}</small>
  26. </td>
  27. <td>{{couponType.discount}}</td>
  28. <td>{{couponType.range_type}}</td>
  29. <th v-for="(column, key) in columns">
  30. <!-- 文本框-->
  31. <input type="text" class="form-control" v-model="couponType[column.name]" @change="changeCouponType(index, key, column.name)" :name="name + '[' + couponType.id + '][' + column.name + ']'" v-if="column.type === 'textInput'">
  32. </th>
  33. <td>
  34. <input type="hidden" class="form-control" v-model="couponType.id" :name="name + '[' + couponType.id + '][id]'">
  35. <a href="javascript:void(0);" class="blue" @click="delCouponType(index)"> 删除</a>
  36. </td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. <div v-if="couponTypes.length === 0">
  41. <input type="hidden" class="form-control" :name="name">
  42. </div>
  43. </div>
  44. <script>
  45. var <?= $box_id ?> = new Vue({
  46. el: '#<?= $box_id ?>',
  47. data: {
  48. min: <?= $min ?>,
  49. max: <?= $max ?>,
  50. name: "<?= $name ?>",
  51. selectUrl: "<?= Url::toRoute(['/tiny-shop/marketing/coupon-type/select', 'multiple' => $multiple]) ?>",
  52. columns: JSON.parse('<?= Json::encode($columns); ?>'),
  53. couponTypes: [],
  54. },
  55. methods: {
  56. // 优惠券
  57. addCouponType: function (couponType, isNewRecord = true) {
  58. // 查找重复
  59. var couponTypesLength = this.couponTypes.length;
  60. if (this.max > 0 && couponTypesLength >= this.max) {
  61. return false;
  62. }
  63. for (let i = 0; i < couponTypesLength; i++) {
  64. if (this.couponTypes[i].id === couponType.id) {
  65. return false;
  66. }
  67. }
  68. // 初始化值
  69. if (isNewRecord === true) {
  70. for (let j = 0; j < this.columns.length; j++) {
  71. var columnField = this.columns[j].name;
  72. if (couponType[columnField] === '' || couponType[columnField] === undefined) {
  73. couponType[columnField] = this.columns[j].value;
  74. }
  75. }
  76. }
  77. this.couponTypes.push(couponType);
  78. },
  79. // 删除优惠券
  80. delCouponType: function (index) {
  81. this.couponTypes.splice(index, 1);
  82. },
  83. // 优惠券变动
  84. changeCouponType: function (index, columnIndex, field) {
  85. // 验证输入的规则
  86. if (this.verify(columnIndex, this.couponTypes[index][field], this.couponTypes[index]) === false) {
  87. this.couponTypes[index][field] = 1;
  88. }
  89. },
  90. verify: function (columnIndex, price, data) {
  91. // price = price.replace(/(^\s*)|(\s*$)/g, "");
  92. if (price === '' || price === undefined || price === 'undefined') {
  93. return false;
  94. }
  95. if (!/^\d+(\.\d+)?$/.test(price)) {
  96. rfMsg('请输入合法的数字');
  97. return false;
  98. }
  99. var rule = this.columns[columnIndex].rule;
  100. // 验证字段对比
  101. if (rule.comparisonFieldMin !== '') {
  102. var comparisonFieldMin = rule.comparisonFieldMin.split(",");
  103. if (comparisonFieldMin.length > 1) {
  104. for (let i = 0; i < comparisonFieldMin.length; i++) {
  105. if (parseFloat(data[comparisonFieldMin[i]]) > price) {
  106. rfMsg('请输入不低于 ' + data[comparisonFieldMin[i]] + ' 的数字');
  107. return false;
  108. }
  109. }
  110. } else {
  111. if (parseFloat(data[rule.comparisonFieldMin]) > price) {
  112. rfMsg('请输入不低于 ' + data[rule.comparisonFieldMin] + ' 的数字');
  113. return false;
  114. }
  115. }
  116. }
  117. // 验证字段对比
  118. if (rule.comparisonFieldMax !== '') {
  119. var comparisonFieldMax = rule.comparisonFieldMax.split(",");
  120. if (comparisonFieldMax.length > 1) {
  121. for (let i = 0; i < comparisonFieldMax.length; i++) {
  122. if (parseFloat(data[comparisonFieldMax[i]]) < price) {
  123. rfMsg('请输入不超过 ' + data[comparisonFieldMax[i]] + ' 的数字');
  124. return false;
  125. }
  126. }
  127. } else {
  128. if (parseFloat(data[rule.comparisonFieldMax]) < price) {
  129. rfMsg('请输入不超过 ' + data[rule.comparisonFieldMax] + ' 的数字');
  130. return false;
  131. }
  132. }
  133. }
  134. // 验证最大值
  135. if (rule.min !== '' && parseFloat(rule.min) > price) {
  136. rfMsg('请输入不低于 ' + rule.min + ' 的数字');
  137. return false;
  138. }
  139. // 验证最小值
  140. if (rule.max !== '' && parseFloat(rule.max) < price) {
  141. rfMsg('请输入不超过 ' + rule.max + ' 的数字');
  142. return false;
  143. }
  144. return true;
  145. },
  146. selectCouponType: function () {
  147. layer.open({
  148. type: 2,
  149. title: '选择优惠券',
  150. shade: 0.3,
  151. offset: "10%",
  152. shadeClose: true,
  153. btn: ['保存', '关闭'],
  154. yes: function (index, layero) {
  155. var body = layer.getChildFrame('body', index);
  156. var couponObj = body.find('.coupon-type-id');
  157. // 获取选中的值
  158. couponObj.each(function (i, item) {
  159. if (item.checked) {
  160. <?= $box_id ?>.addCouponType($(item).data('data'))
  161. }
  162. });
  163. layer.closeAll();
  164. // 触发关闭选择
  165. $(document).trigger('select-coupon-close');
  166. },
  167. btn2: function () {
  168. layer.closeAll();
  169. },
  170. area: ['80%', '80%'],
  171. content: this.selectUrl
  172. });
  173. }
  174. },
  175. // 初始化
  176. mounted() {
  177. var couponTypes = JSON.parse('<?= Json::encode($couponTypes); ?>');
  178. for (let i = 0; i < couponTypes.length; i++) {
  179. this.addCouponType(couponTypes[i], false);
  180. }
  181. },
  182. })
  183. </script>
粤ICP备19079148号