Order.php 43 KB

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