OrderController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package com.caimei.controller.order;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.caimei.entity.CmOperationUser;
  5. import com.caimei.entity.CmOrder;
  6. import com.caimei.entity.Page;
  7. import com.caimei.entity.WxJsonModel;
  8. import com.caimei.service.order.OrderService;
  9. import com.caimei.service.user.LoginService;
  10. import com.caimei.utils.HttpRequest;
  11. import com.caimei.utils.JsonMapper;
  12. import com.github.pagehelper.PageHelper;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.ResponseBody;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpSession;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * 订单
  29. */
  30. @Controller
  31. @RequestMapping("/order")
  32. public class OrderController {
  33. protected static final Logger logger = LoggerFactory.getLogger(OrderController.class);
  34. @Autowired
  35. private OrderService orderService;
  36. @Autowired
  37. private LoginService loginService;
  38. @Value("${miniprogram.AppId}")
  39. private String AppId;
  40. @Value("${miniprogram.AppSecret}")
  41. private String AppSecret;
  42. /**
  43. * 确认订单信息
  44. *
  45. * @return
  46. */
  47. @ResponseBody
  48. @RequestMapping("/confirm")
  49. public WxJsonModel confirmOrder(Integer userId, String classifyIDS, Integer organizeID, String productIDs, Integer count) {
  50. Map<String, Object> map = orderService.confirmOrder(userId, classifyIDS, organizeID, productIDs, count);
  51. return WxJsonModel.newInstance().success(map);
  52. }
  53. /**
  54. * 分享订单
  55. *
  56. * @return
  57. */
  58. @ResponseBody
  59. @RequestMapping("/share")
  60. public WxJsonModel shareOrder(Integer orderID) {
  61. String shareCode = orderService.shareOrder(orderID);
  62. return WxJsonModel.newInstance().success(shareCode);
  63. }
  64. /**
  65. * 分享订单,分享码验证通过
  66. */
  67. @ResponseBody
  68. @RequestMapping("/shareCode")
  69. public WxJsonModel verifyShareCode(String shareCode, String code, Integer orderID, Integer userID,
  70. Integer organizeID, HttpServletRequest request) {
  71. WxJsonModel model = WxJsonModel.newInstance();
  72. logger.info("Start get SessionKey");
  73. Map<String, Object> map = new HashMap<>();
  74. String referer = request.getHeader("Referer"); //获取当前微信小程序的环境
  75. logger.info("referer-is----:" + referer);
  76. map.put("referer", referer);
  77. String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
  78. Map<String, String> requestUrlParam = new HashMap<String, String>();
  79. requestUrlParam.put("appid", AppId);//小程序appId
  80. requestUrlParam.put("secret", AppSecret);//小程序appsecret
  81. requestUrlParam.put("js_code", code);//小程序端返回的code
  82. requestUrlParam.put("grant_type", "authorization_code");//默认参数
  83. //发送post请求读取调用微信接口获取openid用户唯一标识
  84. String infos;
  85. try {
  86. infos = HttpRequest.sendPost(requestUrl, requestUrlParam);
  87. } catch (Exception e) {
  88. model.setData(map);
  89. return model.error("服务器内部异常");
  90. }
  91. //解析相应内容(转换成json对象)
  92. JSONObject jsonObject = JSON.parseObject(infos);
  93. String openid = jsonObject.getString("openid");
  94. logger.info("openid----->" + openid);
  95. String session_key = jsonObject.getString("session_key");
  96. String errcode = jsonObject.getString("errcode");
  97. String errmsg = jsonObject.getString("errmsg");
  98. if (!org.springframework.util.StringUtils.isEmpty(errcode) &&
  99. (errcode.equals("-1") || errcode.equals("40029") || errcode.equals("45011"))) {
  100. model.setMsg(errmsg);
  101. model.setData(map);
  102. map.put("sessionKey", session_key);
  103. model.setCode("-1");
  104. return model;
  105. }
  106. if (orderID == null || userID == null || organizeID == null) {
  107. model.error("参数异常");
  108. }
  109. WxJsonModel jsonModel = orderService.verifyShareCode(shareCode, openid, orderID, userID, organizeID);
  110. return jsonModel;
  111. }
  112. /**
  113. * 订单详情
  114. */
  115. @ResponseBody
  116. @RequestMapping("/particulars")
  117. public WxJsonModel particulars(Integer orderID) {
  118. Map<String, Object> map = orderService.particulars(orderID);
  119. return WxJsonModel.newInstance().success(map);
  120. }
  121. /**
  122. * 提交订单接口
  123. *
  124. * @return code:-1=(用户账户异常,数据异常,操作异常等),1提交成功(msg=1支付完成,2未完成支付)
  125. * @Param params参数格式:
  126. * 参数1=userId用户ID
  127. * 参数2=organizeID组织ID,
  128. * 参数3=cartType购买类型(1购物车提交,2直接购买提交)
  129. * 参数4=ddressID订单收货地址
  130. * 参数5=orderInfo订单信息参数格式:
  131. * [
  132. * {"shopId":1001,
  133. * "note":81,
  134. * "productInfo":[{"productId":1,"productNum":80},{"productId":2,"productNum":100}]
  135. * }
  136. * ]
  137. * 参数6=balanceDeductionFlag是否使用余额抵扣(1使用,2不使用)
  138. * 参数7=orderShouldPayFee订单应付金额(商品总金额 - 余额抵扣 - 经理折扣默认为0)
  139. */
  140. @Transactional
  141. @ResponseBody
  142. @RequestMapping("/submitOrder")
  143. public synchronized WxJsonModel submitOrder(String params, HttpServletRequest request) {
  144. WxJsonModel wxJsonModel = WxJsonModel.newInstance();
  145. logger.info(">>>>>>订单信息params:" + params);
  146. //检查用户是否登入
  147. // CmOperationUser currentUser = SessionHelper.getCurrentUser(request);
  148. HttpSession session = request.getSession();
  149. String openid = (String) session.getAttribute("openid");
  150. Integer organizeID1 = (Integer) session.getAttribute("organizeID");
  151. // CmOperationUser currentUser = loginService.doLogin("oEjHd4gCC7SO5Eo3ogt5g4pj2mNU", 1);
  152. CmOperationUser currentUser = loginService.doLogin(openid, organizeID1);
  153. if (null == currentUser) {
  154. return wxJsonModel.error("-1", "用户账户异常");
  155. }
  156. Integer cmOperationID = currentUser.getId();//当前操作者ID
  157. if (StringUtils.isBlank(params)) {
  158. return wxJsonModel.error("-1", "数据异常");
  159. }
  160. Map<String, Object> map = new HashMap<String, Object>();
  161. try {
  162. map = (Map<String, Object>) JsonMapper.getInstance().fromJsonString(params, Map.class);
  163. if (null == map) {
  164. logger.info(">>>>>数据异常,参数不能为空");
  165. return wxJsonModel.error("-1", "数据异常");
  166. }
  167. Integer userId = (Integer) map.get("userId");
  168. Integer organizeID = (Integer) map.get("organizeID");
  169. String cartType = String.valueOf(map.get("cartType"));
  170. Integer addressID = (Integer) map.get("addressID");
  171. Object orderInfo = map.get("orderInfo");
  172. String balanceDeductionFlag = String.valueOf(map.get("balanceDeductionFlag"));
  173. Double orderShouldPayFee = Double.parseDouble(String.valueOf(map.get("orderShouldPayFee")));//此金额为前端计算,适用于后端计算金额复查
  174. //校验传入参数的正确性
  175. if (null == userId) {
  176. return wxJsonModel.error("-1", "用户数据异常");
  177. }
  178. if (null == organizeID) {
  179. return wxJsonModel.error("-1", "组织数据异常");
  180. }
  181. if (StringUtils.isEmpty(cartType)) {
  182. return wxJsonModel.error("-1", "购买类型数据异常");
  183. }
  184. if (null == addressID) {
  185. return wxJsonModel.error("-1", "地址数据异常");
  186. }
  187. if (null == orderInfo) {
  188. return wxJsonModel.error("-1", "订单数据异常");
  189. }
  190. if (StringUtils.isEmpty(balanceDeductionFlag)) {
  191. return wxJsonModel.error("-1", "余额抵扣数据异常");
  192. }
  193. if (null == orderShouldPayFee) {
  194. return wxJsonModel.error("-1", "订单应付金额数据异常");
  195. }
  196. //保存订单信息
  197. return orderService.saveOrderInfo(wxJsonModel, userId, organizeID, cartType, addressID, orderInfo, balanceDeductionFlag, orderShouldPayFee, cmOperationID);
  198. } catch (Exception e) {
  199. logger.info(">>>>>系统异常" + e.getMessage());
  200. return wxJsonModel.error("-1", "数据异常");
  201. }
  202. }
  203. /**
  204. * 物流详情
  205. */
  206. @ResponseBody
  207. @RequestMapping("/logistics")
  208. public WxJsonModel logistics(Integer orderID) {
  209. WxJsonModel model = WxJsonModel.newInstance();
  210. Map<String, Object> map = orderService.logistics(orderID);
  211. return model.success(map);
  212. }
  213. /**
  214. * 查询我的订单
  215. *
  216. * @param orderState 订单状态
  217. * @return
  218. */
  219. @ResponseBody
  220. @RequestMapping("/myOrder")
  221. public WxJsonModel myOrder(Integer orderState, Integer userID, Integer index, Integer pageSize, Integer organizeID) {
  222. WxJsonModel model = WxJsonModel.newInstance();
  223. if (userID == null || orderState == null || organizeID == null) return model.error("参数异常");
  224. if (index == null) index = 1;
  225. if (pageSize == null) pageSize = 10;
  226. PageHelper.startPage(index, pageSize);
  227. List<CmOrder> orderList = orderService.myOrder(userID, orderState, organizeID);
  228. Page<CmOrder> page = new Page<>(orderList);
  229. return model.success(page);
  230. }
  231. /**
  232. * 常采购商品
  233. */
  234. @ResponseBody
  235. @RequestMapping("/purchase")
  236. public WxJsonModel oftenPurchase(Integer userID, Integer organizeID, Integer index, Integer pageSize) {
  237. WxJsonModel model = WxJsonModel.newInstance();
  238. if (userID == null || organizeID == null) return model.error("参数异常");
  239. Map<String, Object> map = orderService.oftenPurchase(userID, organizeID, index, pageSize);
  240. return model.success(map);
  241. }
  242. /**
  243. * 删除订单
  244. */
  245. @ResponseBody
  246. @RequestMapping("/delete")
  247. public WxJsonModel deleteOrder(Integer orderID) {
  248. WxJsonModel model = null;
  249. try {
  250. model = orderService.deleteOrder(orderID);
  251. } catch (Exception e) {
  252. return model.error("删除订单失败," + e.getMessage());
  253. }
  254. return model;
  255. }
  256. /**
  257. * 取消订单
  258. */
  259. @ResponseBody
  260. @RequestMapping("/cancel")
  261. public WxJsonModel cancelOrder(Integer orderID) {
  262. WxJsonModel model = WxJsonModel.newInstance();
  263. try {
  264. orderService.cancelOrder(orderID);
  265. } catch (Exception e) {
  266. return model.error("" + e.getMessage());
  267. }
  268. return model.success("取消订单成功", "");
  269. }
  270. /**
  271. * 确认收货
  272. */
  273. @ResponseBody
  274. @RequestMapping("/affirm")
  275. public WxJsonModel affirmCargo(Integer orderID) {
  276. WxJsonModel model = WxJsonModel.newInstance();
  277. try {
  278. orderService.affirmCargo(orderID);
  279. } catch (Exception e) {
  280. return model.error("" + e.getMessage());
  281. }
  282. return model.success("确认收货成功", "");
  283. }
  284. }