tool.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {
  2. isIpad,
  3. isIpod,
  4. isIphone,
  5. isWindows,
  6. isMac,
  7. isWechat,
  8. } from "./isTerminal";
  9. /**
  10. * 动态加载script
  11. * @param {string} url
  12. * @param {string} callback
  13. */
  14. export function loadScript(url, callback) {
  15. let script = document.createElement("script");
  16. if (script.readyState) {
  17. // IE
  18. script.onreadystatechange = function () {
  19. if (script.readyState === "loaded" || script.readyState === "complete") {
  20. script.onreadystatechange = null;
  21. callback();
  22. }
  23. };
  24. } else {
  25. // 其他浏览器
  26. script.onload = function () {
  27. callback();
  28. };
  29. }
  30. script.src = url;
  31. document.getElementsByTagName("head")[0].appendChild(script);
  32. }
  33. /**
  34. * 闪视频登录状态
  35. * @param {function} next
  36. * @returns
  37. */
  38. export function getUser(next) {
  39. window.$shanshipin = {};
  40. // 判断闪视频登录状态
  41. if (isWindows || isMac || isWechat) return next && next();
  42. // 获取登录信息
  43. if (isIpad || isIpod || isIphone) {
  44. if (!window.webkit || !window.webkit.messageHandlers) return next && next();
  45. window.setUser = user => {
  46. if (user == "{}")
  47. return window.webkit.messageHandlers.iosJumpLogin.postMessage([]);
  48. const u1 = JSON.parse(user || "{}");
  49. window.webkit.messageHandlers.getAppInfo.postMessage([]);
  50. window.setAppInfo = userJson => {
  51. const u2 = JSON.parse(userJson || "{}");
  52. window.$shanshipin = {
  53. ...u1,
  54. ...u2,
  55. };
  56. next && next();
  57. };
  58. };
  59. window.webkit.messageHandlers.tideGetUser.postMessage([]);
  60. } else {
  61. if (!window.TideApp) return next && next();
  62. const u1 = JSON.parse(window.TideApp.getUser() || "{}");
  63. if (!u1.UserId) window.TideApp.login();
  64. const u2 = JSON.parse(window.TideApp.getAppInfo() || "{}");
  65. window.$shanshipin = {
  66. ...u1,
  67. ...u2,
  68. };
  69. next && next();
  70. }
  71. }
  72. /**
  73. * 帧动画
  74. * @returns
  75. */
  76. export function requestAnimationFrame() {
  77. return (
  78. window.requestAnimationFrame ||
  79. window.webkitRequestAnimationFrame /* Safari 和 Chrome */ ||
  80. window.mozRequestAnimationFrame /* Firefox */ ||
  81. window.oRequestAnimationFrame /* Opera */ ||
  82. window.msRequestAnimationFrame /* IE 9 */ ||
  83. function (callback) {
  84. window.setTimeout(callback, 6000 / 60);
  85. }
  86. );
  87. }