123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- package com.qmrb.system.service.impl;
- import cn.hutool.http.HttpUtil;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.qmrb.system.mapper.OrderMapper;
- import com.qmrb.system.pojo.dto.LoginParam;
- import com.qmrb.system.pojo.dto.UserParam;
- import com.qmrb.system.pojo.entity.Order;
- import com.qmrb.system.pojo.entity.ToolWxConfig;
- import com.qmrb.system.pojo.vo.UserInfo;
- import com.qmrb.system.pojo.vo.WxLoginVO;
- import com.qmrb.system.service.ToolWxConfigService;
- import com.qmrb.system.service.UserService;
- import com.qmrb.system.utils.QRCodeUtil;
- import com.qmrb.system.utils.WxPayUtil;
- import com.qmrb.system.wxpayback.Enum.WxApiType;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.codec.binary.Base64;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import javax.imageio.ImageIO;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintWriter;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- /**
- * <p>
- * 订单表 服务实现类
- * </p>
- *
- * @author 小王八
- * @since 2023-07-28
- */
- @Service
- @Slf4j
- public class UserServiceImpl extends ServiceImpl<OrderMapper, Order> implements UserService {
- @Autowired
- private ToolWxConfigService toolWxConfigService;
- public WxLoginVO getOpenId(ToolWxConfig wxConfig, WxLoginVO loginVO){
- //获取当前的openid
- Map hashMap = new HashMap();
- hashMap.put("appid", wxConfig.getAppId());
- hashMap.put("secret", wxConfig.getAppSecret());
- hashMap.put("js_code", loginVO.getCode());
- hashMap.put("grant_type", "authorization_code");
- String json = HttpUtil.get(WxApiType.WX_LOGIN_URL.getValue(), hashMap);
- JSONObject jsonObject = JSONUtil.parseObj(json);
- log.info("json:{}",jsonObject);
- String openid = jsonObject.getStr("openid");
- String sessionKey = jsonObject.getStr("session_key");
- WxLoginVO result = new WxLoginVO();
- result.setOpenId(openid);
- result.setSession_key(sessionKey);
- // result.setNickName(jsonObject.getStr("nickName"));
- // result.setAvatarUrl(jsonObject.getStr("avatarUrl"));
- // result.setGender(jsonObject.getInt("gender"));
- return result;
- }
- public String getWxAccessToken(ToolWxConfig wxConfig, String code){
- //获取当前的openid
- Map hashMap = new HashMap();
- hashMap.put("appid", wxConfig.getAppId());
- hashMap.put("secret", wxConfig.getAppSecret());
- // hashMap.put("js_code", code);
- hashMap.put("grant_type", "client_credential");
- String json = HttpUtil.get(WxApiType.WX_TOKEN.getValue(), hashMap);
- JSONObject jsonObject = JSONUtil.parseObj(json);
- log.info("json:{}",jsonObject);
- String access_token = jsonObject.getStr("access_token");
- return access_token;
- }
-
- public String getWxAccessToken(ToolWxConfig wxConfig){
- //获取当前的openid
- Map hashMap = new HashMap();
- hashMap.put("appid", wxConfig.getAppId());
- hashMap.put("secret", wxConfig.getAppSecret());
- // hashMap.put("js_code", code);
- hashMap.put("grant_type", "client_credential");
- String json = HttpUtil.get(WxApiType.WX_TOKEN.getValue(), hashMap);
- JSONObject jsonObject = JSONUtil.parseObj(json);
- log.info("json:{}",jsonObject);
- String access_token = jsonObject.getStr("access_token");
- return access_token;
- }
- public String getUserPhoneNumber(ToolWxConfig wxConfig, String code){
- String token = getWxAccessToken(wxConfig, code);
- //获取当前的openid
- Map hashMap = new HashMap();
- hashMap.put("code", code);
- String url = WxApiType.GET_MOBILE_URL.getValue()+"?access_token="+token;
- String json = HttpUtil.post(url, JSONUtil.toJsonStr(hashMap));
- JSONObject jsonObject = JSONUtil.parseObj(json);
- log.info("json:{}",jsonObject);
- String mobile = "";
- if(jsonObject != null && jsonObject.getJSONObject("phone_info") != null){
- JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
- // String phoneNumber = phoneInfo.getStr("phoneNumber");
- mobile = phoneInfo.getStr("purePhoneNumber");
-
- }
- return mobile;
- }
- @Override
- public String getCouponQRCode(ToolWxConfig conf, String key) {
- String token = getWxAccessToken(conf);
- //获取当前的openid
- // Map hashMap = new HashMap();
- // hashMap.put("page", "pages/index/index");
- // hashMap.put("scene", "contractId=" + key);
- // hashMap.put("env_version", "develop");
- // // hashMap.put("width", 430);
- // // hashMap.put("auto_color", false);
- // // hashMap.put("line_color", "{\"r\":\"0\",\"g\":\"0\",\"b\":\"0\"}");
- // // hashMap.put("is_hyaline", false);
- // String url = WxApiType.GET_QRCODE_URL.getValue()+"?access_token="+token;
- // String json = HttpUtil.post(url, JSONUtil.toJsonStr(hashMap));
- // log.info("获取微信二维码:{}",json);
- // byte[] bytes = json.getBytes();
- //
- //
- // String data = Base64.getEncoder().encodeToString(bytes);
- // return data;
- Map<String, Object> resultObj = new HashMap();
- PrintWriter out = null;
- //BufferedReader in = null;
- InputStream in = null;
- String result = "";
- try {
- URL realUrl = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token);
- // 打开和URL之间的连接
- URLConnection conn = realUrl.openConnection();
- // 设置通用的请求属性
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent",
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
- // 发送POST请求必须设置如下两行
- conn.setDoOutput(true);
- conn.setDoInput(true);
- // 获取URLConnection对象对应的输出流
- out = new PrintWriter(conn.getOutputStream());
- // 发送请求参数
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("scene", "contractId=" + key); //参数自定义
- jsonObject.put("page","pages/index/index");//要生成小程序码的链接
- jsonObject.put("width",300);
- jsonObject.put("env_version", conf.getEnvVersion());
- out.print(jsonObject);
- // flush输出流的缓冲
- out.flush();
- in = conn.getInputStream();
- byte[] data = null;
- // 读取图片字节数组
- try {
- ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
- byte[] buff = new byte[100];
- int rc = 0;
- while ((rc = in.read(buff, 0, 100)) > 0) {
- swapStream.write(buff, 0, rc);
- }
- data = swapStream.toByteArray();
- } catch (IOException e) {
- log.error("获取领券二维码出错",e);
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- log.error("获取领券二维码出错",e);
- }
- }
- }
- return new String(Base64.encodeBase64(data));
- } catch (Exception e) {
- System.out.println("发送 POST 请求出现异常!" + e);
- log.error("发送 POST 请求出现异常!",e);
- }
- // 使用finally块来关闭输出流、输入流
- finally {
- try {
- if (out != null) {
- out.close();
- }
- if (in != null) {
- in.close();
- }
- } catch (IOException ex) {
- log.error("关闭输出流、输入流!",ex);
- }
- }
- // resultObj.put("result",result);
- return result;
- }
- public UserInfo getMyInfo(UserParam userParam){
- UserInfo userInfo = new UserInfo();
- userInfo.setId("1");
- userInfo.setNickName("小王八");
- return userInfo;
- }
- /**
- * 获取头像登录
- * @param loginParam
- * @return
- */
- @Override
- public UserInfo wxMpLogin(LoginParam loginParam) {
- WxLoginVO loginVO = new WxLoginVO();
- loginVO.setCode(loginParam.getCode());
- WxLoginVO wxLoginVO = getOpenId(toolWxConfigService.findConf(), loginVO);
- String jsonStr = WxPayUtil.decryptS5(loginParam.getEncryptedData(), wxLoginVO.getSession_key(), loginParam.getIv());
- UserInfo userInfo = JSONUtil.toBean(jsonStr, UserInfo.class, true);
- userInfo.setOpenId(wxLoginVO.getOpenId());
- return userInfo;
- }
- }
|