userInfo.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. } else if (res.tapIndex === 1) { // 获取微信头像
  74. this.getWeChatProfile(false, true); // 只获取头像
  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. }catch(e){
  90. //TODO handle the exception
  91. }
  92. },
  93. fail: (err) => {
  94. console.log(err,'err');
  95. }
  96. });
  97. },
  98. // 从本地选择图片并显示
  99. chooseLocalImage() {
  100. uni.chooseImage({
  101. count: 1, // 只能选择一张
  102. sizeType: ['compressed'], // 压缩图片
  103. sourceType: ['album', 'camera'], // 可以从相册或相机选择
  104. success: (res) => {
  105. const tempFilePaths = res.tempFilePaths;
  106. if (tempFilePaths && tempFilePaths.length > 0) {
  107. this.uploadAvatar(tempFilePaths[0]);
  108. }
  109. },
  110. fail: (err) => {
  111. console.error('选择图片失败', err);
  112. }
  113. });
  114. },
  115. // 获取微信用户头像和/或昵称
  116. // @param {boolean} getNameOnly 是否只获取昵称
  117. // @param {boolean} getAvatarOnly 是否只获取头像
  118. async getWeChatProfile(getNameOnly = false, getAvatarOnly = false) {
  119. uni.getUserProfile({
  120. desc: '用于完善您的个人资料展示', // 声明获取用户个人信息后的用途,必填
  121. success: (res) => {
  122. console.log('获取微信用户信息成功:', res.userInfo);
  123. if (res.userInfo) {
  124. if (getAvatarOnly) { // 只获取头像
  125. this.userInfo.headUrl = res.userInfo.avatarUrl;
  126. }
  127. }
  128. },
  129. fail: (err) => {
  130. console.error('获取微信用户信息失败:', err);
  131. }
  132. });
  133. },
  134. handleSave(){
  135. if(!this.userInfo.headUrl) return this.$showToast('请上传头像');
  136. if(!this.userInfo.realName) return this.$showToast('请输入姓名');
  137. if(this.userInfo.gender==null) return this.$showToast('请选择性别');
  138. this.$api.put('/wx/updateUser',this.userInfo).then(({data:res})=>{
  139. if(res.code!==0) return this.$showToast(res.msg)
  140. this.$showToast('保存成功')
  141. let ui = JSON.parse(uni.getStorageSync('userInfo'));
  142. ui.headUrl = this.userInfo.headUrl;
  143. ui.realName = this.userInfo.realName;
  144. ui.gender = this.userInfo.gender;
  145. uni.setStorageSync('userInfo',JSON.stringify(ui));
  146. setTimeout(()=>{
  147. uni.reLaunch({
  148. url:'/pages/my'
  149. })
  150. },1500)
  151. })
  152. }
  153. }
  154. }
  155. </script>
  156. <style scoped lang="scss">
  157. .default_page{
  158. padding: 0 24rpx 306rpx;
  159. box-sizing: border-box;
  160. background: #F7F7F7;
  161. justify-content: space-between;
  162. .info{
  163. margin-top: 68rpx;
  164. flex: 1;
  165. &-avatar{
  166. width: 168rpx;
  167. height: 168rpx;
  168. border-radius: 50%;
  169. }
  170. &-text{
  171. font-family: PingFangSC, PingFang SC;
  172. font-weight: 400;
  173. font-size: 30rpx;
  174. color: #002846;
  175. line-height: 42rpx;
  176. margin-top: 24rpx;
  177. text-align: center;
  178. }
  179. &-form{
  180. width: 100%;
  181. margin-top: 68rpx;
  182. background: #FFFFFF;
  183. border-radius: 16rpx;
  184. padding: 0 24rpx;
  185. box-sizing: border-box;
  186. &-item{
  187. height: 98rpx;
  188. box-shadow: inset 0rpx -1rpx 0rpx 0rpx #EFEFEF;
  189. &-left{
  190. width: 140rpx;
  191. font-family: PingFang-SC, PingFang-SC;
  192. font-weight: bold;
  193. font-size: 30rpx;
  194. color: #002846;
  195. line-height: 42rpx;
  196. }
  197. &-right{
  198. width: calc(100% - 140rpx);
  199. font-family: PingFangSC, PingFang SC;
  200. font-weight: 400;
  201. font-size: 30rpx;
  202. color: #193D59;
  203. line-height: 32rpx;
  204. &-pre{
  205. image{
  206. width: 36rpx;
  207. height: 36rpx;
  208. }
  209. text{
  210. font-family: PingFang-SC, PingFang-SC;
  211. font-weight: bold;
  212. font-size: 30rpx;
  213. color: #193D59;
  214. line-height: 42rpx;
  215. margin-left: 20rpx;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }
  222. .zt_btn{
  223. width: calc(100% - 32rpx);
  224. margin-left: 16rpx;
  225. }
  226. }
  227. </style>