extSdk.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. window.onResume = document.createEvent('Event');
  2. window.onResume.initEvent('onResume', true, true);
  3. window.HiydSdk = {
  4. fnId : 0,
  5. cbList : [],
  6. }
  7. /**
  8. * 定时执行方法,防止ios里的延迟
  9. */
  10. HiydSdk.run = function(funName,args,duration){
  11. var self = this,timer;
  12. duration = duration || 500;
  13. self[funName].apply(HiydSdk,args);
  14. timer = setInterval(function(){
  15. if(typeof window.Hiyd != "undefined"){
  16. clearInterval(timer);
  17. }
  18. self[funName].apply(HiydSdk,args);
  19. },duration);
  20. }
  21. HiydSdk.run2 = function(funName, args, duration){
  22. var self = this;
  23. var duration = duration || 500;
  24. var timer;
  25. var times = 0; //最大尝试次数
  26. if(typeof window.Hiyd != "undefined") {
  27. self[funName].apply(HiydSdk, args);
  28. } else {
  29. timer = setInterval(function(){
  30. //最多尝试5次
  31. if(typeof window.Hiyd != "undefined" || times == 5){
  32. self[funName].apply(HiydSdk, args);
  33. clearInterval(timer);
  34. } else {
  35. times += 1;
  36. }
  37. }, duration)
  38. }
  39. }
  40. /**
  41. * 检查注入状态
  42. * @param {String} name 方法名
  43. */
  44. HiydSdk.checkInject = function(name) {
  45. if (typeof window.Hiyd != "undefined") {
  46. if(typeof Hiyd[name] == "undefined") {
  47. console.log('没有' + name + '这个方法')
  48. return false;
  49. } else {
  50. return true;
  51. }
  52. } else {
  53. console.log("js方法未注入!name:" + name)
  54. return false;
  55. }
  56. }
  57. HiydSdk.callfn = function() {
  58. var _fnId = this.fnId++;
  59. var name = arguments[0];
  60. var param = [];
  61. for(var i = 1; i < arguments.length; i++) {
  62. if(typeof arguments[i] == "function") {
  63. this.cbList[_fnId] = arguments[i];
  64. arguments[i] = 'HiydSdk.cbList[' + _fnId + ']';
  65. }
  66. param.push(arguments[i]);
  67. }
  68. if(this.checkInject(name)) {
  69. // console.log('Hiyd[' + name + '](' + param.join(',') + ')');
  70. Hiyd[name].apply(Hiyd, param);
  71. return true;
  72. } else {
  73. // console.error('Hiyd[' + name + '] is not exist.');
  74. return false;
  75. }
  76. }
  77. HiydSdk.appBridge = function(uri) {
  78. var iframe = document.createElement("IFRAME");
  79. iframe.setAttribute('src', uri);
  80. document.documentElement.appendChild(iframe);
  81. iframe.parentNode.removeChild(iframe);
  82. iframe = null;
  83. }
  84. HiydSdk.openLogin = function() {
  85. return this.callfn("openLogin");
  86. }
  87. HiydSdk.getUserInfo = function(callback) {
  88. return this.callfn("getUserInfo", callback);
  89. }
  90. HiydSdk.getDeviceInfo = function(callback) {
  91. return this.callfn("getDeviceInfo", callback);
  92. }
  93. HiydSdk.openUrl = function(url, title, needTopBar) {
  94. title = title || '';
  95. needTopBar = needTopBar !== null ? needTopBar : true;
  96. return this.callfn("openUrl", url, title, needTopBar);
  97. }
  98. HiydSdk.copy = function(txt) {
  99. return this.callfn("copy", txt);
  100. }
  101. HiydSdk.historyBack = function() {
  102. return this.callfn("historyBack");
  103. }
  104. HiydSdk.closeWebview = function() {
  105. return this.callfn("closeWebview");
  106. }
  107. HiydSdk.setTitle = function(title) {
  108. return this.callfn("setTitle", title);
  109. }
  110. HiydSdk.getNetworkType = function(callback) {
  111. return this.callfn("getNetworkType", callback);
  112. }
  113. HiydSdk.shareMessage = function(title, link, desc, imgUrl) {
  114. return this.callfn("shareMessage", title, link, desc, imgUrl);
  115. }
  116. HiydSdk.showLoading = function(txt) {
  117. txt = txt || '正在加载中...';
  118. return this.callfn("showLoading", txt);
  119. }
  120. HiydSdk.hideLoading = function() {
  121. return this.callfn("hideLoading");
  122. }
  123. HiydSdk.showTip = function(txt, timeout) {
  124. return this.callfn("showTip", txt, timeout);
  125. }
  126. HiydSdk.showErrorTip = function(txt, timeout) {
  127. if (window.Ouj && Ouj.showErrortip) {
  128. OujSdk.showTip(txt, timeout);
  129. } else {
  130. return this.callfn("showErrortip", txt, timeout);
  131. }
  132. }
  133. HiydSdk.showDialog = function(txt) {
  134. return this.callfn("showDialog", txt);
  135. }
  136. HiydSdk.confirm = function(txt, callback, title, buttonLables) {
  137. title = title || '提醒';
  138. buttonLables = buttonLables || '确定,取消';
  139. return this.callfn("confirm", title, txt, callback, buttonLables);
  140. }
  141. HiydSdk.hideMenuButton = function() {
  142. return this.callfn("hideMenuButton");
  143. }
  144. HiydSdk.hideShare = function() {
  145. return this.callfn("hideShare");
  146. }
  147. HiydSdk.aliPay = function(platformData, callback) {
  148. return this.callfn("aliPay", platformData, callback);
  149. }
  150. HiydSdk.wxPay = function(platformData, callback) {
  151. if (typeof platformData == 'object') {
  152. platformData = JSON.stringify(platformData);
  153. }
  154. return this.callfn("wxPay", platformData, callback);
  155. }
  156. HiydSdk.setShareInfo = function(title, link, desc, imgUrl) {
  157. return this.callfn("setShareInfo", title, link, desc, imgUrl);
  158. }
  159. HiydSdk.toTaskList = function() {
  160. return this.appBridge("hiyd://toTaskList");
  161. }
  162. HiydSdk.toFeedback = function() {
  163. return this.appBridge("hiyd://toFeedback");
  164. }
  165. export default HiydSdk;