Login.vue 7.7 KB
Newer Older
yma16's avatar
yma16 已提交
1
<template>
yma16's avatar
yma16 已提交
2
  <div class="login" style="text-align: center;width: 100%">
yma16's avatar
yma16 已提交
3
    <el-card class="box-card" style="text-align: center" v-loading="loading">
yma16's avatar
yma16 已提交
4 5 6 7 8 9 10 11
      <div style="flex:1;margin: 0 auto">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
        >
            <el-form-item><p style="font-size: 30px">登录</p></el-form-item>
yma16's avatar
yma16 已提交
12
            <el-form-item label="" prop="name">
yma16's avatar
yma16 已提交
13 14
              <el-input v-model="ruleForm.name" placeholder="请输入账号"></el-input>
            </el-form-item>
yma16's avatar
yma16 已提交
15
              <el-form-item label="" prop="pass">
yma16's avatar
yma16 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29
                <el-input
                  type="password"
                  v-model="ruleForm.pass"
                  autocomplete="off"
                  placeholder="请输入密码"
                  show-password
                ></el-input>
              </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
              >登录</el-button
              >
              <el-button @click="resetForm('ruleForm')">清空</el-button>
            </el-form-item>
yma16's avatar
yma16 已提交
30

yma16's avatar
yma16 已提交
31

yma16's avatar
yma16 已提交
32 33 34 35 36 37 38
          <el-form-item>
            <div style="width: 100%">
              <el-divider>三方登录</el-divider>
              <div style="display: flex;width: 100%">
              <div style="width:50%;text-align: center">
                <img src="https://ts2.cn.mm.bing.net/th?id=ODLS.f9daea21-9936-4164-815f-b5209459f3c6&w=32&h=32&qlt=90&pcl=fffffa&o=6&pid=1.2" style="width:10px;line-height: 56px"/>
                <el-link type="primary" @click="thirdLogin('azure')"
yma16's avatar
yma16 已提交
39
                >微软登录(测试中)</el-link
yma16's avatar
yma16 已提交
40 41 42 43 44
                >
              </div>
              <div style="width:50%;text-align: center">
                <img src="https://sola.gtimg.cn/aoi/sola/20210118201807_hzF9zYwEBj.png" style="width:10px;line-height: 56px"/>
                <el-link type="primary" @click="thirdLogin('qq')"
yma16's avatar
yma16 已提交
45
                >QQ登录(测试中)</el-link
yma16's avatar
yma16 已提交
46 47 48 49 50 51
                >
              </div>
              </div>
            </div>
          </el-form-item>

yma16's avatar
yma16 已提交
52 53 54 55 56 57 58 59 60
          <el-form-item>
            <el-link target="_blank" @click="$router.push({ path: '/register' })"
            >没有账号?</el-link
            >
            <el-link type="primary" @click="$router.push({ path: '/register' })"
            >去注册</el-link
            >
          </el-form-item>

yma16's avatar
yma16 已提交
61 62
        </el-form>
      </div>
Y
yma16 已提交
63
    </el-card>
yma16's avatar
yma16 已提交
64 65 66 67
  </div>
</template>

<script>
yma16's avatar
yma16 已提交
68
import store from '@/store'
yma16's avatar
yma16 已提交
69
export default {
yma16's avatar
yma16 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
    name: 'Login',
    data () {
        const checkName = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('账号不能为空'))
            }
            setTimeout(() => {
                if (value.length < 2) {
                    callback(new Error('名字至少两位'))
                } else {
                    callback()
                }
            }, 1000)
yma16's avatar
yma16 已提交
83
        }
yma16's avatar
yma16 已提交
84 85
        const validatePass = (rule, value, callback) => {
            if (value === '') {
yma16's avatar
yma16 已提交
86
                callback(new Error('密码不能为空'))
yma16's avatar
yma16 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            } else {
                callback()
            }
        }
        return {
            loading: false,
            // baseurl:"http://127.0.0.1/user/login/",
            baseurl: '/api/',
            // baseurl: 'http://yongma16.xyz/user/login/',
            ruleForm: {
                pass: '',
                // checkPass: '',
                name: ''
            },
            rules: {
                pass: [{ validator: validatePass, trigger: 'blur' }],
                checkPass: [{ validator: validatePass, trigger: 'blur' }],
                name: [{ validator: checkName, trigger: 'blur' }]
            }
yma16's avatar
yma16 已提交
106
        }
Y
yma16 已提交
107
    },
