LimitPurchaseRule.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <template>
  2. <div class="limit-purchase-rule-container">
  3. <!-- 规则列表区域 -->
  4. <el-card class="rule-list-card">
  5. <template #header>
  6. <div class="card-header">
  7. <span>限购规则列表</span>
  8. <el-button type="primary" size="small" @click="handleCreateRule">
  9. 新建规则
  10. </el-button>
  11. </div>
  12. </template>
  13. <el-table
  14. v-loading="loading.list"
  15. :data="ruleList"
  16. border
  17. style="width: 100%"
  18. >
  19. <el-table-column prop="id" label="ID" width="80" />
  20. <el-table-column prop="name" label="规则名称" min-width="160" />
  21. <el-table-column label="限购地区" min-width="260">
  22. <template #default="scope">
  23. <div v-if="scope.row.regions && scope.row.regions.length">
  24. <el-tag
  25. v-for="(region, index) in scope.row.regions"
  26. :key="index"
  27. type="danger"
  28. size="small"
  29. class="region-tag"
  30. >
  31. {{ formatRegionLabel(region) }}
  32. </el-tag>
  33. </div>
  34. <span v-else class="text-muted">未设置</span>
  35. </template>
  36. </el-table-column>
  37. <el-table-column prop="is_default" label="默认规则" width="100">
  38. <template #default="scope">
  39. <el-tag :type="scope.row.is_default ? 'success' : 'info'" size="small">
  40. {{ scope.row.is_default ? '是' : '否' }}
  41. </el-tag>
  42. </template>
  43. </el-table-column>
  44. <el-table-column prop="status" label="状态" width="100">
  45. <template #default="scope">
  46. <el-tag :type="scope.row.status ? 'success' : 'info'" size="small">
  47. {{ scope.row.status ? '启用' : '停用' }}
  48. </el-tag>
  49. </template>
  50. </el-table-column>
  51. <el-table-column label="操作" width="200" fixed="right">
  52. <template #default="scope">
  53. <el-button type="primary" link size="small" @click="handleEditRule(scope.row)">
  54. 编辑
  55. </el-button>
  56. <el-button type="danger" link size="small" @click="handleDeleteRule(scope.row)">
  57. 删除
  58. </el-button>
  59. </template>
  60. </el-table-column>
  61. </el-table>
  62. <!-- 分页组件 -->
  63. <el-pagination
  64. v-if="pagination.total > 0"
  65. layout="prev, pager, next"
  66. :current-page="pagination.page"
  67. :page-size="pagination.pageSize"
  68. :total="pagination.total"
  69. @current-change="handlePageChange"
  70. style="margin-top: 20px; text-align: right;"
  71. />
  72. </el-card>
  73. <!-- 规则表单弹窗:新建 / 编辑规则基本信息 -->
  74. <el-dialog
  75. :title="ruleForm.id ? '编辑限购规则' : '新建限购规则'"
  76. v-model="ruleDialog.visible"
  77. width="720px"
  78. :close-on-click-modal="false"
  79. >
  80. <el-form
  81. ref="ruleFormRef"
  82. :model="ruleForm"
  83. :rules="ruleRules"
  84. label-width="90px"
  85. >
  86. <el-form-item label="规则名称" prop="name">
  87. <el-input v-model="ruleForm.name" maxlength="64" show-word-limit />
  88. </el-form-item>
  89. <el-form-item label="是否默认" prop="is_default">
  90. <el-switch
  91. v-model="ruleForm.is_default"
  92. :active-value="true"
  93. :inactive-value="false"
  94. />
  95. <div class="form-tip">设置默认后未设置限购规则的商品都使用该规则</div>
  96. </el-form-item>
  97. <el-form-item label="状态" prop="status">
  98. <el-switch
  99. v-model="ruleForm.status"
  100. :active-value="true"
  101. :inactive-value="false"
  102. active-text="启用"
  103. inactive-text="停用"
  104. />
  105. </el-form-item>
  106. <!-- 在新建 / 编辑规则里直接设置限购地区(本地省市区 JSON 级联) -->
  107. <el-form-item label="限购地区">
  108. <div class="region-selector-wrapper">
  109. <el-form label-width="60px" class="region-form">
  110. <el-row :gutter="20" align="middle">
  111. <el-col :span="8">
  112. <el-form-item label="省份">
  113. <el-select
  114. v-model="selectedProvince"
  115. placeholder="请选择省份"
  116. filterable
  117. clearable
  118. @change="onProvinceChange"
  119. >
  120. <el-option
  121. v-for="p in provinces"
  122. :key="p.code"
  123. :label="p.name"
  124. :value="p.name"
  125. />
  126. </el-select>
  127. </el-form-item>
  128. </el-col>
  129. <el-col :span="8">
  130. <el-form-item label="城市">
  131. <el-select
  132. v-model="selectedCity"
  133. placeholder="请选择城市"
  134. :disabled="!selectedProvince"
  135. filterable
  136. clearable
  137. @change="onCityChange"
  138. >
  139. <el-option
  140. v-for="c in cities"
  141. :key="c.code"
  142. :label="c.name"
  143. :value="c.name"
  144. />
  145. </el-select>
  146. </el-form-item>
  147. </el-col>
  148. <el-col :span="8">
  149. <el-form-item label="区县">
  150. <el-select
  151. v-model="selectedArea"
  152. placeholder="请选择区县"
  153. :disabled="!selectedCity"
  154. filterable
  155. clearable
  156. >
  157. <el-option
  158. v-for="a in areas"
  159. :key="a.code"
  160. :label="a.name"
  161. :value="a.name"
  162. />
  163. </el-select>
  164. </el-form-item>
  165. </el-col>
  166. </el-row>
  167. <el-row :gutter="20">
  168. <el-col :span="24" class="text-right" style="margin-top: 10px;">
  169. <el-button type="primary" @click="addRegion">
  170. 添加限购地区
  171. </el-button>
  172. </el-col>
  173. </el-row>
  174. </el-form>
  175. <div class="region-tip">注:可只选择到省或市层级</div>
  176. <div class="limited-region-list">
  177. <div class="list-title">已设置的限购地区</div>
  178. <div v-if="currentRuleRegions.length === 0" class="empty-tip">
  179. 暂未添加限购地区
  180. </div>
  181. <div
  182. v-for="(area, index) in currentRuleRegions"
  183. :key="index"
  184. class="limited-area-item"
  185. >
  186. <span>{{ formatRegionLabel(area) }}</span>
  187. <el-button
  188. type="danger"
  189. link
  190. @click="removeRegion(index)"
  191. >
  192. 删除
  193. </el-button>
  194. </div>
  195. </div>
  196. </div>
  197. </el-form-item>
  198. </el-form>
  199. <template #footer>
  200. <span class="dialog-footer">
  201. <el-button @click="ruleDialog.visible = false">取 消</el-button>
  202. <el-button type="primary" :loading="loading.saveRule" @click="submitRuleForm">
  203. 确 认
  204. </el-button>
  205. </span>
  206. </template>
  207. </el-dialog>
  208. </div>
  209. </template>
  210. <script>
  211. import request from '@/utils/request'
  212. export default {
  213. name: 'LimitPurchaseRule',
  214. data() {
  215. return {
  216. // 列表数据
  217. ruleList: [],
  218. loading: {
  219. list: false,
  220. saveRule: false
  221. },
  222. // 分页数据
  223. pagination: {
  224. page: 1,
  225. pageSize: 15,
  226. total: 0,
  227. hasMore: false
  228. },
  229. // 规则表单
  230. ruleDialog: {
  231. visible: false
  232. },
  233. ruleForm: {
  234. id: null,
  235. name: '',
  236. is_default: 0,
  237. status: 1
  238. },
  239. ruleRules: {
  240. name: [
  241. { required: true, message: '请输入规则名称', trigger: 'blur' }
  242. ]
  243. },
  244. // 使用接口获取省市区数据
  245. provinces: [],
  246. cities: [],
  247. areas: [],
  248. selectedProvince: '',
  249. selectedCity: '',
  250. selectedArea: '',
  251. currentRuleRegions: []
  252. }
  253. },
  254. created() {
  255. this.fetchRuleList()
  256. this.fetchProvinces()
  257. },
  258. methods: {
  259. // 获取省份数据
  260. async fetchProvinces() {
  261. try {
  262. const response = await request.get('/limit_purchase/region/province')
  263. this.provinces = response.data || []
  264. } catch (e) {
  265. console.error('获取省份数据失败:', e)
  266. this.$message.error('获取省份数据失败')
  267. }
  268. },
  269. // ========== 列表相关 ==========
  270. async fetchRuleList() {
  271. this.loading.list = true
  272. try {
  273. const { page } = await request.get('/limit_purchase/rule', {
  274. params: {
  275. page: this.pagination.page,
  276. limit: this.pagination.pageSize
  277. }
  278. })
  279. // 根据分页数据结构处理
  280. if (page) {
  281. this.ruleList = Array.isArray(page.data) ? page.data : []
  282. this.pagination.total = page.total || 0
  283. this.pagination.hasMore = page.has_more || false
  284. } else {
  285. // 兼容旧的数据结构
  286. this.ruleList = []
  287. this.pagination.total = 0
  288. this.pagination.hasMore = false
  289. }
  290. } catch (e) {
  291. console.error(e)
  292. this.$message.error('加载规则列表失败')
  293. } finally {
  294. this.loading.list = false
  295. }
  296. },
  297. handlePageChange(page) {
  298. this.pagination.page = page
  299. this.fetchRuleList()
  300. },
  301. handleCreateRule() {
  302. this.ruleForm = {
  303. id: null,
  304. name: '',
  305. is_default: false,
  306. status: true
  307. }
  308. // 新建规则时清空当前规则地区
  309. this.currentRuleRegions = []
  310. this.selectedProvince = ''
  311. this.selectedCity = ''
  312. this.selectedArea = ''
  313. this.cities = []
  314. this.areas = []
  315. this.ruleDialog.visible = true
  316. this.$nextTick(() => {
  317. if (this.$refs.ruleFormRef && typeof this.$refs.ruleFormRef.clearValidate === 'function') {
  318. this.$refs.ruleFormRef.clearValidate()
  319. }
  320. })
  321. },
  322. handleEditRule(row) {
  323. this.ruleForm = {
  324. id: row.id,
  325. name: row.name,
  326. is_default: row.is_default || false,
  327. status: row.status
  328. }
  329. // 编辑规则时加载该规则已有的地区
  330. this.currentRuleRegions = Array.isArray(row.regions) ? [...row.regions] : []
  331. this.selectedProvince = ''
  332. this.selectedCity = ''
  333. this.selectedArea = ''
  334. this.cities = []
  335. this.areas = []
  336. this.ruleDialog.visible = true
  337. this.$nextTick(() => {
  338. if (this.$refs.ruleFormRef && typeof this.$refs.ruleFormRef.clearValidate === 'function') {
  339. this.$refs.ruleFormRef.clearValidate()
  340. }
  341. })
  342. },
  343. submitRuleForm() {
  344. this.$refs.ruleFormRef.validate(async (valid) => {
  345. if (!valid) return
  346. this.loading.saveRule = true
  347. try {
  348. if (this.ruleForm.id) {
  349. // 更新:名称 + 是否默认 + 状态 + 限购地区
  350. await request.put(`/limit_purchase/rule/${this.ruleForm.id}`, {
  351. name: this.ruleForm.name,
  352. is_default: this.ruleForm.is_default,
  353. status: this.ruleForm.status,
  354. regions: this.currentRuleRegions
  355. })
  356. this.$message.success('更新成功')
  357. } else {
  358. // 创建:名称 + 是否默认 + 状态 + 限购地区
  359. await request.post('/limit_purchase/rule', {
  360. name: this.ruleForm.name,
  361. is_default: this.ruleForm.is_default,
  362. status: this.ruleForm.status,
  363. regions: this.currentRuleRegions
  364. })
  365. this.$message.success('创建成功')
  366. }
  367. this.ruleDialog.visible = false
  368. this.fetchRuleList()
  369. } catch (e) {
  370. console.error(e)
  371. this.$message.error('保存失败')
  372. } finally {
  373. this.loading.saveRule = false
  374. }
  375. })
  376. },
  377. handleDeleteRule(row) {
  378. this.$confirm(`确认删除规则「${row.name}」吗?`, '提示', {
  379. type: 'warning'
  380. })
  381. .then(async () => {
  382. try {
  383. const {code, msg} = await request.delete(`/limit_purchase/rule/${row.id}`)
  384. if (code === 0 || code === 200) {
  385. this.$message.success('删除成功')
  386. this.fetchRuleList()
  387. } else {
  388. this.$message.error(msg || '删除失败')
  389. }
  390. } catch (e) {
  391. console.error(e)
  392. this.$message.error('删除失败')
  393. }
  394. })
  395. .catch(() => {})
  396. },
  397. // 从网络接口获取城市数据
  398. async onProvinceChange() {
  399. this.selectedCity = ''
  400. this.selectedArea = ''
  401. this.areas = []
  402. this.cities = []
  403. if (!this.selectedProvince) return
  404. try {
  405. const province = this.provinces.find(p => p.name === this.selectedProvince)
  406. if (province) {
  407. // 获取城市数据
  408. const response = await request.get(`/limit_purchase/region/city/${province.code}`)
  409. this.cities = response.data || []
  410. }
  411. } catch (e) {
  412. console.error('获取城市数据失败:', e)
  413. this.$message.error('获取城市数据失败')
  414. }
  415. },
  416. // 从网络接口获取区县数据
  417. async onCityChange() {
  418. this.selectedArea = ''
  419. this.areas = []
  420. if (!this.selectedCity || !this.selectedProvince) return
  421. try {
  422. const province = this.provinces.find(p => p.name === this.selectedProvince)
  423. if (province) {
  424. const city = this.cities.find(c => c.name === this.selectedCity)
  425. if (city) {
  426. const response = await request.get(`/limit_purchase/region/area/${city.code}`)
  427. this.areas = response.data || []
  428. }
  429. }
  430. } catch (e) {
  431. console.error('获取区县数据失败:', e)
  432. this.$message.error('获取区县数据失败')
  433. }
  434. },
  435. addRegion() {
  436. if (!this.selectedProvince) {
  437. this.$message.warning('请至少选择省份')
  438. return
  439. }
  440. // 查找选中的省市区对象
  441. const provinceObj = this.provinces.find(p => p.name === this.selectedProvince)
  442. const cityObj = this.cities.find(c => c.name === this.selectedCity) || null
  443. const areaObj = this.areas.find(a => a.name === this.selectedArea) || null
  444. let region = {
  445. province_id: provinceObj ? provinceObj.code : null,
  446. province_name: this.selectedProvince,
  447. city_id: cityObj ? cityObj.code : null,
  448. city_name: this.selectedCity || null,
  449. area_id: areaObj ? areaObj.code : null,
  450. area_name: this.selectedArea || null
  451. };
  452. // 去重:检查是否已经存在相同或更广范围的地区
  453. const exists = this.currentRuleRegions.some(item => {
  454. // 完全匹配检查
  455. if (item.province_name !== region.province_name) return false;
  456. // 如果当前添加的是省级规则
  457. if (!region.city_name) {
  458. return !item.city_name; // 匹配已有省级规则
  459. }
  460. // 如果当前添加的是市级规则
  461. if (region.city_name && !region.area_name) {
  462. return item.city_name === region.city_name && !item.area_name;
  463. }
  464. // 如果当前添加的是区级规则
  465. if (region.city_name && region.area_name) {
  466. return item.city_name === region.city_name && item.area_name === region.area_name;
  467. }
  468. return false;
  469. });
  470. if (exists) {
  471. this.$message.warning('该地区已在列表中')
  472. return
  473. }
  474. this.currentRuleRegions.push(region)
  475. // 清空已选项
  476. this.selectedProvince = ''
  477. this.selectedCity = ''
  478. this.selectedArea = ''
  479. this.cities = []
  480. this.areas = []
  481. },
  482. removeRegion(index) {
  483. this.currentRuleRegions.splice(index, 1)
  484. },
  485. // ========== 工具方法 ==========
  486. formatRegionLabel(region) {
  487. const parts = []
  488. if (region.province_name) parts.push(region.province_name)
  489. if (region.city_name) parts.push(region.city_name)
  490. if (region.area_name) parts.push(region.area_name)
  491. return parts.join(' - ') || '未知地区'
  492. }
  493. }
  494. }
  495. </script>
  496. <style scoped>
  497. .limit-purchase-rule-container {
  498. padding: 0;
  499. }
  500. .rule-list-card {
  501. margin-bottom: 0;
  502. }
  503. .card-header {
  504. display: flex;
  505. justify-content: space-between;
  506. align-items: center;
  507. }
  508. .region-tag {
  509. margin-right: 4px;
  510. margin-bottom: 4px;
  511. }
  512. .text-muted {
  513. color: #999;
  514. }
  515. .region-selector-wrapper {
  516. padding: 10px 0 0;
  517. }
  518. .region-selector-title {
  519. display: flex;
  520. align-items: center;
  521. margin-bottom: 16px;
  522. font-size: 14px;
  523. }
  524. .region-selector-title .text-danger {
  525. color: #f56c6c;
  526. }
  527. .region-form {
  528. margin-bottom: 16px;
  529. }
  530. .limited-region-list {
  531. background: #fff7f7;
  532. border-radius: 4px;
  533. padding: 12px 16px;
  534. }
  535. .inner-region-form {
  536. margin-bottom: 8px;
  537. }
  538. .limited-region-list .list-title {
  539. font-size: 14px;
  540. margin-bottom: 8px;
  541. }
  542. .limited-region-list .empty-tip {
  543. font-size: 13px;
  544. color: #999;
  545. }
  546. .limited-area-item {
  547. display: flex;
  548. justify-content: space-between;
  549. align-items: center;
  550. padding: 8px 0;
  551. border-bottom: 1px solid #f5d1d1;
  552. }
  553. .limited-area-item:last-child {
  554. border-bottom: none;
  555. }
  556. .dialog-footer {
  557. text-align: right;
  558. }
  559. .text-right {
  560. text-align: right;
  561. }
  562. .form-tip {
  563. margin-left: 10px;
  564. font-size: 12px;
  565. color: #999;
  566. }
  567. .region-tip {
  568. font-size: 12px;
  569. color: #999;
  570. margin-top: 5px;
  571. margin-bottom: 15px;
  572. }
  573. </style>