Login.vue 14.5 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 32 33 34 35 36 37 38
          <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 已提交
39

yma16's avatar
yma16 已提交
40 41
          <el-form-item>
            <div style="width: 100%">
yma16's avatar
yma16 已提交
42
              <el-divider>其他登录</el-divider>
yma16's avatar
yma16 已提交
43
              <div style="display: flex;width: 100%">
yma16's avatar
yma16 已提交
44 45 46 47 48 49 50
              <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')"
                >微软登录(alpha阶段)</el-link
                >
              </div>
              <div style="width:50%;text-align: center">
yma16's avatar
yma16 已提交
51 52
                <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 已提交
53
                >QQ登录</el-link
yma16's avatar
yma16 已提交
54 55 56 57 58 59 60 61
                >
              </div>
              </div>
            </div>
          </el-form-item>

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

<script>
yma16's avatar
yma16 已提交
67
import store from '@/store'
yma16's avatar
yma16 已提交
68
export default {
yma16's avatar
yma16 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
    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 已提交
82
        }
yma16's avatar
yma16 已提交
83 84
        const validatePass = (rule, value, callback) => {
            if (value === '') {
yma16's avatar
yma16 已提交
85
                callback(new Error('密码不能为空'))
yma16's avatar
yma16 已提交
86 87 88 89 90 91
            } else {
                callback()
            }
        }
        return {
            loading: false,
yma16's avatar
yma16 已提交
92 93 94
            // baseUrl:"http://127.0.0.1/user/login/",
            baseUrl: '/api/',
            // baseUrl: 'http://yongma16.xyz/user/login/',
yma16's avatar
yma16 已提交
95 96 97 98 99 100 101 102 103
            ruleForm: {
                pass: '',
                // checkPass: '',
                name: ''
            },
            rules: {
                pass: [{ validator: validatePass, trigger: 'blur' }],
                checkPass: [{ validator: validatePass, trigger: 'blur' }],
                name: [{ validator: checkName, trigger: 'blur' }]
yma16's avatar
yma16 已提交
104 105 106 107 108 109 110 111 112
            },
            thirdLoginConfig: {
                azureUrl: '',
                qqUrl: '',
                qCode: '',
                qState: '',
                qToken: '',
                qOpenid: '',
                qUserInfo: ''
yma16's avatar
yma16 已提交
113
            }
yma16's avatar
yma16 已提交
114
        }
Y
yma16 已提交
115
    },
yma16's avatar
yma16 已提交
116 117 118
    mounted () {
        this.getThirdLoginUrl()
    },
