Login.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 ShopBox from './components/ShopBox';
  9. import TopicTitle from './components/TopicTitle';
  10. import ScrollRow from './components/ScrollRow';
  11. import DeviceInfo from 'react-native-device-info';
  12. import commonutil from './utils/commonutil';
  13. import http_user from './services/user';
  14. import wechat from './utils/wechat';
  15. export default class Login extends BasePage {
  16. state = {
  17. phone_num: '',
  18. verification_text: '获取验证码',
  19. verification_code: '',
  20. http_verification_code: '',
  21. deviceCode: '',
  22. sss: ''
  23. };
  24. render() {
  25. return (
  26. <View style={styles.wrapper}>
  27. <StatusBar backgroundColor={'white'} translucent={true} barStyle={'dark-content'} />
  28. <View style={styles.jump}>
  29. <TouchableOpacity style={styles.jumpBtn}>
  30. <Text style={styles.jumpText}>跳过</Text>
  31. <Image source={require('./images/common/arrowRight.png')} />
  32. </TouchableOpacity>
  33. </View>
  34. <View style={styles.phoneNumberBox}>
  35. <Text style={styles.phoneNumber}>手机号</Text>
  36. <TextInput
  37. style={styles.phoneText}
  38. onChangeText={(text) => this.setState({ phone_num: text })}
  39. value={this.state.phone_num}
  40. placeholder={'请输入手机号'}
  41. />
  42. </View>
  43. <View style={styles.signNumberBox}>
  44. <Text style={styles.phoneNumber}>验证码</Text>
  45. <View style={styles.signNumberLine2}>
  46. <TextInput
  47. style={styles.signText}
  48. onChangeText={(text) => this.setState({ verification_code: text })}
  49. value={this.state.verification_code}
  50. placeholder={'请输入验证码'}
  51. />
  52. <TouchableOpacity onPress={this.getVerification.bind(this)}>
  53. <Text style={styles.getSign}> {this.state.verification_text}</Text>
  54. </TouchableOpacity>
  55. </View>
  56. </View>
  57. <View style={styles.loginIn}>
  58. <Text style={styles.loginText} onPress={this.clickOK.bind(this)}>
  59. 登录
  60. </Text>
  61. </View>
  62. <View style={styles.wechatLogin}>
  63. <Image source={require('./images/common/wechat.png')} />
  64. <Text style={styles.wechatLoginText} onPress={this.wechatLogin.bind(this)}>
  65. 微信登录
  66. </Text>
  67. <Image source={require('./images/common/arrowRight.png')} />
  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: '006',
  135. deviceCode: this.state.deviceCode
  136. }
  137. };
  138. http_user.mobileLoginAndReg(option).then((result) => {
  139. if (result.code == 200) {
  140. //登陆成功了,可以存储用户数据到本地
  141. console.log('===============手机号登录成功=====================');
  142. console.log(result.data);
  143. console.log('===============手机号登录成功=====================');
  144. clearTimeout(this.count_timeout);
  145. this.saveUserInfo(result.data);
  146. this.clearPageToNext('SchoolAge');
  147. } else {
  148. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  149. }
  150. });
  151. } else {
  152. ToastAndroid.show('验证码不正确', ToastAndroid.SHORT);
  153. }
  154. }
  155. wechatLogin() {
  156. wechat.wechatLogin((user) => {
  157. let option = {
  158. method: 'POST',
  159. body: {
  160. channel: '006',
  161. deviceCode: this.state.deviceCode,
  162. openId: user['openid'],
  163. unionId: user['unionid'],
  164. avatar: user['province'] + user['city'],
  165. sex: +user['sex'],
  166. nickName: user['nickname']
  167. }
  168. };
  169. http_user.wechatLogin(option).then((result) => {
  170. if (result.code == 200) {
  171. //登陆成功了,可以存储用户数据到本地
  172. this.saveUserInfo(result.data);
  173. this.clearPageToNext('SchoolAge');
  174. } else {
  175. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  176. }
  177. });
  178. });
  179. }
  180. saveUserInfo(user_data) {
  181. global.storage
  182. .save({
  183. key: 'userInfo',
  184. data: user_data
  185. })
  186. .then((result) => {
  187. console.log('保存成功');
  188. })
  189. .catch((err) => {
  190. console.log('保存失败');
  191. });
  192. }
  193. }
  194. const styles = StyleSheet.create({
  195. wrapper: {
  196. flex: 1
  197. },
  198. jumpText: {
  199. color: '#3e3e3e',
  200. fontSize: 16,
  201. marginRight: 4
  202. },
  203. jump: {
  204. // width: Dimensions.width,
  205. flex: 2,
  206. alignItems: 'flex-end',
  207. justifyContent: 'center',
  208. paddingHorizontal: '8%'
  209. },
  210. jumpBtn: {
  211. flexDirection: 'row',
  212. alignItems: 'center',
  213. justifyContent: 'center'
  214. },
  215. phoneNumberBox: {
  216. flex: 2,
  217. paddingHorizontal: '8%'
  218. },
  219. phoneNumber: {
  220. color: '#3e3e3e',
  221. fontSize: 18,
  222. marginBottom: 10
  223. },
  224. phoneText: {
  225. width: '100%',
  226. height: Dimensions.getHeight(50),
  227. borderRadius: 25,
  228. backgroundColor: '#f3f3f3'
  229. },
  230. signNumberBox: {
  231. flex: 2,
  232. paddingHorizontal: '8%'
  233. },
  234. signNumberLine2: {
  235. flexDirection: 'row',
  236. width: Dimensions.width
  237. },
  238. signText: {
  239. width: '54%',
  240. height: Dimensions.getHeight(50),
  241. borderRadius: 25,
  242. backgroundColor: '#f3f3f3',
  243. marginRight: 9
  244. },
  245. getSign: {
  246. width: 105,
  247. height: Dimensions.getHeight(50),
  248. borderRadius: 25,
  249. backgroundColor: '#38da84',
  250. lineHeight: Dimensions.getHeight(50),
  251. color: '#fff',
  252. fontSize: 16,
  253. textAlign: 'center'
  254. },
  255. loginIn: {
  256. flex: 3,
  257. paddingHorizontal: '8%'
  258. },
  259. loginText: {
  260. width: '100%',
  261. height: Dimensions.getHeight(50),
  262. backgroundColor: '#63aeff',
  263. textAlign: 'center',
  264. lineHeight: Dimensions.getHeight(50),
  265. color: '#fff',
  266. fontSize: 20,
  267. borderRadius: 25
  268. },
  269. wechatLogin: {
  270. flex: 3,
  271. flexDirection: 'row',
  272. paddingHorizontal: '33.6%',
  273. alignItems: 'center',
  274. justifyContent: 'center'
  275. },
  276. wechatLoginText: {
  277. fontSize: 16,
  278. color: '#3e3e3e',
  279. marginHorizontal: 6
  280. }
  281. });