Order.php 45 KB

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