main.js.bak 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import store from './store'
  7. import axios from 'axios'
  8. let moment = require('moment-timezone')
  9. require('moment/locale/cs')
  10. import '@/permission'
  11. import MuseUI from 'muse-ui'
  12. import 'lib-flexible/flexible'
  13. import 'normalize.css'
  14. import 'muse-ui/dist/muse-ui.css'
  15. import '@/assets/common.css'
  16. // 全局组件
  17. Vue.use(MuseUI);
  18. import * as comps from '@/components/index'
  19. Vue.use(comps.CommonHead);
  20. Vue.use(comps.Grid);
  21. Vue.use(comps.Message);
  22. Vue.use(comps.Loading);
  23. import { Toast, Button } from 'vant'
  24. Vue.use(Toast).use(Button)
  25. Vue.config.productionTip = false
  26. Vue.prototype.$http = axios;
  27. Vue.directive('anchor', {
  28. inserted : function(el, binding) {
  29. el.onclick = function() {
  30. $('html,body').animate({
  31. scrollTop:($('#' + binding.value).offset().top + 'px')
  32. }, 800);
  33. }
  34. }
  35. })
  36. Vue.directive('authorize', {
  37. inserted: function(el, binding) {
  38. var authorities = store.state.home.authorities;
  39. var auth = binding.value;
  40. var authArr = auth.split(',');
  41. var hasAuth = false;
  42. authArr.forEach(element => {
  43. var index = authorities.indexOf(element, 'authority');
  44. if (index != -1) {
  45. hasAuth = true;
  46. return;
  47. }
  48. });
  49. if (!hasAuth) {
  50. el.parentNode.removeChild(el);
  51. }
  52. }
  53. })
  54. // 注册一个全局自定义指令 `v-focus`
  55. Vue.directive('focus', {
  56. // 当被绑定的元素插入到 DOM 中时……
  57. inserted: function (el, binding) {
  58. // 聚焦元素
  59. var focus = binding.value;
  60. if (focus) {
  61. el.focus()
  62. }
  63. }
  64. })
  65. //全局注册自定义指令,用于判断当前图片是否能够加载成功,可以加载成功则赋值为img的src属性,否则使用默认图片
  66. Vue.directive('real-img', {
  67. inserted: async function (el, binding) {//指令名称为:real-img
  68. let imgURL = binding.value;//获取图片地址
  69. if (imgURL) {
  70. let exist = await imageIsExist(imgURL);
  71. if (exist) {
  72. el.setAttribute('src', imgURL);
  73. } else if (el.parentNode) {
  74. el.parentNode.removeChild(el);
  75. }
  76. }
  77. }
  78. })
  79. /**
  80. * 检测图片是否存在
  81. * @param url
  82. */
  83. let imageIsExist = function(url) {
  84. return new Promise((resolve) => {
  85. var img = new Image();
  86. img.onload = function () {
  87. if (this.complete == true){
  88. resolve(true);
  89. img = null;
  90. }
  91. }
  92. img.onerror = function () {
  93. resolve(false);
  94. img = null;
  95. }
  96. img.src = url;
  97. })
  98. }
  99. Vue.directive('highlight', function (el, binding) {
  100. let obj = binding.value || {};
  101. var keyword = obj.keyword;
  102. var value = obj.value;
  103. if (value && keyword) {
  104. el.innerHTML = value.replaceAll(keyword, '<k style="color:#FF5722">' + keyword + '</k>')
  105. } else {
  106. el.innerHTML = value || ''
  107. }
  108. }
  109. )
  110. Vue.filter('highlight', function (value, keyword, defaultString) {
  111. if (!value || !keyword) {
  112. return value || '';
  113. }
  114. return value.replaceAll(keyword, '<k style="color:#FF5722">' + keyword + '</k>');
  115. });
  116. Vue.filter('moment', function (value, formatString, defaultString) {
  117. var val = value;
  118. if (!isNaN(value)) {
  119. val = Number(val);
  120. }
  121. return formatDate(val, formatString, defaultString);
  122. });
  123. Vue.filter('pv', function (value, property, defaultString) {
  124. return value ? value[property] : '';
  125. });
  126. Vue.filter('thousand', function (num) {
  127. if (num == 0) {
  128. return num;
  129. }
  130. if (!num) {
  131. return "";
  132. }
  133. return num.toString().replace(/\d+/, function (n) { // 先提取整数部分
  134. return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) { // 对整数部分添加分隔符
  135. return $1 + ",";
  136. });
  137. });
  138. });
  139. function formatDate(value, formatString, defaultString){
  140. if(value == null){
  141. return defaultString ? defaultString : "";
  142. }
  143. formatString = formatString || 'YYYY-MM-DD HH:mm';
  144. return moment(value).tz('Asia/Shanghai').format(formatString);
  145. }
  146. Vue.filter('dictValue', function (dicts, dictValue) {
  147. if(dicts){
  148. for(var i = 0; i < dicts.length; i++){
  149. if(dicts[i].value == dictValue){
  150. return dicts[i].label;
  151. }
  152. }
  153. }
  154. return "";
  155. });
  156. // 定义全局点击函数
  157. Vue.prototype.globalClick = function (callback) {
  158. document.getElementById('app').onclick = function (e) {
  159. callback(e);
  160. };
  161. };
  162. Array.prototype.indexOf = function (val, property) {
  163. for (var i = 0; i < this.length; i++) {
  164. if (!property && this[i] == val) {
  165. return i;
  166. } else if (property && this[i][property] == val) {
  167. return i;
  168. }
  169. }
  170. return -1;
  171. };
  172. import { autoLogin } from '@/utils/autoLogin'
  173. function init() {
  174. return new Promise((resolve, reject) => {
  175. let code = getQuery('code');
  176. if (code) {
  177. autoLogin(code, resolve, reject);
  178. } else {
  179. resolve();
  180. }
  181. });
  182. }
  183. init().then(() => {
  184. new Vue({
  185. el: '#app',
  186. router,
  187. store,
  188. components: { App },
  189. template: '<App/>',
  190. created() {
  191. }
  192. })
  193. }).catch((err) => {
  194. console.log(err)
  195. new Vue({
  196. el: '#app',
  197. router,
  198. store,
  199. components: { App },
  200. template: '<App/>',
  201. created() {
  202. }
  203. })
  204. });