|
@@ -8,7 +8,10 @@ function lsGet(name) {
|
|
|
var key = name
|
|
var key = name
|
|
|
for (var i = 0; i < localStorage.length; i++) {
|
|
for (var i = 0; i < localStorage.length; i++) {
|
|
|
var k = localStorage.key(i)
|
|
var k = localStorage.key(i)
|
|
|
- if (k && k.slice(-(name.length + 1)) === '-' + name) { key = k; break }
|
|
|
|
|
|
|
+ if (k && k.slice(-(name.length + 1)) === '-' + name) {
|
|
|
|
|
+ key = k
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
var raw = localStorage.getItem(key)
|
|
var raw = localStorage.getItem(key)
|
|
|
if (raw === null || typeof raw === 'undefined') return null
|
|
if (raw === null || typeof raw === 'undefined') return null
|
|
@@ -24,106 +27,91 @@ var urlParams
|
|
|
var loginResult
|
|
var loginResult
|
|
|
$(function () {
|
|
$(function () {
|
|
|
urlParams = qs.parse(location.search.replace('?', ''))
|
|
urlParams = qs.parse(location.search.replace('?', ''))
|
|
|
|
|
+ alert('sso urlParams=\n' + JSON.stringify(urlParams) + '\nURL=' + URL)
|
|
|
// 获取授权验证
|
|
// 获取授权验证
|
|
|
getAuthorizationCodeValidate(urlParams)
|
|
getAuthorizationCodeValidate(urlParams)
|
|
|
- const client = res.data || {}
|
|
|
|
|
- let KEY = TOKEN
|
|
|
|
|
- if (location.port) KEY += `_${location.port}`
|
|
|
|
|
- // 从本地存储读取($utils.ls 写入)
|
|
|
|
|
- loginResult = lsGet(TOKEN)
|
|
|
|
|
- if (client && loginResult) {
|
|
|
|
|
- loginSuccess()
|
|
|
|
|
- } else {
|
|
|
|
|
- console.error('登录失败,自动跳转到回调地址中')
|
|
|
|
|
- document.location.href = urlParams.redirect_uri
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const client = res.data || {}
|
|
|
|
|
+ let KEY = TOKEN
|
|
|
|
|
+ if (location.port) KEY += `_${location.port}`
|
|
|
|
|
+ // 从本地存储读取($utils.ls 写入)
|
|
|
|
|
+ loginResult = lsGet(TOKEN)
|
|
|
|
|
+ if (client && loginResult) {
|
|
|
|
|
+ loginSuccess()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('登录失败,自动跳转到回调地址中')
|
|
|
|
|
+ document.location.href = urlParams.redirect_uri
|
|
|
|
|
+ }
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
-function getAuthorizationCodeValidate (data = {}) {
|
|
|
|
|
- $.ajax({
|
|
|
|
|
- type: 'POST',
|
|
|
|
|
- url: `${URL}/oauth2/authorizationCodeValidate?${qs.stringify(urlParams)}`,
|
|
|
|
|
- data: {},
|
|
|
|
|
- dataType: 'json',
|
|
|
|
|
- async: false, // 同步模式
|
|
|
|
|
- beforeSend: function (request) {
|
|
|
|
|
- // window.sessionStorage.clear()
|
|
|
|
|
- // request.setRequestHeader("Authorization", "Basic V2ViQXBwOnNhcGlAMTIzNA==")
|
|
|
|
|
- },
|
|
|
|
|
- success: function (result) {
|
|
|
|
|
- res = result
|
|
|
|
|
- },
|
|
|
|
|
- error: function (result) {
|
|
|
|
|
- console.error('error', result)
|
|
|
|
|
- document.location.href = urlParams.redirect_uri
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
|
|
+function getAuthorizationCodeValidate(data = {}) {
|
|
|
|
|
+ $.ajax({
|
|
|
|
|
+ type: 'POST',
|
|
|
|
|
+ url: `${URL}/oauth2/authorizationCodeValidate?${qs.stringify(urlParams)}`,
|
|
|
|
|
+ data: {},
|
|
|
|
|
+ dataType: 'json',
|
|
|
|
|
+ async: false, // 同步模式
|
|
|
|
|
+ beforeSend: function (request) {
|
|
|
|
|
+ // window.sessionStorage.clear()
|
|
|
|
|
+ // request.setRequestHeader("Authorization", "Basic V2ViQXBwOnNhcGlAMTIzNA==")
|
|
|
|
|
+ },
|
|
|
|
|
+ success: function (result) {
|
|
|
|
|
+ res = result
|
|
|
|
|
+ },
|
|
|
|
|
+ error: function (result) {
|
|
|
|
|
+ console.error('error', result)
|
|
|
|
|
+ document.location.href = urlParams.redirect_uri
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function loginSuccess () {
|
|
|
|
|
- if (urlParams.scope === 'snsapi_base') { // 静默授权
|
|
|
|
|
- auth()
|
|
|
|
|
- } else {
|
|
|
|
|
- console.log('回调地址非静默授权')
|
|
|
|
|
- document.location.href = urlParams.redirect_uri
|
|
|
|
|
- }
|
|
|
|
|
|
|
+function loginSuccess() {
|
|
|
|
|
+ if (urlParams.scope === 'snsapi_base') {
|
|
|
|
|
+ // 静默授权
|
|
|
|
|
+ auth()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.log('回调地址非静默授权')
|
|
|
|
|
+ document.location.href = urlParams.redirect_uri
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-function auth () {
|
|
|
|
|
- $.ajax({
|
|
|
|
|
- type: 'POST',
|
|
|
|
|
- url: `${URL}/oauth2/code?${qs.stringify(urlParams)}`,
|
|
|
|
|
- data: {},
|
|
|
|
|
- dataType: 'json',
|
|
|
|
|
- async: false, // 同步模式
|
|
|
|
|
- beforeSend: function (request) {
|
|
|
|
|
- // window.sessionStorage.clear()
|
|
|
|
|
- request.setRequestHeader('user-token', loginResult)
|
|
|
|
|
- },
|
|
|
|
|
- success: function (result) {
|
|
|
|
|
- const flag = result.status === 1
|
|
|
|
|
- if (flag) {
|
|
|
|
|
- const code = result.data
|
|
|
|
|
- const state = urlParams.state ? `&state=${urlParams.state}` : ''
|
|
|
|
|
- if (urlParams && urlParams.redirect_uri && urlParams.redirect_uri.includes('?')) {
|
|
|
|
|
- document.location.href = `${urlParams.redirect_uri}&code=${code}${state}`
|
|
|
|
|
- } else {
|
|
|
|
|
- document.location.href = `${urlParams.redirect_uri}?code=${code}${state}`
|
|
|
|
|
- }
|
|
|
|
|
- // console.log('返回地址', `${urlParams.redirect_uri}?code=${code}${state}`)
|
|
|
|
|
- } else {
|
|
|
|
|
- console.error('登录超时')
|
|
|
|
|
- document.location.href = window.location.protocol + '//' + window.location.host + '/pages/index/login'
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
- error: function (result) {
|
|
|
|
|
- console.error('error', result)
|
|
|
|
|
- document.location.href = urlParams.redirect_uri
|
|
|
|
|
|
|
+function auth() {
|
|
|
|
|
+ alert(
|
|
|
|
|
+ '准备调用 oauth2/code\nURL=' +
|
|
|
|
|
+ URL +
|
|
|
|
|
+ '\nloginResult=' +
|
|
|
|
|
+ loginResult +
|
|
|
|
|
+ '\nurlParams=' +
|
|
|
|
|
+ JSON.stringify(urlParams)
|
|
|
|
|
+ )
|
|
|
|
|
+ $.ajax({
|
|
|
|
|
+ type: 'POST',
|
|
|
|
|
+ url: `${URL}/oauth2/code?${qs.stringify(urlParams)}`,
|
|
|
|
|
+ data: {},
|
|
|
|
|
+ dataType: 'json',
|
|
|
|
|
+ async: false, // 同步模式
|
|
|
|
|
+ beforeSend: function (request) {
|
|
|
|
|
+ // window.sessionStorage.clear()
|
|
|
|
|
+ request.setRequestHeader('user-token', loginResult)
|
|
|
|
|
+ },
|
|
|
|
|
+ success: function (result) {
|
|
|
|
|
+ const flag = result.status === 1
|
|
|
|
|
+ if (flag) {
|
|
|
|
|
+ const code = result.data
|
|
|
|
|
+ const state = urlParams.state ? `&state=${urlParams.state}` : ''
|
|
|
|
|
+ if (urlParams && urlParams.redirect_uri && urlParams.redirect_uri.includes('?')) {
|
|
|
|
|
+ document.location.href = `${urlParams.redirect_uri}&code=${code}${state}`
|
|
|
|
|
+ } else {
|
|
|
|
|
+ document.location.href = `${urlParams.redirect_uri}?code=${code}${state}`
|
|
|
}
|
|
}
|
|
|
- })
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// var cookie_value = getCookie(cookie_name);
|
|
|
|
|
-function getCookie (cookieName) {
|
|
|
|
|
- // var allcookies = document.cookie;
|
|
|
|
|
- // 索引长度,开始索引的位置
|
|
|
|
|
- var arr = document.cookie.match(new RegExp('(^| )' + (cookieName) + '=([^;]*)(;|$)'))
|
|
|
|
|
- if (!!arr && arr.length > 0) {
|
|
|
|
|
- return unescape(arr[2])
|
|
|
|
|
|
|
+ // console.log('返回地址', `${urlParams.redirect_uri}?code=${code}${state}`)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('登录超时')
|
|
|
|
|
+ document.location.href =
|
|
|
|
|
+ window.location.protocol + '//' + window.location.host + '/pages/index/login'
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ error: function (result) {
|
|
|
|
|
+ console.error('error', result)
|
|
|
|
|
+ document.location.href = urlParams.redirect_uri
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // var cookiePos = allcookies.indexOf(cookieName)
|
|
|
|
|
- // // 如果找到了索引,就代表cookie存在,否则不存在
|
|
|
|
|
- // if (cookiePos !== -1) {
|
|
|
|
|
- // // 把cookiePos放在值的开始,只要给值加1即可
|
|
|
|
|
- // // 计算取cookie值得开始索引,加的1为“=”
|
|
|
|
|
- // cookiePos = cookiePos + cookieName.length + 1
|
|
|
|
|
- // // 计算取cookie值得结束索引
|
|
|
|
|
- // var cookieEnd = allcookies.indexOf(';', cookiePos)
|
|
|
|
|
- // if (cookieEnd === -1) {
|
|
|
|
|
- // cookieEnd = allcookies.length
|
|
|
|
|
- // }
|
|
|
|
|
- // // 得到想要的cookie的值
|
|
|
|
|
- // var value = unescape(allcookies.substring(cookiePos, cookieEnd))
|
|
|
|
|
- // }
|
|
|
|
|
- // return value
|
|
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|