TSzsmWxController.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. LambdaQueryWrapper<TSzsmWx> query1 = Wrappers.lambdaQuery();
  74. query1.eq(TSzsmWx::getOpenId,openid);
  75. List<TSzsmWx> list1 = szsmWxService.list(query1);
  76. return R.ok(list1.get(0));
  77. }
  78. }
  79. return R.ok();
  80. }
  81. /**
  82. * 获取手机号
  83. * @param
  84. * @return
  85. */
  86. @GetMapping("/getPhone")
  87. public ResponseModel<?> getPhone(String code,String id) {
  88. // 微信小程序ID
  89. String appid = "wx5071443e63295c29";
  90. // 微信小程序秘钥
  91. String secret = "191b2fb5ad897c53aff19d2b40d677da";
  92. String accessToken =null;
  93. TSzsmWx wx = szsmWxService.getById(1);
  94. accessToken = wx.getAvatarUrl();
  95. //获取phone
  96. String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"
  97. + "?access_token=" + accessToken;
  98. JSONObject jsonObject = new JSONObject();
  99. jsonObject.put("code", code);
  100. String reqJsonStr = JsonUtil.objToString(jsonObject);
  101. JSONObject phoneObject = null;
  102. try {
  103. phoneObject = JSON.parseObject(HttpClientSslUtils.doPost(url, reqJsonStr));
  104. if (phoneObject != null) {
  105. String errmsg = phoneObject.getString("errmsg");
  106. if(errmsg.equals("ok")){
  107. String phone_info = phoneObject.getString("phone_info");
  108. JSONObject phoneInfo = JSONObject.parseObject(phone_info);
  109. String phoneNumber = phoneInfo.getString("phoneNumber");
  110. if(StringUtils.isNotEmpty(phoneNumber)){
  111. TSzsmWx szsmWx = szsmWxService.getById(id);
  112. szsmWx.setPhone(phoneNumber);
  113. szsmWxService.updateById(szsmWx);
  114. return R.ok(szsmWx);
  115. }
  116. }
  117. }
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. }
  121. if (phoneObject == null) {
  122. return R.ok("获取手机号失败");
  123. }
  124. return R.ok();
  125. }
  126. /**
  127. * 获取不限制的小程序码 +优惠券
  128. * @param
  129. * @return
  130. */
  131. @GetMapping("/getQRCodeAndYHJ")
  132. public ResponseModel<?> getQRCodeAndYHJ(String clientId,String flag) {
  133. if(StringUtils.isEmpty(clientId)||StringUtils.isEmpty(flag)){
  134. return R.fail(ResponseCodesEnum.A0001);
  135. }
  136. // 微信小程序ID
  137. String appid = "wx5071443e63295c29";
  138. // 微信小程序秘钥
  139. String secret = "191b2fb5ad897c53aff19d2b40d677da";
  140. String accessToken =null;
  141. TSzsmWx wx = szsmWxService.getById(1);
  142. accessToken = wx.getAvatarUrl();
  143. //获取phone
  144. String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
  145. JSONObject jsonObject = new JSONObject();
  146. jsonObject.put("scene", clientId+"-"+flag);
  147. // jsonObject.put("env_version", "trial");
  148. String reqJsonStr = JsonUtil.objToString(jsonObject);
  149. JSONObject phoneObject = null;
  150. try {
  151. String s = httpPostWithJSON(url, reqJsonStr);
  152. if(StringUtils.isEmpty(s)){
  153. return R.fail("获取失败");
  154. }else {
  155. return R.ok(s);
  156. }
  157. } catch (Exception e) {
  158. e.printStackTrace();
  159. }
  160. if (phoneObject == null) {
  161. return R.fail("获取失败");
  162. }
  163. return R.ok();
  164. }
  165. /**
  166. * 获取不限制的小程序码
  167. * @param
  168. * @return
  169. */
  170. @GetMapping("/getQRCode")
  171. public ResponseModel<?> getQRCode(String clientId) {
  172. if(StringUtils.isEmpty(clientId)){
  173. return R.fail(ResponseCodesEnum.A0001);
  174. }
  175. // 微信小程序ID
  176. String appid = "wx5071443e63295c29";
  177. // 微信小程序秘钥
  178. String secret = "191b2fb5ad897c53aff19d2b40d677da";
  179. String accessToken =null;
  180. TSzsmWx wx = szsmWxService.getById(1);
  181. accessToken = wx.getAvatarUrl();
  182. //获取phone
  183. String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
  184. JSONObject jsonObject = new JSONObject();
  185. jsonObject.put("scene", clientId);
  186. String reqJsonStr = JsonUtil.objToString(jsonObject);
  187. JSONObject phoneObject = null;
  188. try {
  189. String s = httpPostWithJSON(url, reqJsonStr);
  190. if(StringUtils.isEmpty(s)){
  191. return R.fail("获取失败");
  192. }else {
  193. return R.ok(s);
  194. }
  195. } catch (Exception e) {
  196. e.printStackTrace();
  197. }
  198. if (phoneObject == null) {
  199. return R.fail("获取失败");
  200. }
  201. return R.ok();
  202. }
  203. public static String httpPostWithJSON(String url, String json)
  204. throws Exception {
  205. String result = null;
  206. URL target = new URL(url);
  207. try {
  208. HttpURLConnection httpURLConnection = (HttpURLConnection) target.openConnection();
  209. httpURLConnection.setRequestMethod("GET");// 提交模式
  210. // 发送POST请求必须设置如下两行
  211. httpURLConnection.setDoOutput(true);
  212. httpURLConnection.setDoInput(true);
  213. // 获取URLConnection对象对应的输出流
  214. PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
  215. printWriter.write(json.toString());
  216. // flush输出流的缓冲
  217. printWriter.flush();
  218. InputStream instreams = httpURLConnection.getInputStream();
  219. byte[] dest = new byte[1024];
  220. byte[] bytes = new byte[httpURLConnection.getContentLength()];
  221. int len;
  222. int sum = 0;
  223. while ((len = instreams.read(dest)) != -1) {
  224. System.arraycopy(dest, 0, bytes, sum, len);
  225. if (sum == httpURLConnection.getContentLength()) {
  226. break;
  227. } else {
  228. sum = sum + len;
  229. }
  230. }
  231. // 6. 断开连接,释放资源
  232. httpURLConnection.disconnect();
  233. // 将文件中的内容读入到数组中
  234. Base64.Encoder encode = Base64.getEncoder();
  235. result = "data:image/jpeg;base64,"+encode.encodeToString(bytes);
  236. } catch (Exception e) {
  237. }
  238. return result;
  239. }
  240. /**
  241. * 获取用户信息
  242. * @param
  243. * @return
  244. */
  245. @GetMapping("/getUserInfo")
  246. public ResponseModel<?> getUserInfo(String id) {
  247. if(StringUtils.isEmpty(id)){
  248. return R.fail(ResponseCodesEnum.A0001);
  249. }
  250. TSzsmWx szsmWx = szsmWxService.getById(id);
  251. return R.ok(szsmWx);
  252. }
  253. /**
  254. * 设置订阅取餐提醒
  255. * @param
  256. * @return
  257. */
  258. @GetMapping("/setDingYue")
  259. public ResponseModel<?> setDingYue(String id) {
  260. TSzsmWx szsmWx = szsmWxService.getById(id);
  261. szsmWx.setIfSubscribe("1");
  262. szsmWxService.updateById(szsmWx);
  263. return R.ok();
  264. }
  265. }