index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="geetest_component"> </view>
  3. </template>
  4. <script>
  5. export default {
  6. data() {
  7. return {
  8. wv: null,
  9. lastTime: "",
  10. defaultConfig: {
  11. clientVersion: "uniapp-v1.0",
  12. clientType: uni.getSystemInfoSync().platform,
  13. protocol: "http://",
  14. mi: {
  15. geeid: {
  16. bd: "",
  17. d: "",
  18. e: "",
  19. fp: "",
  20. ts: "",
  21. ver: "",
  22. client_type: "",
  23. },
  24. packageName: "",
  25. displayName: "",
  26. appVer: "",
  27. build: "",
  28. clientVersion: "",
  29. process_id: "",
  30. process_id_test: "",
  31. zid: "",
  32. },
  33. },
  34. };
  35. },
  36. props: {
  37. config: {
  38. type: Object,
  39. default: function () {
  40. return {};
  41. },
  42. },
  43. },
  44. mounted() {},
  45. methods: {
  46. showCaptcha() {
  47. var that = this;
  48. // #ifdef APP-PLUS
  49. // 合并参数
  50. _assign(this.defaultConfig, this.config, {
  51. challenge: this.getUuid(),
  52. }); //每次更新challenge
  53. // 创建webview
  54. this.wv = plus.webview.create(
  55. `hybrid/html/captcha4/index.html?data=${encodeURIComponent(
  56. JSON.stringify(this.defaultConfig)
  57. )}`,
  58. "gt_webview",
  59. {
  60. background: "transparent",
  61. width: "100%", //String类型,窗口的宽度.支持百分比、像素值,默认为100%.未设置width属性值时,可同时设置left和right属性值改变窗口的默认宽度.
  62. height: "100%",
  63. }
  64. );
  65. // 获取webview
  66. var currentWebview = this.$root.$scope.$getAppWebview(); //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效
  67. currentWebview.append(this.wv);
  68. plus.globalEvent.addEventListener("plusMessage", (msg) => {
  69. //有重复推送问题
  70. const result = msg.data.args.data;
  71. if (result.name == "postMessage") {
  72. if (result.arg.time === that.lastTime) {
  73. // 处理uni连续推送bug
  74. return;
  75. }
  76. that.lastTime = result.arg.time;
  77. switch (result.arg.type) {
  78. case "ready":
  79. that.captchaReady();
  80. break;
  81. case "error":
  82. that.captchaError(result.arg.data);
  83. break;
  84. case "fail":
  85. that.captchaFail();
  86. break;
  87. case "close":
  88. that.captchaClose();
  89. break;
  90. case "result":
  91. that.captchaSuccess(result.arg.data);
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. });
  98. this.wv.overrideUrlLoading(
  99. {
  100. mode: "reject",
  101. },
  102. (e) => {
  103. plus.runtime.openURL(e.url);
  104. }
  105. );
  106. // #endif
  107. },
  108. captchaReady() {
  109. this.$emit("captchaReady");
  110. this.wv.evalJS("jsBridge.callback('showBox')");
  111. },
  112. captchaSuccess(data) {
  113. this.$emit("captchaSuccess", data);
  114. this.wv.hide();
  115. },
  116. captchaClose() {
  117. this.$emit("captchaClose");
  118. this.wv.hide();
  119. },
  120. captchaError: function (e) {
  121. uni.showToast({
  122. title: JSON.stringify(e),
  123. icon: "none",
  124. duration: 2000,
  125. });
  126. this.$emit("captchaError", e);
  127. this.wv.hide();
  128. },
  129. captchaFail() {
  130. this.$emit("captchaFail");
  131. },
  132. getAppWebview(that) {
  133. if (that.$scope) {
  134. return that.$scope.$getAppWebview();
  135. } else {
  136. this.getAppWebview(that.$parent);
  137. }
  138. },
  139. getUuid() {
  140. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
  141. /[xy]/g,
  142. function (c) {
  143. const r = (Math.random() * 16) | 0;
  144. const v = c === "x" ? r : (r & 0x3) | 0x8;
  145. return v.toString(16);
  146. }
  147. );
  148. },
  149. },
  150. };
  151. function _assign(target) {
  152. if (typeof Object.assign === "function") {
  153. return Object.assign.apply(Object, arguments);
  154. }
  155. if (target == null) {
  156. throw new Error("Cannot convert undefined or null to object");
  157. }
  158. const newTarget = Object(target);
  159. for (let index = 1; index < arguments.length; index++) {
  160. const source = arguments[index];
  161. if (source !== null) {
  162. for (const key in source) {
  163. if (Object.prototype.hasOwnProperty.call(source, key)) {
  164. newTarget[key] = source[key];
  165. }
  166. }
  167. }
  168. }
  169. return newTarget;
  170. }
  171. </script>
  172. <style lang="scss"></style>