Order.php 43 KB

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