PhoneBind.js 7.2 KB

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