Login.js 11 KB

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