Order.php 44 KB

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