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

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