New file |
| | |
| | | package com.qxueyou.scc.wx.service.impl; |
| | | |
| | | import com.alibaba.druid.util.HttpClientUtils; |
| | | import com.alibaba.druid.util.StringUtils; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.qxueyou.scc.base.model.Result; |
| | | import com.qxueyou.scc.base.service.ICacheService; |
| | | import com.qxueyou.scc.teach.student.model.StuStudent; |
| | | import com.qxueyou.scc.wx.service.IWechatService; |
| | | import com.qxueyou.scc.wx.utils.HttpClientUtil; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.io.IOException; |
| | | import java.net.URI; |
| | | import java.time.LocalDateTime; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | |
| | | @Service |
| | | public class WechatService implements IWechatService { |
| | | |
| | | // @Autowired |
| | | // IUserService userService; |
| | | |
| | | /** |
| | | * 小程序appID |
| | | */ |
| | | @Value("${wx.appId}") |
| | | private String appId; |
| | | |
| | | |
| | | /** |
| | | * 小程序secret |
| | | */ |
| | | @Value("${wx.secret}") |
| | | private String secret; |
| | | |
| | | |
| | | |
| | | @Autowired |
| | | private ICacheService cacheService; |
| | | |
| | | private final Logger logger = LogManager.getLogger(); |
| | | |
| | | |
| | | @Override |
| | | public JSONObject getSessionKeyOrOpenId(String code) { |
| | | String requestUrl = "https://api.weixin.qq.com/sns/jscode2session"; |
| | | Map<String, String> requestUrlParam = new HashMap<>(); |
| | | // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN |
| | | //小程序appId |
| | | requestUrlParam.put("appid", appId); |
| | | //小程序secret |
| | | requestUrlParam.put("secret", secret); |
| | | //小程序端返回的code |
| | | requestUrlParam.put("js_code", code); |
| | | //默认参数 |
| | | requestUrlParam.put("grant_type", "authorization_code"); |
| | | //发送post请求读取调用微信接口获取openid用户唯一标识 |
| | | JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam)); |
| | | return jsonObject; |
| | | } |
| | | |
| | | @Override |
| | | public Result getNumber(String tonken, String number) { |
| | | JSONObject jsonCode = new JSONObject(); |
| | | |
| | | jsonCode.put("code",number); |
| | | |
| | | |
| | | String resPhone = HttpClientUtil.doPostUrl("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + tonken, jsonCode); |
| | | |
| | | if(StringUtils.isEmpty(resPhone) || !resPhone.contains("phone_info") || !resPhone.contains("phoneNumber")){ |
| | | return new Result(false,"微信官方修改了小程序获取用户手机号码相关接口!"); |
| | | } |
| | | JSONObject resPhoneInfo = JSON.parseObject(resPhone); |
| | | JSONObject phoneInfo=resPhoneInfo.getJSONObject("phone_info"); |
| | | System.out.println(resPhoneInfo); |
| | | System.out.println(phoneInfo); |
| | | String phoneNumber = phoneInfo.get("phoneNumber").toString(); |
| | | |
| | | return new Result(true,"获取手机号成功",phoneNumber); |
| | | // String requestUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+tonken+jsonObject.toJSONString(); |
| | | |
| | | // Map<String, String> requestUrlParam = new HashMap<>(); |
| | | |
| | | |
| | | |
| | | // requestUrlParam.put("code", number); |
| | | |
| | | // JSONObject s =JSON.parseObject(HttpClientUtil.doPost(requestUrl)); |
| | | |
| | | // return s; |
| | | } |
| | | |
| | | @Override |
| | | public JSONObject gettoken(String appid, String secret) { |
| | | String requestUrl = "https://api.weixin.qq.com/cgi-bin/token"; |
| | | |
| | | Map<String, String> requestUrlParam = new HashMap<>(); |
| | | |
| | | requestUrlParam.put("grant_type", "client_credential"); |
| | | |
| | | requestUrlParam.put("appid", appid); |
| | | |
| | | requestUrlParam.put("secret", secret); |
| | | |
| | | JSONObject s = JSON.parseObject(HttpClientUtil.doGet(requestUrl, requestUrlParam)); |
| | | |
| | | return s; |
| | | } |
| | | @Override |
| | | public JSONObject getUser(String token, String openId) { |
| | | String requestUrl = "https://api.weixin.qq.com/sns/userinfo"; |
| | | |
| | | Map<String, String> requestUrlParam = new HashMap<>(); |
| | | |
| | | requestUrlParam.put("access_token", token); |
| | | |
| | | requestUrlParam.put("openid", openId); |
| | | |
| | | requestUrlParam.put("lang", "zh_CN"); |
| | | |
| | | JSONObject s = JSON.parseObject(HttpClientUtil.doGet(requestUrl, requestUrlParam)); |
| | | |
| | | return s; |
| | | } |
| | | // private String getWechatHeadImgUrl(String unionId) { |
| | | // WechatUserDO wxUser = this.template.findOne(Query.query(condition().and("unionId").is(unionId)), WechatUserDO.class); |
| | | // return wxUser.getHeadImgUrl(); |
| | | // } |
| | | |
| | | // @Override |
| | | // public Result isBindWx(String userId) { |
| | | // UserStudentDO studentDO = read(UserStudentDO.class, userId); |
| | | // boolean status = false; |
| | | // if (StringUtils.isNotBlank(studentDO.getUnionId())) { |
| | | // status = true; |
| | | // } |
| | | // String url = ""; |
| | | // if (status) { |
| | | // url = getWechatHeadImgUrl(studentDO.getUnionId()); |
| | | // } |
| | | // return new Result(true, "status", status, "url", url); |
| | | // } |
| | | // |
| | | // public Result getSignature(String url) { |
| | | // try { |
| | | // long timestamp = System.currentTimeMillis() / 1000; |
| | | // String randomStr = RandomUtils.getRandomStr(); |
| | | // //远程调用获取ticket |
| | | //// String jsapiTicket ="";//getJsapiTicket(false); |
| | | // RestTemplate restTemplate = new RestTemplate(); |
| | | // |
| | | // String jsapiTicket = restTemplate.getForObject("http://192.168.1.111/job/ticket", String.class); |
| | | // |
| | | // String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket, |
| | | // "noncestr=" + randomStr, "timestamp=" + timestamp, "url=" + url); |
| | | // WxJsapiSignature jsapiSignature = new WxJsapiSignature(); |
| | | // jsapiSignature.setAppId(wxProperties.getAppId()); |
| | | // jsapiSignature.setTimestamp(timestamp); |
| | | // jsapiSignature.setNonceStr(randomStr); |
| | | // jsapiSignature.setUrl(url); |
| | | // jsapiSignature.setSignature(signature); |
| | | // return new Result(true, jsapiSignature); |
| | | // } catch (Exception e) { |
| | | // log.error(e, e); |
| | | // return new Result(false, "获取签名失败"); |
| | | // } |
| | | // } |
| | | |
| | | // @Override |
| | | // public String getOpenId(String userId) { |
| | | // UserStudentDO user = read(UserStudentDO.class, userId); |
| | | // |
| | | // return user.getOpenId(); |
| | | // } |
| | | |
| | | |
| | | } |