You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
9.0 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
2 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
2 months ago
2 months ago
4 months ago
2 months ago
2 months ago
4 months ago
2 months ago
2 months ago
4 months ago
4 months ago
3 months ago
4 months ago
2 months ago
4 months ago
2 months ago
4 months ago
4 months ago
  1. <script setup>
  2. import { onMounted, reactive, ref } from 'vue'
  3. import { ElMessage, ElMessageBox } from 'element-plus'
  4. import request from '@/util/http'
  5. import { useAdminStore } from "@/store/index.js";
  6. import { storeToRefs } from "pinia";
  7. import { findMenuById, permissionMapping } from "@/utils/menuTreePermission.js"
  8. const adminStore = useAdminStore()
  9. const { adminData, menuTree } = storeToRefs(adminStore)
  10. const regeEdit = ref(false)
  11. const editFormRef = ref(null)
  12. const tableData = ref([])
  13. //搜索对象
  14. const getObj = ref({
  15. pageNum: 1,
  16. pageSize: 10
  17. })
  18. const total = ref(0)
  19. const rateEdit = ref({
  20. id: null,
  21. rateName: '',
  22. num: null,
  23. adminId: null,
  24. updateTime: Date.now(),
  25. })
  26. //货币条目
  27. const rateNames = [
  28. {
  29. value: 'USD',
  30. label: 'USD'
  31. },
  32. {
  33. value: 'HKD',
  34. label: 'HKD'
  35. },
  36. {
  37. value: 'THB',
  38. label: 'THB'
  39. },
  40. {
  41. value: 'VND',
  42. label: 'VND'
  43. },
  44. {
  45. value: 'CAD',
  46. label: 'CAD'
  47. },
  48. {
  49. value: 'MYR',
  50. label: 'MYR'
  51. },
  52. {
  53. value: 'KRW',
  54. label: 'KRW'
  55. },
  56. {
  57. value: 'JPY',
  58. label: 'JPY'
  59. },
  60. {
  61. value: 'CNY',
  62. label: 'CNY'
  63. }
  64. ]
  65. // 汇率校验
  66. const checkFreeGoldRadio = function (rule, value, callback) {
  67. if (value == '0' || value == null || value == '') {
  68. callback(new Error('请输入汇率比'))
  69. } else if (value < 0 || isNaN(value)) {
  70. callback(new Error('请输入正确的格式'))
  71. } else {
  72. callback()
  73. }
  74. }
  75. // 定义表单验证规则
  76. const rules = reactive({
  77. num: [{ validator: checkFreeGoldRadio, trigger: 'blur' }],
  78. })
  79. // 表单大小
  80. const formSize = ref('default')
  81. const getAllRate = async function (val) {
  82. try {
  83. const result = await request({
  84. url: '/rate/selectAll',
  85. method: 'POST',
  86. data: {
  87. pageNum: getObj.value.pageNum,
  88. pageSize: getObj.value.pageSize,
  89. }
  90. })
  91. console.log('这是汇率列表 请求成功', result)
  92. tableData.value = result.data.list
  93. total.value = result.data.total
  94. } catch (error) {
  95. console.log('请求失败', error);
  96. ElMessage.error('请求失败');
  97. }
  98. }
  99. const handlePageSizeChange = function (val) {
  100. getObj.value.pageSize = val
  101. getAllRate()
  102. }
  103. const handleCurrentChange = function (val) {
  104. getObj.value.pageNum = val
  105. getAllRate()
  106. }
  107. const getEditData = async function (row) {
  108. try {
  109. console.log('搜索参数', getObj.value)
  110. const result = await request({
  111. url: '/rate/selectById',
  112. data: { id: row.id }
  113. })
  114. console.log('根据id查 请求成功', result)
  115. rateEdit.value.id = row.id
  116. rateEdit.value.rateName = row.rateName
  117. rateEdit.value.num = row.num
  118. console.log('根据id获取的数据', rateEdit.value)
  119. rateEdit.value.adminId = adminData.value.id
  120. } catch (error) {
  121. console.log('请求失败', error)
  122. }
  123. }
  124. const editRole = ref(true)
  125. editRole.value = findMenuById(menuTree.value, permissionMapping.Exchange_Rate_Modification)
  126. // 编辑汇率
  127. const editRate = async function () {
  128. if (findMenuById(menuTree.value, permissionMapping.Exchange_Rate_Modification)) {
  129. // 提交前验证 汇率是否为数字
  130. rateEdit.value.num = parseFloat(rateEdit.value.num);
  131. try {
  132. console.log('搜索参数', rateEdit.value)
  133. const result = await request({
  134. url: '/rate/update',
  135. data: rateEdit.value
  136. })
  137. console.log('请求成功', result)
  138. await getAllRate()
  139. } catch (error) {
  140. console.log('请求失败', error)
  141. }
  142. } else {
  143. ElMessage.error('没有权限')
  144. }
  145. }
  146. // 添加前验证
  147. const edit = () => {
  148. editFormRef.value.validate(async (valid) => {
  149. if (valid) {
  150. try {
  151. await ElMessageBox.confirm("确认修改?");
  152. await editRate();
  153. console.log("修改成功");
  154. regeEdit.value = false;
  155. } catch (error) {
  156. console.log("取消修改", error);
  157. regeEdit.value = false;
  158. }
  159. } else {
  160. ElMessage({
  161. type: "error",
  162. message: "请检查输入内容",
  163. })
  164. }
  165. })
  166. }
  167. const cancelEdit = () => {
  168. regeEdit.value = false
  169. }
  170. const handleEditDialogClose = () => {
  171. if (editFormRef.value) {
  172. getAllRate()
  173. }
  174. }
  175. // 日期格式化
  176. function formatDate(value) {
  177. if (!value) return ''
  178. const date = new Date(value)
  179. const year = date.getFullYear()
  180. const month = (date.getMonth() + 1).toString().padStart(2, '0')
  181. const day = date.getDate().toString().padStart(2, '0')
  182. const hours = date.getHours().toString().padStart(2, '0')
  183. const minutes = date.getMinutes().toString().padStart(2, '0')
  184. const seconds = date.getSeconds().toString().padStart(2, '0')
  185. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  186. }
  187. // 输入框过滤
  188. function handleInput(value) {
  189. // 检查是否使用了中文句号
  190. if (value.includes('。') || /[^\d.]/g.test(value)) {
  191. ElMessage.warning('请输入正确的符号');
  192. // value = value.replace('。', '.');
  193. }
  194. // 处理多个小数点的情况,保留第一个,移除后续的
  195. const parts = value.split('.');
  196. if (parts.length > 2) {
  197. value = parts[0] + '.' + parts.slice(1).join('');
  198. ElMessage.warning('只能包含一个小数点');
  199. }
  200. // 禁止输入负数
  201. if (value.startsWith('-')) {
  202. ElMessage.warning('不允许输入负数');
  203. value = value.substring(1);
  204. }
  205. // 最多两位小数,超过时才显示提示
  206. if (value.includes('.')) {
  207. const parts = value.split('.')
  208. // 限制整数部分最多六位
  209. if (parts[0].length > 6) {
  210. parts[0] = parts[0].slice(0, 6)
  211. ElMessage.info('整数部分最多允许六位')
  212. }
  213. // 限制小数部分最多两位
  214. if (parts[1].length > 2) {
  215. parts[1] = parts[1].slice(0, 2)
  216. value = parts[0] + '.' + parts[1]
  217. ElMessage.info('最多允许两位小数')
  218. } else {
  219. value = parts[0] + '.' + parts[1]
  220. }
  221. } else {
  222. // 纯整数时限制最多六位
  223. if (value.length > 6) {
  224. value = value.slice(0, 6)
  225. ElMessage.info('整数部分最多允许六位')
  226. }
  227. }
  228. // 小数点前没有数字时补0
  229. if (value.startsWith('.')) {
  230. value = '0' + value;
  231. // 需求没有,注释,先不显示
  232. // ElMessage.info('已自动补充前导0');
  233. }
  234. // 更新表单值
  235. rateEdit.value.num = value
  236. return value
  237. }
  238. onMounted(async function () {
  239. await getAllRate()
  240. })
  241. </script>
  242. <template>
  243. <el-card style="width:82vw;height:85vh">
  244. <el-table :data="tableData" v-if="(tableData.flag = 1)">
  245. <el-table-column type="index" label="序号" width="100px" fixed="left">
  246. <template #default="scope">
  247. <span>{{
  248. scope.$index + 1 + (getObj.pageNum - 1) * getObj.pageSize
  249. }}</span>
  250. </template>
  251. </el-table-column>
  252. <el-table-column prop="rateName" label="货币名称" :span="2" />
  253. <el-table-column prop="num" label="汇率" :span="2">
  254. <template #default="scope">
  255. <p>
  256. {{ scope.row.num }} 1
  257. </p>
  258. </template>
  259. </el-table-column>
  260. <el-table-column prop="updateTime" label="添加时间" :span="3">
  261. <template #default="scope">
  262. <span>{{ formatDate(scope.row.updateTime) }}</span>
  263. </template>
  264. </el-table-column>
  265. <el-table-column label="操作" :span="3" v-if="editRole">
  266. <template #default="scope">
  267. <el-button type="text" @click="() => {
  268. regeEdit = true
  269. getEditData(scope.row)
  270. }">编辑
  271. </el-button>
  272. </template>
  273. </el-table-column>
  274. </el-table>
  275. <!-- 分页 -->
  276. <div class="pagination">
  277. <el-pagination background :page-size="getObj.pageSize" :page-sizes="[5, 10, 20, 50, 100]"
  278. layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handlePageSizeChange"
  279. @current-change="handleCurrentChange"></el-pagination>
  280. </div>
  281. </el-card>
  282. <!-- 这是编辑弹窗 -->
  283. <el-dialog align-center v-model="regeEdit" title="修改汇率" width="30vw" :close-on-click-modal="false"
  284. @close="handleEditDialogClose">
  285. <el-form ref="editFormRef" :model="rateEdit" :rules="rules" label-width="auto" class="edit-form" :size="formSize"
  286. status-icon>
  287. <el-form-item prop="rateName" label="货币名称:">
  288. <el-input v-model="rateEdit.rateName" disabled style="width: 10vw" />
  289. </el-form-item>
  290. <el-form-item prop="num" label="汇率:">
  291. <el-input v-model="rateEdit.num" @update:modelValue="handleInput" style="width: 120px" />
  292. <span class="unit">:1</span>
  293. <span class="rate-tip">
  294. (提示当前规则每
  295. <span>{{ rateEdit.num }}</span>
  296. <span>{{ rateEdit.rateName }}</span>可兑换 1 新币)
  297. </span>
  298. </el-form-item>
  299. </el-form>
  300. <div class="dialog-footer">
  301. <el-button type="primary" @click="edit">修改</el-button>
  302. <el-button @click="cancelEdit">取消</el-button>
  303. </div>
  304. </el-dialog>
  305. </template>
  306. <style scoped>
  307. .pagination {
  308. margin-top: 20px;
  309. display: flex;
  310. }
  311. .edit-form {
  312. width: 35vw;
  313. height: 13vh;
  314. }
  315. .dialog-footer {
  316. display: flex;
  317. margin-left: 5vw;
  318. }
  319. .unit {
  320. margin-left: 0.5vw;
  321. }
  322. .rate-tip {
  323. hyphens: auto;
  324. }
  325. </style>