TWeixinController.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * Date:2019-12-25 11:12:55
  3. * author:吴洪双
  4. */
  5. package com.shawn.web.controller;
  6. import java.io.*;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.net.URLDecoder;
  10. import java.text.ParseException;
  11. import java.text.SimpleDateFormat;
  12. import java.util.*;
  13. import com.alibaba.fastjson.JSONObject;
  14. //import com.paytm.pg.merchant.CheckSumServiceHelper;
  15. import com.shawn.model.entity.*;
  16. import com.shawn.service.interfac.TAdminServiceInterface;
  17. import com.shawn.util.JavaWebToken;
  18. import com.shawn.util.JoinpayConstant;
  19. import com.shawn.util.PushUtils;
  20. import com.shawn.util.WeChatUtil;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.core.annotation.Order;
  24. import org.springframework.http.HttpStatus;
  25. import org.springframework.http.ResponseEntity;
  26. import org.springframework.web.bind.annotation.*;
  27. import com.shawn.model.dto.ResultMessage;
  28. import com.shawn.web.controller.base.BaseController;
  29. import com.shawn.web.exception.MyException;
  30. import com.shawn.model.param.TWeixinParam;
  31. import com.shawn.service.interfac.TWeixinServiceInterface;
  32. import lombok.extern.apachecommons.CommonsLog;
  33. import javax.servlet.http.HttpServletRequest;
  34. @CommonsLog
  35. @RestController
  36. @RequestMapping("TWeixin")
  37. public class TWeixinController extends BaseController<TWeixin,TWeixinExample,TWeixinParam,Integer>{
  38. @Autowired
  39. private TWeixinServiceInterface tWeixinService;
  40. @Autowired
  41. public TWeixinController(TWeixinServiceInterface service) {
  42. super(service);
  43. }
  44. @Autowired
  45. private TAdminServiceInterface tAdminService;
  46. @Override
  47. protected TWeixinExample createNewExample() {
  48. return new TWeixinExample();
  49. }
  50. /**
  51. * 微信登录
  52. * @param
  53. * @return
  54. */
  55. @PostMapping("/weiXinLogin")
  56. public ResponseEntity<?> weiXinLogin(@RequestBody TWeixin weiXin) {
  57. TWeixin tWeixin = tWeixinService.findByEntity(weiXin);
  58. if(tWeixin!=null){
  59. String adminId = tWeixin.getAdminId();
  60. if(adminId.length()>0){
  61. TAdminExample example = new TAdminExample();
  62. TAdminExample.Criteria criteria = example.createCriteria();
  63. criteria.andIdEqualTo(Long.valueOf(adminId));
  64. List<TAdmin> list = tAdminService.selectByOption(example);
  65. if(list.size()>0){
  66. tWeixin.setModifyDate(new Date());
  67. tWeixin.setNickName(weiXin.getNickName());
  68. tWeixin.setAvatarUrl(weiXin.getAvatarUrl());
  69. tWeixinService.updateById(tWeixin);
  70. Map<String,Object> m = new HashMap<String,Object>();
  71. m.put("userId",list.get(0).getId());
  72. // 根据存在用户的id生成token字符串
  73. String token = JavaWebToken.createJavaWebToken(m);
  74. return ResponseEntity.status(HttpStatus.OK)
  75. .body(new ResultMessage().setCode(true).setData(list.get(0)).setMessage("SUCCESS").setToken(token));
  76. }
  77. }
  78. }else {
  79. return ResponseEntity.status(HttpStatus.OK)
  80. .body(new ResultMessage().setCode(true).setData(weiXin).setMessage("off"));
  81. }
  82. return null;
  83. }
  84. /**
  85. * 微信注册
  86. * @param
  87. * @return
  88. */
  89. @PostMapping("/weiXinZhuce")
  90. public ResponseEntity<?> weiXinZhuce(@RequestBody weixinParm weiXin) {
  91. // 检查必输项
  92. if(StringUtils.isEmpty(weiXin.getUsername())||StringUtils.isEmpty(weiXin.getPassword())){
  93. throw new MyException("用户名密码不能为空");
  94. }
  95. TAdminExample example = new TAdminExample();
  96. TAdminExample.Criteria criteria = example.createCriteria();
  97. criteria.andUsernameEqualTo(weiXin.getUsername());
  98. criteria.andPasswordEqualTo(weiXin.getPassword());
  99. List<TAdmin> list = tAdminService.selectByOption(example);
  100. if(list.size()>0){
  101. Long adminId = list.get(0).getId();
  102. TWeixin newWeixin = new TWeixin();
  103. String adminid = String.valueOf(adminId);
  104. newWeixin.setAdminId(adminid);
  105. newWeixin.setNickName(weiXin.getNickName());
  106. newWeixin.setAvatarUrl(weiXin.getAvatarUrl());
  107. newWeixin.setOpenId(weiXin.getOpenId());
  108. newWeixin.setCreateDate(new Date());
  109. newWeixin.setModifyDate(new Date());
  110. Integer integer = tWeixinService.insert(newWeixin);
  111. Map<String,Object> m = new HashMap<String,Object>();
  112. m.put("userId",list.get(0).getId());
  113. // 根据存在用户的id生成token字符串
  114. String token = JavaWebToken.createJavaWebToken(m);
  115. return ResponseEntity.status(HttpStatus.OK)
  116. .body(new ResultMessage().setCode(true).setData(list.get(0)).setMessage("SUCCESS").setToken(token));
  117. }
  118. return ResponseEntity.status(HttpStatus.OK)
  119. .body(new ResultMessage().setCode(false).setData(null).setMessage("用户名或密码错误"));
  120. }
  121. /**
  122. * 微信注册
  123. * @param
  124. * @return
  125. */
  126. @PostMapping("/weiXinSearch")
  127. public ResponseEntity<?> weiXinSearch(@RequestBody TWeixin weiXin) {
  128. TWeixinExample example = new TWeixinExample();
  129. TWeixinExample.Criteria criteria = example.createCriteria();
  130. criteria.andAdminIdEqualTo(weiXin.getAdminId());
  131. List<TWeixin> tWeixins = tWeixinService.selectByOption(example);
  132. if(tWeixins.size()>0){
  133. return ResponseEntity.status(HttpStatus.OK)
  134. .body(new ResultMessage().setCode(true).setData(tWeixins).setMessage("SUCCESS"));
  135. }else {
  136. return ResponseEntity.status(HttpStatus.OK)
  137. .body(new ResultMessage().setCode(true).setData(null).setMessage("没有绑定微信"));
  138. }
  139. }
  140. /**
  141. * 解除微信绑定
  142. * @param
  143. * @return
  144. */
  145. @PostMapping("/deleteWeixin")
  146. public ResponseEntity<?> deleteWeixin(@RequestBody TWeixin weiXin) {
  147. Integer integer = tWeixinService.deleteById(weiXin.getId());
  148. return ResponseEntity.status(HttpStatus.OK)
  149. .body(new ResultMessage().setCode(true).setData(integer).setMessage("SUCCESS"));
  150. }
  151. /**
  152. * 获取微信openid
  153. * @param
  154. * @return
  155. */
  156. @GetMapping("/getOpenid")
  157. public ResponseEntity<?> getOpenid(String code) {
  158. // 微信小程序ID
  159. String appid = "wx3844925af1740a7d";
  160. // 微信小程序秘钥
  161. String secret = "c1f0363a78d1c580388d6c4f14674733";
  162. // 根据小程序穿过来的code想这个url发送请求
  163. String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
  164. // 发送请求,返回Json字符串
  165. String str = WeChatUtil.httpRequest(url, "GET", null);
  166. // 转成Json对象 获取openid
  167. JSONObject jsonObject = JSONObject.parseObject(str);
  168. // 我们需要的openid,在一个小程序中,openid是唯一的
  169. String openid = jsonObject.get("openid").toString();
  170. return ResponseEntity.status(HttpStatus.OK)
  171. .body(new ResultMessage().setCode(true).setData(openid).setMessage("SUCCESS"));
  172. }
  173. }