Browse Source

feat(home): 首页接口

Tim_Walker 2 years ago
parent
commit
d2adeb2ff4
7 changed files with 157 additions and 88 deletions
  1. 17 0
      src/api/home.js
  2. 16 0
      src/api/login.js
  3. 7 1
      src/manifest.json
  4. 75 68
      src/pages/home/index.vue
  5. 9 12
      src/service/index.js
  6. 23 1
      src/store/modules/user.js
  7. 10 6
      src/utils/system.js

+ 17 - 0
src/api/home.js

@@ -0,0 +1,17 @@
+import { send } from '@/service';
+import { request } from '../service';
+
+/**
+ * 获取首页
+ * @returns
+ */
+export function getHomePage(data) {
+  return request({
+    url: '/maintain/customer/index',
+    method: 'get',
+    data: data,
+    header: {
+      'content-type': 'application/x-www-form-urlencoded',
+    },
+  });
+}

+ 16 - 0
src/api/login.js

@@ -64,3 +64,19 @@ export function loginByPhoneAndSmsCode(data) {
     },
   });
 }
+
+/**
+ * 刷新token
+ * @param {*} data
+ * @returns
+ */
+export function refreshToken(data) {
+  return request({
+    url: '/uc/oauth/token',
+    method: 'post',
+    data: data,
+    header: {
+      'Content-Type': 'application/x-www-form-urlencoded',
+    },
+  });
+}

+ 7 - 1
src/manifest.json

@@ -54,7 +54,13 @@
     "optimization": {
       "subPackages": true
     },
