userInfo.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <view class="default_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header title='用户信息'></cus-header>
  4. <view class="info adffcac">
  5. <image class="info-avatar" :src="userInfo.headUrl||defaultAvatar" @click="changeAvatar"></image>
  6. <view class="info-text">上传头像</view>
  7. <view class="info-form">
  8. <view class="info-form-item adfac">
  9. <view class="info-form-item-left">姓名</view>
  10. <view class="info-form-item-right">
  11. <u-input border="none" v-model="userInfo.realName" placeholder="请输入姓名" style="color: #193D59;font-size: 30rpx;" placeholder-style="color:#99A9B5;font-size: 30rpx;"></u-input>
  12. </view>
  13. </view>
  14. <view class="info-form-item adfac">
  15. <view class="info-form-item-left">性别</view>
  16. <view class="info-form-item-right adfac">
  17. <view class="info-form-item-right-pre adfac" @click="changeGender(0)">
  18. <image :src="imgBase+'selected.png'" v-if="userInfo.gender==0"></image>
  19. <image :src="imgBase+'not_select.png'" v-else></image>
  20. <text>男</text>
  21. </view>
  22. <view class="info-form-item-right-pre adfac" @click="changeGender(1)" style="margin-left: 80rpx;">
  23. <image :src="imgBase+'selected.png'" v-if="userInfo.gender==1"></image>
  24. <image :src="imgBase+'not_select.png'" v-else></image>
  25. <text>女</text>
  26. </view>
  27. </view>
  28. </view>
  29. <view class="info-form-item adfac">
  30. <view class="info-form-item-left">手机</view>
  31. <view class="info-form-item-right">{{ jmPhone }}</view>
  32. </view>
  33. </view>
  34. </view>
  35. <view class="zt_btn" @click="handleSave">保存</view>
  36. </view>
  37. </template>
  38. <script>
  39. import { BaseApi } from '@/http/baseApi.js'
  40. export default {
  41. data(){
  42. return {
  43. defaultAvatar:this.$imgBase+'upload.png',
  44. userInfo:{
  45. id:'',
  46. headUrl:'',
  47. realName:'',
  48. gender:null
  49. },
  50. jmPhone:''
  51. }
  52. },
  53. onLoad() {
  54. if(uni.getStorageSync('userInfo')){
  55. let ui = JSON.parse(uni.getStorageSync('userInfo'));
  56. this.jmPhone = ui?.mobile?.replace(/(\d{3})\d{4}(\d{4})/, '$1 **** $2');
  57. this.userInfo.id = ui?.id;
  58. this.userInfo.headUrl = ui.headUrl;
  59. this.userInfo.realName = ui.realName;
  60. this.userInfo.gender = ui.gender;
  61. }
  62. },
  63. methods:{
  64. changeGender(gender){
  65. this.userInfo.gender = gender;
  66. },
  67. changeAvatar(){
  68. uni.showActionSheet({
  69. itemList: ['从手机相册选择'],
  70. success: (res) => {
  71. if (res.tapIndex === 0) { // 从手机相册选择
  72. this.chooseLocalImage();
  73. }
  74. }
  75. });
  76. },
  77. uploadAvatar(url){
  78. uni.uploadFile({
  79. url: BaseApi + '/uploadFile',
  80. filePath: url,
  81. name: 'file',
  82. success: (res) => {
  83. try{
  84. let data = JSON.parse(res.data);
  85. if(data.code!==0) return this.$showToast(data.msg)
  86. this.userInfo.headUrl = data.data||'';
  87. }catch(e){
  88. //TODO handle the exception
  89. }
  90. },
  91. fail: (err) => {
  92. console.log(err,'err');
  93. }
  94. });
  95. },
  96. // 从本地选择图片并显示
  97. chooseLocalImage() {
  98. uni.chooseImage({
  99. count: 1, // 只能选择一张
  100. sizeType: ['compressed'], // 压缩图片
  101. sourceType: ['album', 'camera'], // 可以从相册或相机选择
  102. success: (res) => {
  103. const tempFilePaths = res.tempFilePaths;
  104. if (tempFilePaths && tempFilePaths.length > 0) {
  105. this.uploadAvatar(tempFilePaths[0]);
  106. }
  107. },
  108. fail: (err) => {
  109. console.error('选择图片失败', err);
  110. }
  111. });
  112. },
  113. handleSave(){
  114. if(!this.userInfo.headUrl) return this.$showToast('请上传头像');
  115. if(!this.userInfo.realName) return this.$showToast('请输入姓名');
  116. if(this.userInfo.gender==null) return this.$showToast('请选择性别');
  117. this.$api.put('/wx/updateUser',this.userInfo).then(({data:res})=>{
  118. if(res.code!==0) return this.$showToast(res.msg)
  119. this.$showToast('保存成功')
  120. let ui = JSON.parse(uni.getStorageSync('userInfo'));
  121. ui.headUrl = this.userInfo.headUrl;
  122. ui.realName = this.userInfo.realName;
  123. ui.gender = this.userInfo.gender;
  124. uni.setStorageSync('userInfo',JSON.stringify(ui));
  125. setTimeout(()=>{
  126. uni.reLaunch({
  127. url:'/pages/my'
  128. })
  129. },1500)
  130. })
  131. }
  132. }
  133. }
  134. </script>
  135. <style scoped lang="scss">
  136. .default_page{
  137. padding: 0 24rpx 306rpx;
  138. box-sizing: border-box;
  139. background: #F7F7F7;
  140. justify-content: space-between;
  141. .info{
  142. margin-top: 68rpx;
  143. flex: 1;
  144. &-avatar{
  145. width: 168rpx;
  146. height: 168rpx;
  147. border-radius: 50%;
  148. }
  149. &-text{
  150. font-family: PingFangSC, PingFang SC;
  151. font-weight: 400;
  152. font-size: 30rpx;
  153. color: #002846;
  154. line-height: 42rpx;
  155. margin-top: 24rpx;
  156. text-align: center;
  157. }
  158. &-form{
  159. width: 100%;
  160. margin-top: 68rpx;
  161. background: #FFFFFF;
  162. border-radius: 16rpx;
  163. padding: 0 24rpx;
  164. box-sizing: border-box;
  165. &-item{
  166. height: 98rpx;
  167. box-shadow: inset 0rpx -1rpx 0rpx 0rpx #EFEFEF;
  168. &-left{
  169. width: 140rpx;
  170. font-family: PingFang-SC, PingFang-SC;
  171. font-weight: bold;
  172. font-size: 30rpx;
  173. color: #002846;
  174. line-height: 42rpx;
  175. }
  176. &-right{
  177. width: calc(100% - 140rpx);
  178. font-family: PingFangSC, PingFang SC;
  179. font-weight: 400;
  180. font-size: 30rpx;
  181. color: #193D59;
  182. line-height: 32rpx;
  183. &-pre{
  184. image{
  185. width: 36rpx;
  186. height: 36rpx;
  187. }
  188. text{
  189. font-family: PingFang-SC, PingFang-SC;
  190. font-weight: bold;
  191. font-size: 30rpx;
  192. color: #193D59;
  193. line-height: 42rpx;
  194. margin-left: 20rpx;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. .zt_btn{
  202. width: calc(100% - 32rpx);
  203. margin-left: 16rpx;
  204. }
  205. }
  206. </style>