index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // @ts-ignore
  2. import Cookie from 'js-cookie';
  3. import { getStorage, storageKey } from './storage'
  4. export const appVersionCode = 17;
  5. export const appType = 1;
  6. // @ts-ignore
  7. export const callBackUrl = process.env.NODE_ENV === `production` ? `https://denet.me/close` : process.env.NODE_ENV === `pre` ? `https://pre.denet.me/close` : `https://test.denet.me/close`
  8. // 获取host
  9. export const getEnvConfig = () => {
  10. let host, logHost
  11. // @ts-ignore
  12. switch(process.env.NODE_ENV) {
  13. case `production`:
  14. host = `https://api.denetme.net`
  15. logHost = `https://log.denetme.net`
  16. break;
  17. case `pre`:
  18. host = `https://preapi.denetme.net`
  19. logHost = `https://prelog.denetme.net`
  20. break;
  21. default:
  22. host = `https://testapi.denetme.net`
  23. logHost = `https://testlog.denetme.net`
  24. break;
  25. }
  26. return {
  27. host,
  28. logHost,
  29. };
  30. }
  31. // 获取mid
  32. export const getMid = () => {
  33. let _mid;
  34. let _cookie_mid_arr = Cookie.get('mid') || []
  35. if (_cookie_mid_arr.length > 0) {
  36. _mid = JSON.parse(_cookie_mid_arr)[0].mid
  37. } else {
  38. _mid = guid()
  39. Cookie.set('mid', JSON.stringify([{ mid: _mid }]), { expires: 1000 })
  40. }
  41. return _mid;
  42. }
  43. export const getUserInfo = () => {
  44. let userInfo = getStorage(storageKey.userInfo) || null;
  45. if (userInfo) {
  46. return userInfo;
  47. } else {
  48. return null
  49. }
  50. }
  51. // 推特授权url
  52. export const getOauthUrl = (token: string) => {
  53. return `https://api.twitter.com/oauth/authenticate?oauth_token=${token}`
  54. }
  55. // 创建窗口
  56. export const createWindow = (url: string, w: number = 400, h: number = 600) => {
  57. var left = Math.round((window.screen.availWidth - w) / 2);
  58. var top = Math.round((window.screen.availHeight - 100 - h) / 2);
  59. var win = window.open(url, `newWin`, `width=${w}, height=${h}, top=${top}, left=${left}, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no`);
  60. return win;
  61. }
  62. // 帮助函数
  63. const guid = () => {
  64. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  65. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  66. return v.toString(16);
  67. });
  68. }
  69. // 获取cookie
  70. export const getCookie = (name: string) => {
  71. if (name) {
  72. let getVal = Cookie.get(name)
  73. return getVal
  74. }
  75. }
  76. // 设置cookie
  77. export const setCookie = (name: string, val: any) => {
  78. Cookie.set(name, JSON.stringify(val), { expires: 1000 })
  79. }
  80. // 删除cookie
  81. export const removeCookie = (name: string) => {
  82. Cookie.remove(name)
  83. }
  84. export function debounce(fn: any, delay: number) {
  85. let timer: number; // 定时器
  86. return function (...args) {
  87. let context = this;
  88. timer && clearTimeout(timer);
  89. timer = setTimeout(function () {
  90. fn.apply(context, args);
  91. }, delay);
  92. };
  93. }
  94. export function getBrowser() {
  95. let browser;
  96. let UserAgent = navigator.userAgent.toLowerCase();
  97. if (UserAgent.indexOf('chrome') > -1 || UserAgent.indexOf('crios') > -1) {
  98. browser = `Chrome`
  99. } else if (UserAgent.indexOf('firefox') > -1) {
  100. browser = `Firefox`
  101. } else if (UserAgent.indexOf('opera') > -1) {
  102. browser = `Opera`
  103. } else if (UserAgent.indexOf('safari') > -1 && UserAgent.indexOf('chrome') == -1) {
  104. browser = `Safari`
  105. } else if (UserAgent.indexOf('edge') > -1) {
  106. browser = `Edge`
  107. } else {
  108. browser = `Other`
  109. }
  110. return browser;
  111. }