| | |
| | | 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.http.HttpRequest; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.core.constant.Constants; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.exception.GlobalException; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.security.service.TokenService; |
| | | import com.ruoyi.system.api.model.LoginUserInfo; |
| | | import com.ruoyi.user.entity.User; |
| | | import com.ruoyi.user.mapper.UserMapper; |
| | | import com.ruoyi.user.service.UserService; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.HashMap; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { |
| | | |
| | | @Value("wx.appId") |
| | | @Resource |
| | | private WxMaUserService wxMaUserService; |
| | | @Resource |
| | | private TokenService tokenService; |
| | | @Resource |
| | | private WxMaService wxMaService; |
| | | |
| | | @Value("${wx.appId}") |
| | | private String appId; |
| | | |
| | | @Value("wx.appSecret") |
| | | @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"; |
| | | |
| | | @Override |
| | | public R<String> decodeOpenid(HttpServletResponse response, String code) { |
| | |
| | | JSONObject json = JSONObject.parseObject(res); |
| | | //获取openid |
| | | String openid = json.getString("openid"); |
| | | return R.ok(openid); |
| | | return R.ok(openid, ""); |
| | | } catch (Exception e) { |
| | | return R.fail("openId生成失败"); |
| | | return R.fail("openId生成失败!"); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public HashMap<String, Object> decodeUserInfo(String code, String encryptedData, String ivStr) { |
| | | WxMaJscode2SessionResult session = null; |
| | | WxMaPhoneNumberInfo phoneNoInfo = null; |
| | | //获取session |
| | | try { |
| | | session = wxMaService.getUserService().getSessionInfo(code); |
| | | String sessionKey = session.getSessionKey(); |
| | | phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, ivStr); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (session != null && StringUtils.isNotBlank(session.getOpenid())) { |
| | | String openid = session.getOpenid(); |
| | | /*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);*/ |
| | | // 封装项目用户信息 |
| | | if (null != phoneNoInfo) { |
| | | String phoneNumber = phoneNoInfo.getPhoneNumber(); |
| | | User user = this.lambdaQuery().eq(User::getPhone, phoneNumber) |
| | | .eq(User::getIsDelete, 0).one(); |
| | | if (null == user) { |
| | | user = new User(); |
| | | user.setUserNo(String.format(Constants.USER_NO_PRE, StringUtils.getCharAndNum(Constants.SIX))); |
| | | user.setPhone(phoneNumber); |
| | | user.setState(Constants.ONE); |
| | | user.setOpenId(openid); |
| | | user.setIsDelete(Constants.ZERO); |
| | | this.save(user); |
| | | } else { |
| | | Integer state = user.getState(); |
| | | if (state == 0) { |
| | | throw new GlobalException("该账号未开启,无法进行登录!"); |
| | | } |
| | | } |
| | | // 校验通过,生成token及过期时间 |
| | | LoginUserInfo loginUserInfo = new LoginUserInfo(); |
| | | loginUserInfo.setName(user.getPhone()); |
| | | loginUserInfo.setUserid(user.getId()); |
| | | loginUserInfo.setPhone(user.getPhone()); |
| | | loginUserInfo.setLoginTime(System.currentTimeMillis()); |
| | | HashMap<String, Object> map = new HashMap<>(8); |
| | | map.put("token", tokenService.createTokenByUser(loginUserInfo)); |
| | | return map; |
| | | } |
| | | } |
| | | 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; |
| | | } |
| | | |
| | | } |