Jelajahi Sumber

获取不限制的小程序码

李天标 2 tahun lalu
induk
melakukan
3ebb7fedfc
1 mengubah file dengan 83 tambahan dan 4 penghapusan
  1. 83 4
      src/main/java/com/szwl/controller/TSzsmWxController.java

+ 83 - 4
src/main/java/com/szwl/controller/TSzsmWxController.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.szwl.constant.ResponseCodesEnum;
 import com.szwl.model.bo.R;
 import com.szwl.model.bo.ResponseModel;
 import com.szwl.model.entity.TSzsmWx;
@@ -20,11 +21,13 @@ import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 
 import org.springframework.web.bind.annotation.RestController;
+import sun.misc.BASE64Encoder;
 
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.*;
 
 /**
  * <p>
@@ -140,5 +143,81 @@ public class TSzsmWxController {
         }
         return R.ok();
     }
+    /**
+     * 获取不限制的小程序码
+     * @param
+     * @return
+     */
+    @GetMapping("/getQRCode")
+    public ResponseModel<?> getQRCode(String clientId) {
+        if(StringUtils.isEmpty(clientId)){
+            return R.fail(ResponseCodesEnum.A0001);
+        }
+        // 微信小程序ID
+        String appid = "wx5071443e63295c29";
+        // 微信小程序秘钥
+        String secret = "191b2fb5ad897c53aff19d2b40d677da";
+
+        String accessToken =null;
+        TSzsmWx wx = szsmWxService.getById(1);
+        accessToken = wx.getAvatarUrl();
+        //获取phone
+        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("scene", clientId);
+        String reqJsonStr = JsonUtil.objToString(jsonObject);
+        JSONObject  phoneObject = null;
+        try {
+            String s = httpPostWithJSON(url, reqJsonStr);
+            if(StringUtils.isEmpty(s)){
+                return R.fail("获取失败");
+            }else {
+                return R.ok(s);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        if (phoneObject == null) {
+            return R.fail("获取失败");
+        }
+        return R.ok();
+    }
+    public  static String httpPostWithJSON(String url, String json)
+            throws Exception {
+        String result = null;
+        URL target = new URL(url);
+        try {
+            HttpURLConnection httpURLConnection = (HttpURLConnection) target.openConnection();
+            httpURLConnection.setRequestMethod("GET");// 提交模式
+            // 发送POST请求必须设置如下两行
+            httpURLConnection.setDoOutput(true);
+            httpURLConnection.setDoInput(true);
+            // 获取URLConnection对象对应的输出流
+            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
+            printWriter.write(json.toString());
+            // flush输出流的缓冲
+            printWriter.flush();
+            InputStream instreams = httpURLConnection.getInputStream();
+            byte[] dest = new byte[1024];
+            byte[] bytes = new byte[httpURLConnection.getContentLength()];
+            int len;
+            int sum = 0;
+            while ((len = instreams.read(dest)) != -1) {
+                System.arraycopy(dest, 0, bytes, sum, len);
+                if (sum == httpURLConnection.getContentLength()) {
+                    break;
+                } else {
+                    sum = sum + len;
+                }
+            }
+            // 6. 断开连接,释放资源
+            httpURLConnection.disconnect();
+            // 将文件中的内容读入到数组中
+            Base64.Encoder encode = Base64.getEncoder();
+            result = "data:image/jpeg;base64,"+encode.encodeToString(bytes);
+        } catch (Exception e) {
+        }
+        return result;
+    }
 }