Login.js 9.1 KB

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