From 46b4f05a6b7a1c02ef912591c683de44f8979c23 Mon Sep 17 00:00:00 2001 From: lifizer <913626195@qq.com> Date: Wed, 31 Jul 2024 21:38:49 +0800 Subject: [PATCH] =?UTF-8?q?Refactor(=E9=A6=96=E9=A1=B5):=E6=94=B9=E7=89=88?= =?UTF-8?q?=E9=87=8D=E6=9E=84-=E6=8E=A5=E5=8F=A3=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/public.js | 4 +- components/xdd-product-item/index.vue | 17 ++++---- pages/home/index.vue | 50 ++++++++++++++++++----- utils/common.js | 58 +++++++++++++++++++++++++++ utils/request.js | 2 +- 5 files changed, 110 insertions(+), 21 deletions(-) diff --git a/api/public.js b/api/public.js index 1acbf7a..d9d233c 100644 --- a/api/public.js +++ b/api/public.js @@ -8,8 +8,8 @@ export function getSplashScreen() { * 首页 * @returns {*} */ -export function getHomeData() { - return request.get("index", {}, { +export function getHomeData(params) { + return request.get("index", params, { login: false }); } diff --git a/components/xdd-product-item/index.vue b/components/xdd-product-item/index.vue index 1e81e3e..f8e7987 100644 --- a/components/xdd-product-item/index.vue +++ b/components/xdd-product-item/index.vue @@ -11,7 +11,7 @@ class="mark-img" /> @@ -25,12 +25,12 @@ {{ item.storeName }} - - {{ item.desc || '古法熬制·甄选原料' }} + + {{ item.bestContent }} - + - 5元无门槛 + {{ item.couponName }} @@ -86,8 +86,8 @@ export default { position: relative; z-index: 2; display: block; - width: 344rpx; - height: 344rpx; + width: 345rpx; + height: 345rpx; border-radius: 20rpx; } .mark-img { @@ -108,12 +108,13 @@ export default { color: #333; } .desc { - padding: 10rpx 0; + padding: 10rpx 0 0 0; color: #FFC543; font-size: 24rpx; } .coupon { display: flex; + padding: 10rpx 0 0 0; .tag + .tag { margin: 0 0 0 -1rpx; } diff --git a/pages/home/index.vue b/pages/home/index.vue index 86767ca..98acf37 100644 --- a/pages/home/index.vue +++ b/pages/home/index.vue @@ -7,16 +7,15 @@ }" :class="scrollTop > 40 ? 'blur' : ''" class="fix-header" - @click="goGoodSearch" > - - 昆明 + + {{ isLocating ? '定位中' : (cityName || '定位失败') }} - + @@ -208,7 +207,7 @@ @@ -251,6 +250,12 @@ import { getCouponReceive } from '@/api/user' import { getHomeData, getShareImage } from '@/api/public' import { sharpBannar } from '@/api/goods' import { pageListenMixins } from '@/mixins/pageListenMixins' +import { + getCurAddress, + getLocationPromise, + getLocationSetting, + openLocationSettting +} from "@/utils/common" export default { name: 'IndexPage', components: { @@ -267,6 +272,8 @@ export default { rightDistance: 10, banner: [], menus: [], + // 定位中 + isLocating: true, cityName: '', // 地标好物 landmarkGoodsImage: {}, @@ -334,11 +341,31 @@ export default { this.bannerList = data.banner this.getPageData() }) + this.getLocationData() + }, + reLocateHandle() { + if (this.cityName) return + getLocationSetting(() => openLocationSettting().then(() => { + this.getLocationData() + })) + }, + async getLocationData() { + this.isLocating = true + const mapData = await getLocationPromise() + if (mapData.errMsg) { + const { latitude, longitude } = mapData + const address = await getCurAddress(latitude, longitude) + this.cityName = address[1].data.result.ad_info.city + this.isLocating = false + this.getPageData() + } else { + this.isLocating = false + } }, getPageData() { const _this = this uni.showLoading({ title: '加载中' }) - getHomeData().then(res => { + getHomeData({ cityName: this.cityName }).then(res => { const { data } = res _this.$set(_this, 'banner', data.banner) _this.$set(_this, 'menus', data.menus) @@ -366,15 +393,15 @@ export default { list: [ { image: 'http://resource.xdd618.com/19/20240722/1721615384602_入口图8.jpg', - url: '/pkg_product/views/festival' + id: 5781 }, { image: 'http://resource.xdd618.com/19/20240715/1721006994007_微信图片_20240715092549.jpg', - url: '/pkg_product/views/festival' + id: 5781 }, { image: 'http://resource.xdd618.com/19/20240722/1721615384602_入口图8.jpg', - url: '/pkg_product/views/festival' + id: 5781 } ] }) @@ -478,6 +505,9 @@ export default { hotelItemClickHandle(item) { uni.navigateTo({ url: item.url }) }, + productSwiperItemClickHandle(item) { + uni.navigateTo({url: '/pkg_product/views/homestay?id=' + item.id}) + }, setTopHandle() { uni.pageScrollTo({ scrollTop: 0, diff --git a/utils/common.js b/utils/common.js index 081b0e3..8cd1a6b 100644 --- a/utils/common.js +++ b/utils/common.js @@ -6,6 +6,19 @@ module.exports = { type: 'wgs84' }) }, + getLocationPromise() { + return new Promise((resolve, reject) => { + uni.getLocation({ + type: 'wgs84', + success: (res) => { + resolve(res) + }, + fail: () => { + resolve({}) + } + }) + }) + }, getCurAddress(latitude, longitude) { return uni.request({ @@ -14,6 +27,51 @@ module.exports = { }); }, + getLocationSetting(cb) { + uni.getSetting({ + success: res => { + if (!res.authSetting['scope.userLocation']) { + uni.showModal({ + title: '提示', + content: '当前定位未开启,请点击确定手动开启定位', + success: response => { + if (response.confirm) { + cb && cb () + } else { + uni.showToast({ + title: '您拒绝了授权,无法获取定位服务', + duration: 2000, + icon: 'none' + }) + } + } + }) + } + } + }) + }, + + openLocationSettting() { + return new Promise((resolve, reject) => { + uni.openSetting({ + success: res => { + if (res.authSetting['scope.userLocation']) { + resolve() + } else { + uni.showToast({ + title: '您拒绝了授权,无法获取定位服务', + duration: 2000, + icon: 'none' + }) + } + }, + fail: () => { + reject() + } + }) + }) + }, + getUrlParam(url, name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = url.match(reg); diff --git a/utils/request.js b/utils/request.js index baf1cbe..3d9d03b 100644 --- a/utils/request.js +++ b/utils/request.js @@ -46,7 +46,7 @@ function baseRequest(options) { // 如果接口需要登录,携带 token 去请求 options.headers = { ...options.headers, - Authorization: "Bearer " + token + Authorization: token ? ('Bearer ' + token) : '' } // 结构请求需要的参数