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

第三方服务

上级 02a407c0
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 } from '@/ibiz-core/utils';
import store from '@/store'; import store from '@/store';
/** /**
* 钉钉服务 * 钉钉服务
...@@ -32,13 +32,15 @@ export class DingTalkService { ...@@ -32,13 +32,15 @@ export class DingTalkService {
* @type {string} * @type {string}
* @memberof WeChatService * @memberof WeChatService
*/ */
private readonly appId: string = "ding0466097cd833d9f9a1320dcb25e91351"; private corpId: string = "ding0466097cd833d9f9a1320dcb25e91351";
/** /**
* 钉钉sdk * 钉钉sdk
* *
* @memberof DingTalkService * @memberof DingTalkService
*/ */
public readonly dd = dd; public readonly dd = dd;
/** /**
* 钉钉是否已经初始化 * 钉钉是否已经初始化
* *
...@@ -64,13 +66,13 @@ export class DingTalkService { ...@@ -64,13 +66,13 @@ export class DingTalkService {
* @type {(any | null)} * @type {(any | null)}
* @memberof DingTalkService * @memberof DingTalkService
*/ */
public $store:any; public $store: any;
/** /**
* Creates an instance of DingTalkService. * Creates an instance of DingTalkService.
* @memberof DingTalkService * @memberof DingTalkService
*/ */
private constructor(store:any) { private constructor(store: any) {
this.$store = store; this.$store = store;
if (DingTalkService.instance) { if (DingTalkService.instance) {
return DingTalkService.instance; return DingTalkService.instance;
...@@ -79,7 +81,7 @@ export class DingTalkService { ...@@ -79,7 +81,7 @@ export class DingTalkService {
if (info.indexOf('DINGTALK') !== -1) { if (info.indexOf('DINGTALK') !== -1) {
dd.ready(() => { dd.ready(() => {
this.$isInit = true; this.$isInit = true;
this.navBack(); this.dd_ready();
}); });
dd.error((err: any) => { dd.error((err: any) => {
alert(`dd加载错误:${JSON.stringify(err)}`); alert(`dd加载错误:${JSON.stringify(err)}`);
...@@ -87,6 +89,16 @@ export class DingTalkService { ...@@ -87,6 +89,16 @@ export class DingTalkService {
} }
} }
/**
* 钉钉初始化
*/
private dd_ready() {
// 设置导航标题
this.setNavBack();
//
this.dd.ui.webViewBounce.disable({});
}
/** /**
* 钉钉登录 * 钉钉登录
* *
...@@ -94,26 +106,132 @@ export class DingTalkService { ...@@ -94,26 +106,132 @@ export class DingTalkService {
* @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.get(`/uaa/dingtalk/jsapi/sign`);
if(access_token.status == 200 && access_token.data && access_token.data.corp_id){ if (access_token.status == 200 && access_token.data && access_token.data.corp_id) {
const res: any= await dd.runtime.permission.requestAuthCode({ corpId: access_token.data.corp_id }); this.corpId = access_token.data.corp_id;
if (res && res.code) { this.authentication(access_token.data.agentId, this.corpId, access_token.data.timeStamp, access_token.data.nonceStr, access_token.data.signature);
const userInfo:any = await this.get(`/uaa/open/dingtalk/auth/${res.code}`); const res: any = await dd.runtime.permission.requestAuthCode({ corpId: this.corpId });
if(userInfo.status == 200 && userInfo.data.token && userInfo.data.user){ if (res && res.code) {
localStorage.setItem("token", userInfo.data.token); const userInfo: any = await this.get(`/uaa/open/dingtalk/auth/${res.code}`);
localStorage.setItem("user", JSON.stringify(userInfo.data.user)); if (userInfo.status == 200 && userInfo.data.token && userInfo.data.user) {
return {issuccess:true,message:""}; localStorage.setItem("token", userInfo.data.token);
}else if(userInfo.status == 400){ localStorage.setItem("user", JSON.stringify(userInfo.data.user));
return {issuccess:false,message:userInfo.data.message}; return { issuccess: true, message: "" };
}else{ } else if (userInfo.status == 400) {
return {issuccess:false,message:userInfo.data.message}; return { issuccess: false, message: userInfo.data.message };
} } else {
}else{ return { issuccess: false, message: userInfo.data.message };
return {issuccess:false,message:"钉钉用户信息获取失败"};
} }
}else{ } else {
return {issuccess:false,message:"获取企业id失败"}; return { issuccess: false, message: "钉钉用户信息获取失败" };
}
} else {
return { issuccess: false, message: "获取企业id失败" };
}
}
/**
* 鉴权
*
* @returns {Promise<any>} 返回用户信息
* @memberof DingTalkService
*/
private authentication(agentId: string, corpId: string, timeStamp: any, nonceStr: string, signature: any) {
this.dd.config({
agentId: agentId, // 必填,微应用ID
corpId: corpId,//必填,企业ID
timeStamp: timeStamp, // 必填,生成签名的时间戳
nonceStr: nonceStr, // 必填,生成签名的随机串
signature: signature, // 必填,签名
type: 0, //选填。0表示微应用的jsapi,1表示服务窗的jsapi;不填默认为0。该参数从dingtalk.js的0.8.3版本开始支持
jsApiList: [
'device.audio.startRecord',
'device.audio.stopRecord',
'device.audio.onRecordEnd',
'device.audio.translateVoice'
] // 必填,需要使用的jsapi列表,注意:不要带dd。
});
this.dd.error((error: any) => {
alert('dd error: ' + error);
});
}
/**
* test
*/
public async test() {
const access_token: any = await this.get(`/uaa/dingtalk/jsapi/sign`);
// alert(access_token.data.agentId)
this.authentication('859826642', access_token.data.corpId, access_token.data.timeStamp, access_token.data.nonceStr, access_token.data.signature);
setTimeout(() => {
// this.getuuid();
}, 2000);
}
/**
* 开始录音
*
* @memberof DingTalkService
*/
private startRecord() {
dd.device.audio.onRecordEnd({
onSuccess: (res: any) => {
res.duration; // 返回音频的时长,单位:秒
},
onFail: (err: any) => {
} }
});
this.dd.device.audio.startRecord({
onSuccess: () => {//支持最长为300秒(包括)的音频录制,默认60秒(包括)。
},
onFail: (err: any) => {
alert(JSON.stringify(err))
}
});
}
/**
* 停止录音
*
* @memberof DingTalkService
*/
private stopRecord(): Promise<any> {
return new Promise((resolve, reject) => {
dd.device.audio.stopRecord({
onSuccess: (res: any) => {
res.mediaId; // 返回音频的MediaID,可用于本地播放和音频下载
res.duration; // 返回音频的时长,单位:秒
resolve(res);
},
onFail: (err: any) => {
alert(JSON.stringify(err))
}
});;
});
}
/**
* 语音转文字
*
* @memberof DingTalkService
*/
private translateVoice(arg: any): Promise<any> {
return new Promise((resolve, reject) => {
this.dd.device.audio.translateVoice({
mediaId: arg.mediaId,
duration: arg.duration,
onSuccess: (res: any) => {
res.mediaId; // 转换的语音的mediaId
res.content; // 语音转换的文字内容
resolve(res);
},
onFail: (err: any) => {
alert(JSON.stringify(err))
}
});
});
} }
/** /**
...@@ -172,7 +290,7 @@ export class DingTalkService { ...@@ -172,7 +290,7 @@ export class DingTalkService {
* @returns {DingTalkService} * @returns {DingTalkService}
* @memberof DingTalkService * @memberof DingTalkService
*/ */
public close(){ private close() {
this.dd.biz.navigation.close({}); this.dd.biz.navigation.close({});
} }
...@@ -183,9 +301,9 @@ export class DingTalkService { ...@@ -183,9 +301,9 @@ export class DingTalkService {
* @returns {DingTalkService} * @returns {DingTalkService}
* @memberof DingTalkService * @memberof DingTalkService
*/ */
public setTitle(title:string){ private setTitle(title: string) {
this.dd.biz.navigation.setTitle({ this.dd.biz.navigation.setTitle({
title : title, title: title,
}); });
} }
...@@ -196,20 +314,20 @@ export class DingTalkService { ...@@ -196,20 +314,20 @@ export class DingTalkService {
* @returns {DingTalkService} * @returns {DingTalkService}
* @memberof DingTalkService * @memberof DingTalkService
*/ */
private navBack(){ private setNavBack() {
if(Util.isAndroid()){ if (Util.isAndroid()) {
document.addEventListener('backbutton', (e:any)=> { document.addEventListener('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
text : '返回', //控制显示文本,空字符串表示显示默认文本 text: '返回', //控制显示文本,空字符串表示显示默认文本
onSuccess : ()=>{ onSuccess: () => {
this.controlBackEvent(); this.controlBackEvent();
} }
}); });
} }
} }
...@@ -220,7 +338,7 @@ export class DingTalkService { ...@@ -220,7 +338,7 @@ export class DingTalkService {
* @returns {DingTalkService} * @returns {DingTalkService}
* @memberof DingTalkService * @memberof DingTalkService
*/ */
private backEvent:Function = ()=>{}; private backEvent: Function = () => { };
/** /**
* 设置钉钉导航栏返回事件 * 设置钉钉导航栏返回事件
...@@ -229,17 +347,45 @@ export class DingTalkService { ...@@ -229,17 +347,45 @@ export class DingTalkService {
* @returns {DingTalkService} * @returns {DingTalkService}
* @memberof DingTalkService * @memberof DingTalkService
*/ */
public setBackEvent(event:Array<Function>){ private setBackEvent(event: Array<Function>) {
this.backEvent = event[event.length -1]; this.backEvent = event[event.length - 1];
} }
/** /**
* 是否调用导航栏返回事件 * 是否调用导航栏返回事件
* @memberof DingTalkService * @memberof DingTalkService
*/ */
public controlBackEvent(){ private controlBackEvent() {
if (this.$store.state.selectStatus && this.$store.state.noticeStatus) { if (this.$store.state.selectStatus && this.$store.state.noticeStatus) {
this.backEvent(); this.backEvent();
} }
} }
/**
* 钉钉开放事件
*/
public ddEvent(tag: string, arg: any) {
if (Object.is(tag, 'startRecord')) {
return this.startRecord();
}
if (Object.is(tag, 'stopRecord')) {
return this.stopRecord();
}
if (Object.is(tag, 'translateVoice')) {
return this.translateVoice(arg);
}
if (Object.is(tag, 'login')) {
return this.login();
}
if (Object.is(tag, 'setTitle')) {
return this.setTitle(arg);
}
if (Object.is(tag, 'setBackEvent')) {
return this.setBackEvent(arg);
}
if (Object.is(tag, 'close')) {
return this.close();
}
}
} }
...@@ -49,7 +49,7 @@ export class ThirdPartyService { ...@@ -49,7 +49,7 @@ export class ThirdPartyService {
* @type {WeChatService} * @type {WeChatService}
* @memberof ThirdPartyService * @memberof ThirdPartyService
*/ */
private backEvent:Array<Function> = []; private backEvent: Array<Function> = [];
/** /**
* 第三方导航标题 * 第三方导航标题
...@@ -57,7 +57,7 @@ export class ThirdPartyService { ...@@ -57,7 +57,7 @@ export class ThirdPartyService {
* @type {WeChatService} * @type {WeChatService}
* @memberof ThirdPartyService * @memberof ThirdPartyService
*/ */
private navTitle:Array<string> = []; private navTitle: Array<string> = [];
/** /**
* 是否已经初始化 * 是否已经初始化
...@@ -143,7 +143,7 @@ export class ThirdPartyService { ...@@ -143,7 +143,7 @@ export class ThirdPartyService {
* @returns {boolean} * @returns {boolean}
* @memberof ThirdPartyService * @memberof ThirdPartyService
*/ */
public isWeChat():boolean { public isWeChat(): boolean {
return Object.is(this.platform, 'WeChat'); return Object.is(this.platform, 'WeChat');
} }
...@@ -165,9 +165,9 @@ export class ThirdPartyService { ...@@ -165,9 +165,9 @@ export class ThirdPartyService {
* @returns {ThirdPartyService} * @returns {ThirdPartyService}
* @memberof ThirdPartyService * @memberof ThirdPartyService
*/ */
public close(){ public close() {
if (this.isDingTalk()) { if (this.isDingTalk()) {
this.dd.close(); this.thirdPartyEvent('close');
} else if (this.isWeChat()) { } else if (this.isWeChat()) {
this.weChat.close(); this.weChat.close();
} }
...@@ -180,15 +180,15 @@ export class ThirdPartyService { ...@@ -180,15 +180,15 @@ export class ThirdPartyService {
* @returns {DingTalkService} * @returns {DingTalkService}
* @memberof DingTalkService * @memberof DingTalkService
*/ */
public setTitle(title:string){ public setTitle(title: string) {
this.navTitle.push(title); this.navTitle.push(title);
if (this.isDingTalk()) { if (this.isDingTalk()) {
this.dd.setTitle(title); this.thirdPartyEvent('setTitle', title);
} else if (this.isWeChat()) { } else if (this.isWeChat()) {
this.weChat.setTitle(title); this.weChat.setTitle(title);
} }
} }
/** /**
* 设置第三方容器导航栏返回事件 * 设置第三方容器导航栏返回事件
...@@ -197,31 +197,46 @@ export class ThirdPartyService { ...@@ -197,31 +197,46 @@ export class ThirdPartyService {
* @returns {DingTalkService} * @returns {DingTalkService}
* @memberof DingTalkService * @memberof DingTalkService
*/ */
public setBackEvent(event:Function){ public setBackEvent(event: Function) {
this.backEvent.push(event); this.backEvent.push(event);
if (this.isDingTalk()) { if (this.isDingTalk()) {
this.dd.setBackEvent(this.backEvent); this.thirdPartyEvent('setBackEvent', this.backEvent);
} else if (this.isWeChat()) { } else if (this.isWeChat()) {
this.weChat.setBackEvent(this.backEvent); this.weChat.setBackEvent(this.backEvent);
} }
} }
/** /**
* 第三方容器导航销毁返回事件 * 第三方容器导航销毁返回事件
*/ */
public destroyBackEvent() { public destroyBackEvent() {
this.backEvent = this.backEvent.slice(0,-1); this.backEvent = this.backEvent.slice(0, -1);
this.navTitle = this.navTitle.slice(0,-1); this.navTitle = this.navTitle.slice(0, -1);
if(!this.backEvent){ if (!this.backEvent) {
return return
} }
if (this.isDingTalk()) { if (this.isDingTalk()) {
this.dd.setTitle(this.navTitle[this.navTitle.length -1]); this.thirdPartyEvent('setTitle', this.navTitle[this.navTitle.length - 1]);
this.dd.setBackEvent(this.backEvent); this.thirdPartyEvent('setBackEvent', this.backEvent);
} else if (this.isWeChat()) { } else if (this.isWeChat()) {
this.weChat.setTitle(this.navTitle[this.navTitle.length -1]); this.weChat.setTitle(this.navTitle[this.navTitle.length - 1]);
this.weChat.setBackEvent(this.backEvent); this.weChat.setBackEvent(this.backEvent);
} }
} }
public test() {
this.dd.test();
}
/**
* 第三方事件
*/
public thirdPartyEvent(tag: string, arg: any = {}): any {
if (!this.isInit) {
return;
}
if (this.isDingTalk()) {
return this.dd.ddEvent(tag, arg);
}
}
} }
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册