Login.js 11 KB

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