四零语境前端代码仓库
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.

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