Przeglądaj źródła

设备详情页修改

zuypeng 1 rok temu
rodzic
commit
ad383268e9

+ 9 - 2
src/components/login-bar/index.vue

@@ -16,10 +16,17 @@ const state= reactive({
   isLogin:false
 });
 
-onShow(()=>{
+onLoad(()=>{
   let userData = getApp<any>().globalData.user;
   state.isLogin =!!(userData&&userData.id);
   console.log("login bar onLoad>>>>", userData, state.isLogin)
+})
+
+
+onShow(()=>{
+  let userData = getApp<any>().globalData.user;
+  state.isLogin =!!(userData&&userData.id);
+  console.log("login bar onShow>>>>", userData, state.isLogin)
   addListener();
 })
 
@@ -29,7 +36,7 @@ onHide(()=>{
 
 const addListener = () => {
   uni.$on('login', function (data) {
-    state.isLogin = true;
+    state.isLogin = data.isLogin;
   })
 
   uni.$on('logout', function (data) {

+ 26 - 12
src/pages-wash/device/index.vue

@@ -5,7 +5,7 @@
         <text>洗车机编号:No.{{ state.device?.shortId }}</text>
       </view>
       <view class="device-header_fun">
-        <view class="device-header_func-tag" v-for="f in state.device.functionList" :key="f" style="margin-right: 10px;">
+        <view class="device-header_func-tag" v-for="f in state.device?.functionList" :key="f" style="margin-right: 10px;">
           <uv-tags :text="f" size="mini" plain plainFill bgColor="#19A497" color="white"></uv-tags>
         </view>
       </view>
@@ -45,7 +45,7 @@ import {onHide, onLoad, onShow} from "@dcloudio/uni-app";
 import {reactive, ref} from "vue";
 import {debounce, fmtDictName} from "@/utils/common";
 import {get, post} from "@/utils/https";
-import {fetchToken, tryLogin} from "@/utils/auth";
+import {checkLogin, fetchToken, tryLogin} from "@/utils/auth";
 import LoginBar from "@/components/login-bar/index.vue";
 
 const initState = () => ({
@@ -66,6 +66,9 @@ onHide(() => {
 })
 
 onLoad((options: any) => {
+  // uni.showModal({
+  //   title:'onLoad,'+options?.shortId
+  // })
   console.log("device onLoad>>>>", options)
   let id = options?.shortId;
   if (!id) {
@@ -80,7 +83,7 @@ onLoad((options: any) => {
     }
   }
   state.device = getApp<any>().globalData.last.device;
-  tryLogin().then(() => {
+  tryLogin().then((token) => {
     setTimeout(() => {
       loadDeviceDetail(id);
     }, 200)
@@ -90,13 +93,24 @@ onLoad((options: any) => {
 
 onShow(() => {
   addListener();
-  if (!state.deviceId) {
-    console.log("deviceId",getApp<any>().globalData.deviceId)
-    let deviceId = getApp<any>().globalData.deviceId;
-    if (deviceId) {
-      loadDeviceDetail(deviceId);
+  // uni.showToast({
+  //   title:'onShow,'
+  // })
+  checkLogin().then(()=>{
+    // uni.showToast({
+    //   title:'onShow,loaded'
+    // })
+    if (!state.deviceId) {
+      console.log("deviceId",getApp<any>().globalData.deviceId)
+      let deviceId = getApp<any>().globalData.deviceId;
+      if (deviceId) {
+        loadDeviceDetail(deviceId);
+      }
+    }else{
+      loadDeviceDetail(state.deviceId);
     }
-  }
+  })
+
 
 })
 
@@ -104,7 +118,7 @@ onShow(() => {
 
 const addListener = () => {
   uni.$on('login', function (data) {
-      if(state.deviceId){
+      if(state.deviceId && data.isLogin){
         loadDeviceDetail(state.deviceId);
       }
   })
@@ -163,7 +177,7 @@ const handleClickDevice = () => {
             title: "启动中",
             mask: true,
           });
-          post(`/wash-device/startDevice/${state.device.shortId}`).then((res: any) => {
+          post(`/wash-device/startDevice/${state.device?.shortId}`).then((res: any) => {
             uni.hideLoading();
             uni.showToast({
               title: '设备启动成功'
@@ -175,7 +189,7 @@ const handleClickDevice = () => {
             title: "停止中",
             mask: true,
           });
-          post(`/wash-device/stopDevice/${state.device.shortId}`).then((res: any) => {
+          post(`/wash-device/stopDevice/${state.device?.shortId}`).then((res: any) => {
             uni.hideLoading();
             uni.showToast({
               title: '设备停机成功'

+ 1 - 1
src/pages.json

@@ -161,7 +161,7 @@
             "navigationBarTitleText": "支付详情",
             "navigationBarBackgroundColor": "#ffffff"
           }
-        },
+        }
       ]
     }
   ],

+ 39 - 13
src/pages/index/index.vue

@@ -7,18 +7,24 @@
           <text class="title">{{ title }}</text>
         </view>-->
     <view style="width: 100%;" class="swiper-content">
-      <uv-swiper
-          class="swiper-item-1"
+<!--      {{state.swiperList}}-->
+      <cover-view>
+        <swiper class="swiper" circular :indicator-dots="true" :autoplay="true" :interval="3000"
+                :duration="500">
+          <swiper-item v-for="(item,idx) in state.swiperList" :key="idx">
+            <image :src="item" mode="widthFix" style="width: 100%" @click="handleBannerClick(idx)"></image>
+          </swiper-item>
+        </swiper>
+      </cover-view>
+
+<!--      <uv-swiper
           style="width: 100%"
           :list="state.swiperList"
           @click="handleBannerClick"
-          previousMargin="30"
-          nextMargin="30"
           circular
           :autoplay="true"
-          radius="5"
           bgColor="#ffffff">
-      </uv-swiper>
+      </uv-swiper>-->
     </view>
     <!--
         <view class="w100">
@@ -39,12 +45,19 @@
       </uv-grid>
     </view>
 
+<!--    <image v-if="state.swiperList.length>0" :src="state.swiperList[0]" mode="widthFix" style="width: 100%"></image>-->
+
     <view class="w100 gap"></view>
 
     <!--    站点清单  start-->
     <view class="w100 content_station">
       <template v-if="isLogin">
         <WashStation v-for="item in state.stationList" :key="item.id" :item="item"></WashStation>
+        <WashStation v-for="item in state.stationList" :key="item.id" :item="item"></WashStation>
+        <WashStation v-for="item in state.stationList" :key="item.id" :item="item"></WashStation>
+        <WashStation v-for="item in state.stationList" :key="item.id" :item="item"></WashStation>
+        <WashStation v-for="item in state.stationList" :key="item.id" :item="item"></WashStation>
+        <WashStation v-for="item in state.stationList" :key="item.id" :item="item"></WashStation>
       </template>
       <uv-empty v-else mode="order" text="请先登录"></uv-empty>
     </view>
@@ -61,7 +74,7 @@
 </template>
 
 <script setup lang="ts">
-import {reactive, ref} from 'vue'
+import {nextTick, reactive, ref} from 'vue'
 import {onHide, onShow, onLoad} from "@dcloudio/uni-app";
 import TabBar from "@/components/tab-bar/index.vue";
 import LoginBar from "@/components/login-bar/index.vue";
@@ -128,8 +141,14 @@ onShow(() => {
   if (token) {
     getApp<any>().globalData.token = token;
     getApp<any>().globalData.isLogin = true;
-    isLogin.value = true;
-    loadUserInfo();
+
+    let gd = getApp<any>().globalData;
+    console.log(gd)
+    if (!gd.user || !gd.user.id) {
+      loadUserInfo();
+    } else {
+      isLogin.value = true;
+    }
   } else {
     //主动退出的不自动登录
     let manualLogout = getApp<any>().globalData.manualLogout;
@@ -160,9 +179,10 @@ onShow(() => {
 
   if (isLogin.value) {
     loadBannerList();
+  } else {
+    addListener();
   }
 
-  addListener();
 
 });
 
@@ -194,8 +214,11 @@ const removeListener = () => {
 
 const loadBannerList = () => {
   get(`/banner`).then((res: any) => {
-    state.bannerList = res;
-    state.swiperList = res.map(k => k.bannerUrl);
+    nextTick(() => {
+      state.bannerList = res;
+      state.swiperList = res.map(k => k.bannerUrl);
+      console.log(state.swiperList)
+    })
   })
 }
 
@@ -241,8 +264,8 @@ const loadCurrentLocation = () => {
           }
           getApp<any>().globalData.location = location;
           state.location = location
-          loadWashStationList();
           console.log("loadCurrentLocation location", res)
+          loadWashStationList();
         }
       });
     },
@@ -256,6 +279,9 @@ const loadCurrentLocation = () => {
 
 const loadWashStationList = () => {
   if (isLogin.value) {
+    if (state.stationList && state.stationList.length > 0) {
+      return;
+    }
     post(`/wash-station/listStation`, {pageSize: 5}).then((res: any) => {
       console.log(res)
       let {list, total} = res;

+ 101 - 74
src/utils/auth.ts

@@ -1,70 +1,70 @@
 import {body, get} from "@/utils/https";
 
-const _tokenQueue: {
-    pending: boolean;
-    list: {
-        resolve: (value: string) => void;
-        reject: (err: any) => void;
-    }[];
-} = {
-    pending: false,
-    list: [],
-};
-
-let _onTokenListener: {
-    cb: (token: string) => void;
-}[] = [];
-
-const _resolveTokenQueue = function (res: any) {
-    _tokenQueue.pending = false;
-    uni.hideLoading();
-    if (res.errMsg) {
-        console.log(res);
-        uni.showModal({
-            title: "登录失败",
-            content: `${res.errMsg}`,
-        });
-        _tokenQueue.list.forEach((item) => {
-            item.reject(res);
-        });
-    } else {
-        uni.showToast({
-            icon: "success",
-            title: "登录成功",
-        });
-        setToken(res as string);
-        _onTokenListener.forEach((item) => {
-            item.cb(res as string);
-        });
-        _tokenQueue.list.forEach((item) => {
-            item.resolve(res as string);
-        });
-    }
-    _onTokenListener = [];
-    _tokenQueue.list = [];
-};
+// const _tokenQueue: {
+//     pending: boolean;
+//     list: {
+//         resolve: (value: string) => void;
+//         reject: (err: any) => void;
+//     }[];
+// } = {
+//     pending: false,
+//     list: [],
+// };
+//
+// let _onTokenListener: {
+//     cb: (token: string) => void;
+// }[] = [];
+//
+// const _resolveTokenQueue = function (res: any) {
+//     _tokenQueue.pending = false;
+//     uni.hideLoading();
+//     if (res.errMsg) {
+//         console.log(res);
+//         uni.showModal({
+//             title: "登录失败",
+//             content: `${res.errMsg}`,
+//         });
+//         _tokenQueue.list.forEach((item) => {
+//             item.reject(res);
+//         });
+//     } else {
+//         uni.showToast({
+//             icon: "success",
+//             title: "登录成功",
+//         });
+//         setToken(res as string);
+//         _onTokenListener.forEach((item) => {
+//             item.cb(res as string);
+//         });
+//         _tokenQueue.list.forEach((item) => {
+//             item.resolve(res as string);
+//         });
+//     }
+//     _onTokenListener = [];
+//     _tokenQueue.list = [];
+// };
 
 export function login(e: any): Promise<string> {
     return new Promise((resolve, reject) => {
         if (/deny|cancel/.test(e.errMsg)) {
-            reject();
+            reject(e.errMsg);
             return;
         }
         if (!e.code) {
             uni.showModal({
                 title: `${e.errMsg},请重试`,
             });
-            reject();
-            return;
-        }
-        _tokenQueue.list.push({
-            resolve,
-            reject,
-        });
-        if (_tokenQueue.pending) {
+            reject(e);
             return;
         }
-        _tokenQueue.pending = true;
+        // _tokenQueue.list.push({
+        //     resolve,
+        //     reject,
+        // });
+        // if (_tokenQueue.pending) {
+        //     return;
+        // }
+        // _tokenQueue.pending = true;
         uni.showLoading({
             title: "登录中",
             mask: true,
@@ -77,32 +77,52 @@ export function login(e: any): Promise<string> {
                     avatar: "",
                     nickname: "",
                 }
-                body(`/user/wxLogin`, data).then((res: any) => {
-                    const {satoken, userId} = res;
+                body(`/user/wxLogin`, data).then(async (res: any) => {
+                    let {satoken, userId} = res;
                     if (satoken) {
-                        _resolveTokenQueue(satoken);
+                        getApp<any>().globalData.user.id=userId
+                        // _resolveTokenQueue(satoken);
                         setToken(satoken);
-                        loadUserInfo();
-
+                        await loadUserInfo();
+                        resolve(satoken)
+                        uni.hideLoading();
                     } else {
-                        _resolveTokenQueue({
-                            errMsg: `${JSON.stringify(res)}`,
+                        uni.showModal({
+                            title: `登录失败`,
+                            icon: 'none',
+                            duration:2000
                         });
+                        uni.hideLoading();
+                        // _resolveTokenQueue({
+                        //     errMsg: `${JSON.stringify(res)}`,
+                        // });
                     }
                 }).catch(e => {
-                    _resolveTokenQueue({})
+                    uni.showModal({
+                        title: `${e}`,
+                        icon: 'none',
+                        duration:2000
+                    });
+                    uni.hideLoading();
                 })
             },
-            fail: _resolveTokenQueue,
+            fail: ()=>{
+                uni.showModal({
+                    title: `登录失败`,
+                    icon: 'none',
+                    duration:2000
+                });
+                uni.hideLoading();
+            },
         });
     });
 }
 
-export  function tryLogin(){
+export function tryLogin() {
     return new Promise(((resolve, reject) => {
         let ftoken = fetchToken();
-        console.log("ftoken",ftoken)
-        if(ftoken){
+        console.log("ftoken", ftoken)
+        if (ftoken) {
             resolve(ftoken)
             return;
         }
@@ -114,11 +134,12 @@ export  function tryLogin(){
                     avatar: "",
                     nickname: "",
                 }
-                body(`/user/wxLogin`, data).then((res: any) => {
+                body(`/user/wxLogin`, data).then(async (res: any) => {
                     const {satoken, userId} = res;
                     if (satoken) {
+                        getApp<any>().globalData.user.id=userId
                         setToken(satoken);
-                        loadUserInfo();
+                        await loadUserInfo();
                         resolve(satoken)
                     } else {
                         console.error("tryLogin")
@@ -153,7 +174,7 @@ export function checkLogin() {
     })
 }
 
-export function loadUserInfo() {
+export async function loadUserInfo() {
     getApp<any>().globalData.manualLogout = false;
     get(`/user/me`).then((res: any) => {
         getApp<any>().globalData.user = res;
@@ -203,17 +224,23 @@ export function refresh(): Promise<string> {
     });
 }
 
-export function onLogin(cb: (token: string) => void) {
-    _onTokenListener.push({
-        cb,
-    });
-}
 
 export function fetchToken() {
-    return uni.getStorageSync("token");
+    let tk = uni.getStorageSync("token");
+    console.log(tk)
+    if(tk.length>16){
+        return tk;
+    }
+    return null;
 }
 
 export function setToken(token: string) {
+    if(!token){
+        console.error("empty token")
+        return;
+    }else{
+        console.log("set token",token)
+    }
     getApp<any>().globalData.token = token;
     getApp<any>().globalData.isLogin = true;
     uni.setStorageSync("token", token)

+ 22 - 42
src/utils/https.ts

@@ -1,20 +1,10 @@
 // @ts-ignore
 import {fetchToken} from "@/utils/auth"
-declare const process: any;
-declare const __wxConfig: any;
-
-let isDevelopment = false;
-// #ifdef MP-WEIXIN
-const e =
-    typeof __wxConfig !== "undefined"
-        ? __wxConfig.envVersion || "release"
-        : "release";
-console.log("env", e);
-isDevelopment = e === "develop" || e === "trial";
-// #endif
+
+let isDevelopment = (process.env.NODE_ENV === 'development')
 
 let env: string = isDevelopment ? "dev" : "prd";
-const apis:any = {
+const apis: any = {
     dev: {
         serverUrl: "https://dev-wash.kuaiyuman.cn/api",
         fileUrl: "https://zyp-1258963180.cos.ap-guangzhou.myqcloud.com/"
@@ -38,11 +28,11 @@ const cfg = {
     }
 };
 
-const serverUrl = cfg.api.serverUrl;
-const fileUrl = cfg.api.fileUrl;
+const serverUrl = apis[env]?.serverUrl;
+const fileUrl = apis[env].fileUrl;
 
 const isEmptyOrNull = function (exp: any) {
-    return !exp || typeof (exp) == "undefined" || exp.length === 0 || exp === '' || Object.keys(exp).length===0;
+    return !exp || typeof (exp) == "undefined" || exp.length === 0 || exp === '' || Object.keys(exp).length === 0;
 };
 
 
@@ -111,6 +101,7 @@ const body = (url: string, param = {}) => {
 
 
 const request = (options: any) => {
+    console.log("url>>>>",options.url)
     return new Promise((resolve, reject) => {
         uni.request({
             url: options.url,
@@ -118,40 +109,29 @@ const request = (options: any) => {
             method: options.method,
             header: options.header,
             dataType: options.dataType
-        }).then((res:any) => {
+        }).then((res: any) => {
             // @ts-ignore
             let response = res.data;
             if (response.code !== 200) {
-                if (response.code == 92213 || response.code == 92305) {
-                    uni.showToast({
-                        title: response.msg,
-                        icon: 'none'
-                    });
-                    setTimeout(() => {
-                        uni.navigateTo({
-                            url: `/pages/login/login`
-                        })
-                    }, 300)
-
-                } else {
-                    let errMsg = response.message || '网络异常,请稍后重试';
-                    uni.showToast({
-                        title: errMsg,
-                        icon: 'none'
-                    });
-                    reject(errMsg);
-                }
+                console.error("error=>", options.url, response);
+                let errMsg = response.message || '服务异常,请稍后重试';
+                uni.showToast({
+                    title: errMsg,
+                    icon: 'error',
+                    duration:5000
+                });
+                reject(errMsg);
             } else {
                 resolve(response.data);
             }
         }).catch(error => {
             uni.hideLoading();
             uni.showToast({
-                title: '网络异常,请稍后重试',
+                title: error,
                 icon: 'none'
             });
             console.error("error=>", error);
-            reject(error.msg);
+            reject(error);
         })
     });
 };
@@ -162,17 +142,17 @@ const upload = (url: string, param: any = {}) => {
     return new Promise((resolve, reject) => {
         uni.uploadFile({
             url: fillUrl(url),
-            filePath: param.filePath||param.url,
+            filePath: param.filePath || param.url,
             name: 'file',
             header: {
-                "satoken":token
+                "satoken": token
             },
             success: function (res) {
                 console.log("upload file=>", res)
                 let response = JSON.parse(res.data);
                 if (response.code === 200) {
                     resolve(response);
-                }else if (response.code === 500) {
+                } else if (response.code === 500) {
                     uni.showToast({
                         title: '系统繁忙,请稍后重试',
                         icon: 'none'
@@ -245,5 +225,5 @@ const formatUrl = (v: string) => {
 };
 
 export {
-    get,post, body, upload, uploadV2, cfg, serverUrl, fileUrl, formatUrl
+    get, post, body, upload, uploadV2, cfg, serverUrl, fileUrl, formatUrl
 }