Ver código fonte

feat():首页

geek 4 anos atrás
pai
commit
596eb09ba2
9 arquivos alterados com 192 adições e 127 exclusões
  1. 14 0
      api/product.js
  2. 8 0
      api/store.js
  3. 2 4
      api/user.js
  4. 52 9
      app.js
  5. 27 68
      pages/index/index.js
  6. 18 18
      pages/index/index.wxml
  7. 40 5
      pages/person/person.js
  8. 10 13
      pages/person/person.wxml
  9. 21 10
      utils/request.js

+ 14 - 0
api/product.js

@@ -0,0 +1,14 @@
+import request from '../utils/request.js'
+
+class product extends request{
+
+    // static errorHandel(res) {
+    //     console.log(res);
+    // }
+
+    static async getProductList(page = 1, pageSize = 10) {
+        const res = await this.getRequest(`${this.BASE_URL}Product/index?page=${page}&pageSize=${pageSize}`)
+        return res.data;
+    }
+}
+export default product

+ 8 - 0
api/store.js

@@ -10,5 +10,13 @@ class store extends request{
         const res = await this.getRequest(`${this.BASE_URL}Store/index?page=${page}&pageSize=${pageSize}`, this._defaultHeader)
         return res.data;
     }
+
+    static async getNearStore(lat, lon) {
+        const res = await this.postRequest(`${this.BASE_URL}Store/getStore`, {
+            lat,
+            lon
+        })
+        return res.data;
+    }
 }
 export default store

+ 2 - 4
api/user.js

