四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

473 lines
11 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="user-info-container" :style="{ minHeight: containerHeight + 'rpx' }">
  3. <!-- 内容区域 -->
  4. <view class="content">
  5. <!-- 头像区域 -->
  6. <view class="avatar-section">
  7. <view class="app-info">
  8. <image class="app-logo" :src="configParamImage('app_logo')" mode="aspectFit"></image>
  9. <text class="app-name">{{ configParamText('app_name') }}</text>
  10. <text class="app-subname">申请获取您的头像昵称</text>
  11. </view>
  12. </view>
  13. <!-- 表单区域 -->
  14. <view class="form-section">
  15. <!-- 头像 -->
  16. <view class="form-item">
  17. <text class="form-label">头像</text>
  18. <button class="avatar-button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  19. <image
  20. class="avatar-image"
  21. :src="userInfo.headImage || '/static/默认图片.png'"
  22. mode="aspectFill"
  23. ></image>
  24. </button>
  25. </view>
  26. <!-- 昵称 -->
  27. <view class="form-item">
  28. <text class="form-label">昵称</text>
  29. <input
  30. class="form-input"
  31. type="nickname"
  32. v-model="userInfo.nickName"
  33. placeholder="请输入昵称"
  34. @blur="onNicknameBlur"
  35. />
  36. </view>
  37. <!-- 手机号 -->
  38. <!-- <view class="form-item">
  39. <text class="form-label">手机号</text>
  40. <view class="phone-container" v-if="!userInfo.phone">
  41. <button
  42. class="get-phone-btn"
  43. open-type="getPhoneNumber"
  44. @getphonenumber="getPhoneNumber"
  45. >
  46. 获取手机号
  47. </button>
  48. </view>
  49. <text class="form-label" v-else>{{ userInfo.phone }}</text>
  50. </view>
  51. -->
  52. <!-- 所在部门 -->
  53. <!-- <view class="form-item">
  54. <text class="form-label">所在部门</text>
  55. <view class="department-container" @click="showDepartmentPicker">
  56. <text class="department-text" :class="{ 'active': userInfo.department.title }">{{ userInfo.department.title || '请选择' }}</text>
  57. <uv-icon name="arrow-down" size="20" color="#000"></uv-icon>
  58. </view>
  59. </view> -->
  60. <!-- 备注 -->
  61. <!-- <view class="form-item ">
  62. <text class="form-label">备注<text class="form-remark">(非必填)</text></text>
  63. <input
  64. class="form-input"
  65. v-model="userInfo.remark"
  66. />
  67. </view> -->
  68. </view>
  69. <!-- 确定按钮 -->
  70. <view class="submit-section">
  71. <view class="submit-btn" @click="submitUserInfo">
  72. <text class="submit-text">确定</text>
  73. </view>
  74. </view>
  75. </view>
  76. <!-- 部门选择器 -->
  77. <uv-picker
  78. confirm-color="#C70019"
  79. ref="departmentPicker"
  80. :columns="departmentColumns"
  81. keyName="title"
  82. @confirm="confirmDepartment"
  83. @cancel="cancelDepartment"
  84. ></uv-picker>
  85. </view>
  86. </template>
  87. <script>
  88. export default {
  89. name: 'UserInfo',
  90. data() {
  91. return {
  92. userInfo: {
  93. headImage: '',
  94. nickName: '',
  95. phone: '',
  96. department: {
  97. title: '',
  98. id: ''
  99. },
  100. remark: ''
  101. },
  102. showDepartmentSheet: false,
  103. // 部门选择器数据
  104. }
  105. },
  106. async onLoad() {
  107. this.calculateContainerHeight();
  108. // await this.getUserInfo();
  109. },
  110. computed: {
  111. // 部门选择器数据格式转换
  112. departmentColumns() {
  113. if (!this.$store.state.departmentList || this.$store.state.departmentList.length === 0) {
  114. return [[]]
  115. }
  116. const departmentNames = this.$store.state.departmentList.map(item => {
  117. return {
  118. title: item.title,
  119. id: item.id
  120. }
  121. })
  122. return [departmentNames]
  123. }
  124. },
  125. methods: {
  126. // 获取微信用户信息
  127. async getWechatUserInfo() {
  128. const { result } = await this.$api.user.queryUser()
  129. this.userInfo.nickName = result.nickName
  130. this.userInfo.headImage = result.headImage
  131. this.userInfo.phone = result.phone
  132. },
  133. // 提交表单
  134. async getUserInfo(){
  135. const { result:info } = await this.$api.user.queryUser()
  136. this.userInfo.nickName = info.nickName
  137. this.userInfo.headImage = info.headImage
  138. this.userInfo.phone = info.phone
  139. },
  140. // 选择头像并上传到OSS
  141. async onChooseAvatar(e) {
  142. console.log('选择头像回调', e);
  143. if (e.detail.avatarUrl) {
  144. try {
  145. // 显示上传中提示
  146. uni.showLoading({ title: '上传头像中...' });
  147. // 构造文件对象
  148. const file = {
  149. path: e.detail.avatarUrl,
  150. tempFilePath: e.detail.avatarUrl
  151. };
  152. // 上传到OSS
  153. const uploadResult = await this.$utils.uploadImage(file);
  154. uni.hideLoading();
  155. if (uploadResult.success) {
  156. // 上传成功,更新头像URL
  157. this.userInfo.headImage = uploadResult.url;
  158. console.log('头像上传成功', uploadResult.url);
  159. uni.showToast({
  160. title: '头像上传成功',
  161. icon: 'success'
  162. });
  163. } else {
  164. }
  165. } catch (error) {
  166. uni.hideLoading();
  167. console.error('头像上传异常:', error);
  168. // 异常情况下使用本地头像
  169. this.userInfo.headImage = e.detail.avatarUrl;
  170. uni.showToast({
  171. title: '头像处理异常,使用本地头像',
  172. icon: 'none'
  173. });
  174. }
  175. } else {
  176. uni.showToast({
  177. title: '头像选择失败',
  178. icon: 'none'
  179. });
  180. }
  181. },
  182. // 昵称输入失焦
  183. onNicknameBlur() {
  184. if (!this.userInfo.nickName.trim()) {
  185. uni.showToast({
  186. title: '请输入昵称',
  187. icon: 'none'
  188. });
  189. }
  190. },
  191. // 显示部门选择器
  192. showDepartmentPicker() {
  193. this.$refs.departmentPicker.open();
  194. },
  195. // 确认部门选择
  196. confirmDepartment(e) {
  197. this.userInfo.department = e.value[0];
  198. },
  199. // 取消部门选择
  200. cancelDepartment() {
  201. // 取消操作
  202. },
  203. // 选择部门(保留原方法以防其他地方使用)
  204. selectDepartment(item) {
  205. this.userInfo.department = item.value;
  206. this.showDepartmentSheet = false;
  207. },
  208. // 获取手机号
  209. async getPhoneNumber(e) {
  210. console.log('获取手机号回调', e);
  211. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  212. // 获取成功,可以通过e.detail.code发送到后端换取手机号
  213. console.log('获取手机号成功', e.detail);
  214. const res = await this.$api.login.bindPhone({
  215. phoneCode: e.detail.code
  216. })
  217. const str = JSON.parse(res.result);
  218. this.userInfo.phone = str.phone_info.phoneNumber
  219. uni.showToast({
  220. title: '手机号获取成功',
  221. icon: 'success'
  222. });
  223. } else {
  224. // 如果失败了就申请开启权限
  225. uni.showToast({
  226. title: '手机号获取失败',
  227. icon: 'error'
  228. })
  229. }
  230. },
  231. // 提交用户信息
  232. async submitUserInfo() {
  233. if (!this.userInfo.nickName.trim()) {
  234. uni.showToast({
  235. title: '请输入昵称',
  236. icon: 'none'
  237. });
  238. return;
  239. }
  240. if (!this.userInfo.phone.trim()) {
  241. uni.showToast({
  242. title: '请获取手机号',
  243. icon: 'none'
  244. });
  245. return;
  246. }
  247. if (!this.userInfo.department.title.trim()) {
  248. uni.showToast({
  249. title: '请选择所在部门',
  250. icon: 'none'
  251. });
  252. return;
  253. }
  254. console.log('提交用户信息', this.userInfo);
  255. try {
  256. // 提交用户信息
  257. await this.$api.user.updateUser({
  258. nickName: this.userInfo.nickName,
  259. phone: this.userInfo.phone,
  260. headImage: this.userInfo.headImage,
  261. department: this.userInfo.department.id,
  262. remark: this.userInfo.remark
  263. });
  264. uni.showToast({
  265. title: '信息保存成功',
  266. icon: 'success'
  267. });
  268. // 跳转到首页
  269. setTimeout(() => {
  270. uni.switchTab({
  271. url: '/pages/index/home'
  272. });
  273. }, 1000);
  274. } catch (error) {
  275. console.error('保存用户信息失败:', error);
  276. uni.showToast({
  277. title: '保存失败,请重试',
  278. icon: 'none'
  279. });
  280. }
  281. }
  282. }
  283. }
  284. </script>
  285. <style lang="scss" scoped>
  286. .user-info-container {
  287. background-color: #fff;
  288. box-sizing: border-box;
  289. padding-top: 200rpx;
  290. }
  291. .content {
  292. padding: 0 66rpx;
  293. }
  294. .avatar-section {
  295. display: flex;
  296. justify-content: center;
  297. margin-bottom: 90rpx;
  298. margin-top: 192rpx;
  299. .app-info {
  300. display: flex;
  301. flex-direction: column;
  302. align-items: center;
  303. gap: 22rpx;
  304. .app-logo {
  305. width: 92rpx;
  306. height: 92rpx;
  307. // border-radius: 20rpx;
  308. // margin-bottom: 23rpx;
  309. }
  310. .app-name {
  311. font-size: 36rpx;
  312. font-weight: bold;
  313. color: $primary-text-color;
  314. }
  315. .app-subname {
  316. font-size: 30rpx;
  317. color: $primary-text-color;
  318. }
  319. }
  320. }
  321. .form-item {
  322. // background: green;
  323. display: flex;
  324. align-items: center;
  325. // margin-bottom: 30rpx;
  326. height: 114rpx;
  327. justify-content: space-between;
  328. &:last-child {
  329. margin-bottom: 45rpx;
  330. }
  331. .form-label {
  332. // width: 120rpx;
  333. // flex: 1;
  334. font-size: 32rpx;
  335. color: $primary-text-color;
  336. // font-weight: 500;
  337. }
  338. .form-input {
  339. // flex: 1;
  340. // height: 80rpx;
  341. // padding: 0 20rpx;
  342. // border-radius: 10rpx;
  343. font-size: 30rpx;
  344. color: $primary-text-color;
  345. border: none;
  346. background-color: transparent;
  347. text-align: right;
  348. }
  349. .form-remark {
  350. margin-left: 16rpx;
  351. color: $secondary-text-color;
  352. }
  353. .department-container{
  354. display: flex;
  355. align-items: center;
  356. gap: 22rpx;
  357. color: $secondary-text-color;
  358. .department-text.active {
  359. color: $primary-text-color;
  360. }
  361. }
  362. }
  363. // 头像按钮
  364. .avatar-button {
  365. margin: 0;
  366. padding: 0;
  367. background: transparent;
  368. border: none;
  369. width: 100rpx;
  370. height: 100rpx;
  371. border: 2rpx solid #cccccc;
  372. &::after {
  373. border: none;
  374. }
  375. .avatar-image {
  376. width: 100rpx;
  377. height: 100rpx;
  378. // border-radius: 10rpx;
  379. }
  380. }
  381. // background: white;
  382. .get-phone-btn {
  383. // margin-left: auto;
  384. width: 176rpx;
  385. height: 76rpx;
  386. background-color: $primary-color;
  387. color: white;
  388. border-radius: 38rpx;
  389. font-size: 22rpx;
  390. line-height: 76rpx;
  391. border: none;
  392. &::after {
  393. border: none;
  394. }
  395. &:active {
  396. opacity: 0.8;
  397. }
  398. }
  399. // }
  400. .submit-section {
  401. width: 594rpx;
  402. height: 94rpx;
  403. margin: 0 auto;
  404. .submit-btn {
  405. width: 100%;
  406. height: 94rpx;
  407. background-color: $primary-color;
  408. border-radius: 41rpx;
  409. display: flex;
  410. align-items: center;
  411. justify-content: center;
  412. .submit-text {
  413. font-size: 32rpx;
  414. // font-weight: 500;
  415. color: #ffffff;
  416. }
  417. }
  418. }
  419. </style>