123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- /**
- * Date:2019-12-25 11:12:55
- * author:吴洪双
- */
- package com.shawn.web.controller;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLDecoder;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import com.alibaba.fastjson.JSONObject;
- //import com.paytm.pg.merchant.CheckSumServiceHelper;
- import com.shawn.model.entity.*;
- import com.shawn.service.interfac.TAdminServiceInterface;
- import com.shawn.util.JavaWebToken;
- import com.shawn.util.JoinpayConstant;
- import com.shawn.util.PushUtils;
- import com.shawn.util.WeChatUtil;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.annotation.Order;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- import com.shawn.model.dto.ResultMessage;
- import com.shawn.web.controller.base.BaseController;
- import com.shawn.web.exception.MyException;
- import com.shawn.model.param.TWeixinParam;
- import com.shawn.service.interfac.TWeixinServiceInterface;
- import lombok.extern.apachecommons.CommonsLog;
- import javax.servlet.http.HttpServletRequest;
- @CommonsLog
- @RestController
- @RequestMapping("TWeixin")
- public class TWeixinController extends BaseController<TWeixin,TWeixinExample,TWeixinParam,Integer>{
- @Autowired
- private TWeixinServiceInterface tWeixinService;
- @Autowired
- public TWeixinController(TWeixinServiceInterface service) {
- super(service);
- }
- @Autowired
- private TAdminServiceInterface tAdminService;
- @Override
- protected TWeixinExample createNewExample() {
- return new TWeixinExample();
- }
- /**
- * 微信登录
- * @param
- * @return
- */
- @PostMapping("/weiXinLogin")
- public ResponseEntity<?> weiXinLogin(@RequestBody TWeixin weiXin) {
- TWeixin tWeixin = tWeixinService.findByEntity(weiXin);
- if(tWeixin!=null){
- String adminId = tWeixin.getAdminId();
- if(adminId.length()>0){
- TAdminExample example = new TAdminExample();
- TAdminExample.Criteria criteria = example.createCriteria();
- criteria.andIdEqualTo(Long.valueOf(adminId));
- List<TAdmin> list = tAdminService.selectByOption(example);
- if(list.size()>0){
- tWeixin.setModifyDate(new Date());
- tWeixin.setNickName(weiXin.getNickName());
- tWeixin.setAvatarUrl(weiXin.getAvatarUrl());
- tWeixinService.updateById(tWeixin);
- Map<String,Object> m = new HashMap<String,Object>();
- m.put("userId",list.get(0).getId());
- // 根据存在用户的id生成token字符串
- String token = JavaWebToken.createJavaWebToken(m);
- return ResponseEntity.status(HttpStatus.OK)
- .body(new ResultMessage().setCode(true).setData(list.get(0)).setMessage("SUCCESS").setToken(token));
- }
- }
- }else {
- return ResponseEntity.status(HttpStatus.OK)
- .body(new ResultMessage().setCode(true).setData(weiXin).setMessage("off"));
- }
- return null;
- }
- /**
- * 微信注册
- * @param
- * @return
- */
- @PostMapping("/weiXinZhuce")
- public ResponseEntity<?> weiXinZhuce(@RequestBody weixinParm weiXin) {
- // 检查必输项
- if(StringUtils.isEmpty(weiXin.getUsername())||StringUtils.isEmpty(weiXin.getPassword())){
- throw new MyException("用户名密码不能为空");
- }
- TAdminExample example = new TAdminExample();
- TAdminExample.Criteria criteria = example.createCriteria();
- criteria.andUsernameEqualTo(weiXin.getUsername());
- criteria.andPasswordEqualTo(weiXin.getPassword());
- List<TAdmin> list = tAdminService.selectByOption(example);
- if(list.size()>0){
- Long adminId = list.get(0).getId();
- TWeixin newWeixin = new TWeixin();
- String adminid = String.valueOf(adminId);
- newWeixin.setAdminId(adminid);
- newWeixin.setNickName(weiXin.getNickName());
- newWeixin.setAvatarUrl(weiXin.getAvatarUrl());
- newWeixin.setOpenId(weiXin.getOpenId());
- newWeixin.setCreateDate(new Date());
- newWeixin.setModifyDate(new Date());
- Integer integer = tWeixinService.insert(newWeixin);
- Map<String,Object> m = new HashMap<String,Object>();
- m.put("userId",list.get(0).getId());
- // 根据存在用户的id生成token字符串
- String token = JavaWebToken.createJavaWebToken(m);
- return ResponseEntity.status(HttpStatus.OK)
- .body(new ResultMessage().setCode(true).setData(list.get(0)).setMessage("SUCCESS").setToken(token));
- }
- return ResponseEntity.status(HttpStatus.OK)
- .body(new ResultMessage().setCode(false).setData(null).setMessage("用户名或密码错误"));
- }
- /**
- * 微信注册
- * @param
- * @return
- */
- @PostMapping("/weiXinSearch")
- public ResponseEntity<?> weiXinSearch(@RequestBody TWeixin weiXin) {
- TWeixinExample example = new TWeixinExample();
- TWeixinExample.Criteria criteria = example.createCriteria();
- criteria.andAdminIdEqualTo(weiXin.getAdminId());
- List<TWeixin> tWeixins = tWeixinService.selectByOption(example);
- if(tWeixins.size()>0){
- return ResponseEntity.status(HttpStatus.OK)
- .body(new ResultMessage().setCode(true).setData(tWeixins).setMessage("SUCCESS"));
- }else {
- return ResponseEntity.status(HttpStatus.OK)
- .body(new ResultMessage().setCode(true).setData(null).setMessage("没有绑定微信"));
- }
- }
- /**
- * 解除微信绑定
- * @param
- * @return
- */
- @PostMapping("/deleteWeixin")
- public ResponseEntity<?> deleteWeixin(@RequestBody TWeixin weiXin) {
- Integer integer = tWeixinService.deleteById(weiXin.getId());
- return ResponseEntity.status(HttpStatus.OK)
- .body(new ResultMessage().setCode(true).setData(integer).setMessage("SUCCESS"));
- }
- /**
- * 获取微信openid
- * @param
- * @return
- */
- @GetMapping("/getOpenid")
- public ResponseEntity<?> getOpenid(String code) {
- // 微信小程序ID
- String appid = "wx3844925af1740a7d";
- // 微信小程序秘钥
- String secret = "c1f0363a78d1c580388d6c4f14674733";
- // 根据小程序穿过来的code想这个url发送请求
- String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
- // 发送请求,返回Json字符串
- String str = WeChatUtil.httpRequest(url, "GET", null);
- // 转成Json对象 获取openid
- JSONObject jsonObject = JSONObject.parseObject(str);
- // 我们需要的openid,在一个小程序中,openid是唯一的
- String openid = jsonObject.get("openid").toString();
- return ResponseEntity.status(HttpStatus.OK)
- .body(new ResultMessage().setCode(true).setData(openid).setMessage("SUCCESS"));
- }
- }
|