userInfo.vue 6.1 KB

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