guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame^] | 1 | // 引入全局mixin |
| 2 | import mixin from './libs/mixin/mixin.js' |
| 3 | // 引入关于是否mixin集成小程序分享的配置 |
| 4 | // import wxshare from './libs/mixin/mpShare.js' |
| 5 | // 全局挂载引入http相关请求拦截插件 |
| 6 | import http from './libs/request' |
| 7 | |
| 8 | function wranning(str) { |
| 9 | // 开发环境进行信息输出,主要是一些报错信息 |
| 10 | // 这个环境的来由是在程序编写时候,点击hx编辑器运行调试代码的时候,详见: |
| 11 | // https://uniapp.dcloud.io/frame?id=%e5%bc%80%e5%8f%91%e7%8e%af%e5%a2%83%e5%92%8c%e7%94%9f%e4%ba%a7%e7%8e%af%e5%a2%83 |
| 12 | if (process.env.NODE_ENV === 'development') { |
| 13 | console.warn(str) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | // 尝试判断在根目录的/store中是否有$u.mixin.js,此文件uView默认为需要挂在到全局的vuex的state变量 |
| 18 | // HX2.6.11版本,放到try中,控制台依然会警告,暂时不用此方式, |
| 19 | // let vuexStore = {}; |
| 20 | // try { |
| 21 | // vuexStore = require("@/store/$u.mixin.js"); |
| 22 | // } catch (e) { |
| 23 | // //TODO handle the exception |
| 24 | // } |
| 25 | |
| 26 | // post类型对象参数转为get类型url参数 |
| 27 | import queryParams from './libs/function/queryParams.js' |
| 28 | // 路由封装 |
| 29 | import route from './libs/function/route.js' |
| 30 | // 时间格式化 |
| 31 | import timeFormat from './libs/function/timeFormat.js' |
| 32 | // 时间戳格式化,返回多久之前 |
| 33 | import timeFrom from './libs/function/timeFrom.js' |
| 34 | // 颜色渐变相关,colorGradient-颜色渐变,hexToRgb-十六进制颜色转rgb颜色,rgbToHex-rgb转十六进制 |
| 35 | import colorGradient from './libs/function/colorGradient.js' |
| 36 | // 生成全局唯一guid字符串 |
| 37 | import guid from './libs/function/guid.js' |
| 38 | // 主题相关颜色,info|success|warning|primary|default|error,此颜色已在uview.scss中定义,但是为js中也能使用,故也定义一份 |
| 39 | import color from './libs/function/color.js' |
| 40 | // 根据type获取图标名称 |
| 41 | import type2icon from './libs/function/type2icon.js' |
| 42 | // 打乱数组的顺序 |
| 43 | import randomArray from './libs/function/randomArray.js' |
| 44 | // 对象和数组的深度克隆 |
| 45 | import deepClone from './libs/function/deepClone.js' |
| 46 | // 对象深度拷贝 |
| 47 | import deepMerge from './libs/function/deepMerge.js' |
| 48 | // 添加单位 |
| 49 | import addUnit from './libs/function/addUnit.js' |
| 50 | |
| 51 | // 规则检验 |
| 52 | import test from './libs/function/test.js' |
| 53 | // 随机数 |
| 54 | import random from './libs/function/random.js' |
| 55 | // 去除空格 |
| 56 | import trim from './libs/function/trim.js' |
| 57 | // toast提示,对uni.showToast的封装 |
| 58 | import toast from './libs/function/toast.js' |
| 59 | // 获取父组件参数 |
| 60 | import getParent from './libs/function/getParent.js' |
| 61 | // 获取整个父组件 |
| 62 | import $parent from './libs/function/$parent.js' |
| 63 | // 获取sys()和os()工具方法 |
| 64 | // 获取设备信息,挂载到$u的sys()(system的缩写)属性中, |
| 65 | // 同时把安卓和ios平台的名称"ios"和"android"挂到$u.os()中,方便取用 |
| 66 | import {sys, os} from './libs/function/sys.js' |
| 67 | // 防抖方法 |
| 68 | import debounce from './libs/function/debounce.js' |
| 69 | // 节流方法 |
| 70 | import throttle from './libs/function/throttle.js' |
| 71 | |
| 72 | |
| 73 | // 配置信息 |
| 74 | import config from './libs/config/config.js' |
| 75 | // 各个需要fixed的地方的z-index配置文件 |
| 76 | import zIndex from './libs/config/zIndex.js' |
| 77 | |
| 78 | const $u = { |
| 79 | queryParams: queryParams, |
| 80 | route: route, |
| 81 | timeFormat: timeFormat, |
| 82 | date: timeFormat, // 另名date |
| 83 | timeFrom, |
| 84 | colorGradient: colorGradient.colorGradient, |
| 85 | guid, |
| 86 | color, |
| 87 | sys, |
| 88 | os, |
| 89 | type2icon, |
| 90 | randomArray, |
| 91 | wranning, |
| 92 | get: http.get, |
| 93 | post: http.post, |
| 94 | put: http.put, |
| 95 | 'delete': http.delete, |
| 96 | hexToRgb: colorGradient.hexToRgb, |
| 97 | rgbToHex: colorGradient.rgbToHex, |
| 98 | test, |
| 99 | random, |
| 100 | deepClone, |
| 101 | deepMerge, |
| 102 | getParent, |
| 103 | $parent, |
| 104 | addUnit, |
| 105 | trim, |
| 106 | type: ['primary', 'success', 'error', 'warning', 'info'], |
| 107 | http, |
| 108 | toast, |
| 109 | config, // uView配置信息相关,比如版本号 |
| 110 | zIndex, |
| 111 | debounce, |
| 112 | throttle, |
| 113 | } |
| 114 | |
| 115 | const install = Vue => { |
| 116 | Vue.mixin(mixin) |
| 117 | if (Vue.prototype.openShare) { |
| 118 | Vue.mixin(mpShare); |
| 119 | } |
| 120 | // Vue.mixin(vuexStore); |
| 121 | // 时间格式化,同时两个名称,date和timeFormat |
| 122 | Vue.filter('timeFormat', (timestamp, format) => { |
| 123 | return timeFormat(timestamp, format) |
| 124 | }) |
| 125 | Vue.filter('date', (timestamp, format) => { |
| 126 | return timeFormat(timestamp, format) |
| 127 | }) |
| 128 | // 将多久以前的方法,注入到全局过滤器 |
| 129 | Vue.filter('timeFrom', (timestamp, format) => { |
| 130 | return timeFrom(timestamp, format) |
| 131 | }) |
| 132 | Vue.prototype.$u = $u |
| 133 | } |
| 134 | |
| 135 | export default { |
| 136 | install |
| 137 | } |