曹睿
2025-04-25 ec1bef3a37e8dcdf22f1bf52e7c272a18306f4b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<template>
  <view class="profile">
    <view v-if="userProfile" class="profile-card">
      <wd-cell-group border>
        <wd-cell class="avatar-cell" title="头像" center is-link>
          <view class="avatar">
            <view v-if="!userProfile.avatar" class="img" @click="avatarUpload">
              <wd-icon name="fill-camera" custom-class="img-icon" />
            </view>
            <wd-img
              v-if="userProfile.avatar"
              round
              width="80px"
              height="80px"
              :src="userProfile.avatar"
              mode="aspectFit"
              custom-class="profile-img"
              @click="avatarUpload"
            />
          </view>
        </wd-cell>
        <wd-cell title="昵称" :value="userProfile.nickname" is-link @click="handleOpenDialog()" />
        <wd-cell
          title="性别"
          :value="userProfile.gender === 1 ? '男' : userProfile.gender === 2 ? '女' : '未知'"
          is-link
          @click="handleOpenDialog()"
        />
        <wd-cell title="用户名" :value="userProfile.username" />
        <wd-cell title="部门" :value="userProfile.deptName" />
        <wd-cell title="角色" :value="userProfile.roleNames" />
        <wd-cell title="创建日期" :value="userProfile.createTime" />
      </wd-cell-group>
    </view>
 
    <!--头像裁剪-->
    <wd-img-cropper v-model="avatarShow" :img-src="originalSrc" @confirm="handleAvatarConfirm" />
 
    <!--用户信息编辑弹出框-->
    <wd-popup v-model="dialog.visible" position="bottom">
      <wd-form ref="userProfileFormRef" :model="userProfileForm" custom-class="edit-form">
        <wd-cell-group border>
          <wd-input
            v-model="userProfileForm.nickname"
            label="昵称"
            label-width="160rpx"
            placeholder="请输入昵称"
            prop="nickname"
            :rules="rules.nickname"
          />
          <wd-cell title="性别" title-width="160rpx" center prop="gender" :rules="rules.gender">
            <wd-radio-group v-model="userProfileForm.gender" shape="button" class="ef-radio-group">
              <wd-radio :value="1">男</wd-radio>
              <wd-radio :value="2">女</wd-radio>
            </wd-radio-group>
          </wd-cell>
        </wd-cell-group>
        <view class="footer">
          <wd-button type="primary" size="large" block @click="handleSubmit">提交</wd-button>
        </view>
      </wd-form>
    </wd-popup>
  </view>
</template>
<script setup lang="ts">
import UserAPI, { type UserProfileVO, UserProfileForm } from "@/api/system/user";
import FileAPI, { type FileInfo } from "@/api/file";
import { checkLogin } from "@/utils/auth";
 
const originalSrc = ref<string>(""); //选取的原图路径
const avatarShow = ref<boolean>(false); //显示头像裁剪
const userProfile = ref<UserProfileVO>(); //用户信息
 
/** 加载用户信息 */
const loadUserProfile = async () => {
  userProfile.value = await UserAPI.getProfile();
};
 
// 头像选择
function avatarUpload() {
  uni.chooseImage({
    count: 1,
    success: (res) => {
      originalSrc.value = res.tempFilePaths[0];
      avatarShow.value = true;
    },
  });
}
// 头像裁剪完成
function handleAvatarConfirm(event: any) {
  const { tempFilePath } = event;
  FileAPI.upload(tempFilePath).then((fileInfo: FileInfo) => {
    const avatarForm: UserProfileForm = {
      avatar: fileInfo.url,
    };
    // 头像路径保存至后端
    UserAPI.updateProfile(avatarForm).then(() => {
      uni.showToast({ title: "头像上传成功", icon: "none" });
      loadUserProfile();
    });
  });
}
 
// 本页面中所有的校验规则
const rules = reactive({
  nickname: [{ required: true, message: "请填写昵称" }],
  gender: [{ required: true, message: "请选择性别" }],
});
 
const dialog = reactive({
  visible: false,
});
 
const userProfileForm = reactive<UserProfileForm>({});
const userProfileFormRef = ref();
 
/**
 * 打开弹窗
 * @param type 弹窗类型 ACCOUNT: 账号资料 PASSWORD: 修改密码 MOBILE: 绑定手机 EMAIL: 绑定邮箱
 */
const handleOpenDialog = () => {
  dialog.visible = true;
  // 初始化表单数据
  userProfileForm.nickname = userProfile.value?.nickname;
  userProfileForm.gender = userProfile.value?.gender;
};
 
// 提交表单
function handleSubmit() {
  userProfileFormRef.value.validate().then(({ valid }: { valid: boolean }) => {
    if (valid) {
      UserAPI.updateProfile(userProfileForm).then(() => {
        uni.showToast({ title: "账号资料修改成功", icon: "none" });
        dialog.visible = false;
        loadUserProfile();
      });
    }
  });
}
 
// 检查登录状态
onLoad(() => {
  if (!checkLogin()) return;
 
  // #ifdef H5
  document.addEventListener("touchstart", touchstartListener, { passive: false });
  document.addEventListener("touchmove", touchmoveListener, { passive: false });
  // #endif
  loadUserProfile();
});
 
onMounted(() => {
  // 在onMounted中不再重复检查登录状态和加载用户信息
  // 如果需要检查登录状态和加载用户信息,请使用onLoad中的逻辑
});
 
// 页面销毁前移除事件监听
onBeforeUnmount(() => {
  // #ifdef H5
  document.removeEventListener("touchstart", touchstartListener);
  document.removeEventListener("touchmove", touchmoveListener);
  // #endif
});
// 禁用浏览器双指缩放,使头像裁剪时双指缩放能够起作用
function touchstartListener(event: TouchEvent) {
  if (event.touches.length > 1) {
    event.preventDefault();
  }
}
// 禁用浏览器下拉刷新,使头像裁剪时能够移动图片
function touchmoveListener(event: TouchEvent) {
  event.preventDefault();
}
</script>
<style lang="scss" scoped>
.profile {
  .profile-card {
    padding: 10rpx;
    margin-bottom: 24rpx;
    line-height: 1.1;
    background-color: rgb(255, 255, 255);
    border-radius: 16px;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.05);
 
    .avatar-cell {
      :deep(.wd-cell__body) {
        align-items: center;
      }
      .avatar {
        display: flex;
        align-items: center;
        justify-content: right;
        .img {
          position: relative;
          width: 80px;
          height: 80px;
          background-color: rgba(0, 0, 0, 0.04);
          border-radius: 50%;
          .img-icon {
            position: absolute;
            top: 50%;
            left: 50%;
            color: #fff;
          }
        }
      }
    }
  }
  .edit-form {
    padding-top: 40rpx;
    .ef-radio-group {
      line-height: 1;
      text-align: left;
    }
    .footer {
      padding: 24rpx;
    }
  }
}
</style>