Nessuna descrizione

SignUpViewController.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**
  2. * Created by zack on 2018/6/19.
  3. */
  4. import {
  5. View,
  6. Text,
  7. StyleSheet,
  8. TouchableOpacity,
  9. Platform,
  10. Image,TextInput,
  11. Alert
  12. } from 'react-native'
  13. import React, {Component} from 'react'
  14. import {NavigationBarHeight, TabBarHeight, ScreenDimensions} from '../../utils/DimensionsTools'
  15. import HttpTools from '../../network/HttpTools'
  16. import {signUpByEmail, signUpByPhone, userSmsCode, userEmailCode} from '../../network/API'
  17. import ToastMsg from '../../utils/ToastMsg'
  18. import {GlobalUserStorageTool, UserStorageKey} from '../../utils/GlobalUserStorageTool'
  19. import BaseNavigationBarStyle from '../base/BaseNavigationBarStyle'
  20. export default class SignUpViewController extends Component {
  21. constructor(props) {
  22. super(props)
  23. this.state = {
  24. account: '',
  25. verificationCode: '',
  26. password: '',
  27. phoneCode: '+86',
  28. isEmailLoginType: false
  29. }
  30. }
  31. clickBackButton() {
  32. this.props.navigator.pop()
  33. }
  34. signUp() {
  35. if (!this.state.account.length) {
  36. ToastMsg.show("请输入您的账号");
  37. return
  38. }
  39. if (!this.state.verificationCode.length) {
  40. ToastMsg.show("请输入您的验证码");
  41. return
  42. }
  43. if (!this.state.password.length) {
  44. ToastMsg.show("请输入您的密码");
  45. return
  46. }
  47. if (this.state.password.length < 6) {
  48. ToastMsg.show("密码长度不能少于6位");
  49. return
  50. }
  51. let param
  52. if (this.state.account.indexOf('@') !== -1) { //email
  53. param = JSON.stringify({
  54. code: this.state.verificationCode,
  55. email: this.state.account,
  56. password: this.state.password
  57. })
  58. }else { // phone
  59. param = JSON.stringify({
  60. code: this.state.verificationCode,
  61. password: this.state.password,
  62. phone: this.state.account,
  63. phone_code: this.state.phoneCode //默认使用中国大陆
  64. })
  65. }
  66. HttpTools.post(signUpByPhone, param, (response) => {
  67. if (response && response.token) { //sign up ok
  68. global.token = response.token
  69. GlobalUserStorageTool.save(UserStorageKey.UserInfo, response)
  70. GlobalUserStorageTool.save(UserStorageKey.AccountAndPassword, {account: this.state.account, password: this.state.password})
  71. Alert.alert(
  72. '注册成功',
  73. '',
  74. [
  75. {text: 'OK', onPress: () => {
  76. this.props.navigator.popToRoot()
  77. }},
  78. ],
  79. { cancelable: false }
  80. )
  81. }else {
  82. ToastMsg.show(response && response.msg)
  83. }
  84. }, (error) => {
  85. })
  86. }
  87. didSelectedTag() {
  88. this.setState({isEmailLoginType: !this.state.isEmailLoginType})
  89. }
  90. goToCountCodeList() {
  91. this.props.navigator.push({
  92. screen: 'CountCodeListViewController',
  93. title: '国家和地区',
  94. backButtonTitle: '',
  95. passProps: {
  96. didSelectedItem: (phoneCode)=> {
  97. this.setState({phoneCode: phoneCode})
  98. }
  99. },
  100. navigatorStyle: BaseNavigationBarStyle
  101. })
  102. }
  103. getVerificationCode() {
  104. let param
  105. let url = ''
  106. if (this.state.account.indexOf('@') !== -1) {
  107. url = userEmailCode
  108. param = JSON.stringify({
  109. email: this.state.account,
  110. purpose: "register"
  111. })
  112. }else {
  113. url = userSmsCode
  114. param = JSON.stringify({
  115. phone: this.state.account,
  116. phone_code: this.state.phoneCode,
  117. purpose: "register"
  118. })
  119. }
  120. HttpTools.post(url, param, (response) => {
  121. }, (error) => {
  122. })
  123. }
  124. renderAccountView(isEmailLoginType) {
  125. if (!isEmailLoginType) {
  126. return(
  127. <View style={{width: ScreenDimensions.width - 30, height: 49, flexDirection: 'row', alignItems: 'center'}}>
  128. <TouchableOpacity onPress={() => {
  129. this.goToCountCodeList()
  130. }} style={{marginLeft: 0, width: 43, height: 47, justifyContent: 'center', }}>
  131. <Text style={{color: '#b8bcbf', fontSize: 14}}>{this.state.phoneCode}</Text>
  132. </TouchableOpacity>
  133. <View style={{width: 0.5, height: 21,backgroundColor: 'rgba(80, 80, 80, 0.36)'}}/>
  134. <TextInput
  135. onChangeText={(text) => {
  136. this.setState({account: text})
  137. }}
  138. keyboardType={'email-address'}
  139. placeholder={ '请输入手机号'} placeholderTextColor={'#b8bcbf'}
  140. style={[styles.AccountTextInput, {width: ScreenDimensions.width - 30 - 44, paddingLeft: 17}]}/>
  141. </View>
  142. )
  143. }else {
  144. return(
  145. <TextInput onChangeText={(text) => {
  146. this.setState({account: text})
  147. }} keyboardType={'email-address'} placeholder={'请输入邮箱'} placeholderTextColor={'#b8bcbf'} style={[styles.AccountTextInput, {width: ScreenDimensions.width - 30,}]}/>
  148. )
  149. }
  150. }
  151. render() {
  152. return(
  153. <View style={styles.View}>
  154. <View style={styles.NavigationBarBgView}>
  155. <View style={styles.NavigationBar}>
  156. <TouchableOpacity onPress={() => {
  157. this.clickBackButton()
  158. }} style={styles.BackImageView}>
  159. <Image source={require("../../resources/images/TabBar/Learn/btn_back.png")}/>
  160. </TouchableOpacity>
  161. <Image source={require('../../resources/images/LoginSignUp/logo.png')} style={styles.LogoImageView}/>
  162. </View>
  163. </View>
  164. <View style={styles.TagButtonView}>
  165. <TouchableOpacity onPress={() => {
  166. if (this.state.isEmailLoginType) {
  167. this.didSelectedTag()
  168. }
  169. }} style={[{marginRight: 92}]}>
  170. <Text style={this.state.isEmailLoginType === false ? styles.TagButtonTextSelected : styles.TagButtonTextNormal}>{'手机验证码'}</Text>
  171. </TouchableOpacity>
  172. <TouchableOpacity onPress={() => {
  173. if (!this.state.isEmailLoginType) {
  174. this.didSelectedTag()
  175. }
  176. }} style={styles.TagButtonText}>
  177. <Text style={this.state.isEmailLoginType === true ? styles.TagButtonTextSelected : styles.TagButtonTextNormal}>{'邮箱验证码'}</Text>
  178. </TouchableOpacity>
  179. </View>
  180. <View style={styles.AccountTextInputBgView}>
  181. {this.renderAccountView(this.state.isEmailLoginType)}
  182. <View style={styles.LineView}/>
  183. </View>
  184. <View style={styles.VerifyTextInputBgView}>
  185. <View style={styles.VerifyTextInputContainerView}>
  186. <TextInput onChangeText={(text) => {
  187. this.setState({verificationCode: text})
  188. }} keyboardType={'phone-pad'} placeholder={'请输入验证码'} placeholderTextColor={'#b8bcbf'} style={styles.VerifyTextInput}/>
  189. <TouchableOpacity onPress={() => {
  190. this.getVerificationCode()
  191. }} style={styles.VerifyButtonBgView}>
  192. <View style={styles.VerifyLineView}/>
  193. <Text style={styles.VerifyButtonText}>{'获取验证码'}</Text>
  194. </TouchableOpacity>
  195. </View>
  196. <View style={styles.LineView}/>
  197. </View>
  198. <View style={styles.PasswordTextInputBgView}>
  199. <TextInput onChangeText={(text) => {
  200. this.setState({password: text})
  201. }} secureTextEntry={true} placeholder={'请输入密码'} placeholderTextColor={'#b8bcbf'} style={styles.PasswordTextInput}/>
  202. <View style={styles.LineView}/>
  203. </View>
  204. <TouchableOpacity onPress={() => {
  205. this.signUp()
  206. }} style = {styles.SignUpButton}>
  207. <Text style={styles.SignUpButtonText}>{'注册'}</Text>
  208. </TouchableOpacity>
  209. <View style={styles.LoginButtonBgView}>
  210. <TouchableOpacity onPress={() => {
  211. this.props.navigator.pop()
  212. }} style={styles.LoginButton}>
  213. <Text style={styles.LoginText1}>{'已有账号?'}
  214. <Text style={styles.LoginText2}>{'马上登录'}</Text>
  215. </Text>
  216. </TouchableOpacity>
  217. </View>
  218. <View style={styles.BottomSignUpView}>
  219. <View style={styles.VBgView}>
  220. <View style={[styles.VLineView, {marginLeft: 15,}]}/>
  221. <Text style={styles.VText}>{'使用其他方式登录'}</Text>
  222. <View style={styles.VLineView}/>
  223. <View />
  224. </View>
  225. <View style={styles.IconBgView}>
  226. <TouchableOpacity style={styles.IconView}>
  227. <Image source={require('../../resources/images/LoginSignUp/wechat.png')}/>
  228. </TouchableOpacity>
  229. <TouchableOpacity style={styles.IconView}>
  230. <Image source={require('../../resources/images/LoginSignUp/QQ.png')}/>
  231. </TouchableOpacity>
  232. <TouchableOpacity style={styles.IconView}>
  233. <Image source={require('../../resources/images/LoginSignUp/weibo.png')}/>
  234. </TouchableOpacity>
  235. <TouchableOpacity style={styles.IconView}>
  236. <Image source={require('../../resources/images/LoginSignUp/Facebook.png')}/>
  237. </TouchableOpacity>
  238. <TouchableOpacity style={styles.IconView}>
  239. <Image source={require('../../resources/images/LoginSignUp/twitter.png')}/>
  240. </TouchableOpacity>
  241. <TouchableOpacity style={styles.IconView}>
  242. <Image source={require('../../resources/images/LoginSignUp/Google.png')}/>
  243. </TouchableOpacity>
  244. </View>
  245. </View>
  246. </View>
  247. )
  248. }
  249. }
  250. const styles = StyleSheet.create({
  251. View: {
  252. width: ScreenDimensions.width,
  253. height: ScreenDimensions.height,
  254. backgroundColor: 'white'
  255. },
  256. NavigationBarBgView: {
  257. position: 'absolute',
  258. left: 0,
  259. top: 0,
  260. height: NavigationBarHeight.height,
  261. width: ScreenDimensions.width,
  262. backgroundColor: "#ffffff",
  263. shadowColor: "#eeeeee",
  264. shadowOffset: {
  265. width: 0,
  266. height: -1
  267. },
  268. shadowRadius: 0,
  269. shadowOpacity: 1,
  270. flexDirection: 'row',
  271. alignItems: 'center',
  272. justifyContent: 'space-between',
  273. elevation: 5, //only for android
  274. },
  275. NavigationBar: {
  276. position: 'absolute',
  277. left: 0,
  278. top: 0,
  279. height: NavigationBarHeight.height,
  280. width: ScreenDimensions.width,
  281. backgroundColor: "#ffffff",
  282. shadowColor: "#eeeeee",
  283. shadowOffset: {
  284. width: 0,
  285. height: -1
  286. },
  287. shadowRadius: 0,
  288. shadowOpacity: 1,
  289. flexDirection: 'row',
  290. alignItems: 'center',
  291. justifyContent: 'center',
  292. elevation: 5, //only for android
  293. },
  294. LogoImageView: {
  295. width: 80,
  296. height: 25,
  297. },
  298. BackImageView: {
  299. position: 'absolute',
  300. left: 15,
  301. top: (NavigationBarHeight.height - 15)/2.0
  302. },
  303. AccountTextInputBgView: {
  304. width: ScreenDimensions.width - 30,
  305. height: 50,
  306. marginLeft: 15,
  307. marginTop: 35
  308. },
  309. LineView: {
  310. width: ScreenDimensions.width - 30,
  311. backgroundColor: '#efeff4',
  312. height: 0.5,
  313. },
  314. AccountTextInput: {
  315. textAlign: 'left',
  316. fontSize: 14,
  317. color: '#4A4A4A',
  318. height:49,
  319. },
  320. VerifyTextInputBgView: {
  321. width: ScreenDimensions.width - 30,
  322. height: 50,
  323. marginLeft: 15,
  324. marginTop: 28
  325. },
  326. VerifyTextInputContainerView: {
  327. width: ScreenDimensions.width - 30,
  328. height: 49,
  329. flexDirection: 'row',
  330. alignItems: 'center',
  331. justifyContent: 'space-between'
  332. },
  333. VerifyTextInput: {
  334. width: ScreenDimensions.width - 30 - 100,
  335. textAlign: 'left',
  336. fontSize: 14,
  337. color: '#4A4A4A',
  338. height:49,
  339. },
  340. VerifyButtonBgView: {
  341. width: 100,
  342. flexDirection: 'row',
  343. alignItems: 'center',
  344. justifyContent: 'space-between',
  345. },
  346. VerifyButtonText: {
  347. fontSize: 14,
  348. color: '#5a9cd9',
  349. },
  350. VerifyLineView: {
  351. width: 1,
  352. height: 21,
  353. backgroundColor: 'rgba(80, 80, 80, 0.36)'
  354. },
  355. PasswordTextInputBgView: {
  356. width: ScreenDimensions.width - 30,
  357. height: 50,
  358. marginLeft: 15,
  359. marginTop: 28,
  360. },
  361. PasswordTextInput: {
  362. width: ScreenDimensions.width - 30,
  363. textAlign: 'left',
  364. fontSize: 14,
  365. color: '#4A4A4A',
  366. height:49,
  367. },
  368. SignUpButton: {
  369. width:ScreenDimensions.width - 30,
  370. height: 50,
  371. backgroundColor: '#e66a6a',
  372. justifyContent: 'center',
  373. alignItems: 'center',
  374. borderRadius: 5,
  375. marginTop: 26,
  376. marginLeft: 15,
  377. },
  378. SignUpButtonText: {
  379. fontSize: 16,
  380. color: '#ffffff'
  381. },
  382. LoginButtonBgView: {
  383. width: ScreenDimensions.width,
  384. justifyContent: 'center',
  385. alignItems: 'center',
  386. marginTop: 18
  387. },
  388. LoginButton: {
  389. },
  390. LoginText1: {
  391. fontSize: 14,
  392. color: '#909090'
  393. },
  394. LoginText2: {
  395. fontSize: 14,
  396. color: '#5a9cd9'
  397. },
  398. BottomSignUpView: {
  399. position: 'absolute',
  400. left: 0,
  401. bottom: 0,
  402. width: ScreenDimensions.width,
  403. height: 100,
  404. },
  405. VBgView: {
  406. flexDirection: 'row',
  407. alignItems: 'center',
  408. justifyContent: 'space-between',
  409. width: ScreenDimensions.width
  410. },
  411. VLineView: {
  412. width: (ScreenDimensions.width - 120 - 15*4)/2.0,
  413. height: 0.5,
  414. backgroundColor: '#e1e3e6'
  415. },
  416. VText: {
  417. fontSize: 14,
  418. color: '#8c8c8c',
  419. width: 120,
  420. textAlign: 'center'
  421. },
  422. IconBgView: {
  423. width: ScreenDimensions.width,
  424. height: 25,
  425. flexDirection: 'row',
  426. alignItems: 'center',
  427. justifyContent: 'space-around',
  428. marginTop: 32
  429. },
  430. IconView: {
  431. width: 25,
  432. height: 25,
  433. },
  434. TagButtonView: {
  435. width: ScreenDimensions.width,
  436. flexDirection: 'row',
  437. alignItems: 'center',
  438. justifyContent: 'center',
  439. marginTop: NavigationBarHeight.height + 40
  440. },
  441. TagButtonTextNormal: {
  442. fontSize: 16,
  443. color: '#505050',
  444. },
  445. TagButtonTextSelected: {
  446. fontSize: 16,
  447. color: '#5a9cd9',
  448. },
  449. })