Plat Admin

editUser.vue 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <el-dialog :title="operateM.tit" :visible.sync="userDialogVisible">
  3. <el-form
  4. :model="userForm"
  5. :rules="userRules"
  6. ref="userForm"
  7. class="form-main"
  8. label-width="80px">
  9. <el-row :gutter="20">
  10. <el-col :span="24">
  11. <el-form-item v-if="activeShow" label="账号状态">
  12. <span>{{actived?'正常账号':'停用账号'}}</span>
  13. </el-form-item>
  14. </el-col>
  15. <el-col :span="item.span||''" v-for="item in formItem" :key="item.index">
  16. <el-form-item v-if="readonlyShow && !item.right" v-show="userForm[item.prop]" :label="item.tit">
  17. <span>{{userForm[item.prop]}}</span>
  18. </el-form-item>
  19. <el-form-item v-if="!readonlyShow && !item.right" :label="item.tit" :prop="item.prop">
  20. <el-input v-model="userForm[item.prop]" :placeholder="`请填写${item.tit}`" :maxlength="item.num||''"></el-input>
  21. </el-form-item>
  22. <el-form-item v-if="readonlyShow && viewShow && item.right" v-show="userForm[item.prop]" label="权限">
  23. <span>{{rightCodeShow(userForm[item.prop])}}</span>
  24. </el-form-item>
  25. <el-form-item v-if="item.right && !activeShow && !viewShow" :prop="item.prop" label-width="0">
  26. <template>
  27. <el-card shadow="never">
  28. <div class="card-title" slot="header">
  29. <span>{{item.tit}}</span>
  30. </div>
  31. <el-checkbox-group
  32. v-model="userForm[item.prop]"
  33. :min="1"
  34. :max="moduleOption.length"
  35. @change="handleCheckedChange">
  36. <el-checkbox v-for="mod in moduleOption" :label="mod.id" :key="mod.index">{{mod.name}}</el-checkbox>
  37. </el-checkbox-group>
  38. </el-card>
  39. </template>
  40. </el-form-item>
  41. </el-col>
  42. <el-col :span="24" class="el-btn-col">
  43. <div class="el-btn-col-box">
  44. <el-button v-if="viewShow" type="primary" @click="userDialogVisible=false">确定</el-button>
  45. <el-button v-else type="primary" @click="submitForm('userForm')">保存</el-button>
  46. </div>
  47. </el-col>
  48. <el-col :span="24" v-if="timeShow">
  49. <el-form-item align="right">
  50. <span>创建时间: {{createTime}}</span>
  51. </el-form-item>
  52. </el-col>
  53. </el-row>
  54. </el-form>
  55. </el-dialog>
  56. </template>
  57. <script>
  58. import { parseTime } from '@/utils'
  59. import {
  60. requiredTip,
  61. limitNumTip,
  62. checkEmailV
  63. } from '@/utils/validator'
  64. import {
  65. existUser,
  66. addUser,
  67. updateUser,
  68. updatePermission,
  69. queryUserOne
  70. } from '@/api/sysSetting'
  71. export default {
  72. props: ['operateM'],
  73. data() {
  74. return {
  75. userDialogVisible: false,
  76. moduleOption: [
  77. { id: '1', name: '内容发布' },
  78. { id: '2', name: '特约专家' },
  79. { id: '3', name: '合作机构' },
  80. { id: '4', name: '企业中心' },
  81. { id: '5', name: '平台信息' },
  82. { id: '6', name: '用户信息' },
  83. { id: '7', name: '需求中心' },
  84. { id: '8', name: '统计' }
  85. ],
  86. userId: '',
  87. actived: 0,
  88. oldAccount: '',
  89. createTime: '',
  90. formItem: [
  91. {
  92. span: 12,
  93. prop: 'account',
  94. tit: '账号',
  95. num: 20,
  96. required: true
  97. },
  98. {
  99. span: 12,
  100. prop: 'name',
  101. tit: '用户名',
  102. required: true
  103. },
  104. {
  105. span: 12,
  106. prop: 'phone',
  107. tit: '联系电话',
  108. num: 20,
  109. required: true
  110. },
  111. {
  112. span: 12,
  113. prop: 'email',
  114. tit: '联系邮箱',
  115. num: 50
  116. },
  117. {
  118. span: 12,
  119. prop: 'job',
  120. tit: '职位',
  121. num: 20
  122. },
  123. {
  124. span: 12,
  125. prop: 'dep',
  126. tit: '所在部门',
  127. num: 20
  128. },
  129. {
  130. span: 24,
  131. prop: 'comp',
  132. tit: '所在机构',
  133. num: 50
  134. },
  135. {
  136. span: 24,
  137. prop: 'rightCode',
  138. tit: '分配权限',
  139. right: true,
  140. required: true
  141. }
  142. ],
  143. userForm: {
  144. account: '',
  145. name: '',
  146. phone: '',
  147. email: '',
  148. job: '',
  149. dep: '',
  150. comp: '',
  151. rightCode: []
  152. },
  153. userRules: {}
  154. }
  155. },
  156. computed: {
  157. readonlyShow() {
  158. if (this.operateM.type === 'right' || this.operateM.type === 'view') {
  159. return true
  160. } else {
  161. return false
  162. }
  163. },
  164. viewShow() {
  165. return this.operateM.type === 'view' || false
  166. },
  167. activeShow() {
  168. return this.operateM.type === 'edit' || false
  169. },
  170. timeShow() {
  171. return this.operateM.type !== 'add' || false
  172. }
  173. },
  174. watch: {
  175. userDialogVisible(val) {
  176. !val && setTimeout(() => {
  177. if (!this.operateM.type === 'right') {
  178. this.$refs['userForm'].resetFields()
  179. }
  180. this.$refs['userForm'].clearValidate()
  181. }, 0)
  182. }
  183. },
  184. created() {
  185. this.pushRulesFn()
  186. },
  187. methods: {
  188. getUserDetail(id) {
  189. var that = this
  190. that.userId = id
  191. if (that.userId) {
  192. that.$http.get(queryUserOne, {
  193. id: that.userId
  194. }, function(res) {
  195. if (res.success) {
  196. const opeRow = res.data
  197. if (opeRow.createTime) {
  198. that.createTime = parseTime(opeRow.createTime)
  199. }
  200. that.actived = opeRow.actived
  201. that.oldAccount = opeRow.account
  202. that.userForm = {
  203. account: opeRow.account,
  204. name: opeRow.name,
  205. phone: opeRow.phone,
  206. email: opeRow.email,
  207. job: opeRow.job,
  208. dep: opeRow.dep,
  209. comp: opeRow.comp,
  210. rightCode: opeRow.rightCode ? opeRow.rightCode : []
  211. }
  212. }
  213. })
  214. } else {
  215. this.actived = 0
  216. this.createTime = ''
  217. this.userForm = {
  218. account: '',
  219. name: '',
  220. phone: '',
  221. email: '',
  222. job: '',
  223. dep: '',
  224. comp: '',
  225. rightCode: []
  226. }
  227. }
  228. setTimeout(function() {
  229. that.userDialogVisible = true
  230. }, 0)
  231. },
  232. pushRulesFn() {
  233. const formItem = this.formItem
  234. const userRules = this.userRules
  235. for (let i = 0; i < formItem.length; ++i) {
  236. const ru = []
  237. if (formItem[i].required) {
  238. ru.push({ required: true, message: requiredTip(formItem[i].tit), trigger: 'blur' })
  239. }
  240. if (formItem[i].num) {
  241. ru.push({ max: formItem[i].num, message: limitNumTip(formItem[i].tit, formItem[i].num), trigger: 'blur, change' })
  242. }
  243. switch (formItem[i].prop) {
  244. case 'email':
  245. ru.push({ validator: checkEmailV, trigger: 'blur' })
  246. break
  247. }
  248. userRules[formItem[i].prop] = ru
  249. }
  250. },
  251. isExistUserFn(fn) {
  252. var that = this
  253. var opeRow = that.userForm
  254. this.$http.get(existUser, {
  255. account: opeRow.account
  256. }, function(res) {
  257. if (res.success) {
  258. if (res.data) {
  259. that.$message({
  260. message: '账户已存在,请重新输入',
  261. type: 'warning'
  262. })
  263. return false
  264. } else {
  265. fn()
  266. }
  267. }
  268. })
  269. },
  270. rightCodeShow(code) {
  271. if (!code) {
  272. return
  273. }
  274. var codeShow = []
  275. for (let i = 0; i < code.length; ++i) {
  276. const nowC = this.moduleOption.find((item) => {
  277. return item.id === code[i]
  278. })
  279. codeShow.push(nowC.name)
  280. }
  281. return codeShow.join(',')
  282. },
  283. submitForm(formName) {
  284. var that = this
  285. this.$refs[formName].validate((valid) => {
  286. if (valid) {
  287. var opeRow = that.userForm
  288. var paramsData = {
  289. account: opeRow.account,
  290. name: opeRow.name,
  291. phone: opeRow.phone,
  292. email: opeRow.email,
  293. job: opeRow.job,
  294. dep: opeRow.dep,
  295. comp: opeRow.comp,
  296. rightCode: opeRow.rightCode
  297. }
  298. if (that.operateM.type === 'add') {
  299. that.isExistUserFn(function() {
  300. that.$http.post(addUser, paramsData, function(res) {
  301. if (res.success) {
  302. that.successFun()
  303. }
  304. })
  305. })
  306. } else if (that.operateM.type === 'edit') {
  307. const paramsId = { id: that.userId }
  308. const obj = Object.assign(paramsId, paramsData)
  309. if (that.oldAccount === opeRow.account) {
  310. that.$http.put(updateUser, obj, function(res) {
  311. if (res.success) {
  312. that.successFun()
  313. }
  314. })
  315. } else {
  316. that.isExistUserFn(function() {
  317. that.$http.put(updateUser, obj, function(res) {
  318. if (res.success) {
  319. that.successFun()
  320. }
  321. })
  322. })
  323. }
  324. } else if (that.operateM.type === 'right') {
  325. that.$http.put(updatePermission + that.userId, opeRow.rightCode, function(res) {
  326. if (res.success) {
  327. that.successFun()
  328. }
  329. })
  330. }
  331. } else {
  332. return false
  333. }
  334. })
  335. },
  336. successFun() {
  337. this.userDialogVisible = false
  338. this.$refs['userForm'].resetFields()
  339. this.$parent.dialogChanged()
  340. },
  341. handleCheckedChange(value) {
  342. this.userForm.rightCode = value
  343. }
  344. }
  345. }
  346. </script>
  347. <style rel="stylesheet/scss" lang="scss">
  348. .el-card__header{
  349. padding: 8px 15px;
  350. .card-title{
  351. font-size: 18px;
  352. font-weight: bold;
  353. span:before{
  354. content: '*';
  355. color: #f56c6c;
  356. margin-right: 4px;
  357. }
  358. }
  359. }
  360. .el-checkbox-group{
  361. .el-checkbox{
  362. font-size: 16px;
  363. margin: 10px 60px;
  364. .el-checkbox__inner{
  365. width: 16px;
  366. height: 16px;
  367. }
  368. .el-checkbox__inner::after{
  369. left:5px;
  370. top:2px;
  371. }
  372. .el-checkbox__label{
  373. line-height:24px;
  374. font-size:16px;
  375. }
  376. }
  377. }
  378. </style>