木邻有你前端代码仓库
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.

411 lines
9.4 KiB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
  1. <template>
  2. <view class="user-info-container">
  3. <!-- 自定义导航栏 -->
  4. <!-- <view class="custom-navbar">
  5. <view class="navbar-content">
  6. <view class="navbar-title">申请获取您的头像昵称</view>
  7. </view>
  8. </view> -->
  9. <!-- 内容区域 -->
  10. <view class="content">
  11. <!-- 头像区域 -->
  12. <view class="avatar-section">
  13. <view class="app-info">
  14. <image class="app-logo" src="/static/logo.png" mode="aspectFit"></image>
  15. <text class="app-name">木邻有你</text>
  16. </view>
  17. </view>
  18. <!-- 表单区域 -->
  19. <view class="form-section">
  20. <!-- 头像 -->
  21. <!-- 在模板中修改头像区域 -->
  22. <view class="form-item">
  23. <text class="form-label">头像</text>
  24. <view class="avatar-upload">
  25. <button
  26. class="avatar-button"
  27. open-type="chooseAvatar"
  28. @chooseavatar="onChooseAvatar"
  29. >
  30. <image
  31. class="avatar-image"
  32. :src="userInfo.headImage || '/static/待上传头像.png'"
  33. mode="aspectFill"
  34. ></image>
  35. </button>
  36. </view>
  37. </view>
  38. <!-- 昵称 -->
  39. <view class="form-item">
  40. <text class="form-label">昵称</text>
  41. <input
  42. class="form-input"
  43. v-model="userInfo.nickName"
  44. placeholder="请输入昵称"
  45. type="nickname"
  46. @blur="onNicknameBlur"
  47. />
  48. </view>
  49. <!-- 手机号 -->
  50. <view class="form-item">
  51. <text class="form-label">手机号</text>
  52. <view class="phone-input-container">
  53. <input
  54. class="form-input phone-input"
  55. v-model="userInfo.phone"
  56. placeholder="请输入手机号"
  57. type="number"
  58. maxlength="11"
  59. />
  60. <button
  61. class="get-phone-btn"
  62. open-type="getPhoneNumber"
  63. @getphonenumber="getPhoneNumber"
  64. >
  65. <text class="btn-text">获取手机号</text>
  66. </button>
  67. </view>
  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. </view>
  78. </template>
  79. <script>
  80. export default {
  81. name: 'UserInfo',
  82. data() {
  83. return {
  84. userInfo: {
  85. headImage: '',
  86. nickName: '',
  87. phone: ''
  88. }
  89. }
  90. },
  91. onLoad() {
  92. // 获取微信用户信息
  93. this.getWechatUserInfo();
  94. },
  95. methods: {
  96. // 获取微信用户信息
  97. async getWechatUserInfo() {
  98. const { result } = await this.$api.user.queryUser()
  99. this.userInfo.nickName = result.nickName
  100. this.userInfo.headImage = result.headImage
  101. this.userInfo.phone = result.phone
  102. },
  103. // 提交表单
  104. // 选择头像并上传到OSS
  105. async onChooseAvatar(e) {
  106. console.log('选择头像回调', e);
  107. if (e.detail.avatarUrl) {
  108. try {
  109. // 显示上传中提示
  110. uni.showLoading({ title: '上传头像中...' });
  111. // 构造文件对象
  112. const file = {
  113. path: e.detail.avatarUrl,
  114. tempFilePath: e.detail.avatarUrl
  115. };
  116. // 上传到OSS
  117. const uploadResult = await this.$utils.uploadImage(file);
  118. uni.hideLoading();
  119. if (uploadResult.success) {
  120. // 上传成功,更新头像URL
  121. this.userInfo.headImage = uploadResult.url;
  122. console.log('头像上传成功', uploadResult.url);
  123. uni.showToast({
  124. title: '头像上传成功',
  125. icon: 'success'
  126. });
  127. } else {
  128. // 上传失败,使用本地头像
  129. this.userInfo.headImage = e.detail.avatarUrl;
  130. uni.showToast({
  131. title: '头像上传失败,使用本地头像',
  132. icon: 'none'
  133. });
  134. }
  135. } catch (error) {
  136. uni.hideLoading();
  137. console.error('头像上传异常:', error);
  138. // 异常情况下使用本地头像
  139. this.userInfo.headImage = e.detail.avatarUrl;
  140. uni.showToast({
  141. title: '头像处理异常,使用本地头像',
  142. icon: 'none'
  143. });
  144. }
  145. } else {
  146. uni.showToast({
  147. title: '头像选择失败',
  148. icon: 'none'
  149. });
  150. }
  151. },
  152. // 昵称输入失焦
  153. onNicknameBlur() {
  154. if (!this.userInfo.nickname.trim()) {
  155. uni.showToast({
  156. title: '请输入昵称',
  157. icon: 'none'
  158. });
  159. }
  160. },
  161. // 获取手机号
  162. getPhoneNumber(e) {
  163. console.log('获取手机号回调', e);
  164. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  165. // 获取成功,可以通过e.detail.code发送到后端换取手机号
  166. console.log('获取手机号成功', e.detail);
  167. uni.showToast({
  168. title: '手机号获取成功',
  169. icon: 'success'
  170. });
  171. // 这里需要将e.detail.code发送到后端解密获取真实手机号
  172. // 暂时模拟设置手机号
  173. // this.userInfo.phone = '138****8888';
  174. } else {
  175. console.log('获取手机号失败', e.detail);
  176. uni.showToast({
  177. title: '获取手机号失败',
  178. icon: 'none'
  179. });
  180. }
  181. },
  182. // 提交用户信息
  183. async submitUserInfo() {
  184. if (!this.userInfo.nickName.trim()) {
  185. uni.showToast({
  186. title: '请输入昵称',
  187. icon: 'none'
  188. });
  189. return;
  190. }
  191. if (!this.userInfo.phone.trim()) {
  192. uni.showToast({
  193. title: '请输入手机号',
  194. icon: 'none'
  195. });
  196. return;
  197. }
  198. if (!/^1[3-9]\d{9}$/.test(this.userInfo.phone)) {
  199. uni.showToast({
  200. title: '请输入正确的手机号',
  201. icon: 'none'
  202. });
  203. return;
  204. }
  205. console.log('提交用户信息', this.userInfo);
  206. // 提交用户信息
  207. await this.$api.user.updateUser({
  208. nickName: this.userInfo.nickName,
  209. phone: this.userInfo.phone,
  210. headImage: this.userInfo.headImage
  211. })
  212. // 这里可以调用API保存用户信息
  213. uni.showToast({
  214. title: '信息保存成功',
  215. icon: 'success'
  216. });
  217. // 跳转到首页或其他页面
  218. setTimeout(() => {
  219. uni.switchTab({
  220. url: '/pages/index/index'
  221. });
  222. }, 1500);
  223. }
  224. }
  225. }
  226. </script>
  227. <style lang="scss" scoped>
  228. .user-info-container {
  229. min-height: 100vh;
  230. background-color: #f5f5f5;
  231. }
  232. .custom-navbar {
  233. position: fixed;
  234. top: 0;
  235. left: 0;
  236. right: 0;
  237. z-index: 1000;
  238. background-color: #1488DB;
  239. .navbar-content {
  240. height: 88rpx;
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. padding-top: var(--status-bar-height, 44rpx);
  245. .navbar-title {
  246. font-size: 36rpx;
  247. font-weight: 500;
  248. color: #ffffff;
  249. }
  250. }
  251. }
  252. .content {
  253. padding-top: calc(88rpx + var(--status-bar-height, 44rpx));
  254. padding: calc(88rpx + var(--status-bar-height, 44rpx)) 40rpx 40rpx;
  255. }
  256. .avatar-section {
  257. display: flex;
  258. justify-content: center;
  259. margin-bottom: 80rpx;
  260. .app-info {
  261. display: flex;
  262. flex-direction: column;
  263. align-items: center;
  264. .app-logo {
  265. width: 160rpx;
  266. height: 160rpx;
  267. border-radius: 20rpx;
  268. border: 4rpx dashed #cccccc;
  269. margin-bottom: 20rpx;
  270. }
  271. .app-name {
  272. font-size: 32rpx;
  273. font-weight: 500;
  274. color: #333333;
  275. }
  276. }
  277. }
  278. .form-section {
  279. background-color: #ffffff;
  280. border-radius: 20rpx;
  281. padding: 40rpx;
  282. margin-bottom: 60rpx;
  283. }
  284. .form-item {
  285. display: flex;
  286. align-items: center;
  287. margin-bottom: 40rpx;
  288. &:last-child {
  289. margin-bottom: 0;
  290. }
  291. .form-label {
  292. width: 120rpx;
  293. font-size: 32rpx;
  294. color: #333333;
  295. font-weight: 500;
  296. }
  297. .form-input {
  298. flex: 3;
  299. height: 80rpx;
  300. padding: 0 20rpx;
  301. // border: 2rpx solid #e0e0e0;
  302. border-radius: 10rpx;
  303. font-size: 30rpx;
  304. color: #333333;
  305. &.phone-input {
  306. margin-right: 20rpx;
  307. }
  308. }
  309. .avatar-upload {
  310. .avatar-image {
  311. width: 120rpx;
  312. height: 120rpx;
  313. border-radius: 10rpx;
  314. border: 2rpx dashed #cccccc;
  315. }
  316. }
  317. .phone-input-container {
  318. flex: 1;
  319. display: flex;
  320. align-items: center;
  321. .get-phone-btn {
  322. flex: 2;
  323. // padding: 0rpx 0rpx;
  324. background-color: #1488DB;
  325. border-radius: 40rpx;
  326. border: none;
  327. outline: none;
  328. &::after {
  329. border: none;
  330. }
  331. .btn-text {
  332. font-size: 26rpx;
  333. color: #ffffff;
  334. }
  335. }
  336. }
  337. }
  338. .submit-section {
  339. padding: 0 40rpx;
  340. .submit-btn {
  341. width: 100%;
  342. height: 88rpx;
  343. background-color: #1488DB;
  344. border-radius: 44rpx;
  345. display: flex;
  346. align-items: center;
  347. justify-content: center;
  348. .submit-text {
  349. font-size: 32rpx;
  350. font-weight: 500;
  351. color: #ffffff;
  352. }
  353. }
  354. }
  355. // 添加按钮样式
  356. .avatar-button {
  357. padding: 0;
  358. margin: 0;
  359. background: transparent;
  360. border: none;
  361. outline: none;
  362. &::after {
  363. border: none;
  364. }
  365. }
  366. </style>