help.js 6.1 KB

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