LimitPurchaseRule.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. export default {
  212. name: 'LimitPurchaseRule',
  213. props: {
  214. axiosInstance: {
  215. type: Object,
  216. default: null
  217. }
  218. },
  219. data() {
  220. return {
  221. // 列表数据
  222. ruleList: [],
  223. loading: {
  224. list: false,
  225. saveRule: false
  226. },
  227. // 分页数据
  228. pagination: {
  229. page: 1,
  230. pageSize: 15,
  231. total: 0,
  232. hasMore: false
  233. },
  234. // 规则表单
  235. ruleDialog: {
  236. visible: false
  237. },
  238. ruleForm: {
  239. id: null,
  240. name: '',
  241. is_default: 0,
  242. status: 1
  243. },
  244. ruleRules: {
  245. name: [
  246. { required: true, message: '请输入规则名称', trigger: 'blur' }
  247. ]
  248. },
  249. // 使用接口获取省市区数据
  250. provinces: [],
  251. cities: [],
  252. areas: [],
  253. selectedProvince: '',
  254. selectedCity: '',
  255. selectedArea: '',
  256. currentRuleRegions: []
  257. }
  258. },
  259. created() {
  260. this.fetchRuleList()
  261. this.fetchProvinces()
  262. },
  263. methods: {
  264. http() {
  265. // 默认用全局 axios,如果父组件传入则优先用父组件的
  266. return this.axiosInstance || this.$axios || this.$http
  267. },
  268. // 获取省份数据
  269. async fetchProvinces() {
  270. if (!this.http()) return
  271. try {
  272. const response = await this.http().get('/limit_purchase/region/province')
  273. this.provinces = response.data || []
  274. } catch (e) {
  275. console.error('获取省份数据失败:', e)
  276. this.$message.error('获取省份数据失败')
  277. }
  278. },
  279. // ========== 列表相关 ==========
  280. async fetchRuleList() {
  281. if (!this.http()) return
  282. this.loading.list = true
  283. try {
  284. const { page } = await this.http().get('/limit_purchase/rule', {
  285. params: {
  286. page: this.pagination.page,
  287. limit: this.pagination.pageSize
  288. }
  289. })
  290. // 根据分页数据结构处理
  291. if (page) {
  292. this.ruleList = Array.isArray(page.data) ? page.data : []
  293. this.pagination.total = page.total || 0
  294. this.pagination.hasMore = page.has_more || false
  295. } else {
  296. // 兼容旧的数据结构
  297. this.ruleList = []
  298. this.pagination.total = 0
  299. this.pagination.hasMore = false
  300. }
  301. } catch (e) {
  302. console.error(e)
  303. this.$message.error('加载规则列表失败')
  304. } finally {
  305. this.loading.list = false
  306. }
  307. },
  308. handlePageChange(page) {
  309. this.pagination.page = page
  310. this.fetchRuleList()
  311. },
  312. handleCreateRule() {
  313. this.ruleForm = {
  314. id: null,
  315. name: '',
  316. is_default: false,
  317. status: true
  318. }
  319. // 新建规则时清空当前规则地区
  320. this.currentRuleRegions = []
  321. this.selectedProvince = ''
  322. this.selectedCity = ''
  323. this.selectedArea = ''
  324. this.cities = []
  325. this.areas = []
  326. this.ruleDialog.visible = true
  327. this.$nextTick(() => {
  328. if (this.$refs.ruleFormRef && typeof this.$refs.ruleFormRef.clearValidate === 'function') {
  329. this.$refs.ruleFormRef.clearValidate()
  330. }
  331. })
  332. },
  333. handleEditRule(row) {
  334. this.ruleForm = {
  335. id: row.id,
  336. name: row.name,
  337. is_default: row.is_default || false,
  338. status: row.status
  339. }
  340. // 编辑规则时加载该规则已有的地区
  341. this.currentRuleRegions = Array.isArray(row.regions) ? [...row.regions] : []
  342. this.selectedProvince = ''
  343. this.selectedCity = ''
  344. this.selectedArea = ''
  345. this.cities = []
  346. this.areas = []
  347. this.ruleDialog.visible = true
  348. this.$nextTick(() => {
  349. if (this.$refs.ruleFormRef && typeof this.$refs.ruleFormRef.clearValidate === 'function') {
  350. this.$refs.ruleFormRef.clearValidate()
  351. }
  352. })
  353. },
  354. submitRuleForm() {
  355. this.$refs.ruleFormRef.validate(async (valid) => {
  356. if (!valid || !this.http()) return
  357. this.loading.saveRule = true
  358. try {
  359. if (this.ruleForm.id) {
  360. // 更新:名称 + 是否默认 + 状态 + 限购地区
  361. await this.http().put(`/limit_purchase/rule/${this.ruleForm.id}`, {
  362. name: this.ruleForm.name,
  363. is_default: this.ruleForm.is_default,
  364. status: this.ruleForm.status,
  365. regions: this.currentRuleRegions
  366. })
  367. this.$message.success('更新成功')
  368. } else {
  369. // 创建:名称 + 是否默认 + 状态 + 限购地区
  370. await this.http().post('/limit_purchase/rule', {
  371. name: this.ruleForm.name,
  372. is_default: this.ruleForm.is_default,
  373. status: this.ruleForm.status,
  374. regions: this.currentRuleRegions
  375. })
  376. this.$message.success('创建成功')
  377. }
  378. this.ruleDialog.visible = false
  379. this.fetchRuleList()
  380. } catch (e) {
  381. console.error(e)
  382. this.$message.error('保存失败')
  383. } finally {
  384. this.loading.saveRule = false
  385. }
  386. })
  387. },
  388. handleDeleteRule(row) {
  389. if (!this.http()) return
  390. this.$confirm(`确认删除规则「${row.name}」吗?`, '提示', {
  391. type: 'warning'
  392. })
  393. .then(async () => {
  394. try {
  395. const {code,msg} = await this.http().delete(`/limit_purchase/rule/${row.id}`)
  396. if (code === 0 || code === 200) {
  397. this.$message.success('删除成功')
  398. this.fetchRuleList()
  399. } else {
  400. this.$message.error(msg || '删除失败')
  401. }
  402. } catch (e) {
  403. console.error(e)
  404. this.$message.error('删除失败')
  405. }
  406. })
  407. .catch(() => {})
  408. },
  409. // 从网络接口获取城市数据
  410. async onProvinceChange() {
  411. this.selectedCity = ''
  412. this.selectedArea = ''
  413. this.areas = []
  414. this.cities = []
  415. if (!this.selectedProvince) return
  416. try {
  417. const province = this.provinces.find(p => p.name === this.selectedProvince)
  418. if (province) {
  419. // 获取城市数据
  420. const response = await this.http().get(`/limit_purchase/region/city/${province.code}`)
  421. this.cities = response.data || []
  422. }
  423. } catch (e) {
  424. console.error('获取城市数据失败:', e)
  425. this.$message.error('获取城市数据失败')
  426. }
  427. },
  428. // 从网络接口获取区县数据
  429. async onCityChange() {
  430. this.selectedArea = ''
  431. this.areas = []
  432. if (!this.selectedCity || !this.selectedProvince) return
  433. try {
  434. const province = this.provinces.find(p => p.name === this.selectedProvince)
  435. if (province) {
  436. const city = this.cities.find(c => c.name === this.selectedCity)
  437. if (city) {
  438. const response = await this.http().get(`/limit_purchase/region/area/${city.code}`)
  439. this.areas = response.data || []
  440. }
  441. }
  442. } catch (e) {
  443. console.error('获取区县数据失败:', e)
  444. this.$message.error('获取区县数据失败')
  445. }
  446. },
  447. addRegion() {
  448. if (!this.selectedProvince) {
  449. this.$message.warning('请至少选择省份')
  450. return
  451. }
  452. // 查找选中的省市区对象
  453. const provinceObj = this.provinces.find(p => p.name === this.selectedProvince)
  454. const cityObj = this.cities.find(c => c.name === this.selectedCity) || null
  455. const areaObj = this.areas.find(a => a.name === this.selectedArea) || null
  456. let region = {
  457. province_id: provinceObj ? provinceObj.code : null,
  458. province_name: this.selectedProvince,
  459. city_id: cityObj ? cityObj.code : null,
  460. city_name: this.selectedCity || null,
  461. area_id: areaObj ? areaObj.code : null,
  462. area_name: this.selectedArea || null
  463. };
  464. // 去重:检查是否已经存在相同或更广范围的地区
  465. const exists = this.currentRuleRegions.some(item => {
  466. // 完全匹配检查
  467. if (item.province_name !== region.province_name) return false;
  468. // 如果当前添加的是省级规则
  469. if (!region.city_name) {
  470. return !item.city_name; // 匹配已有省级规则
  471. }
  472. // 如果当前添加的是市级规则
  473. if (region.city_name && !region.area_name) {
  474. return item.city_name === region.city_name && !item.area_name;
  475. }
  476. // 如果当前添加的是区级规则
  477. if (region.city_name && region.area_name) {
  478. return item.city_name === region.city_name && item.area_name === region.area_name;
  479. }
  480. return false;
  481. });
  482. if (exists) {
  483. this.$message.warning('该地区已在列表中')
  484. return
  485. }
  486. this.currentRuleRegions.push(region)
  487. // 清空已选项
  488. this.selectedProvince = ''
  489. this.selectedCity = ''
  490. this.selectedArea = ''
  491. this.cities = []
  492. this.areas = []
  493. },
  494. removeRegion(index) {
  495. this.currentRuleRegions.splice(index, 1)
  496. },
  497. // ========== 工具方法 ==========
  498. formatRegionLabel(region) {
  499. const parts = []
  500. if (region.province_name) parts.push(region.province_name)
  501. if (region.city_name) parts.push(region.city_name)
  502. if (region.area_name) parts.push(region.area_name)
  503. return parts.join(' - ') || '未知地区'
  504. }
  505. }
  506. }
  507. </script>
  508. <style scoped>
  509. .limit-purchase-rule-container {
  510. padding: 0;
  511. }
  512. .rule-list-card {
  513. margin-bottom: 0;
  514. }
  515. .card-header {
  516. display: flex;
  517. justify-content: space-between;
  518. align-items: center;
  519. }
  520. .region-tag {
  521. margin-right: 4px;
  522. margin-bottom: 4px;
  523. }
  524. .text-muted {
  525. color: #999;
  526. }
  527. .region-selector-wrapper {
  528. padding: 10px 0 0;
  529. }
  530. .region-selector-title {
  531. display: flex;
  532. align-items: center;
  533. margin-bottom: 16px;
  534. font-size: 14px;
  535. }
  536. .region-selector-title .text-danger {
  537. color: #f56c6c;
  538. }
  539. .region-form {
  540. margin-bottom: 16px;
  541. }
  542. .limited-region-list {
  543. background: #fff7f7;
  544. border-radius: 4px;
  545. padding: 12px 16px;
  546. }
  547. .inner-region-form {
  548. margin-bottom: 8px;
  549. }
  550. .limited-region-list .list-title {
  551. font-size: 14px;
  552. margin-bottom: 8px;
  553. }
  554. .limited-region-list .empty-tip {
  555. font-size: 13px;
  556. color: #999;
  557. }
  558. .limited-area-item {
  559. display: flex;
  560. justify-content: space-between;
  561. align-items: center;
  562. padding: 8px 0;
  563. border-bottom: 1px solid #f5d1d1;
  564. }
  565. .limited-area-item:last-child {
  566. border-bottom: none;
  567. }
  568. .dialog-footer {
  569. text-align: right;
  570. }
  571. .text-right {
  572. text-align: right;
  573. }
  574. .form-tip {
  575. margin-left: 10px;
  576. font-size: 12px;
  577. color: #999;
  578. }
  579. .region-tip {
  580. font-size: 12px;
  581. color: #999;
  582. margin-top: 5px;
  583. margin-bottom: 15px;
  584. }
  585. </style>