hint.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div>
  3. <div @click="init()">
  4. <img src="./assets/image/pc.png" style="width:100%;height:auto" v-if="type == 'PC'" />
  5. <div class="app" v-else :style="{ width: width }">
  6. <img src="./assets/image/app1.png" :style="{ width: width, height: 'auto' }" />
  7. </div>
  8. </div>
  9. </div>
  10. </template>
  11. <script>
  12. import { openTryoutSku } from 'dingtalk-design-libs';
  13. import openLink from 'dingtalk-jsapi/api/biz/util/openLink';
  14. import close from 'dingtalk-jsapi/api/biz/navigation/close';
  15. export default {
  16. data() {
  17. return {
  18. corpId: '',
  19. token: '',
  20. type: 'APP',
  21. width: ''
  22. };
  23. },
  24. created() {
  25. this.width = document.body.clientWidth + 'px';
  26. },
  27. mounted() {
  28. var url = window.location.href;
  29. // let url='https://test-ding.g107.com/?purchaseToken=$PURCHASE_TOKEN$&corpId=$CORPID$&appEntityType=PC#/hint';
  30. var data = this.GetRequest(url);
  31. this.corpId = data.corpId;
  32. this.token = data.purchaseToken;
  33. if(data.appEntityType.indexOf('APP')>=0){
  34. this.type ='APP';
  35. }else{
  36. this.type ='PC';
  37. }
  38. console.log(data,this.type)
  39. if (this.corpId) {
  40. this.$nextTick(()=>{
  41. this.init();
  42. })
  43. }
  44. },
  45. methods: {
  46. IsPC() {
  47. var userAgentInfo = navigator.userAgent;
  48. var Agents = ["Android", "iPhone","SymbianOS", "Windows Phone","iPad", "iPod"];
  49. var flag = true;
  50. for (var v = 0; v < Agents.length; v++) {
  51. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  52. flag = false;
  53. break;
  54. }
  55. }
  56. return flag;
  57. },
  58. init() {
  59. // 开发者自行判断调用这个方法的时机
  60. openTryoutSku({
  61. // corpId可以从应用首页的url上获取到
  62. corpId: this.corpId,
  63. // corpId: 'ding897857ddc9137670ffe93478753d9884',
  64. // 应用的appId
  65. appId: 55493,
  66. // 从应用首页的url上获取到,url上参数名为 purchaseToken。
  67. // purchaseToken如何配置参照下文”配置入口地址“章节。
  68. token: this.token,
  69. miniAppId: this.type == 'APP' ? 5000000000104741 : '' // 如果是三方小程序应用,需要设置一下自身的miniAppId。H5微应用可以不设置这个参数
  70. }).then(res => {
  71. const {
  72. // action的值为:
  73. // 'ok',用户执行了开通动作,或将自己加入了已开通的应用的可见范围内
  74. // 'cancel', 用户点击了取消按钮
  75. // 'unknown',用户点击空白区域关闭了弹窗,此时可以跟cancel采取同样的处理逻辑
  76. action,
  77. // 开通了应用的组织的corpId。因为个人开通可能会用钉钉的隐藏组织,所以开通应用的组织的corpId以这里返回的为准。
  78. corpId
  79. } = res;
  80. if (action === 'ok') {
  81. if (this.type == 'APP') {
  82. close({});
  83. } else {
  84. if(this.IsPC()){
  85. window.location.replace('https://pc.dd.g107.com?corpId=' + corpId)
  86. }else{
  87. openLink({ url:'https://pc.dd.g107.com?corpId=' + corpId }).then(() => close({}));
  88. }
  89. }
  90. }
  91. })
  92. .catch((res) => {
  93. console.log(res);
  94. // 钉钉侧出现了技术异常,比如打开弹窗失败等,出现概率非常低
  95. });
  96. },
  97. GetRequest(urlStr) {
  98. if (typeof urlStr == 'undefined') {
  99. var url = decodeURI(location.search); //获取url中"?"符后的字符串
  100. } else {
  101. var url = '?' + urlStr.split('?')[1];
  102. }
  103. var theRequest = new Object();
  104. var strs;
  105. if (url.indexOf('?') != -1) {
  106. var str = url.substr(1);
  107. strs = str.split('&');
  108. for (var i = 0; i < strs.length; i++) {
  109. theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
  110. }
  111. }
  112. return theRequest;
  113. }
  114. }
  115. };
  116. </script>
  117. <style scoped="scoped">
  118. .app {
  119. margin: 0 auto;
  120. height: auto;
  121. padding: 0;
  122. }
  123. .app img {
  124. /* width: 375px; */
  125. }
  126. </style>