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

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