| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- package com.template.controller;
- import com.alibaba.fastjson.JSONObject;
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import com.github.pagehelper.util.StringUtil;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- 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.HashMap;
- 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
- @PassToken
- 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
- @PassToken
- 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);
- System.out.println("access:"+access);
- if (access != null) {
- String openid = access.getOpenid();
- System.out.println("openid: "+openid);
- 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: bindingUserMac2
- * Description: 跳转到绑定用户系统帐号页面
- * @return
- */
- @Override
- @PassToken
- public String bindUserMac(String cardNo) throws UnsupportedEncodingException {
- if (StringUtils.isBlank(cardNo)){
- return "redirect:"+HttpParame.FRONT_URI+"/#/login?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
- @PassToken
- 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+"/#/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");
- }*/
- SmartUser userc = wechatScanLoginService.selectByCardNo(cardNo);
- if (userc==null){
- return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("卡号信息错误", "UTF-8");
- }
- if (userc.getOpenId()!=null){
- return "redirect:"+HttpParame.FRONT_URI+"/#/login?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+"/#/wanzai/home?token=" + token;
- }
- }
- }
- return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("系统异常", "UTF-8");
- }
- @Override
- @PassToken
- public String insertMonthMeter(String month){
- if (month==null){
- month=TimeExchange2.getMonth();
- }
- wechatScanLoginService.insertMonthMeter(month);
- return "插入完毕";
- }
- @Override
- @PassToken
- public String insertDayMeter(String day){
- if (day==null){
- day=TimeExchange2.getDateStr();
- }
- 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);
- }
- @Override
- @PassToken
- public CommonResult vertify(String code){
- if (StringUtils.isBlank(code)){
- return CommonResult.fail("请传入code");
- }
- String url = "https://api.weixin.qq.com/sns/jscode2session?" +
- "appid=" + PropertiesUtil.getValue(HttpParame.APPID) +
- "&secret=" + PropertiesUtil.getValue(HttpParame.SECRET) +
- "&js_code=" + code +
- "&grant_type=authorization_code";
- String json = HttpClientUtils.getInstance().sendHttpGet(url);
- Gson gson = new Gson();
- HashMap<String, String> userMap = gson.fromJson(json.toString(), new TypeToken<HashMap<String, String>>() {}.getType());
- String openid = "";
- try {
- openid = userMap.get("openid").toString();
- } catch (Exception e) {
- return CommonResult.fail("code异常");
- }
- SmartUser user = wechatScanLoginService.selectByOpenid(openid);
- if (user==null){
- return CommonResult.fail("请绑定后再进入");
- }
- String token = JWTUtil.getToken(user);
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("token", token);
- return CommonResult.ok(jsonObject.toString());
- }
- }
|