| | |
| | | package com.ruoyi.user.service.impl; |
| | | |
| | | import cn.binarywang.wx.miniapp.api.WxMaService; |
| | | import cn.binarywang.wx.miniapp.api.WxMaUserService; |
| | | import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
| | | import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; |
| | | import cn.hutool.core.util.RandomUtil; |
| | |
| | | public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { |
| | | |
| | | @Resource |
| | | private WxMaUserService wxMaUserService; |
| | | @Resource |
| | | private TokenService tokenService; |
| | | @Resource |
| | | private WxMaService wxMaService; |
| | |
| | | @Value("${wx.appSecret}") |
| | | private String appSecret; |
| | | |
| | | private final String BASE_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"; |
| | | @Value("${default.profilePicture}") |
| | | private String profilePicture; |
| | | |
| | | @Override |
| | | public R<String> decodeOpenid(HttpServletResponse response, String code) { |
| | |
| | | } |
| | | if (session != null && StringUtils.isNotBlank(session.getOpenid())) { |
| | | String openid = session.getOpenid(); |
| | | // String sessionKey = session.getSessionKey(); |
| | | /*String sessionKey = session.getSessionKey(); |
| | | // 加密明文及初始向量转义 |
| | | // encryptedData = URLEncoder.encode(encryptedData, "UTF-8").replace("%3D", "=").replace("%2F", "/"); |
| | | // ivStr = URLEncoder.encode(ivStr, "UTF-8").replace("%3D", "=").replace("%2F", "/"); |
| | | // // 获取用户信息 |
| | | // String result = WxAesUtils.decryptData(WxAesUtils.getUrlDecoderString(encryptedData), |
| | | // sessionKey, |
| | | // WxAesUtils.getUrlDecoderString(ivStr)); |
| | | // JSONObject userJson = JSONObject.parseObject(result); |
| | | encryptedData = URLEncoder.encode(encryptedData, "UTF-8").replace("%3D", "=").replace("%2F", "/"); |
| | | ivStr = URLEncoder.encode(ivStr, "UTF-8").replace("%3D", "=").replace("%2F", "/"); |
| | | // 获取用户信息 |
| | | String result = WxAesUtils.decryptData(WxAesUtils.getUrlDecoderString(encryptedData), |
| | | sessionKey, |
| | | WxAesUtils.getUrlDecoderString(ivStr)); |
| | | JSONObject userJson = JSONObject.parseObject(result);*/ |
| | | // 封装项目用户信息 |
| | | if (null != phoneNoInfo) { |
| | | String phoneNumber = phoneNoInfo.getPhoneNumber(); |
| | |
| | | if (null == user) { |
| | | user = new User(); |
| | | user.setUserNo(String.format(Constants.USER_NO_PRE, RandomUtil.randomNumbers(Constants.EIGHT))); |
| | | user.setState(Constants.ONE); |
| | | user.setPhone(phoneNumber); |
| | | user.setProfilePicture(profilePicture); |
| | | user.setNickname(String.format(Constants.USER_NO_PRE, StringUtils.getCharAndNum(Constants.SIX))); |
| | | user.setState(Constants.ONE); |
| | | user.setOpenId(openid); |
| | | user.setIsDelete(Constants.ZERO); |
| | | this.save(user); |
| | | } else { |
| | | if (user.getOpenId()!=null && !user.getOpenId().equals(openid)){ |
| | | user.setOpenId(openid); |
| | | this.updateById(user); |
| | | } |
| | | Integer state = user.getState(); |
| | | if (state == 0) { |
| | | throw new GlobalException("该账号未开启,无法进行登录!"); |
| | | throw new GlobalException("账号已被禁用,请联系平台管理员。"); |
| | | } |
| | | } |
| | | // 校验通过,生成token及过期时间 |
| | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public JSONObject getPhone(String code) { |
| | | // 授权(必填) |
| | | String grantType = "client_credential"; |
| | | // 向微信服务器 使用登录凭证 code 获取 session_key 和 openid |
| | | // 请求参数 |
| | | String params2 = "appid=" + appId + "&secret=" + appSecret + "&grant_type=" + grantType; |
| | | // 发送请求 |
| | | String sr2 = com.ruoyi.user.vx.utils.HttpRequest.sendGet("https://api.weixin.qq.com/cgi-bin/token", params2); |
| | | // 解析相应内容(转换成json对象) |
| | | JSONObject json2 = JSONObject.parseObject(sr2); |
| | | String accessToken = json2.getString("access_token"); |
| | | //使用获取到的token和接受到的code像微信获取手机号 |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("code", code); |
| | | String url = ("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken); |
| | | String sr3 = com.ruoyi.user.vx.utils.HttpRequest.sendPostNew(url, jsonObject); |
| | | JSONObject json = JSONObject.parseObject(sr3); |
| | | JSONObject phoneInfo = json.getJSONObject("phone_info"); |
| | | // return phoneInfo.getString("phoneNumber"); |
| | | return phoneInfo; |
| | | } |
| | | |
| | | } |