gt4-index-alicom.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="renderer" content="webkit">
  7. <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  8. <title>请通过以下验证</title>
  9. <style>
  10. body {
  11. margin: 0;
  12. }
  13. .title {
  14. height: 18px;
  15. margin: 15px;
  16. font-size: 18px;
  17. line-height: 18px;
  18. }
  19. #log {
  20. display: none;
  21. margin: 0;
  22. word-break: break-all;
  23. }
  24. #log.log {
  25. display: block;
  26. }
  27. #captcha_index_loading {
  28. position: fixed;
  29. top: 50%;
  30. left: 50%;
  31. overflow: hidden;
  32. transform: translate(-50%, -50%);
  33. }
  34. #captcha {
  35. position: absolute;
  36. top: 50%;
  37. left: 50%;
  38. width: 260px;
  39. height: 50px;
  40. transform: translate(-50%, -50%);
  41. }
  42. /* loading */
  43. .loader {
  44. width: 40px;
  45. height: 40px;
  46. }
  47. svg path,
  48. svg rect {
  49. fill: #3973ff;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div id="log"></div>
  55. <div id="captcha"></div>
  56. <div id="captcha_index_loading">
  57. <img class="captcha_index_img loader" id="captcha_img_loading" alt="加载中" style="display: none;">
  58. </div>
  59. <script>
  60. var debug = false;
  61. var logEle = document.getElementById('log');
  62. var isOffline = false;
  63. var indexVersion = '1.3.4'; // 中间页版本号,每次修改文件要进行版本号升级
  64. function log(str) {
  65. if (!debug) {
  66. return;
  67. }
  68. var p = document.createElement('p');
  69. p.appendChild(document.createTextNode(str));
  70. logEle.insertBefore(p, logEle.firstChild);
  71. // 同时将错误输出到控制台
  72. console.log(str);
  73. }
  74. (function () {
  75. // 错误构造函数
  76. function newError(msg, code, detail) {
  77. return {
  78. desc: {
  79. indexVersion: indexVersion,
  80. detail: detail
  81. },
  82. msg: msg,
  83. code: code
  84. }
  85. }
  86. function uploadExtraData(type, extraData) {
  87. var self = window;
  88. // 参数应该存在length属性,并且参数长度不可超过限制
  89. if (!extraData || (!extraData.length) || (extraData.length >= 1024 * 4)) {
  90. return;
  91. }
  92. if (!self.extraData) {
  93. self.extraData = {};
  94. }
  95. // 设计成可拓展的结构,防止以后有类似的字段增加需求
  96. self.extraData[type] = extraData;
  97. }
  98. var jsBridge = (function () {
  99. var callbacks = {
  100. showBox: function () {
  101. captchaObj.showBox && captchaObj.showBox();
  102. },
  103. postNativeMessage: function (data) {
  104. log('getCore time: ' + ((new Date()).getTime()));
  105. uploadExtraData('GeeToken', data);
  106. }
  107. };
  108. var call = function (data) {
  109. //调用原生方法
  110. try {
  111. nativeCall(JSON.stringify(data))
  112. } catch (e) {
  113. log('调用navtive接口异常');
  114. }
  115. };
  116. // 适配鸿蒙
  117. var harmonys = JSON.parse(decodeURIComponent(location.href.split('?')[1].split('=')[1]));
  118. var nativeCall = harmonys.harmony? function (data) {
  119. if (window.webkit && window.webkit.messageHandlers) {
  120. return window.webkit.messageHandlers.gt4Notify.postMessage.bind(window.webkit.messageHandlers.gt4Notify)
  121. } else if (window.JSInterface) {
  122. // return JSInterface.gt4Notify.bind(JSInterface)
  123. JSInterface.call(data)
  124. } else {
  125. log('请在移动端环境运行')
  126. }
  127. }: (function () {
  128. if (window.webkit && window.webkit.messageHandlers) {
  129. return window.webkit.messageHandlers.gt4Notify.postMessage.bind(window.webkit.messageHandlers.gt4Notify)
  130. } else if (window.JSInterface) {
  131. return JSInterface.gt4Notify.bind(JSInterface)
  132. } else {
  133. log('请在移动端环境运行')
  134. }
  135. })();
  136. return {
  137. callback: function (type, data) {
  138. return callbacks[type](data);
  139. },
  140. callNative: call
  141. }
  142. })();
  143. // 暴露 jsBridge
  144. window.jsBridge = jsBridge;
  145. // jsBridge 与 全局错误捕获要尽可能早的加载,防止gt4中的错误捕获不到;
  146. if (window.addEventListener) {
  147. window.addEventListener('error', function (e) {
  148. jsBridge.callNative({
  149. type: 'error',
  150. data: newError("gt4-index error", 60302, e.message)
  151. });
  152. })
  153. } else {
  154. window.onerror = function (e) {
  155. jsBridge.callNative({
  156. type: 'error',
  157. data: newError("gt4-index error", 60302, e.message)
  158. });
  159. }
  160. }
  161. })();
  162. </script>
  163. <!-- 此链接位置不要改变,防止上面页面错误catch 不到gt4的错误-->
  164. <script src="./ct4.js"></script>
  165. <script>
  166. (function () {
  167. //geeToken兼容ai下无token
  168. if (!isOffline) {
  169. // 非宕机模式下获取移动端coreSDK数据
  170. jsBridge.callNative({
  171. type: 'get'
  172. })
  173. }
  174. var startTime = 0;
  175. var clearID = 0;
  176. var captcha = document.getElementById('captcha');
  177. var loadingEle = document.getElementById('captcha_index_loading');
  178. var img = document.getElementById('captcha_img_loading');
  179. var query = location.href.split('?')[1];
  180. // 兼容安卓4.3版本样式问题
  181. var ua = navigator.userAgent.toLowerCase();
  182. if(/Android/.test(window.navigator.userAgent) && /android\s([\w.]+)/.exec(ua)[1] <= 4.3) {
  183. loadingEle.style.left = '45%';
  184. }
  185. // 兼容低版本Object.assign
  186. if (typeof Object.assign != 'function') {
  187. // Must be writable: true, enumerable: false, configurable: true
  188. Object.defineProperty(Object, "assign", {
  189. value: function assign(target, varArgs) { // .length of function is 2
  190. if (target == null) { // TypeError if undefined or null
  191. throw new TypeError('Cannot convert undefined or null to object');
  192. }
  193. var to = Object(target);
  194. for (var index = 1; index < arguments.length; index++) {
  195. var nextSource = arguments[index];
  196. if (nextSource != null) { // Skip over if undefined or null
  197. for (var nextKey in nextSource) {
  198. // Avoid bugs when hasOwnProperty is shadowed
  199. if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
  200. to[nextKey] = nextSource[nextKey];
  201. }
  202. }
  203. }
  204. }
  205. return to;
  206. },
  207. writable: true,
  208. configurable: true
  209. });
  210. }
  211. // 默认配置
  212. var config = {
  213. product: 'bind',
  214. onError: function (e) {
  215. // 错误信息中插入版本号
  216. e.desc = Object.assign({}, e.desc, { indexVersion: indexVersion });
  217. jsBridge.callNative({
  218. type: 'error',
  219. data: e
  220. });
  221. }
  222. };
  223. function setDebug() {
  224. debug = true;
  225. logEle.className = 'log';
  226. }
  227. function paseURI() {
  228. return JSON.parse(decodeURIComponent(query.split('=')[1]));
  229. }
  230. function checkArgs(args) {
  231. //设置loading
  232. if(args['loading']){
  233. img.src = args['loading'];
  234. clearID = setTimeout(function () {
  235. img.style.display = 'inline-block';
  236. }, 200);
  237. }
  238. // 是否开启debug
  239. if (args['debug']) {
  240. setDebug(true);
  241. startTime = (new Date()).getTime();
  242. log(JSON.stringify(args))
  243. }
  244. // 设置title
  245. if (args['title'] && args['title'] != "") {
  246. var h3 = document.createElement('h3');
  247. h3.className = 'title';
  248. h3.appendChild(document.createTextNode(decodeURIComponent(args['title'])));
  249. captcha.appendChild(h3);
  250. }
  251. // 检查必要参数
  252. if (!args['captchaId']) {
  253. log('args error: ' + query);
  254. }
  255. // 通过useLocalOffline判断是否会加入宕机效验
  256. if (args['useLocalOffline']) {
  257. //宕机启用自己的方法
  258. config.offlineCb = function () {
  259. isOffline = true;
  260. jsBridge.callNative({
  261. type: 'result',
  262. data: {
  263. captcha_id: args['captchaId'],
  264. challenge: args['challenge'],
  265. offline: true
  266. }
  267. });
  268. }
  269. }
  270. }
  271. function mergeOptions(args) {
  272. for (var k in args) {
  273. if (args.hasOwnProperty(k) &&
  274. ['debug', 'title', args['type']].indexOf(k) === -1) {
  275. config[k] = args[k];
  276. }
  277. }
  278. }
  279. if (!query) {
  280. setDebug();
  281. log('no query: ' + location.href);
  282. return false;
  283. }
  284. // 解析参数
  285. var args = paseURI(query);
  286. // 检查参数
  287. checkArgs(args);
  288. // 合并配置项
  289. mergeOptions(args);
  290. // 初始化验证码
  291. var handler = function (captchaObj) {
  292. window.captchaObj = captchaObj;
  293. captchaObj
  294. .appendTo(captcha)
  295. .onSuccess(function () {
  296. var result = captchaObj.getValidate();
  297. log('Success validate: ' + result);
  298. jsBridge.callNative({
  299. type: 'result',
  300. data: result
  301. });
  302. })
  303. .onReady(function () {
  304. log('load time: ' + ((new Date()).getTime() - (startTime || 0)));
  305. jsBridge.callNative({
  306. type: 'ready',
  307. data: {
  308. ready: 0
  309. }
  310. })
  311. clearTimeout(clearID);
  312. loadingEle.style.display = 'none';
  313. })
  314. .onClose(function () {
  315. jsBridge.callNative({
  316. type: 'close'
  317. });
  318. })
  319. .onError(function (e) {
  320. e.desc && (e.desc.indexVersion = indexVersion);
  321. jsBridge.callNative({
  322. type: 'error',
  323. data: e
  324. });
  325. })
  326. .onFail(function (e) {
  327. jsBridge.callNative({
  328. type: 'fail',
  329. data: e
  330. });
  331. })
  332. };
  333. window.initAlicom4(config, handler);
  334. })();
  335. </script>
  336. </body>
  337. </html>