Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
iBiz-Vue-Mob-R7-Res
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
iBiz-R7前端标准模板
iBiz-Vue-Mob-R7-Res
提交
ecbdca48
提交
ecbdca48
编写于
9月 30, 2020
作者:
KK
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
服务基类
上级
0bcb0659
变更
1
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
129 行增加
和
112 行删除
+129
-112
DingTalkService.ts
src/ibiz-core/third-party-service/DingTalkService.ts
+129
-112
未找到文件。
src/ibiz-core/third-party-service/DingTalkService.ts
浏览文件 @
ecbdca48
import
axios
from
'axios'
;
import
*
as
dd
from
'dingtalk-jsapi'
;
import
{
Util
}
from
'@/ibiz-core/utils'
;
import
store
from
'@/store'
;
import
{
Notice
}
from
'../../utils/notice/notice'
;
import
axios
from
"axios"
;
import
*
as
dd
from
"dingtalk-jsapi"
;
import
{
Util
}
from
"@/ibiz-core/utils"
;
import
store
from
"@/store"
;
import
{
Notice
}
from
"../../utils/notice/notice"
;
/**
* 钉钉服务
*
...
...
@@ -10,7 +10,6 @@ import { Notice } from '../../utils/notice/notice';
* @class DingTalkService
*/
export
class
DingTalkService
{
/**
* 提示工具类
*
...
...
@@ -52,7 +51,6 @@ export class DingTalkService {
*/
public
readonly
dd
=
dd
;
/**
* 钉钉是否已经初始化
*
...
...
@@ -89,50 +87,81 @@ export class DingTalkService {
if
(
DingTalkService
.
instance
)
{
return
DingTalkService
.
instance
;
}
this
.
init
();
}
private
init
()
{
const
info
:
string
=
window
.
navigator
.
userAgent
.
toUpperCase
();
if
(
info
.
indexOf
(
'DINGTALK'
)
!==
-
1
)
{
if
(
info
.
indexOf
(
"DINGTALK"
)
!==
-
1
)
{
this
.
getAccess_token
().
then
((
access_token
:
any
)
=>
{
dd
.
ready
(()
=>
{
this
.
$isInit
=
true
;
this
.
dd_ready
();
});
dd
.
config
({
agentId
:
access_token
.
agentId
,
// 必填,微应用ID
corpId
:
access_token
.
corpId
,
//必填,企业ID
timeStamp
:
access_token
.
timeStamp
,
// 必填,生成签名的时间戳
nonceStr
:
access_token
.
nonceStr
,
// 必填,生成签名的随机串
signature
:
access_token
.
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。
});
dd
.
error
((
err
:
any
)
=>
{
alert
(
`dd加载错误:
${
JSON
.
stringify
(
err
)}
`
);
});
});
}
}
/**
* 钉钉初始化
*/
private
async
dd_ready
()
{
// 设置导航标题
// let { issuccess } = await this.login();
private
dd_ready
()
{
this
.
setNavBack
();
// if(issuccess){
// }
}
/**
* 获取access_token
*/
public
async
getAccess_token
()
{
public
async
getAccess_token
():
Promise
<
any
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
let
access_token
=
localStorage
.
getItem
(
"access_token"
);
if
(
access_token
)
{
let
reAccess_token
:
any
=
JSON
.
parse
(
access_token
);
// 鉴权信息2小时过期 设置一小时五十分钟
if
(
reAccess_token
.
time
&&
!
(
new
Date
().
getTime
()
-
reAccess_token
.
tiem
>
5400000
))
{
return
reAccess_token
;
}
}
const
reAccess_token
:
any
=
await
this
.
get
(
`/uaa/dingtalk/jsapi/sign`
);
if
(
reAccess_token
.
status
==
200
){
localStorage
.
setItem
(
"access_token"
,
JSON
.
stringify
(
Object
.
assign
(
reAccess_token
.
data
,
{
time
:
new
Date
().
getTime
()
})));
}
else
{
this
.
notice
.
error
(
'获取dd签名失败'
)
if
(
reAccess_token
.
time
&&
!
(
new
Date
().
getTime
()
-
reAccess_token
.
tiem
>
5400000
)
)
{
resolve
(
reAccess_token
);
}
}
const
promise
:
Promise
<
any
>
=
this
.
get
(
`/uaa/dingtalk/jsapi/sign`
);
promise
.
then
((
response
:
any
)
=>
{
if
(
response
&&
response
.
status
===
200
)
{
localStorage
.
setItem
(
"access_token"
,
JSON
.
stringify
(
Object
.
assign
(
response
.
data
,
{
time
:
new
Date
().
getTime
()
})
)
);
resolve
(
response
.
data
);
}
else
{
resolve
(
null
);
}
return
reAccess_token
.
data
;
})
.
catch
((
response
:
any
)
=>
{
console
.
error
(
response
);
reject
(
null
);
});
});
}
/**
* 钉钉登录
...
...
@@ -140,15 +169,29 @@ export class DingTalkService {
* @memberof DingTalkService
*/
public
async
login
():
Promise
<
any
>
{
const
access_token
:
any
=
await
this
.
get
(
`/uaa/open/dingtalk/access_token`
);
if
(
access_token
.
status
==
200
&&
access_token
.
data
&&
access_token
.
data
.
corp_id
)
{
localStorage
.
setItem
(
"access_token"
,
JSON
.
stringify
(
Object
.
assign
(
access_token
,
new
Date
().
getTime
)));
const
access_token
:
any
=
await
this
.
get
(
`/uaa/open/dingtalk/access_token`
);
if
(
access_token
.
status
==
200
&&
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
;
const
res
:
any
=
await
dd
.
runtime
.
permission
.
requestAuthCode
({
corpId
:
this
.
corpId
});
this
.
doAuthentication
();
const
res
:
any
=
await
dd
.
runtime
.
permission
.
requestAuthCode
({
corpId
:
this
.
corpId
,
});
if
(
res
&&
res
.
code
)
{
const
userInfo
:
any
=
await
this
.
get
(
`/uaa/open/dingtalk/auth/
${
res
.
code
}
`
);
if
(
userInfo
.
status
==
200
&&
userInfo
.
data
.
token
&&
userInfo
.
data
.
user
)
{
const
userInfo
:
any
=
await
this
.
get
(
`/uaa/open/dingtalk/auth/
${
res
.
code
}
`
);
if
(
userInfo
.
status
==
200
&&
userInfo
.
data
.
token
&&
userInfo
.
data
.
user
)
{
localStorage
.
setItem
(
"token"
,
userInfo
.
data
.
token
);
localStorage
.
setItem
(
"user"
,
JSON
.
stringify
(
userInfo
.
data
.
user
));
return
{
issuccess
:
true
,
message
:
""
};
...
...
@@ -163,28 +206,6 @@ export class DingTalkService {
}
}
/**
* 鉴权
*
* @memberof DingTalkService
*/
private
authentication
(
agentId
:
string
,
corpId
:
string
,
timeStamp
:
any
,
nonceStr
:
string
,
signature
:
any
)
{
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。
});
}
/**
* 开始录音
*
...
...
@@ -195,16 +216,15 @@ export class DingTalkService {
onSuccess
:
(
res
:
any
)
=>
{
res
.
duration
;
// 返回音频的时长,单位:秒
},
onFail
:
(
err
:
any
)
=>
{
}
onFail
:
(
err
:
any
)
=>
{
},
});
this
.
dd
.
device
.
audio
.
startRecord
({
onSuccess
:
()
=>
{
//支持最长为300秒(包括)的音频录制,默认60秒(包括)。
onSuccess
:
()
=>
{
//支持最长为300秒(包括)的音频录制,默认60秒(包括)。
},
onFail
:
(
err
:
any
)
=>
{
alert
(
JSON
.
stringify
(
err
))
}
alert
(
JSON
.
stringify
(
err
))
;
}
,
});
}
...
...
@@ -222,9 +242,9 @@ export class DingTalkService {
resolve
(
res
);
},
onFail
:
(
err
:
any
)
=>
{
alert
(
JSON
.
stringify
(
err
))
}
});
;
alert
(
JSON
.
stringify
(
err
))
;
}
,
});
});
}
...
...
@@ -244,8 +264,8 @@ export class DingTalkService {
resolve
(
res
);
},
onFail
:
(
err
:
any
)
=>
{
alert
(
JSON
.
stringify
(
err
))
}
alert
(
JSON
.
stringify
(
err
))
;
}
,
});
});
}
...
...
@@ -279,13 +299,16 @@ export class DingTalkService {
*/
private
async
get
(
url
:
string
):
Promise
<
any
>
{
return
new
Promise
((
resolve
)
=>
{
axios
.
get
(
url
).
then
((
response
:
any
)
=>
{
axios
.
get
(
url
)
.
then
((
response
:
any
)
=>
{
resolve
(
response
);
}).
catch
((
error
:
any
)
=>
{
})
.
catch
((
error
:
any
)
=>
{
resolve
(
error
);
console
.
log
(
'请求异常'
);
console
.
log
(
"请求异常"
);
});
});
})
}
/**
...
...
@@ -324,17 +347,21 @@ export class DingTalkService {
*/
private
setNavBack
()
{
if
(
Util
.
isAndroid
())
{
document
.
addEventListener
(
'backbutton'
,
(
e
:
any
)
=>
{
document
.
addEventListener
(
"backbutton"
,
(
e
:
any
)
=>
{
e
.
preventDefault
();
this
.
controlBackEvent
();
},
false
)
},
false
);
}
else
{
dd
.
biz
.
navigation
.
setLeft
({
control
:
true
,
//是否控制点击事件,true 控制,false 不控制, 默认false
text
:
'返回'
,
//控制显示文本,空字符串表示显示默认文本
control
:
true
,
//是否控制点击事件,true 控制,false 不控制, 默认false
text
:
"返回"
,
//控制显示文本,空字符串表示显示默认文本
onSuccess
:
()
=>
{
this
.
controlBackEvent
();
}
}
,
});
}
}
...
...
@@ -366,42 +393,32 @@ export class DingTalkService {
}
}
/**
* 鉴权
*/
public
async
doAuthentication
()
{
const
reAccess_token
=
await
this
.
getAccess_token
();
alert
(
JSON
.
stringify
(
reAccess_token
));
this
.
authentication
(
reAccess_token
.
agentId
,
this
.
corpId
,
reAccess_token
.
data
.
timeStamp
,
reAccess_token
.
data
.
nonceStr
,
reAccess_token
.
data
.
signature
);
}
/**
* 钉钉开放事件
*
* @memberof DingTalkService
*/
public
async
ddEvent
(
tag
:
string
,
arg
:
any
)
{
if
(
Object
.
is
(
tag
,
'startRecord'
))
{
if
(
Object
.
is
(
tag
,
"startRecord"
))
{
return
this
.
startRecord
();
}
if
(
Object
.
is
(
tag
,
'stopRecord'
))
{
if
(
Object
.
is
(
tag
,
"stopRecord"
))
{
return
this
.
stopRecord
();
}
if
(
Object
.
is
(
tag
,
'translateVoice'
))
{
if
(
Object
.
is
(
tag
,
"translateVoice"
))
{
return
this
.
translateVoice
(
arg
);
}
if
(
Object
.
is
(
tag
,
'login'
))
{
if
(
Object
.
is
(
tag
,
"login"
))
{
return
this
.
login
();
}
if
(
Object
.
is
(
tag
,
'setTitle'
))
{
if
(
Object
.
is
(
tag
,
"setTitle"
))
{
return
this
.
setTitle
(
arg
);
}
if
(
Object
.
is
(
tag
,
'setBackEvent'
))
{
if
(
Object
.
is
(
tag
,
"setBackEvent"
))
{
return
this
.
setBackEvent
(
arg
);
}
if
(
Object
.
is
(
tag
,
'close'
))
{
if
(
Object
.
is
(
tag
,
"close"
))
{
return
this
.
close
();
}
}
}
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录