Order.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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 = input('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 = input('spec', '');
  96. $productData[0] = $product->getDataOnCreateOrder($spec);
  97. } else {
  98. // 多个商品
  99. $cart = input('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. 'wallet' => Db::name('user_wallet')->where('user_id',$this->auth->id)->find(),
  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 = input();
  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 = input('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 = input('type', 0);
  266. $page = input('page', 1);
  267. $pagesize = input('pagesize', 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 = input('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 = input('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 = input('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. * @ApiReturn ({"code":1,"msg":"感谢评价","data":true})
  378. *
  379. */
  380. public function comment()
  381. {
  382. $rate = input('rate', 5);
  383. $anonymous = input('anonymous', 0);
  384. $comment = input('comment');
  385. $order_id = input('order_id', 0);
  386. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  387. $product_id = input('product_id');
  388. $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
  389. $orderProductModel = new \addons\unishop\model\OrderProduct();
  390. $orderProduct = $orderProductModel->where(['product_id' => $product_id, '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 = $product_id;
  405. $evaluate->rate = $rate;
  406. $evaluate->anonymous = $anonymous;
  407. $evaluate->comment = $comment;
  408. $evaluate->spec = $orderProduct->spec;
  409. $result = $evaluate->save();
  410. if ($result) {
  411. $order->have_commented = time();
  412. $order->save();
  413. }
  414. } catch (Exception $e) {
  415. $this->error($e->getMessage());
  416. }
  417. if ($result !== false) {
  418. $this->success(__('Thanks for the evaluation'));
  419. } else {
  420. $this->error(__('Evaluation failure'));
  421. }
  422. }
  423. /**
  424. * @ApiTitle (获取订单数量)
  425. * @ApiSummary (获取订单数量)
  426. * @ApiMethod (POST)
  427. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  428. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  429. * @ApiReturn ({"code":1,"msg":"","data":{}})
  430. *
  431. * @ApiReturnParams (name="unpaid", type="integer", description="未支付数量")
  432. * @ApiReturnParams (name="undelivered", type="integer", description="未发货数量")
  433. * @ApiReturnParams (name="unreceived", type="integer", description="未收货数量")
  434. * @ApiReturnParams (name="uncomment", type="integer", description="未评价数量")
  435. * @ApiReturnParams (name="refund", type="integer", description="正在售后的数量")
  436. *
  437. */
  438. public function count()
  439. {
  440. if (!$this->auth->isLogin()) {
  441. $this->error('');
  442. }
  443. $order = new \addons\unishop\model\Order();
  444. $list = $order
  445. ->where([
  446. 'user_id' => $this->auth->id,
  447. ])
  448. ->where('status', '<>', \addons\unishop\model\Order::STATUS_CANCEL)
  449. ->where(function ($query) {
  450. $query
  451. ->whereOr([
  452. 'have_paid' => \addons\unishop\model\Order::PAID_NO,
  453. 'have_delivered' => \addons\unishop\model\Order::DELIVERED_NO,
  454. 'have_received' => \addons\unishop\model\Order::RECEIVED_NO,
  455. 'have_commented' => \addons\unishop\model\Order::COMMENTED_NO
  456. ])
  457. ->whereOr('refund_status', '>', \addons\unishop\model\Order::REFUND_STATUS_NONE);
  458. })
  459. ->field('have_paid,have_delivered,have_received,have_commented,refund_status,had_refund')
  460. ->select();
  461. $data = [
  462. 'unpaid' => 0,
  463. 'undelivered' => 0,
  464. 'unreceived' => 0,
  465. 'uncomment' => 0,
  466. 'refund' => 0
  467. ];
  468. foreach ($list as $item) {
  469. switch (true) {
  470. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] > 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  471. $data['uncomment']++;
  472. break;
  473. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  474. $data['unreceived']++;
  475. break;
  476. case $item['have_paid'] > 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  477. $data['undelivered']++;
  478. break;
  479. case $item['have_paid'] == 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  480. $data['unpaid']++;
  481. break;
  482. case $item['refund_status'] > 0 && $item['had_refund'] == 0 && $item['refund_status'] != 3:
  483. $data['refund']++;
  484. break;
  485. }
  486. }
  487. $this->success('', $data);
  488. }
  489. /**
  490. * @ApiTitle (订单详情细节)
  491. * @ApiSummary (订单详情细节)
  492. * @ApiMethod (GET)
  493. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  494. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  495. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  496. * @ApiReturn ({"code":1,"msg":"","data":{}})
  497. *
  498. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号")
  499. * @ApiReturnParams (name="order_price", type="string", description="订单原价钱")
  500. * @ApiReturnParams (name="discount_price", type="string", description="优惠多少钱")
  501. * @ApiReturnParams (name="delivery_price", type="string", description="运费多少钱")
  502. * @ApiReturnParams (name="total_price", type="string", description="订单实价")
  503. * @ApiReturnParams (name="pay_type", type="integer", description="支付类型")
  504. * @ApiReturnParams (name="id", type="string", description="下单ip")
  505. * @ApiReturnParams (name="remark", type="string", description="订单备注")
  506. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  507. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  508. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  509. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  510. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  511. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  512. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  513. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  514. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  515. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  516. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  517. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  518. * @ApiReturnParams (name="products[].evaluate", type="integer", description="是否已评价")
  519. * @ApiReturnParams (name="products[].refund", type="integer", description="是否已退款")
  520. * @ApiReturnParams (name="express_number", type="integer", description="运单号")
  521. * @ApiReturnParams (name="pay_type_text", type="string", description="支付类型简述")
  522. * @ApiReturnParams (name="refund_status_text", type="string", description="退款状态简述")
  523. * @ApiReturnParams (name="delivery.username", type="string", description="收货人名称")
  524. * @ApiReturnParams (name="delivery.mobile", type="string", description="收货人电话")
  525. * @ApiReturnParams (name="delivery.address", type="string", description="收货人地址")
  526. *
  527. */
  528. public function detail()
  529. {
  530. $order_id = input('order_id', 0);
  531. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  532. try {
  533. $orderModel = new \addons\unishop\model\Order();
  534. $order = $orderModel
  535. ->with([
  536. 'products' => function ($query) {
  537. $query->field('id,order_id,image,number,price,spec,title,product_id');
  538. },
  539. 'extend' => function ($query) {
  540. $query->field('id,order_id,address_id,address_json,express_number,express_company');
  541. },
  542. 'evaluate' => function ($query) {
  543. $query->field('id,order_id,product_id');
  544. }
  545. ])
  546. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  547. if ($order) {
  548. $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
  549. // 快递单号
  550. $order['express_number'] = $order['extend']['express_number'];
  551. $order['express_company'] = '快递单号';
  552. $order['express'] = '';
  553. if (class_exists(\addons\expressquery\library\Expressquery::class)) {
  554. $expressInfo = Db::name('expressquery')->where(['express' => $order['extend']['express_company']])->find();
  555. $order['express_company'] = $expressInfo['name'] ?? '快递单号';
  556. $order['express'] = $expressInfo['express'] ?? '';
  557. }
  558. // 送货地址
  559. $address = json_decode($order['extend']['address_json'], true);
  560. $area = (new \addons\unishop\model\Area())
  561. ->whereIn('id', [$address['province_id'], $address['city_id'], $address['area_id']])
  562. ->column('name', 'id');
  563. $delivery['username'] = $address['name'];
  564. $delivery['mobile'] = $address['mobile'];
  565. $delivery['address'] = $area[$address['province_id']] . ' ' . $area[$address['city_id']] . ' ' . $area[$address['area_id']] . ' ' . $address['address'];
  566. $order['delivery'] = $delivery;
  567. // 是否已评论
  568. $evaluate = array_column($order['evaluate'], 'product_id');
  569. foreach ($order['products'] as &$product) {
  570. $product['image'] = Config::getImagesFullUrl($product['image']);
  571. if (in_array($product['id'], $evaluate)) {
  572. $product['evaluate'] = true;
  573. } else {
  574. $product['evaluate'] = false;
  575. }
  576. }
  577. unset($order['evaluate']);
  578. unset($order['extend']);
  579. }
  580. } catch (Exception $e) {
  581. $this->error($e->getMessage());
  582. }
  583. $this->success('', $order);
  584. }
  585. /**
  586. * @ApiTitle (申请售后信息)
  587. * @ApiSummary (申请售后信息)
  588. * @ApiMethod (POST)
  589. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  590. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  591. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  592. * @ApiReturn ({"code":1,"msg":"","data":{}})
  593. *
  594. * @ApiReturnParams (name="status", type="string", description="订单状态:-1=退货,0=取消订单,1=正常啊")
  595. * @ApiReturnParams (name="total_price", type="string", description="订单总价钱")
  596. * @ApiReturnParams (name="delivery_price", type="string", description="订单运费")
  597. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  598. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  599. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  600. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  601. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  602. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  603. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  604. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  605. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  606. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  607. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  608. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  609. * @ApiReturnParams (name="products[].choose", type="integer", description="是否选中")
  610. * @ApiReturnParams (name="refund_status_text", type="string", description="退款状态简述")
  611. * @ApiReturnParams (name="refund.id", type="string", description="退款信息id")
  612. * @ApiReturnParams (name="refund.amount", type="float",required=true, description="退款金额")
  613. * @ApiReturnParams (name="refund.service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  614. * @ApiReturnParams (name="refund.receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  615. * @ApiReturnParams (name="refund.reason_type", type="string",required=true, description="换货原因")
  616. * @ApiReturnParams (name="refund.refund_explain", type="string",required=true, description="退款说明")
  617. * @ApiReturnParams (name="refund.express_number", type="string",required=true, description="寄货物流单号")
  618. *
  619. */
  620. public function refundInfo()
  621. {
  622. $order_id = input('order_id');
  623. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  624. $orderModel = new \addons\unishop\model\Order();
  625. $order = $orderModel
  626. ->with([
  627. 'products' => function ($query) {
  628. $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
  629. },
  630. 'refund',
  631. 'refundProducts'
  632. ])
  633. ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
  634. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  635. if (!$order) {
  636. $this->error(__('Order not exist'));
  637. }
  638. $order = $order->append(['refund_status_text'])->toArray();
  639. foreach ($order['products'] as &$product) {
  640. $product['image'] = Config::getImagesFullUrl($product['image']);
  641. $product['choose'] = 0;
  642. // 如果是已提交退货的全选
  643. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
  644. foreach ($order['refund_products'] as $refundProduct) {
  645. if ($product['order_product_id'] == $refundProduct['order_product_id']) {
  646. $product['choose'] = 1;
  647. }
  648. }
  649. }
  650. }
  651. unset($order['refund_products']);
  652. $this->success('', $order);
  653. }
  654. /**
  655. * @ApiTitle (提交申请售后)
  656. * @ApiSummary (提交申请售后)
  657. * @ApiMethod (POST)
  658. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  659. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  660. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  661. * @ApiParams (name="amount", type="float",required=true, description="退款金额")
  662. * @ApiParams (name="service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  663. * @ApiParams (name="receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  664. * @ApiParams (name="reason_type", type="string",required=true, description="换货原因")
  665. * @ApiParams (name="refund_explain", type="string",required=true, description="退款说明")
  666. * @ApiParams (name="order_product_id", type="integer",required=true, description="订单的商品的id")
  667. * @ApiReturn ({"code":1,"msg":"提交","data":1})
  668. *
  669. */
  670. public function refund()
  671. {
  672. $order_id = input('order_id');
  673. $order_id = Hashids::decodeHex($order_id);
  674. $orderModel = new \addons\unishop\model\Order();
  675. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  676. if (!$order) {
  677. $this->error(__('Order not exist'));
  678. }
  679. if ($order['have_paid'] == 0) {
  680. $this->error(__('订单未支付,可直接取消,无需申请售后'));
  681. }
  682. $amount = input('amount', 0);
  683. $serviceType = input('service_type');
  684. $receivingStatus = input('receiving_status');
  685. $reasonType = input('reason_type');
  686. $refundExplain = input('refund_explain');
  687. $orderProductId = input('order_product_id');
  688. if (!$orderProductId) {
  689. $this->error(__('Please select goods'));
  690. }
  691. if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
  692. $this->error(__('Please select goods status'));
  693. }
  694. if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
  695. $this->error(__('Please select service type'));
  696. }
  697. if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
  698. if (!$amount) {
  699. $this->error(__('Please fill in the refund amount'));
  700. }
  701. }
  702. try {
  703. Db::startTrans();
  704. $orderRefund = new OrderRefund();
  705. $orderRefund->user_id = $this->auth->id;
  706. $orderRefund->order_id = $order_id;
  707. $orderRefund->receiving_status = $receivingStatus;
  708. $orderRefund->service_type = $serviceType;
  709. $orderRefund->reason_type = $reasonType;
  710. $orderRefund->amount = $amount;
  711. $orderRefund->refund_explain = $refundExplain;
  712. $orderRefund->save();
  713. $productIdArr = explode(',', $orderProductId);
  714. $refundProduct = [];
  715. foreach ($productIdArr as $orderProductId) {
  716. $tmp['order_product_id'] = $orderProductId;
  717. $tmp['order_id'] = $order_id;
  718. $tmp['user_id'] = $this->auth->id;
  719. $tmp['refund_id'] = $orderRefund['id'];
  720. $tmp['createtime'] = time();
  721. $refundProduct[] = $tmp;
  722. }
  723. (new OrderRefundProduct)->insertAll($refundProduct);
  724. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  725. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  726. $order->save();
  727. Db::commit();
  728. } catch (Exception $e) {
  729. Db::rollback();
  730. $this->error($e->getMessage());
  731. }
  732. $this->success('已申请', 1);
  733. }
  734. /**
  735. * @ApiTitle (售后发货)
  736. * @ApiSummary (售后发货)
  737. * @ApiMethod (POST)
  738. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  739. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  740. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  741. * @ApiParams (name="express_number", type="string",required=true, description="寄货物流单号")
  742. * @ApiReturn ({"code":1,"msg":"","data":1})
  743. *
  744. */
  745. public function refundDelivery()
  746. {
  747. $orderId = input('order_id');
  748. $expressNumber = input('express_number');
  749. if (!$expressNumber) {
  750. $this->error(__('Please fill in the express number'));
  751. }
  752. $orderId = Hashids::decodeHex($orderId);
  753. $orderModel = new \addons\unishop\model\Order();
  754. $order = $orderModel
  755. ->where(['id' => $orderId, 'user_id' => $this->auth->id])
  756. ->with(['refund'])->find();
  757. if (!$order || !$order->refund) {
  758. $this->error(__('Order not exist'));
  759. }
  760. try {
  761. Db::startTrans();
  762. $order->refund->express_number = $expressNumber;
  763. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  764. if ($order->refund->save() && $order->save()) {
  765. Db::commit();
  766. } else {
  767. throw new Exception(__('Operation failed'));
  768. }
  769. } catch (Exception $e) {
  770. Db::rollback();
  771. $this->success($e->getMessage());
  772. }
  773. $this->success('', 1);
  774. }
  775. /**
  776. * @ApiTitle (快递查询)
  777. * @ApiSummary (快递查询)
  778. * @ApiMethod (POST)
  779. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  780. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  781. * @ApiParams (name="express", type="string",required=true, description="寄货物流公司")
  782. * @ApiParams (name="expresssn", type="string",required=true, description="寄货物流单号")
  783. * @ApiReturn ({"code":1,"msg":"","data":1})
  784. *
  785. */
  786. public function express() {
  787. $params = input();
  788. if (!class_exists(\addons\expressquery\library\Expressquery::class)) {
  789. $this->error('请先安装插件《物流信息接口》', []);
  790. }
  791. $info = get_addon_info('expressquery');
  792. if ($info['state'] == 0) {
  793. $this->error('请开启插件《物流信息接口》', []);
  794. }
  795. $expModle = new \addons\expressquery\library\Expressquery();
  796. $list = $expModle->getExpressList($params['express'], $params['expresssn']);
  797. if ($list['code'] == 0) {
  798. $this->error($list['msg']);
  799. }
  800. $expressInfo = Db::name('expressquery')->where(['express' => $params['express']])->find();
  801. $this->success('', [
  802. 'message' => $list['data'] ?? [],
  803. 'company' => $expressInfo['name'] ?? '快递单号'
  804. ]);
  805. }
  806. }