Login.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. if (result.data.isVisitor) {
  181. var usermap = new Map();
  182. usermap.set('isVisitor', result.data.isVisitor);
  183. usermap.set('channel', result.data.visitor.channel + '');
  184. usermap.set('birthday', '');
  185. usermap.set('city', '');
  186. usermap.set('gmtCreated', result.data.visitor.gmtCreated + '');
  187. usermap.set('gmtModified', result.data.visitor.gmtModified + '');
  188. usermap.set('country', '');
  189. usermap.set('eid', '');
  190. usermap.set('grade', '');
  191. usermap.set('mobile', '');
  192. usermap.set('nickName', '');
  193. usermap.set('province', '');
  194. usermap.set('school', '');
  195. usermap.set('sex', '');
  196. usermap.set('status', '');
  197. usermap.set('uid', '');
  198. usermap.set('ageGroup', 'PRESCHOOL');
  199. usermap.set('avatar', '');
  200. this.saveUserInfo(commonutil.mapToJson(usermap));
  201. this.clearPageToNext('SchoolAge');
  202. }
  203. } else {
  204. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  205. }
  206. });
  207. }
  208. wechatLogin() {
  209. wechat.wechatLogin((user) => {
  210. let option = {
  211. method: 'POST',
  212. body: {
  213. channel: commonutil.getAppCode(),
  214. deviceCode: this.state.deviceCode,
  215. openId: user['openid'],
  216. unionId: user['unionid'],
  217. avatar: user['headimgurl'],
  218. sex: user['sex'],
  219. nickName: user['nickname']
  220. }
  221. };
  222. http_user.wechatLogin(option).then((result) => {
  223. if (result.code == 200) {
  224. //登陆成功了,可以存储用户数据到本地
  225. var usermap = new Map();
  226. usermap.set('isVisitor', result.data.isVisitor);
  227. usermap.set('birthday', result.data.user.birthday);
  228. usermap.set('channel', result.data.user.channel);
  229. usermap.set('city', result.data.user.city);
  230. usermap.set('country', result.data.user.country);
  231. usermap.set('eid', result.data.user.eid);
  232. usermap.set('gmtCreated', result.data.user.gmtCreated);
  233. usermap.set('gmtModified', result.data.user.gmtModified);
  234. usermap.set('grade', result.data.user.grade);
  235. usermap.set('mobile', result.data.user.mobile);
  236. usermap.set('nickName', result.data.user.nickName);
  237. usermap.set('province', result.data.user.province);
  238. usermap.set('school', result.data.user.school);
  239. usermap.set('sex', result.data.user.sex);
  240. usermap.set('status', result.data.user.status);
  241. usermap.set('uid', result.data.user.uid);
  242. usermap.set('ageGroup', result.data.user.ageGroup);
  243. usermap.set('avatar', result.data.user.avatar);
  244. console.log('===============微信登录成功=====================');
  245. console.log(commonutil.mapToJson(usermap));
  246. console.log('===============微信登录成功=====================');
  247. this.saveUserInfo(commonutil.mapToJson(usermap));
  248. this.clearPageToNext('SchoolAge');
  249. } else {
  250. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  251. }
  252. });
  253. });
  254. }
  255. }
  256. const styles = StyleSheet.create({
  257. wrapper: {
  258. flex: 1
  259. },
  260. jumpText: {
  261. color: '#3e3e3e',
  262. fontSize: 16,
  263. marginRight: 4
  264. },
  265. jump: {
  266. // width: Dimensions.width,
  267. flex: 2,
  268. alignItems: 'flex-end',
  269. justifyContent: 'center',
  270. paddingHorizontal: '8%'
  271. },
  272. jumpBtn: {
  273. flexDirection: 'row',
  274. alignItems: 'center',
  275. justifyContent: 'center'
  276. },
  277. phoneNumberBox: {
  278. flex: 2,
  279. paddingHorizontal: '8%'
  280. },
  281. phoneNumber: {
  282. color: '#3e3e3e',
  283. fontSize: 18,
  284. marginBottom: 10
  285. },
  286. phoneText: {
  287. width: '100%',
  288. borderRadius: 25,
  289. height: Dimensions.getHeight(50),
  290. backgroundColor: '#f3f3f3'
  291. },
  292. signNumberBox: {
  293. flex: 2,
  294. paddingHorizontal: '8%'
  295. },
  296. signNumberLine2: {
  297. flexDirection: 'row',
  298. width: Dimensions.width
  299. },
  300. signText: {
  301. width: '54%',
  302. height: Dimensions.getHeight(50),
  303. backgroundColor: '#f3f3f3',
  304. borderRadius: 25,
  305. marginRight: 9
  306. },
  307. getSign: {
  308. width: 105,
  309. height: Dimensions.getHeight(50),
  310. borderRadius: 25,
  311. backgroundColor: '#38da84',
  312. lineHeight: Dimensions.getHeight(50),
  313. color: '#fff',
  314. fontSize: 16,
  315. textAlign: 'center'
  316. },
  317. loginIn: {
  318. flex: 3,
  319. paddingHorizontal: '8%'
  320. },
  321. loginText: {
  322. width: '100%',
  323. height: Dimensions.getHeight(50),
  324. backgroundColor: '#63aeff',
  325. textAlign: 'center',
  326. lineHeight: Dimensions.getHeight(50),
  327. color: '#fff',
  328. fontSize: 20,
  329. borderRadius: 25
  330. },
  331. wechatLogin: {
  332. flex: 3,
  333. flexDirection: 'row',
  334. paddingHorizontal: '33.6%',
  335. alignItems: 'center',
  336. justifyContent: 'center'
  337. },
  338. wechatLoginText: {
  339. fontSize: 16,
  340. color: '#3e3e3e',
  341. marginHorizontal: 6
  342. }
  343. });