Order.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. use addons\epay\library\Service;
  24. /**
  25. * 订单
  26. */
  27. class Order extends Base
  28. {
  29. /**
  30. * 允许频繁访问的接口
  31. * @var array
  32. */
  33. protected $frequently = ['getorders'];
  34. protected $noNeedLogin = ['count'];
  35. /**
  36. * @ApiTitle (创建订单)
  37. * @ApiSummary (创建订单)
  38. * @ApiMethod (POST)
  39. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  40. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  41. * @ApiParams (name="id", type=string, required=true, description="商品id")
  42. * @ApiReturn ({"code":1,"msg":"","data":{}})
  43. *
  44. * @ApiReturnParams (name="order_id", type="string", description="订单编号")
  45. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号(支付用)")
  46. *
  47. * @ApiReturnParams (name="product.title", type="string", description="商品名称")
  48. * @ApiReturnParams (name="product.image", type="string", description="商品图片")
  49. * @ApiReturnParams (name="product.sales", type="integer", description="销量")
  50. * @ApiReturnParams (name="product.sales_price", type="string", description="销售价钱")
  51. * @ApiReturnParams (name="product.market_price", type="string", description="市场价钱")
  52. * @ApiReturnParams (name="product.id", type="string", description="商品id")
  53. * @ApiReturnParams (name="product.stock", type="integer", description="库存")
  54. * @ApiReturnParams (name="product.spec", type="integer", description="选中的规格")
  55. * @ApiReturnParams (name="product.number", type="integer", description="购买数量")
  56. *
  57. * @ApiReturnParams (name="address.id", type="integer", description="地址id")
  58. * @ApiReturnParams (name="address.name", type="string", description="收货人名称")
  59. * @ApiReturnParams (name="address.mobile", type="string", description="收货人电话")
  60. * @ApiReturnParams (name="address.address", type="string", description="收货人地址")
  61. * @ApiReturnParams (name="address.province_id", type="integer", description="省份id")
  62. * @ApiReturnParams (name="address.city_id", type="integer", description="城市id")
  63. * @ApiReturnParams (name="address.area_id", type="integer", description="地区id")
  64. * @ApiReturnParams (name="address.is_default", type="integer", description="是否默认")
  65. * @ApiReturnParams (name="address.province.name", type="integer", description="省份")
  66. * @ApiReturnParams (name="address.city.name", type="integer", description="城市")
  67. * @ApiReturnParams (name="address.area.name", type="integer", description="地区")
  68. *
  69. * @ApiReturnParams (name="coupon.id", type="integer", description="优惠券id")
  70. * @ApiReturnParams (name="coupon.title", type="string", description="优惠券名称")
  71. * @ApiReturnParams (name="coupon.least", type="integer", description="至少购买金额")
  72. * @ApiReturnParams (name="coupon.value", type="integer", description="满减金额")
  73. * @ApiReturnParams (name="coupon.starttime_text", type="integer", description="开始使用时间")
  74. * @ApiReturnParams (name="coupon.endtime_text", type="integer", description="到期使用时间")
  75. *
  76. * @ApiReturnParams (name="delivery.id", type="integer", description="货运id")
  77. * @ApiReturnParams (name="delivery.name", type="string", description="货运名称")
  78. * @ApiReturnParams (name="delivery.type", type="string", description="收费类型")
  79. * @ApiReturnParams (name="delivery.min", type="integer", description="至少购买量")
  80. * @ApiReturnParams (name="delivery.first", type="integer", description="首重数量")
  81. * @ApiReturnParams (name="delivery.first_fee", type="string", description="首重价钱")
  82. * @ApiReturnParams (name="delivery.additional", type="integer", description="需重数量")
  83. * @ApiReturnParams (name="delivery.additional_fee", type="string", description="需重价钱") *
  84. *
  85. */
  86. public function create()
  87. {
  88. $productId = input('id', 0);
  89. try {
  90. $user_id = $this->auth->id;
  91. // 单个商品
  92. /*if ($productId) {*/
  93. $number = input('number',1,'intval');
  94. $number = intval($number);
  95. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  96. $product = (new Product)->where(['id' => $productId, 'switch' => Product::SWITCH_ON, 'deletetime' => null])->find();
  97. /** 产品基础数据 **/
  98. $spec = input('spec', '');
  99. $productData[0] = $product->getDataOnCreateOrder($spec,$number);
  100. /* } else {
  101. // 多个商品
  102. $cart = input('cart');
  103. $carts = (new \addons\unishop\model\Cart)
  104. ->whereIn('id', $cart)
  105. ->with(['product'])
  106. ->order(['id' => 'desc'])
  107. ->select();
  108. foreach ($carts as $cart) {
  109. if ($cart->product instanceof Product) {
  110. $productData[] = $cart->product->getDataOnCreateOrder($cart->spec ? $cart->spec : '', $cart->number);
  111. }
  112. }
  113. }*/
  114. if (empty($productData) || !$productData) {
  115. $this->error(__('Product not exist'));
  116. }
  117. /** 默认地址 **/
  118. /*$address = (new AddressModel)->where(['user_id' => $user_id, 'is_default' => AddressModel::IS_DEFAULT_YES])->find();
  119. if ($address) {
  120. $area = (new Area)->whereIn('id', [$address->province_id, $address->city_id, $address->area_id])->column('name', 'id');
  121. $address = $address->toArray();
  122. $address['province']['name'] = $area[$address['province_id']];
  123. $address['city']['name'] = $area[$address['city_id']];
  124. $address['area']['name'] = $area[$address['area_id']];
  125. }*/
  126. /** 可用优惠券 **/
  127. /*$coupon = CouponModel::all(function ($query) {
  128. $time = time();
  129. $query
  130. ->where(['switch' => CouponModel::SWITCH_ON])
  131. ->where('starttime', '<', $time)
  132. ->where('endtime', '>', $time);
  133. });
  134. if ($coupon) {
  135. $coupon = collection($coupon)->toArray();
  136. }*/
  137. /** 运费数据 **/
  138. // $cityId = $address['city_id'] ? $address['city_id'] : 0;
  139. // $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
  140. $order_price = '0';
  141. foreach ($productData as &$product) {
  142. $product['image'] = Config::getImagesFullUrl($product['image']);
  143. $product['sales_price'] = round($product['sales_price'], 2);
  144. $product['market_price'] = round($product['market_price'], 2);
  145. $order_price = bcadd($order_price,$product['total_price'],2);
  146. }
  147. $coupon = Db::name('unishop_coupon_user')->alias('cu')
  148. ->field(['cu.id','c.title','c.least','c.value','c.starttime','c.endtime'])
  149. ->join('unishop_coupon c','cu.coupon_id = c.id','LEFT')
  150. ->where('cu.user_id',$this->auth->id)
  151. ->where('cu.status',0)
  152. ->where('c.deletetime',NULL)
  153. ->where('c.switch',1)
  154. ->where('c.starttime','<',time())
  155. ->where('c.endtime','>',time())
  156. ->where('c.least','>=',$order_price)
  157. ->select();
  158. $this->success('', [
  159. 'product' => $productData,
  160. // 'address' => $address,
  161. 'coupon' => $coupon,
  162. // 'delivery' => $delivery['list']
  163. 'order_price' => $order_price,
  164. 'total_price' => $order_price
  165. ]);
  166. } catch (Exception $e) {
  167. $this->error($e->getMessage(), false);
  168. }
  169. }
  170. /**
  171. * @ApiTitle (提交订单)
  172. * @ApiSummary (提交订单)
  173. * @ApiMethod (POST)
  174. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  175. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  176. * @ApiParams (name="coupon_id", type=string, required=true, description="优惠券id")
  177. * @ApiParams (name="product_id", type=string, required=true, description="商品id,多个用法(product_id,product_id,product_id)")
  178. * @ApiParams (name="number", type=string, required=true, description="商品数量,多个用法(number,number,number)")
  179. * @ApiParams (name="spec", type=string, required=true, description="规格,多个用法(spec|spec2,spec|spec2,spec|spec2)")
  180. * @ApiParams (name="city_id", type=integer, required=true, description="城市id")
  181. * @ApiParams (name="address_id", type=string, required=true, description="收货地址id")
  182. * @ApiParams (name="delivery_id", type=integer, required=true, description="运费模板id")
  183. * @ApiParams (name="remark", type=string, required=true, description="备注")
  184. * @ApiReturn ({"code":1,"msg":"","data":{}})
  185. *
  186. * @ApiReturnParams (name="order_id", type="string", description="订单编号")
  187. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号(支付用)")
  188. *
  189. */
  190. public function submit()
  191. {
  192. $data = input();
  193. try {
  194. $validate = Loader::validate('\\addons\\unishop\\validate\\Order');
  195. if (!$validate->check($data, [], 'submit')) {
  196. throw new Exception($validate->getError());
  197. }
  198. Db::startTrans();
  199. // 判断创建订单的条件
  200. if (empty(Hook::get('create_order_before'))) {
  201. Hook::add('create_order_before', 'addons\\unishop\\behavior\\Order');
  202. }
  203. // 减少商品库存,增加"已下单未支付数量"
  204. if (empty(Hook::get('create_order_after'))) {
  205. Hook::add('create_order_after', 'addons\\unishop\\behavior\\Order');
  206. }
  207. $orderModel = new \addons\unishop\model\Order();
  208. $result = $orderModel->createOrder($this->auth->id, $data);
  209. Db::commit();
  210. } catch (Exception $e) {
  211. Db::rollback();
  212. $this->error($e->getMessage(), false);
  213. }
  214. $this->success('', $result);
  215. }
  216. /**
  217. * @ApiTitle (获取运费模板)
  218. * @ApiSummary (获取运费模板)
  219. * @ApiMethod (POST)
  220. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  221. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  222. * @ApiParams (name="city_id", type=integer, required=true, description="城市id")
  223. * @ApiReturn ({"code":1,"msg":"","data":[]})
  224. *
  225. * @ApiReturnParams (name="id", type="integer", description="模板id")
  226. * @ApiReturnParams (name="name", type="string", description="模板名称")
  227. * @ApiReturnParams (name="type", type="string", description="模板类型")
  228. * @ApiReturnParams (name="min", type="integer", description="至少购买量")
  229. * @ApiReturnParams (name="first", type="integer", description="首重数量")
  230. * @ApiReturnParams (name="first_fee", type="string", description="首重价钱")
  231. * @ApiReturnParams (name="additional", type="integer", description="需重数量")
  232. * @ApiReturnParams (name="additional_fee", type="string", description="需重价钱") *
  233. *
  234. */
  235. public function getDelivery()
  236. {
  237. $cityId = input('city_id', 0);
  238. $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
  239. $this->success('', $delivery['list']);
  240. }
  241. /**
  242. * @ApiTitle (获取订单列表)
  243. * @ApiSummary (获取订单列表信息)
  244. * @ApiMethod (POST)
  245. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  246. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  247. * @ApiParams (name="type", type=integer, required=true, description="类型:0=全部,1=待付款,2=待发货,3=待收货,4=待评价,5=售后")
  248. * @ApiParams (name="page", type=integer, required=true, description="第几页")
  249. * @ApiParams (name="pagesize", type=integer, required=true, description="每页数量")
  250. * @ApiReturn ({"code":1,"msg":"","data":[]})
  251. *
  252. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号")
  253. * @ApiReturnParams (name="order_price", type="string", description="订单原价钱")
  254. * @ApiReturnParams (name="discount_price", type="string", description="优惠多少钱")
  255. * @ApiReturnParams (name="delivery_price", type="string", description="运费多少钱")
  256. * @ApiReturnParams (name="total_price", type="string", description="订单实价")
  257. * @ApiReturnParams (name="pay_type", type="integer", description="支付类型")
  258. * @ApiReturnParams (name="id", type="string", description="下单ip")
  259. * @ApiReturnParams (name="remark", type="string", description="订单备注")
  260. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  261. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  262. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  263. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  264. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  265. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  266. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  267. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  268. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  269. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  270. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  271. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  272. * @ApiReturnParams (name="products[].evaluate", type="integer", description="是否已评价")
  273. * @ApiReturnParams (name="products[].refund", type="integer", description="是否已退款")
  274. * @ApiReturnParams (name="extend.express_number", type="integer", description="运单号")
  275. * @ApiReturnParams (name="order_id", type="string", description="订单id")
  276. * @ApiReturnParams (name="state", type="string", description="状态类型:0=全部,1=待付款,2=待发货,3=待收货,4=待评价,5=售后")
  277. * @ApiReturnParams (name="refund_status_text", type="string", description="已退款")
  278. */
  279. public function getOrders()
  280. {
  281. // 0=全部,1=待付款,3=待核销,4=已完成,20=已取消
  282. $type = input('type', 0);
  283. $page = input('page', 1);
  284. $pagesize = input('pagesize', 10);
  285. try {
  286. $orderModel = new \addons\unishop\model\Order();
  287. $result = $orderModel->getOrdersByType($this->auth->id, $type, $page, $pagesize);
  288. } catch (Exception $e) {
  289. $this->error($e->getMessage());
  290. }
  291. $this->success('', $result);
  292. }
  293. /**
  294. * @ApiTitle (取消订单)
  295. * @ApiSummary (未支付的订单才叫取消,已支付的叫退货)
  296. * @ApiMethod (GET)
  297. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  298. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  299. * @ApiParams (name="order_id", type="string", description="订单id")
  300. * @ApiReturn ({"code":1,"msg":"取消成功","data":true})
  301. *
  302. */
  303. public function cancel()
  304. {
  305. $order_id = input('order_id', 0);
  306. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  307. $orderModel = new \addons\unishop\model\Order();
  308. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  309. if (!$order) {
  310. $this->error(__('Order not exist'));
  311. }
  312. switch ($order['status']) {
  313. case \addons\unishop\model\Order::STATUS_REFUND:
  314. $this->error('此订单已退款,无法取消');
  315. break;
  316. case \addons\unishop\model\Order::STATUS_CANCEL:
  317. $this->error('此订单已取消, 无需再取消');
  318. break;
  319. }
  320. if ($order['have_paid'] != \addons\unishop\model\Order::PAID_NO) {
  321. $this->error('此订单已支付,无法取消');
  322. }
  323. if ($order['have_received'] != \addons\unishop\model\Order::RECEIVED_NO) {
  324. $this->error('此订单已核销,无法取消');
  325. }
  326. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
  327. $order->status = \addons\unishop\model\Order::STATUS_CANCEL;
  328. $order->canceltime = time();
  329. $order->save();
  330. $this->success('取消成功', true);
  331. }
  332. }
  333. //已支付取消,申请退订
  334. public function payed_cancel()
  335. {
  336. $order_id = input('order_id', 0);
  337. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  338. $orderModel = new \addons\unishop\model\Order();
  339. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  340. if (!$order) {
  341. $this->error(__('Order not exist'));
  342. }
  343. switch ($order['status']) {
  344. case \addons\unishop\model\Order::STATUS_REFUND:
  345. $this->error('此订单已退款,无法取消');
  346. break;
  347. case \addons\unishop\model\Order::STATUS_CANCEL:
  348. $this->error('此订单已取消, 无需再取消');
  349. break;
  350. }
  351. if ($order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
  352. $this->error('未支付订单,直接取消即可');
  353. }
  354. if ($order['have_received'] != \addons\unishop\model\Order::RECEIVED_NO) {
  355. $this->error('此订单已核销,无法退订');
  356. }
  357. Db::startTrans();
  358. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_received'] == \addons\unishop\model\Order::RECEIVED_NO) {
  359. $refund_amount = $order['total_price'];
  360. $refund_status = 3;
  361. // 退款
  362. $params = [
  363. 'refund_status' => $refund_status,
  364. 'updatetime' => time(),
  365. 'refund_amount' => $refund_amount,
  366. 'status' => \addons\unishop\model\Order::STATUS_CANCEL,
  367. 'canceltime' => time(),
  368. ];
  369. if($refund_status == 3) {
  370. $params['had_refund'] = time();
  371. }
  372. $result = Db::name('unishop_order')->where(['id' => $order_id])->update($params);
  373. if (!$result) {
  374. Db::rollback();
  375. $this->error('取消失败');
  376. }
  377. //同意并执行退款
  378. if($refund_status == 3 && $refund_amount > 0){
  379. $order = Db::name('unishop_order')->where('id',$order_id)->find();
  380. if($order['pay_type'] == 3){
  381. $refund_result = $this->old_refund($order,$refund_amount);
  382. if($refund_result !== true){
  383. Db::rollback();
  384. $this->error($refund_result);
  385. }
  386. }
  387. }
  388. }
  389. Db::commit();
  390. $this->success('退订成功', true);
  391. }
  392. // 退款
  393. public function old_refund($order, $refund_price)
  394. {
  395. return true;
  396. $table = 'unishop_order';
  397. $remark = '订单退款';
  398. if($order['pay_type'] == 3){
  399. $order['pay_type'] = 'wechat';
  400. }
  401. if($order['pay_type'] == 4){
  402. $order['pay_type'] = 'alipay';
  403. }
  404. // 生成退款单
  405. $refund_data = [
  406. 'order_id' => $order['id'],
  407. 'out_refund_no'=> createUniqueNo('R'),
  408. 'pay_fee' => $order['total_price'],
  409. 'refund_price' => $refund_price,
  410. 'pay_type' => $order['pay_type'],
  411. 'status' => 0,
  412. 'createtime' => time(),
  413. 'updatetime' => time(),
  414. 'table' => $table,
  415. 'table_id' => $order['id'],
  416. ];
  417. $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
  418. if(!$refund_log_id){
  419. return '退款失败';
  420. }
  421. if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
  422. // 微信|支付宝退款
  423. // 退款数据
  424. $order_data = [
  425. 'out_trade_no' => $order['pay_out_trade_no']
  426. ];
  427. if ($order['pay_type'] == 'wechat') {
  428. $total_fee = $order['total_price'] * 100;
  429. $refund_fee = $refund_price * 100;
  430. $order_data = array_merge($order_data, [
  431. 'out_refund_no' => $refund_data['out_refund_no'],
  432. 'total_fee' => $total_fee,
  433. 'refund_fee' => $refund_fee,
  434. 'refund_desc' => $remark,
  435. ]);
  436. } else {
  437. $order_data = array_merge($order_data, [
  438. 'out_request_no' => $refund_data['out_refund_no'],
  439. 'refund_amount' => $refund_price,
  440. ]);
  441. }
  442. //
  443. if ($order['pay_type'] == 'wechat') {
  444. $wxpay = new \app\common\library\Wxpay;
  445. $result = $wxpay->WxPayRefund($order_data);
  446. // 微信通知回调 pay->notifyr
  447. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  448. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  449. return true;
  450. } else {
  451. return $result['return_msg'];
  452. }
  453. } else {
  454. $result = Service::submitRefund($order['total_price'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');
  455. if($result['code'] == '10000'){
  456. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  457. return true;
  458. }else{
  459. return $result['msg'];
  460. }
  461. /* return 'alipay wrong way';
  462. $alipay = new \app\common\library\AliPay;
  463. $result = $alipay->AliPayRefund($order_data);
  464. // 支付宝通知回调 pay->notifyx
  465. return $result;*/
  466. /*if ($result['code'] == "10000") {
  467. return true;
  468. } else {
  469. throw new \Exception($result['msg']);
  470. }*/
  471. }
  472. // { // 微信返回结果
  473. // "return_code":"SUCCESS",
  474. // "return_msg":"OK",
  475. // "appid":"wx39cd0799d4567dd0",
  476. // "mch_id":"1481069012",
  477. // "nonce_str":"huW9eIAb5BDPn0Ma",
  478. // "sign":"250316740B263FE53F5DFF50AF5A8FA1",
  479. // "result_code":"SUCCESS",
  480. // "transaction_id":"4200000497202004072822298902",
  481. // "out_trade_no":"202010300857029180027000",
  482. // "out_refund_no":"1586241595",
  483. // "refund_id":"50300603862020040700031444448",
  484. // "refund_channel":[],
  485. // "refund_fee":"1",
  486. // "coupon_refund_fee":"0",
  487. // "total_fee":"1",
  488. // "cash_fee":"1",
  489. // "coupon_refund_count":"0",
  490. // "cash_refund_fee":"1
  491. // }
  492. // { // 支付宝返回结果
  493. // "code": "10000",
  494. // "msg": "Success",
  495. // "buyer_logon_id": "157***@163.com",
  496. // "buyer_user_id": "2088902485164146",
  497. // "fund_change": "Y",
  498. // "gmt_refund_pay": "2020-08-15 16:11:45",
  499. // "out_trade_no": "202002460317545607015300",
  500. // "refund_fee": "0.01",
  501. // "send_back_fee": "0.00",
  502. // "trade_no": "2020081522001464141438570535"
  503. // }
  504. }
  505. return true;
  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", description="订单id")
  514. * @ApiReturn ({"code":1,"msg":"删除成功","data":true})
  515. *
  516. */
  517. public function delete()
  518. {
  519. $order_id = input('order_id', 0);
  520. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  521. $orderModel = new \addons\unishop\model\Order();
  522. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  523. if (!$order) {
  524. $this->error(__('Order not exist'));
  525. }
  526. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL) {
  527. $this->error('只能删除已取消或已退货的订单');
  528. }
  529. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND && $order['refund_status'] == \addons\unishop\model\Order::REFUND_STATUS_APPLY) {
  530. $this->error('订单退款中,不可删除订单');
  531. }
  532. $order->delete();
  533. $this->success('删除成功', true);
  534. }
  535. /**
  536. * @ApiTitle (确认收货)
  537. * @ApiSummary (确认收货)
  538. * @ApiMethod (GET)
  539. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  540. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  541. * @ApiParams (name="order_id", type="string", description="订单id")
  542. * @ApiReturn ({"code":1,"msg":"已确认收货","data":true})
  543. *
  544. */
  545. public function received()
  546. {
  547. $order_id = input('order_id', 0);
  548. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  549. $orderModel = new \addons\unishop\model\Order();
  550. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  551. if (!$order) {
  552. $this->error(__('Order not exist'));
  553. }
  554. if ($order->have_delivered == 0) {
  555. $this->error('未发货,不能确认收货');
  556. }
  557. $order->have_received = time();
  558. $order->save();
  559. $this->success('已确认收货', true);
  560. }
  561. /**
  562. * @ApiTitle (发表评论/评价)
  563. * @ApiSummary (发表评论/评价)
  564. * @ApiMethod (POST)
  565. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  566. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  567. * @ApiParams (name="order_id", type="string", description="订单id")
  568. * @ApiParams (name="rate", type="integer", description="分数/星星")
  569. * @ApiParams (name="anonymous", type="integer", description="是否匿名")
  570. * @ApiParams (name="comment", type="string", description="评价内容")
  571. * @ApiParams (name="product_id", type="string", description="商品id")
  572. * @ApiReturn ({"code":1,"msg":"感谢评价","data":true})
  573. *
  574. */
  575. public function comment()
  576. {
  577. $rate = input('rate', 5);
  578. $anonymous = input('anonymous', 0);
  579. $comment = input('comment');
  580. $order_id = input('order_id', 0);
  581. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  582. $product_id = input('product_id');
  583. $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
  584. $orderProductModel = new \addons\unishop\model\OrderProduct();
  585. $orderProduct = $orderProductModel->where(['product_id' => $product_id, 'order_id' => $order_id, 'user_id' => $this->auth->id])->find();
  586. $orderModel = new \addons\unishop\model\Order();
  587. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  588. if (!$orderProduct || !$order) {
  589. $this->error(__('Order not exist'));
  590. }
  591. if ($order->have_received == $orderModel::RECEIVED_NO) {
  592. $this->error(__('未收货,不可评价'));
  593. }
  594. $result = false;
  595. try {
  596. $evaluate = new Evaluate();
  597. $evaluate->user_id = $this->auth->id;
  598. $evaluate->order_id = $order_id;
  599. $evaluate->product_id = $product_id;
  600. $evaluate->rate = $rate;
  601. $evaluate->anonymous = $anonymous;
  602. $evaluate->comment = $comment;
  603. $evaluate->spec = $orderProduct->spec;
  604. $result = $evaluate->save();
  605. if ($result) {
  606. $order->have_commented = time();
  607. $order->save();
  608. }
  609. } catch (Exception $e) {
  610. $this->error($e->getMessage());
  611. }
  612. if ($result !== false) {
  613. $this->success(__('Thanks for the evaluation'));
  614. } else {
  615. $this->error(__('Evaluation failure'));
  616. }
  617. }
  618. /**
  619. * @ApiTitle (获取订单数量)
  620. * @ApiSummary (获取订单数量)
  621. * @ApiMethod (POST)
  622. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  623. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  624. * @ApiReturn ({"code":1,"msg":"","data":{}})
  625. *
  626. * @ApiReturnParams (name="unpaid", type="integer", description="未支付数量")
  627. * @ApiReturnParams (name="undelivered", type="integer", description="未发货数量")
  628. * @ApiReturnParams (name="unreceived", type="integer", description="未收货数量")
  629. * @ApiReturnParams (name="uncomment", type="integer", description="未评价数量")
  630. * @ApiReturnParams (name="refund", type="integer", description="正在售后的数量")
  631. *
  632. */
  633. public function count()
  634. {
  635. if (!$this->auth->isLogin()) {
  636. $this->error('');
  637. }
  638. $order = new \addons\unishop\model\Order();
  639. $list = $order
  640. ->where([
  641. 'user_id' => $this->auth->id,
  642. ])
  643. ->where('status', '<>', \addons\unishop\model\Order::STATUS_CANCEL)
  644. ->where(function ($query) {
  645. $query
  646. ->whereOr([
  647. 'have_paid' => \addons\unishop\model\Order::PAID_NO,
  648. 'have_delivered' => \addons\unishop\model\Order::DELIVERED_NO,
  649. 'have_received' => \addons\unishop\model\Order::RECEIVED_NO,
  650. 'have_commented' => \addons\unishop\model\Order::COMMENTED_NO
  651. ])
  652. ->whereOr('refund_status', '>', \addons\unishop\model\Order::REFUND_STATUS_NONE);
  653. })
  654. ->field('have_paid,have_delivered,have_received,have_commented,refund_status,had_refund')
  655. ->select();
  656. $data = [
  657. 'unpaid' => 0,
  658. 'undelivered' => 0,
  659. 'unreceived' => 0,
  660. 'uncomment' => 0,
  661. 'refund' => 0
  662. ];
  663. foreach ($list as $item) {
  664. switch (true) {
  665. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] > 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  666. $data['uncomment']++;
  667. break;
  668. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  669. $data['unreceived']++;
  670. break;
  671. case $item['have_paid'] > 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  672. $data['undelivered']++;
  673. break;
  674. case $item['have_paid'] == 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  675. $data['unpaid']++;
  676. break;
  677. case $item['refund_status'] > 0 && $item['had_refund'] == 0 && $item['refund_status'] != 3:
  678. $data['refund']++;
  679. break;
  680. }
  681. }
  682. $this->success('', $data);
  683. }
  684. /**
  685. * @ApiTitle (订单详情细节)
  686. * @ApiSummary (订单详情细节)
  687. * @ApiMethod (GET)
  688. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  689. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  690. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  691. * @ApiReturn ({"code":1,"msg":"","data":{}})
  692. *
  693. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号")
  694. * @ApiReturnParams (name="order_price", type="string", description="订单原价钱")
  695. * @ApiReturnParams (name="discount_price", type="string", description="优惠多少钱")
  696. * @ApiReturnParams (name="delivery_price", type="string", description="运费多少钱")
  697. * @ApiReturnParams (name="total_price", type="string", description="订单实价")
  698. * @ApiReturnParams (name="pay_type", type="integer", description="支付类型")
  699. * @ApiReturnParams (name="id", type="string", description="下单ip")
  700. * @ApiReturnParams (name="remark", type="string", description="订单备注")
  701. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  702. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  703. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  704. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  705. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  706. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  707. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  708. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  709. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  710. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  711. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  712. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  713. * @ApiReturnParams (name="products[].evaluate", type="integer", description="是否已评价")
  714. * @ApiReturnParams (name="products[].refund", type="integer", description="是否已退款")
  715. * @ApiReturnParams (name="express_number", type="integer", description="运单号")
  716. * @ApiReturnParams (name="pay_type_text", type="string", description="支付类型简述")
  717. * @ApiReturnParams (name="refund_status_text", type="string", description="退款状态简述")
  718. * @ApiReturnParams (name="delivery.username", type="string", description="收货人名称")
  719. * @ApiReturnParams (name="delivery.mobile", type="string", description="收货人电话")
  720. * @ApiReturnParams (name="delivery.address", type="string", description="收货人地址")
  721. *
  722. */
  723. public function detail()
  724. {
  725. $order_id = input('order_id', 0);
  726. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  727. try {
  728. $orderModel = new \addons\unishop\model\Order();
  729. $order = $orderModel
  730. ->with([
  731. 'products' => function ($query) {
  732. $query->field('id,order_id,image,number,price,spec,title,product_id');
  733. },
  734. 'extend' => function ($query) {
  735. $query->field('id,order_id,address_id,address_json,express_number,express_company');
  736. },
  737. /*'evaluate' => function ($query) {
  738. $query->field('id,order_id,product_id');
  739. }*/
  740. ])
  741. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  742. if ($order) {
  743. $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
  744. // 快递单号
  745. /*$order['express_number'] = $order['extend']['express_number'];
  746. $order['express_company'] = '快递单号';
  747. $order['express'] = '';
  748. if (class_exists(\addons\expressquery\library\Expressquery::class)) {
  749. $expressInfo = Db::name('expressquery')->where(['express' => $order['extend']['express_company']])->find();
  750. $order['express_company'] = $expressInfo['name'] ?? '快递单号';
  751. $order['express'] = $expressInfo['express'] ?? '';
  752. }*/
  753. // 送货地址
  754. /*$address = json_decode($order['extend']['address_json'], true);
  755. $area = (new \addons\unishop\model\Area())
  756. ->whereIn('id', [$address['province_id'], $address['city_id'], $address['area_id']])
  757. ->column('name', 'id');
  758. $delivery['username'] = $address['name'];
  759. $delivery['mobile'] = $address['mobile'];
  760. $delivery['address'] = $area[$address['province_id']] . ' ' . $area[$address['city_id']] . ' ' . $area[$address['area_id']] . ' ' . $address['address'];
  761. $order['delivery'] = $delivery;*/
  762. // 是否已评论
  763. // $evaluate = array_column($order['evaluate'], 'product_id');
  764. foreach ($order['products'] as &$product) {
  765. $product['image'] = Config::getImagesFullUrl($product['image']);
  766. /*if (in_array($product['id'], $evaluate)) {
  767. $product['evaluate'] = true;
  768. } else {
  769. $product['evaluate'] = false;
  770. }*/
  771. }
  772. // unset($order['evaluate']);
  773. unset($order['extend']);
  774. unset($order['pay_out_trade_no']);
  775. //预约日期
  776. $week_data = [
  777. 0 => '周日',
  778. 1 => '周一',
  779. 2 => '周二',
  780. 3 => '周三',
  781. 4 => '周四',
  782. 5 => '周五',
  783. 6 => '周六',
  784. 7 => '周日',
  785. ];
  786. $order['bookdate'] = date('Y-m-d H:i',$order['booktime']) .' '. $week_data[date('w',$order['booktime'])];
  787. //追加退改规则
  788. $order['order_refund_rule'] = config('site.order_refund_rule');
  789. //电子凭证,核销码
  790. $qrcode_string = 'order_hexiao_no|' . $order['out_trade_no'];
  791. $order['order_hexiao_qrcode'] = httpurllocal($this->inviteimage($qrcode_string));
  792. }
  793. } catch (Exception $e) {
  794. $this->error($e->getMessage());
  795. }
  796. $this->success('', $order);
  797. }
  798. //生成核销码
  799. private function inviteimage($introcode) {
  800. $params['text'] = $introcode;
  801. $qrcode_service = \addons\qrcode\library\Service::qrcode($params);
  802. $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
  803. if (!is_dir($qrcodePath)) {
  804. @mkdir($qrcodePath);
  805. }
  806. if (is_really_writable($qrcodePath)) {
  807. $filename = md5(implode('', $params)) . '.png';
  808. $filePath = $qrcodePath . $filename;
  809. $qrcode_service->writeFile($filePath);
  810. }
  811. return '/uploads/qrcode/' . $filename;
  812. }
  813. /**
  814. * @ApiTitle (申请售后信息)
  815. * @ApiSummary (申请售后信息)
  816. * @ApiMethod (POST)
  817. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  818. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  819. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  820. * @ApiReturn ({"code":1,"msg":"","data":{}})
  821. *
  822. * @ApiReturnParams (name="status", type="string", description="订单状态:-1=退货,0=取消订单,1=正常啊")
  823. * @ApiReturnParams (name="total_price", type="string", description="订单总价钱")
  824. * @ApiReturnParams (name="delivery_price", type="string", description="订单运费")
  825. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  826. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  827. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  828. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  829. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  830. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  831. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  832. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  833. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  834. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  835. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  836. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  837. * @ApiReturnParams (name="products[].choose", type="integer", description="是否选中")
  838. * @ApiReturnParams (name="refund_status_text", type="string", description="退款状态简述")
  839. * @ApiReturnParams (name="refund.id", type="string", description="退款信息id")
  840. * @ApiReturnParams (name="refund.amount", type="float",required=true, description="退款金额")
  841. * @ApiReturnParams (name="refund.service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  842. * @ApiReturnParams (name="refund.receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  843. * @ApiReturnParams (name="refund.reason_type", type="string",required=true, description="换货原因")
  844. * @ApiReturnParams (name="refund.refund_explain", type="string",required=true, description="退款说明")
  845. * @ApiReturnParams (name="refund.express_number", type="string",required=true, description="寄货物流单号")
  846. *
  847. */
  848. public function refundInfo()
  849. {
  850. $order_id = input('order_id');
  851. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  852. $orderModel = new \addons\unishop\model\Order();
  853. $order = $orderModel
  854. ->with([
  855. 'products' => function ($query) {
  856. $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
  857. },
  858. 'refund',
  859. 'refundProducts'
  860. ])
  861. ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
  862. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  863. if (!$order) {
  864. $this->error(__('Order not exist'));
  865. }
  866. $order = $order->append(['refund_status_text'])->toArray();
  867. foreach ($order['products'] as &$product) {
  868. $product['image'] = Config::getImagesFullUrl($product['image']);
  869. $product['choose'] = 0;
  870. // 如果是已提交退货的全选
  871. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
  872. foreach ($order['refund_products'] as $refundProduct) {
  873. if ($product['order_product_id'] == $refundProduct['order_product_id']) {
  874. $product['choose'] = 1;
  875. }
  876. }
  877. }
  878. }
  879. unset($order['refund_products']);
  880. $this->success('', $order);
  881. }
  882. /**
  883. * @ApiTitle (提交申请售后)
  884. * @ApiSummary (提交申请售后)
  885. * @ApiMethod (POST)
  886. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  887. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  888. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  889. * @ApiParams (name="amount", type="float",required=true, description="退款金额")
  890. * @ApiParams (name="service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  891. * @ApiParams (name="receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  892. * @ApiParams (name="reason_type", type="string",required=true, description="换货原因")
  893. * @ApiParams (name="refund_explain", type="string",required=true, description="退款说明")
  894. * @ApiParams (name="order_product_id", type="integer",required=true, description="订单的商品的id")
  895. * @ApiReturn ({"code":1,"msg":"提交","data":1})
  896. *
  897. */
  898. public function refund()
  899. {
  900. $order_id = input('order_id');
  901. $order_id = Hashids::decodeHex($order_id);
  902. $orderModel = new \addons\unishop\model\Order();
  903. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  904. if (!$order) {
  905. $this->error(__('Order not exist'));
  906. }
  907. if ($order['have_paid'] == 0) {
  908. $this->error(__('订单未支付,可直接取消,无需申请售后'));
  909. }
  910. $amount = input('amount', 0);
  911. $serviceType = input('service_type');
  912. $receivingStatus = input('receiving_status');
  913. $reasonType = input('reason_type');
  914. $refundExplain = input('refund_explain');
  915. $orderProductId = input('order_product_id');
  916. if (!$orderProductId) {
  917. $this->error(__('Please select goods'));
  918. }
  919. if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
  920. $this->error(__('Please select goods status'));
  921. }
  922. if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
  923. $this->error(__('Please select service type'));
  924. }
  925. if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
  926. if (!$amount) {
  927. $this->error(__('Please fill in the refund amount'));
  928. }
  929. }
  930. try {
  931. Db::startTrans();
  932. $orderRefund = new OrderRefund();
  933. $orderRefund->user_id = $this->auth->id;
  934. $orderRefund->order_id = $order_id;
  935. $orderRefund->receiving_status = $receivingStatus;
  936. $orderRefund->service_type = $serviceType;
  937. $orderRefund->reason_type = $reasonType;
  938. $orderRefund->amount = $amount;
  939. $orderRefund->refund_explain = $refundExplain;
  940. $orderRefund->save();
  941. $productIdArr = explode(',', $orderProductId);
  942. $refundProduct = [];
  943. foreach ($productIdArr as $orderProductId) {
  944. $tmp['order_product_id'] = $orderProductId;
  945. $tmp['order_id'] = $order_id;
  946. $tmp['user_id'] = $this->auth->id;
  947. $tmp['refund_id'] = $orderRefund['id'];
  948. $tmp['createtime'] = time();
  949. $refundProduct[] = $tmp;
  950. }
  951. (new OrderRefundProduct)->insertAll($refundProduct);
  952. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  953. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  954. $order->save();
  955. Db::commit();
  956. } catch (Exception $e) {
  957. Db::rollback();
  958. $this->error($e->getMessage());
  959. }
  960. $this->success('已申请', 1);
  961. }
  962. /**
  963. * @ApiTitle (售后发货)
  964. * @ApiSummary (售后发货)
  965. * @ApiMethod (POST)
  966. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  967. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  968. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  969. * @ApiParams (name="express_number", type="string",required=true, description="寄货物流单号")
  970. * @ApiReturn ({"code":1,"msg":"","data":1})
  971. *
  972. */
  973. public function refundDelivery()
  974. {
  975. $orderId = input('order_id');
  976. $expressNumber = input('express_number');
  977. if (!$expressNumber) {
  978. $this->error(__('Please fill in the express number'));
  979. }
  980. $orderId = Hashids::decodeHex($orderId);
  981. $orderModel = new \addons\unishop\model\Order();
  982. $order = $orderModel
  983. ->where(['id' => $orderId, 'user_id' => $this->auth->id])
  984. ->with(['refund'])->find();
  985. if (!$order || !$order->refund) {
  986. $this->error(__('Order not exist'));
  987. }
  988. try {
  989. Db::startTrans();
  990. $order->refund->express_number = $expressNumber;
  991. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  992. if ($order->refund->save() && $order->save()) {
  993. Db::commit();
  994. } else {
  995. throw new Exception(__('Operation failed'));
  996. }
  997. } catch (Exception $e) {
  998. Db::rollback();
  999. $this->success($e->getMessage());
  1000. }
  1001. $this->success('', 1);
  1002. }
  1003. /**
  1004. * @ApiTitle (快递查询)
  1005. * @ApiSummary (快递查询)
  1006. * @ApiMethod (POST)
  1007. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  1008. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  1009. * @ApiParams (name="express", type="string",required=true, description="寄货物流公司")
  1010. * @ApiParams (name="expresssn", type="string",required=true, description="寄货物流单号")
  1011. * @ApiReturn ({"code":1,"msg":"","data":1})
  1012. *
  1013. */
  1014. public function express() {
  1015. $params = input();
  1016. if (!class_exists(\addons\expressquery\library\Expressquery::class)) {
  1017. $this->error('请先安装插件《物流信息接口》', []);
  1018. }
  1019. $info = get_addon_info('expressquery');
  1020. if ($info['state'] == 0) {
  1021. $this->error('请开启插件《物流信息接口》', []);
  1022. }
  1023. $expModle = new \addons\expressquery\library\Expressquery();
  1024. $list = $expModle->getExpressList($params['express'], $params['expresssn']);
  1025. if ($list['code'] == 0) {
  1026. $this->error($list['msg']);
  1027. }
  1028. $expressInfo = Db::name('expressquery')->where(['express' => $params['express']])->find();
  1029. $this->success('', [
  1030. 'message' => $list['data'] ?? [],
  1031. 'company' => $expressInfo['name'] ?? '快递单号'
  1032. ]);
  1033. }
  1034. //出示核销码
  1035. public function show_hexiao_qrcode(){
  1036. }
  1037. //退订
  1038. public function tuiding(){}
  1039. }