PhoneBind.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * 登录
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. Image,
  10. TouchableOpacity,
  11. ToastAndroid,
  12. StatusBar,
  13. TextInput,
  14. BackHandler
  15. } from 'react-native';
  16. import BasePage from './BasePage';
  17. import Dimensions from './utils/dimensions';
  18. import http_user from './services/user';
  19. import commonutil from './utils/commonutil';
  20. export default class PhoneBind extends BasePage {
  21. state = {
  22. type: '',
  23. phone_num: '',
  24. verification_text: '获取验证码',
  25. verification_code: '',
  26. http_verification_code: '',
  27. page_title_text: '绑定手机号',
  28. click_ok_text: '绑 定',
  29. phone_bind_result: false
  30. };
  31. render() {
  32. return (
  33. <View style={styles.wrapper}>
  34. <StatusBar backgroundColor={'white'} translucent={true} barStyle={'dark-content'} />
  35. <View style={{ flex: 2 }}>
  36. <View style={{ marginTop: '6%', flex: 2, flexDirection: 'row' }}>
  37. <TouchableOpacity
  38. style={{ marginLeft: '5%' }}
  39. activeOpacity={1}
  40. onPress={this.backresult.bind(this)}
  41. >
  42. <Image source={require('./images/schedulePage/back_black.png')} />
  43. </TouchableOpacity>
  44. </View>
  45. <View style={styles.jump}>
  46. <TouchableOpacity style={styles.jumpBtn}>
  47. <Text style={styles.jumpText}>{this.state.page_title_text}</Text>
  48. </TouchableOpacity>
  49. </View>
  50. </View>
  51. <View style={styles.phoneNumberBox}>
  52. <Text style={styles.phoneNumber}>手机号</Text>
  53. <TextInput
  54. style={styles.phoneText}
  55. maxLength={11}
  56. onChangeText={(text) => this.setState({ phone_num: text })}
  57. value={this.state.phone_num}
  58. placeholder={'请输入手机号'}
  59. />
  60. </View>
  61. <View style={styles.signNumberBox}>
  62. <Text style={styles.phoneNumber}>验证码</Text>
  63. <View style={styles.signNumberLine2}>
  64. <TextInput
  65. style={styles.signText}
  66. maxLength={4}
  67. onChangeText={(text) => this.setState({ verification_code: text })}
  68. value={this.state.verification_code}
  69. placeholder={'请输入验证码'}
  70. />
  71. <TouchableOpacity>
  72. <Text style={styles.getSign} onPress={this.getVerification.bind(this)}>
  73. {this.state.verification_text}
  74. </Text>
  75. </TouchableOpacity>
  76. </View>
  77. </View>
  78. <View style={styles.loginIn}>
  79. <Text style={styles.loginText} onPress={this.clickOK.bind(this)}>
  80. {this.state.click_ok_text}
  81. </Text>
  82. </View>
  83. <View style={styles.wechatLogin}>
  84. {/* <Image source={require('./images/common/wechat.png')} /> */}
  85. <Text style={styles.wechatLoginText} />
  86. {/* <Image source={require('./images/common/arrowRight.png')} /> */}
  87. </View>
  88. </View>
  89. );
  90. }
  91. componentWillMount() {
  92. BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
  93. var type;
  94. if (this.props.navigation.state.params == undefined) {
  95. type = 1;
  96. } else {
  97. type = this.props.navigation.state.params.type;
  98. }
  99. switch (type) {
  100. case 1:
  101. this.setState({
  102. type: type,
  103. page_title_text: '绑定手机号',
  104. click_ok_text: '绑 定'
  105. });
  106. break;
  107. case 2:
  108. this.setState({
  109. type: type,
  110. page_title_text: '修改手机号',
  111. click_ok_text: '修 改'
  112. });
  113. break;
  114. }
  115. }
  116. componentWillUnmount() {
  117. BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
  118. }
  119. onBackAndroid = () => {
  120. this.props.navigation.state.params.bind_phone_back(this.state.phone_num, this.state.phone_bind_result);
  121. this.props.navigation.goBack();
  122. return true;
  123. };
  124. getVerification() {
  125. if (this.state.verification_text === '获取验证码') {
  126. if (commonutil.isPoneAvailable(this.state.phone_num)) {
  127. http_user.getVerificationCode(this.state.phone_num).then((result) => {
  128. if (result.code != 200) {
  129. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  130. return;
  131. } else {
  132. console.log('====================================');
  133. console.log(result.data);
  134. console.log('====================================');
  135. this.setState({
  136. http_verification_code: result.data
  137. });
  138. }
  139. this.setState({
  140. verification_text: '60'
  141. });
  142. this.CountDown();
  143. });
  144. } else {
  145. ToastAndroid.show('请输入正确的手机号', ToastAndroid.SHORT);
  146. }
  147. } else {
  148. }
  149. }
  150. CountDown() {
  151. if (parseInt(this.state.verification_text) == 0) {
  152. this.setState({
  153. verification_text: '获取验证码'
  154. });
  155. } else {
  156. setTimeout(() => {
  157. this.setState({
  158. verification_text: parseInt(this.state.verification_text) - 1 + ''
  159. });
  160. this.CountDown();
  161. }, 1000);
  162. }
  163. }
  164. //绑定手机号
  165. clickOK() {
  166. if (!commonutil.isPoneAvailable(this.state.phone_num)) {
  167. ToastAndroid.show('请输入正确的手机号', ToastAndroid.SHORT);
  168. return;
  169. }
  170. if (this.state.verification_code == '') {
  171. ToastAndroid.show('请输入验证码', ToastAndroid.SHORT);
  172. return;
  173. }
  174. if (this.state.http_verification_code == this.state.verification_code) {
  175. let option = {
  176. method: 'PUT', //请求方法
  177. //请求体
  178. body: {
  179. mobile: this.state.phone_num,
  180. sign: this.state.verification_code
  181. }
  182. };
  183. http_user.bind_phone(option).then((result) => {
  184. if (result.code == 200) {
  185. this.setState({
  186. phone_bind_result: true
  187. });
  188. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  189. } else {
  190. this.setState({
  191. phone_bind_result: false
  192. });
  193. ToastAndroid.show(result.message, ToastAndroid.SHORT);
  194. }
  195. });
  196. } else {
  197. ToastAndroid.show('验证码不正确', ToastAndroid.SHORT);
  198. }
  199. }
  200. backresult() {
  201. this.props.navigation.state.params.bind_phone_back(this.state.phone_num, this.state.phone_bind_result);
  202. this.props.navigation.goBack();
  203. }
  204. }
  205. const styles = StyleSheet.create({
  206. wrapper: {
  207. flex: 1
  208. },
  209. jumpText: {
  210. color: '#3e3e3e',
  211. fontSize: 16,
  212. marginRight: 4
  213. },
  214. jump: {
  215. // width: Dimensions.width,
  216. flex: 2,
  217. alignItems: 'center',
  218. justifyContent: 'center',
  219. paddingHorizontal: '8%'
  220. },
  221. jumpBtn: {
  222. flexDirection: 'row',
  223. alignItems: 'center',
  224. justifyContent: 'center'
  225. },
  226. phoneNumberBox: {
  227. flex: 2,
  228. paddingHorizontal: '8%'
  229. },
  230. phoneNumber: {
  231. color: '#3e3e3e',
  232. fontSize: 18,
  233. marginBottom: 10
  234. },
  235. phoneText: {
  236. width: '100%',
  237. height: Dimensions.getHeight(50),
  238. borderRadius: 25,
  239. backgroundColor: '#f3f3f3'
  240. },
  241. signNumberBox: {
  242. flex: 2,
  243. paddingHorizontal: '8%'
  244. },
  245. signNumberLine2: {
  246. flexDirection: 'row',
  247. width: Dimensions.width
  248. },
  249. signText: {
  250. width: '54%',
  251. height: Dimensions.getHeight(50),
  252. borderRadius: 25,
  253. backgroundColor: '#f3f3f3',
  254. marginRight: 9
  255. },
  256. getSign: {
  257. width: 105,
  258. height: Dimensions.getHeight(50),
  259. borderRadius: 25,
  260. backgroundColor: '#38da84',
  261. lineHeight: Dimensions.getHeight(50),
  262. color: '#fff',
  263. fontSize: 16,
  264. textAlign: 'center'
  265. },
  266. loginIn: {
  267. flex: 3,
  268. paddingHorizontal: '8%'
  269. },
  270. loginText: {
  271. width: '100%',
  272. height: Dimensions.getHeight(50),
  273. backgroundColor: '#63aeff',
  274. textAlign: 'center',
  275. lineHeight: Dimensions.getHeight(50),
  276. color: '#fff',
  277. fontSize: 20,
  278. borderRadius: 25
  279. },
  280. wechatLogin: {
  281. flex: 3,
  282. flexDirection: 'row',
  283. paddingHorizontal: '33.6%',
  284. alignItems: 'center',
  285. justifyContent: 'center'
  286. },
  287. wechatLoginText: {
  288. fontSize: 16,
  289. color: '#3e3e3e',
  290. marginHorizontal: 6
  291. }
  292. });