UserServiceImpl.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package com.qmrb.system.service.impl;
  2. import cn.hutool.http.HttpUtil;
  3. import cn.hutool.json.JSONObject;
  4. import cn.hutool.json.JSONUtil;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.qmrb.system.mapper.OrderMapper;
  7. import com.qmrb.system.pojo.dto.LoginParam;
  8. import com.qmrb.system.pojo.dto.UserParam;
  9. import com.qmrb.system.pojo.entity.Order;
  10. import com.qmrb.system.pojo.entity.ToolWxConfig;
  11. import com.qmrb.system.pojo.vo.UserInfo;
  12. import com.qmrb.system.pojo.vo.WxLoginVO;
  13. import com.qmrb.system.service.ToolWxConfigService;
  14. import com.qmrb.system.service.UserService;
  15. import com.qmrb.system.utils.QRCodeUtil;
  16. import com.qmrb.system.utils.WxPayUtil;
  17. import com.qmrb.system.wxpayback.Enum.WxApiType;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.codec.binary.Base64;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import javax.imageio.ImageIO;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.PrintWriter;
  27. import java.net.URL;
  28. import java.net.URLConnection;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. import java.util.UUID;
  32. /**
  33. * <p>
  34. * 订单表 服务实现类
  35. * </p>
  36. *
  37. * @author 小王八
  38. * @since 2023-07-28
  39. */
  40. @Service
  41. @Slf4j
  42. public class UserServiceImpl extends ServiceImpl<OrderMapper, Order> implements UserService {
  43. @Autowired
  44. private ToolWxConfigService toolWxConfigService;
  45. public WxLoginVO getOpenId(ToolWxConfig wxConfig, WxLoginVO loginVO){
  46. //获取当前的openid
  47. Map hashMap = new HashMap();
  48. hashMap.put("appid", wxConfig.getAppId());
  49. hashMap.put("secret", wxConfig.getAppSecret());
  50. hashMap.put("js_code", loginVO.getCode());
  51. hashMap.put("grant_type", "authorization_code");
  52. String json = HttpUtil.get(WxApiType.WX_LOGIN_URL.getValue(), hashMap);
  53. JSONObject jsonObject = JSONUtil.parseObj(json);
  54. log.info("json:{}",jsonObject);
  55. String openid = jsonObject.getStr("openid");
  56. String sessionKey = jsonObject.getStr("session_key");
  57. WxLoginVO result = new WxLoginVO();
  58. result.setOpenId(openid);
  59. result.setSession_key(sessionKey);
  60. // result.setNickName(jsonObject.getStr("nickName"));
  61. // result.setAvatarUrl(jsonObject.getStr("avatarUrl"));
  62. // result.setGender(jsonObject.getInt("gender"));
  63. return result;
  64. }
  65. public String getWxAccessToken(ToolWxConfig wxConfig, String code){
  66. //获取当前的openid
  67. Map hashMap = new HashMap();
  68. hashMap.put("appid", wxConfig.getAppId());
  69. hashMap.put("secret", wxConfig.getAppSecret());
  70. // hashMap.put("js_code", code);
  71. hashMap.put("grant_type", "client_credential");
  72. String json = HttpUtil.get(WxApiType.WX_TOKEN.getValue(), hashMap);
  73. JSONObject jsonObject = JSONUtil.parseObj(json);
  74. log.info("json:{}",jsonObject);
  75. String access_token = jsonObject.getStr("access_token");
  76. return access_token;
  77. }
  78. public String getWxAccessToken(ToolWxConfig wxConfig){
  79. //获取当前的openid
  80. Map hashMap = new HashMap();
  81. hashMap.put("appid", wxConfig.getAppId());
  82. hashMap.put("secret", wxConfig.getAppSecret());
  83. // hashMap.put("js_code", code);
  84. hashMap.put("grant_type", "client_credential");
  85. String json = HttpUtil.get(WxApiType.WX_TOKEN.getValue(), hashMap);
  86. JSONObject jsonObject = JSONUtil.parseObj(json);
  87. log.info("json:{}",jsonObject);
  88. String access_token = jsonObject.getStr("access_token");
  89. return access_token;
  90. }
  91. public String getUserPhoneNumber(ToolWxConfig wxConfig, String code){
  92. String token = getWxAccessToken(wxConfig, code);
  93. //获取当前的openid
  94. Map hashMap = new HashMap();
  95. hashMap.put("code", code);
  96. String url = WxApiType.GET_MOBILE_URL.getValue()+"?access_token="+token;
  97. String json = HttpUtil.post(url, JSONUtil.toJsonStr(hashMap));
  98. JSONObject jsonObject = JSONUtil.parseObj(json);
  99. log.info("json:{}",jsonObject);
  100. String mobile = "";
  101. if(jsonObject != null && jsonObject.getJSONObject("phone_info") != null){
  102. JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
  103. // String phoneNumber = phoneInfo.getStr("phoneNumber");
  104. mobile = phoneInfo.getStr("purePhoneNumber");
  105. }
  106. return mobile;
  107. }
  108. @Override
  109. public String getCouponQRCode(ToolWxConfig conf, String key) {
  110. String token = getWxAccessToken(conf);
  111. //获取当前的openid
  112. // Map hashMap = new HashMap();
  113. // hashMap.put("page", "pages/index/index");
  114. // hashMap.put("scene", "contractId=" + key);
  115. // hashMap.put("env_version", "develop");
  116. // // hashMap.put("width", 430);
  117. // // hashMap.put("auto_color", false);
  118. // // hashMap.put("line_color", "{\"r\":\"0\",\"g\":\"0\",\"b\":\"0\"}");
  119. // // hashMap.put("is_hyaline", false);
  120. // String url = WxApiType.GET_QRCODE_URL.getValue()+"?access_token="+token;
  121. // String json = HttpUtil.post(url, JSONUtil.toJsonStr(hashMap));
  122. // log.info("获取微信二维码:{}",json);
  123. // byte[] bytes = json.getBytes();
  124. //
  125. //
  126. // String data = Base64.getEncoder().encodeToString(bytes);
  127. // return data;
  128. Map<String, Object> resultObj = new HashMap();
  129. PrintWriter out = null;
  130. //BufferedReader in = null;
  131. InputStream in = null;
  132. String result = "";
  133. try {
  134. URL realUrl = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token);
  135. // 打开和URL之间的连接
  136. URLConnection conn = realUrl.openConnection();
  137. // 设置通用的请求属性
  138. conn.setRequestProperty("accept", "*/*");
  139. conn.setRequestProperty("connection", "Keep-Alive");
  140. conn.setRequestProperty("user-agent",
  141. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  142. // 发送POST请求必须设置如下两行
  143. conn.setDoOutput(true);
  144. conn.setDoInput(true);
  145. // 获取URLConnection对象对应的输出流
  146. out = new PrintWriter(conn.getOutputStream());
  147. // 发送请求参数
  148. JSONObject jsonObject = new JSONObject();
  149. jsonObject.put("scene", "contractId=" + key); //参数自定义
  150. jsonObject.put("page","pages/index/index");//要生成小程序码的链接
  151. jsonObject.put("width",300);
  152. jsonObject.put("env_version", conf.getEnvVersion());
  153. out.print(jsonObject);
  154. // flush输出流的缓冲
  155. out.flush();
  156. in = conn.getInputStream();
  157. byte[] data = null;
  158. // 读取图片字节数组
  159. try {
  160. ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
  161. byte[] buff = new byte[100];
  162. int rc = 0;
  163. while ((rc = in.read(buff, 0, 100)) > 0) {
  164. swapStream.write(buff, 0, rc);
  165. }
  166. data = swapStream.toByteArray();
  167. } catch (IOException e) {
  168. log.error("获取领券二维码出错",e);
  169. } finally {
  170. if (in != null) {
  171. try {
  172. in.close();
  173. } catch (IOException e) {
  174. log.error("获取领券二维码出错",e);
  175. }
  176. }
  177. }
  178. return new String(Base64.encodeBase64(data));
  179. } catch (Exception e) {
  180. System.out.println("发送 POST 请求出现异常!" + e);
  181. log.error("发送 POST 请求出现异常!",e);
  182. }
  183. // 使用finally块来关闭输出流、输入流
  184. finally {
  185. try {
  186. if (out != null) {
  187. out.close();
  188. }
  189. if (in != null) {
  190. in.close();
  191. }
  192. } catch (IOException ex) {
  193. log.error("关闭输出流、输入流!",ex);
  194. }
  195. }
  196. // resultObj.put("result",result);
  197. return result;
  198. }
  199. public UserInfo getMyInfo(UserParam userParam){
  200. UserInfo userInfo = new UserInfo();
  201. userInfo.setId("1");
  202. userInfo.setNickName("小王八");
  203. return userInfo;
  204. }
  205. /**
  206. * 获取头像登录
  207. * @param loginParam
  208. * @return
  209. */
  210. @Override
  211. public UserInfo wxMpLogin(LoginParam loginParam) {
  212. WxLoginVO loginVO = new WxLoginVO();
  213. loginVO.setCode(loginParam.getCode());
  214. WxLoginVO wxLoginVO = getOpenId(toolWxConfigService.findConf(), loginVO);
  215. String jsonStr = WxPayUtil.decryptS5(loginParam.getEncryptedData(), wxLoginVO.getSession_key(), loginParam.getIv());
  216. UserInfo userInfo = JSONUtil.toBean(jsonStr, UserInfo.class, true);
  217. userInfo.setOpenId(wxLoginVO.getOpenId());
  218. return userInfo;
  219. }
  220. }