第9讲用户信息修改实现

用户信息修改实现

后端修改用户昵称:

bash 复制代码
/**
 * 更新用户昵称
 * @param wxUserInfo
 * @param token
 * @return
 */
@RequestMapping("/updateNickName")
public R updateNickName(@RequestBody WxUserInfo wxUserInfo,@RequestHeader String token){
    if(StringUtil.isNotEmpty(wxUserInfo.getNickName())) {
        Claims claims = JwtUtils.validateJWT(token).getClaims();
        wxUserInfoService.update(new UpdateWrapper<WxUserInfo>().eq("openid", claims.getId()).set("nick_name", wxUserInfo.getNickName()));
    }
    return R.ok();
}

前端修改用户昵称:

bash 复制代码
<input type="nickname"  placeholder="请输入昵称" v-model="userInfo.nickName" @blur="onChangeNickName"/>
bash 复制代码
			onChangeNickName:async function(e){
				console.log(e.detail.value);
				let nickName=e.detail.value;
				if(!isEmpty(nickName)){
					const result=await requestUtil({url:"/user/updateNickName",data:{nickName:nickName},method:"post"});
				}
			}
bash 复制代码
export const isEmpty=(str)=>{
	if(str === '' || str.trim().length === 0 ){
		return true
	}else{
		return false;
	}
}

头像上传 后端:

定义上传路径:

bash 复制代码
userImagesFilePath: D://uniapp/userImgs/
bash 复制代码
@Value("${userImagesFilePath}")
private String userImagesFilePath;
bash 复制代码
/**
 * 上传用户头像图片
 * @param userImage
 * @return
 * @throws Exception
 */
@RequestMapping("/uploadUserImage")
public Map<String,Object> uploadUserImage(MultipartFile userImage, @RequestHeader String token)throws Exception{
    System.out.println("filename:"+userImage.getName());
    Map<String,Object> resultMap=new HashMap<>();
    if(!userImage.isEmpty()){
        // 获取文件名
        String originalFilename = userImage.getOriginalFilename();
        String suffixName=originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName= DateUtil.getCurrentDateStr()+suffixName;
        FileUtils.copyInputStreamToFile(userImage.getInputStream(),new File(userImagesFilePath+newFileName));
        resultMap.put("code",0);
        resultMap.put("msg","上传成功");
        resultMap.put("userImageFileName",newFileName);
        // 更新到数据库
        UpdateWrapper<WxUserInfo> updateWrapper=new UpdateWrapper<>();
        Claims claims = JwtUtils.validateJWT(token).getClaims();
        updateWrapper
                .eq("openid",claims.getId())
                .set("avatar_url",newFileName);
        wxUserInfoService.update(new UpdateWrapper<WxUserInfo>().eq("openid",claims.getId()).set("avatar_url",newFileName));
    }

    return resultMap;
}

前端头像实现:

button上加下 open-type="chooseAvatar"

bash 复制代码
		onChooseAvatar:function(e){
			console.log(e.detail.avatarUrl)
			uni.uploadFile({
				header:{token:uni.getStorageSync("token")},
				url:getBaseUrl()+"/user/uploadUserImage",
				filePath:e.detail.avatarUrl,
				name:"userImage",
				success: (res) => {
					let result=JSON.parse(res.data);
					if(result.code==0){
						this.userInfo.avatarUrl=result.userImageFileName;
					}
				}
			})
		},

my.vue

bash 复制代码
<template>
	<view class="user_center">
		<!-- 用户信息开始 -->
		<view class="user_info_wrap">
			<!--获取头像-->
			<button class="user_image" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
				<image :src="this.baseUrl+'/image/userAvatar/'+userInfo.avatarUrl"></image>
			</button> 
			<view class="user_name">
				<input type="nickname" placeholder="请输入昵称" v-model="userInfo.nickName" @blur="onChangeNickName">
			</view>
		</view>
		<!-- 用户信息结束 -->
		
		<!-- 用户菜单开始 -->
		<view class="user_menu_wrap">
			<view class="user_menu_item" >
				<text>我创建的投票</text>
			</view>
			<view class="user_menu_item"   >
				<text>我参与的投票</text>
			</view>
		</view>
		<!-- 用户菜单结束 -->
		
		<!-- 用户信息修改开始 -->
		<view class="user_info_modify_wrap">
			<view class="user_info_modify_wrap_item" >
				<text>联系小锋老师</text>
			</view>
		</view>
		<!-- 用户信息修改结束 -->
	</view>
