chao 9 months ago
parent
commit
f9000e1dd7
7 changed files with 77 additions and 28 deletions
  1. 30 0
      package-lock.json
  2. 1 0
      package.json
  3. 25 0
      src/App.js
  4. 4 4
      src/config.js
  5. 1 0
      src/http/http.js
  6. 3 3
      src/index.js
  7. 13 21
      src/view/login.js

+ 30 - 0
package-lock.json

@@ -16,6 +16,7 @@
         "@testing-library/jest-dom": "^6.6.3",
         "@testing-library/react": "^16.2.0",
         "@testing-library/user-event": "^13.5.0",
+        "axios": "^1.8.4",
         "qs": "^6.14.0",
         "react": "^19.0.0",
         "react-dom": "^19.0.0",
@@ -5178,6 +5179,30 @@
         "node": ">=4"
       }
     },
+    "node_modules/axios": {
+      "version": "1.8.4",
+      "resolved": "https://registry.npmjs.org/axios/-/axios-1.8.4.tgz",
+      "integrity": "sha512-eBSYY4Y68NNlHbHBMdeDmKNtDgXWhQsJcGqzO3iLUM0GraQFSS9cVgPX5I9b3lbdFKyYoAEGAZF1DwhTaljNAw==",
+      "dependencies": {
+        "follow-redirects": "^1.15.6",
+        "form-data": "^4.0.0",
+        "proxy-from-env": "^1.1.0"
+      }
+    },
+    "node_modules/axios/node_modules/form-data": {
+      "version": "4.0.2",
+      "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.2.tgz",
+      "integrity": "sha512-hGfm/slu0ZabnNt4oaRZ6uREyfCj6P4fT/n6A1rGV+Z0VdGXjfOhVUpkn6qVQONHGIFwmveGXyDs75+nr6FM8w==",
+      "dependencies": {
+        "asynckit": "^0.4.0",
+        "combined-stream": "^1.0.8",
+        "es-set-tostringtag": "^2.1.0",
+        "mime-types": "^2.1.12"
+      },
+      "engines": {
+        "node": ">= 6"
+      }
+    },
     "node_modules/axobject-query": {
       "version": "4.1.0",
       "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-4.1.0.tgz",
@@ -13305,6 +13330,11 @@
         "node": ">= 0.10"
       }
     },
