addEditPage.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. module.exports = function(compoenntName) {
  2. return `<template>
  3. <el-dialog
  4. :title="title"
  5. :center="true"
  6. align="left"
  7. top="5vh"
  8. width="1040px"
  9. @close="closeModel"
  10. :close-on-click-modal="false"
  11. :visible.sync="showModelThis"
  12. v-loading="loading"
  13. element-loading-text="拼命加载中"
  14. element-loading-spinner="el-icon-loading"
  15. element-loading-background="rgba(0, 0, 0, 0.8)"
  16. >
  17. <el-card>
  18. <el-row :gutter="10">
  19. <el-col :span="24">
  20. <el-form
  21. :model="ruleForm"
  22. status-icon
  23. :rules="rulesThis"
  24. ref="ruleForm"
  25. label-width="110px"
  26. class="demo-ruleForm"
  27. >
  28. <el-form-item
  29. label="登录名"
  30. prop="loginName"
  31. v-if="id === 'add' || isDetail"
  32. >
  33. <el-input
  34. v-model="ruleForm.loginName"
  35. :disabled="isDetail"
  36. ></el-input>
  37. </el-form-item>
  38. <el-form-item label="姓名" prop="fullName">
  39. <el-input
  40. v-model="ruleForm.fullName"
  41. :disabled="isDetail"
  42. ></el-input>
  43. </el-form-item>
  44. <el-form-item label="手机号" prop="tel">
  45. <el-input v-model="ruleForm.tel" :disabled="isDetail"></el-input>
  46. </el-form-item>
  47. <el-form-item label="密码" prop="password" v-if="id === 'add'">
  48. <el-input
  49. type="password"
  50. placeholder="密码"
  51. :maxlength="20"
  52. v-model="ruleForm.password"
  53. ></el-input>
  54. </el-form-item>
  55. <el-form-item label="确认密码" prop="password2" v-if="id === 'add'">
  56. <el-input
  57. type="password"
  58. placeholder="再次输入密码"
  59. :maxlength="20"
  60. v-model="ruleForm.password2"
  61. ></el-input>
  62. </el-form-item>
  63. </el-form>
  64. </el-col>
  65. <el-col :span="24" style="text-align: right;">
  66. <el-button type="primary" @click="submitForm" v-if="!isDetail"
  67. >保 存
  68. </el-button>
  69. <el-button @click="showModelThis = false">{{ isDetail ? "关 闭" : "取 消" }}</el-button>
  70. </el-col>
  71. </el-row>
  72. </el-card>
  73. </el-dialog>
  74. </template>
  75. <script>
  76. import asyncRequest from "@/apis/caixiaoService/${compoenntName}";
  77. export default {
  78. name: '${compoenntName}',
  79. props: ["showModel", "id", "isDetail"],
  80. data() {
  81. let validatePass = (rule, value, callback) => {
  82. if (value === "" || value === undefined) {
  83. callback(new Error("请输入新密码"));
  84. } else {
  85. if (value.length < 6 || value.length > 15) {
  86. callback(new Error("必须是6到15位,支持数字、字母、符号组合"));
  87. } else {
  88. if (this.ruleForm.password2 !== "") {
  89. this.$refs.ruleForm.validateField("password2");
  90. }
  91. callback();
  92. }
  93. }
  94. };
  95. let validatePass2 = (rule, value, callback) => {
  96. if (value === "" || value === undefined) {
  97. callback(new Error("请再次输入密码"));
  98. } else if (value !== this.ruleForm.password) {
  99. callback(new Error("两次输入密码不一致!"));
  100. } else {
  101. if (value.length < 6 || value.length > 15) {
  102. callback(new Error("必须是6到15位,支持数字、字母、符号组合"));
  103. } else {
  104. callback();
  105. }
  106. }
  107. };
  108. return {
  109. loading: false,
  110. title: "添加管理人员",
  111. showModelThis: this.showModel,
  112. ruleForm: {
  113. tel: "",
  114. fullName: "",
  115. loginName: "",
  116. passport: ""
  117. // isAdmin: 0
  118. },
  119. rulesThis: this.rules,
  120. rules: {
  121. loginName: [
  122. {
  123. required: true,
  124. message: "请输入登录名",
  125. trigger: "blur"
  126. },
  127. {
  128. min: 2,
  129. max: 30,
  130. message: "长度在 2 到 30 个字符",
  131. trigger: "blur"
  132. }
  133. ],
  134. fullName: [
  135. {
  136. required: true,
  137. message: "请输入姓名",
  138. trigger: "blur"
  139. },
  140. {
  141. min: 2,
  142. max: 30,
  143. message: "长度在 2 到 30 个字符",
  144. trigger: "blur"
  145. }
  146. ],
  147. password: [
  148. {
  149. required: true,
  150. validator: validatePass,
  151. trigger: "blur"
  152. }
  153. ],
  154. password2: [
  155. {
  156. required: true,
  157. validator: validatePass2,
  158. trigger: "blur"
  159. }
  160. ],
  161. tel: [
  162. {
  163. required: true,
  164. message: "请输入手机号码",
  165. trigger: "blur"
  166. },
  167. {
  168. validator: this.$rulesPhone,
  169. trigger: "blur"
  170. }
  171. ]
  172. }
  173. };
  174. },
  175. methods: {
  176. closeModel() {
  177. console.log("closeModel!!");
  178. },
  179. async initForm() {
  180. if (this.id === "add") {
  181. this.title = "添加管理人员";
  182. // this.ruleForm.isAdmin = 0;
  183. this.loading = false;
  184. this.rulesThis = this.rules;
  185. await this.resetForm();
  186. } else {
  187. if (this.isDetail) {
  188. this.title = "管理人员详情";
  189. this.rulesThis = {};
  190. } else {
  191. this.title = "修改管理人员";
  192. this.rulesThis = this.rules;
  193. }
  194. await this.resetForm();
  195. await this.initData();
  196. }
  197. },
  198. async initData() {
  199. this.loading = true;
  200. let res = await asyncRequest.detail({id:this.id});
  201. this.loading = false;
  202. if (res.code === 0) {
  203. this.ruleForm = res.data;
  204. }
  205. },
  206. async resetForm() {
  207. // 重置
  208. await this.$nextTick(() => {
  209. if (this.$refs.ruleForm) {
  210. this.$refs.ruleForm.resetFields();
  211. this.$refs.ruleForm.clearValidate();
  212. this.ruleForm = {
  213. tel: "",
  214. fullName: "",
  215. loginName: "",
  216. passport: ""
  217. // isAdmin: 0
  218. };
  219. }
  220. });
  221. },
  222. async submitForm() {
  223. await this.$refs.ruleForm.validate(async valid => {
  224. if (valid) {
  225. this.loading = true;
  226. let obj = JSON.parse(JSON.stringify(this.ruleForm));
  227. let res = {};
  228. if (this.id === "add") {
  229. obj.passport = obj.password;
  230. res = await asyncRequest.add(obj);
  231. } else {
  232. res = await asyncRequest.update(obj);
  233. }
  234. this.loading = false;
  235. if (res.code === 0) {
  236. let title = this.id === "add" ? "添加成功" : "修改成功";
  237. this.$notify.success({
  238. title,
  239. message: ""
  240. });
  241. this.showModelThis = false;
  242. // 刷新
  243. this.$emit("refresh");
  244. }
  245. } else {
  246. console.log("error submit!!");
  247. return false;
  248. }
  249. });
  250. }
  251. },
  252. watch: {
  253. showModel: function(val) {
  254. this.showModelThis = val;
  255. if (val) {
  256. this.initForm();
  257. }
  258. },
  259. showModelThis(val) {
  260. if (!val) {
  261. this.$emit("cancel");
  262. }
  263. }
  264. }
  265. };
  266. </script>
  267. <style lang="scss" scoped>
  268. .${compoenntName} {
  269. }
  270. </style>
  271. `;
  272. };