cf-wx-app/src/pages/login/login.js

153 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-02-12 08:54:34 +08:00
import Taro, { Component } from '@tarojs/taro'
2019-02-12 17:21:31 +08:00
import { View, Input, Button, Text, Image } from '@tarojs/components'
2019-02-12 08:54:34 +08:00
import URL from '../../serviceAPI.config'
import './login.scss'
class Login extends Component {
config = {
navigationBarTitleText: '登入'
}
2019-02-12 17:21:31 +08:00
constructor() {
this.state = {
username: '',
password: '',
backgroundImage: 'background-image:url(' + URL.Base + '/Public/images/bg3.jpg);'
}
2019-02-12 08:54:34 +08:00
}
2019-02-12 17:21:31 +08:00
//用户信息姓名和电话号码接口
setUserInfoToStorage() {
Taro.request({
url: URL.ShopWxStore,
header: {
'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'),
}
})
.then(res => {
if (res.data.err_msg === 'success') {
Taro.setStorageSync('user_identity', { username: res.data.otherData.userName, userphone: res.data.otherData.userPhone })
2019-02-12 08:54:34 +08:00
2019-02-12 17:21:31 +08:00
} else {
Taro.showToast({
title: res.data.err_msg,
icon: 'none',
duration: 1500
})
}
})
}
2019-02-12 08:54:34 +08:00
loginApi() {
Taro.request({
2019-02-12 17:21:31 +08:00
url: URL.Base + 'user-login',
2019-02-12 08:54:34 +08:00
method: 'POST',
dataType: 'json',
data: {
username: this.state.username,
password: this.state.password,
},
header: {
'content-type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(res => {
2019-02-12 17:21:31 +08:00
if (res.data.err_code === 0) {
2019-02-12 08:54:34 +08:00
Taro.showToast({
2019-02-12 17:21:31 +08:00
title: res.data.err_msg && '登入成功',
2019-02-12 08:54:34 +08:00
icon: 'success',
duration: 1000
})
2019-02-12 17:21:31 +08:00
Taro.setStorageSync('session_id', res.data.session_id)
Taro.setStorageSync('shopInfo', res.data.shop_info)
Taro.setStorageSync('userInfo', res.data.user_info)
this.setUserInfoToStorage()
2019-02-12 08:54:34 +08:00
setTimeout(() => {
2019-02-12 17:21:31 +08:00
// Taro.navigateBack({
// delta: 1
// })
2019-02-12 08:54:34 +08:00
Taro.switchTab({
2019-02-12 17:21:31 +08:00
url:'/pages/home/home'
2019-02-12 08:54:34 +08:00
})
}, 1000);
2019-02-12 17:21:31 +08:00
} else {
2019-02-12 08:54:34 +08:00
Taro.showToast({
2019-02-12 17:21:31 +08:00
title: res.data.err_msg,
2019-02-12 08:54:34 +08:00
icon: 'none',
duration: 2000
})
}
})
}
2019-02-12 17:21:31 +08:00
usernameHandler(e) {
let value = e.detail.value
this.setState({ username: value })
2019-02-12 08:54:34 +08:00
}
2019-02-12 17:21:31 +08:00
passwordHandler(e) {
let value = e.detail.value
this.setState({ password: value })
2019-02-12 08:54:34 +08:00
}
2019-02-12 17:21:31 +08:00
loginHandler() {
2019-02-12 08:54:34 +08:00
this.loginApi()
}
componentDidMount() {
2019-02-12 17:21:31 +08:00
2019-02-12 08:54:34 +08:00
}
2019-02-12 17:21:31 +08:00
2019-02-12 08:54:34 +08:00
componentWillReceiveProps(nextProps) {
console.log(this.props, nextProps)
}
componentWillUnmount() { }
componentDidShow() { }
componentDidHide() { }
render() {
return (
<View className='login'>
2019-02-12 17:21:31 +08:00
<View className='background' style={this.state.backgroundImage}></View>
<View className='logo'>
<View className='image-wrapper'>
<View className='image-inner-wrapper'>
<Image style='width: 240px;height: 80px;background: #fff;' src={URL.Base + '/Public/images/com-logo.png'} />
2019-02-12 08:54:34 +08:00
</View>
2019-02-12 17:21:31 +08:00
<View className='title'>全屋定制商城</View>
</View>
</View>
<View className='login-wrapper'>
<View className='title'>用户登入</View>
<View className='bgtopWrap'>
<View className='loginWrap'>
<View className='username'>
<Input type='text' focus={true} name='username' maxLength='11' placeholder='请输入用户名/手机号' value={this.state.username} onInput={this.usernameHandler} />
</View>
<View className='password'>
<Input type='password' name='password' placeholder='请输入密码' value={this.state.password} onInput={this.passwordHandler} />
</View>
<Text className='register'>
立即注册
2019-02-12 08:54:34 +08:00
</Text>
2019-02-12 17:21:31 +08:00
<Button className='button button-orange' onClick={this.loginHandler}>登录</Button>
</View>
2019-02-12 08:54:34 +08:00
</View>
2019-02-12 17:21:31 +08:00
2019-02-12 08:54:34 +08:00
</View>
</View>
)
}
}
export default Login