UNPKG

kara-reactutil-mobileweb

Version:

kara项目移动端react公共模块功能

35 lines (32 loc) 1.69 kB
function authorize(callback, auth) { if (window.location.href.toLocaleLowerCase().includes('version')) { document.addEventListener('JSSDKReady', () => { // JSSDK将方法注入在了全局变量kara上 window.kara.getToken({ success(result) { window.sessionStorage.setItem('token', result.token) // result.token 用户登录票据token callback() }, }) }, false) } else if (window.location.hash && window.location.hash.includes('access_token=')) { window.sessionStorage.setItem('token', `bearer ${window.location.hash.split('access_token=')[1].split('&')[0]}`) callback() } else if (window.sessionStorage.getItem('token')) { callback() } else { const getCookie = function getCookie(name) { const reg = new RegExp(`${name}=([^;]*)`) const arr = document.cookie.match(reg) return arr === null ? '' : decodeURIComponent(arr[1]) } const keyticketCookieName = getCookie('keyticketCookieName') const ticket = keyticketCookieName ? keyticketCookieName.split('&')[1] : getCookie('ticketCookieName') const ticketKey = keyticketCookieName ? keyticketCookieName.split('&')[0] : getCookie('keyCookieName') const index = window.location.href.indexOf('?') === -1 ? 0 : window.location.href.indexOf('?') const redirectUri = encodeURIComponent(window.location.href.slice(0, index)) // token不存在 去访问第三方授权网站 window.location.href = `${auth[0]}?client_id=${auth[1]}&redirect_uri=${redirectUri}&scope=${auth[2]}&response_type=${auth[3]}&ticket=${encodeURIComponent(ticket)}&ticketKey=${encodeURIComponent(ticketKey)}` } } export default authorize