🎈 perf(登录页面): 完善登陆页面
This commit is contained in:
parent
0ef88a4490
commit
b328419b96
10
src/app.tsx
10
src/app.tsx
@ -6,8 +6,14 @@ import './app.scss'
|
||||
import Taro, { onAppShow, useDidShow } from '@tarojs/taro'
|
||||
import { shareShop } from './common/util'
|
||||
|
||||
type ParamsType = {
|
||||
children?: React.ReactNode
|
||||
}
|
||||
|
||||
const store = configStore()
|
||||
const App: FC = (params) => {
|
||||
const App: FC<ParamsType> = (params) => {
|
||||
const { children } = params
|
||||
|
||||
Taro.showShareMenu({
|
||||
withShareTicket: true,
|
||||
})
|
||||
@ -41,7 +47,7 @@ const App: FC = (params) => {
|
||||
|
||||
return (
|
||||
<ContextBlueTooth>
|
||||
<Provider store={store}>{params.children}</Provider>
|
||||
<Provider store={store}>{children}</Provider>
|
||||
</ContextBlueTooth>
|
||||
)
|
||||
}
|
||||
|
@ -76,6 +76,7 @@ const Login: FC = () => {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<View className={styles.login}>
|
||||
<Image className='full-100' mode='widthFix' src='https://s1.ax1x.com/2022/09/08/vqiVZF.png'></Image>
|
||||
|
@ -140,7 +140,7 @@ const UserInfo: FC = () => {
|
||||
</View>
|
||||
<View className={styles.bottomBar}>
|
||||
<Tag type='primary' size='normal' circle customStyle={{ marginRight: '10px' }}>
|
||||
IT部门
|
||||
{userInfo.userInfo.department_name}
|
||||
</Tag>
|
||||
<Divider direction='vertical'></Divider>
|
||||
<Text className={styles.userTitle}>IT-开发总监</Text>
|
||||
|
@ -181,7 +181,14 @@ export const useRequest = (
|
||||
removeToken()
|
||||
// removeSessionKey()
|
||||
removeUserInfo()
|
||||
login()
|
||||
// 跳转回登录页面
|
||||
login().catch((err)=>{
|
||||
if (err){
|
||||
Taro.reLaunch({
|
||||
url: '/pages/login/index',
|
||||
})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title: `错误:${showStatus(statusCode)}`,
|
||||
|
@ -1,113 +1,108 @@
|
||||
import {BASE_URL, WX_APPID } from "@/common/constant"
|
||||
import Taro, { useRouter } from "@tarojs/taro"
|
||||
import { useRef, useState } from "react"
|
||||
import qs from 'qs'
|
||||
import useUserInfo from "./useUserInfo"
|
||||
import useLogin from "./useLogin"
|
||||
import { BASE_URL, WX_APPID } from '@/common/constant'
|
||||
import Taro, { useRouter } from '@tarojs/taro'
|
||||
import { useRef, useState } from 'react'
|
||||
import qs from 'qs'
|
||||
import useUserInfo from './useUserInfo'
|
||||
import useLogin from './useLogin'
|
||||
|
||||
//开这个hook 主要是为了让useHttp 能够调用
|
||||
|
||||
type Param = {success: true|false, data: any, msg: string, code: null|number, loading: false|true}
|
||||
type Param = { success: boolean; data: any; msg: string; code: null | number; loading: boolean }
|
||||
let loginStatus = false //登录状态,true登录中
|
||||
export default () => {
|
||||
|
||||
let initData = {
|
||||
success: false,
|
||||
data: null,
|
||||
msg: '',
|
||||
code: null,
|
||||
loading: false
|
||||
}
|
||||
let loginData = useRef<Param>(initData)
|
||||
const {setToken, setSessionKey} = useUserInfo()
|
||||
const router = useRouter()
|
||||
//微信登录请求v2
|
||||
const fetchDataLogin = async (login_code) => {
|
||||
const q = {
|
||||
url: BASE_URL + '/v1/mall/login',
|
||||
header: {
|
||||
"Platform": 3,
|
||||
"Appid": WX_APPID,
|
||||
},
|
||||
method: 'post',
|
||||
data:{js_code: login_code}
|
||||
}
|
||||
try{
|
||||
const result = await Taro.request(q as any)
|
||||
const {code, data, msg} = result.data
|
||||
if(result.statusCode === 200) {
|
||||
loginData.current.success = (code === 0 ? true : false)
|
||||
loginData.current.code = code
|
||||
loginData.current.msg = msg
|
||||
loginData.current.data = data
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title: `错误:登录失败`,
|
||||
icon: 'none'
|
||||
})
|
||||
console.log('登录错误:', result.errMsg)
|
||||
|
||||
}
|
||||
}catch(e) {
|
||||
Taro.showToast({
|
||||
title: `错误:登录失败`,
|
||||
icon: 'none'
|
||||
let initData = {
|
||||
success: false,
|
||||
data: null,
|
||||
msg: '',
|
||||
code: null,
|
||||
loading: false,
|
||||
}
|
||||
let loginData = useRef<Param>(initData)
|
||||
const { setToken, setSessionKey } = useUserInfo()
|
||||
const router = useRouter()
|
||||
//微信登录请求v2
|
||||
const fetchDataLogin = async (login_code) => {
|
||||
const q = {
|
||||
url: BASE_URL + '/v1/mall/login',
|
||||
header: {
|
||||
Platform: 3,
|
||||
Appid: WX_APPID,
|
||||
},
|
||||
method: 'post',
|
||||
data: { js_code: login_code },
|
||||
}
|
||||
try {
|
||||
const result = await Taro.request(q as any)
|
||||
const { code, data, msg } = result.data
|
||||
if (result.statusCode === 200) {
|
||||
loginData.current.success = code === 0 ? true : false
|
||||
loginData.current.code = code
|
||||
loginData.current.msg = msg
|
||||
loginData.current.data = data
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title: `错误:登录失败`,
|
||||
icon: 'none',
|
||||
})
|
||||
console.log('登录错误:', result.errMsg)
|
||||
}
|
||||
} catch (e) {
|
||||
Taro.showToast({
|
||||
title: `错误:登录失败`,
|
||||
icon: 'none',
|
||||
})
|
||||
console.log('登录错误:', e.errMsg)
|
||||
}
|
||||
return loginData.current
|
||||
}
|
||||
|
||||
//微信登录
|
||||
const login = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (loginStatus) return reject(false)
|
||||
loginStatus = true
|
||||
Taro.login({
|
||||
success: async (res) => {
|
||||
if (res.code) {
|
||||
const { data, success, msg } = await fetchDataLogin(res.code)
|
||||
if (success) {
|
||||
setToken(data.token)
|
||||
setSessionKey(data.session_key)
|
||||
resolve(data)
|
||||
let params = router.params
|
||||
delete params.$taroTimestamp
|
||||
if (router.path === '/pages/index/index' || router.path === '/pages/user/index') {
|
||||
Taro.reLaunch({
|
||||
url: router.path + '?' + qs.stringify(params),
|
||||
})
|
||||
console.log('登录错误:', e.errMsg)
|
||||
}
|
||||
return loginData.current
|
||||
}
|
||||
|
||||
//微信登录
|
||||
const login = () => {
|
||||
if(loginStatus) return false
|
||||
loginStatus = true
|
||||
return new Promise((reslove, reject) => {
|
||||
Taro.login({
|
||||
success: async (res) => {
|
||||
if (res.code) {
|
||||
const {data, success, msg} = await fetchDataLogin(res.code)
|
||||
if(success) {
|
||||
setToken(data.token)
|
||||
setSessionKey(data.session_key)
|
||||
reslove(data)
|
||||
let params = router.params
|
||||
delete params.$taroTimestamp
|
||||
if(router.path === '/pages/index/index' || router.path === '/pages/user/index') {
|
||||
Taro.reLaunch({
|
||||
url: router.path +'?' + qs.stringify(params)
|
||||
})
|
||||
} else {
|
||||
Taro.redirectTo({
|
||||
url: router.path +'?' + qs.stringify(params)
|
||||
})
|
||||
}
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title:'登录失败',
|
||||
icon:"none"
|
||||
})
|
||||
reject(msg)
|
||||
}
|
||||
} else {
|
||||
console.log('登录失败!' + res.errMsg)
|
||||
reject(res.errMsg)
|
||||
}
|
||||
loginStatus = false
|
||||
},
|
||||
fail: function(e) {
|
||||
console.log('登录失败!::',e)
|
||||
reject(e)
|
||||
loginStatus = false
|
||||
}
|
||||
} else {
|
||||
Taro.redirectTo({
|
||||
url: router.path + '?' + qs.stringify(params),
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return {
|
||||
login
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title: '登录失败',
|
||||
icon: 'none',
|
||||
})
|
||||
reject(msg)
|
||||
}
|
||||
} else {
|
||||
console.log('登录失败!' + res.errMsg)
|
||||
reject(res.errMsg)
|
||||
}
|
||||
loginStatus = false
|
||||
},
|
||||
fail: function (e) {
|
||||
console.log('登录失败!::', e)
|
||||
reject(e)
|
||||
loginStatus = false
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
login,
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user