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