Login.js 7.4 KB

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