Login.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. };
  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. //绑定手机号
  121. clickOK() {
  122. //请求报错
  123. //参数
  124. /**
  125. *
  126. * {
  127. "channel": "006",
  128. "deviceCode": "fb3dcd333348ZX1G42CPJD",
  129. "mobile": "15810271473",
  130. "sign": "9999"
  131. }
  132. *
  133. *
  134. *
  135. *
  136. */
  137. if (!commonutil.isPoneAvailable(this.state.phone_num)) {
  138. ToastAndroid.show('请输入正确的手机号', ToastAndroid.SHORT);
  139. return;
  140. }
  141. if (this.state.verification_code == '') {
  142. ToastAndroid.show('请输入验证码', ToastAndroid.SHORT);
  143. return;
  144. }
  145. if (this.state.http_verification_code == this.state.verification_code) {
  146. let option = {
  147. method: 'POST', //请求方法
  148. //请求体
  149. body: {
  150. mobile: this.state.phone_num,
  151. sign: this.state.verification_code,
  152. channel: '006',
  153. deviceCode: this.state.deviceCode
  154. }
  155. };
  156. http_user.mobileLoginAndReg(option).then((result) => {
  157. if (result.code == 200) {
  158. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  159. } else {
  160. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  161. }
  162. });
  163. } else {
  164. ToastAndroid.show('验证码不正确', ToastAndroid.SHORT);
  165. }
  166. }
  167. wechatLogin() {
  168. //网络请求 status 500
  169. /**
  170. * {
  171. "channel": "006",
  172. "deviceCode": "fb3dcd333348ZX1G42CPJD",
  173. "openId": "odI7ls2Bk_0A2_Y0E6D4dAKuSsqs",
  174. "unionId": "o8sgct5UksQ7kfrSGTBRRgwY5oVU",
  175. "avatar": "",
  176. "sex": "2",
  177. "nickName": "雨露"
  178. }
  179. *
  180. *
  181. *
  182. */
  183. console.log('this.state.deviceCode:' + this.state.deviceCode);
  184. wechat.wechatLogin((user) => {
  185. let option = {
  186. method: 'POST',
  187. body: {
  188. channel: '006',
  189. deviceCode: this.state.deviceCode,
  190. openId: user['openid'],
  191. unionId: user['unionid'],
  192. avatar: user['province'] + user['city'],
  193. sex: +user['sex'],
  194. nickName: user['nickname']
  195. }
  196. };
  197. http_user.wechatLogin(option).then((result) => {
  198. if (result.code == 200) {
  199. //登陆成功了,可以存储用户数据到本地
  200. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  201. } else {
  202. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  203. }
  204. console.log('result-------:' + result);
  205. });
  206. });
  207. }
  208. }
  209. const styles = StyleSheet.create({
  210. wrapper: {
  211. flex: 1
  212. },
  213. jumpText: {
  214. color: '#3e3e3e',
  215. fontSize: 16,
  216. marginRight: 4
  217. },
  218. jump: {
  219. // width: Dimensions.width,
  220. flex: 2,
  221. alignItems: 'flex-end',
  222. justifyContent: 'center',
  223. paddingHorizontal: '8%'
  224. },
  225. jumpBtn: {
  226. flexDirection: 'row',
  227. alignItems: 'center',
  228. justifyContent: 'center'
  229. },
  230. phoneNumberBox: {
  231. flex: 2,
  232. paddingHorizontal: '8%'
  233. },
  234. phoneNumber: {
  235. color: '#3e3e3e',
  236. fontSize: 18,
  237. marginBottom: 10
  238. },
  239. phoneText: {
  240. width: '100%',
  241. height: Dimensions.getHeight(50),
  242. borderRadius: 25,
  243. backgroundColor: '#f3f3f3'
  244. },
  245. signNumberBox: {
  246. flex: 2,
  247. paddingHorizontal: '8%'
  248. },
  249. signNumberLine2: {
  250. flexDirection: 'row',
  251. width: Dimensions.width
  252. },
  253. signText: {
  254. width: '54%',
  255. height: Dimensions.getHeight(50),
  256. borderRadius: 25,
  257. backgroundColor: '#f3f3f3',
  258. marginRight: 9
  259. },
  260. getSign: {
  261. width: 105,
  262. height: Dimensions.getHeight(50),
  263. borderRadius: 25,
  264. backgroundColor: '#38da84',
  265. lineHeight: Dimensions.getHeight(50),
  266. color: '#fff',
  267. fontSize: 16,
  268. textAlign: 'center'
  269. },
  270. loginIn: {
  271. flex: 3,
  272. paddingHorizontal: '8%'
  273. },
  274. loginText: {
  275. width: '100%',
  276. height: Dimensions.getHeight(50),
  277. backgroundColor: '#63aeff',
  278. textAlign: 'center',
  279. lineHeight: Dimensions.getHeight(50),
  280. color: '#fff',
  281. fontSize: 20,
  282. borderRadius: 25
  283. },
  284. wechatLogin: {
  285. flex: 3,
  286. flexDirection: 'row',
  287. paddingHorizontal: '33.6%',
  288. alignItems: 'center',
  289. justifyContent: 'center'
  290. },
  291. wechatLoginText: {
  292. fontSize: 16,
  293. color: '#3e3e3e',
  294. marginHorizontal: 6
  295. }
  296. });