-    "usingComponents": true
+    "usingComponents": true,
+    "permission" : {
+      "scope.userLocation" : {
+        "desc" : "使用定位功能"
+      }
+     },
+     "requiredPrivateInfos": ["getLocation"]
   },
   "mp-alipay": {
     "usingComponents": true

+ 75 - 68
src/pages/home/index.vue

@@ -2,11 +2,11 @@
   <view class="home">
     <!-- 顶部导航 -->
     <view class="home-header">
-      <view class="gotoAddress" @click="gotoAddress">
+      <view class="goto-address" @click="$Router.push('/client/chooseCity/index')">
         <span>地址</span>
-        <i class="iconfont icon-sanjiaoxing1"></i>
+        <text class="iconfont icon-sanjiaoxing1"></text>
       </view>
-      <view class="searchItem" @click="gotoSearch">
+      <view class="search-item" @click="Router.push('/client/search/index')">
         <u-search placeholder="请输入搜索内容" :showAction="false" v-model="keyword" />
       </view>
     </view>
@@ -29,60 +29,60 @@
 
     <!-- 热门推荐 -->
     <view class="home-hot">
-      <view class="hotRec">
-        <h3>热门推荐</h3>
-        <i class="iconfont icon-youjiantou"></i>
+      <view class="hot-rec">
+        <view>热门推荐</view>
+        <text class="iconfont icon-youjiantou"></text>
       </view>
-      <div class="hotList">
-        <view class="hotItem" @click="gotoDetail">
-          <view class="hotImage">图片</view>
-          <view class="hotName">商家名称</view>
-          <view class="hotName">具体位置</view>
+      <view class="hotList">
+        <view class="hot-item" @click="$Router.push('/business/detail/index')">
+          <view class="hot-image">图片</view>
+          <view class="hot-name">商家名称</view>
+          <view class="hot-name">具体位置</view>
         </view>
-        <view class="hotItem">
-          <view class="hotImage">图片</view>
-          <view class="hotName">商家名称</view>
-          <view class="hotName">具体位置</view>
+        <view class="hot-item">
+          <view class="hot-image">图片</view>
+          <view class="hot-name">商家名称</view>
+          <view class="hot-name">具体位置</view>
         </view>
-        <view class="hotItem">
-          <view class="hotImage">图片</view>
-          <view class="hotName">商家名称</view>
-          <view class="hotName">具体位置</view>
+        <view class="hot-item">
+          <view class="hot-image">图片</view>
+          <view class="hot-name">商家名称</view>
+          <view class="hot-name">具体位置</view>
         </view>
-        <view class="hotItem">
-          <view class="hotImage">图片</view>
-          <view class="hotName">商家名称</view>
-          <view class="hotName">具体位置</view>
+        <view class="hot-item">
+          <view class="hot-image">图片</view>
+          <view class="hot-name">商家名称</view>
+          <view class="hot-name">具体位置</view>
         </view>
-      </div>
+      </view>
     </view>
     <!-- 附近商家 -->
 
     <view class="home-hot">
-      <view class="hotRec">
-        <h3>附近商家</h3>
+      <view class="hot-rec">
+        <view>附近商家</view>
         <i class="iconfont icon-youjiantou"></i>
       </view>
       <view class="hotList">
-        <view class="hotItem">
-          <view class="hotImage">图片</view>
-          <view class="hotName">商家名称</view>
-          <view class="hotName">具体位置</view>
+        <view class="hot-item">
+          <view class="hot-image">图片</view>
+          <view class="hot-name">商家名称</view>
+          <view class="hot-name">具体位置</view>
         </view>
-        <view class="hotItem">
-          <view class="hotImage">图片</view>
-          <view class="hotName">商家名称</view>
-          <view class="hotName">具体位置</view>
+        <view class="hot-item">
+          <view class="hot-image">图片</view>
+          <view class="hot-name">商家名称</view>
+          <view class="hot-name">具体位置</view>
         </view>
-        <view class="hotItem">
-          <view class="hotImage">图片</view>
-          <view class="hotName">商家名称</view>
-          <view class="hotName">具体位置</view>
+        <view class="hot-item">
+          <view class="hot-image">图片</view>
+          <view class="hot-name">商家名称</view>
+          <view class="hot-name">具体位置</view>
         </view>
-        <view class="hotItem">
-          <view class="hotImage">图片</view>
-          <view class="hotName">商家名称</view>
-          <view class="hotName">具体位置</view>
+        <view class="hot-item">
+          <view class="hot-image">图片</view>
+          <view class="hot-name">商家名称</view>
+          <view class="hot-name">具体位置</view>
         </view>
       </view>
     </view>
@@ -90,9 +90,15 @@
 </template>
 
 <script>
+import { getHomePage } from '@/api/home';
+
 export default {
   data() {
     return {
+      location: {
+        longitude: 0,
+        latitude: 0,
+      },
       keyword: '',
       list: [
         'https://cdn.uviewui.com/uview/swiper/swiper1.png',
@@ -124,28 +130,29 @@ export default {
     };
   },
   methods: {
-    // 跳转地址栏
-    gotoAddress() {
-      uni.navigateTo({
-        url: "/client/chooseCity/index",
-      })
-    },
-    // 跳转详情页
-    gotoSearch() {
-      uni.navigateTo({
-        url: "/client/search/index",
-      })
-    },
-    // 跳转详情页
-    gotoDetail(){
-      uni.navigateTo({
-        url: "/business/detail/index",
-      })
-    },
-    change() { },
+    change() {},
     click(name) {
       this.$refs.uToast.success(`点击了第${name}个`);
     },
+    onShow() {
+      uni.getLocation({
+        type: 'gcj02',
+        success: res => {
+          console.log('当前位置的经度:' + res.longitude);
+          console.log('当前位置的纬度:' + res.latitude);
+          this.location.longitude = res.longitude;
+          this.location.latitude = res.latitude;
+          this.$store.commit('SET_LOCATION', { longitude: res.longitude, latitude: res.latitude });
+          getHomePage(this.location)
+            .then(res => {
+              console.log(res);
+            })
+            .catch(err => {
+              console.log(err);
+            });
+        },
+      });
+    },
   },
 };
 </script>
@@ -164,7 +171,7 @@ export default {
   padding: 0 20rpx;
   background-color: #347caf;
 
-  .gotoAddress {
+  .goto-address {
     display: flex;
     align-items: center;
 
@@ -173,7 +180,7 @@ export default {
       margin: 17rpx 10rpx 0 6rpx;
     }
   }
-  .searchItem{
+  .search-item {
     flex: 1;
   }
 }
@@ -203,7 +210,7 @@ export default {
 .home-hot {
   margin: 20rpx;
 
-  .hotRec {
+  .hot-rec {
     margin: 25rpx 0;
     display: flex;
     justify-content: space-between;
@@ -220,24 +227,24 @@ export default {
     justify-content: space-between;
   }
 
-  .hotItem:nth-last-child(2n + 1) {
+  .hot-item:nth-last-child(2n + 1) {
     margin-right: 0;
   }
 
-  .hotItem {
+  .hot-item {
     height: 300rpx;
     width: calc(50% - 10rpx);
     margin: 0 20rpx 20rpx 0;
     background-color: #fff;
 
-    .hotImage {
+    .hot-image {
       background-color: #e8e8e8;
       height: 200rpx;
       line-height: 200rpx;
       text-align: center;
     }
 
-    .hotName {
+    .hot-name {
       font-size: 24rpx;
     }
   }

+ 9 - 12
src/service/index.js

@@ -16,13 +16,14 @@ const errStatus = {
   415: '接口媒体类型不支持',
   500: '服务端异常',
 };
-// const { osName, deviceId, osversion } = getSystemInfoAsync();
 
 export function request(options) {
   uni.showLoading({
     title: options.loadingText || '加载中...',
   });
-  return new Promise((resolve, reject) => {
+  return new Promise(async (resolve, reject) => {
+    const { deviceId, osName, osVersion } = await getSystemInfoAsync();
+    const access_token = store.getters.access_token;
     return uni.request({
       timeout: TIME_OUT,
       url: BASE_URL + options.url,
@@ -31,14 +32,16 @@ export function request(options) {
       header: Object.assign(
         {},
         {
+          Authorization: `Bearer ${access_token}`,
           'Content-Type': 'application/json; charset=utf-8',
           client_id: 'chelvc_client',
           client_secret: 'qWBe6jD%GCuPPTkP',
           grant_type: 'wechat',
-          // Platform: osName.toUpperCase(),
-          // Device: deviceId,
-          // Version: osversion,
-          // Terminal: 'APPLET',
+          Device: deviceId,
+          Platform: osName.toUpperCase(),
+          Version: osVersion,
+          Terminal: 'APPLET',
+          Business: 'CUSTOMER',
         },
         options.header,
       ),
@@ -71,9 +74,3 @@ export function request(options) {
     });
   });
 }
-
-export function send(options) {
-  const access_token = store.getters.access_token;
-  options.header['Authorization'] = `Bearer ${access_token}`;
-  return request(options);
-}

+ 23 - 1
src/store/modules/user.js

@@ -1,10 +1,14 @@
 import { setCache, getCache } from '@/utils/cache';
-import { loginByWxLoginCode } from '@/api/login';
+import { loginByWxLoginCode, refreshToken } from '@/api/login';
 
 export default {
   state: () => ({
     access_token: getCache('access_token') || '',
     refresh_token: getCache('refresh_token') || '',
+    location: {
+      longitude: 0,
+      latitude: 0,
+    },
   }),
   mutations: {
     SET_ACCESS_TOKEN(state, data) {
@@ -15,6 +19,9 @@ export default {
       state.refresh_token = data;
       setCache('refresh_token', state.refresh_token);
     },
+    SET_LOCATION(state, data) {
+      state.location = data;
+    },
   },
   actions: {
     LoginByWxCode({ commit }, data) {
@@ -32,5 +39,20 @@ export default {
           });
       });
     },
+    RefreshToken({ commit }, data) {
+      return new Promise((resolve, reject) => {
+        refreshToken(data)
+          .then(res => {
+            if (res.access_token) {
+              commit('SET_ACCESS_TOKEN', res.access_token);
+              commit('SET_REFRESH_TOKEN', res.refresh_token);
+              resolve();
+            }
+          })
+          .catch(err => {
+            reject(err);
+          });
+      });
+    },
   },
 };

+ 10 - 6
src/utils/system.js

@@ -1,11 +1,15 @@
 //获取系统信息
 function getSystemInfoAsync() {
-  uni.getSystemInfoSync({
-    success: res => {
-      return res;
-    },
-    fail: err => {},
-    complete: () => {},
+  return new Promise((resolve, reject) => {
+    uni.getSystemInfo({
+      success: res => {
+        resolve(res);
+      },
+      fail: err => {
+        reject(err);
+      },
+      complete: () => {},
+    });
   });
 }