|
@@ -6,20 +6,21 @@
|
|
|
package com.shawn.web.controller;
|
|
|
|
|
|
import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.shawn.model.entity.*;
|
|
|
+import com.shawn.service.interfac.TAdminServiceInterface;
|
|
|
+import com.shawn.util.WeChatUtil;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+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.entity.TWeixin;
|
|
|
-import com.shawn.model.entity.TWeixinExample;
|
|
|
import com.shawn.model.param.TWeixinParam;
|
|
|
import com.shawn.service.interfac.TWeixinServiceInterface;
|
|
|
|
|
@@ -35,10 +36,137 @@ public class TWeixinController extends BaseController<TWeixin,TWeixinExample,TWe
|
|
|
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);
|
|
|
+ return ResponseEntity.status(HttpStatus.OK)
|
|
|
+ .body(new ResultMessage().setCode(true).setData(list.get(0)).setMessage("SUCCESS"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }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);
|
|
|
+ return ResponseEntity.status(HttpStatus.OK)
|
|
|
+ .body(new ResultMessage().setCode(true).setData(list.get(0)).setMessage("SUCCESS"));
|
|
|
+ }
|
|
|
+ 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"));
|
|
|
+
|
|
|
+ }
|
|
|
}
|