Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
visualcloud
/
Vmatrix-client-taro
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
24fe9cbc
authored
Aug 24, 2019
by
lirandong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完成 登录功能
parent
b1d1b9f4
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
117 additions
and
56 deletions
+117
-56
src/api/axios.ts
+11
-33
src/app.tsx
+19
-10
src/common/index.ts
+0
-1
src/common/msg.ts
+6
-0
src/common/token.ts
+12
-6
src/common/utils.ts
+2
-1
src/pages/home/tempaltes/index.tsx
+2
-0
src/pages/home/user/index.tsx
+8
-1
src/pages/index.tsx
+47
-0
src/pages/login/index.tsx
+10
-4
No files found.
src/api/axios.ts
View file @
24fe9cbc
// 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
src/app.tsx
View file @
24fe9cbc
...
...
@@ -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() 函数没有实际作用
// 请勿修改此函数
...
...
src/common/index.ts
View file @
24fe9cbc
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'
...
...
src/common/msg.ts
0 → 100644
View file @
24fe9cbc
export
const
INTERFACE_REPEAT
=
'接口重复,请求已取消'
export
const
TOKEN_REPEAT
=
'token 为空'
/** 用户信息失效 */
export
const
LOGIN_0003
=
'LOGIN_0003'
src/common/token.ts
View file @
24fe9cbc
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
()
src/common/utils.ts
View file @
24fe9cbc
...
...
@@ -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
}
},
...
...
src/pages/home/tempaltes/index.tsx
View file @
24fe9cbc
...
...
@@ -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
()
}
...
...
src/pages/home/user/index.tsx
View file @
24fe9cbc
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
>
...
...
src/pages/index.tsx
0 → 100644
View file @
24fe9cbc
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
>
src/pages/login/index.tsx
View file @
24fe9cbc
...
...
@@ -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)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment