Commit 48af92d1 by lirandong

添加 token 校验

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