+    "node_modules/proxy-from-env": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
+      "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
+    },
     "node_modules/psl": {
       "version": "1.15.0",
       "resolved": "https://registry.npmjs.org/psl/-/psl-1.15.0.tgz",

+ 1 - 0
package.json

@@ -11,6 +11,7 @@
     "@testing-library/jest-dom": "^6.6.3",
     "@testing-library/react": "^16.2.0",
     "@testing-library/user-event": "^13.5.0",
+    "axios": "^1.8.4",
     "qs": "^6.14.0",
     "react": "^19.0.0",
     "react-dom": "^19.0.0",

+ 25 - 0
src/App.js

@@ -6,6 +6,9 @@ import SignIn from './view/signIn';
 import hans from './assets/json/hans.json'
 import hant from './assets/json/hant.json'
 import en from './assets/json/en.json'
+import httpRequest from './http/http'
+import config from './config'
+import axios from 'axios';
 function App() {
   const [lang, setLang] = useState(hans)
   useEffect(() => {
@@ -16,7 +19,29 @@ function App() {
     } else {
       setLang(hans)
     }
+    axios.defaults.withCredentials = true;
+    axios.get(`${config.api}api/getcookieuserid`).then(res => {
+      if (res.data.length > 0) {
+        axios.get(`${config.edu}admin/userinfo/userinfoById/${res.data[0][0].userid}`).then(r => {
+          if (r.data.length > 0) {
+            postMessageToParent('loginVerify', 'logged', r.data[0][0])
+          } 
+        })
+      }
+    }).catch(e => {
+      console.log(e)
+    })
+
   }, [])
+  const postMessageToParent = (id, status, data) => {
+    //TODO: replace origin
+    let result = {
+      id,
+      status,
+      data
+    }
+    window.parent.postMessage(result, '*')
+  }
   return (
     <BrowserRouter>
       <Routes>

+ 4 - 4
src/config.js

@@ -14,11 +14,11 @@
 // }
 const config = {}
 if (window.location.hostname.indexOf('cocorobo.com') > -1) {
-    config.edu = 'https://api.edu.cocorobo.com/edu/';
-    config.api = 'https://beta.api.cocorobo.com/';
+    config.edu = 'https://api.edu.cocorobo.cn/edu/';
+    config.api = 'https://cloud.api.cocorobo.com/';
 } else if (window.location.hostname.indexOf('cocorobo.hk') > -1) {
-    config.edu = 'https://api.edu.cocorobo.hk/edu/';
-    config.api = 'https://beta.api.cocorobo.hk/';
+    config.edu = 'https://api.edu.cocorobo.cn/edu/';
+    config.api = 'https://cloud.api.cocorobo.hk/';
 } else {
     config.edu = 'https://api.edu.cocorobo.cn/edu/';
     config.api = 'https://beta.api.cocorobo.cn/';

+ 1 - 0
src/http/http.js

@@ -4,6 +4,7 @@ const httpRequest = (url, options = {}) =>{
     const config = {
         ...options,
         mode: 'cors',
+        Credentials: 'include',
         headers: {
             ...options.headers,
         },

+ 3 - 3
src/index.js

@@ -6,9 +6,9 @@ import reportWebVitals from './reportWebVitals';
 
 const root = ReactDOM.createRoot(document.getElementById('root'));
 root.render(
-  <React.StrictMode>
-    <App />
-  </React.StrictMode>
+  // <React.StrictMode>
+  <App />
+  // </React.StrictMode>
 );
 
 // If you want to start measuring performance in your app, pass a function

+ 13 - 21
src/view/login.js

@@ -8,6 +8,7 @@ import { Alert, CircularProgress } from '@mui/joy';
 // import CircularProgress from '@mui/joy/CircularProgress';
 import httpRequest from '../http/http'
 import qs from 'qs';
+import axios from 'axios'
 
 // eslint-disable-next-line no-useless-escape
 const regEmail = new RegExp("^[A-Za-z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$");
@@ -52,23 +53,19 @@ function Login(props) {
         let progressTimer = setInterval(() => {
             setProgress()
         }, 100);
+        let aa = window.location.hostname.indexOf('cocorobo.cn') > -1||window.location.hostname.indexOf('localhost') > -1
         let params = {
             loginUsername: email.trim(),
-            loginPassword: window.btoa(passworld.trim()),
+            // loginPassword: passworld.trim(),
+            loginPassword: aa ? window.btoa(passworld.trim()) : passworld.trim(),
             geetest_challenge: "",
             geetest_validate: "",
             geetest_seccode: ""
         }
-        httpRequest(`${config.api}api/user`, {
-            method: 'POST',
-            headers: {
-                'Content-Type': 'application/json'
-            },
-            body: JSON.stringify(params)
-        }).then(res => {
-            console.log(res)
+        axios.defaults.withCredentials = true;
+        axios.post(`${config.api}api/user`, qs.stringify(params)).then(res => {
             clearInterval(progressTimer)
-            if (res[0].length > 0) {
+            if (res.data.length > 0) {
                 setProgressValue(100)
                 showAlert(props.lang.loginSuccess)
                 setAlertShow(true)
@@ -76,7 +73,7 @@ function Login(props) {
                     setLoading(false)
                     setProgressValue(0)
                     setAlertShow(false)
-                    postMessageToParent('loginVerify', 'logged', res[0][0])
+                    postMessageToParent('loginVerify', 'logged', res.data[0][0])
                 }, 1000)
             } else {
                 setLoading(false)
@@ -94,6 +91,7 @@ function Login(props) {
             showAlert(props.lang.loginError)
             console.log(err)
         })
+        
     }
     const postMessageToParent = (id, status, data) => {
         //TODO: replace origin
@@ -119,17 +117,11 @@ function Login(props) {
 
     const getSchool = e => {
         // 获取学校信息
-        httpRequest(`${config.edu}admin/selectorganize`, {
-            method: 'POST',
-            headers: {
-                'Content-Type': 'application/x-www-form-urlencoded'
-            },
-            body: qs.stringify({ mode: e })
-        }).then(res => {
+        axios.post(`${config.edu}admin/selectorganize`, qs.stringify({ mode: e })).then(res => {
             console.log(res)
-            if (res[0].length > 0) {
-                setSchoolNum(res[0][0].mail)
-                setSchoolName(res[0][0].name)
+            if (res.data.length > 0) {
+                setSchoolNum(res.data[0][0].mail)
+                setSchoolName(res.data[0][0].name)
                 setSchoolShow(true)
             } else {
                 setSchoolName(props.lang.notSchool)