Quellcode durchsuchen

页面记住我功能开发

user5 vor 2 Jahren
Ursprung
Commit
60afb13799
3 geänderte Dateien mit 64 neuen und 3 gelöschten Zeilen
  1. 11 0
      package-lock.json
  2. 1 0
      package.json
  3. 52 3
      src/views/modules/sys/login/login.vue

+ 11 - 0
package-lock.json

@@ -23,6 +23,7 @@
         "jeeplus-flowable": "^2.1.5",
         "jeeplus-form": "^2.1.2",
         "jeeplus-gencode": "^2.1.2",
+        "js-base64": "^3.7.2",
         "lodash": "^4.17.15",
         "lodash.omit": "^4.5.0",
         "lodash.pick": "^4.4.0",
@@ -11233,6 +11234,11 @@
       "resolved": "https://registry.npmmirror.com/jquery/-/jquery-3.4.1.tgz",
       "integrity": "sha512-36+AdBzCL+y6qjw5Tx7HgzeGCzC81MDDgaUP8ld2zhx58HdqXGoBd+tHdrBMiyjGQs0Hxs/MLZTu/eHNJJuWPw=="
     },
+    "node_modules/js-base64": {
+      "version": "3.7.2",
+      "resolved": "https://registry.npmmirror.com/js-base64/-/js-base64-3.7.2.tgz",
+      "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ=="
+    },
     "node_modules/js-message": {
       "version": "1.0.7",
       "resolved": "https://registry.npmmirror.com/js-message/-/js-message-1.0.7.tgz",
@@ -28766,6 +28772,11 @@
       "resolved": "https://registry.npmmirror.com/jquery/-/jquery-3.4.1.tgz",
       "integrity": "sha512-36+AdBzCL+y6qjw5Tx7HgzeGCzC81MDDgaUP8ld2zhx58HdqXGoBd+tHdrBMiyjGQs0Hxs/MLZTu/eHNJJuWPw=="
     },
+    "js-base64": {
+      "version": "3.7.2",
+      "resolved": "https://registry.npmmirror.com/js-base64/-/js-base64-3.7.2.tgz",
+      "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ=="
+    },
     "js-message": {
       "version": "1.0.7",
       "resolved": "https://registry.npmmirror.com/js-message/-/js-message-1.0.7.tgz",

+ 1 - 0
package.json

@@ -25,6 +25,7 @@
     "jeeplus-flowable": "^2.1.5",
     "jeeplus-form": "^2.1.2",
     "jeeplus-gencode": "^2.1.2",
+    "js-base64": "^3.7.2",
     "lodash": "^4.17.15",
     "lodash.omit": "^4.5.0",
     "lodash.pick": "^4.4.0",

+ 52 - 3
src/views/modules/sys/login/login.vue

@@ -47,6 +47,12 @@
                   </el-col>
                  </el-row>
               </el-form-item>
+              <div style ="margin: 20px">
+                <!-- 记住我 -->
+                <el-checkbox v-model="checked" class="rememberMe">记住我</el-checkbox>
+                <!-- 找回密码 -->
+                <!--<el-button type="text" @click="open()" class="forgetPw">忘记密码?</el-button>-->
+              </div>
               <el-form-item>
                 <el-button type="primary" :loading="loading" class="login-submit" @click="login">登录</el-button>
               </el-form-item>
@@ -89,9 +95,10 @@
         loading: false,
         captchaImg: '',
         isShow: false,
+        checked: false,
         inputForm: {
-          username: 'admin',
-          password: 'admin',
+          username: '',
+          password: '',
           uuid: '',
           code: ''
         },
@@ -120,6 +127,7 @@
       }, 1000)
     },
     mounted () {
+      this.getCookie()
       this.configService.getConfig().then(({data}) => {
         this.productName = data.productName
       })
@@ -131,12 +139,26 @@
     methods: {
       // 提交表单
       login () {
+        var checked = this.checked
+        var password = this.inputForm.password
+        var username = this.inputForm.username
         this.$refs['inputForm'].validate((valid) => {
           if (valid) {
             this.loading = true
             this.loginService.login(this.inputForm).then(({data}) => {
               this.$cookie.set('token', data.token)
               this.$router.push({name: 'home'})
+              let Base64 = require('js-base64').Base64
+              /* ------ 账号密码的存储 ------ */
+              if (checked) {
+                // eslint-disable-next-line no-undef
+                // let password = Base64.encode(passwordLogin) // base64加密
+                this.setCookie(username, Base64.encode(password), 7)
+              } else {
+                this.setCookie('', '', -1) // 修改2值都为空,天数为负1天就好了
+              }
+              /* ------ http登录请求 ------ */
+
               this.$notify({
                 title: '登录成功',
                 message: `欢迎回来!<br/>你上次的登录IP是 ${data.oldLoginIp},登录时间是 ${data.oldLoginDate}。`,
@@ -146,7 +168,6 @@
               })
             }).catch(e => {
               this.loading = false
-              console.log(e)
               this.getLoginCodeNumber(this.inputForm.username)
             })
           }
@@ -170,6 +191,34 @@
             this.isShow = true
           }
         })
+      },
+      // 设置cookie
+      setCookie (username, password, days) {
+        let date = new Date() // 获取时间
+        date.setTime(date.getTime() + 24 * 60 * 60 * 1000 * days) // 保存的天数
+      // 字符串拼接cookie
+        window.document.cookie =
+          'username' + '=' + username + ';path=/;expires=' + date.toGMTString()
+        window.document.cookie =
+          'password' + '=' + password + ';path=/;expires=' + date.toGMTString()
+      },
+
+      // 读取cookie 将用户名和密码回显到input框中
+      getCookie () {
+        let Base64 = require('js-base64').Base64
+        if (document.cookie.length > 0) {
+          let arr = document.cookie.split('; ') // 分割成一个个独立的“key=value”的形式
+          for (let i = 0; i < arr.length; i++) {
+            let arr2 = arr[i].split('=') // 再次切割,arr2[0]为key值,arr2[1]为对应的value
+            if (arr2[0] === 'username') {
+              this.inputForm.username = arr2[1]
+            } else if (arr2[0] === 'password') {
+              // eslint-disable-next-line no-undef
+              this.inputForm.password = Base64.decode(arr2[1])// base64解密
+              this.checked = true
+            }
+          }
+        }
       }
     }
   }