Login.js 11 KB

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