Commit 24fe9cbc by lirandong

完成 登录功能

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