|
@@ -0,0 +1,378 @@
|
|
|
+package com.usky.iot.controller.web;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.usky.common.core.bean.ApiResult;
|
|
|
+import com.usky.common.core.exception.BusinessException;
|
|
|
+import com.usky.common.core.util.HttpUtils;
|
|
|
+import com.usky.common.redis.core.RedisHelper;
|
|
|
+import com.usky.iot.constant.constant;
|
|
|
+import com.usky.iot.domain.SysWxOpuser;
|
|
|
+import com.usky.iot.service.SysWxOpuserService;
|
|
|
+import com.usky.iot.service.vo.SendWeChatMessageRequestVO;
|
|
|
+import com.usky.iot.service.vo.SignUpRequestVO;
|
|
|
+import com.usky.iot.service.vo.TemplateMsgEntityVO;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import ma.glasnost.orika.impl.util.StringUtil;
|
|
|
+import me.chanjar.weixin.common.api.WxConsts;
|
|
|
+import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
|
|
+import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
+import me.chanjar.weixin.mp.enums.WxMpApiUrl;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.ExitCodeEvent;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.servlet.view.RedirectView;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/weChat")
|
|
|
+@Slf4j
|
|
|
+public class WeChatController {
|
|
|
+ @Autowired
|
|
|
+ private WxMpService wxMpService;
|
|
|
+ @Resource
|
|
|
+ private HttpServletRequest request;
|
|
|
+ @Resource
|
|
|
+ private HttpServletResponse response;
|
|
|
+ @Autowired
|
|
|
+ private SysWxOpuserService sysWxOpuserService;
|
|
|
+ @Autowired
|
|
|
+ private RedisHelper redisHelper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用的第一个接口,获取微信公众号CODE,获取openid
|
|
|
+ */
|
|
|
+ @GetMapping("/getFirst1")
|
|
|
+ public void getFirst1(){
|
|
|
+ String http = "https://";
|
|
|
+ String code = request.getParameter("code");
|
|
|
+ String userAgent = request.getHeader("User-Agent");
|
|
|
+
|
|
|
+ if(userAgent.contains("MicroMessenger")){ //wx
|
|
|
+ request.getSession().setAttribute("type","wx");
|
|
|
+ if(StringUtils.isBlank(code)){
|
|
|
+ String url = URLDecoder.decode(http+constant.call_back_domain+"/mobile/#/pages"+request.getRequestURI());
|
|
|
+ String sendUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+constant.WE_CHAT_APP_ID+"&redirect_uri="+url+"&response_type=code&scope=snsapi_base&state=abc123#wechat_redirect";
|
|
|
+ try{
|
|
|
+ System.out.println("sendUrl: "+sendUrl);
|
|
|
+ response.sendRedirect(sendUrl);
|
|
|
+ }catch (IOException e){
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ System.out.println("code: "+code);
|
|
|
+ try {
|
|
|
+ WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
|
|
|
+
|
|
|
+ String openid = wxOAuth2AccessToken.getOpenId();
|
|
|
+ String access_token = wxOAuth2AccessToken.getAccessToken();
|
|
|
+ System.out.println("openid: "+openid);
|
|
|
+ request.getSession().setAttribute("openid",openid);
|
|
|
+ LambdaQueryWrapper<SysWxOpuser> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.select(SysWxOpuser::getPhone)
|
|
|
+ .eq(SysWxOpuser::getStatus,1)
|
|
|
+ .eq(SysWxOpuser::getOpenid,openid);
|
|
|
+ SysWxOpuser one = sysWxOpuserService.getOne(queryWrapper);
|
|
|
+ if(one != null){
|
|
|
+ request.getSession().setAttribute("phone",one.getPhone());
|
|
|
+ }
|
|
|
+
|
|
|
+ response.sendRedirect("https://manager.usky.cn/mobile/#/pages/login?flag=true");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{ //app
|
|
|
+ request.getSession().setAttribute("type","app");
|
|
|
+ request.getSession().setAttribute("app_token","");
|
|
|
+ try{
|
|
|
+ response.sendRedirect("");
|
|
|
+ }catch (IOException e){
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 跳转首页权限
|
|
|
+ */
|
|
|
+ @GetMapping("/getPageAuthorization")
|
|
|
+ public void getPageAuthorization(){
|
|
|
+ String http = "https://";
|
|
|
+ String code = request.getParameter("code");
|
|
|
+ String userAgent = request.getHeader("User-Agent");
|
|
|
+ if(null != request.getSession().getAttribute("app_token") && (null != request.getSession().getAttribute("phone"))){
|
|
|
+ LambdaQueryWrapper<SysWxOpuser> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(SysWxOpuser::getPhone,request.getSession().getAttribute("phone"))
|
|
|
+ .eq(SysWxOpuser::getStatus,1)
|
|
|
+ .eq(SysWxOpuser::getType,"app");
|
|
|
+ SysWxOpuser one = sysWxOpuserService.getOne(queryWrapper);
|
|
|
+ if(one != null){
|
|
|
+ String sendUrl = "https://manager.usky.cn/mobile/#/pages/index?op="+one.getOpenid();
|
|
|
+ try{
|
|
|
+ System.out.println("sendUrl: "+sendUrl);
|
|
|
+ response.sendRedirect(sendUrl);
|
|
|
+ }catch (IOException e){
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{
|
|
|
+ if(!userAgent.contains("MicroMessenger")){
|
|
|
+ try{
|
|
|
+ response.sendRedirect("https://manager.usky.cn/mobile/#/pages/login");
|
|
|
+ }catch (IOException e){
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{
|
|
|
+ if((null == request.getSession().getAttribute("openid")) && (StringUtils.isBlank(request.getSession().getAttribute("openid").toString()))){
|
|
|
+ if(StringUtils.isBlank(code)){
|
|
|
+ String url = URLDecoder.decode(http+constant.call_back_domain+request.getRequestURI());
|
|
|
+ String sendUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+constant.WE_CHAT_APP_ID+"&redirect_uri="+url+"&response_type=code&scope=snsapi_base&state=abc123#wechat_redirect";
|
|
|
+ try{
|
|
|
+ System.out.println("sendUrl: "+sendUrl);
|
|
|
+ response.sendRedirect(sendUrl);
|
|
|
+ }catch (IOException e){
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ System.out.println("code: "+code);
|
|
|
+ try {
|
|
|
+ WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
|
|
|
+
|
|
|
+ String openid = wxOAuth2AccessToken.getOpenId();
|
|
|
+ String access_token = wxOAuth2AccessToken.getAccessToken();
|
|
|
+ System.out.println("openid: "+openid);
|
|
|
+ request.getSession().setAttribute("openid",openid);
|
|
|
+ LambdaQueryWrapper<SysWxOpuser> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(SysWxOpuser::getType,"wx")
|
|
|
+ .eq(SysWxOpuser::getStatus,1)
|
|
|
+ .eq(SysWxOpuser::getOpenid,openid);
|
|
|
+ SysWxOpuser one = sysWxOpuserService.getOne(queryWrapper);
|
|
|
+ if(one != null){
|
|
|
+ request.getSession().setAttribute("phone",one.getPhone());
|
|
|
+
|
|
|
+ String sendUrl = "https://manager.usky.cn/mobile/#/pages/index?op="+one.getOpenid();
|
|
|
+ response.sendRedirect(sendUrl);
|
|
|
+ }else{
|
|
|
+ request.getSession().removeAttribute("openid");
|
|
|
+ request.getSession().removeAttribute("phone");
|
|
|
+
|
|
|
+ response.sendRedirect("https://manager.usky.cn/mobile/#/pages/login");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ try{
|
|
|
+ LambdaQueryWrapper<SysWxOpuser> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(SysWxOpuser::getType,"wx")
|
|
|
+ .eq(SysWxOpuser::getStatus,1)
|
|
|
+ .eq(SysWxOpuser::getOpenid,request.getSession().getAttribute("openid"));
|
|
|
+ SysWxOpuser one = sysWxOpuserService.getOne(queryWrapper);
|
|
|
+ if(one != null){
|
|
|
+ request.getSession().setAttribute("phone",one.getPhone());
|
|
|
+ String sendUrl = "https://manager.usky.cn/mobile/#/pages/index?op="+one.getOpenid();
|
|
|
+ response.sendRedirect(sendUrl);
|
|
|
+ }else{
|
|
|
+ request.getSession().removeAttribute("openid");
|
|
|
+ request.getSession().removeAttribute("phone");
|
|
|
+
|
|
|
+ response.sendRedirect("https://manager.usky.cn/mobile/#/pages/login");
|
|
|
+ }
|
|
|
+ }catch (IOException e){
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录并注册
|
|
|
+ */
|
|
|
+ @PostMapping("/signUp")
|
|
|
+ public Map<String,Object> signUp(@RequestBody SignUpRequestVO requestVO){
|
|
|
+ String phone = requestVO.getPhone();
|
|
|
+ String verify = requestVO.getVerify();
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+
|
|
|
+ //手机验证码登录
|
|
|
+ if (StringUtils.isBlank(phone)) {
|
|
|
+ throw new BusinessException("手机号不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(verify)) {
|
|
|
+ throw new BusinessException("验证码不能为空");
|
|
|
+ }
|
|
|
+ if (!verify.equals(redisHelper.get(phone))) {
|
|
|
+ throw new BusinessException("验证码错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ LambdaQueryWrapper<SysWxOpuser> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(SysWxOpuser::getPhone,phone)
|
|
|
+ .eq(SysWxOpuser::getType,request.getSession().getAttribute("type"));
|
|
|
+ List<SysWxOpuser> list = sysWxOpuserService.list(queryWrapper);
|
|
|
+ if(CollectionUtils.isEmpty(list)){
|
|
|
+ SysWxOpuser one = new SysWxOpuser();
|
|
|
+ one.setPhone(phone);
|
|
|
+ if(request.getSession().getAttribute("type") == "app"){
|
|
|
+ one.setOpenid(request.getSession().getAttribute("app_token").toString());
|
|
|
+ }else{
|
|
|
+ one.setOpenid(request.getSession().getAttribute("openid").toString());
|
|
|
+ }
|
|
|
+ one.setType(request.getSession().getAttribute("type").toString());
|
|
|
+ one.setCreatedTime(LocalDateTime.now());
|
|
|
+ one.setStatus(1);
|
|
|
+
|
|
|
+ if(sysWxOpuserService.save(one)){
|
|
|
+
|
|
|
+ }else{
|
|
|
+ map.put("msg","注册失败,请重新注册");
|
|
|
+ map.put("flag",true);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ request.getSession().setAttribute("openid",list.get(0).getOpenid());
|
|
|
+ }
|
|
|
+
|
|
|
+ request.getSession().setAttribute("phone",phone);
|
|
|
+
|
|
|
+ map.put("msg","验证成功,正在登录");
|
|
|
+ map.put("flag",true);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Description:[获取微信公众号的Access_Token]
|
|
|
+ *
|
|
|
+ * @return JSONResult
|
|
|
+ * @date 2019-05-19
|
|
|
+ * @author huazai
|
|
|
+ */
|
|
|
+ @GetMapping("/getWeChatAccessToken")
|
|
|
+ public String getWeChatAccessToken() {
|
|
|
+ try {
|
|
|
+ // 微信公众号官方获取AccessToken
|
|
|
+ String accessToken = wxMpService.getAccessToken();
|
|
|
+
|
|
|
+ return accessToken;
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 微信消息提醒
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// @PostMapping("sendWeChatMessage")
|
|
|
+// public Map<String,Object> sendWeChatMessage(@RequestBody SendWeChatMessageRequestVO requestVO){
|
|
|
+// String infoType = requestVO.getInfoType();
|
|
|
+// String infoTitle = requestVO.getInfoTitle();
|
|
|
+// String infoContent = requestVO.getInfoContent();
|
|
|
+// Map<String,Object> map = new HashMap<>();
|
|
|
+//
|
|
|
+// String s = null;
|
|
|
+// try {
|
|
|
+// s = HttpUtils.postForm(URL, body, null);
|
|
|
+// }catch (Exception e){
|
|
|
+// log.info("发送微信推送报警出错"+e);
|
|
|
+// }
|
|
|
+// return map;
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信消息提醒
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping("sendWeChatMessage")
|
|
|
+ public JSONObject sendWeChatMessage(@RequestBody SendWeChatMessageRequestVO requestVO){
|
|
|
+
|
|
|
+ String infoType = requestVO.getInfoType();
|
|
|
+ String infoTitle = requestVO.getInfoTitle();
|
|
|
+ String infoContent = requestVO.getInfoContent();
|
|
|
+ Integer infoId = requestVO.getInfoId();
|
|
|
+ String openId = requestVO.getOpenId();
|
|
|
+ String token = this.getWeChatAccessToken();
|
|
|
+
|
|
|
+ TemplateMsgEntityVO messageVo=new TemplateMsgEntityVO();
|
|
|
+ messageVo.setTTitle(infoTitle);
|
|
|
+ messageVo.setTKeyword1("测试1");
|
|
|
+ messageVo.setTKeyword2("测试2");
|
|
|
+ messageVo.setTKeyword3("测试3");
|
|
|
+ messageVo.setTKeyword4("测试4");
|
|
|
+ messageVo.setTRemark(infoContent);
|
|
|
+ messageVo.setTUrl(String.format(constant.WE_CHAT_CUSTOMER_CALL_URL,infoId));
|
|
|
+ messageVo.setTemplateId(constant.WE_CHAT_TEMPLATE_ID);
|
|
|
+
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
|
|
|
+ Map<String,Object> content=new HashMap<>();
|
|
|
+ JSONObject data = new JSONObject();
|
|
|
+ data.put("first",new JSONObject().put("value",messageVo.getTTitle()));
|
|
|
+ data.put("keyword1",new JSONObject().put("value",messageVo.getTKeyword1()));
|
|
|
+ data.put("keyword2",new JSONObject().put("value",messageVo.getTKeyword2()));
|
|
|
+ data.put("keyword3",new JSONObject().put("value",messageVo.getTKeyword3()));
|
|
|
+ data.put("keyword4",new JSONObject().put("value",messageVo.getTKeyword4()));
|
|
|
+ data.put("remark",new JSONObject().put("value",messageVo.getTRemark()));
|
|
|
+
|
|
|
+ content.put("touser",openId);
|
|
|
+ content.put("url",messageVo.getTUrl());
|
|
|
+ content.put("template_id",messageVo.getTemplateId());
|
|
|
+ content.put("data",data);
|
|
|
+ String resp = HttpUtil.post(requestUrl, JSONUtil.parseObj(content).toString());
|
|
|
+ System.out.println(content.toString());
|
|
|
+ System.out.println(JSONUtil.parseObj(content));
|
|
|
+ JSONObject result = JSONObject.parseObject(resp);
|
|
|
+ System.out.println("发送消息:" + resp);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|