user_info.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view>
  3. <view class="top-back"></view>
  4. <view class="content">
  5. <view class="margin-top" >
  6. <uv-gap height="10" ></uv-gap>
  7. <!-- <p style="font-size: 25px;">个人信息</p> -->
  8. <uv-divider class="head-text" text="个人资料" textSize="26" textColor="#000"></uv-divider>
  9. <uv-gap height="10" ></uv-gap>
  10. <view class="margin-top"><!-- 昵称 -->
  11. <view class="title">昵称</view>
  12. <uv-gap height="10" ></uv-gap>
  13. <view class="flex justify-between">
  14. <input maxlength=20 class="" type="nickname" style="width: 60%;" v-model="user.name" placeholder-style="color: #cccccc; font-size: 24px; font-weight: 100;" placeholder="请输入昵称" />
  15. <image src="@/static/img/user.png" style="background-color: #fff;width: 15vw;height: 15vw;margin: 0 30px 5px;"></image>
  16. </view>
  17. </view>
  18. <uv-divider customStyle="margin: 0 0"></uv-divider>
  19. <uv-gap height="20" ></uv-gap>
  20. <view><!-- 性别 -->
  21. <view class="">
  22. <view class="title">性别</view>
  23. <uv-gap height="10" ></uv-gap>
  24. <view class="flex justify-between">
  25. <input placeholder="请选择性别" disabled="disabled" style="width: 60%;" name="input" v-model="gender" placeholder-style="color: #cccccc; font-size: 24px; font-weight: 100;" @click="changegender"></input>
  26. <image @click="confirm({'indexs': [0]})" src="@/static/img/man.png" style="background-color: #fff;width: 15vw;height: 15vw;"></image>
  27. <image @click="confirm({'indexs': [1]})" src="@/static/img/woman.png" style="background-color: #fff;width: 15vw;height: 15vw;"></image>
  28. </view>
  29. </view>
  30. <uv-picker ref="picker" :columns="columns" @confirm="confirm"></uv-picker>
  31. </view>
  32. <uv-divider customStyle="margin: 0 0"></uv-divider>
  33. <uv-gap height="20" ></uv-gap>
  34. <view class="" style="margin-bottom: -20upx;"><!-- 年龄 -->
  35. <view class="title">年龄</view>
  36. <uv-gap height="10" ></uv-gap>
  37. <input class="weui-input" type="number" v-model="user.age" placeholder-style="color: #cccccc; font-size: 24px; font-weight: 100;" placeholder="请输入年龄" />
  38. </view>
  39. <uv-divider customStyle="margin: 0 0"></uv-divider>
  40. <uv-gap height="10" ></uv-gap>
  41. </view>
  42. <uv-modal width=260 ref="modal" title="请将信息填写完整" @confirm="confirm"></uv-modal>
  43. </view>
  44. <uv-button @click="update()" customStyle="position: fixed; bottom: 40px;width: calc(100vw - 60px);margin: 0 30px" shape="circle" size="large" color="#1678ff">保存</uv-button>
  45. </view>
  46. </template>
  47. <script>
  48. export default {
  49. onLoad() {
  50. this.user.name = uni.getStorageSync("name");
  51. this.$user_api.user_get().then(res => {
  52. this.user = this.$util.removeNullValues(res);
  53. this.gender = this.user.gender === 0 ? '男' : '女';
  54. uni.setStorageSync("name",res.name);
  55. uni.setStorageSync("user",this.user);
  56. });
  57. },
  58. onPullDownRefresh(){
  59. this.user.name = uni.getStorageSync("name");
  60. this.$user_api.user_get().then(res => {
  61. this.user = this.$util.removeNullValues(res);
  62. this.gender = this.user.gender === 0 ? '男' : '女';
  63. uni.setStorageSync("name",res.name);
  64. uni.setStorageSync("user",this.user);
  65. });
  66. setTimeout(() => {uni.stopPullDownRefresh();}, 1000);
  67. },
  68. data() {
  69. return {
  70. user:{
  71. name: '',
  72. gender: '',
  73. age: '',
  74. },
  75. gender: '',
  76. columns: [['男','女']],
  77. request: 1,
  78. }
  79. },
  80. methods: {
  81. changegender() {
  82. this.$refs.picker.open();
  83. },
  84. confirm(e) {
  85. this.user.gender = e['indexs'][0];
  86. this.gender = this.user.gender === 0 ? '男' : '女';
  87. },
  88. update(){
  89. if(this.user.name.length > 0){
  90. //用户名必须由1到20个字符组成,并且只能包含字母和数字。
  91. const regex = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$/;
  92. const isValid = regex.test(this.user.name);
  93. if (isValid) {
  94. console.log("用户名有效");
  95. if(this.request){
  96. let user = uni.getStorageSync("user");
  97. if(!this.$util.ObjectEqual(user,this.user)){
  98. this.request = 0;
  99. uni.showLoading({title: '加载中...'});
  100. console.log('this.user: ',this.user);
  101. this.$user_api.user_update(this.$util.removeNullValues(this.user))
  102. .then((res) => {
  103. this.request = 1;
  104. uni.hideLoading();
  105. uni.showToast({duration:1000,icon:'success',title: '保存成功 !'});
  106. }).catch((err) => {
  107. this.request = 1;
  108. uni.hideLoading();
  109. uni.showToast({icon:"error",title: '保存失败 !'});
  110. });
  111. }
  112. else{
  113. console.log("信息未改变");
  114. uni.showToast({duration:1500,icon:"error",title: '信息未发生改变'});
  115. }
  116. }
  117. } else {
  118. console.log("用户名无效");
  119. uni.showToast({duration:1500,icon:"none",title: '用户名只能包含字母和数字'});
  120. }
  121. }
  122. else{
  123. uni.showToast({duration:1500,icon:"none",title: '请输入用户名'});
  124. }
  125. }
  126. }
  127. }
  128. </script>
  129. <style>
  130. page {
  131. height: 100vh;
  132. }
  133. .top-back{
  134. height: 25px;
  135. background-color: #1678ff;
  136. color: #fff;
  137. margin-bottom: -5vh;
  138. }
  139. .top-back p{
  140. height: 7.5vh;
  141. display: flex; /* 使用flex布局来实现居中 */
  142. justify-content: center; /* 水平居中 */
  143. align-items: center; /* 垂直居中 */
  144. font-size: 20px;
  145. letter-spacing: 10px;
  146. margin-left: 10px;
  147. font-weight: 600;
  148. }
  149. .head-text{
  150. letter-spacing: 5px;
  151. font-weight: 600;
  152. }
  153. .title{
  154. letter-spacing: 2px;
  155. color: #8c92ad;
  156. font-size: 18px;
  157. }
  158. input {
  159. letter-spacing: 1px;
  160. width: 100%;
  161. height: 8vh;
  162. padding-bottom: 5px;
  163. font-size: 24px;
  164. font-weight: 600;
  165. font-weight: 400;
  166. color: #000;
  167. outline: none; /* 移除聚焦时的默认边框 */
  168. }
  169. .content {
  170. padding: 0 35px;
  171. border-radius: 25px;
  172. border: none;
  173. background-color: #ffffff;
  174. }
  175. .content image:active {
  176. transform: translateY(2px) translateX(2px);
  177. }
  178. </style>