global.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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,如:goods.get,goods.get/1.0
  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('err' + error) // for debug
  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. /**
  78. * 重置表单
  79. * @param formName 表单元素的ref
  80. */
  81. resetForm(formName) {
  82. const frm = this.$refs[formName]
  83. frm && frm.resetFields()
  84. },
  85. logout: function() {
  86. const fullPath = this.$route.fullPath
  87. if (fullPath.indexOf('login?redirect') === -1) {
  88. this.$router.push({ path: `/login?redirect=${fullPath}` })
  89. }
  90. },
  91. goRoute: function(path) {
  92. this.$router.push({ path: path })
  93. },
  94. cellStyleSmall: function() {
  95. return { padding: '5px 0' }
  96. },
  97. headCellStyleSmall: function() {
  98. return { padding: '5px 0' }
  99. },
  100. initCopy: function() {
  101. const _this = this
  102. const clipboard = new ClipboardJS('.copyBtn')
  103. clipboard.on('success', function() {
  104. _this.tip('复制成功')
  105. })
  106. this.clipboard = clipboard
  107. },
  108. cleanCopy: function() {
  109. if (this.clipboard) {
  110. this.clipboard.destroy()
  111. }
  112. },
  113. downloadText(filename, text) {
  114. const element = document.createElement('a')
  115. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text))
  116. element.setAttribute('download', filename)
  117. element.style.display = 'none'
  118. document.body.appendChild(element)
  119. element.click()
  120. document.body.removeChild(element);
  121. }
  122. })