index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <div class="langConfig">
  3. <div class="content">
  4. <byTable
  5. :source="sourceList.data"
  6. :pagination="sourceList.pagination"
  7. :config="config"
  8. :loading="loading"
  9. highlight-current-row
  10. :selectConfig="selectConfig"
  11. searchKey="userName"
  12. :table-events="{
  13. //element talbe事件都能传
  14. select: select,
  15. }"
  16. :action-list="[
  17. {
  18. text: '同步其他语种配置',
  19. action: () => openModal('add'),
  20. disabled: !sourceList.pagination.tenantId,
  21. },
  22. ]"
  23. @get-list="getList"
  24. >
  25. <template #slotName="{ item }">
  26. {{ item.createTime }}
  27. </template>
  28. </byTable>
  29. </div>
  30. <el-dialog
  31. :title="modalType == 'add' ? '修改语言配置' : '修改语言配置'"
  32. v-if="dialogVisible"
  33. v-model="dialogVisible"
  34. width="500"
  35. v-loading="loading"
  36. >
  37. <byForm
  38. :formConfig="formConfig"
  39. :formOption="formOption"
  40. v-model="formData.data"
  41. :rules="rules"
  42. ref="byform"
  43. >
  44. </byForm>
  45. <template #footer>
  46. <el-button @click="dialogVisible = false" size="large"
  47. >取 消</el-button
  48. >
  49. <el-button
  50. type="primary"
  51. @click="submitForm('byform')"
  52. size="large"
  53. :loading="submitLoading"
  54. >确 定</el-button
  55. >
  56. </template>
  57. </el-dialog>
  58. </div>
  59. </template>
  60. <script setup>
  61. /* eslint-disable vue/no-unused-components */
  62. import { ElMessage, ElMessageBox } from 'element-plus'
  63. import byTable from '@/components/byTable/index'
  64. import byForm from '@/components/byForm/index'
  65. import treeList from '@/components/treeList/index'
  66. import { computed, defineComponent, ref } from 'vue'
  67. const loading = ref(false)
  68. const submitLoading = ref(false)
  69. const sourceList = ref({
  70. data: [],
  71. pagination: {
  72. total: 3,
  73. pageNum: 1,
  74. pageSize: 10,
  75. type:'web'
  76. },
  77. })
  78. let dialogVisible = ref(false)
  79. let modalType = ref('add')
  80. const validatePass = (rule, value, callback) => {
  81. if (!formData.data.password && modalType.value == 'add') {
  82. callback(new Error('请输入账号和密码'))
  83. } else {
  84. callback()
  85. }
  86. }
  87. let rules = ref({
  88. })
  89. const userId = ref('')
  90. const { proxy } = getCurrentInstance()
  91. const password = ref('')
  92. const selectConfig = computed(() => {
  93. return [
  94. {
  95. label: "选择系统",
  96. prop: "type",
  97. data: [
  98. {
  99. label:'web',
  100. id:'web'
  101. },
  102. {
  103. label:'app',
  104. id:'app'
  105. }
  106. ],
  107. },
  108. ]
  109. })
  110. const config = computed(() => {
  111. return [
  112. {
  113. attrs: {
  114. label: '中文名称',
  115. prop: 'value',
  116. },
  117. },
  118. {
  119. attrs: {
  120. label: '英文名称',
  121. prop: 'enText',
  122. },
  123. },
  124. {
  125. attrs: {
  126. label: '对象索引',
  127. prop: 'key',
  128. align: 'left',
  129. },
  130. },
  131. {
  132. attrs: {
  133. label: '操作',
  134. width: '200',
  135. align: 'right',
  136. },
  137. // 渲染 el-button,一般用在最后一列。
  138. renderHTML(row) {
  139. return [
  140. {
  141. attrs: {
  142. label: '修改',
  143. type: 'primary',
  144. text: true,
  145. },
  146. el: 'button',
  147. click() {
  148. getDtl(row)
  149. },
  150. },
  151. ]
  152. },
  153. },
  154. ]
  155. })
  156. let formData = reactive({
  157. data: {},
  158. })
  159. const formOption = reactive({
  160. inline: true,
  161. labelWidth: 100,
  162. itemWidth: 100,
  163. rules: [],
  164. })
  165. const byform = ref(null)
  166. const treeListData = ref([])
  167. const formConfig = computed(() => {
  168. return [
  169. {
  170. type: 'input',
  171. prop: 'value',
  172. label: '中文名称',
  173. required: true,
  174. itemType: 'text',
  175. disabled:true,
  176. },
  177. {
  178. type: 'input',
  179. prop: 'enText',
  180. label: '英文名称',
  181. required: true,
  182. itemType: 'text',
  183. },
  184. ]
  185. })
  186. //根据resurl获取数据
  187. const getFormat = (formatStr, props) => {
  188. if (!formatStr) return props;
  189. return formatStr
  190. .split(".")
  191. .reduce((total, cur) => (!total ? "" : total[cur]), props);
  192. };
  193. const submitData = ref('')
  194. const getList = async (req) => {
  195. sourceList.value.pagination = { ...sourceList.value.pagination, ...req }
  196. console.log(sourceList.value.pagination.type)
  197. loading.value = false
  198. proxy
  199. .get('/open/multilingual/getJson', {})
  200. .then((message) => {
  201. submitData.value = JSON.parse(message.data)
  202. const res = JSON.parse(message.data)
  203. // proxy.post('/open/multilingual/setJson',{configValue:JSON.stringify(res)}).then((message) => {
  204. // })
  205. console.log(res[sourceList.value.pagination.type],12312312)
  206. const tableData = getAllKeys(res.app.cn)
  207. console.log(tableData)
  208. //根据key获取res里其他语言的值
  209. for (let i = 0; i < tableData.length; i++) {
  210. const element = tableData[i];
  211. element.enText = getFormat(element.key,res.app.en)
  212. }
  213. sourceList.value.data = tableData
  214. })
  215. }
  216. //递归对象所有对象,将不是对象的属性放入数组,数组为对象类型,记录所有父级的key以数组方式存储
  217. const getAllKeys = (obj) => {
  218. let result = []
  219. const getKeys = (obj, parentKey) => {
  220. Object.keys(obj).forEach((key) => {
  221. if (typeof obj[key] === 'object') {
  222. getKeys(obj[key], parentKey ? `${parentKey}.${key}` : key)
  223. } else {
  224. result.push({
  225. key: parentKey ? `${parentKey}.${key}` : key,
  226. value: obj[key],
  227. })
  228. }
  229. })
  230. }
  231. getKeys(obj)
  232. return result
  233. }
  234. const treeChange = (e) => {
  235. console.log(e)
  236. sourceList.value.pagination.tenantId = e.id
  237. getList({ tenantId: e.id })
  238. getUserList()
  239. getDept()
  240. }
  241. const openModal = () => {
  242. dialogVisible.value = true
  243. modalType.value = 'add'
  244. formData.data = {
  245. userType: 1,
  246. }
  247. }
  248. const tree = ref(null)
  249. const submitForm = () => {
  250. byform.value.handleSubmit((valid) => {
  251. console.log(formData.data)
  252. //根据key修改submitData里的值
  253. const key = formData.data.key
  254. const value = formData.data.value
  255. const enText = formData.data.enText
  256. const keyArr = key.split('.')
  257. let obj = submitData.value.app.en
  258. for (let i = 0; i < keyArr.length; i++) {
  259. const element = keyArr[i]
  260. if (i == keyArr.length - 1) {
  261. obj[element] = enText
  262. } else {
  263. obj = obj[element]
  264. }
  265. }
  266. proxy.post('/open/multilingual/setJson',{configValue:JSON.stringify(submitData.value)}).then((message) => {
  267. console.log(message)
  268. dialogVisible.value = false
  269. getList()
  270. })
  271. })
  272. }
  273. const getDtl = (row) => {
  274. formData.data = { ...row}
  275. modalType.value = 'edit'
  276. dialogVisible.value = true
  277. }
  278. getList()
  279. </script>
  280. <style lang="scss" scoped>
  281. .langConfig {
  282. padding: 20px;
  283. display: flex;
  284. justify-content: space-between;
  285. .content {
  286. width: 100%;
  287. }
  288. }
  289. </style>