yma16's avatar
yma16 已提交
119
    methods: {
yma16's avatar
yma16 已提交
120 121 122
        getThirdLoginUrl () {
            console.log('init')
        },
yma16's avatar
yma16 已提交
123 124 125 126 127 128 129
        thirdLogin (type) {
            if (type === 'azure') {
                this.azureLogin()
            } else {
                this.qqLogin()
            }
        },
yma16's avatar
yma16 已提交
130 131 132 133
        async azureLogin () {
            try {
                const that = this
                // qq
yma16's avatar
yma16 已提交
134 135
                const res = await that.getAzureUrl()
                console.log('res azureLogin', res)
yma16's avatar
yma16 已提交
136 137 138 139
                if (res.data && res.data.data) {
                    const resData = res.data.data
                    console.log('resData', resData)
                    if (res.data.code === 200) {
yma16's avatar
yma16 已提交
140
                        let url = resData
yma16's avatar
yma16 已提交
141 142 143
                        console.log('url', url)
                        const openHandle = window.open(url, 'width:800px;height:700px', '_black')
                        console.log('openHandle', openHandle)
yma16's avatar
yma16 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156
                        window.onmessage = async (res) => {
                            const {origin, data} = res
                            if (origin.includes('yongma16.xyz')) {
                                const {code, state} = data
                                console.log('code state', code, state)
                                that.thirdLoginConfig.qCode = code
                                that.thirdLoginConfig.qState = state
                                if (openHandle) {
                                    openHandle.close()
                                }
                                if (code) {
                                    await that.getAzureToken(code)
                                }
yma16's avatar
yma16 已提交
157
                            }
yma16's avatar
yma16 已提交
158
                        }
yma16's avatar
yma16 已提交
159
                    }
yma16's avatar
yma16 已提交
160
                }
yma16's avatar
yma16 已提交
161 162 163 164 165 166 167 168
                return new Promise(resolve => {
                    resolve(true)
                })
            } catch (e) {
                return new Promise((resolve, reject) => {
                    reject(e)
                })
            }
yma16's avatar
yma16 已提交
169
        },
yma16's avatar
yma16 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        getAzureUrl () {
            return new Promise(async (resolve, reject) => {
                try {
                    const azureAuthUrl = '/third-login/getAzureAuthUrl'
                    const azureRes = await this.$axios.get(azureAuthUrl)
                    console.log('azureRes', azureRes)
                    resolve(azureRes)
                } catch (e) {
                    reject(e)
                }
            })
        },
        async getAzureToken (code) {
            try {
                const azureAuthTokenUrl = '/third-login/getAzureToken'
                const params = {
                    code
                }
                const azureRes = await this.$axios.get(azureAuthTokenUrl, {params})
                console.log('azureRes', azureRes)
            } catch (e) {
                console.log('e', e)
            } finally {
                this.$message.info('微软登录还在alpha测试阶段!')
            }
        },
yma16's avatar
yma16 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        async qqLogin () {
            try {
                const that = this
                // qq
                const res = await that.$axios
                    .post(that.baseUrl + 'dictModel/index/', {
                        kind: 'qqThirdLogin'
                        // 传入索引
                    })
                console.log('res', res)
                if (res.data && res.data.data) {
                    const resData = res.data.data
                    console.log('resData', resData)
                    if (res.data.code === 200) {
                        let url = resData[0].option
                        console.log('url', url)
                        const openHandle = window.open(url, 'width:800px;height:700px', '_black')
                        console.log('openHandle', openHandle)
                        window.onmessage = async (res) => {
                            const {origin, data} = res
                            if (origin.includes('yongma16.xyz')) {
                                const {code, state} = data
                                console.log('code state', code, state)
                                that.thirdLoginConfig.qCode = code
                                that.thirdLoginConfig.qState = state
yma16's avatar
yma16 已提交
221 222 223
                                if (openHandle) {
                                    openHandle.close()
                                }
yma16's avatar
yma16 已提交
224 225 226 227 228
                                const tokenRes = await that.getAccessToken(code)
                                console.log('tokenRes', tokenRes)
                            }
                        }
                    }
yma16's avatar
yma16 已提交
229
                }
yma16's avatar
yma16 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
                return new Promise(resolve => {
                    resolve(true)
                })
            } catch (e) {
                return new Promise((resolve, reject) => {
                    reject(e)
                })
            }
        },
        async getAccessToken (code) {
            try {
                const tokenUrl = '/third-login/getToken'
                const params = {
                    code
                }
                const tokenRes = await this.$axios.get(tokenUrl, {params})

                console.log('tokenRes', tokenRes)
                if (tokenRes) {
                    const {access_token, expires_in, refresh_token} = tokenRes.data.data
                    if (access_token) {
                        this.thirdLoginConfig.qToken = access_token
                        await this.getOpenId(access_token)
                    }
                }
            } catch (e) {
                console.log('token error', e)
            }
        },
        async getOpenId (token) {
            try {
                const tokenUrl = '/third-login/getOpenId'
                const params = {
                    access_token: token
                }
                const openIdRes = await this.$axios.get(tokenUrl, {params})
                console.log('openIdRes', openIdRes)
                if (openIdRes) {
                    const {openid} = openIdRes.data.data
                    if (openid) {
                        this.thirdLoginConfig.qOpenid = openid
                        await this.getQUserinfo(this.thirdLoginConfig.qToken, openid)
                    }
                }
            } catch (e) {
                console.log('token error', e)
            }
        },
        async getQUserinfo (token, openId) {
            try {
                const userInfoUrl = '/third-login/getUserInfo'
                const params = {
                    access_token: token,
                    openId: openId
                }
                const userRes = await this.$axios.get(userInfoUrl, {params})
                if (userRes) {
                    this.thirdLoginConfig.qUserInfo = userRes.data.data
                    this.registerThirdLogin()
                }
                console.log('userRes', userRes)
            } catch (e) {
                console.log('token error', e)
            }
yma16's avatar
yma16 已提交
294
        },
yma16's avatar
yma16 已提交
295 296 297 298 299 300
        submitForm (formName) {
            const that = this // this指向
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    that.loading = true
                    // 提交数据
yma16's avatar
yma16 已提交
301
                    const loginUrl = that.baseUrl + 'user/login/'
yma16's avatar
yma16 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
                    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
                }
            })
        },
yma16's avatar
yma16 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        async registerThirdLogin () {
            const that = this
            return new Promise(async (resolve, reject) => {
                try {
                    const res = await that.$axios
                        .post(that.baseUrl + 'user/register/', {
                            // 传递的名字和密码
                            name: that.thirdLoginConfig.qOpenid,
                            password: that.thirdLoginConfig.qOpenid
                        })
                    if (res.data.code === 0) {
                        console.log('重复注册')
                    }
                    const userInfo = {
                        username: that.thirdLoginConfig.qOpenid,
                        password: that.thirdLoginConfig.qOpenid,
                        thirdUserInfo: that.thirdLoginConfig.qUserInfo
                    }
                    store.commit('setUserInfo', userInfo) // store信息
                    localStorage.setItem(
                        'yma16siteUserInfo',
                        JSON.stringify(userInfo)
                    )
                    that.$router.push({ path: '/' })
                    resolve(userInfo)
                } catch (e) {
                    console.log('注册三方账号失败')
                    reject(e)
                }
            })
        },
yma16's avatar
yma16 已提交
368 369 370 371 372
        resetForm (formName) {
            this.$refs[formName].resetFields()
        }
    }
}
yma16's avatar
yma16 已提交
373 374 375
</script>

<style scoped>
Y
yma16 已提交
376
.login {
yma16's avatar
yma16 已提交
377
  position: relative;
Y
yma16 已提交
378
  width: 100%;
yma16's avatar
yma16 已提交
379 380
  height: auto;
}
Y
yma16 已提交
381
.box-card {
yma16's avatar
yma16 已提交
382
  position: relative;
Y
yma16 已提交
383
  display: flex;
yma16's avatar
yma16 已提交
384
  max-width: 450px;
Y
yma16 已提交
385 386
  margin: 0 auto;
  margin-top: 80px;
yma16's avatar
yma16 已提交
387
  background-color: rgba(255,255,255,.95);
yma16's avatar
yma16 已提交
388
  box-sizing: border-box;
Y
yma16 已提交
389
}
yma16's avatar
yma16 已提交
390 391 392
/deep/ .el-card__body{
  width: 100% !important;
}
yma16's avatar
yma16 已提交
393 394 395 396

  /deep/ .el-divider__text{
    background-color: transparent !important;
  }
yma16's avatar
yma16 已提交
397
</style>