| | |
| | | package com.ruoyi.user.service.impl; |
| | | |
| | | import cn.binarywang.wx.miniapp.api.WxMaService; |
| | | import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
| | | import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; |
| | | import cn.hutool.core.util.RandomUtil; |
| | | 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 TokenService tokenService; |
| | | @Resource |
| | | private WxMaService wxMaService; |
| | | |
| | | @Value("${wx.appId}") |
| | | private String appId; |
| | | |
| | | @Value("wx.appSecret") |
| | | @Value("${wx.appSecret}") |
| | | private String appSecret; |
| | | |
| | | @Value("${default.profilePicture}") |
| | | private String profilePicture; |
| | | |
| | | @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, 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("账号已被禁用,请联系平台管理员。"); |
| | | } |
| | | } |
| | | // 校验通过,生成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; |
| | | } |
| | | |
| | | |
| | | } |