help.js 6.9 KB

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