uniapp,h5

userInfo.vue 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <view class="page">
  3. <view class="avatar" @click="showAvatarPopup()">
  4. <image class="avatar-image" :src="user.avatar" mode="aspectFill"></image>
  5. </view>
  6. <h6>(点击修改头像)</h6>
  7. <view>
  8. <uni-popup ref="popup" type="center" background-color="#fff" borderRadius="10px 10px 10px 10px"
  9. padding="40upx">
  10. <view class="popup-content">
  11. <view class="avatar-list">
  12. <block v-for="(item, index) in avatars" :key="index">
  13. <image :src="item" mode="aspectFill" @click="selectAvatar(item)"></image>
  14. </block>
  15. </view>
  16. </view>
  17. </uni-popup>
  18. </view>
  19. <view class="input-container">
  20. <view class="input">
  21. <label class="label">邮箱</label>
  22. <label class="label-new">{{ user.email }}</label>
  23. </view>
  24. <view class="input">
  25. <label class="label">用户名</label>
  26. <label class="label-new">{{ user.username }}</label>
  27. </view>
  28. <view class="input">
  29. <label class="label">个性签名</label>
  30. <view class="input-field-bio">
  31. <textarea v-model="user.bio" placeholder="请输入个性签名" />
  32. </view>
  33. </view>
  34. </view>
  35. <view class="button-container">
  36. <button class="return-button" type="primary" @click="onReturnBtnClick">返回</button>
  37. <button class="modify-button" type="primary" @click="onModifyBtnClick">保存修改</button>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import {
  43. storeJwtInfo2User
  44. } from '@/utils/storage.js';
  45. export default {
  46. data() {
  47. return {
  48. user: {
  49. avatar: '',
  50. email: '',
  51. username: '',
  52. bio: '',
  53. userId: ''
  54. },
  55. password_prompt: '******',
  56. avatars: [
  57. 'https://afanai.top:8088/imgs/default_avatar_1.jpeg',
  58. 'https://afanai.top:8088/imgs/default_avatar_2.jpeg',
  59. 'https://afanai.top:8088/imgs/default_avatar_3.jpeg',
  60. 'https://afanai.top:8088/imgs/default_avatar_4.jpeg',
  61. 'https://afanai.top:8088/imgs/default_avatar_5.jpeg',
  62. 'https://afanai.top:8088/imgs/default_avatar_6.jpeg',
  63. // 更多头像url...
  64. ],
  65. }
  66. },
  67. onLoad() {
  68. let userInfo = uni.getStorageSync('userInfo');
  69. if (userInfo) {
  70. this.user = {
  71. ...userInfo
  72. };
  73. }
  74. },
  75. methods: {
  76. onReturnBtnClick() {
  77. uni.$emit("refreshData",{});
  78. uni.navigateBack();
  79. },
  80. async onModifyBtnClick() {
  81. // if (!this.validateEmail() || !this.validateUsername()) {
  82. // uni.showToast({
  83. // title: '用户名和邮箱不能为空',
  84. // mask: true // 可选,显示加载遮罩
  85. // });
  86. // return;
  87. // }
  88. // 显示加载提示
  89. uni.showLoading({
  90. title: '修改中...',
  91. mask: true // 可选,显示加载遮罩
  92. });
  93. const item = {
  94. avatar_url: this.user.avatar,
  95. bio: this.user.bio
  96. };
  97. try {
  98. // 调用修改接口
  99. const res = await uni.request({
  100. url: 'https://afanai.top:8089/v1/user/modify/' + this.user.userId,
  101. method: 'PUT',
  102. data: item,
  103. header: {
  104. 'content-type': 'application/json'
  105. }
  106. });
  107. // 关闭加载提示
  108. uni.hideLoading();
  109. // 成功响应处理
  110. if (res.statusCode === 201) {
  111. // 如果状态码为201,表示请求成功
  112. uni.showToast({
  113. title: '修改成功',
  114. duration: 2000,
  115. icon: 'success'
  116. });
  117. const jwt = res.data.token; // 后端返回的JWT字段名为token
  118. uni.setStorageSync('jwt', jwt);
  119. storeJwtInfo2User(jwt);
  120. } else {
  121. try {
  122. // 确保 res.data 是一个对象且包含 error 键
  123. if (res.data && typeof res.data === 'object' && 'error' in res.data) {
  124. uni.showToast({
  125. title: res.data.error,
  126. duration: 2000,
  127. icon: 'none'
  128. });
  129. }
  130. } catch (e) {
  131. // 如果尝试处理 res.data 时出错,显示默认错误信息
  132. uni.showToast({
  133. title: '处理服务器响应时出错',
  134. duration: 2000,
  135. icon: 'none'
  136. });
  137. console.error('Error processing response data:', e);
  138. }
  139. }
  140. } catch (error) {
  141. // 关闭加载提示
  142. uni.hideLoading();
  143. // 错误处理
  144. uni.showToast({
  145. title: '网络错误,请检查网络',
  146. duration: 2000,
  147. icon: 'none'
  148. });
  149. console.error('修改请求出错:', error);
  150. }
  151. },
  152. validateEmail() {
  153. // 邮箱验证逻辑
  154. if (this.user.email) {
  155. return true;
  156. }
  157. return false;
  158. },
  159. validateUsername() {
  160. // 用户名验证逻辑
  161. if (this.user.email) {
  162. return true;
  163. }
  164. return false;
  165. },
  166. selectAvatar(avatarUrl) {
  167. this.user.avatar = avatarUrl;
  168. },
  169. showAvatarPopup() {
  170. // console.log("showAvatarPopup :", this.showPopup);
  171. this.$refs.popup.open();
  172. }
  173. }
  174. }
  175. </script>
  176. <style scoped>
  177. .page {
  178. display: flex;
  179. flex-direction: column;
  180. align-items: center;
  181. padding: 40upx;
  182. }
  183. .avatar-image {
  184. width: 320rpx;
  185. height: 320rpx;
  186. border-radius: 12rpx;
  187. border: 3px solid #abb0b6;
  188. }
  189. /* 合并后的纯CSS样式 */
  190. .input-container {
  191. width: 100%;
  192. display: flex;
  193. flex-direction: column;
  194. gap: 1rem;
  195. }
  196. .input {
  197. display: flex;
  198. flex-direction: column;
  199. gap: 0.5rem;
  200. }
  201. .label {
  202. margin-bottom: 5rpx;
  203. font-size: 48upx;
  204. font-weight: bold;
  205. color: #333;
  206. }
  207. .label-new {
  208. font-size: 36upx;
  209. }
  210. .input-field, .input-field-bio {
  211. width: 100%;
  212. padding: 5px;
  213. border: 1px solid #ccc;
  214. border-radius: 5rpx;
  215. box-sizing: border-box;
  216. font-size: 64upx;
  217. height: 60upx;
  218. }
  219. .input-field-bio {
  220. height: 200upx;
  221. display: flex;
  222. align-items: center;
  223. overflow: auto;
  224. }
  225. .input-field-bio textarea {
  226. width: 100%;
  227. height: 100%;
  228. border: none;
  229. outline: none;
  230. resize: none;
  231. font-size: inherit;
  232. padding: 0;
  233. padding: 0.5rem; /* 从SCSS代码中 */
  234. border-radius: 4px; /* 从SCSS代码中 */
  235. border-color: #ddd; /* 从SCSS代码中 */
  236. background-color: #fff; /* 从SCSS代码中 */
  237. color: #333; /* 从SCSS代码中 */
  238. font-size: 1rem;
  239. line-height: 1.5; /* 从SCSS代码中 */
  240. /* 为聚焦状态添加样式 */
  241. &:focus {
  242. outline: none;
  243. border-color: #007bff; /* 聚焦时的边框颜色 */
  244. box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25); /* 聚焦时的阴影效果 */
  245. }
  246. }
  247. .button-container {
  248. display: flex;
  249. justify-content: space-between;
  250. width: 100%;
  251. margin-top: 20px;
  252. }
  253. .button {
  254. width: 48%;
  255. border-radius: 5px;
  256. }
  257. .popup-content {
  258. width: 620upx;
  259. display: flex;
  260. flex-direction: column;
  261. align-items: center;
  262. }
  263. .avatar-list {
  264. display: flex;
  265. flex-wrap: wrap;
  266. justify-content: left;
  267. }
  268. .avatar-list image {
  269. width: 100rpx;
  270. height: 100rpx;
  271. margin: 10rpx;
  272. border: 1px solid #ccc;
  273. /* 简约边框 */
  274. border-radius: 20%;
  275. /* 圆形头像,更简约 */
  276. }
  277. </style>