userInfo.vue 6.2 KB

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