Order.php 43 KB

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