WechatScanLoginController.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package com.template.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.github.pagehelper.PageHelper;
  4. import com.github.pagehelper.PageInfo;
  5. import com.github.pagehelper.util.StringUtil;
  6. import com.google.gson.Gson;
  7. import com.google.gson.reflect.TypeToken;
  8. import com.template.annotation.PassToken;
  9. import com.template.api.WechatScanLoginControllerAPI;
  10. import com.template.common.constanst.Constanst;
  11. import com.template.common.result.ResponseStatusEnum;
  12. import com.template.common.utils.*;
  13. import com.template.model.pojo.SmartUser;
  14. import com.template.model.result.CommonResult;
  15. import com.template.model.result.PageUtils;
  16. import com.template.model.tongji.*;
  17. import com.template.model.vo.LoginVO;
  18. import com.template.model.weixin.AccessToken;
  19. import com.template.model.weixin.HttpParame;
  20. import com.template.model.weixin.WechatUserUnionID;
  21. import com.template.services.SmartUserService;
  22. import com.template.services.WechatScanLoginService;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.stereotype.Controller;
  26. import org.springframework.ui.Model;
  27. import org.springframework.web.bind.annotation.GetMapping;
  28. import org.springframework.web.bind.annotation.RequestBody;
  29. import org.springframework.web.bind.annotation.RequestMapping;
  30. import org.springframework.web.bind.annotation.RestController;
  31. import java.io.UnsupportedEncodingException;
  32. import java.math.BigDecimal;
  33. import java.net.URLEncoder;
  34. import java.text.ParseException;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. /**
  39. * Title: WechatScanLoginController
  40. * Description: 微信扫码登录controller
  41. *
  42. * @author fengyong
  43. * @date 2018年9月7日
  44. */
  45. @Controller
  46. public class WechatScanLoginController implements WechatScanLoginControllerAPI {
  47. @Autowired
  48. private WechatScanLoginService wechatScanLoginService;
  49. @Autowired
  50. public SmartUserService smartUserService;
  51. /**
  52. * Title: list
  53. * Description: 重定向到微信扫码登录二维码页面
  54. * 此处显示要微信要扫描的二维码
  55. *
  56. * @param model
  57. * @return
  58. * @throws UnsupportedEncodingException
  59. */
  60. @Override
  61. @PassToken
  62. public String login(Model model) throws UnsupportedEncodingException {
  63. Map<String, String> wechatLoginUrl = wechatScanLoginService.wechatLoginUrl();
  64. String url = wechatLoginUrl.get("url");
  65. System.out.println(url);
  66. return "redirect:" + url;
  67. }
  68. /**
  69. * Title: callback
  70. * Description: 回调地址处理
  71. *
  72. * @param code
  73. * @param state
  74. * @return
  75. * @return
  76. */
  77. @Override
  78. @PassToken
  79. public String callback(String code, String state) throws UnsupportedEncodingException {
  80. System.out.println(code+"====="+state);
  81. if (code != null && state != null) {
  82. // 验证state为了用于防止跨站请求伪造攻击
  83. String decrypt = AesUtil.decrypt(AesUtil.parseHexStr2Byte(state), AesUtil.PASSWORD_SECRET_KEY, 16);
  84. if (!decrypt.equals(Constanst.PWD_MD5 + DateUtils.getYYYYMMdd())) {
  85. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("请勿非法进入", "UTF-8");
  86. }
  87. AccessToken access = wechatScanLoginService.getAccessToken(code);
  88. System.out.println("access:"+access);
  89. if (access != null) {
  90. String openid = access.getOpenid();
  91. System.out.println("openid: "+openid);
  92. SmartUser user = wechatScanLoginService.selectByOpenid(openid);
  93. if (user==null) { /*不存在*/
  94. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("请绑定信息", "UTF-8");
  95. } else {
  96. String token = JWTUtil.getToken(user);
  97. return "redirect:"+HttpParame.FRONT_URI+"/#/wanzai/home?token=" + token;
  98. }
  99. }
  100. }
  101. return null;
  102. }
  103. /**
  104. * Title: bindingUserMac2
  105. * Description: 跳转到绑定用户系统帐号页面
  106. * @return
  107. */
  108. @Override
  109. @PassToken
  110. public String bindUserMac(String cardNo) throws UnsupportedEncodingException {
  111. if (StringUtils.isBlank(cardNo)){
  112. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("请绑定信息", "UTF-8");
  113. }
  114. Map<String, String> wechatLoginUrl = wechatScanLoginService.wechatBindUrl(cardNo);
  115. String url = wechatLoginUrl.get("url");
  116. System.out.println(url);
  117. return "redirect:" + url;
  118. }
  119. @Override
  120. @PassToken
  121. public String bindcallback(String code, String state, String cardNo) throws UnsupportedEncodingException {
  122. System.out.println(code+"==="+state+"=="+cardNo);
  123. if (code != null && state != null) {
  124. // 验证state为了用于防止跨站请求伪造攻击
  125. String decrypt = AesUtil.decrypt(AesUtil.parseHexStr2Byte(state), AesUtil.PASSWORD_SECRET_KEY, 16);
  126. if (!decrypt.equals(Constanst.PWD_MD5 + DateUtils.getYYYYMMdd())) {
  127. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("请勿非法进入", "UTF-8");
  128. }
  129. AccessToken access = wechatScanLoginService.getAccessToken(code);
  130. if (access != null) {
  131. String openid = access.getOpenid();
  132. /*SmartUser user = wechatScanLoginService.selectByOpenid(openid);
  133. if (user!=null) { *//*不存在*//*
  134. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("该卡号已绑定其他用户", "UTF-8");
  135. }*/
  136. SmartUser userc = wechatScanLoginService.selectByCardNo(cardNo);
  137. if (userc==null){
  138. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("卡号信息错误", "UTF-8");
  139. }
  140. if (userc.getOpenId()!=null){
  141. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("该卡号已绑定其他用户", "UTF-8");
  142. }
  143. userc.setOpenId(openid);
  144. int m = wechatScanLoginService.updateSmartUser(userc);
  145. if (m>0){
  146. String token = JWTUtil.getToken(userc);
  147. return "redirect:"+HttpParame.FRONT_URI+"/#/wanzai/home?token=" + token;
  148. }
  149. }
  150. }
  151. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("系统异常", "UTF-8");
  152. }
  153. @Override
  154. @PassToken
  155. public String insertMonthMeter(String month){
  156. if (month==null){
  157. month=TimeExchange2.getMonth();
  158. }
  159. wechatScanLoginService.insertMonthMeter(month);
  160. return "插入完毕";
  161. }
  162. @Override
  163. @PassToken
  164. public String insertDayMeter(String day){
  165. if (day==null){
  166. day=TimeExchange2.getDateStr();
  167. }
  168. wechatScanLoginService.insertDayMeter(day);
  169. return "插入完毕";
  170. }
  171. /**
  172. * Description: 用户类别统计
  173. */
  174. @Override
  175. @PassToken
  176. public CommonResult getUserIdTj(){
  177. List<Tj> list = wechatScanLoginService.getUserIdTj();
  178. return CommonResult.ok(list);
  179. }
  180. /**
  181. * Description: 用户总数
  182. */
  183. @Override
  184. @PassToken
  185. public CommonResult getUserIdTjt(){
  186. All list = wechatScanLoginService.getUserIdTjt();
  187. return CommonResult.ok(list);
  188. }
  189. /**
  190. * Description: 水表总计
  191. */
  192. @Override
  193. @PassToken
  194. public CommonResult getWaterTj(){
  195. DAll list = wechatScanLoginService.getWaterTj();
  196. return CommonResult.ok(list);
  197. }
  198. /**
  199. * Description: 电表总计
  200. */
  201. @Override
  202. @PassToken
  203. public CommonResult getElcTj(){
  204. DAll list = wechatScanLoginService.getElcTj();
  205. return CommonResult.ok(list);
  206. }
  207. /**
  208. * Description: 每月水费查询
  209. */
  210. @Override
  211. @PassToken
  212. public CommonResult getMonWater() throws ParseException {
  213. List<MonthMeterDetail> list = wechatScanLoginService.getMonWater();
  214. return CommonResult.ok(list);
  215. }
  216. /**
  217. * Description: 每月电费查询
  218. */
  219. @Override
  220. @PassToken
  221. public CommonResult getMonElc() throws ParseException {
  222. List<MonthMeterDetail> list = wechatScanLoginService.getMonElc();
  223. return CommonResult.ok(list);
  224. }
  225. /**
  226. * Description: 区域能耗统计
  227. */
  228. @Override
  229. @PassToken
  230. public CommonResult getMeterMonthPage(@RequestBody MeterMonthData meterMonthData,int currentPage, int pageCount) {
  231. PageHelper.startPage(currentPage, pageCount);
  232. PageInfo<MeterMonthData> list = wechatScanLoginService.getMeterMonthPage(meterMonthData);
  233. return CommonResult.ok(list);
  234. }
  235. /**
  236. * Description: 实时抄表
  237. */
  238. @Override
  239. @PassToken
  240. public CommonResult getMeterDayPage(@RequestBody MeterMonthData meterMonthData, int currentPage, int pageCount) {
  241. PageHelper.startPage(currentPage, pageCount);
  242. PageInfo<MeterMonthData> list = wechatScanLoginService.getMeterDayPage(meterMonthData);
  243. return CommonResult.ok(list);
  244. }
  245. /**
  246. * Description: 查轨迹
  247. */
  248. @Override
  249. @PassToken
  250. public CommonResult getPosition() {
  251. List<List<BigDecimal>> ld = RandomTrackAlgorithm.getPosition();
  252. return CommonResult.ok(ld);
  253. }
  254. @Override
  255. @PassToken
  256. public CommonResult vertify(String code){
  257. if (StringUtils.isBlank(code)){
  258. return CommonResult.fail("请传入code");
  259. }
  260. String url = "https://api.weixin.qq.com/sns/jscode2session?" +
  261. "appid=" + PropertiesUtil.getValue(HttpParame.APPID) +
  262. "&secret=" + PropertiesUtil.getValue(HttpParame.SECRET) +
  263. "&js_code=" + code +
  264. "&grant_type=authorization_code";
  265. String json = HttpClientUtils.getInstance().sendHttpGet(url);
  266. Gson gson = new Gson();
  267. HashMap<String, String> userMap = gson.fromJson(json.toString(), new TypeToken<HashMap<String, String>>() {}.getType());
  268. String openid = "";
  269. try {
  270. openid = userMap.get("openid").toString();
  271. } catch (Exception e) {
  272. return CommonResult.fail("code异常");
  273. }
  274. SmartUser user = wechatScanLoginService.selectByOpenid(openid);
  275. if (user==null){
  276. return CommonResult.fail("请绑定后再进入");
  277. }
  278. String token = JWTUtil.getToken(user);
  279. JSONObject jsonObject = new JSONObject();
  280. jsonObject.put("token", token);
  281. return CommonResult.ok(jsonObject.toString());
  282. }
  283. }