Login_20250812105614.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // entrypoints/popup/pages/Login.tsx
  2. import React, { useState } from 'react';
  3. import { useNavigate } from 'react-router-dom';
  4. import { Button, Input, Message, Form, Grid } from '@alifd/next';
  5. const { Row, Col } = Grid
  6. export default function Login({ setIsLoggedIn }: { setIsLoggedIn: (loggedIn: boolean) => void }) {
  7. const [username, setUsername] = useState('');
  8. const [password, setPassword] = useState('');
  9. const [loading, setLoading] = useState(false);
  10. const [errors, setErrors] = useState<{ username?: string, password?: string }>({});
  11. const [keys, setKeys] = useState('');
  12. const [validImg, setValidImg] = useState('');
  13. const navigate = useNavigate();
  14. useEffect(() => {
  15. evVerify()
  16. }, [])
  17. const validateForm = () => {
  18. const newErrors: { username?: string, password?: string } = {};
  19. if (!username.trim()) {
  20. newErrors.username = '请输入用户名';
  21. }
  22. if (!password) {
  23. newErrors.password = '请输入密码';
  24. } else if (password.length < 6) {
  25. newErrors.password = '密码长度至少为6位';
  26. }
  27. setErrors(newErrors);
  28. return Object.keys(newErrors).length === 0;
  29. };
  30. const evVerify = () => {
  31. console.log('username')
  32. fetch('https://user.landwu.com/api/user/verify', {
  33. method: 'POST',
  34. headers: {
  35. 'Content-Type': 'application/json',
  36. }
  37. }).then(resjson => {
  38. return resjson.json(); // 正确解析 Response 为 JSON 数据
  39. }).then(res => {
  40. const { data = {} } = res;
  41. const { data: info = {} } = data;
  42. const { key = "", img = "" } = info;
  43. setKeys(key)
  44. setValidImg(img)
  45. })
  46. }
  47. const handleLogin = (e: React.FormEvent) => {
  48. console.log(e,"rrrrrrr")
  49. return
  50. e.preventDefault();
  51. if (!validateForm()) {
  52. return;
  53. }
  54. setLoading(true);
  55. try {
  56. const params = {
  57. }
  58. // 发送登录请求
  59. fetch('https://user.landwu.com/api/user/login', {
  60. method: 'POST',
  61. headers: {
  62. 'Content-Type': 'application/json',
  63. },
  64. body: JSON.stringify({
  65. username,
  66. password,
  67. code
  68. }),
  69. }).then(response => response.json()).then(res => {
  70. const { key = "" } = this.state;
  71. values["key"] = key;
  72. login
  73. .login(values)
  74. .then((res) => {
  75. // this.props.isLogin && this.props.isLogin( {access_token:'access_token--11'});
  76. const { msg = "", token = "", code } = res;
  77. if (!code) {
  78. this.setState(() => {
  79. return {
  80. loading: false,
  81. };
  82. });
  83. return false;
  84. }
  85. console.log(res, "--res", code);
  86. if (code == -1) {
  87. console.log("code", code)
  88. this.verify();
  89. message.error(msg);
  90. this.setState(() => {
  91. return {
  92. loading: false,
  93. };
  94. });
  95. return false;
  96. }
  97. message.success(msg);
  98. // console.log(res, "res",token);
  99. // localStorage.setItem("access_token", toCode(token));
  100. localStorage.setItem("access_token", token);
  101. // Cookies.set("access_token", toCode(token));
  102. this.setState(
  103. () => {
  104. return {
  105. loading: true,
  106. };
  107. },
  108. () => {
  109. // this.props.isLogin && this.props.isLogin({ admin: "data.admin" });
  110. // this.props.isLogin && this.props.isLogin({ access_token: token });
  111. console.log(this.props.history.location.pathname, 'historytoCode', toCode(1), fromCode('<(('))
  112. localStorage.removeItem('lw_config_page')
  113. localStorage.removeItem('lw_config_limit')
  114. localStorage.removeItem('lw_menu_config')
  115. localStorage.setItem('nav_index_1', 0)
  116. localStorage.removeItem('nav_index_2')
  117. localStorage.removeItem('nav_index_3')
  118. localStorage.removeItem('is_offline');//是否门店
  119. Loading.init()
  120. setTimeout(() => {
  121. let parms = {}
  122. if (getIsoffline() == 1) {
  123. parms['is_shipping'] = getIsoffline()
  124. }
  125. user
  126. .user_getProfile(parms)
  127. .then((res) => {
  128. Loading.remove()
  129. console.log(res, '获取用户信息')
  130. const { data = {} } = res;
  131. const { userinfo = {} } = data;
  132. this.getUserInfoLog(userinfo, token)
  133. if (userinfo.is_offline) {
  134. localStorage.setItem('is_offline', toCode(userinfo.is_offline))
  135. }
  136. localStorage.setItem("user_info", JSON.stringify(userinfo));
  137. if (!getQuery('is_link') && getQuery('code')) {
  138. this.pdd_pddServiceAuth(() => {
  139. location.reload(true);
  140. })
  141. } else {
  142. Loading.init()
  143. rule.rule_getRuleAuth().then(res => {
  144. Loading.remove()
  145. const { data } = res
  146. let data_menu = data;
  147. console.log(data_menu, '-data_menudata_menudata_menu')
  148. if (data_menu.length && !getQuery('is_link')) {
  149. const _indexUrl = data_menu[0]
  150. const { push } = this.props.history
  151. console.log(_indexUrl, '_indexUrl')
  152. // push('')
  153. push(_indexUrl.url)
  154. // console.log(_indexUrl,'_indexUrl_indexUrl',this.props.history)
  155. }
  156. localStorage.setItem('lw_menu_config', JSON.stringify(data_menu))
  157. window.location.reload(true);
  158. this.props.isLogin &&
  159. this.props.isLogin({
  160. admin: userinfo.username,
  161. access_token: token,
  162. });
  163. }).catch(() => {
  164. Loading.remove()
  165. })
  166. // this.props.history.push('/index')
  167. // console.log(res,'res')
  168. // location.reload(true);
  169. }
  170. return false;
  171. })
  172. .catch((e) => {
  173. Loading.remove()
  174. // location.reload(true);
  175. this.setState(() => {
  176. return {
  177. loading: false,
  178. };
  179. });
  180. });
  181. }, 200);
  182. }
  183. );
  184. })
  185. .catch((e) => {
  186. console.log("catch")
  187. this.setState(() => {
  188. return {
  189. loading: false,
  190. };
  191. });
  192. });
  193. })
  194. } catch (error) {
  195. console.error('登录请求失败:', error);
  196. Message.error('网络错误,请稍后重试');
  197. }
  198. // 模拟登录请求
  199. // setTimeout(() => {
  200. // // 这里添加实际的登录逻辑
  201. // if (username && password) {
  202. // // 模拟登录成功
  203. // localStorage.setItem('isLoggedIn', 'true');
  204. // setIsLoggedIn(true);
  205. // // 跳转到首页
  206. // navigate('/');
  207. // Message.success('登录成功');
  208. // } else {
  209. // Message.error('登录失败,请检查用户名和密码');
  210. // }
  211. // setLoading(false);
  212. // }, 1000);
  213. };
  214. return (
  215. <div className="bg-gray-50 p-4">
  216. <div className="text-center mb-6">
  217. <h1 className="text-2xl font-bold text-gray-800">登录</h1>
  218. </div>
  219. <Form onSubmit={handleLogin}>
  220. <Form.Item
  221. label="用户名"
  222. required
  223. help={errors.username}
  224. validateState={errors.username ? 'error' : undefined}
  225. >
  226. <Input
  227. placeholder="请输入用户名"
  228. value={username}
  229. onChange={(value) => {
  230. setUsername(value);
  231. if (errors.username) {
  232. setErrors({ ...errors, username: undefined });
  233. }
  234. }}
  235. disabled={loading}
  236. />
  237. </Form.Item>
  238. <Form.Item
  239. label="密码"
  240. required
  241. help={errors.password}
  242. validateState={errors.password ? 'error' : undefined}
  243. >
  244. <Input
  245. htmlType="password"
  246. placeholder="请输入密码"
  247. value={password}
  248. onChange={(value) => {
  249. setPassword(value);
  250. if (errors.password) {
  251. setErrors({ ...errors, password: undefined });
  252. }
  253. }}
  254. disabled={loading}
  255. />
  256. </Form.Item>
  257. <Row>
  258. <Col span={16}>
  259. <Form.Item
  260. label="验证码"
  261. name="code"
  262. required
  263. >
  264. <Input
  265. size="large"
  266. />
  267. </Form.Item>
  268. </Col>
  269. <Col span={8}>
  270. <img
  271. src={validImg}
  272. style={{
  273. width: 82,
  274. height: 30,
  275. marginTop: 28
  276. }}
  277. onClick={evVerify}
  278. />
  279. </Col>
  280. </Row>
  281. <Form.Item>
  282. <Button
  283. type="primary"
  284. htmlType="submit"
  285. loading={loading}
  286. className="w-full"
  287. >
  288. {loading ? '登录中...' : '登录'}
  289. </Button>
  290. </Form.Item>
  291. </Form>
  292. <div className="mt-4 text-center text-sm text-gray-500">
  293. <p>忘记密码?请联系管理员</p>
  294. </div>
  295. </div>
  296. );
  297. }