123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- package com.szwl.controller;
- 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;
- import com.szwl.model.utils.HttpClientSslUtils;
- import com.szwl.model.utils.JsonUtil;
- import com.szwl.model.utils.WeChatUtil;
- import com.szwl.service.TSzsmWxService;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- 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.io.InputStream;
- import java.io.PrintWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.*;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author wuhs
- * @since 2022-10-20
- */
- @RestController
- @RequestMapping("/tSzsmWx")
- public class TSzsmWxController {
- @Autowired
- TSzsmWxService szsmWxService;
- /**
- * 获取微信openid
- * @param
- * @return
- */
- @GetMapping("/getOpenid")
- public ResponseModel<?> getOpenid(String code) {
- // 微信小程序ID
- String appid = "wx5071443e63295c29";
- // 微信小程序秘钥
- String secret = "191b2fb5ad897c53aff19d2b40d677da";
- // 根据小程序穿过来的code想这个url发送请求
- String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
- // 发送请求,返回Json字符串
- String str = WeChatUtil.httpRequest(url, "GET", null);
- // 转成Json对象 获取openid
- JSONObject jsonObject = JSONObject.parseObject(str);
- // 我们需要的openid,在一个小程序中,openid是唯一的
- String openid = jsonObject.get("openid").toString();
- //根据openid去创建账户
- if(StringUtils.isNotEmpty(openid)){
- //查找是否已注册
- LambdaQueryWrapper<TSzsmWx> query = Wrappers.lambdaQuery();
- query.eq(TSzsmWx::getOpenId,openid);
- List<TSzsmWx> list = szsmWxService.list(query);
- if(list.size()>0){
- return R.ok(list.get(0));
- }else {
- TSzsmWx wx = new TSzsmWx();
- wx.setCreateDate(new Date());
- wx.setModifyDate(new Date());
- wx.setOpenId(openid);
- szsmWxService.save(wx);
- LambdaQueryWrapper<TSzsmWx> query1 = Wrappers.lambdaQuery();
- query1.eq(TSzsmWx::getOpenId,openid);
- List<TSzsmWx> list1 = szsmWxService.list(query1);
- return R.ok(list1.get(0));
- }
- }
- return R.ok();
- }
- /**
- * 获取手机号
- * @param
- * @return
- */
- @GetMapping("/getPhone")
- public ResponseModel<?> getPhone(String code,String id) {
- // 微信小程序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/business/getuserphonenumber"
- + "?access_token=" + accessToken;
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("code", code);
- String reqJsonStr = JsonUtil.objToString(jsonObject);
- JSONObject phoneObject = null;
- try {
- phoneObject = JSON.parseObject(HttpClientSslUtils.doPost(url, reqJsonStr));
- if (phoneObject != null) {
- String errmsg = phoneObject.getString("errmsg");
- if(errmsg.equals("ok")){
- String phone_info = phoneObject.getString("phone_info");
- JSONObject phoneInfo = JSONObject.parseObject(phone_info);
- String phoneNumber = phoneInfo.getString("phoneNumber");
- if(StringUtils.isNotEmpty(phoneNumber)){
- TSzsmWx szsmWx = szsmWxService.getById(id);
- szsmWx.setPhone(phoneNumber);
- szsmWxService.updateById(szsmWx);
- return R.ok(szsmWx);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (phoneObject == null) {
- return R.ok("获取手机号失败");
- }
- return R.ok();
- }
- /**
- * 获取不限制的小程序码 +优惠券
- * @param
- * @return
- */
- @GetMapping("/getQRCodeAndYHJ")
- public ResponseModel<?> getQRCodeAndYHJ(String clientId,String flag) {
- if(StringUtils.isEmpty(clientId)||StringUtils.isEmpty(flag)){
- 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+"-"+flag);
- // jsonObject.put("env_version", "trial");
- 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();
- }
- /**
- * 获取不限制的小程序码
- * @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;
- }
- /**
- * 获取用户信息
- * @param
- * @return
- */
- @GetMapping("/getUserInfo")
- public ResponseModel<?> getUserInfo(String id) {
- if(StringUtils.isEmpty(id)){
- return R.fail(ResponseCodesEnum.A0001);
- }
- TSzsmWx szsmWx = szsmWxService.getById(id);
- return R.ok(szsmWx);
- }
- /**
- * 设置订阅取餐提醒
- * @param
- * @return
- */
- @GetMapping("/setDingYue")
- public ResponseModel<?> setDingYue(String id) {
- TSzsmWx szsmWx = szsmWxService.getById(id);
- szsmWx.setIfSubscribe("1");
- szsmWxService.updateById(szsmWx);
- return R.ok();
- }
- }
|