userInfo.vue 6.7 KB

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