index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { onActivated, onMounted, reactive, ref, watch } from 'vue'
  2. import { create, list, remove, update } from '@/api/tag'
  3. import { ElMessage, ElMessageBox } from 'element-plus'
  4. import { useRoute } from 'vue-router'
  5. import { T } from '@/utils/i18n'
  6. export function useRepositories () {
  7. //获取query
  8. const route = useRoute()
  9. const user_id = route.query?.user_id
  10. const listRes = reactive({
  11. list: [], total: 0, loading: false,
  12. })
  13. const listQuery = reactive({
  14. page: 1,
  15. page_size: 10,
  16. is_my: 0,
  17. user_id: user_id ? parseInt(user_id) : null,
  18. })
  19. const flutterColor2rgba = (color) => {
  20. // color 是十进制的数字,先转成16进制
  21. let hex = color.toString(16)
  22. console.log('hex', hex)
  23. if (hex.length < 8) {
  24. //前面补0
  25. hex = '0'.repeat(8 - hex.length) + hex
  26. }
  27. //前两位是透明度
  28. let alpha = hex.slice(0, 2)
  29. //后六位是颜色
  30. let rgba = hex.slice(2)
  31. return `rgba(${parseInt(rgba.slice(0, 2), 16)}, ${parseInt(rgba.slice(2, 4), 16)}, ${parseInt(rgba.slice(4, 6), 16)}, ${parseInt(alpha, 16) / 255})`
  32. }
  33. const rgba2flutterColor = (color) => {
  34. console.log('color', color)
  35. //rgba(133, 33, 33, 0.81)
  36. let rgba = color.match(/rgba\((\d+),\s*(\d+),\s*(\d+),\s*(\d+(\.\d+)?)\)/)
  37. console.log('rgba', rgba)
  38. let alpha = Math.round(parseFloat(rgba[4]) * 255).toString(16)
  39. let r = parseInt(rgba[1]).toString(16)
  40. let g = parseInt(rgba[2]).toString(16)
  41. let b = parseInt(rgba[3]).toString(16)
  42. //如果是1位要补位
  43. if (alpha.length === 1) {
  44. alpha = '0' + alpha
  45. }
  46. if (r.length === 1) {
  47. r = '0' + r
  48. }
  49. if (g.length === 1) {
  50. g = '0' + g
  51. }
  52. if (b.length === 1) {
  53. b = '0' + b
  54. }
  55. console.log('to f color', alpha + r + g + b, parseInt(alpha + r + g + b, 16))
  56. return parseInt(alpha + r + g + b, 16)
  57. }
  58. const getList = async () => {
  59. listRes.loading = true
  60. const res = await list(listQuery).catch(_ => false)
  61. listRes.loading = false
  62. if (res) {
  63. listRes.list = res.data.list.map(item => {
  64. item.color = flutterColor2rgba(item.color)
  65. return item
  66. })
  67. listRes.total = res.data.total
  68. }
  69. }
  70. const handlerQuery = () => {
  71. if (listQuery.page === 1) {
  72. getList()
  73. } else {
  74. listQuery.page = 1
  75. }
  76. }
  77. const del = async (row) => {
  78. const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('Delete') }), {
  79. confirmButtonText: T('Confirm'),
  80. cancelButtonText: T('Cancel'),
  81. type: 'warning',
  82. }).catch(_ => false)
  83. if (!cf) {
  84. return false
  85. }
  86. const res = await remove({ id: row.id }).catch(_ => false)
  87. if (res) {
  88. ElMessage.success(T('OperationSuccess'))
  89. getList()
  90. }
  91. }
  92. onMounted(getList)
  93. onActivated(getList)
  94. watch(() => listQuery.page, getList)
  95. watch(() => listQuery.page_size, handlerQuery)
  96. const formVisible = ref(false)
  97. const formData = reactive({
  98. id: 0,
  99. name: '',
  100. color: 0,
  101. user_id: 0,
  102. })
  103. const currentColor = ref('')
  104. const activeChange = (c) => {
  105. console.log(c)
  106. currentColor.value = c
  107. }
  108. const toEdit = (row) => {
  109. console.log('row', row)
  110. formVisible.value = true
  111. formData.id = row.id
  112. formData.name = row.name
  113. formData.color = row.color
  114. formData.user_id = row.user_id
  115. }
  116. const toAdd = () => {
  117. formVisible.value = true
  118. formData.id = 0
  119. formData.name = ''
  120. formData.color = 0
  121. formData.user_id = 0
  122. }
  123. const submit = async () => {
  124. console.log(formData)
  125. const api = formData.id ? update : create
  126. const data = {
  127. ...formData,
  128. color: rgba2flutterColor(formData.color),
  129. }
  130. console.log(data)
  131. const res = await api(data).catch(_ => false)
  132. if (res) {
  133. ElMessage.success(T('OperationSuccess'))
  134. formVisible.value = false
  135. getList()
  136. }
  137. }
  138. return {
  139. listRes,
  140. listQuery,
  141. getList,
  142. handlerQuery,
  143. del,
  144. formVisible,
  145. formData,
  146. toEdit,
  147. toAdd,
  148. submit,
  149. activeChange,
  150. currentColor,
  151. }
  152. }