Commit 8d8ebbc2 authored by liuyang's avatar liuyang

修改工程

parent 54084114
...@@ -7,6 +7,11 @@ import com.google.common.collect.Lists; ...@@ -7,6 +7,11 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.qiankun.annotation.Auth; import com.qiankun.annotation.Auth;
import com.qiankun.config.WxMaConfiguration; import com.qiankun.config.WxMaConfiguration;
import com.qiankun.config.WxMaProperties;
import com.qiankun.dao.RainbowPlanUserDao;
import com.qiankun.dao.WishDao;
import com.qiankun.entity.RainbowPlanUser;
import com.qiankun.entity.Wish;
import com.qiankun.utils.JwtTokenUtil; import com.qiankun.utils.JwtTokenUtil;
import com.qiankun.vo.PublishWish; import com.qiankun.vo.PublishWish;
import com.qiankun.vo.RegisterInfo; import com.qiankun.vo.RegisterInfo;
...@@ -27,49 +32,53 @@ import java.io.File; ...@@ -27,49 +32,53 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.ResourceBundle;
@RestController @RestController
@RequestMapping("/wx/rainbowplan/{appid}") @RequestMapping("/wx/rainbowplan")
public class RainbowPlanController { public class RainbowPlanController {
private final Logger logger = LoggerFactory.getLogger(RainbowPlanController.class); private final Logger logger = LoggerFactory.getLogger(RainbowPlanController.class);
private static Map<String, UserInfo> userInfoMap = Maps.newConcurrentMap();
private static Map<String, List<PublishWish>> wishMap = Maps.newConcurrentMap();
@Autowired @Autowired
private JwtTokenUtil tokenUtil; private JwtTokenUtil tokenUtil;
@Autowired
private RainbowPlanUserDao userDao;
@Autowired
private WishDao wishDao;
@Autowired
private WxMaProperties propertis;
private ResourceBundle rb = ResourceBundle.getBundle("system");
private String uploadAvatarPath = File.separator + "rainbowplan" + File.separator + "uploadAvatar";
private String uploadWishImagePath = File.separator + "rainbowplan" + File.separator + "wishUploadImage";
@Auth(verifyLogin = false,verifyURL = false) private String getAppid(){
@RequestMapping(value = "/validateExpired",method = RequestMethod.GET) return propertis.getConfigs().get(0).getAppid();
public Map<String,Object> validateToken(@PathVariable String appid,@RequestHeader("token") String token){
Map<String,Object> result = Maps.newConcurrentMap();
result.put("result",tokenUtil.isTokenExpired(token));
return result;
} }
@Auth(verifyLogin = false,verifyURL = false) @Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/login",method = RequestMethod.GET) @RequestMapping(value = "/login",method = RequestMethod.GET)
public Map<String,String> login(@PathVariable String appid, @RequestParam String code){ public Map<String,Object> login(@RequestParam String code){
if (StringUtils.isBlank(code)) { if (StringUtils.isBlank(code)) {
return null; return null;
} }
final WxMaService wxService = WxMaConfiguration.getMaService(appid); final WxMaService wxService = WxMaConfiguration.getMaService(getAppid());
try { try {
WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code); WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
this.logger.info(session.getSessionKey()); this.logger.info(session.getSessionKey());
this.logger.info(session.getOpenid()); this.logger.info(session.getOpenid());
Map<String,String> result = Maps.newConcurrentMap(); Map<String,Object> result = Maps.newConcurrentMap();
//TODO 判断openid是否在数据库中存在
if (userInfoMap.containsKey(session.getOpenid())){ RainbowPlanUser userInfo = userDao.findByOpenid(session.getOpenid());
//生成sKey if (userInfo != null){
String sKey = tokenUtil.generateToken(session.getOpenid()); String sKey = tokenUtil.generateToken(userInfo.getOpenid());
// result.put("session",session);
result.put("sKey",sKey); result.put("sKey",sKey);
result.put("userInfo",userInfo);
}else { }else {
result.put("sKey",""); result.put("sKey","");
result.put("userInfo",null);
} }
result.put("openid",session.getOpenid());
return result; return result;
} catch (WxErrorException e) { } catch (WxErrorException e) {
...@@ -79,132 +88,171 @@ public class RainbowPlanController { ...@@ -79,132 +88,171 @@ public class RainbowPlanController {
} }
@Auth(verifyLogin = false,verifyURL = false) @Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/register",method = RequestMethod.POST) @RequestMapping(value = "/register",method = RequestMethod.POST)
public String register(@PathVariable String appid,@RequestBody RegisterInfo info){ public String register(@RequestBody RegisterInfo info) throws Exception {
if (Strings.isNullOrEmpty(info.getOpenid())){ if (Strings.isNullOrEmpty(info.getOpenid())){
throw new IllegalArgumentException("请求参数错误"); throw new IllegalArgumentException("请求参数错误");
} }
UserInfo userInfo = new UserInfo(); RainbowPlanUser user = new RainbowPlanUser();
userInfo.setUid(UUID.randomUUID().toString()); user.setPhone(info.getPhone());
userInfo.setPhone(info.getPhone()); user.setName(info.getName());
userInfo.setName(info.getName()); user.setGender(info.getGender());
userInfo.setGender(info.getGender()); if (Strings.isNullOrEmpty(info.getInviter())){
userInfoMap.put(info.getOpenid(),userInfo); user.setStatus(RainbowPlanUser.STATUS_UNAUDIT);
}else if (inviterExist(info.getInviter())){
user.setStatus(RainbowPlanUser.STATUS_AUDIT);
user.setInviterId(info.getInviter());
}else {
throw new Exception("注册失败,邀请者不存在");
}
userDao.save(user);
return tokenUtil.generateToken(info.getOpenid()); return tokenUtil.generateToken(info.getOpenid());
} }
private boolean inviterExist(String inviterId) {
//TODO 判断邀请人是否存在
return true;
}
@Auth(verifyLogin = false,verifyURL = false) @Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/userInfo",method = RequestMethod.GET) @RequestMapping(value = "/userInfo",method = RequestMethod.GET)
public UserInfo getUserInfo(@PathVariable String appid, @RequestHeader String sKey) throws Exception { public RainbowPlanUser getUserInfo(@RequestHeader String sKey) throws Exception {
if (Strings.isNullOrEmpty(sKey)){ if (Strings.isNullOrEmpty(sKey)){
throw new IllegalArgumentException("请求参数错误"); throw new IllegalArgumentException("请求参数错误");
} }
String openid = tokenUtil.getUsernameFromToken(sKey); String openid = tokenUtil.getUsernameFromToken(sKey);
if (!userInfoMap.containsKey(openid)) { RainbowPlanUser user = userDao.findByOpenid(openid);
if (user == null) {
throw new Exception("请求的用户不存在"); throw new Exception("请求的用户不存在");
} }
return userInfoMap.get(openid); return user;
} }
@Auth(verifyLogin = false,verifyURL = false) @Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/userInfo",method = RequestMethod.POST) @RequestMapping(value = "/userInfo",method = RequestMethod.POST)
public String updateUserInfo(@PathVariable String appid,@RequestBody UserInfo info,@RequestHeader String sKey){ public String updateUserInfo(@RequestBody UserInfo info,@RequestHeader String sKey) throws Exception {
if (Strings.isNullOrEmpty(sKey)){ if (Strings.isNullOrEmpty(sKey)){
throw new IllegalArgumentException("请求参数错误"); throw new IllegalArgumentException("请求参数错误");
} }
String openid = tokenUtil.getUsernameFromToken(sKey); String openid = tokenUtil.getUsernameFromToken(sKey);
UserInfo userInfo = findUserInfo(openid); RainbowPlanUser user = userDao.findByOpenid(openid);
userInfo.setName(info.getName()); if (user != null){
userInfo.setNickName(info.getNickName()); user.setPhone(info.getPhone());
userInfo.setPhone(info.getPhone()); user.setName(info.getName());
userInfo.setGender(info.getGender()); user.setGender(info.getGender());
userInfo.setBirthday(info.getBirthday()); user.setBirthday(info.getBirthday());
userInfo.setBedNum(info.getBedNum()); user.setBedNum(info.getBedNum());
userInfo.setReceiveName(info.getReceiveName()); user.setReceiveName(info.getReceiveName());
userInfo.setConnectTel(info.getConnectTel()); user.setConnectTel(info.getConnectTel());
userInfo.setReceiveAddress(info.getReceiveAddress()); user.setReceiveAddress(info.getReceiveAddress());
userInfo.setAvatar(info.getAvatar()); }else {
throw new Exception("保存失败,请求用户不存在");
}
return "OK"; return "OK";
} }
@Auth(verifyLogin = false,verifyURL = false) @Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/userInfoWithAvatar",method = RequestMethod.POST) @RequestMapping(value = "/userInfoWithAvatar",method = RequestMethod.POST)
public String updateUserInfoWithAvatar(@PathVariable String appid, UserInfo info, @RequestHeader String sKey) throws IOException { public String updateUserInfoWithAvatar(UserInfo info, @RequestHeader String sKey) throws Exception {
if (Strings.isNullOrEmpty(sKey)){ if (Strings.isNullOrEmpty(sKey)){
throw new IllegalArgumentException("请求参数错误"); throw new IllegalArgumentException("请求参数错误");
} }
String openid = tokenUtil.getUsernameFromToken(sKey); String openid = tokenUtil.getUsernameFromToken(sKey);
UserInfo userInfo = findUserInfo(openid); RainbowPlanUser user = userDao.findByOpenid(openid);
if (!info.getFile().isEmpty()){ if (user != null){
// BASE64Encoder encoder = new BASE64Encoder(); if (!info.getFile().isEmpty()){
// String imageData = encoder.encode(info.getFile().getBytes()); String originalFileName = info.getFile().getOriginalFilename();
String rootPath = rb.getString("file_path");
File destFile = new File(rootPath + uploadAvatarPath,originalFileName);
String originalFileName = info.getFile().getOriginalFilename(); FileUtils.writeByteArrayToFile(destFile,info.getFile().getBytes());
String path = "/Users/liuyang/uploadImage"; user.setPhone(info.getPhone());
File destFile = new File(path,originalFileName); user.setName(info.getName());
FileUtils.writeByteArrayToFile(destFile,info.getFile().getBytes()); user.setGender(info.getGender());
userInfo.setName(info.getName()); user.setBirthday(info.getBirthday());
userInfo.setNickName(info.getNickName()); user.setBedNum(info.getBedNum());
userInfo.setPhone(info.getPhone()); user.setReceiveName(info.getReceiveName());
userInfo.setGender(info.getGender()); user.setConnectTel(info.getConnectTel());
userInfo.setBirthday(info.getBirthday()); user.setReceiveAddress(info.getReceiveAddress());
userInfo.setBedNum(info.getBedNum()); user.setAvatar(uploadAvatarPath + File.separator + originalFileName);
userInfo.setReceiveName(info.getReceiveName()); userDao.update(user);
userInfo.setConnectTel(info.getConnectTel()); }else {
userInfo.setReceiveAddress(info.getReceiveAddress()); throw new Exception("保存失败,请上传图片");
userInfo.setAvatar(info.getAvatar()); }
userInfo.setAvatar(originalFileName); }else {
throw new Exception("保存失败,请求用户不存在");
} }
return "OK"; return "OK";
} }
@Auth(verifyLogin = false,verifyURL = false) @Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/publishWish",method = RequestMethod.POST) @RequestMapping(value = "/publishWish",method = RequestMethod.POST)
public String publishWish(@PathVariable String appid, PublishWish publishWish,@RequestHeader String sKey) throws IOException { public String publishWish(PublishWish publishWish,@RequestHeader String sKey) throws Exception {
if (Strings.isNullOrEmpty(sKey)){ if (Strings.isNullOrEmpty(sKey)){
throw new IllegalArgumentException("请求参数错误"); throw new IllegalArgumentException("请求参数错误");
} }
String openid = tokenUtil.getUsernameFromToken(sKey); String openid = tokenUtil.getUsernameFromToken(sKey);
if (!publishWish.getFile().isEmpty()){ RainbowPlanUser user = userDao.findByOpenid(openid);
// BASE64Encoder encoder = new BASE64Encoder(); if (user != null){
// String imageData = encoder.encode(info.getFile().getBytes()); if (!publishWish.getFile().isEmpty()){
String originalFileName = publishWish.getFile().getOriginalFilename();
String originalFileName = publishWish.getFile().getOriginalFilename(); String rootPath = rb.getString("file_path");
String path = "/Users/liuyang/uploadImage"; File destFile = new File(rootPath + uploadWishImagePath,originalFileName);
File destFile = new File(path,originalFileName); FileUtils.writeByteArrayToFile(destFile,publishWish.getFile().getBytes());
FileUtils.writeByteArrayToFile(destFile,publishWish.getFile().getBytes()); Wish wish = new Wish();
List<PublishWish> wishList = Lists.newArrayList(); wish.setTitle(publishWish.getTitle());
if (wishMap.containsKey(openid)){ wish.setContent(publishWish.getWish());
wishList = wishMap.get(openid); wish.setUploadImage(uploadWishImagePath + File.separator + originalFileName);
wish.setStatus(Wish.STATUS_PUBLISH);
wish.setRainbowPlanUserId(user.getId());
wishDao.save(wish);
}else {
throw new Exception("保存失败,请上传图片");
} }
PublishWish wish = new PublishWish(); }else {
wish.setTitle(publishWish.getTitle()); throw new Exception("保存失败,请求用户不存在");
wish.setWish(publishWish.getWish());
wish.setWishImage(originalFileName);
wishList.add(wish);
wishMap.put(openid,wishList);
} }
return "OK"; return "OK";
} }
@Auth(verifyLogin = false,verifyURL = false) @Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/wish/list",method = RequestMethod.GET) @RequestMapping(value = "/wish/list",method = RequestMethod.GET)
public List<PublishWish> wishList(@PathVariable String appid,@RequestHeader String sKey){ public List<Wish> wishList(@RequestHeader String sKey) throws Exception {
if (Strings.isNullOrEmpty(sKey)){ if (Strings.isNullOrEmpty(sKey)){
throw new IllegalArgumentException("请求参数错误"); throw new IllegalArgumentException("请求参数错误");
} }
String openid = tokenUtil.getUsernameFromToken(sKey); String openid = tokenUtil.getUsernameFromToken(sKey);
return wishMap.get(openid); RainbowPlanUser user = userDao.findByOpenid(openid);
List<Wish> resultList = Lists.newArrayList();
if (user != null){
wishDao.findByRainbowPlanUserId(user.getId());
}else {
throw new Exception("获取数据失败,请求用户不存在");
}
return resultList;
} }
@Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/wish",method = RequestMethod.GET)
public Wish getWish(@RequestParam String id,@RequestHeader String sKey){
if (Strings.isNullOrEmpty(sKey)){
throw new IllegalArgumentException("请求参数错误");
}
String openid = tokenUtil.getUsernameFromToken(sKey);
Wish wish = wishDao.findById(id);
return wish;
private UserInfo findUserInfo(String openid){
return userInfoMap.get(openid);
} }
@Auth(verifyLogin = false,verifyURL = false) @Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/loadResource",method = RequestMethod.GET) @RequestMapping(value = "/loadResource",method = RequestMethod.GET)
public ResponseEntity<byte[]> loadResource(@RequestParam String imageName) throws IOException { public ResponseEntity<byte[]> loadResource(@RequestParam String imageName) throws IOException {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
File file = new File("/Users/liuyang/uploadImage",imageName); File file = new File(rb.getString("file_path"),imageName);
return new ResponseEntity<>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK); return new ResponseEntity<>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
} }
} }
package com.qiankun.dao;
import com.qiankun.dao.core.IDao;
import com.qiankun.entity.RainbowPlanUser;
public interface RainbowPlanUserDao extends IDao<RainbowPlanUser,String> {
RainbowPlanUser findByOpenid(String openid);
}
package com.qiankun.dao;
import com.qiankun.dao.core.AbsDao;
import com.qiankun.dao.core.Hints;
import com.qiankun.entity.RainbowPlanUser;
import org.springframework.stereotype.Repository;
@Repository
public class RainbowPlanUserDaoImpl extends AbsDao<RainbowPlanUser,String> implements RainbowPlanUserDao {
@Override
public RainbowPlanUser findByOpenid(String openid) {
return findUnique("from RainbowPlanUser where openid=?",new Hints(),openid);
}
}
package com.qiankun.dao;
import com.qiankun.dao.core.IDao;
import com.qiankun.entity.Wish;
import java.util.List;
public interface WishDao extends IDao<Wish,String> {
List<Wish> findByRainbowPlanUserId(String id);
Wish findById(String id);
}
package com.qiankun.dao;
import com.qiankun.dao.core.AbsDao;
import com.qiankun.dao.core.Hints;
import com.qiankun.entity.Wish;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class WishDaoImpl extends AbsDao<Wish,String> implements WishDao {
@Override
public List<Wish> findByRainbowPlanUserId(String rainbowPlanUserId) {
return find(" from Wish where rainbowPlanUserId = ? order by createTime desc",new Hints(0),rainbowPlanUserId);
}
@Override
public Wish findById(String id) {
return findUnique("from Wish where id = ?",new Hints(),id);
}
}
package com.qiankun.entity;
import org.hibernate.annotations.GenericGenerator;
import org.omg.CORBA.PUBLIC_MEMBER;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
@Entity
public class RainbowPlanUser {
public static final int STATUS_UNAUDIT = 0;
public static final int STATUS_AUDIT = 1;
@Id
@GenericGenerator(name = "systemUUID", strategy = "uuid2")
@GeneratedValue(generator = "systemUUID")
private String id;
private String openid;//小程序用户ID
private String unionid; //微信全局ID
private String phone; //注册手机号
private String name; //真实姓名
private String nickName; //昵称
private String gender; //性别
private String birthday; //出生日期
private String bedNum; //楼层床号
private String receiveName; //收件人姓名
private String connectTel;//联系方式
private String receiveAddress;//收件地址
private int status;//用户状态 0:已注册未审核,1:已审核
private String avatar;//头像图片名
private Date createTime = new Date();
private String inviterId;//邀请者ID
public String getInviterId() {
return inviterId;
}
public void setInviterId(String inviterId) {
this.inviterId = inviterId;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public String getUnionid() {
return unionid;
}
public void setUnionid(String unionid) {
this.unionid = unionid;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getBedNum() {
return bedNum;
}
public void setBedNum(String bedNum) {
this.bedNum = bedNum;
}
public String getReceiveName() {
return receiveName;
}
public void setReceiveName(String receiveName) {
this.receiveName = receiveName;
}
public String getConnectTel() {
return connectTel;
}
public void setConnectTel(String connectTel) {
this.connectTel = connectTel;
}
public String getReceiveAddress() {
return receiveAddress;
}
public void setReceiveAddress(String receiveAddress) {
this.receiveAddress = receiveAddress;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
}
package com.qiankun.entity;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
@Entity
public class Wish {
public static final int STATUS_PUBLISH = 0;
public static final int STATUS_RECEIVE = 1;
public static final int STATUS_COMPLETE = 2;
@Id
@GenericGenerator(name = "systemUUID", strategy = "uuid2")
@GeneratedValue(generator = "systemUUID")
private String id; //愿望ID
private String title;//愿望标题
private String content;//愿望内容
private String uploadImage; //上传的图片
private int status;//愿望状态,0:发布;1:领取;2:实现
private String volunteerId; //领取愿望的志愿者ID
private String rainbowPlanUserId; //小朋友ID
private Date createTime = new Date();
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getRainbowPlanUserId() {
return rainbowPlanUserId;
}
public void setRainbowPlanUserId(String rainbowPlanUserId) {
this.rainbowPlanUserId = rainbowPlanUserId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getUploadImage() {
return uploadImage;
}
public void setUploadImage(String uploadImage) {
this.uploadImage = uploadImage;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getVolunteerId() {
return volunteerId;
}
public void setVolunteerId(String volunteerId) {
this.volunteerId = volunteerId;
}
}
...@@ -4,11 +4,20 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -4,11 +4,20 @@ import org.springframework.web.multipart.MultipartFile;
public class PublishWish { public class PublishWish {
public String id;
public String title; public String title;
public String wish; public String wish;
public String wishImage; public String wishImage;
public MultipartFile file; public MultipartFile file;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() { public String getTitle() {
return title; return title;
} }
......
file_path=D:/file/ #file_path=D:/file/
#file_path=/usr/local/tomcat/upload/ file_path=/Users/liuyang/work/argus_work/tjmdp/udata
account=admin account=admin
password=passed password=passed
rainbowplan.appid=wx7289ed9f69379a53 rainbowplan.appid=wxd097672957e48ba8
rainbowplan.secret=cc4df9e8a63d2b0faa2a25bbec79d2e3 rainbowplan.secret=6500d648cc5715f8829dab993de746f3
rainbowplan.token= rainbowplan.token=
rainbowplan.aesKey= rainbowplan.aesKey=
rainbowplan.msgDataFormat: JSON rainbowplan.msgDataFormat: JSON
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment