userCenter.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <div class="sys-userCenter-container">
  3. <el-row :gutter="5" style="width: 100%">
  4. <el-col :span="8" :xs="24">
  5. <el-card shadow="hover">
  6. <div class="account-center-avatarHolder">
  7. <!-- <el-upload class="h100" ref="uploadAvatarRef" action="" :limit="1" :show-file-list="false" :auto-upload="false" :on-change="uploadAvatarFile" accept=".jpg,.png,.bmp,.gif">
  8. <el-avatar :size="100" :src="userInfos.avatar" />
  9. </el-upload> -->
  10. <el-avatar
  11. :size="100"
  12. :src="userInfos.avatar"
  13. @click="openCropperDialog"
  14. v-loading="state.avatarLoading"
  15. element-loading-spinner="el-icon-Upload"
  16. element-loading-background="rgba(0, 0, 0, 0.2)"
  17. @mouseenter="mouseEnterAvatar"
  18. @mouseleave="mouseLeaveAvatar"
  19. />
  20. <div class="username">{{ userInfos.realName }}</div>
  21. </div>
  22. <div class="account-center-org">
  23. <p>
  24. <el-icon><ele-School /></el-icon> <span>{{ userInfos.orgName ?? '超级管理员' }}</span>
  25. </p>
  26. <p>
  27. <el-icon><ele-Mug /></el-icon> <span>{{ userInfos.posName ?? '超级管理员' }}</span>
  28. </p>
  29. <p>
  30. <el-icon><ele-LocationInformation /></el-icon> <span>{{ userInfos.address ?? '家庭住址' }}</span>
  31. </p>
  32. </div>
  33. <div class="image-signature">
  34. <el-image :src="userInfos.signature" fit="contain" alt="电子签名" loading="lazy" style="width: 100%; height: 100%"> </el-image>
  35. </div>
  36. <el-button icon="ele-Edit" type="primary" @click="openSignDialog" v-auth="'sysFile:uploadSignature'"> 电子签名 </el-button>
  37. <el-upload
  38. ref="uploadSignRef"
  39. action=""
  40. accept=".png"
  41. :limit="1"
  42. :show-file-list="false"
  43. :auto-upload="false"
  44. :on-change="uploadSignFile"
  45. :on-exceed="uploadSignFileExceed"
  46. style="display: inline-block; margin-left: 12px; position: absolute"
  47. >
  48. <el-button icon="ele-UploadFilled" v-auth="'sysFile:uploadSignature'">上传手写签名</el-button>
  49. </el-upload>
  50. </el-card>
  51. </el-col>
  52. <el-col :span="16" :xs="24">
  53. <el-card shadow="hover">
  54. <el-tabs>
  55. <el-tab-pane label="基础信息" v-loading="state.loading">
  56. <el-form :model="state.ruleFormBase" ref="ruleFormBaseRef" label-width="auto">
  57. <el-row :gutter="35">
  58. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  59. <el-form-item label="真实姓名" prop="realName" :rules="[{ required: true, message: '真实姓名不能为空', trigger: 'blur' }]">
  60. <el-input v-model="state.ruleFormBase.realName" placeholder="真实姓名" clearable />
  61. </el-form-item>
  62. </el-col>
  63. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  64. <el-form-item label="昵称">
  65. <el-input v-model="state.ruleFormBase.nickName" placeholder="昵称" clearable />
  66. </el-form-item>
  67. </el-col>
  68. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  69. <el-form-item label="手机号码" prop="phone" :rules="[{ required: true, message: '手机号码不能为空', trigger: 'blur' }]">
  70. <el-input v-model="state.ruleFormBase.phone" placeholder="手机号码" clearable />
  71. </el-form-item>
  72. </el-col>
  73. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  74. <el-form-item label="邮箱">
  75. <el-input v-model="state.ruleFormBase.email" placeholder="邮箱" clearable />
  76. </el-form-item>
  77. </el-col>
  78. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  79. <el-form-item label="出生日期" prop="birthday" :rules="[{ required: true, message: '出生日期不能为空', trigger: 'blur' }]">
  80. <el-date-picker v-model="state.ruleFormBase.birthday" type="date" placeholder="出生日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD" class="w100" />
  81. </el-form-item>
  82. </el-col>
  83. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  84. <el-form-item label="性别">
  85. <el-radio-group v-model="state.ruleFormBase.sex">
  86. <el-radio :value="1">男</el-radio>
  87. <el-radio :value="2">女</el-radio>
  88. </el-radio-group>
  89. </el-form-item>
  90. </el-col>
  91. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
  92. <el-form-item label="地址">
  93. <el-input v-model="state.ruleFormBase.address" placeholder="地址" clearable type="textarea" />
  94. </el-form-item>
  95. </el-col>
  96. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
  97. <el-form-item label="备注">
  98. <el-input v-model="state.ruleFormBase.remark" placeholder="备注" clearable type="textarea" />
  99. </el-form-item>
  100. </el-col>
  101. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
  102. <el-form-item>
  103. <el-button icon="ele-SuccessFilled" type="primary" @click="submitUserBase" v-auth="'sysUser:baseInfo'"> 保存基本信息 </el-button>
  104. </el-form-item>
  105. </el-col>
  106. </el-row>
  107. </el-form>
  108. </el-tab-pane>
  109. <el-tab-pane label="组织机构">
  110. <OrgTree ref="orgTreeRef" />
  111. </el-tab-pane>
  112. <el-tab-pane label="修改密码">
  113. <el-form ref="ruleFormPasswordRef" :model="state.ruleFormPassword" label-width="auto">
  114. <el-form-item label="当前密码" prop="passwordOld" :rules="[{ required: true, message: '当前密码不能为空', trigger: 'blur' }]">
  115. <el-input v-model="state.ruleFormPassword.passwordOld" type="password" autocomplete="off" show-password />
  116. </el-form-item>
  117. <el-form-item label="新密码" prop="passwordNew" :rules="[{ required: true, message: '新密码不能为空', trigger: 'blur' }]">
  118. <el-input v-model="state.ruleFormPassword.passwordNew" type="password" autocomplete="off" show-password />
  119. </el-form-item>
  120. <el-form-item label="确认密码" prop="passwordNew2" :rules="[{ validator: validatePassword, required: true, trigger: 'blur' }]">
  121. <el-input v-model="state.passwordNew2" type="password" autocomplete="off" show-password />
  122. </el-form-item>
  123. <el-form-item>
  124. <el-button icon="ele-Refresh" @click="resetPassword">重 置</el-button>
  125. <el-button icon="ele-SuccessFilled" type="primary" @click="submitPassword" v-auth="'sysUser:changePwd'">确 定</el-button>
  126. </el-form-item>
  127. </el-form>
  128. </el-tab-pane>
  129. </el-tabs>
  130. </el-card>
  131. </el-col>
  132. </el-row>
  133. <el-dialog v-model="state.signDialogVisible" draggable width="600px">
  134. <template #header>
  135. <div style="color: #fff">
  136. <el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-EditPen /> </el-icon>
  137. <span> 电子签名 </span>
  138. </div>
  139. </template>
  140. <div style="border: 1px dashed gray; width: 100%; height: 250px">
  141. <VueSignaturePad ref="signaturePadRef" :options="state.signOptions" style="background-color: #fff" />
  142. </div>
  143. <div style="margin-top: 10px">
  144. <div style="display: inline">画笔粗细:<el-input-number v-model="state.signOptions.minWidth" :min="0.5" :max="2.5" :step="0.1" size="small" /></div>
  145. <div style="display: inline; margin-left: 30px">画笔颜色:<el-color-picker v-model="state.signOptions.penColor" color-format="hex" size="default"> </el-color-picker></div>
  146. </div>
  147. <template #footer>
  148. <span class="dialog-footer">
  149. <el-button @click="unDoSign">撤销</el-button>
  150. <el-button @click="clearSign">清屏</el-button>
  151. <el-button type="primary" @click="saveUploadSign">保存</el-button>
  152. </span>
  153. </template>
  154. </el-dialog>
  155. <CropperDialog ref="cropperDialogRef" :title="state.cropperTitle" @uploadCropperImg="uploadCropperImg" />
  156. </div>
  157. </template>
  158. <script lang="ts" setup name="sysUserCenter">
  159. import { onMounted, watch, reactive, ref } from 'vue';
  160. import { storeToRefs } from 'pinia';
  161. import { ElForm, ElMessageBox, genFileId } from 'element-plus';
  162. import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
  163. import { useUserInfo } from '/@/stores/userInfo';
  164. import { base64ToFile, blobToFile } from '/@/utils/base64Conver';
  165. import OrgTree from '/@/views/system/user/component/orgTree.vue';
  166. import CropperDialog from '/@/components/cropper/index.vue';
  167. import VueGridLayout from 'vue-grid-layout';
  168. import { sm2 } from 'sm-crypto-v2';
  169. import { clearAccessTokens, getAPI } from '/@/utils/axios-utils';
  170. import { SysFileApi, SysUserApi } from '/@/api-services/api';
  171. import { ChangePwdInput, SysUser, SysFile } from '/@/api-services/models';
  172. const stores = useUserInfo();
  173. const { userInfos } = storeToRefs(stores);
  174. const uploadSignRef = ref<UploadInstance>();
  175. //const uploadAvatarRef = ref<UploadInstance>();
  176. const signaturePadRef = ref<InstanceType<typeof VueGridLayout>>();
  177. const ruleFormBaseRef = ref<InstanceType<typeof ElForm>>();
  178. const ruleFormPasswordRef = ref<InstanceType<typeof ElForm>>();
  179. const cropperDialogRef = ref<InstanceType<typeof CropperDialog>>();
  180. const state = reactive({
  181. loading: false,
  182. avatarLoading: false,
  183. signDialogVisible: false,
  184. ruleFormBase: {} as SysUser,
  185. ruleFormPassword: {} as ChangePwdInput,
  186. signOptions: {
  187. penColor: '#000000',
  188. minWidth: 1.0,
  189. onBegin: () => {
  190. signaturePadRef.value.resizeCanvas();
  191. },
  192. },
  193. signFileList: [] as any,
  194. passwordNew2: '',
  195. cropperTitle: '',
  196. });
  197. onMounted(async () => {
  198. state.loading = true;
  199. var res = await getAPI(SysUserApi).apiSysUserBaseInfoGet();
  200. state.ruleFormBase = res.data.result ?? { account: '' };
  201. state.loading = false;
  202. });
  203. watch(state.signOptions, () => {
  204. signaturePadRef.value.signaturePad.penColor = state.signOptions.penColor;
  205. signaturePadRef.value.signaturePad.minWidth = state.signOptions.minWidth;
  206. });
  207. // 上传头像图片
  208. const uploadCropperImg = async (e: any) => {
  209. var res = await getAPI(SysFileApi).apiSysFileUploadAvatarPostForm(blobToFile(e.img, userInfos.value.account + '.png'));
  210. userInfos.value.avatar = getFileUrl(res.data.result!);
  211. state.ruleFormBase.avatar = userInfos.value.avatar;
  212. };
  213. // 打开电子签名页面
  214. const openSignDialog = () => {
  215. state.signDialogVisible = true;
  216. };
  217. // 保存并上传电子签名
  218. const saveUploadSign = async () => {
  219. const { isEmpty, data } = signaturePadRef.value.saveSignature();
  220. if (isEmpty) {
  221. userInfos.value.signature = null;
  222. state.ruleFormBase.signature = null;
  223. } else {
  224. var res = await getAPI(SysFileApi).apiSysFileUploadSignaturePostForm(base64ToFile(data, userInfos.value.account + '.png'));
  225. userInfos.value.signature = getFileUrl(res.data.result!);
  226. state.ruleFormBase.signature = userInfos.value.signature;
  227. }
  228. clearSign();
  229. state.signDialogVisible = false;
  230. };
  231. // 撤销电子签名
  232. const unDoSign = () => {
  233. signaturePadRef.value.undoSignature();
  234. };
  235. // 清空电子签名
  236. const clearSign = () => {
  237. signaturePadRef.value.clearSignature();
  238. };
  239. // 上传手写电子签名
  240. const uploadSignFile = async (file: any) => {
  241. var res = await getAPI(SysFileApi).apiSysFileUploadSignaturePostForm(file.raw);
  242. userInfos.value.signature = res.data.result?.url;
  243. state.ruleFormBase.signature = userInfos.value.signature;
  244. };
  245. // 获得电子签名文件列表
  246. const handleChangeSignFile = (_file: any, fileList: []) => {
  247. state.signFileList = fileList;
  248. };
  249. // 修改个人信息
  250. const submitUserBase = () => {
  251. ruleFormBaseRef.value?.validate(async (valid: boolean) => {
  252. if (!valid) return;
  253. ElMessageBox.confirm('确定修改个人基础信息?', '提示', {
  254. confirmButtonText: '确定',
  255. cancelButtonText: '取消',
  256. type: 'warning',
  257. }).then(async () => {
  258. await getAPI(SysUserApi).apiSysUserBaseInfoPost(state.ruleFormBase);
  259. });
  260. });
  261. };
  262. // 密码验证
  263. const validatePassword = (_rule: any, value: any, callback: any) => {
  264. if (state.passwordNew2 != state.ruleFormPassword.passwordNew) {
  265. callback(new Error('两次密码不一致!'));
  266. } else {
  267. callback();
  268. }
  269. };
  270. // 密码重置
  271. const resetPassword = () => {
  272. state.ruleFormPassword.passwordOld = '';
  273. state.ruleFormPassword.passwordNew = '';
  274. state.passwordNew2 = '';
  275. };
  276. // 密码提交
  277. const submitPassword = () => {
  278. ruleFormPasswordRef.value?.validate(async (valid: boolean) => {
  279. if (!valid) return;
  280. // SM2加密密码
  281. const cpwd: ChangePwdInput = { passwordOld: '', passwordNew: '' };
  282. const publicKey = window.__env__.VITE_SM_PUBLIC_KEY;
  283. cpwd.passwordOld = sm2.doEncrypt(state.ruleFormPassword.passwordOld, publicKey, 1);
  284. cpwd.passwordNew = sm2.doEncrypt(state.ruleFormPassword.passwordNew, publicKey, 1);
  285. await getAPI(SysUserApi).apiSysUserChangePwdPost(cpwd);
  286. // 退出系统
  287. ElMessageBox.confirm('密码已修改,是否重新登录系统?', '提示', {
  288. confirmButtonText: '确定',
  289. cancelButtonText: '取消',
  290. type: 'warning',
  291. }).then(async () => {
  292. clearAccessTokens();
  293. });
  294. });
  295. };
  296. // 打开裁剪弹窗
  297. const openCropperDialog = () => {
  298. state.cropperTitle = '更换头像';
  299. cropperDialogRef.value?.openDialog(userInfos.value.avatar);
  300. };
  301. // 鼠标进入和离开头像时
  302. const mouseEnterAvatar = () => {
  303. state.avatarLoading = true;
  304. };
  305. const mouseLeaveAvatar = () => {
  306. state.avatarLoading = false;
  307. };
  308. // 上传签名超出数量限制时执行
  309. const uploadSignFileExceed: UploadProps['onExceed'] = (files) => {
  310. uploadSignRef.value!.clearFiles();
  311. const file = files[0] as UploadRawFile;
  312. file.uid = genFileId();
  313. uploadSignRef.value!.handleStart(file);
  314. };
  315. // 获取文件地址
  316. const getFileUrl = (row: SysFile): string => {
  317. if (row.bucketName == 'Local') {
  318. return `/${row.filePath}/${row.id}${row.suffix}`;
  319. } else {
  320. return row.url!;
  321. }
  322. };
  323. // 导出对象
  324. defineExpose({ handleChangeSignFile });
  325. </script>
  326. <style lang="scss" scoped>
  327. .account-center-avatarHolder {
  328. text-align: center;
  329. margin-bottom: 24px;
  330. .username {
  331. font-size: 20px;
  332. line-height: 28px;
  333. font-weight: 500;
  334. margin-bottom: 4px;
  335. }
  336. }
  337. .account-center-org {
  338. margin-bottom: 8px;
  339. position: relative;
  340. p {
  341. margin-top: 10px;
  342. }
  343. span {
  344. padding-left: 10px;
  345. }
  346. }
  347. .avatar {
  348. margin: 0 auto;
  349. width: 104px;
  350. height: 104px;
  351. margin-bottom: 20px;
  352. border-radius: 50%;
  353. overflow: hidden;
  354. img {
  355. height: 100%;
  356. width: 100%;
  357. }
  358. }
  359. .image-signature {
  360. margin-top: 20px;
  361. margin-bottom: 10px;
  362. width: 100%;
  363. height: 150px;
  364. background-color: #fff;
  365. text-align: center;
  366. vertical-align: middle;
  367. border: solid 1px var(--el-border-color);
  368. }
  369. </style>