Explorar el Código

feat: 增加退出登录

xyh hace 2 años
padre
commit
10dfaf858b
Se han modificado 4 ficheros con 47 adiciones y 22 borrados
  1. 18 15
      src/pages/index/hooks.js
  2. 22 4
      src/pages/index/index.jsx
  3. 6 2
      src/stores/user.js
  4. 1 1
      src/utils/constants.js

+ 18 - 15
src/pages/index/hooks.js

@@ -12,23 +12,26 @@ import {
 import {useStore} from 'zustand';
 import {userStore} from '@stores';
 import {useEffect, useState} from 'react';
+import {klona} from 'klona/json';
+
+const defaultBtnList = [
+  {
+    title: '收发货',
+    children: [
+      {title: '发货', icon: sendIcon, url: DELIVER_GOODS_PATH},
+      {title: '收货', icon: complateIcon, url: RECEIVE_GOODS_PATH},
+    ],
+  },
+  {
+    title: '查询',
+    children: [],
+  },
+];
 
 export function useBtnList() {
   const {power} = useStore(userStore);
 
-  const [btnList, setBtnList] = useState([
-    {
-      title: '收发货',
-      children: [
-        {title: '发货', icon: sendIcon, url: DELIVER_GOODS_PATH},
-        {title: '收货', icon: complateIcon, url: RECEIVE_GOODS_PATH},
-      ],
-    },
-    {
-      title: '查询',
-      children: [],
-    },
-  ]);
+  const [btnList, setBtnList] = useState(klona(defaultBtnList));
 
   useEffect(
     function () {
@@ -37,8 +40,8 @@ export function useBtnList() {
         anomalyContext = /**    */ 0x000010,
         customerContext = /**   */ 0x000100;
 
-      setBtnList(function (prev) {
-        const next = [...prev];
+      setBtnList(function () {
+        const next = klona(defaultBtnList);
 
         powerContext & sendContext &&
           next[1].children.push({

+ 22 - 4
src/pages/index/index.jsx

@@ -1,4 +1,4 @@
-import {View, Image, Text} from '@tarojs/components';
+import {View, Image, Text, Button} from '@tarojs/components';
 import face from '@assets/face.svg';
 import classNames from 'classnames';
 import {useStore} from 'zustand';
@@ -8,11 +8,22 @@ import {ORDER_PATH} from '@routes';
 import {useNavigate} from '@hooks';
 import {useBtnList} from './hooks';
 import {userStore} from '@stores';
+import {showModal} from '@tarojs/taro';
 
 export default function App() {
+  const btnList = useBtnList();
   const [visible, {toggle, setFalse}] = useBoolean();
   const {navigateWithLogin, navigate} = useNavigate();
-  const {userName} = useStore(userStore);
+  const {userName, token, logout} = useStore(userStore);
+
+  function onLogout() {
+    showModal({
+      title: '你确定要退出登录吗?',
+      success({confirm}) {
+        confirm && logout();
+      },
+    });
+  }
 
   function navigateTo(name, needLogin) {
     return function () {
@@ -20,8 +31,6 @@ export default function App() {
     };
   }
 
-  const btnList = useBtnList();
-
   return (
     <>
       <View
@@ -84,6 +93,15 @@ export default function App() {
             </View>
           );
         })}
+
+        {token ? (
+          <Button
+            onClick={onLogout}
+            className='mt-10 text-sm bg-primary h-10 text-white leading-10 rounded-full'
+          >
+            退出登录
+          </Button>
+        ) : null}
       </View>
 
       <Login visible={visible} onClose={setFalse} />

+ 6 - 2
src/stores/user.js

@@ -1,4 +1,4 @@
-import {getStorageSync} from '@tarojs/taro';
+import {removeStorageSync, getStorageSync} from '@tarojs/taro';
 import {USER_TOKEN_STORAGE} from '@utils';
 import {createStore} from 'zustand';
 
@@ -17,7 +17,11 @@ export const userStore = createStore(function (set) {
     userName,
     power,
     init(data) {
-      set(data, true);
+      set(data);
+    },
+    logout() {
+      set({token: '', userName: '', power: '000000'});
+      removeStorageSync(USER_TOKEN_STORAGE);
     },
   };
 });

+ 1 - 1
src/utils/constants.js

@@ -1,7 +1,7 @@
 /** 请求地址 */
 export const NETWORK_URL =
   process.env.NODE_ENV === 'development'
-    ? 'http://192.168.0.147:8090'
+    ? 'https://tms.tuyatrip.com'
     : 'https://tms.tuyatrip.com';
 /** token存储 */
 export const USER_TOKEN_STORAGE = 'userToken';