edit.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <!-- 引一个当前组件 -->
  2. <?php
  3. use common\helpers\ComponentHelper;
  4. use common\helpers\AddonHelper;
  5. //Yii::$app->loadComponentView('com-coupon');
  6. ComponentHelper::loadComponentView('coupon-select', AddonHelper::getAddonRootPath('Coupon') . 'backend/views/components');
  7. ?>
  8. <div id="app" v-cloak>
  9. <el-card shadow="never" style="border:0;min-width: 1200px;">
  10. <el-form ref="formData" :model="formData" label-width="90px" :rules="rules">
  11. <el-form-item label="活动名称" prop="name">
  12. <el-input v-model="formData.name" style="max-width: 200px;"></el-input>
  13. </el-form-item>
  14. <el-form-item label="活动时间" prop="dataTime">
  15. <el-date-picker
  16. v-model="formData.dataTime"
  17. type="datetimerange"
  18. range-separator="至"
  19. start-placeholder="开始日期"
  20. end-placeholder="结束日期"
  21. @change="updateTime">
  22. </el-date-picker>
  23. </el-form-item>
  24. <el-form-item label="赠送说明">
  25. <el-input type="textarea" v-model="formData.give_desc" style="max-width: 400px;"></el-input>
  26. </el-form-item>
  27. <el-form-item label="赠送条件">
  28. <el-radio-group v-model="formData.give_cond">
  29. <el-radio :label="0">单次充值</el-radio>
  30. <el-radio :label="1">累计充值</el-radio>
  31. </el-radio-group>
  32. </el-form-item>
  33. <el-form-item label="赠送奖励">
  34. <el-table :data="rewardInfo" stripe style="width: 100%">
  35. <el-table-column prop="num" label="赠送条件" width="240" class="accon">
  36. <template slot-scope="scope">
  37. <el-input v-model="scope.row.num" type="number" style="max-width: 150px;padding-left: 10px;"
  38. @input="validatorInfo($event,scope.$index)">
  39. <template slot="append">元</template>
  40. </el-input>
  41. </template>
  42. </el-table-column>
  43. <el-table-column prop="data" label="赠送内容">
  44. <template slot-scope="scope">
  45. <div style="margin-bottom: 14px;" v-for="(type,index) in scope.row.data" :key="index">
  46. <div style="margin-bottom: 10px;" flex>
  47. <el-select v-model="scope.row.data[index].key" placeholder="请选择"
  48. @change="updateType($event,scope.$index,index)">
  49. <el-option
  50. v-for="item in rechargeType"
  51. :key="item.value"
  52. :label="item.label"
  53. :value="item.value"
  54. :disabled="scope.row.data.findIndex(v => v.key == item.value) != -1"
  55. >
  56. </el-option>
  57. </el-select>
  58. <el-button v-if="scope.row.data[index].key == 0" style="margin-left: 12px;"
  59. @click="openCouponPop(scope.$index)">选择优惠券
  60. </el-button>
  61. <div style="margin-left: 10px;" v-else>
  62. <el-input v-model="scope.row.data[index].data" type="number"
  63. @input="validatorInfo($event,scope.$index,2,scope.row.data[index].key)">
  64. <template slot="append">{{scope.row.data[index].key == 2?'分':'元'}}
  65. </template>
  66. </el-input>
  67. </div>
  68. <el-button @click="delAward(scope.row.data[index].key,scope.$index)"
  69. v-if="scope.row.data.length > 1" type="text" icon="el-icon-close"
  70. style="color: #ec1717;font-size: 20px;padding: 0;margin-left: 12px;"></el-button>
  71. </div>
  72. <el-table v-if="scope.row.data[index].key == 0" :data="scope.row.data[index].data"
  73. stripe style="width: 100%;">
  74. <el-table-column prop="name" label="优惠券名称" width="160"></el-table-column>
  75. <el-table-column prop="discount_method" label="优惠券内容"></el-table-column>
  76. <el-table-column label="库存(张)" width="160">
  77. <template slot-scope="couponScope">
  78. {{couponScope.row.total_count == -1?'不限量':couponScope.row.total_count}}
  79. </template>
  80. </el-table-column>
  81. <el-table-column prop="dcount" label="赠送张数" width="160">
  82. <template slot-scope="couponScope">
  83. <el-input v-model="couponScope.row.num" type="number"
  84. @input="validatorInfo($event,scope.$index,3,0,couponScope.$index)">
  85. <template slot="append">张</template>
  86. </el-input>
  87. </template>
  88. </el-table-column>
  89. <el-table-column label="操作" width="100" align="center">
  90. <template slot-scope="couponScope">
  91. <el-button type="text" size="small"
  92. @click="delCoupon(couponScope.$index,scope.$index)">删除
  93. </el-button>
  94. </template>
  95. </el-table-column>
  96. </el-table>
  97. </div>
  98. <div v-if="scope.row.data.length != rechargeType.length">
  99. <el-button @click="addAward(scope.$index)" size="small" plain>添加奖励内容</el-button>
  100. </div>
  101. </template>
  102. </el-table-column>
  103. <el-table-column label="操作" width="100" align="center">
  104. <template slot-scope="scope">
  105. <el-button @click="deleteReward(scope.$index)" type="text" size="small">删除</el-button>
  106. </template>
  107. </el-table-column>
  108. </el-table>
  109. <div>
  110. <el-button size="small" style="margin-top: 10px;" @click="addRewardInfo">添加赠送条件</el-button>
  111. </div>
  112. </el-form-item>
  113. <el-form-item>
  114. <el-button type="primary" @click="submitForm" :loading="subLoading">保存</el-button>
  115. </el-form-item>
  116. </el-form>
  117. <!-- 优惠券弹窗 -->
  118. <!-- <com-coupon :coupon-pop="couponPop" :sel-coupon="selCoupon" @close="showCoupon" @submit="determine"></com-coupon>-->
  119. <coupon-select :coupon-loading="couponPop" :coupon_arr="coupon_arr"
  120. @submit="handleCoupon"
  121. @close="showCoupon"></coupon-select>
  122. </el-card>
  123. </div>
  124. <script>
  125. const app = new Vue({
  126. el: '#app',
  127. data() {
  128. return {
  129. id: -1,
  130. formData: {
  131. name: "",
  132. start_at: "",
  133. end_at: "",
  134. give_desc: "",
  135. give_cond: 0,
  136. dataTime: [],
  137. give_info: [],
  138. },
  139. coupon_arr: [],
  140. couponLoading: false,
  141. rechargeType: [
  142. {
  143. value: 0,
  144. label: '优惠券'
  145. },
  146. {
  147. value: 1,
  148. label: '余额'
  149. },
  150. {
  151. value: 2,
  152. label: '积分'
  153. },
  154. // {
  155. // value: '3',
  156. // label: '红包'
  157. // },
  158. ],
  159. // typeArr: ['coupon', 'balance', 'score', 'res_packet'], //跟rechargeType对应
  160. typeArr: ['coupon', 'balance', 'score'], //跟rechargeType对应
  161. selRechargeType: [{
  162. key: 0,
  163. type: 'coupon',
  164. data: [],
  165. }],
  166. rewardInfo: [{
  167. num: 0,
  168. data: [{
  169. key: 0,
  170. name: "优惠券",
  171. type: 'coupon',
  172. data: []
  173. }],
  174. }],//奖励内容
  175. couponPop: false,
  176. couponPopIndex: -1,
  177. rules: {
  178. name: [{required: true, message: '活动名称不能为空', trigger: 'blur'},],
  179. dataTime: [{required: true, message: '活动时间不能为空', trigger: 'blur'}],
  180. },
  181. subLoading: false,
  182. selCoupon: [],//打开优惠券弹窗传递已选中的优惠券
  183. coupon_arr: [],//打开优惠券弹窗传递已选中的优惠券
  184. }
  185. },
  186. created() {
  187. if (getQuery('id') && getQuery('id') != 0) {
  188. this.id = getQuery('id');
  189. this.getRechargeData();
  190. }
  191. },
  192. methods: {
  193. // 获取充值信息
  194. getRechargeData() {
  195. request({
  196. params: {
  197. r: '/recharge/default/detail',
  198. },
  199. method: 'post',
  200. data: {
  201. id: this.id,
  202. }
  203. }).then(res => {
  204. if (res.data.code == 0) {
  205. this.formData = res.data.data;
  206. this.$set(this.formData,'dataTime',[new Date(res.data.data.start_at * 1000), new Date(res.data.data.end_at * 1000)])
  207. this.rewardInfo = res.data.data.give_info;
  208. console.log(this.formData.dataTime)
  209. }
  210. })
  211. },
  212. // 添加奖励条件
  213. addAward(index) {
  214. let key = 0;
  215. for (let i = 0; i < this.rechargeType.length; i++) {
  216. let dataIndex = this.rewardInfo[index].data.findIndex(v => v.key == this.rechargeType[i].value);
  217. if (dataIndex == -1) {
  218. key = i;
  219. break;
  220. }
  221. }
  222. let obj = {
  223. key,
  224. name: this.rechargeType[key].label,
  225. type: this.typeArr[key],
  226. data: key == 0 ? [] : "",
  227. }
  228. if (key != 0) {
  229. obj.data = 0;
  230. }
  231. this.rewardInfo[index].data.push(obj);
  232. },
  233. // 删除奖励条件
  234. delAward(key, index) {
  235. let couponIndex = this.rewardInfo[index].data.findIndex(v => v.key == key);
  236. this.rewardInfo[index].data.splice(couponIndex, 1);
  237. },
  238. // 打开优惠券弹窗
  239. openCouponPop(index) {
  240. this.couponPopIndex = index;
  241. let dataIndex = this.rewardInfo[index].data.findIndex(v => v.type == 'coupon');
  242. this.selCoupon = this.rewardInfo[index].data[dataIndex].data;
  243. this.coupon_arr = this.rewardInfo[index].data[dataIndex].data;
  244. this.couponPop = true;
  245. },
  246. showCoupon() {
  247. this.couponPopIndex = -1;
  248. this.couponPop = false;
  249. },
  250. //弹窗确定
  251. determine(arr) {
  252. let couponPopData = this.rewardInfo[this.couponPopIndex].data;
  253. let couponIndex = couponPopData.findIndex(v => v.key == 0);
  254. if (couponIndex != -1) {
  255. this.rewardInfo[this.couponPopIndex].data[couponIndex].data = arr;
  256. }
  257. this.couponPop = false;
  258. },
  259. // 删除优惠券
  260. delCoupon(selCouponIndex, index) {
  261. let couponPopData = this.rewardInfo[index].data;
  262. let couponIndex = couponPopData.findIndex(v => v.key == 0);
  263. if (couponIndex != -1) {
  264. this.rewardInfo[index].data[couponIndex].data.splice(selCouponIndex, 1);
  265. }
  266. },
  267. handleCoupon(data) {
  268. this.couponLoading = false;
  269. let couponPopData = this.rewardInfo[this.couponPopIndex].data;
  270. let couponIndex = couponPopData.findIndex(v => v.key == 0);
  271. if(couponIndex != -1){
  272. this.rewardInfo[this.couponPopIndex].data[couponIndex].data = data;
  273. }
  274. this.couponPop = false;
  275. },
  276. // 添加赠送条件
  277. addRewardInfo() {
  278. this.rewardInfo.push({
  279. num: 0,
  280. data: [{
  281. key: 0,
  282. name: "优惠券",
  283. type: 'coupon',
  284. data: []
  285. }],
  286. });
  287. },
  288. // 删除赠送条件
  289. deleteReward(index) {
  290. this.rewardInfo.splice(index, 1);
  291. },
  292. updateTime(val) {
  293. if (val) {
  294. this.formData.start_at = val[0].getTime() / 1000;
  295. this.formData.end_at = val[1].getTime() / 1000;
  296. } else {
  297. this.formData.start_at = "";
  298. this.formData.end_at = "";
  299. }
  300. },
  301. // 表单数值验证
  302. validatorInfo(val, index, type = 1, key, couponIndex) {
  303. console.info(val, index, type, key, couponIndex);
  304. if (val == '') {
  305. return;
  306. }
  307. let dataIndex = -1;
  308. if (type != 1) {
  309. dataIndex = this.rewardInfo[index].data.findIndex(v => v.key == key);
  310. }
  311. if (val <= 0) {
  312. if (type == 2) {
  313. this.rewardInfo[index].data[dataIndex].data = 0;
  314. } else if (type == 3) {
  315. this.rewardInfo[index].data[dataIndex].data[couponIndex].num = 0;
  316. } else {
  317. this.rewardInfo[index].num = 0;
  318. }
  319. return;
  320. }
  321. // 是否能最多保留两位小数
  322. let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
  323. if (!reg.test(val) && type != 3) {
  324. let i = val.indexOf(".") + 3;
  325. val = val.substr(0, i)
  326. if (type == 2) {
  327. this.rewardInfo[index].data[dataIndex].data = val;
  328. } else {
  329. this.rewardInfo[index].num = val;
  330. }
  331. }
  332. if (type == 3) {
  333. // total_count
  334. val = parseInt(val);
  335. let totalCount = this.rewardInfo[index].data[dataIndex].data[couponIndex].total_count;
  336. if (totalCount < val && totalCount != -1) {
  337. this.rewardInfo[index].data[dataIndex].data[couponIndex].num = totalCount;
  338. } else {
  339. this.rewardInfo[index].data[dataIndex].data[couponIndex].num = val;
  340. }
  341. }
  342. },
  343. // 赠送奖励表单严重
  344. validatorForm() {
  345. /* this.rewardInfo.forEach(v => {
  346. if()
  347. }) */
  348. // let arr = ['优惠券', '余额', '积分', '红包'];
  349. let arr = ['优惠券', '余额', '积分'];
  350. for (let i = 0; i < this.rewardInfo.length; i++) {
  351. if (this.rewardInfo[i].num <= 0) {
  352. return "赠送条件不能小于等于0";
  353. }
  354. for (let j = 0; j < this.rewardInfo[i].data.length; j++) {
  355. if (this.rewardInfo[i].data[j].key == 0) {
  356. if (this.rewardInfo[i].data[j].data.length == 0) {
  357. return "请选择赠送内容的优惠券";
  358. }
  359. text = this.validatorCoupon(this.rewardInfo[i].data[j].data);
  360. if (text) return text;
  361. } else {
  362. if (!this.rewardInfo[i].data[j].data || this.rewardInfo[i].data[j].data <= 0) {
  363. return "赠送内容的" + arr[this.rewardInfo[i].data[j].key] + "值不能为空或小于等于0";
  364. }
  365. }
  366. }
  367. }
  368. for (let i = 0; i < this.rewardInfo.length; i++) {
  369. let num = this.rewardInfo[i].num;
  370. let arr = this.rewardInfo.filter(v => v.num == num);
  371. if (arr.length > 1) {
  372. return "赠送条件不能重复"
  373. }
  374. }
  375. return "";
  376. },
  377. // 验证优惠券
  378. validatorCoupon(couponList) {
  379. for (let i = 0; i < couponList.length; i++) {
  380. if (couponList[i].num <= 0) {
  381. return "优惠券赠送数量不能小于等于0";
  382. }
  383. }
  384. return "";
  385. },
  386. // 更改赠送内容类型
  387. updateType(key, index, dataIndex) {
  388. this.rewardInfo[index].data[dataIndex].name = this.rechargeType[key].label;
  389. this.rewardInfo[index].data[dataIndex].type = this.typeArr[key];
  390. if (key == 0) {
  391. this.rewardInfo[index].data[dataIndex].data = [];
  392. } else {
  393. if (Array.isArray(this.rewardInfo[index].data[dataIndex].data)) {
  394. this.rewardInfo[index].data[dataIndex].data = 0;
  395. }
  396. }
  397. },
  398. // 保存
  399. submitForm() {
  400. this.$refs['formData'].validate((valid) => {
  401. if (valid) {
  402. let text = this.validatorForm();
  403. console.info(text);
  404. if (text) {
  405. this.$message.error(text);
  406. return;
  407. }
  408. if (!this.formData.id) {
  409. if (this.formData.start_at < (Date.parse(new Date()) / 1000)) {
  410. this.$message.error('开始时间只能创建当前时间或者之后的时间');
  411. return;
  412. }
  413. }
  414. this.$set(this.formData, 'give_info', this.rewardInfo);
  415. console.log(this.formData)
  416. this.subLoading = true;
  417. this.$request({
  418. params: {
  419. r: 'recharge/default/edit',
  420. },
  421. method: 'post',
  422. data: this.formData,
  423. }).then(res => {
  424. this.subLoading = false;
  425. if (res.data.code == 0) {
  426. this.$message.success("保存成功");
  427. navigateTo({
  428. r: "recharge/default/index"
  429. })
  430. } else {
  431. this.$message.error(res.data.msg);
  432. }
  433. }).catch(e => {
  434. this.btnLoading = false;
  435. });
  436. }
  437. });
  438. },
  439. },
  440. })
  441. </script>
  442. <style>
  443. .el-table_1_column_1 .cell {
  444. display: flex;
  445. align-items: center;
  446. }
  447. </style>