global.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. 注册全局方法
  3. */
  4. import Vue from 'vue'
  5. import axios from 'axios'
  6. import ClipboardJS from 'clipboard'
  7. // 创建axios实例
  8. const client = axios.create({
  9. baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
  10. timeout: 60000 // 请求超时时间,60秒
  11. })
  12. Object.assign(Vue.prototype, {
  13. /**
  14. * 请求接口
  15. * @param uri uri
  16. * @param data 请求数据
  17. * @param callback 成功时回调
  18. * @param errorCallback 错误时回调
  19. */
  20. post: function(uri, data, callback, errorCallback) {
  21. const that = this
  22. client.post(uri, data).then(function(response) {
  23. const resp = response.data
  24. const code = resp.code
  25. if (code === '0') { // 成功
  26. callback && callback.call(that, resp)
  27. } else {
  28. that.$message.error(resp.msg)
  29. }
  30. }).catch(function(error) {
  31. console.error(error)
  32. errorCallback && errorCallback(error)
  33. that.$message.error(error.message)
  34. })
  35. },
  36. /**
  37. * tip,使用方式:this.tip('操作成功'),this.tip('错误', 'error')
  38. * @param msg 内容
  39. * @param type success / info / warning / error
  40. * @param stay 停留几秒,默认3秒
  41. */
  42. tip: function(msg, type, stay) {
  43. stay = parseInt(stay) || 3
  44. this.$message({
  45. message: msg,
  46. type: type || 'success',
  47. duration: stay * 1000
  48. })
  49. },
  50. /**
  51. * 提醒框
  52. * @param msg 消息
  53. * @param okHandler 成功回调
  54. * @param cancelHandler
  55. */
  56. confirm: function(msg, okHandler, cancelHandler) {
  57. const that = this
  58. this.$confirm(msg, '提示', {
  59. confirmButtonText: '确定',
  60. cancelButtonText: '取消',
  61. type: 'warning',
  62. beforeClose: (action, instance, done) => {
  63. if (action === 'confirm') {
  64. okHandler.call(that, done)
  65. } else if (action === 'cancel') {
  66. if (cancelHandler) {
  67. cancelHandler.call(that, done)
  68. } else {
  69. done()
  70. }
  71. } else {
  72. done()
  73. }
  74. }
  75. }).catch(function() {})
  76. },
  77. handleCommand: function(command) {
  78. command()
  79. },
  80. /**
  81. * 重置表单
  82. * @param formName 表单元素的ref
  83. */
  84. resetForm(formName) {
  85. const frm = this.$refs[formName]
  86. frm && frm.resetFields()
  87. },
  88. setAttr: function(key, val) {
  89. if (val === undefined) {
  90. val = ''
  91. }
  92. localStorage.setItem(key, val + '')
  93. },
  94. getAttr: function(key) {
  95. return localStorage.getItem(key)
  96. },
  97. logout: function() {
  98. const fullPath = this.$route.fullPath
  99. if (fullPath.indexOf('login?redirect') === -1) {
  100. this.$router.push({ path: `/login?redirect=${fullPath}` })
  101. }
  102. },
  103. goRoute: function(path) {
  104. this.$router.push({ path: path })
  105. },
  106. cellStyleSmall: function() {
  107. return { padding: '5px 0' }
  108. },
  109. headCellStyleSmall: function() {
  110. return { padding: '5px 0' }
  111. },
  112. initCopy: function() {
  113. const _this = this
  114. const clipboard = new ClipboardJS('.copyBtn')
  115. clipboard.on('success', function() {
  116. _this.tip('复制成功')
  117. })
  118. this.clipboard = clipboard
  119. },
  120. cleanCopy: function() {
  121. if (this.clipboard) {
  122. this.clipboard.destroy()
  123. }
  124. },
  125. /**
  126. * 文件必须放在public下面
  127. * @param path 相对于public文件夹路径,如文件在public/static/sign.md,填:static/sign.md
  128. * @param callback 回调函数,函数参数是文件内容
  129. */
  130. getFile: function(path, callback) {
  131. axios.get(path)
  132. .then(function(response) {
  133. callback.call(this, response.data)
  134. })
  135. },
  136. downloadText(filename, text) {
  137. const element = document.createElement('a')
  138. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text))
  139. element.setAttribute('download', filename)
  140. element.style.display = 'none'
  141. document.body.appendChild(element)
  142. element.click()
  143. document.body.removeChild(element)
  144. }
  145. })