@jxstjh/jhvideo
Version:
HTML5 jhvideo base on MPEG2-TS Stream Player
155 lines (148 loc) • 4.97 kB
text/typescript
import httpClient from './core/httpClient'
import { Base64 } from '../node_modules/js-base64/base64';
/*
*@description
*@author jsyang
*@date 2021-06-03 14:55:19
*@variable LoginUrl 登录接口
*@variable 变量2
*@variable 变量3
*/
export const STATUS_TYPE = {
'LOGIN_SUCCESS': 'LoginSuccess',
'LOGIN_ERROR': 'LoginError'
}
const LoginUrl = '/oauth/token'
export const serverName = '/video-platform-basedata'
// export const getAislesBySearchObjUrl = '/hik-stream/aisle-page'
export const getAislesUrl = '/hik-stream/aisle-page'
export interface GlobalClientData {
"access_token": string;
"token_type": "bearer" | string;
"refresh_token": string;
"expires_in": number;
"scope": "[server]" | string;
}
export class GlobalClientConfig {
endPoint: string;
username?: string;
password?: string;
appKey: string;
appSecret:string;
clientId?: string;
clientSecret?: string;
workerPath?: string;
logoPath?: string;
}
/**
* authClient
* @param 参数1
* @param 参数2
* @return
* @description
* @author jsyang
* @date 2021-11-01 20:13:07
*/
export class GlobalClient {
private _config: GlobalClientConfig;
private _onStatusChanged
private _data: GlobalClientData
public get config() {
return this._config
}
public set config(config) {
this._config = { ...config }
}
private get data() {
return this._data
}
private set data(data: GlobalClientData) {
this._data = { ...data }
}
public get accessToken() {
return this.data.access_token
}
constructor(config: GlobalClientConfig, onStatusChanged = (statusMsg) => { }) {
this.config = { clientId: "third-aplication", clientSecret: "third-aplication2021", ...config, }
this._onStatusChanged = onStatusChanged
const username = config.appKey || config.username
const password = config.appSecret || config.password
this.login(config.endPoint + serverName + LoginUrl + '?grant_type=password&scope=server', { username, password }).then(res => {
if (res.code === 0) {
this.data = res.data
} else {
throw new Error(res);
}
}).then(() => {
this._onStatusChanged(STATUS_TYPE.LOGIN_SUCCESS)
}).catch(err => {
this._onStatusChanged(STATUS_TYPE.LOGIN_ERROR, err)
})
}
private login(url, body) {
const { clientId, clientSecret } = this.config;
return httpClient.post(url, body, {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": 'Basic ' + Base64.encode(`${clientId}:${clientSecret}`),
})
}
async reLogin() {
const { config } = this
return new Promise((reslove, reject) => {
const url = config.endPoint + serverName + LoginUrl + '?grant_type=password&scope=server'
this.login(url, {
username: this.config.username,
password: this.config.password
}).then(res => {
if (res.code === 0) {
this.data = res.data
reslove(res.data)
} else {
reject(res)
}
}).catch(err => {
reject(err)
})
})
}
/*public getAislesBySearchObj(obj) {
return new Promise((reslove, reject) => {
if (!this.accessToken) {
reject('无登录信息!')
}
const { config } = this;
const url = config.endPoint + serverName + getAislesBySearchObjUrl
httpClient.get(url, obj, {
"Content-Type": "application/json;charset=UTF-8",
"Authorization": 'Bearer ' + this.accessToken,
}).then(res => {
if (res.code === 0) {
reslove(res)
} else {
reject(res)
}
}).catch(err => reject(err))
})
}*/
public getAisles(params) {
return new Promise((reslove, reject) => {
if (!this.accessToken) {
reject('无登录信息!')
}
const { config } = this;
const url = config.endPoint + serverName + getAislesUrl
httpClient.get(url, params, {
"Content-Type": "application/json;charset=UTF-8",
"Authorization": 'Bearer ' + this.accessToken,
}).then(res => {
if (res.code === 0) {
reslove(res)
} else {
reject(res)
}
}).catch(err => reject(err))
})
}
destroy() {}
};
export default GlobalClient;