Login.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * 登录
  3. */
  4. import React, { Component } from 'react';
  5. import { StyleSheet, Text, View, Image, TouchableOpacity, StatusBar, ToastAndroid, TextInput } from 'react-native';
  6. import BasePage from './BasePage';
  7. import Dimensions from './utils/dimensions';
  8. import ShopBox from './components/ShopBox';
  9. import TopicTitle from './components/TopicTitle';
  10. import ScrollRow from './components/ScrollRow';
  11. import DeviceInfo from 'react-native-device-info';
  12. import commonutil from './utils/commonutil';
  13. import http_user from './services/user';
  14. import wechat from './utils/wechat';
  15. export default class Login extends BasePage {
  16. state = {
  17. phone_num: '',
  18. verification_text: '获取验证码',
  19. verification_code: '',
  20. http_verification_code: '',
  21. deviceCode: '',
  22. sss: ''
  23. };
  24. render() {
  25. return (
  26. <View style={styles.wrapper}>
  27. <StatusBar backgroundColor={'white'} translucent={true} barStyle={'dark-content'} />
  28. <View style={styles.jump}>
  29. <TouchableOpacity style={styles.jumpBtn}>
  30. <Text style={styles.jumpText}>跳过</Text>
  31. <Image source={require('./images/common/arrowRight.png')} />
  32. </TouchableOpacity>
  33. </View>
  34. <View style={styles.phoneNumberBox}>
  35. <Text style={styles.phoneNumber}>手机号</Text>
  36. <TextInput
  37. style={styles.phoneText}
  38. onChangeText={(text) => this.setState({ phone_num: text })}
  39. value={this.state.phone_num}
  40. placeholder={'请输入手机号'}
  41. />
  42. </View>
  43. <View style={styles.signNumberBox}>
  44. <Text style={styles.phoneNumber}>验证码</Text>
  45. <View style={styles.signNumberLine2}>
  46. <TextInput
  47. style={styles.signText}
  48. onChangeText={(text) => this.setState({ verification_code: text })}
  49. value={this.state.verification_code}
  50. placeholder={'请输入验证码'}
  51. />
  52. <TouchableOpacity onPress={this.getVerification.bind(this)}>
  53. <Text style={styles.getSign}> {this.state.verification_text}</Text>
  54. </TouchableOpacity>
  55. </View>
  56. </View>
  57. <View style={styles.loginIn}>
  58. <Text style={styles.loginText} onPress={this.clickOK.bind(this)}>
  59. 登录
  60. </Text>
  61. </View>
  62. <View style={styles.wechatLogin}>
  63. <Image source={require('./images/common/wechat.png')} />
  64. <Text style={styles.wechatLoginText} onPress={this.wechatLogin.bind(this)}>
  65. 微信登录
  66. </Text>
  67. <Image source={require('./images/common/arrowRight.png')} />
  68. </View>
  69. </View>
  70. );
  71. }
  72. componentWillMount() {
  73. console.log('================================================'); // e.g US
  74. console.log('Device getUniqueID', DeviceInfo.getUniqueID()); // e.g US
  75. console.log('Device getSerialNumber', DeviceInfo.getSerialNumber()); // e.g US
  76. console.log('================================================'); // e.g US
  77. this.setState({
  78. deviceCode: DeviceInfo.getUniqueID() + DeviceInfo.getSerialNumber()
  79. });
  80. }
  81. getVerification() {
  82. if (this.state.verification_text === '获取验证码') {
  83. if (commonutil.isPoneAvailable(this.state.phone_num)) {
  84. http_user.getVerificationCode(this.state.phone_num).then((result) => {
  85. if (result.code != 200) {
  86. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  87. return;
  88. } else {
  89. console.log('====================================');
  90. console.log(result.data);
  91. console.log('====================================');
  92. this.setState({
  93. http_verification_code: result.data
  94. });
  95. }
  96. this.setState({
  97. verification_text: '60'
  98. });
  99. this.CountDown();
  100. });
  101. } else {
  102. ToastAndroid.show('请输入正确的手机号', ToastAndroid.SHORT);
  103. }
  104. } else {
  105. }
  106. }
  107. CountDown() {
  108. if (parseInt(this.state.verification_text) == 0) {
  109. this.setState({
  110. verification_text: '获取验证码'
  111. });
  112. } else {
  113. setTimeout(() => {
  114. this.setState({
  115. verification_text: parseInt(this.state.verification_text) - 1 + ''
  116. });
  117. this.CountDown();
  118. }, 1000);
  119. }
  120. }
  121. clickOK() {
  122. if (!commonutil.isPoneAvailable(this.state.phone_num)) {
  123. ToastAndroid.show('请输入正确的手机号', ToastAndroid.SHORT);
  124. return;
  125. }
  126. if (this.state.verification_code == '') {
  127. ToastAndroid.show('请输入验证码', ToastAndroid.SHORT);
  128. return;
  129. }
  130. if (this.state.http_verification_code == this.state.verification_code) {
  131. let option = {
  132. method: 'POST', //请求方法
  133. //请求体
  134. body: {
  135. mobile: this.state.phone_num,
  136. sign: this.state.verification_code,
  137. channel: '006',
  138. deviceCode: this.state.deviceCode
  139. }
  140. };
  141. http_user.mobileLoginAndReg(option).then((result) => {
  142. if (result.code == 200) {
  143. //登陆成功了,可以存储用户数据到本地
  144. this.clearPageToNext('SchoolAge');
  145. } else {
  146. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  147. }
  148. });
  149. } else {
  150. ToastAndroid.show('验证码不正确', ToastAndroid.SHORT);
  151. }
  152. }
  153. wechatLogin() {
  154. console.log('this.state.deviceCode:' + this.state.deviceCode);
  155. wechat.wechatLogin((user) => {
  156. let option = {
  157. method: 'POST',
  158. body: {
  159. channel: '006',
  160. deviceCode: this.state.deviceCode,
  161. openId: user['openid'],
  162. unionId: user['unionid'],
  163. avatar: user['province'] + user['city'],
  164. sex: +user['sex'],
  165. nickName: user['nickname']
  166. }
  167. };
  168. http_user.wechatLogin(option).then((result) => {
  169. if (result.code == 200) {
  170. //登陆成功了,可以存储用户数据到本地
  171. this.clearPageToNext('SchoolAge');
  172. } else {
  173. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  174. }
  175. console.log('result-------:' + result);
  176. });
  177. });
  178. }
  179. }
  180. const styles = StyleSheet.create({
  181. wrapper: {
  182. flex: 1
  183. },
  184. jumpText: {
  185. color: '#3e3e3e',
  186. fontSize: 16,
  187. marginRight: 4
  188. },
  189. jump: {
  190. // width: Dimensions.width,
  191. flex: 2,
  192. alignItems: 'flex-end',
  193. justifyContent: 'center',
  194. paddingHorizontal: '8%'
  195. },
  196. jumpBtn: {
  197. flexDirection: 'row',
  198. alignItems: 'center',
  199. justifyContent: 'center'
  200. },
  201. phoneNumberBox: {
  202. flex: 2,
  203. paddingHorizontal: '8%'
  204. },
  205. phoneNumber: {
  206. color: '#3e3e3e',
  207. fontSize: 18,
  208. marginBottom: 10
  209. },
  210. phoneText: {
  211. width: '100%',
  212. height: Dimensions.getHeight(50),
  213. borderRadius: 25,
  214. backgroundColor: '#f3f3f3'
  215. },
  216. signNumberBox: {
  217. flex: 2,
  218. paddingHorizontal: '8%'
  219. },
  220. signNumberLine2: {
  221. flexDirection: 'row',
  222. width: Dimensions.width
  223. },
  224. signText: {
  225. width: '54%',
  226. height: Dimensions.getHeight(50),
  227. borderRadius: 25,
  228. backgroundColor: '#f3f3f3',
  229. marginRight: 9
  230. },
  231. getSign: {
  232. width: 105,
  233. height: Dimensions.getHeight(50),
  234. borderRadius: 25,
  235. backgroundColor: '#38da84',
  236. lineHeight: Dimensions.getHeight(50),
  237. color: '#fff',
  238. fontSize: 16,
  239. textAlign: 'center'
  240. },
  241. loginIn: {
  242. flex: 3,
  243. paddingHorizontal: '8%'
  244. },
  245. loginText: {
  246. width: '100%',
  247. height: Dimensions.getHeight(50),
  248. backgroundColor: '#63aeff',
  249. textAlign: 'center',
  250. lineHeight: Dimensions.getHeight(50),
  251. color: '#fff',
  252. fontSize: 20,
  253. borderRadius: 25
  254. },
  255. wechatLogin: {
  256. flex: 3,
  257. flexDirection: 'row',
  258. paddingHorizontal: '33.6%',
  259. alignItems: 'center',
  260. justifyContent: 'center'
  261. },
  262. wechatLoginText: {
  263. fontSize: 16,
  264. color: '#3e3e3e',
  265. marginHorizontal: 6
  266. }
  267. });