Commit 24fe9cbc by lirandong

完成 登录功能

parent b1d1b9f4
// import users from '@api/users'
// import { concatParam } from '@/utils/utils'
import Taro from '@tarojs/taro'
import token from '../common/token'
import { concatParam } from '../common/utils'
// import $config, { message, requestCode, tokens } from '@/config'
import { BASE_URL, FETCH_TIME_OUT } from '@/common'
import { TOKEN_REPEAT, INTERFACE_REPEAT, LOGIN_0003 } from '@/common/msg'
import { axios, CancelTokenStatic, AxiosInstance } from 'taro-axios'
import { BASE_URL, FETCH_TIME_OUT, INTERFACE_REPEAT } from '@/common'
import token from '../common/token'
const concatParamMethod = new Set(['post', 'put'])
// let refreshTokenState = false // 刷新 token的状态
......@@ -27,7 +28,7 @@ $axios.interceptors.request.use((config: any) => {
if (!tokne) {
config.cancelToken = new CancelToken(cancel => {
console.warn(`token 为空取消请求~`)
cancel(INTERFACE_REPEAT)
cancel(TOKEN_REPEAT)
})
return config
}
......@@ -57,7 +58,6 @@ $axios.interceptors.request.use((config: any) => {
FetchSet.add(config.FetchKey) // 添加请求key到防重复容器
delete config.headers.repeat // 删除标识
}
// console.log(1111, { config })
return config
})
......@@ -68,30 +68,17 @@ $axios.interceptors.response.use(
FetchSet.delete(config.config.FetchKey)
// token过期处理
const { data } = config
// if (!data || !data.code) return Promise.reject(config)
const { code } = data
// console.log({ config })
if (code === LOGIN_0003) {
// 用户信息失效
token.reset()
Taro.navigateTo({ url: '/pages/login/index' })
}
if (!!code && code !== '0') return Promise.reject(config.data)
return data
// if (code === '0') {
// // 正确请求
// return data
// // } else if (code === requestCode.TOKNE_EXPIRED) {
// // // token失效重新请求
// // try {
// // await refreshToken()
// // return $axios.request(config) // token刷新成功重新发起请求
// // } catch (error) {
// // return Promise.reject(config)
// // }
// } else {
// // 请求出现错误
// return Promise.reject(config.data)
// }
},
async (error: any) => {
const { config, stack } = error
console.log({ stack })
const { config } = error
if (config && config.FetchKey) {
// 取消当前接口的防重复请求限制
FetchSet.delete(config.FetchKey)
......@@ -100,13 +87,4 @@ $axios.interceptors.response.use(
}
)
// async function refreshToken() {
// let refresh
// if (refreshTokenState) return refresh
// refreshTokenState = true
// refresh = await tokens.tryRefreshToken()
// refreshTokenState = false
// return refresh
// }
export default $axios
......@@ -19,7 +19,7 @@ import './app.scss'
/** 初始化 tokne */
// !(async () => {
// await token.init()
// console.log('object')
// console.log('!token.isLogon()1111', token.isLogon())
// Taro.render(<App />, document.getElementById('app'))
// })()
......@@ -35,9 +35,10 @@ class App extends Component {
*/
config: Config = {
pages: [
'pages/home/device/index',
'pages/index',
'pages/home/tempaltes/index',
// 'pages/login/index',
'pages/home/device/index',
'pages/login/index',
'pages/home/device/device_detail/index',
'pages/system/wifi_list/index',
'pages/home/device/device_bind/index',
......@@ -80,14 +81,22 @@ class App extends Component {
]
}
}
constructor(props) {
super(props)
// console.error('object')
}
componentWillMount() {
// console.log('token.isLogon()', token.isLogon())
}
// constructor(props: any) {
// super(props)
// this.initToekn()
// }
// async initToekn() {
// const toekn = await token.init()
// if (!toekn) {
// Taro.navigateTo({ url: '/pages/login/index' })
// }
// }
// componentWillMount() {
// console.log('componentWillMount')
// }
// 在 App 类中的 render() 函数没有实际作用
// 请勿修改此函数
......
export const FETCH_TIME_OUT = 5000
export const BASE_URL = 'https://server.bdideal.com'
export const INTERFACE_REPEAT = '接口重复,请求已取消'
/** 蓝牙的服务 uuid */
export const BLE_SERVICE_ID = '000088A0-0000-1000-8000-00805F9B34FB'
......
export const INTERFACE_REPEAT = '接口重复,请求已取消'
export const TOKEN_REPEAT = 'token 为空'
/** 用户信息失效 */
export const LOGIN_0003 = 'LOGIN_0003'
import { MyStorage } from './utils'
import Taro from '@tarojs/taro'
class Token {
private initDone = false
// private token = '676191a8b17442cfb06d31fdc936db6e-APP'
private token = '78b98f74f89f43aea0d393e0fae63b5d-APP'
private token = ''
// constructor() {
// this.getStorageToken()
// }
......@@ -22,7 +23,8 @@ class Token {
async setToken(token: string) {
if (!token) return
this.token = token
await MyStorage.setItem({ key: 'token', data: token })
const res = await MyStorage.setItem({ key: 'token', data: token })
console.log({ res })
}
getToken() {
......@@ -31,14 +33,18 @@ class Token {
async getStorageToken() {
try {
// const { data } = await MyStorage.getItem({ key: 'token' })
console.log('11111')
return MyStorage.getItem({ key: 'token' })
// if (!!data) this.token = data
const token = await MyStorage.getItem({ key: 'token' })
this.setToken(token)
return token
} catch (error) {
console.warn('获取本地 token 失败~', error)
}
}
reset() {
this.token = ''
MyStorage.setItem({ key: 'token', data: '' })
}
}
export default new Token()
......@@ -16,7 +16,8 @@ export const MyStorage = {
// const data = await AsyncStorage.getItem(key)
return await AsyncStorage.getItem(key)
} else {
return Taro.getStorageSync(key)
const data = Taro.getStorageSync(key)
return data
}
},
......
......@@ -46,6 +46,7 @@ class Tempaltes extends Component {
navigationBarTitleText: '',
navigationStyle: process.env.TARO_ENV === 'rn' ? 'custom' : 'default'
}
// protected page = 1
constructor(props) {
super(props)
this.state = {
......@@ -57,6 +58,7 @@ class Tempaltes extends Component {
// !token.isLogon() && Taro.navigateTo({ url: '/pages/login/index' })
}
async componentWillMount() {
console.log('componentWillMount')
this.getData()
}
......
import { ComponentClass } from 'react'
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Text, Image, ScrollView, Button } from '@tarojs/components'
import './index.scss'
import token from '@/common/token'
type PageStateProps = {}
......@@ -54,6 +56,11 @@ class User extends Component {
Taro.navigateTo({ url })
}
logOut() {
token.reset()
Taro.navigateTo({ url: '/pages/login/index.tsx' })
}
shouldComponentUpdate(_nextPorps, _nextState) {
const { nickName, avatarUrl } = this.state
const { nickName: _nickName, avatarUrl: _avatarUrl } = _nextState
......@@ -96,7 +103,7 @@ class User extends Component {
</View>
</View>
<View className="user-logout-bar">
<Button className="user-logout-bar-btn" type="primary">
<Button onClick={this.logOut} className="user-logout-bar-btn" type="primary">
退出登录
</Button>
</View>
......
import { ComponentClass } from 'react'
import { View } from '@tarojs/components'
import Taro, { Component, Config } from '@tarojs/taro'
import token from '@/common/token'
type PageStateProps = {}
type PageDispatchProps = {}
type PageOwnProps = {}
type PageState = {
name: string
}
type IProps = PageStateProps & PageDispatchProps & PageOwnProps
interface Index {
props: IProps
state: PageState
}
class Index extends Component {
config: Config = {
navigationBarTitleText: '',
navigationStyle: 'custom'
}
constructor(props) {
super(props)
this.initToekn()
}
async initToekn() {
// 初始化 用戶信息
const toekn = await token.init()
if (!toekn) {
Taro.navigateTo({ url: '/pages/login/index' })
} else {
Taro.switchTab({ url: '/pages/home/tempaltes/index' })
}
}
render() {
return <View />
}
}
export default Index as ComponentClass<PageOwnProps, PageState>
......@@ -83,15 +83,12 @@ class Login extends Component {
}
}, 1000)
} catch (error) {
// console.warn('获取验证码失败~', error)
showMyToast({ result: error, title: '获取验证码失败~' })
}
// Taro.hideLoading()
}
}
login = async () => {
// Taro.redirectTo({ url: HONE })
const { userPhone, checkCode } = this.state
try {
Taro.showLoading({ title: '登录中...' })
......@@ -99,7 +96,16 @@ class Login extends Component {
Taro.hideLoading()
if (data) {
await token.setToken(data)
Taro.redirectTo({ url: '/pages/home/tempaltes/index' })
if (process.env.TARO_ENV !== 'rn') {
Taro.switchTab({ url: '/pages/home/tempaltes/index' })
} else {
Taro.redirectTo({ url: '/pages/home/tempaltes/index' })
}
// if (Taro.getCurrentPages().length === 0) {
// Taro.redirectTo({ url: '/pages/home/tempaltes/index' })
// } else {
// Taro.navigateBack()
// }
}
} catch (error) {
// console.warn('用户登录出错~', error)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment