TSzsmWxController.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package com.szwl.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.szwl.constant.ResponseCodesEnum;
  7. import com.szwl.model.bo.R;
  8. import com.szwl.model.bo.ResponseModel;
  9. import com.szwl.model.entity.TSzsmWx;
  10. import com.szwl.model.utils.HttpClientSslUtils;
  11. import com.szwl.model.utils.JsonUtil;
  12. import com.szwl.model.utils.WeChatUtil;
  13. import com.szwl.service.TSzsmWxService;
  14. import org.apache.commons.lang.StringUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.http.HttpStatus;
  17. import org.springframework.http.ResponseEntity;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import sun.misc.BASE64Encoder;
  22. import java.io.InputStream;
  23. import java.io.PrintWriter;
  24. import java.net.HttpURLConnection;
  25. import java.net.URL;
  26. import java.util.*;
  27. /**
  28. * <p>
  29. * 前端控制器
  30. * </p>
  31. *
  32. * @author wuhs
  33. * @since 2022-10-20
  34. */
  35. @RestController
  36. @RequestMapping("/tSzsmWx")
  37. public class TSzsmWxController {
  38. @Autowired
  39. TSzsmWxService szsmWxService;
  40. /**
  41. * 获取微信openid
  42. * @param
  43. * @return
  44. */
  45. @GetMapping("/getOpenid")
  46. public ResponseModel<?> getOpenid(String code) {
  47. // 微信小程序ID
  48. String appid = "wx5071443e63295c29";
  49. // 微信小程序秘钥
  50. String secret = "191b2fb5ad897c53aff19d2b40d677da";
  51. // 根据小程序穿过来的code想这个url发送请求
  52. String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
  53. // 发送请求,返回Json字符串
  54. String str = WeChatUtil.httpRequest(url, "GET", null);
  55. // 转成Json对象 获取openid
  56. JSONObject jsonObject = JSONObject.parseObject(str);
  57. // 我们需要的openid,在一个小程序中,openid是唯一的
  58. String openid = jsonObject.get("openid").toString();
  59. //根据openid去创建账户
  60. if(StringUtils.isNotEmpty(openid)){
  61. //查找是否已注册
  62. LambdaQueryWrapper<TSzsmWx> query = Wrappers.lambdaQuery();
  63. query.eq(TSzsmWx::getOpenId,openid);
  64. List<TSzsmWx> list = szsmWxService.list(query);
  65. if(list.size()>0){
  66. return R.ok(list.get(0));
  67. }else {
  68. TSzsmWx wx = new TSzsmWx();
  69. wx.setCreateDate(new Date());
  70. wx.setModifyDate(new Date());
  71. wx.setOpenId(openid);
  72. szsmWxService.save(wx);
  73. return R.ok(wx);
  74. }
  75. }
  76. return R.ok();
  77. }
  78. /**
  79. * 获取微信openid
  80. * @param
  81. * @return
  82. */
  83. @GetMapping("/getPhone")
  84. public ResponseModel<?> getPhone(String code,String id) {
  85. // 微信小程序ID
  86. String appid = "wx5071443e63295c29";
  87. // 微信小程序秘钥
  88. String secret = "191b2fb5ad897c53aff19d2b40d677da";
  89. // //1,获取获取token
  90. // String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
  91. // JSONObject token = null;
  92. // try {
  93. // token = JSON.parseObject(HttpClientSslUtils.doGet(token_url));
  94. // } catch (Exception e) {
  95. // e.printStackTrace();
  96. // }
  97. // if (token == null) {
  98. // return R.ok("获取token失败");
  99. // }
  100. // String accessToken = token.getString("access_token");
  101. // if (StringUtils.isEmpty(accessToken)) {
  102. // return R.ok("获取token失败");
  103. // }
  104. String accessToken =null;
  105. TSzsmWx wx = szsmWxService.getById(1);
  106. accessToken = wx.getAvatarUrl();
  107. //获取phone
  108. String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"
  109. + "?access_token=" + accessToken;
  110. JSONObject jsonObject = new JSONObject();
  111. jsonObject.put("code", code);
  112. String reqJsonStr = JsonUtil.objToString(jsonObject);
  113. JSONObject phoneObject = null;
  114. try {
  115. phoneObject = JSON.parseObject(HttpClientSslUtils.doPost(url, reqJsonStr));
  116. if (phoneObject != null) {
  117. String errmsg = phoneObject.getString("errmsg");
  118. if(errmsg.equals("ok")){
  119. String phone_info = phoneObject.getString("phone_info");
  120. JSONObject phoneInfo = JSONObject.parseObject(phone_info);
  121. String phoneNumber = phoneInfo.getString("phoneNumber");
  122. if(StringUtils.isNotEmpty(phoneNumber)){
  123. TSzsmWx szsmWx = szsmWxService.getById(id);
  124. szsmWx.setPhone(phoneNumber);
  125. szsmWxService.save(szsmWx);
  126. return R.ok(szsmWx);
  127. }
  128. }
  129. }
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. }
  133. if (phoneObject == null) {
  134. return R.ok("获取手机号失败");
  135. }
  136. return R.ok();
  137. }
  138. /**
  139. * 获取不限制的小程序码
  140. * @param
  141. * @return
  142. */
  143. @GetMapping("/getQRCode")
  144. public ResponseModel<?> getQRCode(String clientId) {
  145. if(StringUtils.isEmpty(clientId)){
  146. return R.fail(ResponseCodesEnum.A0001);
  147. }
  148. // 微信小程序ID
  149. String appid = "wx5071443e63295c29";
  150. // 微信小程序秘钥
  151. String secret = "191b2fb5ad897c53aff19d2b40d677da";
  152. String accessToken =null;
  153. TSzsmWx wx = szsmWxService.getById(1);
  154. accessToken = wx.getAvatarUrl();
  155. //获取phone
  156. String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
  157. JSONObject jsonObject = new JSONObject();
  158. jsonObject.put("scene", clientId);
  159. String reqJsonStr = JsonUtil.objToString(jsonObject);
  160. JSONObject phoneObject = null;
  161. try {
  162. String s = httpPostWithJSON(url, reqJsonStr);
  163. if(StringUtils.isEmpty(s)){
  164. return R.fail("获取失败");
  165. }else {
  166. return R.ok(s);
  167. }
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. }
  171. if (phoneObject == null) {
  172. return R.fail("获取失败");
  173. }
  174. return R.ok();
  175. }
  176. public static String httpPostWithJSON(String url, String json)
  177. throws Exception {
  178. String result = null;
  179. URL target = new URL(url);
  180. try {
  181. HttpURLConnection httpURLConnection = (HttpURLConnection) target.openConnection();
  182. httpURLConnection.setRequestMethod("GET");// 提交模式
  183. // 发送POST请求必须设置如下两行
  184. httpURLConnection.setDoOutput(true);
  185. httpURLConnection.setDoInput(true);
  186. // 获取URLConnection对象对应的输出流
  187. PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
  188. printWriter.write(json.toString());
  189. // flush输出流的缓冲
  190. printWriter.flush();
  191. InputStream instreams = httpURLConnection.getInputStream();
  192. byte[] dest = new byte[1024];
  193. byte[] bytes = new byte[httpURLConnection.getContentLength()];
  194. int len;
  195. int sum = 0;
  196. while ((len = instreams.read(dest)) != -1) {
  197. System.arraycopy(dest, 0, bytes, sum, len);
  198. if (sum == httpURLConnection.getContentLength()) {
  199. break;
  200. } else {
  201. sum = sum + len;
  202. }
  203. }
  204. // 6. 断开连接,释放资源
  205. httpURLConnection.disconnect();
  206. // 将文件中的内容读入到数组中
  207. Base64.Encoder encode = Base64.getEncoder();
  208. result = "data:image/jpeg;base64,"+encode.encodeToString(bytes);
  209. } catch (Exception e) {
  210. }
  211. return result;
  212. }
  213. }