yma16's avatar
yma16 已提交
108
    methods: {
yma16's avatar
yma16 已提交
109 110 111 112 113 114 115 116 117 118 119
        thirdLogin (type) {
            if (type === 'azure') {
                this.azureLogin()
            } else {
                this.qqLogin()
            }
        },
        azureLogin () {
            const url = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=3a46d800-6bcc-47b5-9724-5a651b336bdb&scope=User.Read%20openid%20profile%20offline_access&redirect_uri=https%3A%2F%2Fyongma16.xyz%2Fazure_login_callback%2F&client-request-id=d4f3e873-d59f-417d-8101-ad19aac54168&response_mode=fragment&response_type=code&x-client-SKU=msal.js.browser&x-client-VER=2.26.0&client_info=1&code_challenge=pmwq-hZFKj0arSiO6WXFHngszqW0cH0fwMpd-a1Vuns&code_challenge_method=S256&nonce=e8d65872-8fe9-45b4-9b4d-b0c7db70a61f&state=eyJpZCI6IjVjNGUyZDQyLTI5Y2MtNDc5MS1iMmQ4LTBiZDAwZWM5Y2M2MyIsIm1ldGEiOnsiaW50ZXJhY3Rpb25UeXBlIjoicG9wdXAifX0%3D'
            const openHandle = window.open(url, '_black')
            console.log('openHandle', openHandle)
yma16's avatar
yma16 已提交
120 121 122 123 124 125
            var winLoop = setInterval(function () {
                if (openHandle.closed) {
                    clearInterval(winLoop)
                    alert('closed')
                }
            }, 1000)
yma16's avatar
yma16 已提交
126 127 128 129 130
        },
        qqLogin () {
            const url = 'https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=100330589&response_type=token&scope=all&redirect_uri=https%3A%2F%2Fconnect.qq.com%2Fsdk%2Fwebtools%2Findex2.html'
            const openHandle = window.open(url, '_black')
            console.log('openHandle', openHandle)
yma16's avatar
yma16 已提交
131 132 133 134 135 136
            var winLoop = setInterval(function () {
                if (openHandle.closed) {
                    clearInterval(winLoop)
                    alert('closed')
                }
            }, 1000)
yma16's avatar
yma16 已提交
137
        },
yma16's avatar
yma16 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        submitForm (formName) {
            const that = this // this指向
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    that.loading = true
                    // 提交数据
                    const loginUrl = that.baseurl + 'user/login/'
                    const userInfo = {
                        name: that.ruleForm.name,
                        password: that.ruleForm.pass
                    }
                    that.$axios
                        .post(loginUrl, userInfo)
                        .then((res) => {
                            // alter('提交中');
                            if (res.data.code === 1) {
                                localStorage.setItem(
                                    'yma16siteUserInfo',
                                    JSON.stringify({
                                        username: userInfo.name,
                                        password: userInfo.password
                                    })
                                )
                                store.commit('setUserInfo', userInfo) // store信息
                                document.cookie = `user=${that.ruleForm.name}`
                                that.$router.push({ path: '/' })
                                // 添加cookie
                            } else if (res.data.code === 0) {
                                // alter("失败!" res.data.msg)
                                that.$message.error('用户名或密码错误')
                            }
                            that.loading = false
                        })
                        .catch((error) => {
                            that.loading = false
                            that.$message.error(error)
                        })
                } else {
                    return false
                }
            })
        },
        resetForm (formName) {
            this.$refs[formName].resetFields()
        }
    }
}
yma16's avatar
yma16 已提交
185 186 187
</script>

<style scoped>
Y
yma16 已提交
188
.login {
yma16's avatar
yma16 已提交
189
  position: relative;
Y
yma16 已提交
190
  width: 100%;
yma16's avatar
yma16 已提交
191 192
  height: auto;
}
Y
yma16 已提交
193
.box-card {
yma16's avatar
yma16 已提交
194
  position: relative;
Y
yma16 已提交
195
  display: flex;
yma16's avatar
yma16 已提交
196
  max-width: 450px;
Y
yma16 已提交
197 198
  margin: 0 auto;
  margin-top: 80px;
yma16's avatar
yma16 已提交
199
  background-color: rgba(255,255,255,.95);
yma16's avatar
yma16 已提交
200
  box-sizing: border-box;
Y
yma16 已提交
201
}
yma16's avatar
yma16 已提交
202 203 204
/deep/ .el-card__body{
  width: 100% !important;
}
yma16's avatar
yma16 已提交
205
</style>