@@ -6,10 +6,8 @@ class store extends request{
     //     console.log(res);
     // }
 
-    static async userLogin(code) {
-        const res = await this.postRequest(`${this.BASE_URL}User/save`, {
-            code: code
-        })
+    static async userLogin(data) {
+        const res = await this.postRequest(`${this.BASE_URL}User/save`, data)
         return res.data;
     }
 }

+ 52 - 9
app.js

@@ -1,5 +1,6 @@
 //app.js
 import { default as commonApi } from "./api/commonApi"
+import { default as requestApi } from "./utils/request"
 import { default as userApi } from "./api/user"
 
 import { promisifyAll, promisify } from 'miniprogram-api-promise';
@@ -9,12 +10,14 @@ promisifyAll(wx, wxp)
 App({
   onLaunch: async function () {
     // 展示本地存储能力
-    if(!wx.getStorageSync('token')){
-      const tokenData = await commonApi.getToken();
-      if(tokenData.token){
-        wx.setStorageSync('token', tokenData.token)
-      }
-    }
+    // if(!wx.getStorageSync('token')){
+      // const tokenData = await commonApi.getToken();
+      // if(tokenData.token){
+      //   wx.setStorageSync('token', tokenData.token)
+      //   requestApi.HEAD.token = tokenData.token
+      // }
+    // }
+     const self = this
     // 登录
     const auth = await wxp.getSetting()
     if (!auth.authSetting['scope.userInfo']){
@@ -23,13 +26,53 @@ App({
     }
     // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
     const info = await wxp.getUserInfo()
-    this.globalData.userInfo = res.userInfo
+    this.globalData.userInfo = info.userInfo
     if (this.userInfoReadyCallback) {
       this.userInfoReadyCallback(res)
     }
-
+    await self.wxLogin(info.userInfo);
   },
   globalData: {
     userInfo: null
-  }
+  },
+  wxLogin:async function(data){
+    console.log(data)
+    if (!data){
+      console.log('你还没有授权')
+      return
+    }
+    let _self = this;
+    let res = await wxp.login()
+    console.log(res)
+    if (!res.code){
+      return
+    }
+    data['code'] = res.code
+    console.log(data)
+    let info =  await userApi.userLogin(data)
+    wx.setStorageSync('token', info.userInfo.token)
+    requestApi.HEAD.token = info.userInfo.token
+    this.setUserInfo(info.userInfo)
+    return info.userInfo
+  },
+  setUserInfo: function(userInfo){
+    wx.setStorage({
+      data: userInfo,
+      key: 'userInfo',
+    });
+    this.globalData.userInfo = userInfo
+  },
+  getWxUserInfo: async function(){
+    if (!this.globalData.userInfo){
+      try {
+        let userInfo = wx.getStorageSync({key: 'userInfo'})
+        return userInfo.data
+      } catch (error) {
+        console.log('缓存中没有用户信息')
+        return null
+      }
+    }else {
+      return this.globalData.userInfo
+    }
+  },
 })

+ 27 - 68
pages/index/index.js

@@ -1,3 +1,8 @@
+import { default as storeApi} from "../../api/store"
+import { default as commonApi} from "../../api/commonApi"
+import { default as requestApi} from "../../utils/request"
+import { default as productApi} from "../../api/product"
+
 Page({
   data: {
     shopList: [{
@@ -54,15 +59,22 @@ Page({
 
 
   onLoad: async function () {
+    const self = this
+    const tokenData = await commonApi.getToken();
+      if(tokenData.token){
+        wx.setStorageSync('token', tokenData.token)
+        requestApi.HEAD.token = tokenData.token
+      }
     wx.getSetting({
       success: (res) => {
         let authSetting = res.authSetting
         if (authSetting['scope.userLocation']) {
           console.log('已授权地理位置')
           // 已授权
+          self.getLocation();
         } else {
           // 未授权
-          console.log('未授权');
+          console.log('未授权地理位置');
         }
       }
     })
@@ -85,70 +97,13 @@ Page({
 
   getProductList: async function () {
     const self = this
-    var titles = ["全部项目", "SPA", "泡浴", "推拿", "足浴"];
-    const tabs = [];
-    var list = [{
-        id: "1",
-        title: "葛优躺(北京躺)电影足疗",
-        price: "299",
-        desc: "全店通用",
-        open: true,
-        imgurl: "../../images/product01.jpg"
-      },
-      {
-        id: "2",
-        title: "中式古法推拿",
-        price: "299",
-        desc: "暂未开放",
-        open: false,
-        imgurl: "../../images/product02.jpg"
-      },
-      {
-        id: "3",
-        title: "中式古法推拿",
-        price: "299",
-        desc: "暂未开放",
-        open: false,
-        imgurl: "../../images/product02.jpg"
-      },
-      {
-        id: "4",
-        title: "葛优躺(北京躺)电影足疗",
-        price: "299",
-        desc: "暂未开放",
-        open: false,
-        imgurl: "../../images/product01.jpg"
-      }
-    ]
-    /* for (let i = 0; i < titles.length; i++) {
-      let title = titles[i]
-      let rs = await self.requestAll('https://example.com/ajax?index=0', { // \
-        mock: true // - 添加这三行
-      }, {}, 'get')
-      tabs.push({
-        title,
-        productList: rs.list
-      })
-    } */
-
-
-    for (let i = 0; i < titles.length; i++) {
-      let title = titles[i];
-      tabs.push({
-        title,
-        list
-      })
-    }
-    tabs[2].list = [{
-      id: "1",
-      title: "葛优躺(北京躺)电影足疗",
-      price: "299",
-      desc: "全店通用",
-      open: true,
-      imgurl: "../../images/product01.jpg"
-    }]
-    return tabs;
-
+    const productList = await productApi.getProductList(1,10)
+    console.log()
+    self.setData({
+      productList: productList,
+      page:1,
+      pageSize:10
+    })
   },
   requestAll(url, data, header = {}, method) {
     const _self = this
@@ -212,18 +167,22 @@ Page({
       url: './webview',
     })
   },
-  getLocation: function(){
+  getLocation: async function(){
+    console.log(1111)
     const self = this
     wx.getLocation({
       type: 'wgs84',
-      success (res) {
+      async success (res) {
         console.log(res)
         const latitude = res.latitude
         const longitude = res.longitude
         const speed = res.speed
         const accuracy = res.accuracy
+        const rs = await storeApi.getNearStore(latitude,longitude)
+        console.log(rs.info);
         self.setData({
-          isLocation: true
+          locationFlag: true,
+          storeInfo: rs.info
         })
       }
      })

+ 18 - 18
pages/index/index.wxml

@@ -17,34 +17,34 @@
         <navigator url="../../pages/shopinfo/shopinfo?id={{item.id}}" wx:for="{{shopList}}" wx:key="id" hover-class="none">
           <view class="shop-info">
               <view class="shopimg">
-                  <image src="{{item.imgurl}}" mode="aspectFill" lazy-load="false" class="img">
+                  <image src="{{storeInfo.logo}}" mode="aspectFill" lazy-load="false" class="img">
                   </image>
               </view>
               <view class="shopmsg">
                   <view class="shopview">
                       <view class="c-202020">
-                          {{item.shopname}}
+                          {{storeInfo.storeName}}
                       </view>
-                      <view class="c-202020 mark f12 {{item.new?'new':''}} {{item.discount?'discount':''}}" wx:if="{{item.mark}}">
+                      <!-- <view class="c-202020 mark f12 {{item.new?'new':''}} {{item.discount?'discount':''}}" wx:if="{{item.mark}}">
                           {{item.marktext}}
-                      </view>
+                      </view> -->
                   </view>
                   <view class="shopview">
                       <view>
-                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{item.stars>0?'filled':'outline'}}"></mp-icon>
-                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{item.stars>1?'filled':'outline'}}"></mp-icon>
-                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{item.stars>2?'filled':'outline'}}"></mp-icon>
-                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{item.stars>3?'filled':'outline'}}"></mp-icon>
-                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{item.stars>4?'filled':'outline'}}"></mp-icon>
+                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{storeInfo.score>0?'filled':'outline'}}"></mp-icon>
+                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{storeInfo.score>1?'filled':'outline'}}"></mp-icon>
+                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{storeInfo.score>2?'filled':'outline'}}"></mp-icon>
+                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{storeInfo.score>3?'filled':'outline'}}"></mp-icon>
+                          <mp-icon icon="star" color="#E15F3D" size="18" type="{{storeInfo.score>4?'filled':'outline'}}"></mp-icon>
                       </view>
                   </view>
                   <view class="shop-address shopview">
                       <view class="c-707070 f12">
                           <mp-icon icon="location" color="#707070" size="20" class="address-icon" ></mp-icon>
-                          {{item.shop_address}}
+                          {{storeInfo.address}}
                       </view>
                       <view class="c-707070 f12">
-                          {{item.shop_distance}}
+                          {{storeInfo.distance}}
                       </view>
                   </view>
               </view>
@@ -52,7 +52,7 @@
       </navigator>
       </view>
       <view class="user-location-btn" wx:if="{{!locationFlag}}" >
-        <button class="location-btn" open-type="getLocation"    bindtap="getLocation" >授权获取附近门店</button>
+        <button class="location-btn" open-type="getLocation" catchtap="getLocation" >授权获取附近门店</button>
       </view>
     </view>
     <!-- 产品信息 -->
@@ -89,15 +89,15 @@
       </mp-tabs>
  -->
 
- <block wx:for="{{productList}}" wx:key="id">
+ <block wx:for="{{productList.list}}" wx:key="id">
   <view class="productinfo">
-    <view class="product-item" style="background-image: url({{item.imgurl}});">
+    <view class="product-item" style="background-image: url({{item.productImg}});">
       <view class="product-desc" >
-        <view class="f17">{{item.title}}</view>
-        <view class=".fw700">¥{{item.price}}/人</view>
-        <view class="f13">{{item.desc}}</view>
+        <view class="f17">{{item.productName}}</view>
+        <view class=".fw700">¥{{item.currentPrice}}/人</view>
+        <view class="f13">{{item.productContent}}</view>
       </view>
-      <view class="order {{item.open?'':'gray'}}">
+      <view class="order {{item.status?'':'gray'}}">
         <view >
           立即预约
         </view>

+ 40 - 5
pages/person/person.js

@@ -1,4 +1,5 @@
 // pages/person/person.js
+const app = getApp()
 Page({
 
   /**
@@ -8,14 +9,23 @@ Page({
     person:{
       tel:"18822225555",
       userName:"伊布"
-    }
+    },
+    hasUserInfo: false
   },
 
   /**
    * 生命周期函数--监听页面加载
    */
-  onLoad: function (options) {
-
+  onLoad: async function (options) {
+    let userInfo = await app.getWxUserInfo()
+    console.log(userInfo);
+    if (userInfo){
+      this.setData({
+        userInfo: userInfo,
+        hasUserInfo: true,
+        hasMobile: userInfo.mobile ? true : false
+      })
+    }
   },
 
   /**
@@ -28,8 +38,16 @@ Page({
   /**
    * 生命周期函数--监听页面显示
    */
-  onShow: function () {
-
+  onShow: async function () {
+    let userInfo = await app.getWxUserInfo()
+    console.log(userInfo);
+    if (userInfo){
+      this.setData({
+        userInfo: userInfo,
+        hasUserInfo: true,
+        hasMobile: userInfo.mobile ? true : false
+      })
+    }
   },
 
   /**
@@ -65,5 +83,22 @@ Page({
    */
   onShareAppMessage: function () {
 
+  },
+  getInfo : async function(e){
+    const _self = this
+    let userInfo = e.detail.userInfo
+    userInfo['encryptedData'] = e.detail.encryptedData
+    userInfo['iv'] = e.detail.iv
+    let info = await app.wxLogin(userInfo);
+    let hasMobile = false;
+    if (info.mobile){
+      hasMobile = true
+    }
+    _self.setData({
+      hasUserInfo: true,
+      userInfo: info,
+      hasMobile: hasMobile,
+    })
+    console.log(2222)
   }
 })

+ 10 - 13
pages/person/person.wxml

@@ -1,31 +1,28 @@
 <!--pages/person/person.wxml-->
 <view class="container">
-    <navigator class="person card" url="../personinfo/personinfo">
+    <navigator class="person card" url="../personinfo/personinfo" wx:if="{{hasUserInfo}}">
         <view class="person-info">
-            <view class="user-name f18 c-202020">{{person.userName}}
-                <image class="" src="../../images/icons/next.png" >
-                    
+            <view class="user-name f18 c-202020">{{userInfo.name}}
+                <image class="" src="../../images/icons/next.png" >      
                 </image>
             </view>
-            <view class="tel c-707070 fw300">{{person.tel}}</view>
+            <view class="tel c-707070 fw300">{{userInfo.mobile}}</view>
         </view>
         <view class="head-img">
-            <image class="" src="../../images/headImg.jpg">
-
+            <image class="" src="{{userInfo.avatar}}">
             </image>
         </view>
     </navigator>
+    <view wx:else>
+        <button open-type="getUserInfo" bindgetuserinfo="getInfo">授权登入</button>
+    </view>
     <view class="order">
         <image class="icons" src="../../images/icons/order.png">
-
         </image>
-        <view class="order-text">我的订单
-            
+        <view class="order-text">我的订单        
         </view>
-        <image class="next-gray" src="../../images/icons/next-gray.png">
-                
+        <image class="next-gray" src="../../images/icons/next-gray.png">             
         </image>
-
     </view>
     <navigator class="assess" hover-class="none" url="../comments/comments?title=我的评论">
         <image class="icons" src="../../images/icons/assess.png"></image>

+ 21 - 10
utils/request.js

@@ -2,13 +2,12 @@
 class request {
 
     static BASE_URL = 'https://llzlovesh.top/api/'
+    static HEAD = {
+        "Content-Type": "application/json",
+        "token": wx.getStorageSync('token')
+    }
+    static constructor() {
 
-     static constructor() {
-         this._header = {
-            "Content-Type": "application/json",
-            "token": wx.getStorageSync('token')
-
-        }
     }
 
     /**
@@ -21,28 +20,40 @@ class request {
     /**
      * GET类型的网络请求
      */
-    static getRequest(url, data, header = this._header ) {
+    static getRequest(url, data, header = this.HEAD ) {
+        if (!this.HEAD.token){
+            header.token = wx.getStorageSync('token')
+        }
         return this.requestAll(url, data, header, 'GET')
     }
 
     /**
      * DELETE类型的网络请求
      */
-    static deleteRequest(url, data, header = this._header ) {
+    static deleteRequest(url, data, header = this.HEAD ) {
+        if (!this.HEAD.token){
+            header.token = wx.getStorageSync('token')
+        }
         return this.requestAll(url, data, header, 'DELETE')
     }
 
     /**
      * PUT类型的网络请求
      */
-    static putRequest(url, data, header = this._header ) {
+    static putRequest(url, data, header = this.HEAD ) {
+        if (!this.HEAD.token){
+            header.token = wx.getStorageSync('token')
+        }
         return this.requestAll(url, data, header, 'PUT')
     }
 
     /**
      * POST类型的网络请求
      */
-    static postRequest(url, data, header = this._header) {
+    static postRequest(url, data, header = this.HEAD) {
+        if (!this.HEAD.token){
+            header.token = wx.getStorageSync('token')
+        }
         return this.requestAll(url, data, header, 'POST')
     }