Преглед на файлове

```
feat(address): 添加城市名称获取功能并集成到电影模块

- 新增 getCityName 工具函数用于从地址字符串中提取城市名称
- 在 auto-imports.d.ts 中注册 getCityName 自动导入
- 在 address store 中使用 getCityName 更新城市信息
- 将城市名称传递给电影列表 API 请求以实现地理位置相关的数据筛选
- 更新电影首页和电影详情页的数据请求逻辑
```

wenjie преди 2 дни
родител
ревизия
2c37390ebc
променени са 5 файла, в които са добавени 26 реда и са изтрити 3 реда
  1. 2 0
      src/auto-imports.d.ts
  2. 2 0
      src/store/address.ts
  3. 2 1
      src/subPack-film/index/index.vue
  4. 2 2
      src/subPack-film/movie/index.vue
  5. 18 0
      src/utils/index.ts

+ 2 - 0
src/auto-imports.d.ts

@@ -49,6 +49,7 @@ declare global {
   const extendRef: typeof import('@vueuse/core')['extendRef']
   const fixImgStyle: typeof import('./utils/index')['fixImgStyle']
   const getActivePinia: typeof import('pinia')['getActivePinia']
+  const getCityName: typeof import('./utils/index')['getCityName']
   const getCurrentInstance: typeof import('vue')['getCurrentInstance']
   const getCurrentPath: typeof import('./utils/index')['getCurrentPath']
   const getCurrentScope: typeof import('vue')['getCurrentScope']
@@ -415,6 +416,7 @@ declare module 'vue' {
     readonly extendRef: UnwrapRef<typeof import('@vueuse/core')['extendRef']>
     readonly fixImgStyle: UnwrapRef<typeof import('./utils/index')['fixImgStyle']>
     readonly getActivePinia: UnwrapRef<typeof import('pinia')['getActivePinia']>
+    readonly getCityName: UnwrapRef<typeof import('./utils/index')['getCityName']>
     readonly getCurrentInstance: UnwrapRef<typeof import('vue')['getCurrentInstance']>
     readonly getCurrentPath: UnwrapRef<typeof import('./utils/index')['getCurrentPath']>
     readonly getCurrentScope: UnwrapRef<typeof import('vue')['getCurrentScope']>

+ 2 - 0
src/store/address.ts

@@ -1,4 +1,5 @@
 import { defineStore } from 'pinia'
+import { getCityName } from '@/utils/index'
 
 interface addressState {
   Location: {
@@ -104,6 +105,7 @@ export const useAddressStore = defineStore('address', {
           this.Location.latitude = res.latitude
           this.Location.longitude = res.longitude
           this.name = res.name
+          this.city = getCityName(res.address)
         },
         fail: (e) => {
           console.log('获取地址失败', e)

+ 2 - 1
src/subPack-film/index/index.vue

@@ -10,6 +10,7 @@ definePage({
     backgroundColorBottom: '#fff',
   },
 })
+const addressStore = useAddressStore()
 
 const hotList = ref<Api.filmMovieList>([
 
@@ -30,7 +31,7 @@ function handleBuy(item: Api.filmMovieList) {
 
 async function getList(showSt: number) {
   uni.showLoading({ title: '加载中' })
-  const res = await Apis.film.getMovieList({ data: { showSt, pageNum: 1, pageSize: 8 } })
+  const res = await Apis.film.getMovieList({ data: { showSt, pageNum: 1, pageSize: 8, cityName: addressStore.city } })
   console.log(res, '请求')
   if (!res.data) {
     useGlobalToast().show('暂无该商品查看权限!')

+ 2 - 2
src/subPack-film/movie/index.vue

@@ -24,7 +24,7 @@ const tabList = reactive([
 ])
 
 const { data: hotList, isLastPage, page, reload, error, refresh } = usePagination((pageNum, pageSize) =>
-  Apis.film.getMovieList({ data: { showSt: 1, pageNum, pageSize } }), {
+  Apis.film.getMovieList({ data: { showSt: 1, pageNum, pageSize, cityName: addressStore.city } }), {
   data: resp => resp.data?.records,
   initialData: [],
   initialPage: 1,
@@ -44,7 +44,7 @@ const { data: filmList, isLastPage: isLastPage1, page: page1, reload: reload1, e
 })
 
 const { data: comingSoonList, isLastPage: isLastPage2, page: page2, reload: reload2, error: error2, refresh: refresh2 } = usePagination((pageNum, pageSize) =>
-  Apis.film.getMovieList({ data: { showSt: 2, pageNum, pageSize } }), {
+  Apis.film.getMovieList({ data: { showSt: 2, pageNum, pageSize, cityName: addressStore.city } }), {
   data: resp => resp.data?.records,
   initialData: [],
   initialPage: 1,

+ 18 - 0
src/utils/index.ts

@@ -239,3 +239,21 @@ export function fixImgStyle(html: string) {
 
   return result
 }
+
+// 从地址字符串中提取城市名称
+export function getCityName(address: string): string {
+  if (!address)
+    return ''
+
+  // 安全正则,无 ESLint 警告
+  const regex = /^[^省]+省([^市]+)/
+  const match = address.match(regex)
+
+  if (match) {
+    return match[1]
+  }
+
+  // 兼容直辖市(北京 / 上海 / 重庆 / 天津)
+  const municipality = address.match(/(北京|上海|重庆|天津)/)
+  return municipality ? municipality[1] : ''
+}