LoginScreen.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* eslint-disable no-unused-vars */
  2. /* eslint-disable camelcase */
  3. import * as WebBrowser from "expo-web-browser";
  4. import * as React from "react";
  5. import { Image, StyleSheet, View } from "react-native";
  6. import { Layout, Tab, TabView, useTheme, Button } from "@ui-kitten/components";
  7. import { useModel } from "flooks";
  8. import { useFocusEffect } from "@react-navigation/native";
  9. import * as RootNavigation from "../navigation/RootNavigation";
  10. import FormInput from "../components/FormInput";
  11. import SmsInput from "../components/SmsInput";
  12. // import { useAndroidBackHandler } from "react-navigation-backhandler";
  13. const styles = StyleSheet.create({
  14. container: {
  15. flex: 1,
  16. alignItems: "center",
  17. justifyContent: "center",
  18. },
  19. center: {
  20. alignSelf: "stretch",
  21. marginHorizontal: 13,
  22. backgroundColor: "#fff",
  23. paddingTop: 20,
  24. paddingBottom: 10,
  25. borderRadius: 7,
  26. },
  27. tabContent: {
  28. backgroundColor: "#fff",
  29. marginTop: 20,
  30. paddingHorizontal: 9,
  31. },
  32. tabMain: {
  33. // paddingVertical: 10,
  34. width: "100%",
  35. paddingTop: 10,
  36. borderTopWidth: 1,
  37. borderColor: "#E5E5E5",
  38. },
  39. logo: {
  40. width: 217,
  41. height: 100,
  42. alignSelf: "center",
  43. },
  44. image: {
  45. width: 256,
  46. height: 100,
  47. position: "absolute",
  48. left: "50%",
  49. top: -65,
  50. marginLeft: -128,
  51. },
  52. tab: {
  53. borderBottomWidth: 1,
  54. borderColor: "#e5e5e5",
  55. marginHorizontal: 10,
  56. },
  57. tabItem: {
  58. paddingTop: 20,
  59. paddingBottom: 10,
  60. textAlign: "center",
  61. color: "#000",
  62. fontSize: 13,
  63. },
  64. tabItemActive: {
  65. paddingTop: 20,
  66. paddingBottom: 11,
  67. textAlign: "center",
  68. fontSize: 13,
  69. color: "#FFC21C",
  70. },
  71. form: {
  72. paddingTop: 10,
  73. },
  74. layoutLeft: {
  75. paddingLeft: 70,
  76. flexDirection: "row",
  77. paddingBottom: 10,
  78. },
  79. btnList: {
  80. flexDirection: "row",
  81. justifyContent: "space-between",
  82. paddingLeft: 60,
  83. marginTop: -10,
  84. marginBottom: 10,
  85. },
  86. btn: {
  87. paddingVertical: 0,
  88. },
  89. formSub: {
  90. alignItems: "flex-start",
  91. },
  92. primary: {
  93. minWidth: 112,
  94. paddingHorizontal: 10,
  95. },
  96. });
  97. const img1 = require("../assets/images/loginImg.png");
  98. const img2 = require("../assets/images/loginLogo.png");
  99. export default function LoginScreen() {
  100. const theme = useTheme();
  101. const { changeBackground } = useModel("barModel", true);
  102. const {
  103. welcom,
  104. login_tab_1,
  105. login_tab_2,
  106. login_form_1,
  107. login_pla_1,
  108. login_form_2,
  109. login_pla_2,
  110. login_form_3,
  111. login_pla_3,
  112. login_btn_sub,
  113. login_btn_forget,
  114. login_btn_rej,
  115. login_btn_code_1,
  116. login_btn_code_2,
  117. } = useModel("wordsModel");
  118. const tabs = [
  119. {
  120. value: 1,
  121. label: login_tab_1,
  122. },
  123. {
  124. value: 2,
  125. label: login_tab_2,
  126. },
  127. ];
  128. const [selectedIndex, setSelectedIndex] = React.useState(0);
  129. const [phone, changePhone] = React.useState("");
  130. const [password, changePassword] = React.useState("");
  131. const [code, changeCode] = React.useState("");
  132. const [sendNum, changeNum] = React.useState(60);
  133. const [isSend, changeSend] = React.useState(false);
  134. const canLogin = React.useMemo(() => {
  135. if (
  136. (selectedIndex === 0 && phone && password) ||
  137. (selectedIndex === 1 && phone && code)
  138. ) {
  139. return true;
  140. // eslint-disable-next-line no-else-return
  141. } else {
  142. return false;
  143. }
  144. }, [phone, password, code, selectedIndex]);
  145. const { loginByPassword, loginByCode } = useModel("userModel", true);
  146. useFocusEffect(
  147. React.useCallback(() => {
  148. changeBackground(theme["color-primary-100"]);
  149. }, [])
  150. );
  151. return (
  152. <View
  153. style={[
  154. styles.container,
  155. { backgroundColor: theme["color-primary-100"] },
  156. ]}
  157. >
  158. <View style={styles.center}>
  159. <Image source={img1} style={styles.image} />
  160. <Image source={img2} style={styles.logo} />
  161. <View style={styles.tabContent}>
  162. <TabView
  163. selectedIndex={selectedIndex}
  164. onSelect={index => setSelectedIndex(index)}
  165. indicatorStyle={{ height: 0 }}
  166. >
  167. <Tab title={login_tab_1}>
  168. <Layout style={styles.tabMain}>
  169. {/* 手机号 */}
  170. <FormInput
  171. label={`${login_form_1}:`}
  172. value={phone}
  173. type="phone"
  174. placeholder={login_pla_1}
  175. onChange={changePhone}
  176. textAlign="right"
  177. labelStyle={{ width: 50 }}
  178. />
  179. {/* 密码 */}
  180. <FormInput
  181. label={`${login_form_2}:`}
  182. value={password}
  183. type="password"
  184. placeholder={login_pla_2}
  185. onChange={changePassword}
  186. textAlign="right"
  187. labelStyle={{ width: 50 }}
  188. />
  189. </Layout>
  190. </Tab>
  191. <Tab title={login_tab_2}>
  192. <Layout style={styles.tabMain}>
  193. {/* 手机号 */}
  194. <FormInput
  195. label={`${login_form_1}:`}
  196. value={phone}
  197. type="phone"
  198. placeholder={login_pla_1}
  199. onChange={changePhone}
  200. textAlign="right"
  201. labelStyle={{ width: 50 }}
  202. />
  203. {/* 验证码 */}
  204. <SmsInput
  205. label={`${login_form_3}:`}
  206. placeholder={login_pla_3}
  207. labelWidth={50}
  208. phone={phone}
  209. onCodeChange={changeCode}
  210. type="login"
  211. />
  212. </Layout>
  213. </Tab>
  214. </TabView>
  215. {/* 注册找回密码 */}
  216. <Layout style={styles.btnList} level="1">
  217. <Button appearance="ghost" status="basic" size="small">
  218. {login_btn_forget}
  219. </Button>
  220. <Button
  221. appearance="ghost"
  222. status="basic"
  223. size="tiny"
  224. onPress={() => RootNavigation.navigate("Register")}
  225. >
  226. {login_btn_rej}
  227. </Button>
  228. </Layout>
  229. {/* 登录 */}
  230. <Layout style={styles.layoutLeft} level="1">
  231. <Button
  232. status="primary"
  233. disabled={!canLogin}
  234. onPress={() => {
  235. if (selectedIndex === 0) {
  236. loginByPassword(phone, password);
  237. } else {
  238. loginByCode(phone, code);
  239. }
  240. }}
  241. >
  242. {login_btn_sub}
  243. </Button>
  244. </Layout>
  245. </View>
  246. </View>
  247. </View>
  248. );
  249. }