Browse Source

上传文件

陈士柏 2 years ago
parent
commit
f4a52c89a0
1 changed files with 258 additions and 0 deletions
  1. 258 0
      src/main/java/com/template/controller/WechatScanLoginController.java

+ 258 - 0
src/main/java/com/template/controller/WechatScanLoginController.java

@@ -0,0 +1,258 @@
+package com.template.controller;
+
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+import com.github.pagehelper.util.StringUtil;
+import com.template.annotation.PassToken;
+import com.template.api.WechatScanLoginControllerAPI;
+import com.template.common.constanst.Constanst;
+import com.template.common.result.ResponseStatusEnum;
+import com.template.common.utils.*;
+import com.template.model.pojo.SmartUser;
+import com.template.model.result.CommonResult;
+import com.template.model.result.PageUtils;
+import com.template.model.tongji.*;
+import com.template.model.vo.LoginVO;
+import com.template.model.weixin.AccessToken;
+import com.template.model.weixin.HttpParame;
+import com.template.model.weixin.WechatUserUnionID;
+import com.template.services.SmartUserService;
+import com.template.services.WechatScanLoginService;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.UnsupportedEncodingException;
+import java.math.BigDecimal;
+import java.net.URLEncoder;
+import java.text.ParseException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Title: WechatScanLoginController
+ * Description: 微信扫码登录controller
+ * 
+ * @author fengyong
+ * @date 2018年9月7日
+ */
+@Controller
+public class WechatScanLoginController implements WechatScanLoginControllerAPI {
+
+	@Autowired
+	private WechatScanLoginService wechatScanLoginService;
+	@Autowired
+	public SmartUserService smartUserService;
+
+	/**
+	 * Title: list
+	 * Description: 重定向到微信扫码登录二维码页面
+	 * 此处显示要微信要扫描的二维码
+	 * 
+	 * @param model
+	 * @return
+	 * @throws UnsupportedEncodingException
+	 */
+	@Override
+	public String login(Model model) throws UnsupportedEncodingException {
+		Map<String, String> wechatLoginUrl = wechatScanLoginService.wechatLoginUrl();
+		String url = wechatLoginUrl.get("url");
+		System.out.println(url);
+		return "redirect:" + url;
+	}
+
+	/**
+	 * Title: callback
+	 * Description: 回调地址处理
+	 * 
+	 * @param code
+	 * @param state
+	 * @return
+	 * @return
+	 */
+	@Override
+	public String callback(String code, String state) throws UnsupportedEncodingException {
+		System.out.println(code+"====="+state);
+		if (code != null && state != null) {
+			// 验证state为了用于防止跨站请求伪造攻击
+			String decrypt = AesUtil.decrypt(AesUtil.parseHexStr2Byte(state), AesUtil.PASSWORD_SECRET_KEY, 16);
+			if (!decrypt.equals(Constanst.PWD_MD5 + DateUtils.getYYYYMMdd())) {
+				return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("请勿非法进入", "UTF-8");
+			}
+			AccessToken access = wechatScanLoginService.getAccessToken(code);
+			if (access != null) {
+				String openid = access.getOpenid();
+				SmartUser user = wechatScanLoginService.selectByOpenid(openid);
+				if (user==null) {		/*不存在*/
+					return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("请绑定信息", "UTF-8");
+				} else {
+					String token = JWTUtil.getToken(user);
+					return "redirect:"+HttpParame.FRONT_URI+"/#/wanzai/home?token=" + token;
+				}
+			}
+		}
+		return null;
+	}
+
+	/**
+	 * Title: bindingUserMac
+	 * Description: 跳转到绑定用户系统帐号页面
+	 * @return
+	 */
+	@Override
+	public String bindUserMac(String cardNo) throws UnsupportedEncodingException {
+		if (StringUtils.isBlank(cardNo)){
+			return "redirect:"+HttpParame.FRONT_URI+"pc/#/pages/404/404/?message=" + URLEncoder.encode("请输入卡号", "UTF-8");
+		}
+		Map<String, String> wechatLoginUrl = wechatScanLoginService.wechatBindUrl(cardNo);
+		String url = wechatLoginUrl.get("url");
+		System.out.println(url);
+		return "redirect:" + url;
+	}
+
+	@Override
+	public String bindcallback(String code, String state, String cardNo) throws UnsupportedEncodingException {
+		System.out.println(code+"==="+state+"=="+cardNo);
+		if (code != null && state != null) {
+			// 验证state为了用于防止跨站请求伪造攻击
+			String decrypt = AesUtil.decrypt(AesUtil.parseHexStr2Byte(state), AesUtil.PASSWORD_SECRET_KEY, 16);
+			if (!decrypt.equals(Constanst.PWD_MD5 + DateUtils.getYYYYMMdd())) {
+				return "redirect:"+HttpParame.FRONT_URI+"pc/#/pages/404/404/?message=" + URLEncoder.encode("请勿非法进入", "UTF-8");
+			}
+			AccessToken access = wechatScanLoginService.getAccessToken(code);
+			if (access != null) {
+				String openid = access.getOpenid();
+				SmartUser user = wechatScanLoginService.selectByOpenid(openid);
+				// 我只是给个例子
+				if (user!=null) {		/*不存在*/
+					return "redirect:"+HttpParame.FRONT_URI+"pc/#/pages/404/404/?message=" + URLEncoder.encode("请勿重复绑定", "UTF-8");
+				}
+				SmartUser userc = wechatScanLoginService.selectByCardNo(cardNo);
+				if (userc==null){
+					return "redirect:"+HttpParame.FRONT_URI+"pc/#/pages/404/404/?message=" + URLEncoder.encode("未查询到用户信息", "UTF-8");
+				}
+				if (userc.getOpenId()!=null){
+					return "redirect:"+HttpParame.FRONT_URI+"pc/#/pages/404/404/?message=" + URLEncoder.encode("请勿重复绑定", "UTF-8");
+				}
+				userc.setOpenId(openid);
+				int m = wechatScanLoginService.updateSmartUser(userc);
+				if (m>0){
+					String token = JWTUtil.getToken(userc);
+					return "redirect:"+HttpParame.FRONT_URI+"pc/#/pages/index/?token=" + token;
+				}
+			}
+		}
+		return "redirect:"+HttpParame.FRONT_URI+"pc/#/pages/404/404/?message=" + URLEncoder.encode("绑定失败", "UTF-8");
+	}
+
+	@Override
+	@PassToken
+    public String insertMonthMeter(String month){
+		wechatScanLoginService.insertMonthMeter(month);
+		return "插入完毕";
+	}
+
+	@Override
+	@PassToken
+	public String insertDayMeter(String day){
+		wechatScanLoginService.insertDayMeter(day);
+		return "插入完毕";
+	}
+
+	/**
+	 * Description: 用户类别统计
+	 */
+	@Override
+	@PassToken
+	public CommonResult getUserIdTj(){
+		List<Tj> list = wechatScanLoginService.getUserIdTj();
+		return CommonResult.ok(list);
+	}
+
+	/**
+	 * Description: 用户总数
+	 */
+	@Override
+	@PassToken
+	public CommonResult getUserIdTjt(){
+		All list = wechatScanLoginService.getUserIdTjt();
+		return CommonResult.ok(list);
+	}
+
+	/**
+	 * Description: 水表总计
+	 */
+	@Override
+	@PassToken
+	public CommonResult getWaterTj(){
+		DAll list = wechatScanLoginService.getWaterTj();
+		return CommonResult.ok(list);
+	}
+
+	/**
+	 * Description: 电表总计
+	 */
+	@Override
+	@PassToken
+	public CommonResult getElcTj(){
+		DAll list = wechatScanLoginService.getElcTj();
+		return CommonResult.ok(list);
+	}
+
+	/**
+	 * Description: 每月水费查询
+	 */
+	@Override
+	@PassToken
+	public CommonResult getMonWater() throws ParseException {
+		List<MonthMeterDetail> list = wechatScanLoginService.getMonWater();
+		return CommonResult.ok(list);
+	}
+
+	/**
+	 * Description: 每月电费查询
+	 */
+	@Override
+	@PassToken
+	public CommonResult getMonElc() throws ParseException {
+		List<MonthMeterDetail> list = wechatScanLoginService.getMonElc();
+		return CommonResult.ok(list);
+	}
+
+	/**
+	 * Description: 实时抄表
+	 */
+	@Override
+	@PassToken
+	public CommonResult getMeterMonthPage(@RequestBody MeterMonthData meterMonthData,int currentPage, int pageCount) {
+		PageHelper.startPage(currentPage, pageCount);
+		PageInfo<MeterMonthData> list = wechatScanLoginService.getMeterMonthPage(meterMonthData);
+		return CommonResult.ok(list);
+	}
+
+	/**
+	 * Description: 实时抄表
+	 */
+	@Override
+	@PassToken
+	public CommonResult getMeterDayPage(@RequestBody MeterMonthData meterMonthData, int currentPage, int pageCount) {
+		PageHelper.startPage(currentPage, pageCount);
+		PageInfo<MeterMonthData> list = wechatScanLoginService.getMeterDayPage(meterMonthData);
+		return CommonResult.ok(list);
+	}
+
+	/**
+	 * Description: 查轨迹
+	 */
+	@Override
+	@PassToken
+	public CommonResult getPosition() {
+		List<List<BigDecimal>> ld = RandomTrackAlgorithm.getPosition();
+		return CommonResult.ok(ld);
+	}
+}