addEdit.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <template>
  2. <el-dialog
  3. :title="title"
  4. :center="true"
  5. align="left"
  6. top="5vh"
  7. width="1040px"
  8. @close="closeModel"
  9. :close-on-click-modal="false"
  10. :visible.sync="showModelThis"
  11. v-loading="loading"
  12. element-loading-text="拼命加载中"
  13. element-loading-spinner="el-icon-loading"
  14. element-loading-background="rgba(0, 0, 0, 0.8)"
  15. >
  16. <el-card>
  17. <el-row :gutter="10">
  18. <el-col :span="24">
  19. <el-form
  20. :model="ruleForm"
  21. status-icon
  22. :rules="rulesThis"
  23. ref="ruleForm"
  24. label-width="110px"
  25. class="demo-ruleForm"
  26. >
  27. <el-form-item
  28. label="登录名"
  29. prop="loginName"
  30. v-if="id === 'add' || isDetail"
  31. >
  32. <el-input
  33. v-model="ruleForm.loginName"
  34. :disabled="isDetail"
  35. ></el-input>
  36. </el-form-item>
  37. <el-form-item label="姓名" prop="fullName">
  38. <el-input
  39. v-model="ruleForm.fullName"
  40. :disabled="isDetail"
  41. ></el-input>
  42. </el-form-item>
  43. <el-form-item label="手机号" prop="tel">
  44. <el-input v-model="ruleForm.tel" :disabled="isDetail"></el-input>
  45. </el-form-item>
  46. <el-form-item label="密码" prop="password" v-if="id === 'add'">
  47. <el-input
  48. type="password"
  49. placeholder="密码"
  50. :maxlength="20"
  51. v-model="ruleForm.password"
  52. ></el-input>
  53. </el-form-item>
  54. <el-form-item label="确认密码" prop="password2" v-if="id === 'add'">
  55. <el-input
  56. type="password"
  57. placeholder="再次输入密码"
  58. :maxlength="20"
  59. v-model="ruleForm.password2"
  60. ></el-input>
  61. </el-form-item>
  62. </el-form>
  63. </el-col>
  64. <el-col :span="24" style="text-align: right">
  65. <el-button type="primary" @click="submitForm" v-if="!isDetail"
  66. >保 存
  67. </el-button>
  68. <el-button @click="showModelThis = false">{{
  69. isDetail ? "关 闭" : "取 消"
  70. }}</el-button>
  71. </el-col>
  72. </el-row>
  73. </el-card>
  74. </el-dialog>
  75. </template>
  76. <script>
  77. import asyncRequest from "@/apis/service/stock/survey";
  78. import resToken from "@/mixins/resToken";
  79. import {
  80. isnumber,
  81. isMobile,
  82. validEmail,
  83. isAlphanumeric,
  84. isChinese,
  85. isEmoticon,
  86. validAlphabets,
  87. } from "@/utils/validate";
  88. export default {
  89. name: "survey",
  90. props: ["showModel", "id", "isDetail", "sitem"],
  91. mixins: [resToken],
  92. data() {
  93. const validateusername = (rule, value, callback) => {
  94. if (value === "") {
  95. callback(new Error("账号不能为空!"));
  96. } else {
  97. if (value.length < 6 || value.length > 18) {
  98. callback(new Error("账号规则为6~18位数字与字母组合!"));
  99. } else {
  100. if (isnumber(value)) {
  101. callback(new Error("账号规则为6~18位数字与字母组合!"));
  102. } else if (validAlphabets(value)) {
  103. callback(new Error("账号规则为6~18位数字与字母组合!"));
  104. } else if (!isAlphanumeric(value)) {
  105. callback(new Error("账号规则为6~18位数字与字母组合!"));
  106. } else {
  107. callback();
  108. }
  109. }
  110. }
  111. };
  112. const validatename = (rule, value, callback) => {
  113. if (value === "") {
  114. callback(new Error("真实姓名不能为空!"));
  115. } else {
  116. if (value.length < 2 || value.length > 12) {
  117. callback(new Error("真实姓名规则为2~12位汉字!"));
  118. } else {
  119. if (!isChinese(value)) {
  120. console.log(9999);
  121. callback(new Error("真实姓名规则为2~12位汉字!"));
  122. } else if (isEmoticon(value)) {
  123. console.log(2345);
  124. callback(new Error("真实姓名规则为2~12位汉字!"));
  125. } else {
  126. callback();
  127. }
  128. }
  129. }
  130. };
  131. const validatemobile = (rule, value, callback) => {
  132. if (value === "") {
  133. callback(new Error("手机号不能为空!"));
  134. } else {
  135. if (!isMobile(value)) {
  136. callback(new Error("手机号格式不正确!"));
  137. } else {
  138. callback();
  139. }
  140. }
  141. };
  142. const validateEmail = (rule, value, callback) => {
  143. if (value === "") {
  144. callback();
  145. } else {
  146. if (!validEmail(value)) {
  147. callback(new Error("邮箱格式不正确!"));
  148. } else {
  149. callback();
  150. }
  151. }
  152. };
  153. return {
  154. loading: false,
  155. title: "添加账号",
  156. showModelThis: this.showModel,
  157. ruleForm: {
  158. username: "", // 账号
  159. name: "", // 真实姓名
  160. mobile: "",
  161. email: "",
  162. role_id: "",
  163. status: "1",
  164. item: [],
  165. },
  166. rulesThis: this.rules,
  167. rules: {
  168. name: [
  169. {
  170. required: true,
  171. validator: validatename,
  172. trigger: "blur",
  173. },
  174. ],
  175. username: [
  176. {
  177. required: true,
  178. validator: validateusername,
  179. trigger: "blur",
  180. },
  181. ],
  182. mobile: [
  183. {
  184. required: true,
  185. validator: validatemobile,
  186. trigger: "blur",
  187. },
  188. ],
  189. email: [
  190. {
  191. required: false,
  192. validator: validateEmail,
  193. trigger: "blur",
  194. },
  195. ],
  196. role_id: [
  197. {
  198. required: true,
  199. message: "请选择角色",
  200. trigger: "change",
  201. },
  202. ],
  203. item: [
  204. {
  205. type: "array",
  206. required: true,
  207. message: "请选择所在部门",
  208. trigger: "change",
  209. },
  210. ],
  211. status: [
  212. {
  213. required: true,
  214. message: "请选择状态",
  215. trigger: "change",
  216. },
  217. ],
  218. },
  219. };
  220. },
  221. watch: {
  222. showModel: function (val) {
  223. this.showModelThis = val;
  224. if (val) {
  225. this.initForm();
  226. }
  227. },
  228. showModelThis(val) {
  229. if (!val) {
  230. this.$emit("cancel");
  231. }
  232. },
  233. },
  234. methods: {
  235. closeModel() {
  236. console.log("closeModel!!");
  237. },
  238. async initForm() {
  239. this.loading = true;
  240. // await this.getRole();
  241. if (this.id === "add") {
  242. this.title = "添加账号";
  243. this.rulesThis = this.rules;
  244. await this.resetForm();
  245. } else {
  246. if (this.isDetail) {
  247. this.title = "账号详情";
  248. this.rulesThis = {};
  249. } else {
  250. this.title = "修改账号";
  251. this.rulesThis = this.rules;
  252. }
  253. await this.resetForm(this.sitem);
  254. // await this.initData()
  255. }
  256. this.loading = false;
  257. },
  258. // async getRole() {
  259. // const model = {
  260. // status: "", // 状态
  261. // level: "", // 姓名
  262. // role_name: "",
  263. // };
  264. // const res = await asyncRequest.getRole(model);
  265. // if (res && res.code === 0 && res.data) {
  266. // this.roleList = res.data;
  267. // this.roleList.map((v1) => {
  268. // v1.id += "";
  269. // v1.status += "";
  270. // return v1;
  271. // });
  272. // }
  273. // },
  274. async initData() {
  275. const res = await asyncRequest.detail({ id: this.id });
  276. if (res && res.code === 0 && res.data) {
  277. this.ruleForm = res.data;
  278. this.ruleForm.role_id = this.ruleForm.role;
  279. } else if (res && res.code >= 100 && res.code <= 104) {
  280. await this.logout();
  281. } else {
  282. this.$message.warning(res.message);
  283. }
  284. },
  285. async resetForm(sitem) {
  286. // 重置
  287. await this.$nextTick(() => {
  288. if (this.$refs.ruleForm) {
  289. this.$refs.ruleForm.resetFields();
  290. this.$refs.ruleForm.clearValidate();
  291. const { username, nickname, mobile, email, roleid, status, item } =
  292. sitem;
  293. this.ruleForm = {
  294. username: username || "", // 账号
  295. name: nickname || "", // 真实姓名
  296. mobile: mobile || "",
  297. email: email || "",
  298. role_id: roleid || "",
  299. status: status || "",
  300. item: item || [],
  301. };
  302. if (this.id === "add" || this.isDetail) {
  303. this.rules.username[0].required = false;
  304. }
  305. }
  306. });
  307. },
  308. async submitForm() {
  309. await this.$refs.ruleForm.validate(async (valid) => {
  310. if (valid) {
  311. this.loading = true;
  312. const { username, name, mobile, email, role_id, status } = JSON.parse(
  313. JSON.stringify(this.ruleForm)
  314. );
  315. const model = {
  316. id: this.id,
  317. username: username || "", // 账号
  318. nickname: name || "", // 真实姓名
  319. mobile: mobile || "",
  320. email: email || "",
  321. role: role_id || "",
  322. status: status || "",
  323. };
  324. let res = {};
  325. if (this.id === "add") {
  326. delete model["id"];
  327. res = await asyncRequest.add(model);
  328. } else {
  329. res = await asyncRequest.update(model);
  330. }
  331. this.loading = false;
  332. if (res && res.code === 0) {
  333. const title = this.id === "add" ? "添加成功" : "修改成功";
  334. this.$notify.success({
  335. title,
  336. message: "",
  337. });
  338. this.showModelThis = false;
  339. // 刷新
  340. this.$emit("refresh");
  341. } else if (res && res.code >= 100 && res.code <= 104) {
  342. await this.logout();
  343. } else {
  344. this.$message.warning(res.message);
  345. }
  346. } else {
  347. console.log("error submit!!");
  348. return false;
  349. }
  350. });
  351. },
  352. },
  353. };
  354. </script>
  355. <style lang="scss" scoped>
  356. .survey {
  357. }
  358. </style>