dingApi.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import * as dd from "dingtalk-jsapi";
  2. const ding = {}; // 钉钉jsapi对象
  3. let $loading = false; // 避免多个请求闪屏
  4. import progress from "@/service/progress";
  5. // loading 显示
  6. ding.showLoading = function (message = "拼命加载中...") {
  7. if ($loading) return;
  8. $loading = true;
  9. dd.device.notification.showPreloader({
  10. showIcon: true,
  11. text: message,
  12. });
  13. };
  14. // loading 关闭
  15. ding.hideLoading = function () {
  16. if (!$loading) return;
  17. dd.device.notification.hidePreloader();
  18. $loading = false;
  19. };
  20. // toast 提示: info 详见 https://ding-doc.dingtalk.com/doc#/dev/oo98ye/6pwsuy
  21. ding.showMessageToast = function (info = {}) {
  22. if (!info.message) return;
  23. dd.device.notification.toast(info);
  24. };
  25. // 获取定位
  26. ding.getLocation = function () {
  27. return new Promise((resolve, reject) => {
  28. dd.device.geolocation.get({
  29. targetAccuracy: 200,
  30. coordinate: 1,
  31. withReGeocode: true,
  32. useCache: true,
  33. onSuccess (result) {
  34. resolve(result);
  35. },
  36. onFail (err) {
  37. reject(err);
  38. },
  39. });
  40. });
  41. };
  42. // 根据chatId跳转到对应会话
  43. ding.toConversation = function (chatId) {
  44. return new Promise((resolve, reject) => {
  45. dd.biz.chat.toConversation({
  46. corpId: conf.corpId,
  47. chatId,
  48. onSuccess: () => resolve(),
  49. onFail: (err) => reject(err),
  50. });
  51. });
  52. };
  53. // 图片预览: 支持缩放和左右切换
  54. ding.previewImageScale = function (urls, index = 0) {
  55. if (!urls) return;
  56. dd.biz.previewImage({
  57. urls,
  58. current: urls[index],
  59. });
  60. };
  61. // 获取UUID
  62. ding.getUUID = function () {
  63. return new Promise((resolve) => {
  64. dd.device.base.getUUID({
  65. onSuccess (data) {
  66. resolve(data);
  67. },
  68. });
  69. });
  70. };
  71. /** @exports 钉钉新开页面方法 */
  72. ding.openNavigation = function (url, isReplace) {
  73. if (!url) return;
  74. // 区分环境, 兼容分享
  75. if (dd.env.platform === "notInDingTalk") {
  76. window.open(url, isReplace ? "_self" : "");
  77. } else {
  78. if (isReplace) {
  79. dd.biz.navigation.replace({ url });
  80. } else {
  81. dd.biz.util.openLink({ url });
  82. }
  83. }
  84. };
  85. /** @exports 设置右侧导航栏: [{ id: "1", text: "附 件", url: "http://alading-20210318.oss-cn-shanghai.aliyuncs.com/assets/nav-link.png",},] */
  86. ding.settingNavigationMenu = function (menus = [], isClear, callback) {
  87. if (isClear) {
  88. menus = [
  89. {
  90. id: "999",
  91. text: " ",
  92. },
  93. ];
  94. }
  95. dd.biz.navigation.setMenu({
  96. items: menus,
  97. onSuccess (result) {
  98. callback && callback(result);
  99. },
  100. });
  101. };
  102. // 注册钉钉js接口鉴权 [H5无需配置, 但宜搭内需要配置鉴权, 才能获取到code]
  103. ding.registerConfig = function () {
  104. if (dd.env.platform == "notInDingTalk") {
  105. progress.dialogConfirm("操作提示", "请在钉钉工作台打开!")
  106. return
  107. }
  108. /** // 页面环境:0提交(其它),1查看,2编辑(审批)
  109. if (!mjs.env) {
  110. // 注册jsApi ticket
  111. const rsp = await mjs.request.xhr.doPost("https://mc.cloudpure.cn/api/hangshi/register", {}, {
  112. url: mjs.conf.ticketUrl,
  113. nonceStr: mjs.conf.nonceStr
  114. }, { noLoading: true });
  115. dd.config({
  116. ...rsp.data,
  117. type: 0,
  118. jsApiList: ["runtime.permission.requestAuthCode"]
  119. });
  120. // 钉钉免登逻辑
  121. const _this = this;
  122. mjs.ding.dd.ready(function () {
  123. mjs.ding.dd.runtime.permission.requestAuthCode({
  124. corpId: mjs.conf.corpId, // 企业id
  125. onSuccess: async function (info) {
  126. const res = await mjs.request.xhr.doPost("https://mc.cloudpure.cn/api/hangshi/user/code", {}, {
  127. code: info.code
  128. }, { noLoading: true });
  129. console.log("钉钉免登", res.data)
  130. _this.$('textField_ll3m8vz5').setValue(res.data.name)
  131. _this.$('textField_ll3m8vz6').setValue(res.data.userid)
  132. }
  133. });
  134. });
  135. } */
  136. dd.ready(function () {
  137. // dd.config({
  138. // agentId,
  139. // corpId,
  140. // timeStamp,
  141. // nonceStr,
  142. // signature,
  143. // type: 0,
  144. // jsApiList: ["biz.chat.toConversation"], // 注意:不要带dd。
  145. // });
  146. })
  147. };
  148. // 注册配置异常抛出
  149. dd.error(function (error) {
  150. window.alert(JSON.stringify(error));
  151. });
  152. ding.dd = dd;
  153. export default ding;