Login.js 9.7 KB

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