java springboot 微信小程序开发框架源码
程序员文章站
2024-02-26 17:53:04
...
最近小程序越来越火,很多开发微信小程序这块,后面给大家讲解下java作为后台开发语言如何开发微信小程序
首先后台采用springboot mybatis作为框架
小程序这块用weui组件
开发小程序的第一步其实就是授权登陆
界面展示
<view class='container'>
<view class="weui-msg">
<view class="weui-msg__icon-area">
<icon type="info" size="93"></icon>
</view>
<view class="weui-msg__text-area">
<view class="weui-msg__title">未登录</view>
</view>
<view class="weui-msg__opr-area">
<view class="weui-btn-area">
<button class="weui-btn" type="primary" bindtap="showTopTips" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权登录</button>
</view>
</view>
<view class="weui-msg__extra-area">
<view class="weui-footer">
<view class="weui-footer__text">Copyright © 2018 wfuhui.com</view>
</view>
</view>
</view>
</view>
js代码
bindGetUserInfo: function (res) {
app.login(function(){
console.log("xxxx")
wx.navigateBack({
})
});
}
login: function(callback) {
var that = this;
var token = that.globalData.token;
if (token) {
wx.request({
url: that.globalData.domain + '/api/checkToken',
data: {
token: token
},
success: function(res) {
if (res.data.code != 0) {
that.globalData.token = null;
that.login(callback);
}
}
})
return;
}
wx.login({
success: function(res) {
wx.request({
url: that.globalData.domain + '/api/wechat/user/login',
data: {
code: res.code
},
success: function(res) {
if (res.data.code == 1) {
that.globalData.sessionKey = res.data.sessionKey; //暂时,不应该在网络传输
// 去注册
that.registerUser(callback);
return;
}
if (res.data.code != 0) {
// 登录错误
wx.hideLoading();
wx.showModal({
title: '提示',
content: '无法登录,请重试',
showCancel: false
})
return;
}
that.globalData.token = res.data.token;
that.globalData.userInfo = res.data.userInfo;
callback();
}
})
}
})
},
registerUser: function(callback) {
var that = this;
wx.login({
success: function(res) {
var code = res.code; // 微信登录接口返回的 code 参数,下面注册接口需要用到
wx.getUserInfo({
success: function(res) {
var iv = res.iv;
var encryptedData = res.encryptedData;
var rawData = res.rawData;
var signature = res.signature;
// 下面开始调用注册接口
wx.request({
url: that.globalData.domain + '/api/wechat/user/register',
data: {
code: code,
encryptedData: encryptedData,
iv: iv,
rawData: rawData,
signature: signature,
sessionKey: that.globalData.sessionKey
}, // 设置请求的 参数
success: (res) => {
if (res.data.code == 0) {
wx.hideLoading();
that.login(callback);
} else {
// 登录错误
wx.hideLoading();
wx.showModal({
title: '提示',
content: '无法登录,请重试',
showCancel: false
})
}
}
})
},
fail: function(res) {
console.log(res)
}
})
}
})
},
后台对应的controller
if (StringUtils.isBlank(code)) {
return R.error("empty jscode");
}
try {
WxMaJscode2SessionResult session = this.wxService.getUserService().getSessionInfo(code);
this.logger.info(session.getSessionKey());
this.logger.info(session.getOpenid());
//查询用户信息
UserEntity user = userService.queryByOpenid(session.getOpenid());
if(user == null) {
String sessionKey = session.getSessionKey();
return R.error(1, "未注册").put("sessionKey", sessionKey);
}
//生成token
Map<String, Object> map = tokenService.createToken(user.getId());
map.put("userInfo", user);
return R.ok(map);
} catch (WxErrorException e) {
this.logger.error(e.getMessage(), e);
return R.error();
}
代码直接扫码下载