edit.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div class="app-container">
  3. <header>
  4. <img :src="userInfos.avatar" alt="" />
  5. <div class="content">
  6. <p class="bold">
  7. <van-text-ellipsis :content="`${userInfos.nickName}-${userInfos.userRoleNames}`" />
  8. </p>
  9. <p>
  10. <van-text-ellipsis :content="userInfos.pgName" />
  11. </p>
  12. <p>
  13. <van-text-ellipsis :content="userInfos.deptName" />
  14. </p>
  15. </div>
  16. </header>
  17. <van-form ref="formRef" @submit="onSubmit" class="mt10" required="auto">
  18. <van-cell-group>
  19. <van-field v-model="state.form.nickName" label="用户昵称" placeholder="用户昵称" :rules="[{ required: true, message: '请输入用户昵称' }]" />
  20. <van-field v-model="state.form.phone" label="手机号" placeholder="手机号"> </van-field>
  21. <van-field v-model="state.form.email" label="电子邮箱" placeholder="电子邮箱" />
  22. <van-field v-model="state.form.sex" label="性别" placeholder="性别">
  23. <template #input>
  24. <van-radio-group v-model="state.form.sex" direction="horizontal">
  25. <van-radio v-for="item in userSexList" :key="item.id" :name="item.dictValue">{{ item.dictLabel }}</van-radio>
  26. </van-radio-group>
  27. </template>
  28. </van-field>
  29. <van-field v-model="state.form.pgName" label="课题组" placeholder="课题组" readonly></van-field>
  30. </van-cell-group>
  31. <div style="margin: 16px">
  32. <van-button round block type="primary" native-type="submit"> 保存 </van-button>
  33. </div>
  34. </van-form>
  35. </div>
  36. </template>
  37. <script lang="ts" setup>
  38. import { storeToRefs } from 'pinia'
  39. import { useUserInfo } from '/@/stores/userInfo'
  40. import { Local } from '/@/utils/storage'
  41. import { showConfirmDialog, showNotify } from 'vant'
  42. import { useRouter } from 'vue-router'
  43. import { onMounted, reactive, ref } from 'vue'
  44. import { useUserApi } from '/@/api/system/user'
  45. import to from 'await-to-js'
  46. import { useDictApi } from '/@/api/system/dict'
  47. const router = useRouter()
  48. const storesUseUserInfo = useUserInfo()
  49. const { userInfos } = storeToRefs(storesUseUserInfo)
  50. const userApi = useUserApi()
  51. const formRef = ref()
  52. const dictApi = useDictApi()
  53. const userSexList = ref(<RowDicDataType[]>[])
  54. const state = reactive({
  55. form: {
  56. id: 0,
  57. userId: 0, // 用户账号
  58. nickName: '', // 用户昵称
  59. phone: '', // 手机号
  60. email: '', // 电子邮件
  61. sex: '', // 性别
  62. avatar: '', // 头像图片地址
  63. pgName: ''
  64. },
  65. dialog: {
  66. isShowDialog: false,
  67. type: '',
  68. title: '',
  69. submitTxt: ''
  70. }
  71. })
  72. const getDicts = () => {
  73. Promise.all([dictApi.getDictDataByType('sys_com_sex')]).then(([sex]) => {
  74. userSexList.value = sex.data.values || []
  75. })
  76. }
  77. const onClickRight = () => {
  78. router.go(-1)
  79. }
  80. const initForm = async () => {
  81. const [err, res]: ToResponse = await to(userApi.getProfile())
  82. if (err) return
  83. state.form = res?.data
  84. }
  85. const onSubmit = async () => {
  86. const [errValid] = await to(formRef.value.validate())
  87. if (errValid) return
  88. state.form.userId = state.form.id
  89. const params = JSON.parse(JSON.stringify(state.form))
  90. const [err]: ToResponse = await to(userApi.updateProfile(params))
  91. if (err) return
  92. showNotify({
  93. type: 'success',
  94. message: '修改成功'
  95. })
  96. storesUseUserInfo.setUserInfos()
  97. router.push('/user')
  98. }
  99. onMounted(() => {
  100. getDicts()
  101. initForm()
  102. })
  103. </script>
  104. <style lang="scss" scoped>
  105. .app-container {
  106. header {
  107. background-color: #1c9bfd;
  108. color: #fff;
  109. padding: 10px;
  110. border-radius: 8px;
  111. margin-top: 10px;
  112. display: flex;
  113. img {
  114. width: 100px;
  115. height: 100px;
  116. border-radius: 50%;
  117. margin-right: 10px;
  118. }
  119. .content {
  120. flex: 1;
  121. display: flex;
  122. flex-direction: column;
  123. justify-content: space-around;
  124. overflow: hidden;
  125. .bold {
  126. display: flex;
  127. align-items: center;
  128. font-weight: bold;
  129. // flex-wrap: nowrap;
  130. white-space: nowrap;
  131. .van-text-ellipsis {
  132. flex: 1;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. </style>