PhoneBind.js 7.2 KB

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