| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import Foundation from '@/utils/Foundation.js';
- import {
- md5
- } from '@/utils/md5.js';
- import storage from "@/utils/storage.js";
- import store from "@/store/index.js";
- // 重新整理一下config
- const configHandle = (config) => {
- // 'development', 'production'
- if (process.env.NODE_ENV === 'development') {
- const nonce = Foundation.randomString(6)
- const timestamp = parseInt(new Date().getTime() / 1000)
- const sign = md5( nonce + timestamp + storage.getAccessToken())
- if (config.url.indexOf('?') === -1) {
- config.url = `${config.url}?&nonce=${nonce}×tamp=${timestamp}&sign=${sign}`
- } else {
- let params = urlParse(config.url);
- console.info(params);
- let url = config.url.split('?')[0];
- params = { ...params,
- nonce,
- timestamp,
- sign
- };
- let str = '';
- for (var k in params) {
- console.info(k, params[k])
- str += '&' + k + '=' + params[k];
- }
- str = str.substr(1);
- config.url = `${url}?${str}`;
- }
- config.header = {
- ...config.header,
- uuid: storage.getUuid()
- }
- } else {
- config.header = {
- ...config.header,
- Authorization: storage.getAccessToken(),
- uuid: storage.getUuid()
- }
- }
- return config
- }
- /**
- * 解析url参数
- * @example ?id=12345&a=b
- * @return Object {id:12345,a:b}
- */
- function urlParse(url) {
- let obj = {};
- let reg = /[?&][^?&]+=[^?&]+/g;
- let arr = url.match(reg);
- if (arr) {
- arr.forEach((item) => {
- let tempArr = item.substring(1).split('=');
- let key = decodeURIComponent(tempArr[0]);
- let val = decodeURIComponent(tempArr.splice(1).join('='));
- obj[key] = val;
- });
- }
- return obj;
- };
- const getNetworkType = () => {
- uni.getNetworkType({
- success: (res) => {
- if (res.networkType === 'none') {
- uni.showToast({
- title: '网络好像有点问题,请检查后重试!',
- duration: 2000,
- icon: 'none'
- });
- let pages = getCurrentPages();
- if (pages.length) {
- let route = pages[pages.length - 1].route;
- if (route !== 'pages/empty/empty') {
- uni.navigateTo({
- url: `/pages/empty/empty?type=wifi`
- })
- }
- }else{
- uni.navigateTo({
- url: `/pages/empty/empty?type=wifi`
- })
- }
- }
- }
- })
- }
- const throttle = (fn, that, gapTime) => {
- // export function throttle(fn, gapTime) {
- if (gapTime == null || gapTime == undefined) {
- gapTime = 1800
- }
- let _lastTime = that.lastTime
- let _nowTime = +new Date()
- if (_nowTime - _lastTime > gapTime || !_lastTime) {
- fn.apply(that, arguments) //将this和参数传给原函数
- that.lastTime = _nowTime
- }
- }
- /**
- * 计算传秒数的倒计时【天、时、分、秒】
- * @param seconds
- * @returns {{day : *, hours : *, minutes : *, seconds : *}}
- */
- const countTimeDown = (seconds) => {
- const leftTime = (time) => {
- if (time < 10) time = '0' + time
- return time + ''
- }
- return {
- day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
- hours: leftTime(parseInt(seconds / 60 / 60 % 24, 10)),
- minutes: leftTime(parseInt(seconds / 60 % 60, 10)),
- seconds: leftTime(parseInt(seconds % 60, 10))
- }
- }
- /**
- * 计算当前时间到第二天0点的倒计时[秒]
- * @returns {number}
- */
- const theNextDayTime = () => {
- const nowDate = new Date()
- const time = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate() + 1, 0, 0, 0).getTime() -
- nowDate.getTime()
- return parseInt(time / 1000)
- }
- export {
- //configHandle,
- getNetworkType,
- throttle,
- countTimeDown,
- theNextDayTime
- }
|