From e7a130533d43d36b4d774427d8abc4e7b80484c3 Mon Sep 17 00:00:00 2001 From: czm <2192718639@qq.com> Date: Mon, 25 Apr 2022 19:02:10 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=B0=81=E8=A3=85=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/client.js | 17 ++ src/common/fotmat.js | 135 ++++++++++++++++ src/common/system.js | 34 ++++ src/common/uploadImage.js | 150 ++++++++++++++++++ src/common/user.js | 36 +++++ src/common/util.js | 94 +++++++++++ src/components/search/index.tsx | 27 +++- src/constants/userInfo.ts | 8 +- .../components/preview/index.module.scss | 2 +- src/pages/details/index.tsx | 6 +- src/pages/index/index.module.scss | 3 + src/pages/index/index.tsx | 24 +-- .../order/components/remark/index.module.scss | 3 +- src/pages/order/components/remark/index.tsx | 12 +- src/pages/search/index.tsx | 2 +- src/reducers/userInfo.ts | 60 +++++-- src/use/useHttp.ts | 8 +- src/use/useLogin.tsx | 12 +- src/use/useUserInfo.ts | 41 +++++ 19 files changed, 604 insertions(+), 70 deletions(-) create mode 100644 src/common/client.js create mode 100644 src/common/fotmat.js create mode 100644 src/common/system.js create mode 100644 src/common/uploadImage.js create mode 100644 src/common/user.js create mode 100644 src/common/util.js create mode 100644 src/use/useUserInfo.ts diff --git a/src/common/client.js b/src/common/client.js new file mode 100644 index 0000000..0aefa83 --- /dev/null +++ b/src/common/client.js @@ -0,0 +1,17 @@ +import Taro from "@tarojs/taro"; + + +/** + * 设置 客户 本地存储 + * @param {Object} clientInfo + */ +export const setClient = (clientInfo) => { + Taro.setStorageSync('client', clientInfo) +} + +/** + * 返回 客户 本地存储 + */ +export const getClient = () => { + Taro.getStorageSync('client') || null +} diff --git a/src/common/fotmat.js b/src/common/fotmat.js new file mode 100644 index 0000000..8bef05c --- /dev/null +++ b/src/common/fotmat.js @@ -0,0 +1,135 @@ +/** + * 移除井号 + * @param {String} val code 编码 + * @returns + */ +export const formatRemoveHashTag = (val = "") => { + // console.log('移除标签',val,val.endsWith("#")); + return val.endsWith("#") ? val.replace("#", "") : val; +}; + + +/** + * 格式化编码+名称显示方式 + * @param {String} code 编码 + * @param {String} name 名称 + * @param {*} mode 模式 both:code + 名称 name: 仅显示名称 + * @returns + */ +export const formatHashTag = (code = "", name = "", mode = "both") => { + if (mode == 'both') { + return `${formatRemoveHashTag(code)}# ${name}` + } else if (mode == 'name') { + return `${name}` + } +} + +const Digit = 10 * 10 + +/** + * 重量 进退位 单位 + */ +export const weightDigit = 1000 + +/** + * 除以 + * @param {*} val + * @param {*} digit + * @returns + */ +export const formatPriceDiv = (val, digit = Digit) => { + return strip(Number(val / digit)) || 0 +} +/** + * 乘以 + * @param {*} val + * @param {*} digit + * @returns + */ +export const formatPriceMul = (val, digit = Digit) => { + return strip(Number(val * digit)) || 0 +} + +/** + * 格式化重量单位 (乘以) + * @param {Number} val + * @returns + */ +export const formatWeightMul = (val, digit = weightDigit) => { + return strip(Number(val * digit)) || 0 +} + +/** + * 格式化重量单位 (除以) + * @param {*} val + */ +export const formatWeightDiv = (val, digit = weightDigit) => { + return strip(Number(val / digit)) || 0 +} + +export const formatDateTime = (val, fmt = "YYYY-MM-DD HH:mm:ss") => { + if (val) { + let time = new Date(val) + let Y = time.getFullYear() + let M = time.getMonth() + 1 + let d = time.getDate() + let h = time.getHours() + let m = time.getMinutes() + let s = time.getSeconds() + + fmt = fmt.replace('YYYY', Y).replace('MM', M.toString().padStart(2, '0')).replace('DD', d.toString().padStart(2, '0')).replace('HH', h.toString().padStart(2, '0')).replace('mm', m.toString().padStart(2, '0')).replace('ss', s.toString().padStart(2, '0')) + // fmt = fmt.replace('MM', M) + // fmt = fmt.replace('DD', d) + // fmt = fmt.replace('HH', h) + // fmt = fmt.replace('mm', m) + // fmt = fmt.replace('ss', s) + + return fmt + } else { + return val + } + +} + +/** + * 精度 + * @param {*} num + * @param {*} precision + * @returns + */ +export const strip = (num, precision = 12) => { + return +parseFloat(num.toPrecision(precision)); +} + +/** + * 转换金额单位 + * @param {*} num 金额 / 数值 + * @param {*} digit 转换单位 + * @returns + */ +export const formatMillionYuan = (num, digit = 10000) => { + return num / digit > 1 ? { num: toDecimal2(num / digit), million: true } : { num, million: false } +} + +/** + * 数值保留两位小数 + * @param {*} x + * @returns + */ +export const toDecimal2 = (x) => { + var f = parseFloat(x); + if (isNaN(f)) { + return 0; + } + f = f + ""; + let index = f.lastIndexOf('.'); + if (index >= 0) { + let decimal = f.substring(index + 1); + if (decimal.length == 1) { + f = f.substring(0, index + 1) + decimal + "0"; + } else { + f = f.substring(0, index + 1) + decimal.substring(0, 2); + } + } + return f; +} diff --git a/src/common/system.js b/src/common/system.js new file mode 100644 index 0000000..12e2036 --- /dev/null +++ b/src/common/system.js @@ -0,0 +1,34 @@ +import Taro from "@tarojs/taro"; + + +/** + * 设置 系统 本地存储 + * @param {Object} systemInfo + */ +export const setSystem = (systemInfo) => { + Taro.setStorageSync('system', JSON.stringify(systemInfo)) +} + +/** + * 返回 系统 本地存储 + */ +export const getSystem = () => { + const result = Taro.getStorageSync('system') + return result ? JSON.parse(result) : null +} + +/** + * 设置 小程序 本地存储 + * @param {Object} systemInfo + */ +export const setAccountInfo = (systemInfo) => { + Taro.setStorageSync('accountInfo', JSON.stringify(systemInfo)) +} + +/** + * 返回 系统 本地存储 + */ +export const getAccountInfo = () => { + const result = Taro.getStorageSync('accountInfo') + return result ? JSON.parse(result) : null +} diff --git a/src/common/uploadImage.js b/src/common/uploadImage.js new file mode 100644 index 0000000..6a6cf8a --- /dev/null +++ b/src/common/uploadImage.js @@ -0,0 +1,150 @@ +import Taro from '@tarojs/taro'; +import { GET_UPLOAD_SIGN, CDN_UPLOAD_IMG, UPLOAD_CDN_URL } from './constant' + +import { GetSignApi } from '@/api/cdn' + + +const { fetchData: GetSign, success, data: resData, msg, code } = GetSignApi() + + +// 上传图片 获取auth,Policy +/* + scene 场景值,区分上传文件的根路径 + type 类型值,区分上传业务bucket +*/ +const getSecret = (scene, type) => { + return new Promise(async (resolve, reject) => { + + const SAVE_PATH = `/${scene}/{filemd5}{day}{hour}{min}{sec}{.suffix}`; + + + let params = { + 'method': 'post', + 'save_key': SAVE_PATH + } + + // 获取签名 + await GetSign(params) + if (success.value) { + // console.log('返回签名',resData.value); + resolve(resData.value) + } else { + reject({ + code: code.value || '9999', + msg: msg.value + }); + } + + }) +} +const getFileType = (name) => { + if (!name) return false; + var imgType = ["gif", "jpeg", "jpg", "bmp", "png"]; + var videoType = ["avi", "wmv", "mkv", "mp4", "mov", "rm", "3gp", "flv", "mpg", "rmvb", "quicktime"]; + + if (RegExp("\.?(" + imgType.join("|") + ")$", "i").test(name.toLowerCase())) { + return 'image'; + } else if (RegExp("\.(" + videoType.join("|") + ")$", "i").test(name.toLowerCase())) { + return 'video'; + } else { + return false; + } +} + +const upYunbucket = (type) => { + var bucket = "" + switch (type) { + case "product": + bucket = "testzzfzyc" + break; + } + return bucket +} + + +/** + * + * @param {*} file 传入文件 + * @param {String} secene 传入 'product' + * @param {String} type 传入 'product' + * @returns + */ +const uploadCDNImg = (file, secene, type) => { + // var file = event.target.files[0]; + // var filetype = file.type + let filetype = file.tempFilePath + + if (!getFileType(filetype)) { + Taro.showToast({ + title: "上传文件类型错误", + icon: "none", + duration: 3800 + }) + return false + } + return new Promise((resolve, reject, race) => { + getSecret(secene, type) + .then(result => { + + console.log('bucket', result.bucket); + var formdata = { + 'authorization': result.authorization, + 'policy': result.policy, + // "file": file.tempFilePath, + } + + const uploadTask = Taro.uploadFile({ + url: `${UPLOAD_CDN_URL}${result.bucket}`, + formData: formdata, + filePath: file.tempFilePath, + name: 'file', + success: res => { + resolve(JSON.parse(`${res.data}`)) + }, + fail: err => { + console.log(err) + reject(err) + } + }) + + uploadTask.progress(res => { + console.log('上传进度', res.progress); + if (res.progress < 100) { + Taro.showLoading({ + title: '上传中...' + }) + } else { + Taro.hideLoading() + } + }) + }) + .catch(result => { + reject(result) + Taro.showToast({ + title: "获取密钥失败!", + icon: "none", + duration: 3800 + }) + }) + }) +} + +const taroChooseImg = () => { + Taro.chooseImage({ + count: 1, + sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 + sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 + success: (res) => { + console.log('res:', res) + Taro.chooseMessageFile({ + count: 1, + + }) + }, + fail: (err) => { + console.log('图片选择失败:', err) + } + }) +} + +export default uploadCDNImg \ No newline at end of file diff --git a/src/common/user.js b/src/common/user.js new file mode 100644 index 0000000..aabdbfc --- /dev/null +++ b/src/common/user.js @@ -0,0 +1,36 @@ +import Taro from "@tarojs/taro"; +import { useStore } from "vuex"; +import { computed } from "@vue/runtime-core"; + +/** + * + * @param {String} token + */ +export const setToken = (token) => { + Taro.setStorageSync('token', token) +} +/** + * + * @param {Object} userinfo + */ +export const setUserInfo = (userinfo) => { + Taro.setStorageSync('userInfo', userinfo) +} + + +/** + * 检查登录 + */ +export const checkLogin = () => { + const store = useStore() + const token = computed(() => store.state.token) + console.log('checklogin token', token); + + // 本地调试屏蔽 + if (token == '') { + Taro.redirectTo({ + url: '/pages/login/index', + }) + } + +} \ No newline at end of file diff --git a/src/common/util.js b/src/common/util.js new file mode 100644 index 0000000..323856d --- /dev/null +++ b/src/common/util.js @@ -0,0 +1,94 @@ +/** + * 防抖 + * @param {*} fn + * @param {*} delay + * @returns + */ +export const debounce = (fn, delay) => { + let timer = null; + return (...param) => { + if (timer) clearTimeout(timer); + timer = setTimeout(() => { + fn(...param); + }, delay); + }; +} + +/** + * 节流 + * @param {*} fn + * @param {*} delay + * @returns + */ +export const throttle = (fn, delay) => { + let pre = 0; + return (...params) => { + let now = new Date().getTime(); + console.log('相差:',now-pre) + if (now - pre > delay) { + fn(...params); + pre = now; + } + }; +} + + +/** + * 批量过滤对象值为空的属性 + * @param {Object} val 需要过滤的对象 + * @param {Array} arr 排除过滤的属性 + * @returns + */ + export const getFilterData = (val = {}, arr = []) => { + let res = {} + for(let key in val) { + if(val[key]!=undefined&&val[key]!=null&&(!arr.includes(key))){ + if(val[key] instanceof Number){ + console.log("+++",val[key]); + if(!isNaN(val[key])) { + res[key] = val[key] + } + }else{ + res[key] = val[key] + } + } + } + return res +} +/** + * 对象深拷贝 + * @param {*} object + * @returns + */ + export const copyObject = (object)=>{ + if(object.constructor==Object){ + let keys = Object.keys(object); + let newObject = {}; + keys.map(key=>{ + newObject[key]= copyObject(object[key]); + }) + return newObject; + }else if(object.constructor==Array){ + return object.map(item=>{ + return copyObject(item); + }) + }else{ + return object; + } +} + +/** + * + * @param {*} suffix + * !w80 + !w100 + !w160 + !w200 + !w400 + !w800 + !wh400 + !w600 + */ + export const screenshot = (url, suffix="!w200")=>{ + return url+suffix; + } \ No newline at end of file diff --git a/src/components/search/index.tsx b/src/components/search/index.tsx index 6f539d9..75b5958 100644 --- a/src/components/search/index.tsx +++ b/src/components/search/index.tsx @@ -2,7 +2,8 @@ import { Input, View } from "@tarojs/components"; import styles from "./index.module.scss" import CloseBtn from "@/components/closeBtn" import classnames from "classnames"; -import { memo, useEffect, useState } from "react"; +import { debounce } from "@/common/util"; +import { memo, useEffect, useRef, useState } from "react"; type Params = { clickOnSearch?: (val: string) => void @@ -14,7 +15,8 @@ type Params = { style?: Object, showBtn?: false|true, btnStyle?: Object, - btnTitle?: string + btnTitle?: string, + debounceTime?: number //防抖时间,不设默认wei'ling } export default memo(({ @@ -24,17 +26,21 @@ export default memo(({ placeholder = '输入搜索内容', showIcon = true, showBtn = false, - style = {}, btnStyle = {}, placeIcon = 'inner', - btnTitle = '搜索' + btnTitle = '搜索', + debounceTime = 0 }:Params) => { const [inputCon , setInputCon] = useState('') - + const debounceTimeRef = useRef(0) + + useEffect(() => { + debounceTimeRef.current = debounceTime + }, [debounceTime]) + const onInputEven = (e) => { const value = e.detail.value - setInputCon(value) - changeOnSearch?.(value) + changeData(value) } const clearInput = () => { @@ -42,13 +48,18 @@ export default memo(({ changeOnSearch?.('') } + const changeData = debounce((value) => { + setInputCon(value) + changeOnSearch?.(value) + }, debounceTimeRef.current) + const onSearch = () => { clickOnSearch?.(inputCon) } return ( <> - onSearch()} style={style}> + clickOnSearch?.(inputCon)}> {showIcon&&} onInputEven(e)}> diff --git a/src/constants/userInfo.ts b/src/constants/userInfo.ts index 8df3b84..77585a2 100644 --- a/src/constants/userInfo.ts +++ b/src/constants/userInfo.ts @@ -1,2 +1,6 @@ -export const GETUSER = 'GETUSER' -export const SETUSER = 'SETUSER' \ No newline at end of file +export const SET_USERINFO = 'setUserInfo' +export const SET_TOKEN = 'setToken' +export const SET_SESSIONKEY = 'setSessionkey' +export const CLEAR_TOKEN = 'clearToken' +export const CLEAR_SESSIONKEY = 'clearSessionkey' +export const CLEAR_USERINFO = 'clearUserInfo' \ No newline at end of file diff --git a/src/pages/details/components/preview/index.module.scss b/src/pages/details/components/preview/index.module.scss index 5b57efc..cab4ce5 100644 --- a/src/pages/details/components/preview/index.module.scss +++ b/src/pages/details/components/preview/index.module.scss @@ -30,7 +30,7 @@ margin-top: 10px; background-color: rgba(0,0,0, 0.5); padding: 0 10px; - @include common_ellipsis + @include common_ellipsis(1); } } } \ No newline at end of file diff --git a/src/pages/details/index.tsx b/src/pages/details/index.tsx index cadff96..d4a034d 100644 --- a/src/pages/details/index.tsx +++ b/src/pages/details/index.tsx @@ -10,6 +10,7 @@ import styles from './index.module.scss' import { useEffect, useMemo, useState } from 'react'; import useManualPullDownRefresh from '@/use/useManualPullDownRefresh'; import { goLink } from '@/common/common'; +import useUserInfo from '@/use/useUserInfo'; type item = {title:string, img:string, url:string, id:number} type params = { @@ -103,10 +104,7 @@ export default (props:params) => { } }) - useDidShow(() => { - Taro.getCurrentPages() - }) - + const {user} = useUserInfo() return ( diff --git a/src/pages/index/index.module.scss b/src/pages/index/index.module.scss index 3e1e5c9..b1732f8 100644 --- a/src/pages/index/index.module.scss +++ b/src/pages/index/index.module.scss @@ -20,6 +20,9 @@ text-align: center; line-height: 44px; } + .search_input{ + width: 300px; + } } .products{ flex:1; diff --git a/src/pages/index/index.tsx b/src/pages/index/index.tsx index d0ffe2c..aea1ece 100644 --- a/src/pages/index/index.tsx +++ b/src/pages/index/index.tsx @@ -10,7 +10,7 @@ import { goLink } from '@/common/common' import { useEffect, useState } from 'react' import Taro, { useDidShow, usePullDownRefresh, useRouter } from '@tarojs/taro' import useManualPullDownRefresh from '@/use/useManualPullDownRefresh' - +import useUserInfo from '@/use/useUserInfo' export default () => { const tabs_list = [ @@ -47,31 +47,9 @@ export default () => { }, 1000) } - const goto = function (e) { - var t = this - Taro.navigateTo({ - url: '/pages/details/index', - events: { - // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据 - backFromTargetPage: function (backData) { - console.log('好啦,可以在这里写你的逻辑了', backData) - if (backData.from == 'page2') { - // 监听到 page2 返回 - console.log('好啦,可以在这里写你的逻辑了', backData) - t.setData({ - isBackFromPage2: !0 - }) - } - } - } - }) - } - return ( setShowShopCart(!showShopCart)}> - - goto(e)}>aaa 我的收藏 diff --git a/src/pages/order/components/remark/index.module.scss b/src/pages/order/components/remark/index.module.scss index 9f2b3d0..d624050 100644 --- a/src/pages/order/components/remark/index.module.scss +++ b/src/pages/order/components/remark/index.module.scss @@ -24,7 +24,7 @@ color: $color_font_two; } textarea{ - background-color: $color_bg_one; + background-color: #f3f3f3; border-radius: 10px; width: 100%; height: 313px; @@ -32,6 +32,7 @@ padding-bottom: 50px; box-sizing: border-box; font-size: $font_size; + border: 2px solid #e6e6e6; } } .order_save_address{ diff --git a/src/pages/order/components/remark/index.tsx b/src/pages/order/components/remark/index.tsx index 05f485b..404e232 100644 --- a/src/pages/order/components/remark/index.tsx +++ b/src/pages/order/components/remark/index.tsx @@ -10,13 +10,13 @@ type Param = { export default ({onBlur, onSave}:Param) => { const [descData, setDescData] = useState({ number: 0, - value: '' + value: '', + count: 10 }) - - const getDesc = useCallback((e) => { let value = e.detail.value - setDescData({...descData, number:value.length, value}) + let res = value.slice(0, descData.count) + setDescData({...descData, number:res.length, value: res}) },[]) const setSave = () => { @@ -26,8 +26,8 @@ export default ({onBlur, onSave}:Param) => { 添加备注 - - {descData.number}/200 + + {descData.number}/{descData.count} setSave()}>保存 diff --git a/src/pages/search/index.tsx b/src/pages/search/index.tsx index c24f320..d342443 100644 --- a/src/pages/search/index.tsx +++ b/src/pages/search/index.tsx @@ -10,7 +10,7 @@ export default () => { return ( - goLink('/pages/searchList/index')}/> + console.log(e)} placeholder="请输入面料关键词" placeIcon="out" showBtn={true} /> diff --git a/src/reducers/userInfo.ts b/src/reducers/userInfo.ts index e2c3f8d..5cfd21b 100644 --- a/src/reducers/userInfo.ts +++ b/src/reducers/userInfo.ts @@ -1,23 +1,51 @@ -import { GETUSER, SETUSER } from '../constants/userInfo' +import Taro from '@tarojs/taro' +import { SET_USERINFO, SET_TOKEN, SET_SESSIONKEY, CLEAR_TOKEN, CLEAR_USERINFO, CLEAR_SESSIONKEY} from '../constants/userInfo' -const INIT_USER = { - name: '张三', - phone: '110', - avatarUrl: '', - token: '', +export type UserParam = { + name?:string, + phone?:string, + avatarUrl?:string, } -export default function counter (state = INIT_USER, action) { - switch (action.type) { - case GETUSER: - return { - ...state, - } - case SETUSER: - return { - ...state, - } +type Action = { + type?: string, + data?: DataParam +} + +export type DataParam = { + token?: string + sessionkey?: string, + userInfo: UserParam +} + +const INIT_USER = { + userInfo: Taro.getStorageSync('userInfo')?JSON.parse(Taro.getStorageSync('userInfo')):null, + token: Taro.getStorageSync('token')||'', + session_key: Taro.getStorageSync('sessionkey')||'', +} + +export default function counter (state = INIT_USER, action: Action) { + const {type, data} = action + switch (type) { + case SET_USERINFO: + Taro.setStorageSync('userInfo',JSON.stringify(data?.userInfo)) + return {...state,...data} + case SET_TOKEN: + Taro.setStorageSync('token',JSON.stringify(data?.token)) + return {...state,...data} + case SET_SESSIONKEY: + Taro.setStorageSync('sessionkey',JSON.stringify(data?.sessionkey)) + return {...state,...data} + case CLEAR_TOKEN: + Taro.removeStorageSync('token') + return {...state, token:''} + case CLEAR_SESSIONKEY: + Taro.removeStorageSync('sessionkey') + return {...state, session_key:''} + case CLEAR_USERINFO: + Taro.removeStorageSync('userInfo') + return {...state, userInfo: null} default: return state } diff --git a/src/use/useHttp.ts b/src/use/useHttp.ts index c188d42..64af04d 100644 --- a/src/use/useHttp.ts +++ b/src/use/useHttp.ts @@ -193,9 +193,10 @@ export const useRequest = (options = { title: `错误:${showStatus(statusCode)}` }) if (statusCode === 401) { - Taro.reLaunch({ - url: '/pages/login/index' - }) + //未登录 + // Taro.reLaunch({ + // url: '/pages/login/index' + // }) } } @@ -209,6 +210,7 @@ export const useRequest = (options = { stateRef.current.error = false stateRef.current.loading = false setState(() => stateRef.current) + return stateRef.current } return { diff --git a/src/use/useLogin.tsx b/src/use/useLogin.tsx index 554d834..2541549 100644 --- a/src/use/useLogin.tsx +++ b/src/use/useLogin.tsx @@ -1,17 +1,17 @@ import { useEffect, useState } from "react" import { WX_APPID } from "@/common/constant" +import useUserInfo from "./useUserInfo" + export default () => { + const {setToken, setSessionKey, setUserInfo} = useUserInfo() + const [loginState, setLoginState] = useState({ session_key: '', token: '', userInfo: '' }) - useEffect(() => { - getToken() - }, []) - const getToken = () => { Taro.login({ success: function (res) { @@ -34,6 +34,8 @@ export default () => { session_key: data.session_key, token: data.token }) + setToken(data.token) + setLoginState(data.session_key) } }) } else { @@ -66,7 +68,7 @@ export default () => { Appid: WX_APPID }, success: (e) => { - console.log('123123') + setUserInfo({}) }, fail: (e) => { console.log(e) diff --git a/src/use/useUserInfo.ts b/src/use/useUserInfo.ts new file mode 100644 index 0000000..cdf67ec --- /dev/null +++ b/src/use/useUserInfo.ts @@ -0,0 +1,41 @@ +import { useDispatch, useSelector } from 'react-redux' +import { CLEAR_SESSIONKEY, SET_USERINFO, SET_TOKEN, SET_SESSIONKEY, CLEAR_USERINFO, CLEAR_TOKEN} from '@/constants/userInfo' +import {DataParam, UserParam} from '@/reducers/userInfo' +export default () => { + const user = useSelector((state:DataParam) => state.userInfo) as DataParam + const dispatch = useDispatch() + + const setToken = (token: string) => { + dispatch({type:SET_TOKEN, data:{token}}) + } + + const setSessionKey = (sessionkey: string) => { + dispatch({type:SET_SESSIONKEY, data:{sessionkey}}) + } + + const setUserInfo = (userInfo: UserParam) => { + dispatch({type:SET_USERINFO, data:{userInfo}}) + } + + const removeUserInfo = () => { + dispatch({type:CLEAR_USERINFO}) + } + + const removeToken = () => { + dispatch({type:CLEAR_TOKEN}) + } + + const removeSessionKey = () => { + dispatch({type:CLEAR_SESSIONKEY}) + } + + return { + setToken, + setUserInfo, + setSessionKey, + removeUserInfo, + removeToken, + removeSessionKey, + user, //响应式数据返回 + } +} \ No newline at end of file