0.java web端
public class UserController {
@Autowired UserService userService;
@Autowired ObjectMapper objectMapper;
private String wxspAppid = "your_Appid";
//小程序的 app secret (在微信小程序管理后台获取)
private String wxspSecret = "your_Secret ";
private String openid;
private Logger logger = LoggerFactory.getLogger(UserService.class);
@PostMapping ("/signinUser")
public User signinUser(@RequestParam String code) throws IOException
{
RestTemplate restTemplate = new RestTemplate();// 发送request请求
String params = "appid=" + wxspAppid + "&secret=" + wxspSecret + "&js_code=" +code;//参数
String url = "https://api.weixin.qq.com/sns/jscode2session?"+params;// 微信接口 用于查询oponid
String response = restTemplate.getForObject(url,String.class);
logger.info("response:"+response);
WeixinRespense weixinRespense = objectMapper.readValue(response,WeixinRespense.class); // 逆序列化 ,将字符串中的有效信息取出
String session_key = weixinRespense.getSession_key();//如果解密encryptData获取unionId,会用的到
openid= weixinRespense.getOpenid();//微信小程序 用户唯一标识
logger.info("session_key:"+session_key);
logger.info("openid:"+openid);
// 注册用户,将查询到的oponid作为id
User user = new User();
user.setUid(openid);
user.setBalence(Float.valueOf(0));
if (userService.existbyid(openid))
{
return userService.getUserById(openid);
}
else {
if (userService.signupUser(user) == 1)
{
return userService.getUserById(openid);
}
else {
return null;
}
}
}
1.微信小程序端
<view class="userinfo">
<button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">授权 </button>
</view>
const app = getApp()
function getCom()
{
var url = 'http://39.105.78.64:8081/micromarket/getCommodityList'
var com = {}
wx.request({
url: url,
data: { page: 0 },
method: 'GET',
dataType: 'json',
success: function (res) {
com = res;
},
fail: function (err) {
console.log(err)
}
})
page = page + 1
return com
}
Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
commodities:{}
},
//事件处理函数
bindViewTap: function () {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad: function () {
var that = this
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
wx.login({
success: function (r) {
console.log("code:" + r.code + "\n end")
if (r.code) {
app.globalData.code = r.code;//登录凭证
var url = 'http://39.105.78.64:8081/micromarket/signinUser'
//发起网络请求
wx.request({
url: url,
method: "POST",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
data: { code: app.globalData.code },
dataType: 'json',
success: function (result) {
app.globalData.user = result.data
console.log(app.globalData.user)
console.log(app.globalData.user.balence)
},
fail: function () {
console.log(" post error")
}
})
}
},
fail: function () {
console.log("error")
}
})
commodities: getCom()
},
getUserInfo: function (info) {
console.log(info)
app.globalData.userInfo = info.detail.userInfo
this.setData({
userInfo: info.detail.userInfo,
hasUserInfo: true
})
}
})