WechatScanLoginController.java 8.2 KB

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