Order.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\OrderService;
  5. use app\common\service\ToolService;
  6. use think\Exception;
  7. use think\exception\HttpResponseException;
  8. use think\Log;
  9. class Order extends Api
  10. {
  11. protected $noNeedLogin = ['payNotify', 'detail'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 列表
  15. *
  16. * @ApiMethod (POST)
  17. * @param string $desc 需求描述
  18. * @param array $images 图片数组
  19. * @param array $files 文件数组
  20. * @param string $contact 联系方式
  21. */
  22. public function list()
  23. {
  24. try {
  25. $user_id = $this->auth->id ?? -1;
  26. $params = $this->request->param('parameter/a', []);
  27. $page = $params['pageNum'] ?? 1;
  28. $page_size = $params['pageSize'] ?? 10;
  29. $order_status = $params['orderStatus'] ?? null;
  30. $order_type = $params['orderType'] ?? 0;
  31. $keywords = $params['keywords'] ?? '';
  32. $where = [
  33. 'user_id' => $user_id,
  34. ];
  35. if ($keywords) {
  36. $where['order_no'] = $keywords;
  37. }
  38. if ($order_type) {
  39. $where['order_type'] = $order_type;
  40. }
  41. if (!is_null($order_status)) {
  42. $where['order_status'] = $order_status;
  43. }
  44. $list = OrderService::getList($where, $page_size, 'id desc', '', ['orderGoods', 'orderPayment'], ['config' => ['page' => $page]]);
  45. $pageList = $list->toArray();
  46. $pageList['data'] = convert_keys_to_camelcase($pageList['data']);
  47. foreach ($pageList['data'] as &$item) {
  48. $item['status'] = $item['orderStatus'];
  49. $item['statusName'] = OrderService::ORDER_STATUS_MAP[$item['orderStatus']] ?? '';
  50. $item['buttonVOs'] = OrderService::getOrderButtons($item['orderStatus']);
  51. $item['groupInfoVo'] = [];
  52. $item['freightFee'] = 0;
  53. }
  54. $this->success('', $pageList);
  55. } catch (\Exception $e) {
  56. if ($e instanceof HttpResponseException) {
  57. throw $e;
  58. }
  59. $this->error($e->getMessage());
  60. }
  61. }
  62. public function count()
  63. {
  64. try {
  65. $data = OrderService::getOrderCount($this->auth->id ?? 0);
  66. $this->success('', $data);
  67. } catch (\Exception $e) {
  68. if ($e instanceof HttpResponseException) {
  69. throw $e;
  70. }
  71. $this->error($e->getMessage());
  72. }
  73. }
  74. public function detail()
  75. {
  76. try {
  77. $orderNo = $this->request->get('order_no', 0);
  78. $where = [
  79. 'order_no' => $orderNo
  80. ];
  81. $info = OrderService::getOne($where, ['orderPayment', 'orderShip', 'orderGoods'=>function($query){
  82. $query->with('goodsInfo');
  83. }]);
  84. if (!$info) {
  85. throw new Exception('该订单不存在或已删除');
  86. }
  87. //格式转换
  88. $info = convert_keys_to_camelcase($info->toArray());
  89. $info['orderStatusName'] = $info['orderStatusText'];
  90. $info['orderStatusRemark'] = '';
  91. $info['storeName'] = '商品信息';
  92. $info['invoiceStatus'] = 3;
  93. $info['invoiceDesc'] = '';
  94. $info['formatCreateTime'] = date('Y-m-d H:i', $info['createtime']);
  95. $info['buttonVOs'] = []; // [['primary' => true, 'type' => 1, 'name' => '付款']],
  96. $this->success('', $info);
  97. } catch (\Exception $e) {
  98. if ($e instanceof HttpResponseException) {
  99. throw $e;
  100. }
  101. $this->error($e->getMessage());
  102. }
  103. }
  104. public function settleDetail()
  105. {
  106. try {
  107. $goodsRequestList = $this->request->post('goodsRequestList/a', []);
  108. $storeInfoList = $this->request->post('storeInfoList/a', []);
  109. $couponList = $this->request->post('couponList/a', []);
  110. $totalSalePrice = 0;
  111. $totalPayAmount = 0;
  112. $totalGoodsCount = 0;
  113. $storeGoodsList = [];
  114. $goodsList = [];
  115. foreach ($goodsRequestList as $goods) {
  116. $totalGoodsCount += $goods['quantity'];
  117. $totalSalePrice += $goods['quantity'] * $goods['price'];
  118. $goodsList[] = [
  119. 'storeId' => $goods['storeId'], // 假设这是从其他地方获取的数据
  120. 'spuId' => $goods['spuId'],
  121. 'skuId' => 0,
  122. 'goodsName' => $goods['goodsName'],
  123. 'image' => $goods['primaryImage'],
  124. 'quantity' => $goods['quantity'],
  125. 'price' => $goods['price'],
  126. 'settlePrice' => $goods['price'],
  127. 'originPrice' => $goods['price'],
  128. 'specInfo' => [
  129. ],
  130. 'roomId' => ''
  131. ];
  132. $storeGoodsList[] = [
  133. 'storeId' => $goods['storeId'],
  134. 'storeName' => '',
  135. 'remark' => null,
  136. 'goodsCount' => $goods['quantity'],
  137. 'deliveryFee' => '0',
  138. 'deliveryWords' => null,
  139. 'storeTotalAmount' => $totalSalePrice,
  140. 'storeTotalPayAmount' => $totalSalePrice,
  141. 'storeTotalDiscountAmount' => '0',
  142. 'storeTotalCouponAmount' => '0',
  143. 'skuDetailVos' => $goodsList,
  144. 'couponList' => [
  145. ],
  146. ];
  147. }
  148. $resp = [
  149. 'settleType' => 1, // 0不可结算 1可结算
  150. 'userAddress' => null,
  151. 'totalGoodsCount' => $totalGoodsCount,
  152. 'packageCount' => 1,
  153. 'totalAmount' => $totalSalePrice,
  154. 'totalPayAmount' => $totalSalePrice,
  155. 'totalDiscountAmount' => 0,
  156. 'totalPromotionAmount' => 0,
  157. 'totalCouponAmount' => '0',
  158. 'totalSalePrice' => $totalSalePrice,
  159. 'totalGoodsAmount' => $totalSalePrice,
  160. 'totalDeliveryFee' => '0',
  161. 'invoiceRequest' => null,
  162. 'skuImages' => null,
  163. 'deliveryFeeList' => null,
  164. 'storeGoodsList' => $storeGoodsList,
  165. 'inValidGoodsList' => null,
  166. 'outOfStockGoodsList' => null,
  167. 'limitGoodsList' => null,
  168. 'abnormalDeliveryGoodsList' => null,
  169. 'invoiceSupport' => 1,
  170. ];
  171. $this->success('', $resp);
  172. } catch (\Exception $e) {
  173. if ($e instanceof HttpResponseException) {
  174. throw $e;
  175. }
  176. $this->error($e->getMessage());
  177. }
  178. }
  179. public function commitPay()
  180. {
  181. try {
  182. $param = $this->request->param();
  183. $param['userInfo'] = $this->auth->getUserinfo();
  184. $res = !empty($param['orderNo']) ? OrderService::orderPay($param) : OrderService::createOrder($param);
  185. $this->success('', $res);
  186. } catch (\Exception $e) {
  187. if ($e instanceof HttpResponseException) {
  188. throw $e;
  189. }
  190. $this->error($e->getMessage());
  191. }
  192. }
  193. /**
  194. * 订单取消
  195. * @return void
  196. */
  197. public function cancelOrder()
  198. {
  199. try {
  200. $orderId = $this->request->param('order_id', 0);
  201. $userId = $this->auth->id ?? 0;
  202. $res = OrderService::cancelOrder($orderId, $userId);
  203. $this->success('', $res);
  204. } catch (\Exception $e) {
  205. if ($e instanceof HttpResponseException) {
  206. throw $e;
  207. }
  208. $this->error($e->getMessage());
  209. }
  210. }
  211. /**
  212. * @param
  213. */
  214. public function orderOperate()
  215. {
  216. try {
  217. $param = $this->request->param();
  218. $userId = $this->auth->id ?? 0;
  219. $res = OrderService::orderOperate($param, $userId);
  220. $this->success('', $res);
  221. } catch (\Exception $e) {
  222. if ($e instanceof HttpResponseException) {
  223. throw $e;
  224. }
  225. $this->error($e->getMessage());
  226. }
  227. }
  228. /**
  229. * 支付回调
  230. * @return void
  231. * @throws \Exception
  232. */
  233. public function payNotify()
  234. {
  235. header('Content-Type: text/xml; charset=utf-8');
  236. try {
  237. $xml = OrderService::payNotify();
  238. echo $xml;
  239. exit;
  240. } catch (\Exception $e) {
  241. $res = [
  242. 'return_code' => 'FAIL',
  243. 'return_msg' => $e->getMessage()
  244. ];
  245. $xml = ToolService::ToXml($res);
  246. Log::info('微信回调信息信息_result_error:' . json_encode(['res' => $res, 'xml' => $xml], JSON_UNESCAPED_UNICODE));
  247. echo $xml;
  248. exit;
  249. }
  250. }
  251. public function businessTime()
  252. {
  253. $res = [
  254. 'businessTime' => ['周一,周二,周三,周四,周五:00:20:00-08:00:00'],
  255. 'telphone' => '18565372257',
  256. 'saasId' => '88888',
  257. ];
  258. $this->success('Success', $res);
  259. }
  260. }