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

376 lines
7.7 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. <template>
  2. <view class="profile-container">
  3. <!-- 基本资料 -->
  4. <view class="section">
  5. <view class="section-title">
  6. <!-- 小竖线 -->
  7. <view class="vertical-line"></view>
  8. <view>
  9. <text class="title-text">基本资料</text>
  10. </view>
  11. </view>
  12. <!-- 头像 -->
  13. <view class="avatar-section">
  14. <button
  15. class="avatar-button"
  16. open-type="chooseAvatar"
  17. @chooseavatar="onChooseAvatar"
  18. >
  19. <image
  20. class="avatar"
  21. :src="userInfo.headImage || '/static/待上传头像.png'"
  22. mode="aspectFill"
  23. ></image>
  24. </button>
  25. <text class="avatar-tip">点击更换头像</text>
  26. </view>
  27. <!-- 昵称 -->
  28. <view class="info-item">
  29. <text class="label">昵称</text>
  30. <view class="value-container">
  31. <input
  32. class="nickname-input"
  33. v-model="userInfo.nickName"
  34. placeholder="请输入"
  35. type="nickname"
  36. @blur="onNicknameBlur"
  37. />
  38. </view>
  39. </view>
  40. <!-- 手机号 -->
  41. <view class="info-item">
  42. <text class="label">手机号</text>
  43. <view class="value-container">
  44. <input
  45. class="phone-input"
  46. v-model="userInfo.phone"
  47. placeholder="请输入"
  48. type="number"
  49. maxlength="11"
  50. />
  51. </view>
  52. </view>
  53. <!-- 地址 -->
  54. <view class="info-item">
  55. <text class="label">地址</text>
  56. <view class="value-container" @click="openMap">
  57. <text class="address-input">
  58. {{ userInfo.address || '请选择地址' }}
  59. </text>
  60. <!-- <text class="address-input" v-else>
  61. 请选择地址
  62. </text> -->
  63. </view>
  64. </view>
  65. </view>
  66. <!-- 保存按钮 -->
  67. <view class="save-section">
  68. <button class="save-button" @click="saveProfile">
  69. 保存
  70. </button>
  71. </view>
  72. </view>
  73. </template>
  74. <script>
  75. export default {
  76. name: 'MyProfile',
  77. data() {
  78. return {
  79. userInfo: {
  80. headImage: '',
  81. nickName: '',
  82. phone: '',
  83. address: ''
  84. }
  85. }
  86. },
  87. methods: {
  88. // 返回上一页
  89. goBack() {
  90. uni.navigateBack();
  91. },
  92. // 选择微信头像
  93. async onChooseAvatar(e) {
  94. console.log('选择头像回调', e);
  95. if (e.detail.avatarUrl) {
  96. const file = {
  97. path: e.detail.avatarUrl
  98. }
  99. const res = await this.$utils.uploadImage(file)
  100. this.userInfo.headImage = res.url
  101. uni.showToast({
  102. title: '头像更新成功',
  103. icon: 'success'
  104. });
  105. } else {
  106. uni.showToast({
  107. title: '头像选择失败',
  108. icon: 'none'
  109. });
  110. }
  111. },
  112. // 昵称输入失焦验证
  113. onNicknameBlur() {
  114. if (!this.userInfo.nickName.trim()) {
  115. uni.showToast({
  116. title: '请输入昵称',
  117. icon: 'none'
  118. });
  119. }
  120. },
  121. // 保存资料
  122. async saveProfile() {
  123. // 验证昵称
  124. if (!this.userInfo.nickName.trim()) {
  125. uni.showToast({
  126. title: '请输入昵称',
  127. icon: 'none'
  128. });
  129. return;
  130. }
  131. // 验证手机号(如果填写了)
  132. if (this.userInfo.phone && !/^1[3-9]\d{9}$/.test(this.userInfo.phone)) {
  133. uni.showToast({
  134. title: '请输入正确的手机号',
  135. icon: 'none'
  136. });
  137. return;
  138. }
  139. const res = await this.$api.user.updateUser({
  140. nickName: this.userInfo.nickName,
  141. phone: this.userInfo.phone,
  142. headImage: this.userInfo.headImage,
  143. address: this.userInfo.address
  144. })
  145. if (res.code === 200) {
  146. uni.showToast({
  147. title: `${res.message}`,
  148. icon: 'success'
  149. });
  150. setTimeout(() => {
  151. uni.navigateBack()
  152. }, 1000);
  153. }
  154. },
  155. // 获取个人信息
  156. async getUserInfo() {
  157. const res = await this.$api.user.queryUser()
  158. this.userInfo = {...this.userInfo, ...res.result}
  159. // this.userInfo = res.result
  160. },
  161. // 打开地图
  162. openMap() {
  163. uni.chooseLocation({
  164. success: (res) => {
  165. console.log('位置名称:' + res.name)
  166. console.log('详细地址:' + res.address)
  167. console.log('纬度:' + res.latitude)
  168. console.log('经度:' + res.longitude)
  169. this.userInfo.address = res.address || res.name
  170. uni.showToast({
  171. title: '地址更新成功',
  172. icon: 'success'
  173. });
  174. },
  175. fail: (err) => {
  176. uni.showToast({
  177. title: `获取位置失败${err}`,
  178. icon: 'none'
  179. })
  180. }
  181. })
  182. }
  183. },
  184. onLoad() {
  185. this.getUserInfo();
  186. }
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. .profile-container {
  191. min-height: 100vh;
  192. background-color: #f5f5f5;
  193. }
  194. .vertical-line {
  195. width: 9rpx;
  196. height: 33rpx;
  197. background-color: #1488DB;
  198. margin-right: 20rpx;
  199. }
  200. // 自定义导航栏
  201. .custom-navbar {
  202. background: linear-gradient(90deg, #1488db 0%, #1488db 100%);
  203. padding-top: var(--status-bar-height, 44rpx);
  204. .navbar-content {
  205. height: 88rpx;
  206. display: flex;
  207. align-items: center;
  208. justify-content: space-between;
  209. padding: 0 30rpx;
  210. .navbar-left {
  211. .back-icon {
  212. font-size: 40rpx;
  213. color: #ffffff;
  214. font-weight: bold;
  215. }
  216. }
  217. .navbar-title {
  218. font-size: 36rpx;
  219. color: #ffffff;
  220. font-weight: 500;
  221. }
  222. .navbar-right {
  223. display: flex;
  224. gap: 20rpx;
  225. .more-icon,
  226. .settings-icon {
  227. font-size: 32rpx;
  228. color: #ffffff;
  229. }
  230. }
  231. }
  232. }
  233. .section {
  234. margin: 20rpx;
  235. background-color: #ffffff;
  236. border-radius: 20rpx;
  237. overflow: hidden;
  238. .section-title {
  239. display: flex;
  240. align-items: center;
  241. padding: 30rpx;
  242. border-bottom: 1rpx solid #f0f0f0;
  243. .title-text {
  244. font-size: 32rpx;
  245. font-weight: 500;
  246. color: #333333;
  247. }
  248. }
  249. }
  250. .avatar-section {
  251. display: flex;
  252. flex-direction: column;
  253. align-items: center;
  254. padding: 40rpx 30rpx;
  255. border-bottom: 1rpx solid #f0f0f0;
  256. .avatar-button {
  257. padding: 0;
  258. margin: 0;
  259. background: transparent;
  260. border: none;
  261. outline: none;
  262. margin-bottom: 20rpx;
  263. &::after {
  264. border: none;
  265. }
  266. .avatar {
  267. width: 160rpx;
  268. height: 160rpx;
  269. border-radius: 80rpx;
  270. border: 4rpx dashed #cccccc;
  271. }
  272. }
  273. .avatar-tip {
  274. font-size: 26rpx;
  275. color: #999999;
  276. }
  277. }
  278. .info-item {
  279. display: flex;
  280. align-items: center;
  281. justify-content: space-between;
  282. padding: 30rpx;
  283. border-bottom: 1rpx solid #f0f0f0;
  284. &:last-child {
  285. border-bottom: none;
  286. }
  287. .label {
  288. font-size: 30rpx;
  289. color: #333333;
  290. font-weight: 500;
  291. width: 120rpx;
  292. }
  293. .value-container {
  294. flex: 1;
  295. display: flex;
  296. align-items: center;
  297. justify-content: flex-end;
  298. .nickname-input,
  299. .phone-input,
  300. .address-input {
  301. width: 100%;
  302. text-align: right;
  303. font-size: 30rpx;
  304. color: #666666;
  305. border: none;
  306. outline: none;
  307. background: transparent;
  308. &::placeholder {
  309. color: #cccccc;
  310. }
  311. }
  312. }
  313. }
  314. .save-section {
  315. padding: 40rpx 20rpx;
  316. .save-button {
  317. width: 100%;
  318. height: 88rpx;
  319. background-color: #1488db;
  320. border-radius: 44rpx;
  321. border: none;
  322. outline: none;
  323. font-size: 32rpx;
  324. font-weight: 500;
  325. color: #ffffff;
  326. display: flex;
  327. align-items: center;
  328. justify-content: center;
  329. &::after {
  330. border: none;
  331. }
  332. }
  333. }
  334. </style>