help.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import Cookie from 'js-cookie';
  2. //application/vnd.chromium.remoting-viewer 可能为360特有 通过_mine判断是否是360
  3. export function isBrowser() {
  4. var agent = navigator.userAgent.toLowerCase();
  5. console.log(agent);
  6. const System = function () {
  7. if (agent.indexOf('qqbrowser') > 0) {
  8. //判断是qq浏览器还是其它浏览器
  9. return 'qq浏览器';
  10. }
  11. if (agent.indexOf('se 2.x') > 0) {
  12. return '搜狗浏览器';
  13. }
  14. var is360 = _mime('type', 'application/vnd.chromium.remoting-viewer');
  15. if (is360) {
  16. return '360浏览器';
  17. }
  18. //检测是否是谷歌内核(可排除360及谷歌以外的浏览器)
  19. //测试mime
  20. function _mime(option, value) {
  21. var mimeTypes = navigator.mimeTypes;
  22. console.log(mimeTypes);
  23. for (var mt in mimeTypes) {
  24. if (mimeTypes[mt][option] == value) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. };
  31. let s = System();
  32. if (s) {
  33. return s;
  34. }
  35. if (agent.indexOf('firefox') > 0) {
  36. return 'firefox浏览器';
  37. }
  38. if (agent.indexOf('trident') > 0) {
  39. return 'IE浏览器';
  40. }
  41. if (agent.indexOf('edg') > 0) {
  42. return 'IE浏览器';
  43. }
  44. if (agent.indexOf('safari') > 0 && agent.indexOf('chrome') < 0) {
  45. return 'Safari';
  46. }
  47. if (agent.indexOf('chrome') > 0) {
  48. return 'chrome';
  49. }
  50. }
  51. // 获取host
  52. export const getEnvConfig = () => {
  53. let host, logHost;
  54. console.log('NODE_ENV=', process.env.NUXT_ENV.MODE);
  55. // @ts-ignore
  56. switch (process.env.NUXT_ENV.MODE) {
  57. case `prod`:
  58. host = `https://api.denetme.net`;
  59. logHost = `https://log.weiqumeta.com`;
  60. break;
  61. case `pre`:
  62. host = `https://preapi.denetme.net`;
  63. logHost = `https://prelog.weiqumeta.com`;
  64. break;
  65. default:
  66. host = `https://testapi.denetme.net`;
  67. logHost = `https://testlog.weiqumeta.com`;
  68. break;
  69. }
  70. return {
  71. host,
  72. logHost,
  73. };
  74. };
  75. export function getBrowser() {
  76. let browser;
  77. let UserAgent = navigator.userAgent.toLowerCase();
  78. if (UserAgent.indexOf('chrome') > -1 || UserAgent.indexOf('crios') > -1) {
  79. browser = `Chrome`;
  80. } else if (UserAgent.indexOf('firefox') > -1) {
  81. browser = `Firefox`;
  82. } else if (UserAgent.indexOf('opera') > -1) {
  83. browser = `Opera`;
  84. } else if (UserAgent.indexOf('safari') > -1 && UserAgent.indexOf('chrome') == -1) {
  85. browser = `Safari`;
  86. } else if (UserAgent.indexOf('edge') > -1) {
  87. browser = `Edge`;
  88. } else {
  89. browser = `Other`;
  90. }
  91. return browser;
  92. }
  93. export function formatSecondsAsTime(secs) {
  94. var hr = Math.floor(secs / 3600);
  95. var min = Math.floor((secs - hr * 3600) / 60);
  96. var sec = Math.floor(secs - hr * 3600 - min * 60);
  97. var text;
  98. if (hr < 10) {
  99. hr = '0' + hr;
  100. }
  101. if (min < 10) {
  102. min = '0' + min;
  103. }
  104. if (sec < 10) {
  105. sec = '0' + sec;
  106. }
  107. text = hr + ':' + min + ':' + sec;
  108. return text;
  109. }
  110. // 抽奖红包 left
  111. export function formatSecondsAsDaysOrTime(secs) {
  112. if (secs <= 0) {
  113. return '00:00:00';
  114. }
  115. let text = '';
  116. var hr = Math.floor(secs / 3600);
  117. if (hr >= 24) {
  118. let day = parseInt(hr / 24);
  119. text = `${day} days left`;
  120. } else {
  121. text = formatSecondsAsTime(secs);
  122. }
  123. return text;
  124. }
  125. export const appVersionCode = 17;
  126. export const appType = 1;
  127. export function getBrowserType() {
  128. let device = ''; // ios 安卓 chrome no-chrome
  129. if (/android/i.test(navigator.userAgent)) {
  130. device = '安卓';
  131. } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  132. device = 'ios';
  133. } else if (isBrowser() == 'chrome') {
  134. device = 'chrome';
  135. } else {
  136. device = 'no-chrome';
  137. }
  138. return device;
  139. }
  140. const api = {
  141. prod: 'https://api.denetme.net',
  142. pre: 'https://preapi.denetme.net',
  143. test: 'https://testapi.denetme.net',
  144. };
  145. const page = {
  146. prod: 'https://h5.denetme.net',
  147. pre: 'https://preh5.denetme.net',
  148. test: 'https://testh5.denetme.net',
  149. };
  150. export const jumpUrl = page[process.env.NUXT_ENV.MODE] + '/';
  151. export const baseURL = api[process.env.NUXT_ENV.MODE];
  152. export const getStorage = (key) => {
  153. return JSON.parse(localStorage.getItem(key));
  154. };
  155. export const setStorage = (key, data) => {
  156. localStorage.setItem(key, JSON.stringify(data));
  157. };
  158. export const removeStorage = (key) => {
  159. localStorage.removeItem(key);
  160. };
  161. // storageKey
  162. export const storageKey = {
  163. verifier: 'verifierKey',
  164. userInfo: 'userInfo',
  165. backFromTwitterLogin: 'denet-mobile-landpage-back-from-twitter-login',
  166. };
  167. // 推特授权url
  168. export const getOauthUrl = (token) => {
  169. return `https://api.twitter.com/oauth/authenticate?oauth_token=${token}`;
  170. };
  171. // 创建窗口
  172. export const createWindow = (url, w = 400, h = 600) => {
  173. var left = Math.round((window.screen.availWidth - w) / 2);
  174. var top = Math.round((window.screen.availHeight - 100 - h) / 2);
  175. 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`);
  176. return win;
  177. };
  178. // 帮助函数
  179. const guid = () => {
  180. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  181. var r = (Math.random() * 16) | 0,
  182. v = c == 'x' ? r : (r & 0x3) | 0x8;
  183. return v.toString(16);
  184. });
  185. };
  186. // 获取mid
  187. export const getMid = () => {
  188. let _mid;
  189. let _cookie_mid_arr = Cookie.get('mid') || [];
  190. if (_cookie_mid_arr.length > 0) {
  191. _mid = JSON.parse(_cookie_mid_arr)[0].mid;
  192. } else {
  193. _mid = guid();
  194. Cookie.set('mid', JSON.stringify([{ mid: _mid }]), { expires: 1000 });
  195. }
  196. return _mid;
  197. };
  198. export const getUserInfo = () => {
  199. let userInfo = getStorage(storageKey.userInfo) || null;
  200. if (userInfo) {
  201. return userInfo;
  202. } else {
  203. return null;
  204. }
  205. };
  206. export function getQueryString(name) {
  207. let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  208. let r = window.location.search.substr(1).match(reg);
  209. if (r != null) {
  210. return decodeURIComponent(r[2]);
  211. }
  212. return null;
  213. }
  214. export const denetExtensionId = 'inlfbeejfdgkknpiodhemfcokbdgofja';
  215. export function detectExtension(extensionId, callback) {
  216. var img;
  217. img = new Image();
  218. img.src = 'chrome-extension://' + extensionId + '/img/icon-denet-logo.svg';
  219. img.onload = function () {
  220. callback(true);
  221. };
  222. img.onerror = function () {
  223. callback(false);
  224. };
  225. }