微信公众号官网:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
1.构造网页授权url
@Inner(value = false)
@ApiOperation(value = "构造网页授权url", notes = "构造网页授权url")
@GetMapping("/getUrl")
public R<String> getUrl() {
String authUrl = String.format(SecurityConstants.GZH_URL, gzhConfig.getAppid(),
URIUtil.encodeURIComponent(gzhConfig.getRedirectUri()), "snsapi_userinfo", "STATE");
return R.ok(authUrl);
}
2.根据code登录注册
/**
* 根据code登录注册
* @return
*/
@Inner(value = false)
@ApiOperation(value = "根据code登录注册", notes = "根据code登录注册")
@PostMapping("/login/{code}")
public R<SysUser> login(@PathVariable String code) {
SysUser user1 = remoteUserService.gzhUser(code).getData();
//获取openid
String url = String.format(SecurityConstants.GZH_TOKEN_URL, gzhConfig.getAppid(),gzhConfig.getSecret(),code);
String result = HttpUtil.get(url);
JSONObject j=JSONObject.parseObject(result);
if (StringUtils.isNotBlank(j.getString("errcode"))){
return R.failed(j.getString("errmsg"));
}
String openid=j.getString("openid");
String accessToken = j.getString("access_token");
//获取用户信息
String userUrl = String.format(SecurityConstants.GZH_USER_URL, accessToken ,openid);
String userResult = HttpUtil.get(userUrl);
JSONObject userJson=JSONObject.parseObject(userResult);
if (StringUtils.isNotBlank(userJson.getString("errcode"))){
return R.failed(userJson.getString("errmsg"));
}
/**注册、更新*/
SysUser user = remoteUserService.gzhUser(openid).getData();
if (user==null){
user=new SysUser();
user.setCreateTime(new Date());
}else {
user.setUpdateTime(new Date());
}
user.setAvatar(userJson.getString("headimgurl"));
user.setRealName(userJson.getString("nickname"));
user.setUsername(openid);
user.setWxWechatOpenid(openid);
user.setLockFlag(CommonConstants.STATUS_NORMAL);
user.setDelFlag(CommonConstants.STATUS_NORMAL);
int sex=userJson.getInteger("sex");
if (sex==1) {
user.setSex(1);//男
}else if (sex==2){
user.setSex(0);//女
}else {
user.setSex(2);//未知
}
remoteUserService.saveOrUpdate(user,SecurityConstants.FROM);
if (ObjectUtil.isNotEmpty(accessToken)){
redisTemplate.opsForValue().set(SecurityConstants.MINI_ACCESS_TOKEN+openid,result,2, TimeUnit.HOURS); //两小时过期
}
return R.ok(user);
}
3.小程序获取用户手机号
/**
* author
* 小程序获取用户手机号
*/
@Inner(value = false)
@ApiOperation(value = "绑定/解密用户手机号", tags = "绑定/解密用户手机号")
@PostMapping("/getPhoneNumber")
public R getPhoneNumber(@RequestBody PhoneForm phoneForm) {
System.out.println("================================="+phoneForm.getOpenId());
Object accessToken = redisTemplate.opsForValue().get(SecurityConstants.MINI_ACCESS_TOKEN+phoneForm.getOpenId());
if(ObjectUtil.isNull(accessToken)){
return R.failed("登录已过期");
}
String session_key = JSONUtil.parseObj(accessToken).getStr("session_key");;
JSONObject phoneNumber = WxUtils.getPhoneNumber(session_key, phoneForm.getEncryptedData(), phoneForm.getIv());
return R.ok(phoneNumber);
}
配置
(1)SecurityConstants
/**
* 公众号授权URL
*/
String GZH_URL="https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
/**
*通过code换取网页授权access_token
*/
String GZH_TOKEN_URL="https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
/**
* 获取用户信息
*/
String GZH_USER_URL="https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";
(2)GzhConfig
package com.jiayou.peis.official.account.biz.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author 杨朝勇
* @date 2018/8/16 微信登录配置
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "wx.mp")
public class GzhConfig {
private String appid;
private String secret;
//重定向地址
private String redirectUri;
}
(3)yaml
#微信登录
wx:
mp:
appId:
secret:
#重定向地址
redirectUri: