index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { reactive, ref } from 'vue'
  2. import { create as admin_create, list as admin_list, remove as admin_remove, update as admin_update } from '@/api/tag'
  3. import { create as my_create, list as my_list, remove as my_remove, update as my_update } from '@/api/my/tag'
  4. import { ElMessage, ElMessageBox } from 'element-plus'
  5. import { useRoute } from 'vue-router'
  6. import { T } from '@/utils/i18n'
  7. import { useRepositories as useCollectionRepositories } from '@/views/address_book/collection'
  8. const apis = {
  9. admin: { list: admin_list, remove: admin_remove, update: admin_update, create: admin_create },
  10. my: { list: my_list, remove: my_remove, create: my_create, update: my_update },
  11. }
  12. export function useRepositories (api_type = 'my') {
  13. //获取query
  14. const route = useRoute()
  15. const user_id = route.query?.user_id
  16. const listRes = reactive({
  17. list: [], total: 0, loading: false,
  18. })
  19. const listQuery = reactive({
  20. page: 1,
  21. page_size: 10,
  22. user_id: user_id ? parseInt(user_id) : null,
  23. collection_id: null,
  24. })
  25. const flutterColor2rgba = (color) => {
  26. // color 是十进制的数字,先转成16进制
  27. let hex = color.toString(16)
  28. console.log('hex', hex)
  29. if (hex.length < 8) {
  30. //前面补0
  31. hex = '0'.repeat(8 - hex.length) + hex
  32. }
  33. //前两位是透明度
  34. let alpha = hex.slice(0, 2)
  35. //后六位是颜色
  36. let rgba = hex.slice(2)
  37. return `rgba(${parseInt(rgba.slice(0, 2), 16)}, ${parseInt(rgba.slice(2, 4), 16)}, ${parseInt(rgba.slice(4, 6), 16)}, ${parseInt(alpha, 16) / 255})`
  38. }
  39. const rgba2flutterColor = (color) => {
  40. console.log('color', color)
  41. //rgba(133, 33, 33, 0.81)
  42. let rgba = color.match(/rgba\((\d+),\s*(\d+),\s*(\d+),\s*(\d+(\.\d+)?)\)/)
  43. console.log('rgba', rgba)
  44. let alpha = Math.round(parseFloat(rgba[4]) * 255).toString(16)
  45. let r = parseInt(rgba[1]).toString(16)
  46. let g = parseInt(rgba[2]).toString(16)
  47. let b = parseInt(rgba[3]).toString(16)
  48. //如果是1位要补位
  49. if (alpha.length === 1) {
  50. alpha = '0' + alpha
  51. }
  52. if (r.length === 1) {
  53. r = '0' + r
  54. }
  55. if (g.length === 1) {
  56. g = '0' + g
  57. }
  58. if (b.length === 1) {
  59. b = '0' + b
  60. }
  61. console.log('to f color', alpha + r + g + b, parseInt(alpha + r + g + b, 16))
  62. return parseInt(alpha + r + g + b, 16)
  63. }
  64. const getList = async () => {
  65. listRes.loading = true
  66. const res = await apis[api_type].list(listQuery).catch(_ => false)
  67. listRes.loading = false
  68. if (res) {
  69. listRes.list = res.data.list.map(item => {
  70. item.color = flutterColor2rgba(item.color)
  71. return item
  72. })
  73. listRes.total = res.data.total
  74. }
  75. }
  76. const handlerQuery = () => {
  77. if (listQuery.page === 1) {
  78. getList()
  79. } else {
  80. listQuery.page = 1
  81. }
  82. }
  83. const del = async (row) => {
  84. const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('Delete') }), {
  85. confirmButtonText: T('Confirm'),
  86. cancelButtonText: T('Cancel'),
  87. type: 'warning',
  88. }).catch(_ => false)
  89. if (!cf) {
  90. return false
  91. }
  92. const res = await apis[api_type].remove({ id: row.id }).catch(_ => false)
  93. if (res) {
  94. ElMessage.success(T('OperationSuccess'))
  95. getList()
  96. }
  97. }
  98. const formVisible = ref(false)
  99. const formData = reactive({
  100. id: 0,
  101. name: '',
  102. color: 0,
  103. user_id: null,
  104. collection_id: null,
  105. })
  106. const currentColor = ref('')
  107. const activeChange = (c) => {
  108. currentColor.value = c
  109. }
  110. const toEdit = (row) => {
  111. console.log('row', row)
  112. formVisible.value = true
  113. formData.id = row.id
  114. formData.name = row.name
  115. formData.color = row.color
  116. formData.user_id = row.user_id
  117. formData.collection_id = row.collection_id
  118. collectionListQuery.user_id = row.user_id
  119. getCollectionList()
  120. }
  121. const toAdd = () => {
  122. formVisible.value = true
  123. formData.id = 0
  124. formData.name = ''
  125. formData.color = ''
  126. formData.user_id = null
  127. formData.collection_id = null
  128. }
  129. const submit = async () => {
  130. console.log(formData)
  131. if (!formData.color) {
  132. ElMessage.error('请选择颜色')
  133. return
  134. }
  135. const api = formData.id ? apis[api_type].update : apis[api_type].create
  136. const data = {
  137. ...formData,
  138. color: rgba2flutterColor(formData.color),
  139. }
  140. console.log(data)
  141. const res = await api(data).catch(_ => false)
  142. if (res) {
  143. ElMessage.success(T('OperationSuccess'))
  144. formVisible.value = false
  145. getList()
  146. }
  147. }
  148. //query form collection
  149. const {
  150. listRes: collectionListRes,
  151. listQuery: collectionListQuery,
  152. getList: getCollectionList,
  153. } = useCollectionRepositories(api_type)
  154. collectionListQuery.page_size = 9999
  155. const changeUser = async (val) => {
  156. formData.collection_id = 0
  157. if (!val) {
  158. collectionListRes.list = []
  159. } else {
  160. collectionListQuery.user_id = val
  161. getCollectionList()
  162. }
  163. }
  164. const {
  165. listRes: collectionListResForUpdate,
  166. listQuery: collectionListQueryForUpdate,
  167. getList: getCollectionListForUpdate,
  168. } = useCollectionRepositories(api_type)
  169. collectionListQueryForUpdate.page_size = 9999
  170. //create or update form collection
  171. const changeUserForUpdate = async (val) => {
  172. listQuery.collection_id = null
  173. if (!val) {
  174. collectionListRes.list = []
  175. } else {
  176. collectionListQuery.user_id = val
  177. getCollectionListForUpdate()
  178. }
  179. }
  180. return {
  181. listRes,
  182. listQuery,
  183. getList,
  184. handlerQuery,
  185. del,
  186. formVisible,
  187. formData,
  188. toEdit,
  189. toAdd,
  190. submit,
  191. activeChange,
  192. currentColor,
  193. collectionListRes,
  194. changeUser,
  195. getCollectionList,
  196. collectionListResForUpdate,
  197. changeUserForUpdate,
  198. getCollectionListForUpdate,
  199. }
  200. }