edit-dialog.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <script setup lang="ts">
  2. import { FormRules, ElForm } from "element-plus";
  3. import { reactive, ref } from "vue";
  4. import {
  5. httpUpdate,
  6. httpAdd,
  7. httpDetail,
  8. httpRoleAll,
  9. httpCardAll
  10. } from "/@/api/interest/account";
  11. import { responseHandle } from "/@/utils/responseHandle";
  12. import { useNav } from "/@/layout/hooks/useNav";
  13. enum FROM_TYPE {
  14. create = "create",
  15. edit = "edit",
  16. view = "view"
  17. }
  18. const { logout } = useNav();
  19. const showModel = ref(false);
  20. const currentMenuId = ref("");
  21. const TYPE = ref<FROM_TYPE>(FROM_TYPE.create);
  22. const formRef = ref<InstanceType<typeof ElForm>>(null);
  23. const loading = ref(false);
  24. const titleType = ref("");
  25. const roleOptions = ref([]);
  26. const cardOptions = ref([]);
  27. const emit = defineEmits(["reload"]);
  28. const initform = {
  29. username: "",
  30. role_id: "",
  31. card_id: [],
  32. id: ""
  33. };
  34. const ruleForm = ref({ ...initform });
  35. const rules = reactive<FormRules>({
  36. username: [{ required: true, trigger: "blur", message: "请输入账号" }],
  37. role_id: [{ required: true, trigger: "change", message: "请选择角色" }],
  38. card_id: [
  39. {
  40. required: true,
  41. type: "array",
  42. trigger: "change",
  43. message: "请选择卡类型"
  44. }
  45. ]
  46. });
  47. async function show(node: any, id: string, isCreate: string) {
  48. Object.keys(ruleForm.value).forEach(key => {
  49. ruleForm.value[key] = initform[key];
  50. });
  51. console.log(ruleForm.value);
  52. TYPE.value = isCreate;
  53. switch (TYPE.value) {
  54. case "create":
  55. titleType.value = "新建运营账号";
  56. break;
  57. case "edit":
  58. titleType.value = "编辑运营账号";
  59. break;
  60. case "view":
  61. titleType.value = "运营账号详情";
  62. break;
  63. default:
  64. titleType.value = "新建运营账号";
  65. }
  66. if (TYPE.value !== "create") {
  67. await getDetail(id);
  68. }
  69. await getRole();
  70. await getCard();
  71. showModel.value = true;
  72. }
  73. function handleUpdate() {
  74. const data = {
  75. ...ruleForm.value
  76. };
  77. return {
  78. data,
  79. api: httpUpdate
  80. };
  81. }
  82. async function getDetail(id) {
  83. const { message, code, data } = await httpDetail({ id });
  84. responseHandle({
  85. code,
  86. message,
  87. logout,
  88. handler: () => {
  89. console.log(data);
  90. Object.keys(ruleForm.value).forEach(key => {
  91. if (key === "card_id") {
  92. ruleForm.value[key] = data[key];
  93. } else {
  94. ruleForm.value[key] = data[key] + "";
  95. }
  96. });
  97. }
  98. });
  99. }
  100. async function getRole() {
  101. const { message, code, data } = await httpRoleAll({});
  102. responseHandle({
  103. code,
  104. message,
  105. logout,
  106. handler: () => {
  107. roleOptions.value = data;
  108. }
  109. });
  110. }
  111. async function getCard() {
  112. const { message, code, data } = await httpCardAll({});
  113. responseHandle({
  114. code,
  115. message,
  116. logout,
  117. handler: () => {
  118. cardOptions.value = data;
  119. }
  120. });
  121. }
  122. function handleCreate() {
  123. const data = {
  124. ...ruleForm.value
  125. };
  126. return {
  127. data,
  128. api: httpAdd
  129. };
  130. }
  131. function handleSave() {
  132. formRef.value.validate(async vaild => {
  133. if (vaild) {
  134. if (loading.value) return;
  135. const handler =
  136. TYPE.value === FROM_TYPE.create ? handleCreate : handleUpdate;
  137. loading.value = true;
  138. const { api, data } = handler();
  139. const { message, code } = await api(data);
  140. responseHandle({
  141. code,
  142. message,
  143. logout,
  144. handler: () => {
  145. showModel.value = false;
  146. loading.value = false;
  147. emit("reload");
  148. }
  149. });
  150. }
  151. });
  152. }
  153. defineExpose({
  154. show,
  155. changeCurrentMenuId: (id: string) => (currentMenuId.value = id)
  156. });
  157. </script>
  158. <template>
  159. <div>
  160. <el-dialog
  161. v-model="showModel"
  162. :close-on-press-escape="false"
  163. center
  164. append-to-body
  165. destroy-on-close
  166. :width="'700px'"
  167. :title="titleType"
  168. v-loading="loading"
  169. >
  170. <el-form
  171. ref="formRef"
  172. :model="ruleForm"
  173. :rules="rules"
  174. label-width="90px"
  175. style="margin-top: -10px"
  176. class="demo-ruleForm"
  177. status-icon
  178. >
  179. <el-row>
  180. <el-col :span="24">
  181. <el-form-item label="账号名称" prop="username">
  182. <el-input
  183. v-model="ruleForm.username"
  184. :disabled="TYPE === 'view'"
  185. placeholder="账号名称"
  186. /> </el-form-item
  187. ></el-col>
  188. <el-col :span="24">
  189. <el-form-item label="角色" prop="role_id">
  190. <el-select
  191. v-model="ruleForm.role_id"
  192. style="width: 100%"
  193. :disabled="TYPE === 'view'"
  194. placeholder="角色"
  195. >
  196. <el-option
  197. v-for="(si, sii) in roleOptions"
  198. :key="'type' + si.id + sii"
  199. :label="si.name"
  200. :disabled="si.status + '' === '0'"
  201. :value="si.id + ''"
  202. />
  203. </el-select> </el-form-item
  204. ></el-col>
  205. <el-col :span="24">
  206. <el-form-item label="卡类型" prop="card_id">
  207. <el-checkbox-group
  208. v-model="ruleForm.card_id"
  209. :disabled="TYPE === 'view'"
  210. >
  211. <el-checkbox
  212. v-for="(si, sii) in cardOptions"
  213. :key="'card_id' + si.id + sii"
  214. :label="si.id + ''"
  215. :disabled="si.status + '' === '0'"
  216. >{{ si.title }}</el-checkbox
  217. >
  218. </el-checkbox-group>
  219. </el-form-item></el-col
  220. >
  221. </el-row>
  222. <div class="flex justify-end">
  223. <el-button
  224. v-if="TYPE !== 'view'"
  225. :loading="loading"
  226. type="primary"
  227. @click="handleSave"
  228. >保存</el-button
  229. >
  230. <el-button @click="showModel = false">取消</el-button>
  231. </div>
  232. </el-form>
  233. </el-dialog>
  234. </div>
  235. </template>