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

416 lines
9.6 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 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. async getPhoneNumber(e) {
  163. console.log('获取手机号回调', e);
  164. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  165. // 获取成功,可以通过e.detail.code发送到后端换取手机号
  166. console.log('获取手机号成功', e.detail);
  167. const res = await this.$api.login.bindPhone({
  168. phoneCode: e.detail.code
  169. })
  170. const str = JSON.parse(res.result);
  171. this.userInfo.phone = str.phone_info.phoneNumber
  172. uni.showToast({
  173. title: '手机号获取成功',
  174. icon: 'success'
  175. });
  176. // 这里需要将e.detail.code发送到后端解密获取真实手机号
  177. // 暂时模拟设置手机号
  178. // this.userInfo.phone = '138****8888';
  179. } else {
  180. // 如果失败了就申请开启权限
  181. uni.showToast({
  182. title: '手机号获取失败',
  183. icon: 'error'
  184. })
  185. }
  186. },
  187. // 提交用户信息
  188. async submitUserInfo() {
  189. if (!this.userInfo.nickName.trim()) {
  190. uni.showToast({
  191. title: '请输入昵称',
  192. icon: 'none'
  193. });
  194. return;
  195. }
  196. if (!this.userInfo.phone.trim()) {
  197. uni.showToast({
  198. title: '请输入手机号',
  199. icon: 'none'
  200. });
  201. return;
  202. }
  203. if (!/^1[3-9]\d{9}$/.test(this.userInfo.phone)) {
  204. uni.showToast({
  205. title: '请输入正确的手机号',
  206. icon: 'none'
  207. });
  208. return;
  209. }
  210. console.log('提交用户信息', this.userInfo);
  211. // 提交用户信息
  212. await this.$api.user.updateUser({
  213. nickName: this.userInfo.nickName,
  214. phone: this.userInfo.phone,
  215. headImage: this.userInfo.headImage
  216. })
  217. // 这里可以调用API保存用户信息
  218. uni.showToast({
  219. title: '信息保存成功',
  220. icon: 'success'
  221. });
  222. // 跳转到首页或其他页面
  223. setTimeout(() => {
  224. uni.switchTab({
  225. url: '/pages/index/index'
  226. });
  227. }, 1500);
  228. }
  229. }
  230. }
  231. </script>
  232. <style lang="scss" scoped>
  233. .user-info-container {
  234. min-height: 100vh;
  235. background-color: #f5f5f5;
  236. }
  237. .custom-navbar {
  238. position: fixed;
  239. top: 0;
  240. left: 0;
  241. right: 0;
  242. z-index: 1000;
  243. background-color: #1488DB;
  244. .navbar-content {
  245. height: 88rpx;
  246. display: flex;
  247. align-items: center;
  248. justify-content: center;
  249. padding-top: var(--status-bar-height, 44rpx);
  250. .navbar-title {
  251. font-size: 36rpx;
  252. font-weight: 500;
  253. color: #ffffff;
  254. }
  255. }
  256. }
  257. .content {
  258. padding-top: calc(88rpx + var(--status-bar-height, 44rpx));
  259. padding: calc(88rpx + var(--status-bar-height, 44rpx)) 40rpx 40rpx;
  260. }
  261. .avatar-section {
  262. display: flex;
  263. justify-content: center;
  264. margin-bottom: 80rpx;
  265. .app-info {
  266. display: flex;
  267. flex-direction: column;
  268. align-items: center;
  269. .app-logo {
  270. width: 160rpx;
  271. height: 160rpx;
  272. border-radius: 20rpx;
  273. border: 4rpx dashed #cccccc;
  274. margin-bottom: 20rpx;
  275. }
  276. .app-name {
  277. font-size: 32rpx;
  278. font-weight: 500;
  279. color: #333333;
  280. }
  281. }
  282. }
  283. .form-section {
  284. background-color: #ffffff;
  285. border-radius: 20rpx;
  286. padding: 40rpx;
  287. margin-bottom: 60rpx;
  288. }
  289. .form-item {
  290. display: flex;
  291. align-items: center;
  292. margin-bottom: 40rpx;
  293. &:last-child {
  294. margin-bottom: 0;
  295. }
  296. .form-label {
  297. width: 120rpx;
  298. font-size: 32rpx;
  299. color: #333333;
  300. font-weight: 500;
  301. }
  302. .form-input {
  303. flex: 3;
  304. height: 80rpx;
  305. padding: 0 20rpx;
  306. // border: 2rpx solid #e0e0e0;
  307. border-radius: 10rpx;
  308. font-size: 30rpx;
  309. color: #333333;
  310. &.phone-input {
  311. margin-right: 20rpx;
  312. }
  313. }
  314. .avatar-upload {
  315. .avatar-image {
  316. width: 120rpx;
  317. height: 120rpx;
  318. border-radius: 10rpx;
  319. border: 2rpx dashed #cccccc;
  320. }
  321. }
  322. .phone-input-container {
  323. flex: 1;
  324. display: flex;
  325. align-items: center;
  326. .get-phone-btn {
  327. flex: 2;
  328. // padding: 0rpx 0rpx;
  329. background-color: #1488DB;
  330. border-radius: 40rpx;
  331. border: none;
  332. outline: none;
  333. &::after {
  334. border: none;
  335. }
  336. .btn-text {
  337. font-size: 26rpx;
  338. color: #ffffff;
  339. }
  340. }
  341. }
  342. }
  343. .submit-section {
  344. padding: 0 40rpx;
  345. .submit-btn {
  346. width: 100%;
  347. height: 88rpx;
  348. background-color: #1488DB;
  349. border-radius: 44rpx;
  350. display: flex;
  351. align-items: center;
  352. justify-content: center;
  353. .submit-text {
  354. font-size: 32rpx;
  355. font-weight: 500;
  356. color: #ffffff;
  357. }
  358. }
  359. }
  360. // 添加按钮样式
  361. .avatar-button {
  362. padding: 0;
  363. margin: 0;
  364. background: transparent;
  365. border: none;
  366. outline: none;
  367. &::after {
  368. border: none;
  369. }
  370. }
  371. </style>