提交 d30de9d9 编写于 作者: KK's avatar KK

钉钉服务调整

上级 dd44a917
import axios from "axios"; import axios from "axios";
import * as dd from "dingtalk-jsapi"; import * as dd from "dingtalk-jsapi";
import { Util } from "@/ibiz-core/utils"; import { Util, Http } from "@/ibiz-core/utils";
import store from "@/store"; import store from "@/store";
import { Notice } from "../../utils/notice/notice"; import { Notice } from "../../utils/notice/notice";
/** /**
...@@ -35,7 +35,7 @@ export class DingTalkService { ...@@ -35,7 +35,7 @@ export class DingTalkService {
* @type {string} * @type {string}
* @memberof DingTalkService * @memberof DingTalkService
*/ */
private readonly infoName: string = "UserInfo"; private readonly infoName: string = "";
/** /**
* 企业corpId * 企业corpId
* *
...@@ -43,7 +43,7 @@ export class DingTalkService { ...@@ -43,7 +43,7 @@ export class DingTalkService {
* @type {string} * @type {string}
* @memberof WeChatService * @memberof WeChatService
*/ */
private corpId: string = "ding0466097cd833d9f9a1320dcb25e91351"; private corpId: string = "";
/** /**
* 钉钉sdk * 钉钉sdk
* *
...@@ -78,6 +78,15 @@ export class DingTalkService { ...@@ -78,6 +78,15 @@ export class DingTalkService {
*/ */
public $store: any; public $store: any;
/**
* 请求服务对象
*
* @protected
* @type {Http}
* @memberof DingTalkService
*/
protected http: Http = Http.getInstance();
/** /**
* Creates an instance of DingTalkService. * Creates an instance of DingTalkService.
* @memberof DingTalkService * @memberof DingTalkService
...@@ -90,6 +99,10 @@ export class DingTalkService { ...@@ -90,6 +99,10 @@ export class DingTalkService {
this.init(); this.init();
} }
/**
* 钉钉初始化 鉴权
* @memberof DingTalkService
*/
private init() { private init() {
const info: string = window.navigator.userAgent.toUpperCase(); const info: string = window.navigator.userAgent.toUpperCase();
if (info.indexOf("DINGTALK") !== -1) { if (info.indexOf("DINGTALK") !== -1) {
...@@ -122,7 +135,7 @@ export class DingTalkService { ...@@ -122,7 +135,7 @@ export class DingTalkService {
} }
/** /**
* 钉钉初始化 * 钉钉初始化回调方法
*/ */
private dd_ready() { private dd_ready() {
this.setNavBack(); this.setNavBack();
...@@ -134,26 +147,16 @@ export class DingTalkService { ...@@ -134,26 +147,16 @@ export class DingTalkService {
public async getAccess_token(): Promise<any> { public async getAccess_token(): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let access_token = localStorage.getItem("access_token"); let access_token = localStorage.getItem("access_token");
if (access_token) { if (access_token && JSON.parse(access_token).time && !(new Date().getTime() - JSON.parse(access_token).time > 5400000)) {
let reAccess_token: any = JSON.parse(access_token);
// 鉴权信息2小时过期 设置一小时五十分钟 // 鉴权信息2小时过期 设置一小时五十分钟
if ( resolve(JSON.parse(access_token));
reAccess_token.time && } else {
!(new Date().getTime() - reAccess_token.tiem > 5400000) let openAccessId = document.referrer ? document.referrer : "dingtalk";
) { let url = window.location.href.replace(window.location.hash, '')
resolve(reAccess_token); const promise: Promise<any> = this.http.get(`/uaa/dingtalk/jsapi/sign`, { openAccessId: openAccessId, url: url });
} promise.then((response: any) => {
}
const promise: Promise<any> = this.get(`/uaa/dingtalk/jsapi/sign`);
promise
.then((response: any) => {
if (response && response.status === 200) { if (response && response.status === 200) {
localStorage.setItem( localStorage.setItem("access_token", JSON.stringify(Object.assign(response.data, { time: new Date().getTime() })));
"access_token",
JSON.stringify(
Object.assign(response.data, { time: new Date().getTime() })
)
);
resolve(response.data); resolve(response.data);
} else { } else {
resolve(response); resolve(response);
...@@ -161,40 +164,26 @@ export class DingTalkService { ...@@ -161,40 +164,26 @@ export class DingTalkService {
}) })
.catch((response: any) => { .catch((response: any) => {
console.error(response); console.error(response);
// alert("sign"+JSON.stringify(response));
reject(response); reject(response);
}); });
}
}); });
} }
/** /**
* 钉钉登录 * 钉钉登录
* *
* @memberof DingTalkService * @memberof DingTalkService
*/ */
public async login(): Promise<any> { public async login(): Promise<any> {
const access_token: any = await this.get(`/uaa/open/dingtalk/access_token`); const access_token: any = await this.http.get(`/uaa/open/dingtalk/access_token`);
if ( if (access_token.status == 200 && access_token.data && access_token.data.corp_id) {
access_token.status == 200 && localStorage.setItem("access_token", JSON.stringify(Object.assign(access_token, new Date().getTime)));
access_token.data &&
access_token.data.corp_id
) {
localStorage.setItem(
"access_token",
JSON.stringify(Object.assign(access_token, new Date().getTime))
);
this.corpId = access_token.data.corp_id; this.corpId = access_token.data.corp_id;
const res: any = await dd.runtime.permission.requestAuthCode({ const res: any = await dd.runtime.permission.requestAuthCode({ corpId: this.corpId, });
corpId: this.corpId,
});
if (res && res.code) { if (res && res.code) {
const userInfo: any = await this.get( const userInfo: any = await this.http.get(`/uaa/open/dingtalk/auth/${res.code}`);
`/uaa/open/dingtalk/auth/${res.code}` if (userInfo.status == 200 && userInfo.data.token && userInfo.data.user) {
);
if (
userInfo.status == 200 &&
userInfo.data.token &&
userInfo.data.user
) {
localStorage.setItem("token", userInfo.data.token); localStorage.setItem("token", userInfo.data.token);
localStorage.setItem("user", JSON.stringify(userInfo.data.user)); localStorage.setItem("user", JSON.stringify(userInfo.data.user));
return { issuccess: true, message: "" }; return { issuccess: true, message: "" };
...@@ -289,28 +278,6 @@ export class DingTalkService { ...@@ -289,28 +278,6 @@ export class DingTalkService {
return dd.util.domainStorage.getItem({ name: this.infoName }); return dd.util.domainStorage.getItem({ name: this.infoName });
} }
/**
* 请求(需要替换为项目)
*
* @private
* @param {string} url
* @returns {Promise<any>}
* @memberof DingTalkService
*/
private async get(url: string): Promise<any> {
return new Promise((resolve) => {
axios
.get(url)
.then((response: any) => {
resolve(response);
})
.catch((error: any) => {
resolve(error);
console.log("请求异常");
});
});
}
/** /**
* 获取实例 * 获取实例
* *
...@@ -347,14 +314,10 @@ export class DingTalkService { ...@@ -347,14 +314,10 @@ export class DingTalkService {
*/ */
private setNavBack() { private setNavBack() {
if (Util.isAndroid()) { if (Util.isAndroid()) {
document.addEventListener( document.addEventListener("backbutton", (e: any) => {
"backbutton",
(e: any) => {
e.preventDefault(); e.preventDefault();
this.controlBackEvent(); this.controlBackEvent();
}, }, false);
false
);
} else { } else {
dd.biz.navigation.setLeft({ dd.biz.navigation.setLeft({
control: true, //是否控制点击事件,true 控制,false 不控制, 默认false control: true, //是否控制点击事件,true 控制,false 不控制, 默认false
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册