WechatScanLoginController.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if (access != null) {
  89. String openid = access.getOpenid();
  90. SmartUser user = wechatScanLoginService.selectByOpenid(openid);
  91. if (user==null) { /*不存在*/
  92. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("请绑定信息", "UTF-8");
  93. } else {
  94. String token = JWTUtil.getToken(user);
  95. return "redirect:"+HttpParame.FRONT_URI+"/#/wanzai/home?token=" + token;
  96. }
  97. }
  98. }
  99. return null;
  100. }
  101. /**
  102. * Title: bindingUserMac2
  103. * Description: 跳转到绑定用户系统帐号页面
  104. * @return
  105. */
  106. @Override
  107. @PassToken
  108. public String bindUserMac(String cardNo) throws UnsupportedEncodingException {
  109. if (StringUtils.isBlank(cardNo)){
  110. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("请绑定信息", "UTF-8");
  111. }
  112. Map<String, String> wechatLoginUrl = wechatScanLoginService.wechatBindUrl(cardNo);
  113. String url = wechatLoginUrl.get("url");
  114. System.out.println(url);
  115. return "redirect:" + url;
  116. }
  117. @Override
  118. @PassToken
  119. public String bindcallback(String code, String state, String cardNo) throws UnsupportedEncodingException {
  120. System.out.println(code+"==="+state+"=="+cardNo);
  121. if (code != null && state != null) {
  122. // 验证state为了用于防止跨站请求伪造攻击
  123. String decrypt = AesUtil.decrypt(AesUtil.parseHexStr2Byte(state), AesUtil.PASSWORD_SECRET_KEY, 16);
  124. if (!decrypt.equals(Constanst.PWD_MD5 + DateUtils.getYYYYMMdd())) {
  125. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("请勿非法进入", "UTF-8");
  126. }
  127. AccessToken access = wechatScanLoginService.getAccessToken(code);
  128. if (access != null) {
  129. String openid = access.getOpenid();
  130. /*SmartUser user = wechatScanLoginService.selectByOpenid(openid);
  131. if (user!=null) { *//*不存在*//*
  132. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("该卡号已绑定其他用户", "UTF-8");
  133. }*/
  134. SmartUser userc = wechatScanLoginService.selectByCardNo(cardNo);
  135. if (userc==null){
  136. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("卡号信息错误", "UTF-8");
  137. }
  138. if (userc.getOpenId()!=null){
  139. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("该卡号已绑定其他用户", "UTF-8");
  140. }
  141. userc.setOpenId(openid);
  142. int m = wechatScanLoginService.updateSmartUser(userc);
  143. if (m>0){
  144. String token = JWTUtil.getToken(userc);
  145. return "redirect:"+HttpParame.FRONT_URI+"/#/wanzai/home?token=" + token;
  146. }
  147. }
  148. }
  149. return "redirect:"+HttpParame.FRONT_URI+"/#/login?message=" + URLEncoder.encode("系统异常", "UTF-8");
  150. }
  151. @Override
  152. @PassToken
  153. public String insertMonthMeter(String month){
  154. if (month==null){
  155. month=TimeExchange2.getMonth();
  156. }
  157. wechatScanLoginService.insertMonthMeter(month);
  158. return "插入完毕";
  159. }
  160. @Override
  161. @PassToken
  162. public String insertDayMeter(String day){
  163. if (day==null){
  164. day=TimeExchange2.getDateStr();
  165. }
  166. wechatScanLoginService.insertDayMeter(day);
  167. return "插入完毕";
  168. }
  169. /**
  170. * Description: 用户类别统计
  171. */
  172. @Override
  173. @PassToken
  174. public CommonResult getUserIdTj(){
  175. List<Tj> list = wechatScanLoginService.getUserIdTj();
  176. return CommonResult.ok(list);
  177. }
  178. /**
  179. * Description: 用户总数
  180. */
  181. @Override
  182. @PassToken
  183. public CommonResult getUserIdTjt(){
  184. All list = wechatScanLoginService.getUserIdTjt();
  185. return CommonResult.ok(list);
  186. }
  187. /**
  188. * Description: 水表总计
  189. */
  190. @Override
  191. @PassToken
  192. public CommonResult getWaterTj(){
  193. DAll list = wechatScanLoginService.getWaterTj();
  194. return CommonResult.ok(list);
  195. }
  196. /**
  197. * Description: 电表总计
  198. */
  199. @Override
  200. @PassToken
  201. public CommonResult getElcTj(){
  202. DAll list = wechatScanLoginService.getElcTj();
  203. return CommonResult.ok(list);
  204. }
  205. /**
  206. * Description: 每月水费查询
  207. */
  208. @Override
  209. @PassToken
  210. public CommonResult getMonWater() throws ParseException {
  211. List<MonthMeterDetail> list = wechatScanLoginService.getMonWater();
  212. return CommonResult.ok(list);
  213. }
  214. /**
  215. * Description: 每月电费查询
  216. */
  217. @Override
  218. @PassToken
  219. public CommonResult getMonElc() throws ParseException {
  220. List<MonthMeterDetail> list = wechatScanLoginService.getMonElc();
  221. return CommonResult.ok(list);
  222. }
  223. /**
  224. * Description: 区域能耗统计
  225. */
  226. @Override
  227. @PassToken
  228. public CommonResult getMeterMonthPage(@RequestBody MeterMonthData meterMonthData,int currentPage, int pageCount) {
  229. PageHelper.startPage(currentPage, pageCount);
  230. PageInfo<MeterMonthData> list = wechatScanLoginService.getMeterMonthPage(meterMonthData);
  231. return CommonResult.ok(list);
  232. }
  233. /**
  234. * Description: 实时抄表
  235. */
  236. @Override
  237. @PassToken
  238. public CommonResult getMeterDayPage(@RequestBody MeterMonthData meterMonthData, int currentPage, int pageCount) {
  239. PageHelper.startPage(currentPage, pageCount);
  240. PageInfo<MeterMonthData> list = wechatScanLoginService.getMeterDayPage(meterMonthData);
  241. return CommonResult.ok(list);
  242. }
  243. /**
  244. * Description: 查轨迹
  245. */
  246. @Override
  247. @PassToken
  248. public CommonResult getPosition() {
  249. List<List<BigDecimal>> ld = RandomTrackAlgorithm.getPosition();
  250. return CommonResult.ok(ld);
  251. }
  252. @Override
  253. @PassToken
  254. public CommonResult vertify(String code){
  255. if (StringUtils.isBlank(code)){
  256. return CommonResult.fail("请传入code");
  257. }
  258. String url = "https://api.weixin.qq.com/sns/jscode2session?" +
  259. "appid=" + PropertiesUtil.getValue(HttpParame.APPID) +
  260. "&secret=" + PropertiesUtil.getValue(HttpParame.SECRET) +
  261. "&js_code=" + code +
  262. "&grant_type=authorization_code";
  263. String json = HttpClientUtils.getInstance().sendHttpGet(url);
  264. Gson gson = new Gson();
  265. HashMap<String, String> userMap = gson.fromJson(json.toString(), new TypeToken<HashMap<String, String>>() {}.getType());
  266. String openid = "";
  267. try {
  268. openid = userMap.get("openid").toString();
  269. } catch (Exception e) {
  270. return CommonResult.fail("code异常");
  271. }
  272. SmartUser user = wechatScanLoginService.selectByOpenid(openid);
  273. if (user==null){
  274. return CommonResult.fail("请绑定后再进入");
  275. }
  276. String token = JWTUtil.getToken(user);
  277. JSONObject jsonObject = new JSONObject();
  278. jsonObject.put("token", token);
  279. return CommonResult.ok(jsonObject.toString());
  280. }
  281. }