Order.php 53 KB

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