Springboot +vue +uniapp生活记账小程序,前端采用vue uni-app设计开发,后端采用 Springboot 开发前端对应的数据接口,首页显示生活账单信息,我的野蛮统计记账信息和微信登录状况。记账页面可以,根据不同类别的日常消费记账,数据库mysql 缓存redis ,
主页面如下:
记账页面如下:
微信登录前端的代码如下:
login(state, fun) {
var data = {}
//获取用户信息
wx.getUserProfile({
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: info => {
data["nickName"] = info.userInfo.nickName;
data["rawData"] = info.rawData;
data["signature"] = info.signature;
data["encryptedData"] = info.encryptedData;
data["iv"] = info.iv;
wx.login({
success: res => {
console.log("获取code:"+res.code)
data["code"] = res.code;
if (res.code) {
//发起网络请求
request("/sys-login/wxLogin",JSON.stringify(data),"post").then(res=>{
uni.setStorageSync("token",res.token);
state.userInfo = info;
uni.$u.toast("登录成功")
if(fun!=null && fun!=""){
fun();
}
}).catch(err=>{
uni.$u.toast(""+err.msg)
//登录失败清楚token
uni.removeStorageSync('token');
uni.$u.toast(err.msg)
//登录成功后回调
})
} else {
uni.$u.toast('登录失败!' + res.errMsg)
}
}
})
}
})
},
后端登录控制类代码
@ApiOperation("微信登录")
@RequestMapping(value = "/wxLogin",method = RequestMethod.POST)
@Transactional
public JsonResult wxLogin(@RequestBody @Valid SysWxUser en){
if(StringUtil.isEmpty(en.getCode())){
return JsonResult.error("code不能为空");
}
//微信接口服务,通过调用微信接口服务中jscode2session接口获取到openid和session_key
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + secret + "&js_code=" + en.getCode() + "&grant_type=authorization_code";
String str = com.bowei.officialwebsite.utils.WeChatUtil.httpRequest(url, "GET", null);
JSONObject jsonObject= JSONObject.parseObject(str);
//设置openID
en.setOpenId(jsonObject.getString(AppletConstants.openId));
en.setUnionId(jsonObject.getString(AppletConstants.unionId));
//更新用户
int i = sysWxUserMapper.update(
en,
new QueryWrapper<SysWxUser>()
.eq("open_id", en.getOpenId())
);
if(i==0){
en.setCreateDate(LocalDateTime.now());
//插入一个用户
sysWxUserMapper.insert(en);
}else{
//查询出用户
SysWxUser wxUser = sysWxUserMapper.selectOne(new QueryWrapper<SysWxUser>().eq("open_id", en.getOpenId()));
en.setId(wxUser.getId());
}
HashMap<String, Object> jwtmap = new HashMap<>();
jwtmap.put("id",en.getId());
jwtmap.put("nickName",en.getNickName());
//生成token
String sign = JwtUtils.sign(jwtmap);
//将token也加进去 先写个假的
jsonObject.put("token",sign);
//将用户信息存到redis
redisUtil.set(RedisConstants.USER_INFOS_PREFIX+sign,jwtmap,EXPIRE_TIME);
return JsonResult.success(jsonObject);
}