CustomizedHandler.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package com.iamberry.wechat.handles.customized;
  2. import com.iamberry.wechat.core.entity.ResultMsg;
  3. import com.iamberry.wechat.core.entity.WechatUtils;
  4. import com.iamberry.wechat.core.entity.customized.CustomizedColor;
  5. import com.iamberry.wechat.core.entity.customized.CustomizedPattern;
  6. import com.iamberry.wechat.core.entity.customized.CustomizedPosition;
  7. import com.iamberry.wechat.core.entity.customized.CustomizedTooth;
  8. import com.iamberry.wechat.core.entity.member.Member;
  9. import com.iamberry.wechat.face.customized.CustomizedColorService;
  10. import com.iamberry.wechat.face.customized.CustomizedPatternService;
  11. import com.iamberry.wechat.face.customized.CustomizedPositionService;
  12. import com.iamberry.wechat.face.customized.CustomizedToothService;
  13. import com.iamberry.wechat.face.member.MemberService;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import sun.misc.BASE64Decoder;
  20. import javax.servlet.http.HttpServletRequest;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.text.SimpleDateFormat;
  24. import java.util.Date;
  25. import java.util.List;
  26. /**
  27. * 定制牙刷
  28. */
  29. @Controller
  30. @RequestMapping("/wechat/customized")
  31. public class CustomizedHandler {
  32. @Autowired
  33. private CustomizedColorService customizedColorService;
  34. @Autowired
  35. private CustomizedPatternService customizedPatternService;
  36. @Autowired
  37. private CustomizedPositionService customizedPositionService;
  38. @Autowired
  39. private CustomizedToothService customizedToothService;
  40. @Autowired
  41. private MemberService memberService;
  42. /**
  43. * 获取颜色和图案
  44. * @param req
  45. * @return
  46. */
  47. @RequestMapping("/color_patterns")
  48. @ResponseBody
  49. public ResultMsg getColorPatterns(HttpServletRequest req){
  50. ResultMsg rm = ResultMsg.getSuccess();
  51. CustomizedColor customizedColor = new CustomizedColor();
  52. customizedColor.setColorStatus(1);
  53. List<CustomizedColor> customizedColorList = customizedColorService.getCustomizedColorList(customizedColor);
  54. CustomizedPattern customizedPattern = new CustomizedPattern();
  55. customizedPattern.setPatternStatus(1);
  56. List<CustomizedPattern> customizedPatternList = customizedPatternService.getCustomizedPatternList(customizedPattern);
  57. rm.addData("customizedColorList",customizedColorList);
  58. rm.addData("customizedPatternList",customizedPatternList);
  59. return rm;
  60. }
  61. /**
  62. * 根据纹理获取位置信息
  63. * @param req
  64. * @return
  65. */
  66. @RequestMapping("/get_position")
  67. @ResponseBody
  68. public ResultMsg getPosition(HttpServletRequest req,Integer patternId){
  69. ResultMsg rm = ResultMsg.getSuccess();
  70. if(patternId == null){
  71. return ResultMsg.getError();
  72. }
  73. CustomizedPosition customizedPosition = new CustomizedPosition();
  74. customizedPosition.setPositionStatus(1);
  75. customizedPosition.setPatternId(patternId);
  76. List<CustomizedPosition> customizedPositionList = customizedPositionService.getCustomizedPositionList(customizedPosition);
  77. rm.addData("customizedPositionList",customizedPositionList);
  78. return rm;
  79. }
  80. /**
  81. * 上传头像
  82. * @param req
  83. * @return
  84. */
  85. @RequestMapping("/upload_avatar")
  86. @ResponseBody
  87. public ResultMsg uploadAvatar(HttpServletRequest req,String avatarImg){
  88. // Member member = WechatUtils.getUserBySession(req);
  89. // member = memberService.getMemberByUserOpenId(member.getUserOpenid());
  90. ResultMsg rm = ResultMsg.getSuccess();
  91. if(avatarImg == null || "".equals(avatarImg)){
  92. return ResultMsg.getError();
  93. }
  94. String acatar[] = avatarImg.split(",");
  95. avatarImg = acatar[1];
  96. String dataStaing = new SimpleDateFormat("yyyyMMdd").format(new Date());
  97. String path = "common/images/customized/avatar/" + dataStaing + "/";
  98. // 生成文件
  99. String filePath = req.getSession().getServletContext().getRealPath("/") + path ;
  100. File imgfile = new File(filePath); //全路径
  101. if (!imgfile.exists()){
  102. imgfile.mkdirs();
  103. }
  104. // 生成文件名
  105. String files = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + ".png";
  106. // 生成文件
  107. String filename = filePath + files;
  108. String avatarImgPath = path + files; //返回路径
  109. byte[] buffer;//图像转换的目的字节数组
  110. BASE64Decoder base64 = new BASE64Decoder(); //新建64位解码器
  111. try {
  112. buffer = base64.decodeBuffer(avatarImg);//64位解码到字节数组中
  113. //写进文件 创建一个向具有指定名称的文件中写入数据的输出文件流。创建一个新 FileDescriptor 对象来表示此文件连接。
  114. File file = new File(filename); //全路径
  115. if (!file.exists()){
  116. file.createNewFile();
  117. }
  118. FileOutputStream fos = new FileOutputStream(file); //设置好保存路径,创建文件输出流。
  119. fos.write(buffer); //写入
  120. fos.flush(); //刷新
  121. fos.close(); //关闭
  122. fos = null;//释放
  123. }catch (Exception e){
  124. e.printStackTrace();
  125. return ResultMsg.getError();
  126. }
  127. rm.addData("avatarImgPath",avatarImgPath);
  128. return rm;
  129. }
  130. /**
  131. * 生成牙刷
  132. * @param req
  133. * @param productId 商品id
  134. * @param colorId 颜色id
  135. * @param toothColorId 定制颜色id
  136. * @param positionId 位置id
  137. * @param avatarImgPath 头像图片位置
  138. * @param toothImg 牙刷
  139. * @param toothText 文字
  140. * @return
  141. */
  142. @RequestMapping("/generate")
  143. @ResponseBody
  144. public ResultMsg generate(HttpServletRequest req,
  145. @RequestParam(value= "productId",defaultValue= "70" ,required=false) Integer productId,
  146. @RequestParam(value= "colorId",defaultValue= "75" ,required=false) Integer colorId,
  147. Integer toothColorId,Integer positionId,String avatarImgPath,String toothImg,String toothText){
  148. Member member = WechatUtils.getUserBySession(req);
  149. member = memberService.getMemberByUserOpenId(member.getUserOpenid());
  150. ResultMsg rm = ResultMsg.getSuccess();
  151. if(toothColorId == null || positionId == null || avatarImgPath == null || "".equals(avatarImgPath)
  152. || toothImg == null || "".equals(toothImg) || toothText == null || "".equals(toothText) ){
  153. return ResultMsg.getError();
  154. }
  155. /*生成图片*/
  156. String acatar[] = toothImg.split(",");
  157. toothImg = acatar[1];
  158. String dataStaing = new SimpleDateFormat("yyyyMMdd").format(new Date());
  159. String path = "common/images/customized/tooth/" + dataStaing + "/";
  160. // 生成文件
  161. String filePath = req.getSession().getServletContext().getRealPath("/") + path ;
  162. File imgfile = new File(filePath); //全路径
  163. if (!imgfile.exists()){
  164. imgfile.mkdirs();
  165. }
  166. // 生成文件名
  167. String files = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + ".png";
  168. // 生成文件
  169. String filename = filePath + files;
  170. String toothImgPath = path + files; //返回路径
  171. byte[] buffer;//图像转换的目的字节数组
  172. BASE64Decoder base64 = new BASE64Decoder(); //新建64位解码器
  173. try {
  174. buffer = base64.decodeBuffer(toothImg);//64位解码到字节数组中
  175. File file = new File(filename); //全路径
  176. if (!file.exists()){
  177. file.createNewFile();
  178. }
  179. FileOutputStream fos = new FileOutputStream(file); //设置好保存路径,创建文件输出流。
  180. fos.write(buffer); //写入
  181. fos.flush(); //刷新
  182. fos.close(); //关闭
  183. fos = null;//释放
  184. }catch (Exception e){
  185. e.printStackTrace();
  186. return ResultMsg.getError();
  187. }
  188. CustomizedPosition customizedPosition = customizedPositionService.getCustomizedPositionById(positionId);
  189. CustomizedTooth customizedTooth = new CustomizedTooth();
  190. customizedTooth.setUserOpenId(member.getUserOpenid());
  191. customizedTooth.setProductId(productId);
  192. customizedTooth.setColorId(colorId);
  193. customizedTooth.setToothColorId(toothColorId);
  194. customizedTooth.setToothPatternId(customizedPosition.getPatternId());
  195. customizedTooth.setToothPositionId(positionId);
  196. customizedTooth.setToothAvatarImg(avatarImgPath);
  197. customizedTooth.setToothImg(toothImgPath);
  198. customizedTooth.setToothText(toothText);
  199. customizedTooth.setToothType(1); //1微信
  200. customizedTooth.setToothStatus(1); //正常
  201. Integer flag = customizedToothService.save(customizedTooth);
  202. if(flag < 0){
  203. return ResultMsg.getError();
  204. }
  205. return rm.addData("toothId",customizedTooth.getToothId());
  206. }
  207. /**
  208. * 生成位置集合
  209. * @return
  210. */
  211. // @RequestMapping("/gp")
  212. // @ResponseBody
  213. // public ResultMsg generatedPosition(HttpServletRequest req){
  214. // ResultMsg rm = ResultMsg.getSuccess();
  215. // List<CustomizedPosition> customizedPositionList = new ArrayList<>();
  216. // List<CustomizedPattern> customizedPatternList = customizedPatternService.getCustomizedPatternList(new CustomizedPattern());
  217. // for (CustomizedPattern customizedPattern:customizedPatternList) {
  218. // for (int i=0;i<4;i++){
  219. // CustomizedPosition customizedPosition = new CustomizedPosition();
  220. // customizedPosition.setPatternId(customizedPattern.getPatternId());
  221. // customizedPosition.setPositionType(i+1); //位置类型 1:正面 2:右侧 3:反面 4:左侧
  222. // customizedPosition.setPositionPatternX(1000);
  223. // customizedPosition.setPositionPatternY(1000);
  224. // customizedPosition.setPositionAvatarX(1000);
  225. // customizedPosition.setPositionAvatarY(1000);
  226. // customizedPosition.setPositionTextX(1000);
  227. // customizedPosition.setPositionTextY(1000);
  228. // customizedPosition.setPositionStatus(1);
  229. // customizedPositionList.add(customizedPosition);
  230. // }
  231. // }
  232. // Integer flag = customizedPositionService.saveList(customizedPositionList);
  233. // return rm;
  234. // }
  235. }