</template>

<script>
	import {getBaseUrl,requestUtil} from "../../util/requestUtil.js"
	import {isEmpty} from "../../util/stringUtil.js"
	export default{
		data(){
			return{
				userInfo:{
					nickName:'',
					avatarUrl:''
				},
				baseUrl:''
			}
		},
		onShow() {
			this.getUserInfo()
			this.baseUrl=getBaseUrl();
		},
		methods:{
			getUserInfo:async function(){
				const result=await requestUtil({url:'/user/getUserInfo',method:'get'});
				console.log("result="+result)
				this.userInfo=result.currentUser;
			},
			onChangeNickName:async function(e){
				console.log(e.detail.value)
				let nickName=e.detail.value;
				if(!isEmpty(nickName)){
					const result=await requestUtil({url:'/user/updateNickName',data:{nickName:nickName},method:'post'});
				}
			},
			onChooseAvatar:function(e){
				console.log(e.detail.avatarUrl);
				uni.uploadFile({
					header:{token:uni.getStorageSync("token")},
					url:getBaseUrl()+"/user/updateUserImage",
					filePath:e.detail.avatarUrl,
					name:"userImage",
					success: (res) => {
						let result=JSON.parse(res.data);
						if(result.code==0){
							this.userInfo.avatarUrl=result.userImageFileName
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.user_center{
		.user_info_wrap{
			width: 100%;
			height: 120rpx;
			display: flex;
			flex-direction: row;
			background-color: white;
			padding-left: 50rpx;
			.user_image{
				width: 100rpx;
				height: 100rpx;
				text-align: center;
				padding: 0rpx;
				margin: 0rpx;
				image{
					width: 90rpx;
					height: 90rpx;
				}
			}
			.user_name{
				display: flex;
				flex-direction: column;
				justify-content: center;
				padding-left: 20rpx;
				padding-bottom: 15rpx;
			}
		}
		.user_menu_wrap{
			margin: 15rpx;
			margin-top: 20rpx;
			background-color: #fff;
			.user_menu_item{
				padding: 20rpx;
			    padding-left: 35rpx;
			    border-bottom: 5rpx solid #F6F6F4;
			}
		}
		.user_info_modify_wrap{
			margin: 15rpx;
			margin-top: 20rpx;
			background-color: #fff;
			padding: 20rpx 0;
			padding-left: 35rpx;
		}
	}
</style>

注意 id

bash 复制代码
weixin:
  jscode2sessionUrl: https://api.weixin.qq.com/sns/jscode2session
  appid: 自己的
  secret: 自己的

userImagesFilePath: D://uniapp/userImgs/

用户信息问题

相关推荐
懂软件的胡子个哥37 分钟前
微信群运营怎么通过微信 API 做问题提醒和活动管理
微信·自动化·wechatapi·个人微信号二次开发
数据狐(Datafox)42 分钟前
1688商品列表API接口解析(附 JSON 样例)
java·开发语言·数据库·爬虫·json
iPad协议个微协议2 小时前
微信数据同步怎么做异常补偿,避免 CRM 和业务系统数据不一致
微信·微信开发·wechatapi·个人微信号二次开发
扶风ff3 小时前
练题簿企业培训方案:统一题库、在线任务与岗位考核
学习·小程序
AI行业应用研究4 小时前
会务问答机器人落地拆解:三级路由、知识库组织与防幻觉——会务小程序能自己回答参会者提问吗?
大数据·人工智能·安全·小程序·架构
盟道科技4 小时前
小程序订阅消息大规模投递实践:频控令牌池、Redis限流与失败补偿设计
分布式·算法·小程序
m0_587383004 小时前
本地同城无人机作业接单系统源码:架构拆解与抢单调度实现
java·小程序·架构·需求分析
2501_915909065 小时前
怎么用 FlutterFlow 把应用发布到 App Store?
android·ios·小程序·https·uni-app·iphone·webview
本人手速666+6 小时前
WeComApi 适合哪些企业微信二次开发场景?从外部群、自动回复到 CRM 对接
微信·企业微信·企微外部群开发·wecomapi·企业微信二次开发
白远山19 小时前
家政服务家政社区派单实战:调度系统架构设计与实现指南
java·开发语言·小程序·架构·需求分析