addEdit.vue 9.3 KB

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