Commit 48af92d1 by lirandong

添加 token 校验

parent 33c12755
...@@ -22,7 +22,8 @@ ...@@ -22,7 +22,8 @@
"dev:h5": "npm run build:h5 -- --watch", "dev:h5": "npm run build:h5 -- --watch",
"dev:rn": "npm run build:rn -- --watch", "dev:rn": "npm run build:rn -- --watch",
"lint": "tslint --project ./tsconfig.json && stylelint \"./src/**/*.less\"", "lint": "tslint --project ./tsconfig.json && stylelint \"./src/**/*.less\"",
"precommit": "npm run lint" "precommit": "npm run lint",
"run:shell": "cd ../taro-native-shell/ && yarn android"
}, },
"author": "", "author": "",
"license": "MIT", "license": "MIT",
......
...@@ -4,13 +4,6 @@ import Index from './pages/index' ...@@ -4,13 +4,6 @@ import Index from './pages/index'
import { Provider } from '@tarojs/redux' import { Provider } from '@tarojs/redux'
import Taro, { Component, Config } from '@tarojs/taro' import Taro, { Component, Config } from '@tarojs/taro'
import token from './constants/token' import token from './constants/token'
import { LOGIN } from './constants/router'
// import TaroRouter from '@tarojs/taro-router-rn'
// import Login from './pages/login'
// import './app.less'
// import token from './constants/token'
// import api from './api';
// 如果需要在 h5 环境中开启 React Devtools // 如果需要在 h5 环境中开启 React Devtools
// 取消以下注释: // 取消以下注释:
...@@ -18,8 +11,15 @@ import { LOGIN } from './constants/router' ...@@ -18,8 +11,15 @@ import { LOGIN } from './constants/router'
// require('nerv-devtools') // require('nerv-devtools')
// } // }
const store = configStore() /** 初始化 tokne */
!(async () => {
await token.init()
console.log('1111')
Taro.render(<App />, document.getElementById('app'))
console.log('done')
})()
const store = configStore()
class App extends Component { class App extends Component {
/** /**
* 指定config的类型声明为: Taro.Config * 指定config的类型声明为: Taro.Config
...@@ -29,7 +29,6 @@ class App extends Component { ...@@ -29,7 +29,6 @@ class App extends Component {
* 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型 * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
*/ */
config: Config = { config: Config = {
// pages: pages,
pages: [ pages: [
'pages/index/index', 'pages/index/index',
'pages/login/index', 'pages/login/index',
...@@ -44,36 +43,27 @@ class App extends Component { ...@@ -44,36 +43,27 @@ class App extends Component {
navigationBarTitleText: 'Taro Demo', navigationBarTitleText: 'Taro Demo',
navigationBarBackgroundColor: '#fff' navigationBarBackgroundColor: '#fff'
}, },
tabBar: { tabBar:
list: [ process.env.TARO_ENV !== 'rn'
{ ? {
pagePath: 'pages/home/tempaltes/index', list: [
text: '模板市场' {
}, pagePath: 'pages/home/tempaltes/index',
{ text: '模板市场'
pagePath: 'pages/home/device/index', },
text: '设备与视片' {
}, pagePath: 'pages/home/device/index',
{ text: '设备与视片'
pagePath: 'pages/home/user/index', },
text: '个人中心' {
} pagePath: 'pages/home/user/index',
] text: '个人中心'
} }
} ]
}
componentDidMount() { : undefined
if (!token.isLogon()) Taro.navigateTo({ url: LOGIN })
// if (!token.isLogon()) Taro.login()
// if (!token.isLogon()) Taro.redirectTo({ url: LOGIN })
} }
componentDidShow() {}
componentDidHide() {}
componentDidCatchError() {}
// 在 App 类中的 render() 函数没有实际作用 // 在 App 类中的 render() 函数没有实际作用
// 请勿修改此函数 // 请勿修改此函数
render() { render() {
...@@ -84,7 +74,3 @@ class App extends Component { ...@@ -84,7 +74,3 @@ class App extends Component {
) )
} }
} }
// const RootStack = TaroRouter.initRouter()
Taro.render(<App />, document.getElementById('app'))
import { MyStorage } from './utils' import { MyStorage } from './utils'
class Token { class Token {
private token private initDone = false
constructor() { private token = '0265dc09f4d24cf49fa360269cd8edbc-APP'
this.getStorageToken() // constructor() {
// this.getStorageToken()
// }
init() {
if (!this.initDone) {
this.initDone = true
console.log('init')
return this.getStorageToken()
}
} }
/** 检测用户是否登录 */ /** 检测用户是否登录 */
...@@ -23,6 +32,7 @@ class Token { ...@@ -23,6 +32,7 @@ class Token {
async getStorageToken() { async getStorageToken() {
try { try {
const { data } = await MyStorage.getItem({ key: 'token' }) const { data } = await MyStorage.getItem({ key: 'token' })
console.log({ data })
if (!!data) this.token = data if (!!data) this.token = data
} catch (error) { } catch (error) {
console.warn('获取本地 token 失败~', error) console.warn('获取本地 token 失败~', error)
......
...@@ -30,13 +30,6 @@ class Device extends Component { ...@@ -30,13 +30,6 @@ class Device extends Component {
name: '小红' name: '小红'
} }
} }
componentWillReceiveProps(_nextProps) {}
componentWillUnmount() {}
componentDidShow() {}
componentDidHide() {}
render() { render() {
return ( return (
......
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 } from '@tarojs/components' import TaroRouter from '@tarojs/taro-router-rn'
import pagesTempaltes from './tempaltes'
import pagesDevice from './device'
import pagesUser from './user'
import './index.less' import './index.less'
type PageStateProps = {} /** RN 的 tabar */
type PageDispatchProps = {} class Index extends Component {
confog: Config = {
type PageOwnProps = {} navigationStyle: 'custom'
}
type PageState = {
// name: string
}
type IProps = PageStateProps & PageDispatchProps & PageOwnProps
interface Index { render() {
props: IProps return <RootStack />
state: PageState }
} }
class Index extends Component { const RootStack = TaroRouter.initRouter(
confog: Config = { [
navigationStyle: 'custom', ['pages/home/tempaltes/index', pagesTempaltes],
pages: ['device/index', 'tempaltes/index', 'user/index'], ['pages/home/device/index', pagesDevice],
['pages/home/user/index', pagesUser]
],
Taro,
{
tabBar: { tabBar: {
list: [ list: [
{ {
pagePath: 'device/index', pagePath: 'pages/home/tempaltes/index',
text: '模板市场' text: '模板市场'
}, },
{ {
pagePath: 'tempaltes/index', pagePath: 'pages/home/device/index',
text: '设备与视片' text: '设备与视片'
}, },
{ {
pagePath: 'user/index', pagePath: 'pages/home/user/index',
text: '个人中心' text: '个人中心'
} }
] ]
} }
} }
)
constructor(props) { export default Index as ComponentClass
super(props)
this.state = {
// name: '小红'
}
}
render() {
return (
// <TabB
<View className="Index">
<Text>hello world111</Text>
</View>
)
}
}
export default Index as ComponentClass<PageOwnProps, PageState>
...@@ -30,13 +30,6 @@ class Tempaltes extends Component { ...@@ -30,13 +30,6 @@ class Tempaltes extends Component {
name: '小红' name: '小红'
} }
} }
componentWillReceiveProps(_nextProps) {}
componentWillUnmount() {}
componentDidShow() {}
componentDidHide() {}
render() { render() {
return ( return (
......
...@@ -30,13 +30,6 @@ class User extends Component { ...@@ -30,13 +30,6 @@ class User extends Component {
name: '小红' name: '小红'
} }
} }
componentWillReceiveProps(_nextProps) {}
componentWillUnmount() {}
componentDidShow() {}
componentDidHide() {}
render() { render() {
return ( return (
......
...@@ -3,7 +3,6 @@ import Login from '../login' ...@@ -3,7 +3,6 @@ import Login from '../login'
import { ComponentClass } from 'react' import { ComponentClass } from 'react'
import { View } from '@tarojs/components' import { View } from '@tarojs/components'
import token from '../../constants/token' import token from '../../constants/token'
// import { LOGIN, HONE } from '../../constants/router'
import Taro, { Component, Config } from '@tarojs/taro' import Taro, { Component, Config } from '@tarojs/taro'
class Index extends Component { class Index extends Component {
...@@ -11,17 +10,10 @@ class Index extends Component { ...@@ -11,17 +10,10 @@ class Index extends Component {
navigationStyle: 'custom' navigationStyle: 'custom'
} }
// componentDidMount() {
// // !token.isLogon() ? Taro.redirectTo({ url: LOGIN }) : Taro.redirectTo({ url: HONE })
// // Taro.redirectTo({ url: HONE })
// }
render() { render() {
return ( const isLogin = token.isLogon()
<View style={{ height: '100%', width: '100%' }}> console.log({ isLogin })
{!token.isLogon() ? <Home /> : <Login />} return <View style={{ height: '100%', width: '100%' }}>{isLogin ? <Home /> : <Login />}</View>
</View>
)
} }
} }
......
...@@ -2,6 +2,7 @@ import api from '../../api' ...@@ -2,6 +2,7 @@ import api from '../../api'
import { ComponentClass } from 'react' import { ComponentClass } from 'react'
import { CheckBox } from 'react-native' import { CheckBox } from 'react-native'
import { connect } from '@tarojs/redux' import { connect } from '@tarojs/redux'
import token from '../../constants/token'
import { HONE } from '../../constants/router' import { HONE } from '../../constants/router'
import { checkPhone, showErrorToast } from '../../constants/utils' import { checkPhone, showErrorToast } from '../../constants/utils'
import Taro, { Component, Config } from '@tarojs/taro' import Taro, { Component, Config } from '@tarojs/taro'
...@@ -71,6 +72,7 @@ class Login extends Component { ...@@ -71,6 +72,7 @@ class Login extends Component {
try { try {
Taro.showLoading({ title: '获取中...' }) Taro.showLoading({ title: '获取中...' })
await api.user.getCheckCode(userPhone) await api.user.getCheckCode(userPhone)
Taro.hideLoading()
this.setState({ getCodeTime: 60 }) this.setState({ getCodeTime: 60 })
const timer = setInterval(() => { const timer = setInterval(() => {
if (this.state.getCodeTime === 0) { if (this.state.getCodeTime === 0) {
...@@ -88,16 +90,20 @@ class Login extends Component { ...@@ -88,16 +90,20 @@ class Login extends Component {
} }
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: '登录中...' })
await api.user.login(userPhone, checkCode) const { data } = await api.user.login(userPhone, checkCode)
Taro.navigateTo({ url: HONE }) Taro.hideLoading()
if (data) {
await token.setToken(data)
Taro.redirectTo({ url: HONE })
}
} catch (error) { } catch (error) {
// console.warn('用户登录出错~', error) // console.warn('用户登录出错~', error)
showErrorToast({ error, tips: '用户登录出错~' }) showErrorToast({ error, tips: '用户登录出错~' })
} }
// Taro.hideLoading()
} }
shouldComponentUpdate(_nextProps, _nextState) { shouldComponentUpdate(_nextProps, _nextState) {
...@@ -141,7 +147,7 @@ class Login extends Component { ...@@ -141,7 +147,7 @@ class Login extends Component {
</View> </View>
<Label className="app-clause" for="app-clause"> <Label className="app-clause" for="app-clause">
{/* Taro 的 CheckBox 在 RN 上不能触发点击事件 */} {/* Taro 的 CheckBox 在 RN 上不能触发点击事件 */}
{Taro.getEnv() === Taro.ENV_TYPE.RN ? ( {process.env.TARO_ENV === 'rn' ? (
<CheckBox value={checked} onChange={this.changeCheckBox} /> <CheckBox value={checked} onChange={this.changeCheckBox} />
) : ( ) : (
<Checkbox <Checkbox
......
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