index.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. <template>
  2. <div class="app-container">
  3. <!-- 查询条件 -->
  4. <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
  5. <el-form-item label="模板名称" prop="templateName">
  6. <el-input
  7. v-model="queryParams.templateName"
  8. placeholder="请输入模板名称"
  9. clearable
  10. @keyup.enter.native="handleQuery"
  11. />
  12. </el-form-item>
  13. <el-form-item label="状态" prop="status">
  14. <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
  15. <el-option label="启用" value="1" />
  16. <el-option label="禁用" value="0" />
  17. </el-select>
  18. </el-form-item>
  19. <el-form-item label="标签" prop="tag">
  20. <el-input
  21. v-model="queryParams.tag"
  22. placeholder="请输入标签"
  23. clearable
  24. @keyup.enter.native="handleQuery"
  25. />
  26. </el-form-item>
  27. <el-form-item>
  28. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  29. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  30. </el-form-item>
  31. </el-form>
  32. <!-- 操作按钮 -->
  33. <el-row :gutter="10" class="mb8">
  34. <el-col :span="1.5">
  35. <el-button
  36. type="primary"
  37. plain
  38. icon="el-icon-plus"
  39. size="mini"
  40. @click="handleAdd"
  41. v-hasPermi="['recharge:template:add']"
  42. >新增</el-button>
  43. </el-col>
  44. <el-col :span="1.5">
  45. <el-button
  46. type="danger"
  47. plain
  48. icon="el-icon-delete"
  49. size="mini"
  50. :disabled="multiple"
  51. @click="handleDelete"
  52. v-hasPermi="['recharge:template:remove']"
  53. >删除</el-button>
  54. </el-col>
  55. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  56. </el-row>
  57. <!-- 表格数据 -->
  58. <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange">
  59. <el-table-column type="selection" width="55" align="center" />
  60. <el-table-column label="模板ID" align="center" prop="id" width="80" />
  61. <el-table-column label="模板名称" align="center" prop="templateName" :show-overflow-tooltip="true" />
  62. <el-table-column label="储值金额" align="center" prop="rechargeAmount" width="100">
  63. <template slot-scope="scope">
  64. <span>¥{{ scope.row.rechargeAmount }}</span>
  65. </template>
  66. </el-table-column>
  67. <el-table-column label="赠送金额" align="center" prop="bonusAmount" width="100">
  68. <template slot-scope="scope">
  69. <span>¥{{ scope.row.bonusAmount || 0 }}</span>
  70. </template>
  71. </el-table-column>
  72. <el-table-column label="标签" align="center" prop="tag" width="100">
  73. <template slot-scope="scope">
  74. <el-tag v-if="scope.row.tag" size="mini">{{ scope.row.tag }}</el-tag>
  75. </template>
  76. </el-table-column>
  77. <el-table-column label="限购次数" align="center" prop="purchaseLimit" width="100" />
  78. <el-table-column label="排序" align="center" prop="sortOrder" width="80" />
  79. <el-table-column label="状态" align="center" prop="status" width="100">
  80. <template slot-scope="scope">
  81. <el-switch
  82. v-model="scope.row.status"
  83. :active-value="1"
  84. :inactive-value="0"
  85. @change="handleStatusChange(scope.row)"
  86. ></el-switch>
  87. </template>
  88. </el-table-column>
  89. <el-table-column label="默认选中" align="center" prop="isDefault" width="100">
  90. <template slot-scope="scope">
  91. <el-tag v-if="scope.row.isDefault === 1" type="success" size="mini">是</el-tag>
  92. <el-tag v-else type="info" size="mini">否</el-tag>
  93. </template>
  94. </el-table-column>
  95. <el-table-column label="绑定优惠券" align="center" width="100">
  96. <template slot-scope="scope">
  97. <el-tag v-if="scope.row.couponCount" type="primary" size="mini">{{ scope.row.couponCount }}张</el-tag>
  98. <el-tag v-else type="info" size="mini">无</el-tag>
  99. </template>
  100. </el-table-column>
  101. <el-table-column label="有效期" align="center" width="180">
  102. <template slot-scope="scope">
  103. <span v-if="scope.row.startTime && scope.row.endTime">
  104. {{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }} 至 {{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}
  105. </span>
  106. <span v-else>永久有效</span>
  107. </template>
  108. </el-table-column>
  109. <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
  110. <template slot-scope="scope">
  111. <el-button
  112. size="mini"
  113. type="text"
  114. icon="el-icon-view"
  115. @click="handleDetail(scope.row)"
  116. >详情</el-button>
  117. <el-button
  118. size="mini"
  119. type="text"
  120. icon="el-icon-edit"
  121. @click="handleUpdate(scope.row)"
  122. v-hasPermi="['recharge:template:edit']"
  123. >修改</el-button>
  124. <el-button
  125. size="mini"
  126. type="text"
  127. icon="el-icon-delete"
  128. @click="handleDelete(scope.row)"
  129. v-hasPermi="['recharge:template:remove']"
  130. >删除</el-button>
  131. </template>
  132. </el-table-column>
  133. </el-table>
  134. <!-- 分页 -->
  135. <pagination
  136. v-show="total>0"
  137. :total="total"
  138. :page.sync="queryParams.pageNum"
  139. :limit.sync="queryParams.pageSize"
  140. @pagination="getList"
  141. />
  142. <!-- 添加或修改充值模板对话框 -->
  143. <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
  144. <el-form ref="form" :model="form" :rules="rules" label-width="120px">
  145. <el-row>
  146. <el-col :span="12">
  147. <el-form-item label="模板名称" prop="templateName">
  148. <el-input v-model="form.templateName" placeholder="请输入模板名称" />
  149. </el-form-item>
  150. </el-col>
  151. <el-col :span="12">
  152. <el-form-item label="储值金额" prop="rechargeAmount">
  153. <el-input-number v-model="form.rechargeAmount" :precision="2" :min="0" placeholder="请输入储值金额" />
  154. </el-form-item>
  155. </el-col>
  156. </el-row>
  157. <el-row>
  158. <el-col :span="12">
  159. <el-form-item label="赠送金额" prop="bonusAmount">
  160. <el-input-number v-model="form.bonusAmount" :precision="2" :min="0" placeholder="请输入赠送金额" />
  161. </el-form-item>
  162. </el-col>
  163. <el-col :span="12">
  164. <el-form-item label="限购次数" prop="purchaseLimit">
  165. <el-input-number v-model="form.purchaseLimit" :min="0" placeholder="请输入限购次数,0为不限制" />
  166. </el-form-item>
  167. </el-col>
  168. </el-row>
  169. <el-row>
  170. <el-col :span="12">
  171. <el-form-item label="排序序号" prop="sortOrder">
  172. <el-input-number v-model="form.sortOrder" :min="0" placeholder="请输入排序序号" />
  173. </el-form-item>
  174. </el-col>
  175. <el-col :span="12">
  176. <el-form-item label="标签" prop="tag">
  177. <el-input v-model="form.tag" placeholder="请输入标签" />
  178. </el-form-item>
  179. </el-col>
  180. </el-row>
  181. <el-row>
  182. <el-col :span="12">
  183. <el-form-item label="状态" prop="status">
  184. <el-radio-group v-model="form.status">
  185. <el-radio :label="1">启用</el-radio>
  186. <el-radio :label="0">禁用</el-radio>
  187. </el-radio-group>
  188. </el-form-item>
  189. </el-col>
  190. <el-col :span="12">
  191. <el-form-item label="默认选中" prop="isDefault">
  192. <el-radio-group v-model="form.isDefault">
  193. <el-radio :label="1">是</el-radio>
  194. <el-radio :label="0">否</el-radio>
  195. </el-radio-group>
  196. </el-form-item>
  197. </el-col>
  198. </el-row>
  199. <el-row>
  200. <el-col :span="24">
  201. <el-form-item label="有效期" prop="validTime">
  202. <el-date-picker
  203. v-model="validTime"
  204. type="datetimerange"
  205. range-separator="至"
  206. start-placeholder="开始日期"
  207. end-placeholder="结束日期"
  208. value-format="yyyy-MM-dd HH:mm:ss"
  209. format="yyyy-MM-dd HH:mm:ss"
  210. ></el-date-picker>
  211. </el-form-item>
  212. </el-col>
  213. </el-row>
  214. <el-row>
  215. <el-col :span="24">
  216. <el-form-item label="图标" prop="iconUrl">
  217. <el-upload
  218. class="avatar-uploader"
  219. :action="uploadFileUrl"
  220. :headers="headers"
  221. :show-file-list="false"
  222. :on-success="handleIconSuccess"
  223. :before-upload="beforeIconUpload"
  224. :on-error="handleIconError"
  225. >
  226. <img v-if="form.iconUrl" :src="form.iconUrl" class="avatar" />
  227. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  228. </el-upload>
  229. <div class="el-upload__tip">
  230. <div v-if="form.iconUrl" style="margin-top: 10px;">
  231. <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleIconRemove">移除</el-button>
  232. </div>
  233. <div style="color: #999; font-size: 12px; margin-top: 5px;">
  234. 只能上传jpg/png文件,且不超过2MB
  235. </div>
  236. </div>
  237. </el-form-item>
  238. </el-col>
  239. </el-row>
  240. <el-row>
  241. <el-col :span="24">
  242. <el-form-item label="详情链接" prop="detailUrl">
  243. <el-input v-model="form.detailUrl" placeholder="请输入详情链接" />
  244. </el-form-item>
  245. </el-col>
  246. </el-row>
  247. <el-row>
  248. <el-col :span="24">
  249. <el-form-item label="描述文案" prop="shortDesc">
  250. <el-input v-model="form.shortDesc" type="textarea" placeholder="请输入描述文案" />
  251. </el-form-item>
  252. </el-col>
  253. </el-row>
  254. <el-row>
  255. <el-col :span="24">
  256. <el-form-item label="权益详情" prop="benefitDetails">
  257. <el-input v-model="form.benefitDetails" type="textarea" placeholder="请输入权益详情" />
  258. </el-form-item>
  259. </el-col>
  260. </el-row>
  261. <el-row>
  262. <el-col :span="24">
  263. <el-form-item label="绑定优惠券" prop="couponIdList">
  264. <el-select
  265. v-model="form.couponIdList"
  266. multiple
  267. filterable
  268. remote
  269. reserve-keyword
  270. placeholder="请输入优惠券名称搜索"
  271. :remote-method="remoteCouponSearch"
  272. :loading="couponLoading"
  273. style="width: 100%"
  274. @focus="handleCouponFocus"
  275. @clear="handleCouponClear"
  276. clearable
  277. >
  278. <el-option
  279. v-for="item in couponOptions"
  280. :key="item.couponId"
  281. :label="`${item.title}(¥${item.limitTime})`"
  282. :value="item.couponId"
  283. >
  284. <span>{{ item.title }}</span>
  285. <span style="float: right; color: #8492a6; font-size: 13px">
  286. ¥{{ item.price }}
  287. </span>
  288. </el-option>
  289. </el-select>
  290. <div class="el-form-item__tip" style="margin-top: 5px; color: #999; font-size: 12px;">
  291. 已选择 {{ form.couponIdList ? form.couponIdList.length : 0 }} 张优惠券
  292. </div>
  293. </el-form-item>
  294. </el-col>
  295. </el-row>
  296. </el-form>
  297. <div slot="footer" class="dialog-footer">
  298. <el-button type="primary" @click="submitForm">确 定</el-button>
  299. <el-button @click="cancel">取 消</el-button>
  300. </div>
  301. </el-dialog>
  302. <!-- 详情对话框 -->
  303. <el-dialog title="充值模板详情" :visible.sync="detailOpen" width="800px" append-to-body>
  304. <el-descriptions :column="2" border>
  305. <el-descriptions-item label="模板名称">{{ detailData.templateName }}</el-descriptions-item>
  306. <el-descriptions-item label="储值金额">¥{{ detailData.rechargeAmount }}</el-descriptions-item>
  307. ```vue
  308. <el-descriptions-item label="赠送金额">¥{{ detailData.bonusAmount || 0 }}</el-descriptions-item>
  309. <el-descriptions-item label="限购次数">{{ detailData.purchaseLimit || '不限制' }}</el-descriptions-item>
  310. <el-descriptions-item label="排序序号">{{ detailData.sortOrder }}</el-descriptions-item>
  311. <el-descriptions-item label="标签">{{ detailData.tag || '无' }}</el-descriptions-item>
  312. <el-descriptions-item label="状态">
  313. <el-tag :type="detailData.status === 1 ? 'success' : 'danger'">
  314. {{ detailData.status === 1 ? '启用' : '禁用' }}
  315. </el-tag>
  316. </el-descriptions-item>
  317. <el-descriptions-item label="默认选中">
  318. <el-tag :type="detailData.isDefault === 1 ? 'success' : 'info'">
  319. {{ detailData.isDefault === 1 ? '是' : '否' }}
  320. </el-tag>
  321. </el-descriptions-item>
  322. <el-descriptions-item label="有效期" :span="2">
  323. <span v-if="detailData.startTime && detailData.endTime">
  324. {{ parseTime(detailData.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }} 至 {{ parseTime(detailData.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
  325. </span>
  326. <span v-else>永久有效</span>
  327. </el-descriptions-item>
  328. <el-descriptions-item label="图标" :span="2">
  329. <img v-if="detailData.iconUrl" :src="getImageUrl(detailData.iconUrl)" style="max-height: 100px;" />
  330. <span v-else>无</span>
  331. </el-descriptions-item>
  332. <el-descriptions-item label="详情链接" :span="2">{{ detailData.detailUrl || '无' }}</el-descriptions-item>
  333. <el-descriptions-item label="描述文案" :span="2">{{ detailData.shortDesc || '无' }}</el-descriptions-item>
  334. <el-descriptions-item label="权益详情" :span="2">{{ detailData.benefitDetails || '无' }}</el-descriptions-item>
  335. <el-descriptions-item label="绑定优惠券" :span="2">
  336. <div v-if="detailData.coupons && detailData.coupons.length > 0">
  337. <el-tag v-for="(coupon, index) in detailData.coupons" :key="index" type="success" style="margin-right: 5px; margin-bottom: 5px;">
  338. {{ coupon.title }} (¥{{ coupon.price }})
  339. </el-tag>
  340. </div>
  341. <span v-else>无</span>
  342. </el-descriptions-item>
  343. </el-descriptions>
  344. <div slot="footer" class="dialog-footer">
  345. <el-button @click="detailOpen = false">关 闭</el-button>
  346. </div>
  347. </el-dialog>
  348. </div>
  349. </template>
  350. <script>
  351. import {
  352. listRechargeTemplate,
  353. getRechargeTemplate,
  354. delRechargeTemplate,
  355. addRechargeTemplate,
  356. updateRechargeTemplate,
  357. updateRechargeTemplateStatus,
  358. getCouponList
  359. } from "@/api/recharge/template";
  360. import { getToken } from "@/utils/auth";
  361. import { parseTime } from "@/utils/common";
  362. export default {
  363. name: "RechargeTemplate",
  364. data() {
  365. return {
  366. // 遮罩层
  367. loading: true,
  368. // 选中数组
  369. ids: [],
  370. // 非单个禁用
  371. single: true,
  372. // 非多个禁用
  373. multiple: true,
  374. // 显示搜索条件
  375. showSearch: true,
  376. // 总条数
  377. total: 0,
  378. // 充值模板表格数据
  379. templateList: [],
  380. // 弹出层标题
  381. title: "",
  382. // 是否显示弹出层
  383. open: false,
  384. // 是否显示详情弹出层
  385. detailOpen: false,
  386. // 详情数据
  387. detailData: {},
  388. // 有效期时间范围
  389. validTime: [],
  390. // 查询参数
  391. queryParams: {
  392. pageNum: 1,
  393. pageSize: 10,
  394. templateName: null,
  395. status: null,
  396. tag: null
  397. },
  398. // 表单参数
  399. form: {},
  400. // 表单校验
  401. rules: {
  402. templateName: [
  403. { required: true, message: "模板名称不能为空", trigger: "blur" }
  404. ],
  405. rechargeAmount: [
  406. { required: true, message: "储值金额不能为空", trigger: "blur" }
  407. ],
  408. status: [
  409. { required: true, message: "状态不能为空", trigger: "change" }
  410. ],
  411. sortOrder: [
  412. { required: true, message: "排序序号不能为空", trigger: "blur" }
  413. ]
  414. },
  415. // 上传参数
  416. uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
  417. headers: {
  418. Authorization: "Bearer " + getToken()
  419. },
  420. // 优惠券相关
  421. couponOptions: [],
  422. couponLoading: false,
  423. couponSearchTimer: null // 搜索防抖定时器
  424. };
  425. },
  426. created() {
  427. this.getList();
  428. },
  429. beforeDestroy() {
  430. // 组件销毁时清理定时器
  431. if (this.couponSearchTimer) {
  432. clearTimeout(this.couponSearchTimer);
  433. }
  434. },
  435. methods: {
  436. parseTime,
  437. handleIconRemove(){
  438. this.form.iconUrl = null;
  439. },
  440. /** 查询充值模板列表 */
  441. getList() {
  442. this.loading = true;
  443. listRechargeTemplate(this.queryParams).then(response => {
  444. this.templateList = response.rows;
  445. this.total = response.total;
  446. this.loading = false;
  447. });
  448. },
  449. /** 搜索按钮操作 */
  450. handleQuery() {
  451. this.queryParams.pageNum = 1;
  452. this.getList();
  453. },
  454. /** 重置按钮操作 */
  455. resetQuery() {
  456. this.resetForm("queryForm");
  457. this.handleQuery();
  458. },
  459. // 多选框选中数据
  460. handleSelectionChange(selection) {
  461. this.ids = selection.map(item => item.id);
  462. this.single = selection.length !== 1;
  463. this.multiple = !selection.length;
  464. },
  465. /** 新增按钮操作 */
  466. handleAdd() {
  467. this.reset();
  468. this.loadDefaultCoupons(); // 新增时加载默认选项
  469. this.open = true;
  470. this.title = "添加充值模板";
  471. },
  472. /** 修改按钮操作 */
  473. handleUpdate(row) {
  474. this.reset();
  475. const id = row.id || this.ids;
  476. getRechargeTemplate(id).then(response => {
  477. this.form = response.data;
  478. // 设置有效期时间范围
  479. if (this.form.startTime && this.form.endTime) {
  480. this.validTime = [this.form.startTime, this.form.endTime];
  481. }
  482. // 如果已有绑定的优惠券,加载到选项中
  483. if (this.form.couponIdList && this.form.couponIdList.length > 0) {
  484. this.loadSelectedCoupons(this.form.couponIdList);
  485. } else {
  486. // 没有已选择的优惠券时,加载默认选项
  487. this.loadDefaultCoupons();
  488. }
  489. this.open = true;
  490. this.title = "修改充值模板";
  491. });
  492. },
  493. /** 详情按钮操作 */
  494. handleDetail(row) {
  495. const id = row.id;
  496. getRechargeTemplate(id).then(response => {
  497. this.detailData = response.data;
  498. this.detailOpen = true;
  499. });
  500. },
  501. /** 提交按钮 */
  502. submitForm() {
  503. this.$refs["form"].validate(valid => {
  504. if (valid) {
  505. // 处理有效期时间
  506. if (this.validTime && this.validTime.length === 2) {
  507. this.form.startTime = this.validTime[0];
  508. this.form.endTime = this.validTime[1];
  509. } else {
  510. this.form.startTime = null;
  511. this.form.endTime = null;
  512. }
  513. if (this.form.id != null) {
  514. updateRechargeTemplate(this.form).then(response => {
  515. this.$message.success("修改成功");
  516. this.open = false;
  517. this.getList();
  518. });
  519. } else {
  520. addRechargeTemplate(this.form).then(response => {
  521. this.$message.success("新增成功");
  522. this.open = false;
  523. this.getList();
  524. });
  525. }
  526. }
  527. });
  528. },
  529. /** 删除按钮操作 */
  530. handleDelete(row) {
  531. const ids = row.id || this.ids;
  532. this.$confirm('是否确认删除充值模板编号为"' + ids + '"的数据项?', '提示', {
  533. confirmButtonText: '确定',
  534. cancelButtonText: '取消',
  535. type: 'warning'
  536. }).then(() => {
  537. return delRechargeTemplate(ids);
  538. }).then(() => {
  539. this.getList();
  540. this.$message.success("删除成功");
  541. }).catch(() => {});
  542. },
  543. /** 状态修改 */
  544. handleStatusChange(row) {
  545. let text = row.status === 1 ? "启用" : "停用";
  546. this.$confirm('确认要"' + text + '""' + row.templateName + '"模板吗?').then(() => {
  547. return updateRechargeTemplateStatus(row.id, row.status);
  548. }).then(() => {
  549. this.$message.success(text + "成功");
  550. }).catch(() => {
  551. row.status = row.status === 0 ? 1 : 0;
  552. });
  553. },
  554. // 取消按钮
  555. cancel() {
  556. this.open = false;
  557. this.reset();
  558. },
  559. // 表单重置
  560. reset() {
  561. // 清理搜索定时器
  562. if (this.couponSearchTimer) {
  563. clearTimeout(this.couponSearchTimer);
  564. this.couponSearchTimer = null;
  565. }
  566. this.form = {
  567. id: null,
  568. templateName: null,
  569. rechargeAmount: null,
  570. bonusAmount: null,
  571. benefitDetails: null,
  572. status: 1,
  573. sortOrder: 0,
  574. startTime: null,
  575. endTime: null,
  576. userType: null,
  577. purchaseLimit: null,
  578. tag: null,
  579. shortDesc: null,
  580. detailUrl: null,
  581. iconUrl: null,
  582. isDefault: 0,
  583. couponIdList: []
  584. };
  585. this.validTime = [];
  586. this.resetForm("form");
  587. this.couponOptions = [];
  588. },
  589. // 优惠券下拉框获得焦点时
  590. handleCouponFocus() {
  591. // 如果没有选项且没有已选择的优惠券,加载默认选项
  592. if (this.couponOptions.length === 0 && (!this.form.couponIdList || this.form.couponIdList.length === 0)) {
  593. this.loadDefaultCoupons();
  594. }
  595. },
  596. // 清空优惠券选择
  597. handleCouponClear() {
  598. this.form.couponIdList = [];
  599. },
  600. // 加载默认优惠券列表
  601. loadDefaultCoupons() {
  602. this.couponLoading = true;
  603. getCouponList({
  604. pageSize: 20,
  605. pageNum: 1,
  606. status: 1 // 只查询有效的优惠券
  607. }).then(response => {
  608. this.couponOptions = response.data.list || [];
  609. this.couponLoading = false;
  610. }).catch(() => {
  611. this.couponLoading = false;
  612. this.couponOptions = [];
  613. });
  614. },
  615. // 远程搜索优惠券(添加防抖)
  616. remoteCouponSearch(query) {
  617. // 清除之前的定时器
  618. if (this.couponSearchTimer) {
  619. clearTimeout(this.couponSearchTimer);
  620. }
  621. // 设置新的定时器,300ms后执行搜索
  622. this.couponSearchTimer = setTimeout(() => {
  623. if (query && query.trim() !== '') {
  624. this.couponLoading = true;
  625. getCouponList({
  626. couponName: query.trim(),
  627. pageSize: 50, // 增加搜索结果数量
  628. pageNum: 1,
  629. status: 1 // 只查询有效的优惠券
  630. }).then(response => {
  631. this.couponOptions = response.data.list || [];
  632. this.couponLoading = false;
  633. }).catch(() => {
  634. this.couponLoading = false;
  635. this.couponOptions = [];
  636. this.$message.error('搜索优惠券失败');
  637. });
  638. } else {
  639. // 如果搜索为空,加载默认选项
  640. this.loadDefaultCoupons();
  641. }
  642. }, 300);
  643. },
  644. // 加载已选择的优惠券
  645. loadSelectedCoupons(couponIds) {
  646. if (couponIds && couponIds.length > 0) {
  647. this.couponLoading = true;
  648. getCouponList({
  649. couponIds: couponIds.join(',')
  650. }).then(response => {
  651. // 合并已选择的优惠券到选项中,避免重复
  652. const selectedCoupons = response.data.list || [];
  653. const existingIds = this.couponOptions.map(item => item.couponId);
  654. selectedCoupons.forEach(coupon => {
  655. if (!existingIds.includes(coupon.couponId)) {
  656. this.couponOptions.push(coupon);
  657. }
  658. });
  659. this.couponLoading = false;
  660. }).catch(() => {
  661. this.couponLoading = false;
  662. });
  663. }
  664. },
  665. // 获取完整的图片URL
  666. getImageUrl(url) {
  667. if (!url) return '';
  668. // 如果已经是完整URL,直接返回
  669. if (url.startsWith('http://') || url.startsWith('https://')) {
  670. return url;
  671. }
  672. // 如果是相对路径,拼接基础URL
  673. return process.env.VUE_APP_BASE_API + url;
  674. },
  675. // 图标上传前校验
  676. beforeIconUpload(file) {
  677. const isJPG = file.type === 'image/jpeg';
  678. const isPNG = file.type === 'image/png';
  679. const isGIF = file.type === 'image/gif';
  680. const isWebP = file.type === 'image/webp';
  681. const isLt2M = file.size / 1024 /1024 < 2; // 限制大小为2MB
  682. if (!isJPG && !isPNG && !isGIF && !isWebP) {
  683. this.$message.error('上传头像图片只能是 JPG、PNG、GIF 或 WebP 格式!');
  684. }
  685. if (!isLt2M) {
  686. this.$message.error('上传头像图片大小不能超过 2MB!');
  687. }
  688. return (isJPG || isPNG || isGIF || isWebP) && isLt2M;
  689. },
  690. // 图标上传成功后处理
  691. handleIconUploadSuccess(response, file) {
  692. if (response && response.url) {
  693. this.form.iconUrl = response.url; // 赋值上传后的图片URL
  694. } else {
  695. this.$message.error('图标上传失败,请重试');
  696. }
  697. },
  698. // 图标上传失败处理
  699. handleIconUploadError() {
  700. this.$message.error('图标上传失败,请重试');
  701. },
  702. handleIconSuccess(response, file) {
  703. // 处理图标上传成功的逻辑
  704. this.form.iconUrl = response.url; // 假设返回的数据结构有URL
  705. this.$message.success('图标上传成功!');
  706. },
  707. handleIconError(err, file) {
  708. // 处理图标上传失败的逻辑
  709. this.$message.error('图标上传失败,请重试.');
  710. },
  711. }
  712. };
  713. </script>
  714. <style scoped>
  715. .dialog-footer {
  716. text-align: right;
  717. }
  718. </style>