method.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import vue from 'vue';
  2. import qs from 'qs'
  3. import app from '@/app'
  4. // 过去本地文件
  5. let getFile = function (config) {
  6. return new Promise((res, rec) => {
  7. uni.chooseImage({
  8. count: config.config || 9,
  9. sizeType:['compressed '],
  10. success: (chooseImageRes) => {
  11. res(chooseImageRes)
  12. },
  13. fail: () => {
  14. rec()
  15. }
  16. });
  17. })
  18. }
  19. // 复制文本
  20. function copy(txt,msg="内容已复制") {
  21. uni.setClipboardData({
  22. data: txt,
  23. success: function () {
  24. uni.showToast({
  25. title: '',
  26. duration: 2000
  27. });
  28. }
  29. });
  30. }
  31. // 页面后退方法
  32. vue.prototype.$back = (num = 1) => {
  33. uni.navigateBack(num)
  34. navFontColor()
  35. }
  36. // 标签过滤
  37. function filterCode(str) {
  38. return str.replace(/<[^<>]+>/g, '').replace(/&nbsp;/ig, '')
  39. }
  40. // 解决uni-app slot 数组传递bug
  41. vue.prototype.$list = function (obj) {
  42. return Object.entries(obj).filter(item => item[0] != '_i').map(item => item[1])
  43. }
  44. // 替换路由
  45. vue.prototype._router = {
  46. push(path) {
  47. var url = '', query, animationType, animationDuration;
  48. if (typeof path == 'string') {
  49. url = path
  50. } else {
  51. url = path.path;
  52. query = qs.stringify(path.query);
  53. animationType = path.animationType
  54. animationDuration = path.animationDuration
  55. }
  56. uni.navigateTo({
  57. url: `${url}${url.includes('?') ? '&' : '?'}${query || ''}`,
  58. animationType,
  59. animationDuration
  60. });
  61. navFontColor()
  62. },
  63. replace(path) {
  64. var url = '', query, animationType, animationDuration;
  65. if (typeof path == 'string') {
  66. url = path
  67. } else {
  68. url = path.path || '';
  69. query = qs.stringify(path.query);
  70. animationType = path.animationType
  71. animationDuration = path.animationDuration
  72. }
  73. uni.redirectTo({
  74. url: `${url}${url.includes('?') ? '&' : '?'}${query || ''}`,
  75. animationType,
  76. animationDuration
  77. });
  78. navFontColor()
  79. }
  80. }
  81. function defaultTheme() {
  82. return `dark`
  83. // 获取当前时间
  84. let timeNow = new Date();
  85. // 获取当前小时
  86. let hours = timeNow.getHours();
  87. // 设置默认文字
  88. let state = ``;
  89. // 判断当前时间段
  90. if (hours >= 19 || hours <= 7) {
  91. state = `dark`;
  92. } else {
  93. state = `light`;
  94. }
  95. return state;
  96. }
  97. function navFontColor() {
  98. let theme = uni.getStorageSync('theme') || defaultTheme()
  99. setTimeout(() => {
  100. uni.setNavigationBarColor({
  101. frontColor: theme == 'light' ? '#000000' : '#ffffff',
  102. backgroundColor: '#cccccc',
  103. })
  104. }, 300)
  105. }
  106. function localImgUrl(name) {
  107. let theme = uni.getStorageSync('theme') || defaultTheme()
  108. let str = theme == 'light' ? 'static/img/light/' : 'static/img/'
  109. return str + name
  110. }
  111. vue.prototype.$imgUrl = app.imgUrl
  112. vue.prototype.$baseUrl = app.baseUrl
  113. vue.prototype.$getFile = getFile
  114. vue.prototype.$copy = copy
  115. vue.prototype.$filterCode = filterCode
  116. vue.prototype.$navFontColor = navFontColor
  117. vue.prototype.$localImgUrl = localImgUrl