utils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import querystring from "querystring"
  2. import _this from "../main"
  3. import $CONFIG from "../config"
  4. import { Base64 } from "js-base64"
  5. var CryptoJS = require('crypto-js');
  6. var Md5 = require("js-md5");
  7. const utils = {
  8. // 设置cookie
  9. setCookie(name, value, days) {
  10. var exp = new Date();
  11. exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
  12. document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
  13. },
  14. // 读取cookie
  15. getCookie(name) {
  16. var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  17. if (arr = document.cookie.match(reg)) {
  18. return unescape(arr[2]);
  19. }
  20. return null;
  21. },
  22. // 删除cookies
  23. delCookie(name) {
  24. var exp = new Date();
  25. exp.setTime(exp.getTime() - 1);
  26. var cval = this.getCookie(name);
  27. if (cval != null) {
  28. document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
  29. }
  30. },
  31. /**
  32. * 用来遍历传入的浏览器标识,查询其中包含的build/下标
  33. * @param {*} msg 浏览器标识,手机型号
  34. * @param {*} str 从msg中寻找的参数
  35. */
  36. getModels(msg, str) {
  37. for (let i in msg) {
  38. if (msg[i].indexOf(str) > 0) {
  39. return i;
  40. }
  41. }
  42. return -1;
  43. },
  44. /**
  45. * 手机号格式校验
  46. * @param {number} tel 手机号
  47. */
  48. checkVerification(tel) {
  49. // 判断手机号格式是否正确
  50. if (!/^((1[3,5,8,7,9][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/.test(
  51. tel
  52. )) {
  53. return false;
  54. }
  55. return true;
  56. },
  57. /**
  58. * cryptoJs base64 加密
  59. * @param {string} encStr 需要加密的值
  60. */
  61. encrypt(encStr) {
  62. const wordArray = CryptoJS.enc.Utf8.parse(encStr);
  63. const base64EncStr = CryptoJS.enc.Base64.stringify(wordArray);
  64. return base64EncStr;
  65. },
  66. /**
  67. * cryptoJs base64 解密
  68. * @param {string} decStr 需要解密的值
  69. */
  70. decrypt(decStr) {
  71. const parsedWordArray = CryptoJS.enc.Base64.parse(decStr);
  72. const parsedStr = parsedWordArray.toString(CryptoJS.enc.Utf8);
  73. return parsedStr;
  74. },
  75. /**
  76. * 判断是否微信内
  77. */
  78. isWeChat() {
  79. if (typeof WeixinJSBridge !== "undefined") {
  80. return 1;
  81. }
  82. return 0;
  83. },
  84. /**
  85. * 判断是否pc
  86. * @returns 是否pc端
  87. */
  88. IsPC() {
  89. if (!navigator.userAgent.match(/AppleWebKit.*Mobile.*/)) {
  90. // pc
  91. return true
  92. }
  93. return false
  94. },
  95. /**
  96. * 判断是否ios
  97. * @returns 是否ios端(已排除pc)
  98. */
  99. isiOS() {
  100. if (!this.IsPC()) {
  101. var u = navigator.userAgent;
  102. var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  103. return isiOS
  104. }
  105. },
  106. /**
  107. * 获取支付类型
  108. * @param {string} pay_platform 支付平台
  109. */
  110. payType(pay_platform) {
  111. const payType = {}
  112. switch (pay_platform) {
  113. case "ALIPAY":
  114. payType.h5 = "ALIPAY-H5"
  115. break;
  116. case "WECHATPAY":
  117. const isWeChat = this.isWeChat()
  118. payType.h5 = isWeChat ? "WECHAT-JSAPI" : "WECHAT-H5"
  119. break;
  120. case "HB":
  121. payType.h5 = "HB"
  122. break;
  123. }
  124. return payType
  125. },
  126. /**
  127. * md5 加密
  128. * @param {string} str 需要加密的字符串
  129. */
  130. md5(str) {
  131. return Md5(str)
  132. },
  133. /**
  134. * 查找query 不传即为查找window.location中的参数
  135. * @param {*} name 需要查找的字符串的key
  136. * @param {*} search 查找search中的name
  137. */
  138. getQueryString(name, search) {
  139. const str =
  140. search ||
  141. window.location.hash.split("?")[1] ||
  142. window.location.search.substr(1);
  143. let result = querystring.parse(str)[name];
  144. // 当有重复参数时querystring.parse返回的是数组
  145. if (result instanceof Array) {
  146. result = result[0];
  147. }
  148. return result || null;
  149. },
  150. /**
  151. * 获取url中所有的参数
  152. * @param {string} search 查找search中 ? 后面带的 所有的参数
  153. */
  154. getAllQueryString(search) {
  155. const str =
  156. search ||
  157. window.location.hash.split("?")[1] ||
  158. window.location.search.substr(1)
  159. if (!str) {
  160. return null
  161. }
  162. let resuleObj = new Object();
  163. let strs = str.split("&");
  164. for (var i = 0; i < strs.length; i++) {
  165. resuleObj[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
  166. }
  167. return resuleObj
  168. },
  169. // 删除url参数值
  170. delQueStr(url, ref) {
  171. var str = "";
  172. if (url.indexOf('?') != -1)
  173. str = url.substr(url.indexOf('?') + 1);
  174. else
  175. return url;
  176. var arr = "";
  177. var returnurl = "";
  178. if (str.indexOf('&') != -1) {
  179. arr = str.split('&');
  180. for (i in arr) {
  181. if (arr[i].split('=')[0] != ref) {
  182. returnurl = returnurl + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
  183. }
  184. }
  185. return url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
  186. } else {
  187. arr = str.split('=');
  188. if (arr[0] == ref)
  189. return url.substr(0, url.indexOf('?'));
  190. else
  191. return url;
  192. }
  193. },
  194. // 记住密码工具函数
  195. rememberFun(account, password) {
  196. const platform = this.getQueryString("platform");
  197. // 忘川伏魔录
  198. if (platform === "wcfml") {
  199. return this.onRememberFun(account, password)
  200. }
  201. },
  202. // 记住密码工具函数
  203. onRememberFun(account, password) {
  204. const accountEnc = this.encrypt(account);
  205. const pwdEnc = this.encrypt(password);
  206. const accountPwd = `${accountEnc}&&${pwdEnc}`;
  207. let accountLocal = this.readStorage("", "jhremember");
  208. accountLocal = accountLocal && JSON.parse(accountLocal);
  209. let accountList = []
  210. // 获取账号密码列表
  211. accountLocal &&
  212. accountLocal.length &&
  213. accountLocal.map((ele) => {
  214. const _account = ele.split("&&")[0];
  215. const _pwd = ele.split("&&")[1];
  216. const account = this.decrypt(_account);
  217. const pwd = this.decrypt(_pwd);
  218. accountList.push({
  219. account,
  220. pwd,
  221. });
  222. });
  223. // 初始状态
  224. if (!accountLocal) {
  225. accountLocal = [accountPwd];
  226. accountList.unshift({
  227. account,
  228. pwd: password,
  229. });
  230. this.writeStorage("", "jhremember", JSON.stringify(accountLocal));
  231. return accountList;
  232. }
  233. // 已经存有账密
  234. // 1. 判断账号是否存在
  235. const reAccount = accountList.filter((ele) => {
  236. return ele.account == account;
  237. });
  238. // 1 如果账号存在
  239. if (reAccount.length > 0) {
  240. // 删除原来的账密 存
  241. accountLocal = accountLocal.filter((ele) => {
  242. const _accountEcv = ele.split("&&")[0];
  243. return _accountEcv !== accountEnc;
  244. });
  245. accountList = accountList.filter((ele) => {
  246. return ele.account !== account
  247. })
  248. }
  249. // 2. 账号不存在 存
  250. accountLocal = accountLocal ? [accountPwd, ...accountLocal] : [accountPwd];
  251. accountList.unshift({
  252. account,
  253. pwd: password,
  254. });
  255. this.writeStorage("", "jhremember", JSON.stringify(accountLocal));
  256. return accountList
  257. },
  258. // 读取数据
  259. readStorage(bookName, keyName) {
  260. const platform = this.getQueryString("platform");
  261. // 忘川伏魔录
  262. if (platform == "wcfml") {
  263. // 解密 data
  264. const urlData = this.getQueryString('data')
  265. const decodeUrlData = decodeURIComponent(this.base64Decode(urlData));
  266. let data = urlData ? JSON.parse(decodeUrlData) : {}
  267. return localStorage.getItem(keyName) || data[keyName];
  268. }
  269. },
  270. // 存数据
  271. writeStorage(bookName, keyName, value) {
  272. // 忘川伏魔录
  273. const platform = this.getQueryString("platform");
  274. if (platform == "wcfml") {
  275. localStorage.setItem(keyName, value);
  276. const query = this.getAllQueryString();
  277. let data = decodeURIComponent(query.data);
  278. const dataUrl = decodeURIComponent(this.base64Decode(data))
  279. query.data = data ? JSON.parse(dataUrl) : {}
  280. query.data[keyName] = value;
  281. // 加密传递给wcfml
  282. query.data = this.base64Encode(JSON.stringify(query.data))
  283. this.routeLink("", query, "", _this)
  284. return
  285. }
  286. },
  287. // 删除数据(keyName)
  288. deleteStorage(bookName, keyName) {
  289. // 忘川伏魔录特制版
  290. const platform = this.getQueryString("platform");
  291. if (platform == "wcfml") {
  292. return
  293. }
  294. },
  295. /**
  296. * 判断是否pc
  297. * @returns 是否pc端
  298. */
  299. IsPC() {
  300. if (!navigator.userAgent.match(/AppleWebKit.*Mobile.*/)) {
  301. // pc
  302. return true
  303. }
  304. return false
  305. },
  306. // 销毁数据(bookName)
  307. destoryStorage(bookName) {
  308. // 忘川伏魔录特制版
  309. },
  310. // 获取所有bookName里面的key列表
  311. getKeysInStorage(bookName) {
  312. // 忘川伏魔录特制版
  313. },
  314. // 对象序列化工具函数
  315. queryStringUtil(query) {
  316. return querystring.stringify(query)
  317. },
  318. // vue-router跳转页面
  319. routeLink(routeName, query, params) {
  320. let _routeName = routeName || _this.$route.name
  321. _this.$router
  322. .push({
  323. name: _routeName,
  324. query,
  325. params,
  326. })
  327. .catch((err) => {
  328. // console.log(err);
  329. });
  330. },
  331. // 关闭支付页面工具函数
  332. closePage(uid) {
  333. // 忘川伏魔录
  334. const platform = this.getQueryString("platform");
  335. const data = encodeURIComponent(this.getQueryString("data"));
  336. if (platform == "wcfml") {
  337. // 跳转loading
  338. const query = this.getAllQueryString();
  339. this.routeLink("Loading", query);
  340. window.location.href = `${$CONFIG.wcfmlCloseUrl}?uid=${uid}&data=${data}`
  341. return
  342. }
  343. },
  344. // 联系qq客服
  345. onQQService() {
  346. const { kfQQ } = $CONFIG;
  347. const isPc = this.IsPC()
  348. const urlMobil = `mqqwpa://im/chat?chat_type=wpa&uin=${kfQQ}&version=1&src_type=web&web_src=http:://wpa.b.qq.com`;
  349. const urlPc = `http://wpa.qq.com/msgrd?v=3&uin=${kfQQ}&site=qq&menu=yes`;
  350. const url = isPc ? urlPc : urlMobil;
  351. window.open(url, "_brank");
  352. },
  353. // wcfml 游戏内登录工具函数
  354. wcfmlLoginUtils(data) {
  355. // 记住密码 todo
  356. const savedata = decodeURIComponent(this.getQueryString("data"));
  357. // 跳转对应登录链接
  358. const query = this.queryStringUtil({
  359. uid: data.uid,
  360. data: encodeURIComponent(savedata),
  361. });
  362. // 跳转loading
  363. const _query = this.getAllQueryString();
  364. this.routeLink("Loading", _query);
  365. window.location.href = `${$CONFIG.wcfmlLoginUrl}?${query}`;
  366. },
  367. // 倒计时
  368. countDown(startTime, endTime) {
  369. // 两个日期对象,相差的秒数
  370. let interval = endTime - startTime;
  371. interval /= 1000
  372. // 求 相差的天数/小时数/分钟数/秒数
  373. let hour, minute, second;
  374. hour = Math.floor(interval / 60 / 60);
  375. minute = Math.floor(interval / 60 - hour * 60);
  376. second = Math.floor(interval - hour * 60 * 60 - minute * 60);
  377. hour = hour < 10 ? `0${hour}` : hour
  378. minute = minute < 10 ? `0${minute}` : minute
  379. second = second < 10 ? `0${second}` : second
  380. return {
  381. hour,
  382. minute,
  383. second
  384. }
  385. },
  386. // base64 加密
  387. base64Encode(string) {
  388. const str = encodeURIComponent(string)
  389. return Base64.encode(str)
  390. },
  391. // 解密
  392. base64Decode(string) {
  393. const str = decodeURIComponent(string)
  394. return Base64.decode(str)
  395. }
  396. };
  397. export default utils;