EditForm.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <div>
  3. <div class="alert-body__main_content">
  4. <zz-form
  5. :cols="formCols"
  6. :data="formData"
  7. :rules="formRules"
  8. :errors="formErrors"
  9. labelWidth="100"
  10. ref="form"
  11. :disabled="!isEdit"
  12. >
  13. <el-select
  14. @change="selectChanged"
  15. slot="orgType"
  16. v-model="formData.orgType"
  17. :disabled="isEdit == 'edit'"
  18. clearable
  19. placeholder="请选择机构类型"
  20. >
  21. <el-option v-for="item in organTypeList" :key="item.value" :value="item.value" :label="item.label"></el-option>
  22. </el-select>
  23. <el-input
  24. v-if="isEdit !== 'edit' && isEdit !== 'add' && formData.parentOrgId == ''"
  25. slot="parentOrgId"
  26. type="text"
  27. placeholder="请选择上级机构"
  28. >
  29. </el-input>
  30. <select-tree
  31. v-else
  32. slot="parentOrgId"
  33. class="new-select-tree"
  34. empty-text="请选择上级机构"
  35. placeholder="请选择上级机构"
  36. :disabled="isEdit !== 'edit' && isEdit !== 'add'"
  37. :options="organList"
  38. :props="defaultProps"
  39. v-model="formData.parentOrgId"
  40. />
  41. <el-cascader
  42. slot="orgAreaId"
  43. v-model="formData.orgAreaId"
  44. :options="areaList"
  45. placeholder="请选择区域"
  46. :props="{ label: 'name', value: 'code' }"
  47. ></el-cascader>
  48. <el-select slot="orgState" v-model="formData.orgState" clearable>
  49. <el-option :value="0" label="停用"></el-option>
  50. <el-option :value="1" label="启用"></el-option>
  51. </el-select>
  52. <el-select slot="orgLeaderSex" v-model="formData.orgLeaderSex" placeholder="请选择性别">
  53. <el-option value="0" label="女"></el-option>
  54. <el-option value="1" label="男"></el-option>
  55. </el-select>
  56. <div slot="remark" style="position: relative">
  57. <el-input type="textarea" v-model="formData.remark" maxlength="50" placeholder="请输入备注"> </el-input>
  58. <span style="position: absolute; bottom: 0; right: 10px" v-if="formData.remark">{{ formData.remark.length }}/50</span>
  59. </div>
  60. </zz-form>
  61. <zz-form :cols="formTwoCols" :data="formData" :errors="formErrors" ref="form1" labelWidth="100" :disabled="!isEdit">
  62. <el-select slot="orgLeaderSex" v-model="formData.orgLeaderSex" placeholder="请选择性别">
  63. <el-option value="0" label="女"></el-option>
  64. <el-option value="1" label="男"></el-option>
  65. </el-select>
  66. <div slot="remark" style="position: relative">
  67. <el-input type="textarea" v-model="formData.remark" maxlength="50" placeholder="请输入备注"> </el-input>
  68. <span style="position: absolute; bottom: 0; right: 10px" v-if="formData.remark">{{ formData.remark.length }}/50</span>
  69. </div>
  70. </zz-form>
  71. </div>
  72. <div class="buttons" v-if="isEdit">
  73. <el-button @click="reset">重置</el-button>
  74. <el-button type="primary" :disabled="onSubmit" @click="submit">保存</el-button>
  75. </div>
  76. </div>
  77. </template>
  78. <script>
  79. // import SelectTree from '@/components/common/SelectTree.vue';
  80. export default {
  81. components: {
  82. // SelectTree
  83. },
  84. props: ['formData', 'isEdit'],
  85. data() {
  86. return {
  87. defaultProps: {
  88. value: 'id', // 唯一标识
  89. label: 'orgName', // 标签显示
  90. children: 'orgs' // 子级
  91. },
  92. // formData: {
  93. // id: '',
  94. // orgType: '', //机构类型
  95. // parentOrgId: '', //上级机构
  96. // orgAreaId: '', //所属区域
  97. // orgName: '', //机构名称
  98. // orgState: 1, //状态
  99. // orgLeaderName: '', //负责人
  100. // orgLeaderPhone: '', //手机号
  101. // orgLeaderSex: '', //性别
  102. // orgLeaderEmail: '', //邮箱
  103. // remark: '' //备注
  104. // },
  105. formCols: [
  106. [
  107. {
  108. label: '机构类型',
  109. prop: 'orgType',
  110. slot: 'orgType'
  111. },
  112. {
  113. label: '上级机构',
  114. prop: 'parentOrgId',
  115. slot: 'parentOrgId'
  116. },
  117. {
  118. label: '所属区域',
  119. prop: 'orgAreaId',
  120. slot: 'orgAreaId'
  121. },
  122. {
  123. label: '机构名称',
  124. prop: 'orgName',
  125. input: true,
  126. placeholder: '限20位,中文、字母、数字或下划线',
  127. maxlength: 20
  128. },
  129. {
  130. label: '状态',
  131. prop: 'orgState',
  132. slot: 'orgState'
  133. }
  134. // {
  135. // label: '负责人',
  136. // prop: 'orgLeaderName',
  137. // input: true,
  138. // placeholder: '限20位,中文、字母、数字或下划线',
  139. // maxlength: 20
  140. // },
  141. // {
  142. // label: '手机号',
  143. // prop: 'orgLeaderPhone',
  144. // input: true,
  145. // placeholder: '请输入11位手机号码',
  146. // maxlength: 11
  147. // },
  148. // {
  149. // label: '性别',
  150. // prop: 'orgLeaderSex',
  151. // slot: 'orgLeaderSex'
  152. // },
  153. // {
  154. // label: '邮箱',
  155. // prop: 'orgLeaderEmail',
  156. // input: true,
  157. // placeholder: '请输入邮箱'
  158. // },
  159. // {
  160. // label: '备注',
  161. // prop: 'remark',
  162. // slot: 'remark'
  163. // }
  164. ]
  165. ],
  166. formTwoCols: [
  167. [
  168. {
  169. label: '负责人',
  170. prop: 'orgLeaderName',
  171. input: true,
  172. placeholder: '限20位,中文、字母、数字或下划线',
  173. maxlength: 20
  174. },
  175. {
  176. label: '手机号',
  177. prop: 'orgLeaderPhone',
  178. input: true,
  179. placeholder: '请输入11位手机号码',
  180. maxlength: 11
  181. },
  182. {
  183. label: '性别',
  184. prop: 'orgLeaderSex',
  185. slot: 'orgLeaderSex'
  186. },
  187. {
  188. label: '邮箱',
  189. prop: 'orgLeaderEmail',
  190. input: true,
  191. placeholder: '请输入邮箱'
  192. },
  193. {
  194. label: '备注',
  195. prop: 'remark',
  196. slot: 'remark'
  197. }
  198. ]
  199. ],
  200. formRules: {
  201. orgType: [this.$valid.selectRequired('请选择机构类型')],
  202. parentOrgId: [this.$valid.selectRequired('请选择上级机构')],
  203. orgAreaId: [this.$valid.inputRequired('请选择所属区域')],
  204. orgName: [this.$valid.inputRequired('机构名称'), this.$valid.pattern(/^[\u4e00-\u9fa5A-Za-z0-9_]{1,20}$/)],
  205. orgState: [this.$valid.inputRequired('状态')],
  206. orgLeaderName: [this.$valid.pattern(/^[\u4e00-\u9fa5A-Za-z0-9_]{0,20}$/)],
  207. orgLeaderPhone: [
  208. this.$valid.pattern(
  209. /^((0\d{2,3}-\d{7,8})|((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|147)\d{8})$/
  210. )
  211. ],
  212. orgLeaderEmail: [this.$valid.pattern(/^\w+@[a-z0-9]+\.[a-z]{2,4}$/)]
  213. },
  214. formErrors: {},
  215. organList: [],
  216. areaList: [],
  217. cascaderProps: {
  218. label: 'orgName',
  219. value: 'id'
  220. },
  221. organTypeList: [
  222. {
  223. value: 'company',
  224. label: '公司'
  225. },
  226. {
  227. value: 'department',
  228. label: '部门'
  229. }
  230. ],
  231. // 省市区
  232. selectArea: []
  233. };
  234. },
  235. watch: {
  236. 'formData.orgType'(val) {
  237. this.getorgTree();
  238. }
  239. },
  240. methods: {
  241. reset() {
  242. const id = this.formData.id;
  243. this.formData = Object.assign(
  244. {},
  245. this.data,
  246. {
  247. id: '',
  248. orgType: '', //机构类型
  249. parentOrgId: '', //上级机构
  250. orgAreaId: '', //所属区域
  251. orgName: '', //机构名称
  252. orgState: 1, //状态
  253. orgLeaderName: '', //负责人
  254. orgLeaderPhone: '', //手机号
  255. orgLeaderSex: '', //性别
  256. orgLeaderEmail: '', //邮箱
  257. remark: '' //备注
  258. },
  259. { id }
  260. );
  261. this.initFrom('form');
  262. },
  263. initFrom(name) {
  264. this.$refs[name].resetFields();
  265. },
  266. selectChanged() {
  267. this.formData.parentOrgId = '';
  268. },
  269. getorgTree() {
  270. this.$http.postForm('/org/getOrgTree', { orgType: this.formData.orgType }).then(({ status, data, msg }) => {
  271. if (status === 0 && data) {
  272. this.organList = data;
  273. }
  274. });
  275. },
  276. submit() {
  277. if (this.formData.id === this.formData.parentOrgId) {
  278. this.$message.error('上级机构不能选择自身!');
  279. return;
  280. }
  281. let newData = {
  282. ...this.formData,
  283. orgAreaId: this.formData.orgAreaId[this.formData.orgAreaId.length - 1]
  284. };
  285. new Promise((resolve) => {
  286. this.$refs.form.validate(resolve);
  287. }).then(() => {
  288. let data = JSON.parse(JSON.stringify(newData)),
  289. method = 'post',
  290. url = '/org/insert';
  291. if ('edit' == this.isEdit) {
  292. url = '/org/update';
  293. }
  294. this.$http[method](url, data)
  295. .then(({ status, data, msg }) => {
  296. if (0 == status) {
  297. this.$emit('getList');
  298. this.$message.success(msg);
  299. this.formData.callback && this.formData.callback();
  300. } else {
  301. this.$message.error(msg);
  302. }
  303. })
  304. .catch((err) => {});
  305. });
  306. },
  307. getAreaList() {
  308. this.$http.postForm('/area/selectAll', { id: 123 }).then(({ status, data, msg }) => {
  309. if (status === 0 && data) {
  310. this.areaList = data;
  311. }
  312. });
  313. }
  314. },
  315. created() {
  316. this.getAreaList();
  317. // if ('edit' === this.formData.todo) {
  318. // this.__setValue('formData');
  319. // this.isEdit=true;
  320. // }
  321. }
  322. };
  323. </script>
  324. <style lang='scss' scoped>
  325. .alert-body__main_content {
  326. display: flex;
  327. justify-content: space-around;
  328. & > form {
  329. width: 48%;
  330. }
  331. }
  332. .buttons {
  333. text-align: center;
  334. }
  335. .new-select-tree {
  336. vertical-align: top;
  337. display: inline-block;
  338. width: 100%;
  339. }
  340. </style>