addEditPage.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. module.exports = function(inputName,compoenntName) {
  2. return `<template>
  3. <el-dialog
  4. :title="title"
  5. :center="true"
  6. align="left"
  7. top="5vh"
  8. width="1040px"
  9. @close="showModelThis=false"
  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/service/${inputName}";
  77. import resToken from "@/mixins/resToken";
  78. import {
  79. isnumber,
  80. isMobile,
  81. validEmail,
  82. isAlphanumeric,
  83. isChinese,
  84. isEmoticon,
  85. validAlphabets,
  86. } from "@/utils/validate";
  87. export default {
  88. name: '${compoenntName}',
  89. props: ["showModel", "id", "isDetail","sitem"],
  90. mixins: [resToken],
  91. data() {
  92. const validateusername = (rule, value, callback) => {
  93. if (value === "") {
  94. callback(new Error("账号不能为空!"));
  95. } else {
  96. if (value.length < 6 || value.length > 18) {
  97. callback(new Error("账号规则为6~18位数字与字母组合!"));
  98. } else {
  99. if (isnumber(value)) {
  100. callback(new Error("账号规则为6~18位数字与字母组合!"));
  101. } else if (validAlphabets(value)) {
  102. callback(new Error("账号规则为6~18位数字与字母组合!"));
  103. } else if (!isAlphanumeric(value)) {
  104. callback(new Error("账号规则为6~18位数字与字母组合!"));
  105. } else {
  106. callback();
  107. }
  108. }
  109. }
  110. };
  111. const validatename = (rule, value, callback) => {
  112. if (value === "") {
  113. callback(new Error("真实姓名不能为空!"));
  114. } else {
  115. if (value.length < 2 || value.length > 12) {
  116. callback(new Error("真实姓名规则为2~12位汉字!"));
  117. } else {
  118. if (!isChinese(value)) {
  119. console.log(9999);
  120. callback(new Error("真实姓名规则为2~12位汉字!"));
  121. } else if (isEmoticon(value)) {
  122. console.log(2345);
  123. callback(new Error("真实姓名规则为2~12位汉字!"));
  124. } else {
  125. callback();
  126. }
  127. }
  128. }
  129. };
  130. const validatemobile = (rule, value, callback) => {
  131. if (value === "") {
  132. callback(new Error("手机号不能为空!"));
  133. } else {
  134. if (!isMobile(value)) {
  135. callback(new Error("手机号格式不正确!"));
  136. } else {
  137. callback();
  138. }
  139. }
  140. };
  141. const validateEmail = (rule, value, callback) => {
  142. if (value === "") {
  143. callback();
  144. } else {
  145. if (!validEmail(value)) {
  146. callback(new Error("邮箱格式不正确!"));
  147. } else {
  148. callback();
  149. }
  150. }
  151. };
  152. return {
  153. loading: false,
  154. title: "添加账号",
  155. showModelThis: this.showModel,
  156. ruleForm: {
  157. username: "", // 账号
  158. name: "", // 真实姓名
  159. mobile: "",
  160. email: "",
  161. role_id: "",
  162. status: "1",
  163. item: [],
  164. },
  165. rulesThis: this.rules,
  166. rules: {
  167. name: [
  168. {
  169. required: true,
  170. validator: validatename,
  171. trigger: "blur",
  172. },
  173. ],
  174. username: [
  175. {
  176. required: true,
  177. validator: validateusername,
  178. trigger: "blur",
  179. },
  180. ],
  181. mobile: [
  182. {
  183. required: true,
  184. validator: validatemobile,
  185. trigger: "blur",
  186. },
  187. ],
  188. email: [
  189. {
  190. required: false,
  191. validator: validateEmail,
  192. trigger: "blur",
  193. },
  194. ],
  195. role_id: [
  196. {
  197. required: true,
  198. message: "请选择角色",
  199. trigger: "change",
  200. },
  201. ],
  202. item: [
  203. {
  204. type: "array",
  205. required: true,
  206. message: "请选择所在部门",
  207. trigger: "change",
  208. },
  209. ],
  210. status: [
  211. {
  212. required: true,
  213. message: "请选择状态",
  214. trigger: "change",
  215. },
  216. ],
  217. }
  218. };
  219. },
  220. watch: {
  221. showModel: function(val) {
  222. this.showModelThis = val;
  223. if (val) {
  224. this.initForm();
  225. }
  226. },
  227. showModelThis(val) {
  228. if (!val) {
  229. this.$emit("cancel");
  230. }
  231. }
  232. },
  233. methods: {
  234. async initForm() {
  235. this.loading = true;
  236. // await this.getRole();
  237. if (this.id === "add") {
  238. this.title = "添加账号";
  239. this.rulesThis = this.rules;
  240. await this.resetForm();
  241. } else {
  242. if (this.isDetail) {
  243. this.title = "账号详情";
  244. this.rulesThis = {};
  245. } else {
  246. this.title = "修改账号";
  247. this.rulesThis = this.rules;
  248. }
  249. await this.resetForm(this.sitem);
  250. // await this.initData()
  251. }
  252. this.loading = false;
  253. },
  254. // async getRole() {
  255. // const model = {
  256. // status: "", // 状态
  257. // level: "", // 姓名
  258. // role_name: "",
  259. // };
  260. // const res = await asyncRequest.getRole(model);
  261. // if (res && res.code === 0 && res.data) {
  262. // this.roleList = res.data;
  263. // this.roleList.map((v1) => {
  264. // v1.id += "";
  265. // v1.status += "";
  266. // return v1;
  267. // });
  268. // }
  269. // },
  270. async initData() {
  271. const res = await asyncRequest.detail({ id: this.id });
  272. if (res && res.code === 0 && res.data) {
  273. this.ruleForm = res.data;
  274. this.ruleForm.role_id = this.ruleForm.role;
  275. } else if (res && res.code >= 100 && res.code <= 104) {
  276. await this.logout();
  277. } else {
  278. this.$message.warning(res.message);
  279. }
  280. },
  281. async resetForm(sitem) {
  282. // 重置
  283. await this.$nextTick(() => {
  284. if (this.$refs.ruleForm) {
  285. this.$refs.ruleForm.resetFields();
  286. this.$refs.ruleForm.clearValidate();
  287. const { username, nickname, mobile, email, roleid, status, item } =
  288. sitem;
  289. this.ruleForm = {
  290. username: username || "", // 账号
  291. name: nickname || "", // 真实姓名
  292. mobile: mobile || "",
  293. email: email || "",
  294. role_id: roleid || "",
  295. status: status || "",
  296. item: item || [],
  297. };
  298. if (this.id === "add" || this.isDetail) {
  299. this.rules.username[0].required = false;
  300. }
  301. }
  302. });
  303. },
  304. async submitForm() {
  305. await this.$refs.ruleForm.validate(async (valid) => {
  306. if (valid) {
  307. this.loading = true;
  308. const { username, name, mobile, email, role_id, status } = JSON.parse(
  309. JSON.stringify(this.ruleForm)
  310. );
  311. const model = {
  312. id: this.id,
  313. username: username || "", // 账号
  314. nickname: name || "", // 真实姓名
  315. mobile: mobile || "",
  316. email: email || "",
  317. role: role_id || "",
  318. status: status || "",
  319. };
  320. let res = {};
  321. if (this.id === "add") {
  322. delete model["id"];
  323. res = await asyncRequest.add(model);
  324. } else {
  325. res = await asyncRequest.update(model);
  326. }
  327. this.loading = false;
  328. if (res && res.code === 0) {
  329. const title = this.id === "add" ? "添加成功" : "修改成功";
  330. this.$notify.success({
  331. title,
  332. message: "",
  333. });
  334. this.showModelThis = false;
  335. // 刷新
  336. this.$emit("refresh");
  337. } else if (res && res.code >= 100 && res.code <= 104) {
  338. await this.logout();
  339. } else {
  340. this.$message.warning(res.message);
  341. }
  342. } else {
  343. console.log("error submit!!");
  344. return false;
  345. }
  346. });
  347. },
  348. },
  349. };
  350. </script>
  351. <style lang="scss" scoped>
  352. .${compoenntName} {
  353. }
  354. </style>
  355. `;
  356. };