JeecgThirdLoginMixin.js
6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
*第三方登录
*/
import { mapActions } from 'vuex'
import { postAction } from '@api/manage'
import { timeFix } from '@/utils/util'
export const JeecgThirdLoginMixin = {
data() {
return {
//第三方登录相关信息
thirdLoginInfo: '',
thirdPasswordShow: false,
thirdLoginPassword: '',
thirdLoginUser: '',
thirdConfirmShow: false,
thirdCreateUserLoding: false,
thirdLoginState: false,
//绑定手机号弹窗
bindingPhoneModal: false,
thirdPhone: '',
thirdCaptcha: '',
//获取验证码按钮30s之内是否可点击
thirdState: {
time: 30,
smsSendBtn: false
},
//第三方用户UUID
thirdUserUuid: '',
thirdType: '',
url: {
bindingThirdPhone: '/sys/thirdLogin/bindingThirdPhone'
}
}
},
created() {
},
methods: {
...mapActions(['ThirdLogin']),
//第三方登录
onThirdLogin(source) {
let url = window._CONFIG['domianURL'] + `/sys/thirdLogin/render/${source}`
window.open(url, `login ${source}`, 'height=500, width=500, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no')
let that = this
that.thirdType = source
that.thirdLoginInfo = ''
that.thirdLoginState = false
let receiveMessage = function(event) {
let token = event.data
if (typeof token === 'string') {
//如果是字符串类型 说明是token信息
if (token === '登录失败') {
that.$message.warning(token)
} else if (token.includes('绑定手机号')) {
that.bindingPhoneModal = true
let strings = token.split(',')
that.thirdUserUuid = strings[1]
} else {
that.doThirdLogin(token)
}
} else if (typeof token === 'object') {
//对象类型 说明需要提示是否绑定现有账号
if (token['isObj'] === true) {
that.thirdConfirmShow = true
that.thirdLoginInfo = { ...token }
}
} else {
that.$message.warning('不识别的信息传递')
}
}
window.addEventListener('message', receiveMessage, false)
},
// 根据token执行登录
doThirdLogin(token) {
if (this.thirdLoginState === false) {
this.thirdLoginState = true
let param={};
param.thirdType=this.thirdType
param.token=token
this.ThirdLogin(param).then(res => {
if (res.success) {
this.loginSuccess()
} else {
this.requestFailed(res)
}
})
}
},
// 绑定已有账号 需要输入密码
thirdLoginUserBind() {
this.thirdLoginPassword = ''
this.thirdLoginUser = this.thirdLoginInfo.uuid
this.thirdConfirmShow = false
this.thirdPasswordShow = true
},
//创建新账号
thirdLoginUserCreate() {
this.thirdCreateUserLoding = true
// 账号名后面添加两位随机数
this.thirdLoginInfo['suffix'] = parseInt(Math.random() * 98 + 1)
//this.thirdLoginInfo.operateCode = 123 //测试校验失败
postAction('/sys/third/user/create', this.thirdLoginInfo).then(res => {
if (res.success) {
let token = res.result
console.log('thirdCreateNewAccount', token)
this.doThirdLogin(token)
this.thirdConfirmShow = false
} else {
this.$message.warning(res.message)
}
}).finally(() => {
this.thirdCreateUserLoding = false
})
},
// 核实密码
thirdLoginCheckPassword() {
//this.thirdLoginInfo.operateCode = 123 //测试校验失败
let param = Object.assign({}, this.thirdLoginInfo, { password: this.thirdLoginPassword })
postAction('/sys/third/user/checkPassword', param).then(res => {
if (res.success) {
this.thirdLoginNoPassword()
this.doThirdLogin(res.result)
} else {
this.$message.warning(res.message)
}
})
},
// 没有密码 取消操作
thirdLoginNoPassword() {
this.thirdPasswordShow = false
this.thirdLoginPassword = ''
this.thirdLoginUser = ''
},
//获取第三方验证码
getThirdCaptcha() {
let that = this
if (!this.thirdPhone) {
that.cmsFailed('请输入手机号')
} else {
this.thirdState.smsSendBtn = true
let interval = window.setInterval(() => {
if (that.thirdState.time-- <= 0) {
that.thirdState.time = 30
that.thirdState.smsSendBtn = false
window.clearInterval(interval)
}
}, 1000)
const hide = this.$message.loading('验证码发送中..', 0)
let smsParams = {}
smsParams.mobile = this.thirdPhone
smsParams.smsmode = '1'
postAction('/sys/sms', smsParams).then(res => {
if (!res.success) {
setTimeout(hide, 0)
this.cmsFailed(res.message)
}
setTimeout(hide, 500)
}).catch(err => {
setTimeout(hide, 1)
clearInterval(interval)
that.thirdState.time = 30
that.thirdState.smsSendBtn = false
this.requestFailed(err)
})
}
},
//绑定手机号点击确定按钮
thirdHandleOk() {
let bingingParams = {}
bingingParams.mobile = this.thirdPhone
bingingParams.captcha = this.thirdCaptcha
bingingParams.thirdUserUuid = this.thirdUserUuid
postAction(this.url.bindingThirdPhone, bingingParams).then(res => {
if (res.success) {
this.bindingPhoneModal = false
this.doThirdLogin(res.result)
} else {
this.$message.warning(res.message)
}
})
},
loginSuccess () {
// update-begin- author:sunjianlei --- date:20190812 --- for: 登录成功后不解除禁用按钮,防止多次点击
// this.loginBtn = false
// update-end- author:sunjianlei --- date:20190812 --- for: 登录成功后不解除禁用按钮,防止多次点击
this.$router.push({ path: "/dashboard/analysis" }).catch(()=>{
console.log('登录跳转首页出错,这个错误从哪里来的')
})
this.$notification.success({
message: '欢迎',
description: `${timeFix()},欢迎回来`,
});
},
cmsFailed(err){
this.$notification[ 'error' ]({
message: "登录失败",
description:err,
duration: 4,
});
},
requestFailed (err) {
this.$notification[ 'error' ]({
message: '登录失败',
description: ((err.response || {}).data || {}).message || err.message || "请求出现错误,请稍后再试",
duration: 4,
});
this.loginBtn = false;
},
}
}