|
@@ -0,0 +1,301 @@
|
|
|
+package com.usky.iot.controller.web;
|
|
|
+
|
|
|
+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.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.SignUpRequestVO;
|
|
|
+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 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")
|
|
|
+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+request.getServerName()+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://qhome.usky.cn/work/index.html#/pages/login/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://iot.usky.cn/work/#/?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://iot.usky.cn/work/index.html#/pages/login/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+request.getServerName()+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://iot.usky.cn/work/#/?op="+one.getOpenid();
|
|
|
+ response.sendRedirect(sendUrl);
|
|
|
+ }else{
|
|
|
+ request.getSession().removeAttribute("openid");
|
|
|
+ request.getSession().removeAttribute("phone");
|
|
|
+
|
|
|
+ response.sendRedirect("https://iot.usky.cn/work/index.html#/pages/login/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://iot.usky.cn/work/#/?op="+one.getOpenid();
|
|
|
+ response.sendRedirect(sendUrl);
|
|
|
+ }else{
|
|
|
+ request.getSession().removeAttribute("openid");
|
|
|
+ request.getSession().removeAttribute("phone");
|
|
|
+
|
|
|
+ response.sendRedirect("https://iot.usky.cn/work/index.html#/pages/login/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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|