UNPKG

enc-framework

Version:

enc-framework 核心组件.

144 lines (127 loc) 4.36 kB
// 爱加密http请求对象 import AjmHttp from '../utils/ajm-http'; import {base64Utils} from '../utils/ajm-utils'; import AjmConfig from '../utils/ajm-config' import { URLSearchParams } from '../utils/ajm-objects'; // 用户api function LoginApi(store, options) { this.options = options || {}; this.store = store || {}; this.baseUrl = this.options.oauthUrl || this.options.baseUrl || ""; this.contextPath = AjmConfig.get("OAUTH_CONTEXT_PATH") || this.options.contextPath || ""; //客户端信息 this.clientInfo = this.options.clientInfo || this.getDefaultClientInfo(); // $http对象 this.$http = new AjmHttp({baseUrl: this.baseUrl, contextPath: this.contextPath}).getHttpClient(); return this; } // 获取默认客户端信息 LoginApi.prototype.getDefaultClientInfo = function () { return { //客户端ID clientId: "admin", //客户端密钥 clientSecret: "secret" } } // 获取授权信息 LoginApi.prototype.getAuthorization = function () { return "Basic " + base64Utils._btoa(this.clientInfo.clientId + ":" + this.clientInfo.clientSecret); } LoginApi.prototype.createLoginParams = function(params){ if(!params){ return {}; } var urlParams = new URLSearchParams(); if(params.grant_type == "authorization_code"){ urlParams.append("code", this.params.code); urlParams.append("redirect_uri", this.params.redirect_uri); }else{ var urlParams = new URLSearchParams(); urlParams.append("username", this.params.username); urlParams.append("password", this.params.password); urlParams.append("_t", this.params._t); urlParams.append("_validCode", this.params._validCode); } urlParams.append("grant_type", this.params.grant_type); return urlParams; } //获取JWT-TOEKN LoginApi.prototype.refreshToken = function (params, callback) { this.params = params || {}; if(typeof(params) != "object"){ return null; } let urlParams = new URLSearchParams(); let grant_type = null; //let deviceId=localStorage.getItem("device_id") if(params.code){ grant_type = "authorization_code"; }else if(params.refresh_token){ grant_type = "refresh_token"; }else{ grant_type = "password"; } // urlParams.append("device_id",'ijiami002'); urlParams.append("grant_type",grant_type); urlParams.append("client_id",this.clientInfo.clientId); urlParams.append("client_secret",this.clientInfo.clientSecret); for(let key in params){ urlParams.append(key,params[key]); } return this.$http.post('/oauth/token',urlParams.toString(),{ headers: { 'Content-Type': 'application/x-www-form-urlencoded', // 'Authorization': this.getAuthorization() } }).then((resp) => { if(resp.data.status==200){ var data = resp.data||{}; data.cookiepath = this.options.cookiePath||'/' this.store.commit("user/saveTokenInfo", data); if (callback) { callback(data); } return data } }).catch((error)=>{ //执行清除缓存的登录信息 this.store.dispatch('user/clearUser',this.options.cookiePath); window.location.reload() }) } // 登录 LoginApi.prototype.login = function (params, callback) { this.params = params || {}; // 授权类型 params.grant_type = params.grant_type || "password"; var urlParams = this.createLoginParams(params); //发起登录请求 return this.$http.post('/oauth/token', urlParams.toString(), { headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': this.getAuthorization() } }).then(resp => { var data = resp.data||{}; // 保存token信息 //crossDomain token是否要跨域 data.cookiepath = this.options.cookiePath||'/' this.store.commit("user/saveTokenInfo", data); if (callback) { callback.call(); } }); } //退出 LoginApi.prototype.logout = function (callback) { // 清理token信息 return this.$http.get('/api/user/logout').then(resp => { //cookie设置了path 删除的时候需要指定path 删除 this.store.dispatch('user/clearUser',this.options.cookiePath); if (callback) { callback.call(); } }) } export default LoginApi