Order.php 44 KB

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