operation_mixin.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. function setBtnName(name, row) {
  2. if (!name) {
  3. return false
  4. }
  5. if (typeof name == 'function') {
  6. return name(row)
  7. } else if (typeof name == 'string') {
  8. return name
  9. }
  10. }
  11. function hasCommonElements(arr1, arr2) {
  12. for (let i = 0; i < arr1.length; i++) {
  13. if (arr2.includes(arr1[i])) {
  14. return true;
  15. }
  16. }
  17. return false;
  18. }
  19. export default {
  20. data(){
  21. return {
  22. pageType:this?.$route?.params?.pageType,
  23. pageCode:this?.$route?.params?.pageCode,
  24. pagePam:this?.$route?.params?.pagePam
  25. }
  26. },
  27. methods: {
  28. optionsEvensAuth(key, obj) {
  29. var roleItems = this.$route.meta.roleItems
  30. if (!roleItems || !roleItems.length) {
  31. return { isRole: false }
  32. }
  33. if (key instanceof Array) {
  34. if (!hasCommonElements(roleItems.map(item => item.code), key)) {
  35. return { isRole: false }
  36. } else {
  37. return typeof obj == 'function' ? obj(roleItems) : obj
  38. }
  39. } else {
  40. var role = roleItems?.find(item => item.code === key)
  41. if (!role) {
  42. return { isRole: false }
  43. } else {
  44. return typeof obj == 'function' ? obj(role) : { name: role.moduleName, ...obj }
  45. }
  46. }
  47. },
  48. operationBtn(opt = {}) {
  49. var roleItems = this.$route.meta.roleItems
  50. return (...p) => {
  51. if (!roleItems || !roleItems.length) {
  52. return null
  53. }
  54. return (
  55. <div class="operation-btns">
  56. {Object.keys(opt)
  57. .filter(key => (opt[key]?.click ? true : false))
  58. .map(key => {
  59. return (h, { row, index, column }) => {
  60. var role = roleItems?.find(item => item.code === key)
  61. if (opt[key]?.prompt) {
  62. return role && (opt[key]?.conditions ? opt[key]?.conditions({ row, index, column }) : true) ? (
  63. <el-popconfirm
  64. title={setBtnName(opt[key]?.prompt, { row, index, column })}
  65. onConfirm={() => {
  66. opt[key]?.click({ row, index, column })
  67. }}
  68. >
  69. <el-button size="mini" type={opt[key]?.btnType || 'text'} slot="reference">
  70. {setBtnName(opt[key]?.name, { row, index, column }) || role.moduleName}
  71. </el-button>
  72. </el-popconfirm>
  73. ) : null
  74. } else {
  75. return role && (opt[key]?.conditions ? opt[key]?.conditions({ row, index, column }) : true) ? (
  76. <el-button
  77. size="mini"
  78. type={opt[key]?.btnType || 'text'}
  79. onClick={() => {
  80. opt[key]?.click({ row, index, column })
  81. }}
  82. >
  83. {setBtnName(opt[key]?.name, { row, index, column }) || role.moduleName}
  84. </el-button>
  85. ) : null
  86. }
  87. }
  88. })
  89. .map(fun => fun(...p))}
  90. </div>
  91. )
  92. }
  93. }
  94. }
  95. }