Order.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/11/9
  6. * Time: 10:00 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use addons\unishop\extend\Hashids;
  10. use addons\unishop\model\Area;
  11. use addons\unishop\model\Config;
  12. use addons\unishop\model\Evaluate;
  13. use addons\unishop\model\Product;
  14. use app\admin\model\unishop\Coupon as CouponModel;
  15. use addons\unishop\model\DeliveryRule as DeliveryRuleModel;
  16. use addons\unishop\model\OrderRefund;
  17. use app\admin\model\unishop\OrderRefundProduct;
  18. use think\Db;
  19. use think\Exception;
  20. use addons\unishop\model\Address as AddressModel;
  21. use think\Hook;
  22. use think\Loader;
  23. /**
  24. * 订单
  25. */
  26. class Order extends Base
  27. {
  28. /**
  29. * 允许频繁访问的接口
  30. * @var array
  31. */
  32. protected $frequently = ['getorders'];
  33. protected $noNeedLogin = ['count'];
  34. /**
  35. * @ApiTitle (创建订单)
  36. * @ApiSummary (创建订单)
  37. * @ApiMethod (POST)
  38. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  39. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  40. * @ApiParams (name="id", type=string, required=true, description="商品id")
  41. * @ApiReturn ({"code":1,"msg":"","data":{}})
  42. *
  43. * @ApiReturnParams (name="order_id", type="string", description="订单编号")
  44. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号(支付用)")
  45. *
  46. * @ApiReturnParams (name="product.title", type="string", description="商品名称")
  47. * @ApiReturnParams (name="product.image", type="string", description="商品图片")
  48. * @ApiReturnParams (name="product.sales", type="integer", description="销量")
  49. * @ApiReturnParams (name="product.sales_price", type="string", description="销售价钱")
  50. * @ApiReturnParams (name="product.market_price", type="string", description="市场价钱")
  51. * @ApiReturnParams (name="product.id", type="string", description="商品id")
  52. * @ApiReturnParams (name="product.stock", type="integer", description="库存")
  53. * @ApiReturnParams (name="product.spec", type="integer", description="选中的规格")
  54. * @ApiReturnParams (name="product.number", type="integer", description="购买数量")
  55. *
  56. * @ApiReturnParams (name="address.id", type="integer", description="地址id")
  57. * @ApiReturnParams (name="address.name", type="string", description="收货人名称")
  58. * @ApiReturnParams (name="address.mobile", type="string", description="收货人电话")
  59. * @ApiReturnParams (name="address.address", type="string", description="收货人地址")
  60. * @ApiReturnParams (name="address.province_id", type="integer", description="省份id")
  61. * @ApiReturnParams (name="address.city_id", type="integer", description="城市id")
  62. * @ApiReturnParams (name="address.area_id", type="integer", description="地区id")
  63. * @ApiReturnParams (name="address.is_default", type="integer", description="是否默认")
  64. * @ApiReturnParams (name="address.province.name", type="integer", description="省份")
  65. * @ApiReturnParams (name="address.city.name", type="integer", description="城市")
  66. * @ApiReturnParams (name="address.area.name", type="integer", description="地区")
  67. *
  68. * @ApiReturnParams (name="coupon.id", type="integer", description="优惠券id")
  69. * @ApiReturnParams (name="coupon.title", type="string", description="优惠券名称")
  70. * @ApiReturnParams (name="coupon.least", type="integer", description="至少购买金额")
  71. * @ApiReturnParams (name="coupon.value", type="integer", description="满减金额")
  72. * @ApiReturnParams (name="coupon.starttime_text", type="integer", description="开始使用时间")
  73. * @ApiReturnParams (name="coupon.endtime_text", type="integer", description="到期使用时间")
  74. *
  75. * @ApiReturnParams (name="delivery.id", type="integer", description="货运id")
  76. * @ApiReturnParams (name="delivery.name", type="string", description="货运名称")
  77. * @ApiReturnParams (name="delivery.type", type="string", description="收费类型")
  78. * @ApiReturnParams (name="delivery.min", type="integer", description="至少购买量")
  79. * @ApiReturnParams (name="delivery.first", type="integer", description="首重数量")
  80. * @ApiReturnParams (name="delivery.first_fee", type="string", description="首重价钱")
  81. * @ApiReturnParams (name="delivery.additional", type="integer", description="需重数量")
  82. * @ApiReturnParams (name="delivery.additional_fee", type="string", description="需重价钱") *
  83. *
  84. */
  85. public function create()
  86. {
  87. $productId = $this->request->post('id', 0);
  88. try {
  89. $user_id = $this->auth->id;
  90. // 单个商品
  91. if ($productId) {
  92. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  93. $product = (new Product)->where(['id' => $productId, 'switch' => Product::SWITCH_ON, 'deletetime' => null])->find();
  94. /** 产品基础数据 **/
  95. $spec = $this->request->post('spec', '');
  96. $productData[0] = $product->getDataOnCreateOrder($spec);
  97. } else {
  98. // 多个商品
  99. $cart = $this->request->post('cart');
  100. $carts = (new \addons\unishop\model\Cart)
  101. ->whereIn('id', $cart)
  102. ->with(['product'])
  103. ->order(['id' => 'desc'])
  104. ->select();
  105. foreach ($carts as $cart) {
  106. if ($cart->product instanceof Product) {
  107. $productData[] = $cart->product->getDataOnCreateOrder($cart->spec ? $cart->spec : '', $cart->number);
  108. }
  109. }
  110. }
  111. if (empty($productData) || !$productData) {
  112. $this->error(__('Product not exist'));
  113. }
  114. /** 默认地址 **/
  115. $address = (new AddressModel)->where(['user_id' => $user_id, 'is_default' => AddressModel::IS_DEFAULT_YES])->find();
  116. if ($address) {
  117. $area = (new Area)->whereIn('id', [$address->province_id, $address->city_id, $address->area_id])->column('name', 'id');
  118. $address = $address->toArray();
  119. $address['province']['name'] = $area[$address['province_id']];
  120. $address['city']['name'] = $area[$address['city_id']];
  121. $address['area']['name'] = $area[$address['area_id']];
  122. }
  123. /** 可用优惠券 **/
  124. $coupon = CouponModel::all(function ($query) {
  125. $time = time();
  126. $query
  127. ->where(['switch' => CouponModel::SWITCH_ON])
  128. ->where('starttime', '<', $time)
  129. ->where('endtime', '>', $time);
  130. });
  131. if ($coupon) {
  132. $coupon = collection($coupon)->toArray();
  133. }
  134. /** 运费数据 **/
  135. $cityId = $address['city_id'] ? $address['city_id'] : 0;
  136. $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
  137. foreach ($productData as &$product) {
  138. $product['image'] = Config::getImagesFullUrl($product['image']);
  139. $product['sales_price'] = round($product['sales_price'], 2);
  140. $product['market_price'] = round($product['market_price'], 2);
  141. }
  142. $this->success('', [
  143. 'product' => $productData,
  144. 'address' => $address,
  145. 'coupon' => $coupon,
  146. 'delivery' => $delivery['list']
  147. ]);
  148. } catch (Exception $e) {
  149. $this->error($e->getMessage(), false);
  150. }
  151. }
  152. /**
  153. * @ApiTitle (提交订单)
  154. * @ApiSummary (提交订单)
  155. * @ApiMethod (POST)
  156. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  157. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  158. * @ApiParams (name="coupon_id", type=string, required=true, description="优惠券id")
  159. * @ApiParams (name="product_id", type=string, required=true, description="商品id,多个用法(product_id,product_id,product_id)")
  160. * @ApiParams (name="number", type=string, required=true, description="商品数量,多个用法(number,number,number)")
  161. * @ApiParams (name="spec", type=string, required=true, description="规格,多个用法(spec|spec2,spec|spec2,spec|spec2)")
  162. * @ApiParams (name="city_id", type=integer, required=true, description="城市id")
  163. * @ApiParams (name="address_id", type=string, required=true, description="收货地址id")
  164. * @ApiParams (name="delivery_id", type=integer, required=true, description="运费模板id")
  165. * @ApiParams (name="remark", type=string, required=true, description="备注")
  166. * @ApiReturn ({"code":1,"msg":"","data":{}})
  167. *
  168. * @ApiReturnParams (name="order_id", type="string", description="订单编号")
  169. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号(支付用)")
  170. *
  171. */
  172. public function submit()
  173. {
  174. $data = $this->request->post();
  175. try {
  176. $validate = Loader::validate('\\addons\\unishop\\validate\\Order');
  177. if (!$validate->check($data, [], 'submit')) {
  178. throw new Exception($validate->getError());
  179. }
  180. Db::startTrans();
  181. // 判断创建订单的条件
  182. if (empty(Hook::get('create_order_before'))) {
  183. Hook::add('create_order_before', 'addons\\unishop\\behavior\\Order');
  184. }
  185. // 减少商品库存,增加"已下单未支付数量"
  186. if (empty(Hook::get('create_order_after'))) {
  187. Hook::add('create_order_after', 'addons\\unishop\\behavior\\Order');
  188. }
  189. $orderModel = new \addons\unishop\model\Order();
  190. $result = $orderModel->createOrder($this->auth->id, $data);
  191. Db::commit();
  192. } catch (Exception $e) {
  193. Db::rollback();
  194. $this->error($e->getMessage(), false);
  195. }
  196. $this->success('', $result);
  197. }
  198. /**
  199. * @ApiTitle (获取运费模板)
  200. * @ApiSummary (获取运费模板)
  201. * @ApiMethod (POST)
  202. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  203. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  204. * @ApiParams (name="city_id", type=integer, required=true, description="城市id")
  205. * @ApiReturn ({"code":1,"msg":"","data":[]})
  206. *
  207. * @ApiReturnParams (name="id", type="integer", description="模板id")
  208. * @ApiReturnParams (name="name", type="string", description="模板名称")
  209. * @ApiReturnParams (name="type", type="string", description="模板类型")
  210. * @ApiReturnParams (name="min", type="integer", description="至少购买量")
  211. * @ApiReturnParams (name="first", type="integer", description="首重数量")
  212. * @ApiReturnParams (name="first_fee", type="string", description="首重价钱")
  213. * @ApiReturnParams (name="additional", type="integer", description="需重数量")
  214. * @ApiReturnParams (name="additional_fee", type="string", description="需重价钱") *
  215. *
  216. */
  217. public function getDelivery()
  218. {
  219. $cityId = $this->request->get('city_id', 0);
  220. $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
  221. $this->success('', $delivery['list']);
  222. }
  223. /**
  224. * @ApiTitle (获取订单列表)
  225. * @ApiSummary (获取订单列表信息)
  226. * @ApiMethod (POST)
  227. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  228. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  229. * @ApiParams (name="type", type=integer, required=true, description="类型:0=全部,1=待付款,2=待发货,3=待收货,4=待评价,5=售后")
  230. * @ApiParams (name="page", type=integer, required=true, description="第几页")
  231. * @ApiParams (name="pagesize", type=integer, required=true, description="每页数量")
  232. * @ApiReturn ({"code":1,"msg":"","data":[]})
  233. *
  234. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号")
  235. * @ApiReturnParams (name="order_price", type="string", description="订单原价钱")
  236. * @ApiReturnParams (name="discount_price", type="string", description="优惠多少钱")
  237. * @ApiReturnParams (name="delivery_price", type="string", description="运费多少钱")
  238. * @ApiReturnParams (name="total_price", type="string", description="订单实价")
  239. * @ApiReturnParams (name="pay_type", type="integer", description="支付类型")
  240. * @ApiReturnParams (name="id", type="string", description="下单ip")
  241. * @ApiReturnParams (name="remark", type="string", description="订单备注")
  242. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  243. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  244. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  245. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  246. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  247. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  248. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  249. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  250. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  251. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  252. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  253. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  254. * @ApiReturnParams (name="products[].evaluate", type="integer", description="是否已评价")
  255. * @ApiReturnParams (name="products[].refund", type="integer", description="是否已退款")
  256. * @ApiReturnParams (name="extend.express_number", type="integer", description="运单号")
  257. * @ApiReturnParams (name="order_id", type="string", description="订单id")
  258. * @ApiReturnParams (name="state", type="string", description="状态类型:0=全部,1=待付款,2=待发货,3=待收货,4=待评价,5=售后")
  259. * @ApiReturnParams (name="refund_status_text", type="string", description="已退款")
  260. */
  261. public function getOrders()
  262. {
  263. // 0=全部,1=待付款,2=待发货,3=待收货,4=待评价,5=售后
  264. $type = $this->request->get('type', 0);
  265. $page = $this->request->get('page', 1);
  266. $pagesize = $this->request->get('pagesize', 10);
  267. try {
  268. $orderModel = new \addons\unishop\model\Order();
  269. $result = $orderModel->getOrdersByType($this->auth->id, $type, $page, $pagesize);
  270. } catch (Exception $e) {
  271. $this->error($e->getMessage());
  272. }
  273. $this->success('', $result);
  274. }
  275. /**
  276. * @ApiTitle (取消订单)
  277. * @ApiSummary (未支付的订单才叫取消,已支付的叫退货)
  278. * @ApiMethod (GET)
  279. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  280. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  281. * @ApiParams (name="order_id", type="string", description="订单id")
  282. * @ApiReturn ({"code":1,"msg":"取消成功","data":true})
  283. *
  284. */
  285. public function cancel()
  286. {
  287. $order_id = $this->request->get('order_id', 0);
  288. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  289. $orderModel = new \addons\unishop\model\Order();
  290. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  291. if (!$order) {
  292. $this->error(__('Order not exist'));
  293. }
  294. switch ($order['status']) {
  295. case \addons\unishop\model\Order::STATUS_REFUND:
  296. $this->error('此订单已退款,无法取消');
  297. break;
  298. case \addons\unishop\model\Order::STATUS_CANCEL:
  299. $this->error('此订单已取消, 无需再取消');
  300. break;
  301. }
  302. if ($order['have_paid'] != \addons\unishop\model\Order::PAID_NO) {
  303. $this->error('此订单已支付,无法取消');
  304. }
  305. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
  306. $order->status = \addons\unishop\model\Order::STATUS_CANCEL;
  307. $order->save();
  308. $this->success('取消成功', true);
  309. }
  310. }
  311. /**
  312. * @ApiTitle (删除订单)
  313. * @ApiSummary (只能删除已取消或已退货的订单)
  314. * @ApiMethod (GET)
  315. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  316. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  317. * @ApiParams (name="order_id", type="string", description="订单id")
  318. * @ApiReturn ({"code":1,"msg":"删除成功","data":true})
  319. *
  320. */
  321. public function delete()
  322. {
  323. $order_id = $this->request->get('order_id', 0);
  324. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  325. $orderModel = new \addons\unishop\model\Order();
  326. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  327. if (!$order) {
  328. $this->error(__('Order not exist'));
  329. }
  330. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL) {
  331. $this->error('只能删除已取消或已退货的订单');
  332. }
  333. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND && $order['refund_status'] == \addons\unishop\model\Order::REFUND_STATUS_APPLY) {
  334. $this->error('订单退款中,不可删除订单');
  335. }
  336. $order->delete();
  337. $this->success('删除成功', true);
  338. }
  339. /**
  340. * @ApiTitle (确认收货)
  341. * @ApiSummary (确认收货)
  342. * @ApiMethod (GET)
  343. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  344. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  345. * @ApiParams (name="order_id", type="string", description="订单id")
  346. * @ApiReturn ({"code":1,"msg":"已确认收货","data":true})
  347. *
  348. */
  349. public function received()
  350. {
  351. $order_id = $this->request->get('order_id', 0);
  352. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  353. $orderModel = new \addons\unishop\model\Order();
  354. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  355. if (!$order) {
  356. $this->error(__('Order not exist'));
  357. }
  358. if ($order->have_delivered == 0) {
  359. $this->error('未发货,不能确认收货');
  360. }
  361. $order->have_received = time();
  362. $order->save();
  363. $this->success('已确认收货', true);
  364. }
  365. /**
  366. * @ApiTitle (发表评论/评价)
  367. * @ApiSummary (发表评论/评价)
  368. * @ApiMethod (POST)
  369. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  370. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  371. * @ApiParams (name="order_id", type="string", description="订单id")
  372. * @ApiParams (name="rate", type="integer", description="分数/星星")
  373. * @ApiParams (name="anonymous", type="integer", description="是否匿名")
  374. * @ApiParams (name="comment", type="string", description="评价内容")
  375. * @ApiParams (name="product_id", type="string", description="商品id")
  376. * @ApiReturn ({"code":1,"msg":"感谢评价","data":true})
  377. *
  378. */
  379. public function comment()
  380. {
  381. $rate = $this->request->post('rate', 5);
  382. $anonymous = $this->request->post('anonymous', 0);
  383. $comment = $this->request->post('comment');
  384. $order_id = $this->request->post('order_id', 0);
  385. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  386. $product_id = $this->request->post('product_id');
  387. $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
  388. $orderProductModel = new \addons\unishop\model\OrderProduct();
  389. $orderProduct = $orderProductModel->where(['product_id' => $product_id, 'order_id' => $order_id, 'user_id' => $this->auth->id])->find();
  390. $orderModel = new \addons\unishop\model\Order();
  391. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  392. if (!$orderProduct || !$order) {
  393. $this->error(__('Order not exist'));
  394. }
  395. if ($order->have_received == $orderModel::RECEIVED_NO) {
  396. $this->error(__('未收货,不可评价'));
  397. }
  398. $result = false;
  399. try {
  400. $evaluate = new Evaluate();
  401. $evaluate->user_id = $this->auth->id;
  402. $evaluate->order_id = $order_id;
  403. $evaluate->product_id = $product_id;
  404. $evaluate->rate = $rate;
  405. $evaluate->anonymous = $anonymous;
  406. $evaluate->comment = $comment;
  407. $evaluate->spec = $orderProduct->spec;
  408. $result = $evaluate->save();
  409. if ($result) {
  410. $order->have_commented = time();
  411. $order->save();
  412. }
  413. } catch (Exception $e) {
  414. $this->error($e->getMessage());
  415. }
  416. if ($result !== false) {
  417. $this->success(__('Thanks for the evaluation'));
  418. } else {
  419. $this->error(__('Evaluation failure'));
  420. }
  421. }
  422. /**
  423. * @ApiTitle (获取订单数量)
  424. * @ApiSummary (获取订单数量)
  425. * @ApiMethod (POST)
  426. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  427. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  428. * @ApiReturn ({"code":1,"msg":"","data":{}})
  429. *
  430. * @ApiReturnParams (name="unpaid", type="integer", description="未支付数量")
  431. * @ApiReturnParams (name="undelivered", type="integer", description="未发货数量")
  432. * @ApiReturnParams (name="unreceived", type="integer", description="未收货数量")
  433. * @ApiReturnParams (name="uncomment", type="integer", description="未评价数量")
  434. * @ApiReturnParams (name="refund", type="integer", description="正在售后的数量")
  435. *
  436. */
  437. public function count()
  438. {
  439. if (!$this->auth->isLogin()) {
  440. $this->error('');
  441. }
  442. $order = new \addons\unishop\model\Order();
  443. $list = $order
  444. ->where([
  445. 'user_id' => $this->auth->id,
  446. ])
  447. ->where('status', '<>', \addons\unishop\model\Order::STATUS_CANCEL)
  448. ->where(function ($query) {
  449. $query
  450. ->whereOr([
  451. 'have_paid' => \addons\unishop\model\Order::PAID_NO,
  452. 'have_delivered' => \addons\unishop\model\Order::DELIVERED_NO,
  453. 'have_received' => \addons\unishop\model\Order::RECEIVED_NO,
  454. 'have_commented' => \addons\unishop\model\Order::COMMENTED_NO
  455. ])
  456. ->whereOr('refund_status', '>', \addons\unishop\model\Order::REFUND_STATUS_NONE);
  457. })
  458. ->field('have_paid,have_delivered,have_received,have_commented,refund_status,had_refund')
  459. ->select();
  460. $data = [
  461. 'unpaid' => 0,
  462. 'undelivered' => 0,
  463. 'unreceived' => 0,
  464. 'uncomment' => 0,
  465. 'refund' => 0
  466. ];
  467. foreach ($list as $item) {
  468. switch (true) {
  469. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] > 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  470. $data['uncomment']++;
  471. break;
  472. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  473. $data['unreceived']++;
  474. break;
  475. case $item['have_paid'] > 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  476. $data['undelivered']++;
  477. break;
  478. case $item['have_paid'] == 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  479. $data['unpaid']++;
  480. break;
  481. case $item['refund_status'] > 0 && $item['had_refund'] == 0 && $item['refund_status'] != 3:
  482. $data['refund']++;
  483. break;
  484. }
  485. }
  486. $this->success('', $data);
  487. }
  488. /**
  489. * @ApiTitle (订单详情细节)
  490. * @ApiSummary (订单详情细节)
  491. * @ApiMethod (GET)
  492. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  493. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  494. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  495. * @ApiReturn ({"code":1,"msg":"","data":{}})
  496. *
  497. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号")
  498. * @ApiReturnParams (name="order_price", type="string", description="订单原价钱")
  499. * @ApiReturnParams (name="discount_price", type="string", description="优惠多少钱")
  500. * @ApiReturnParams (name="delivery_price", type="string", description="运费多少钱")
  501. * @ApiReturnParams (name="total_price", type="string", description="订单实价")
  502. * @ApiReturnParams (name="pay_type", type="integer", description="支付类型")
  503. * @ApiReturnParams (name="id", type="string", description="下单ip")
  504. * @ApiReturnParams (name="remark", type="string", description="订单备注")
  505. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  506. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  507. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  508. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  509. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  510. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  511. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  512. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  513. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  514. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  515. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  516. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  517. * @ApiReturnParams (name="products[].evaluate", type="integer", description="是否已评价")
  518. * @ApiReturnParams (name="products[].refund", type="integer", description="是否已退款")
  519. * @ApiReturnParams (name="express_number", type="integer", description="运单号")
  520. * @ApiReturnParams (name="pay_type_text", type="string", description="支付类型简述")
  521. * @ApiReturnParams (name="refund_status_text", type="string", description="退款状态简述")
  522. * @ApiReturnParams (name="delivery.username", type="string", description="收货人名称")
  523. * @ApiReturnParams (name="delivery.mobile", type="string", description="收货人电话")
  524. * @ApiReturnParams (name="delivery.address", type="string", description="收货人地址")
  525. *
  526. */
  527. public function detail()
  528. {
  529. $order_id = $this->request->get('order_id', 0);
  530. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  531. try {
  532. $orderModel = new \addons\unishop\model\Order();
  533. $order = $orderModel
  534. ->with([
  535. 'products' => function ($query) {
  536. $query->field('id,order_id,image,number,price,spec,title,product_id');
  537. },
  538. 'extend' => function ($query) {
  539. $query->field('id,order_id,address_id,address_json,express_number,express_company');
  540. },
  541. 'evaluate' => function ($query) {
  542. $query->field('id,order_id,product_id');
  543. }
  544. ])
  545. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  546. if ($order) {
  547. $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
  548. // 快递单号
  549. $order['express_number'] = $order['extend']['express_number'];
  550. $order['express_company'] = '快递单号';
  551. $order['express'] = '';
  552. if (class_exists(\addons\expressquery\library\Expressquery::class)) {
  553. $expressInfo = Db::name('expressquery')->where(['express' => $order['extend']['express_company']])->find();
  554. $order['express_company'] = $expressInfo['name'] ?? '快递单号';
  555. $order['express'] = $expressInfo['express'] ?? '';
  556. }
  557. // 送货地址
  558. $address = json_decode($order['extend']['address_json'], true);
  559. $area = (new \addons\unishop\model\Area())
  560. ->whereIn('id', [$address['province_id'], $address['city_id'], $address['area_id']])
  561. ->column('name', 'id');
  562. $delivery['username'] = $address['name'];
  563. $delivery['mobile'] = $address['mobile'];
  564. $delivery['address'] = $area[$address['province_id']] . ' ' . $area[$address['city_id']] . ' ' . $area[$address['area_id']] . ' ' . $address['address'];
  565. $order['delivery'] = $delivery;
  566. // 是否已评论
  567. $evaluate = array_column($order['evaluate'], 'product_id');
  568. foreach ($order['products'] as &$product) {
  569. $product['image'] = Config::getImagesFullUrl($product['image']);
  570. if (in_array($product['id'], $evaluate)) {
  571. $product['evaluate'] = true;
  572. } else {
  573. $product['evaluate'] = false;
  574. }
  575. }
  576. unset($order['evaluate']);
  577. unset($order['extend']);
  578. }
  579. } catch (Exception $e) {
  580. $this->error($e->getMessage());
  581. }
  582. $this->success('', $order);
  583. }
  584. /**
  585. * @ApiTitle (申请售后信息)
  586. * @ApiSummary (申请售后信息)
  587. * @ApiMethod (POST)
  588. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  589. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  590. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  591. * @ApiReturn ({"code":1,"msg":"","data":{}})
  592. *
  593. * @ApiReturnParams (name="status", type="string", description="订单状态:-1=退货,0=取消订单,1=正常啊")
  594. * @ApiReturnParams (name="total_price", type="string", description="订单总价钱")
  595. * @ApiReturnParams (name="delivery_price", type="string", description="订单运费")
  596. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  597. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  598. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  599. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  600. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  601. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  602. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  603. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  604. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  605. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  606. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  607. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  608. * @ApiReturnParams (name="products[].choose", type="integer", description="是否选中")
  609. * @ApiReturnParams (name="refund_status_text", type="string", description="退款状态简述")
  610. * @ApiReturnParams (name="refund.id", type="string", description="退款信息id")
  611. * @ApiReturnParams (name="refund.amount", type="float",required=true, description="退款金额")
  612. * @ApiReturnParams (name="refund.service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  613. * @ApiReturnParams (name="refund.receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  614. * @ApiReturnParams (name="refund.reason_type", type="string",required=true, description="换货原因")
  615. * @ApiReturnParams (name="refund.refund_explain", type="string",required=true, description="退款说明")
  616. * @ApiReturnParams (name="refund.express_number", type="string",required=true, description="寄货物流单号")
  617. *
  618. */
  619. public function refundInfo()
  620. {
  621. $order_id = $this->request->post('order_id');
  622. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  623. $orderModel = new \addons\unishop\model\Order();
  624. $order = $orderModel
  625. ->with([
  626. 'products' => function ($query) {
  627. $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
  628. },
  629. 'refund',
  630. 'refundProducts'
  631. ])
  632. ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
  633. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  634. if (!$order) {
  635. $this->error(__('Order not exist'));
  636. }
  637. $order = $order->append(['refund_status_text'])->toArray();
  638. foreach ($order['products'] as &$product) {
  639. $product['image'] = Config::getImagesFullUrl($product['image']);
  640. $product['choose'] = 0;
  641. // 如果是已提交退货的全选
  642. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
  643. foreach ($order['refund_products'] as $refundProduct) {
  644. if ($product['order_product_id'] == $refundProduct['order_product_id']) {
  645. $product['choose'] = 1;
  646. }
  647. }
  648. }
  649. }
  650. unset($order['refund_products']);
  651. $this->success('', $order);
  652. }
  653. /**
  654. * @ApiTitle (提交申请售后)
  655. * @ApiSummary (提交申请售后)
  656. * @ApiMethod (POST)
  657. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  658. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  659. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  660. * @ApiParams (name="amount", type="float",required=true, description="退款金额")
  661. * @ApiParams (name="service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  662. * @ApiParams (name="receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  663. * @ApiParams (name="reason_type", type="string",required=true, description="换货原因")
  664. * @ApiParams (name="refund_explain", type="string",required=true, description="退款说明")
  665. * @ApiParams (name="order_product_id", type="integer",required=true, description="订单的商品的id")
  666. * @ApiReturn ({"code":1,"msg":"提交","data":1})
  667. *
  668. */
  669. public function refund()
  670. {
  671. $order_id = $this->request->post('order_id');
  672. $order_id = Hashids::decodeHex($order_id);
  673. $orderModel = new \addons\unishop\model\Order();
  674. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  675. if (!$order) {
  676. $this->error(__('Order not exist'));
  677. }
  678. if ($order['have_paid'] == 0) {
  679. $this->error(__('订单未支付,可直接取消,无需申请售后'));
  680. }
  681. $amount = $this->request->post('amount', 0);
  682. $serviceType = $this->request->post('service_type');
  683. $receivingStatus = $this->request->post('receiving_status');
  684. $reasonType = $this->request->post('reason_type');
  685. $refundExplain = $this->request->post('refund_explain');
  686. $orderProductId = $this->request->post('order_product_id');
  687. if (!$orderProductId) {
  688. $this->error(__('Please select goods'));
  689. }
  690. if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
  691. $this->error(__('Please select goods status'));
  692. }
  693. if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
  694. $this->error(__('Please select service type'));
  695. }
  696. if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
  697. if (!$amount) {
  698. $this->error(__('Please fill in the refund amount'));
  699. }
  700. }
  701. try {
  702. Db::startTrans();
  703. $orderRefund = new OrderRefund();
  704. $orderRefund->user_id = $this->auth->id;
  705. $orderRefund->order_id = $order_id;
  706. $orderRefund->receiving_status = $receivingStatus;
  707. $orderRefund->service_type = $serviceType;
  708. $orderRefund->reason_type = $reasonType;
  709. $orderRefund->amount = $amount;
  710. $orderRefund->refund_explain = $refundExplain;
  711. $orderRefund->save();
  712. $productIdArr = explode(',', $orderProductId);
  713. $refundProduct = [];
  714. foreach ($productIdArr as $orderProductId) {
  715. $tmp['order_product_id'] = $orderProductId;
  716. $tmp['order_id'] = $order_id;
  717. $tmp['user_id'] = $this->auth->id;
  718. $tmp['refund_id'] = $orderRefund['id'];
  719. $tmp['createtime'] = time();
  720. $refundProduct[] = $tmp;
  721. }
  722. (new OrderRefundProduct)->insertAll($refundProduct);
  723. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  724. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  725. $order->save();
  726. Db::commit();
  727. } catch (Exception $e) {
  728. Db::rollback();
  729. $this->error($e->getMessage());
  730. }
  731. $this->success('已申请', 1);
  732. }
  733. /**
  734. * @ApiTitle (售后发货)
  735. * @ApiSummary (售后发货)
  736. * @ApiMethod (POST)
  737. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  738. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  739. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  740. * @ApiParams (name="express_number", type="string",required=true, description="寄货物流单号")
  741. * @ApiReturn ({"code":1,"msg":"","data":1})
  742. *
  743. */
  744. public function refundDelivery()
  745. {
  746. $orderId = $this->request->post('order_id');
  747. $expressNumber = $this->request->post('express_number');
  748. if (!$expressNumber) {
  749. $this->error(__('Please fill in the express number'));
  750. }
  751. $orderId = Hashids::decodeHex($orderId);
  752. $orderModel = new \addons\unishop\model\Order();
  753. $order = $orderModel
  754. ->where(['id' => $orderId, 'user_id' => $this->auth->id])
  755. ->with(['refund'])->find();
  756. if (!$order || !$order->refund) {
  757. $this->error(__('Order not exist'));
  758. }
  759. try {
  760. Db::startTrans();
  761. $order->refund->express_number = $expressNumber;
  762. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  763. if ($order->refund->save() && $order->save()) {
  764. Db::commit();
  765. } else {
  766. throw new Exception(__('Operation failed'));
  767. }
  768. } catch (Exception $e) {
  769. Db::rollback();
  770. $this->success($e->getMessage());
  771. }
  772. $this->success('', 1);
  773. }
  774. /**
  775. * @ApiTitle (快递查询)
  776. * @ApiSummary (快递查询)
  777. * @ApiMethod (POST)
  778. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  779. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  780. * @ApiParams (name="express", type="string",required=true, description="寄货物流公司")
  781. * @ApiParams (name="expresssn", type="string",required=true, description="寄货物流单号")
  782. * @ApiReturn ({"code":1,"msg":"","data":1})
  783. *
  784. */
  785. public function express() {
  786. $params = $this->request->post();
  787. if (!class_exists(\addons\expressquery\library\Expressquery::class)) {
  788. $this->error('请先安装插件《物流信息接口》', []);
  789. }
  790. $info = get_addon_info('expressquery');
  791. if ($info['state'] == 0) {
  792. $this->error('请开启插件《物流信息接口》', []);
  793. }
  794. $expModle = new \addons\expressquery\library\Expressquery();
  795. $list = $expModle->getExpressList($params['express'], $params['expresssn']);
  796. if ($list['code'] == 0) {
  797. $this->error($list['msg']);
  798. }
  799. $expressInfo = Db::name('expressquery')->where(['express' => $params['express']])->find();
  800. $this->success('', [
  801. 'message' => $list['data'] ?? [],
  802. 'company' => $expressInfo['name'] ?? '快递单号'
  803. ]);
  804. }
  805. }