Order.php 53 KB

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