Login.js 10.0 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. <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. if (result.code == 200) {
  177. let info = {
  178. avatar: result.data.user.avatar,
  179. ageGroup: result.data.visitor.ageGroup,
  180. birthday: result.data.visitor.birthday,
  181. channel: result.data.visitor.channel,
  182. city: result.data.visitor.city,
  183. country: result.data.visitor.country,
  184. eid: result.data.visitor.eid,
  185. gmtCreated: result.data.visitor.gmtCreated,
  186. gmtModified: result.data.visitor.gmtModified,
  187. mobile: result.data.visitor.mobile,
  188. nickName: result.data.visitor.nickName, //=== '' ? '未设置' : result.data.user.nickName,
  189. province: result.data.visitor.province,
  190. school: result.data.visitor.school,
  191. sex: result.data.visitor.sex,
  192. status: result.data.visitor.status,
  193. uid: result.data.visitor.uid,
  194. isVisitor: result.data.isVisitor.toUpperCase() === 'TRUE' ? true : false
  195. };
  196. global.userInfo = info;
  197. console.log('================跳过登录====================');
  198. console.log('global.userInfo', global.userInfo);
  199. console.log('====================================');
  200. commonutil.saveUserInfo();
  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. let info = {
  226. avatar: result.data.user.avatar,
  227. ageGroup: result.data.user.ageGroup,
  228. birthday: result.data.user.birthday,
  229. channel: result.data.user.channel,
  230. city: result.data.user.city,
  231. country: result.data.user.country,
  232. eid: result.data.user.eid,
  233. gmtCreated: result.data.user.gmtCreated,
  234. gmtModified: result.data.user.gmtModified,
  235. mobile: result.data.user.mobile,
  236. nickName: result.data.user.nickName,
  237. province: result.data.user.province,
  238. school: result.data.user.school,
  239. sex: result.data.user.sex,
  240. status: result.data.user.status,
  241. uid: result.data.user.uid,
  242. isVisitor: result.data.isVisitor.toUpperCase() === 'TRUE' ? true : false
  243. };
  244. global.userInfo = info;
  245. commonutil.saveUserInfo();
  246. this.clearPageToNext('SchoolAge');
  247. } else {
  248. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  249. }
  250. });
  251. });
  252. }
  253. }
  254. const styles = StyleSheet.create({
  255. wrapper: {
  256. flex: 1
  257. },
  258. jumpText: {
  259. color: '#3e3e3e',
  260. fontSize: 16,
  261. marginRight: 4
  262. },
  263. jump: {
  264. // width: Dimensions.width,
  265. flex: 2,
  266. alignItems: 'flex-end',
  267. justifyContent: 'center',
  268. paddingHorizontal: '8%'
  269. },
  270. jumpBtn: {
  271. flexDirection: 'row',
  272. alignItems: 'center',
  273. justifyContent: 'center'
  274. },
  275. phoneNumberBox: {
  276. flex: 2,
  277. paddingHorizontal: '8%'
  278. },
  279. phoneNumber: {
  280. color: '#3e3e3e',
  281. fontSize: 18,
  282. marginBottom: 10
  283. },
  284. phoneText: {
  285. width: '100%',
  286. borderRadius: 25,
  287. height: Dimensions.getHeight(50),
  288. backgroundColor: '#f3f3f3'
  289. },
  290. signNumberBox: {
  291. flex: 2,
  292. paddingHorizontal: '8%'
  293. },
  294. signNumberLine2: {
  295. flexDirection: 'row',
  296. width: Dimensions.width
  297. },
  298. signText: {
  299. width: '54%',
  300. height: Dimensions.getHeight(50),
  301. backgroundColor: '#f3f3f3',
  302. borderRadius: 25,
  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. });