Order.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. /*$coupon = CouponModel::all(function ($query) {
  133. $time = time();
  134. $query
  135. ->where(['switch' => CouponModel::SWITCH_ON])
  136. ->where('starttime', '<', $time)
  137. ->where('endtime', '>', $time);
  138. });
  139. if ($coupon) {
  140. $coupon = collection($coupon)->toArray();
  141. }*/
  142. /** 运费数据 **/
  143. /*$cityId = $address['city_id'] ? $address['city_id'] : 0;
  144. $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);*/
  145. $order_price = '0';
  146. $delivery_price = '0';
  147. foreach ($productData as &$product) {
  148. $product['image'] = Config::getImagesFullUrl($product['image']);
  149. $product['sales_price'] = round($product['sales_price'], 2);
  150. $product['market_price'] = round($product['market_price'], 2);
  151. $order_price = bcadd($order_price,$product['total_price'],2);
  152. $delivery_price = bcadd($delivery_price,$product['yunfei_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','alipay');
  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=待付款,2=待发货,3=待收货,4=待评价,5=售后
  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_delivered'] != \addons\unishop\model\Order::DELIVERED_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. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_delivered'] == \addons\unishop\model\Order::DELIVERED_NO) {
  330. Db::startTrans();
  331. /*$order->status = \addons\unishop\model\Order::STATUS_CANCEL;
  332. $order->save();*/
  333. //已付款的,自动退款
  334. if($order['have_paid'] != \addons\unishop\model\Order::PAID_NO){
  335. $refund_amount = $order['total_price'];
  336. $refund_status = 3;
  337. // 退款
  338. $params = [
  339. 'refund_status' => $refund_status,
  340. 'updatetime' => time(),
  341. 'refund_amount' => $refund_amount,
  342. 'status' => \addons\unishop\model\Order::STATUS_CANCEL,
  343. ];
  344. if($refund_status == 3) {
  345. $params['had_refund'] = time();
  346. }
  347. $result = Db::name('unishop_order')->where(['id' => $order_id])->update($params);
  348. if (!$result) {
  349. Db::rollback();
  350. $this->error('取消失败');
  351. }
  352. //同意并执行退款
  353. if($refund_status == 3 && $refund_amount > 0){
  354. $order = Db::name('unishop_order')->where('id',$order_id)->find();
  355. if($order['pay_type'] == 2){
  356. $wallet = new \app\common\model\Wallet();
  357. $wallet_rs = $wallet->lockChangeAccountRemain($order['user_id'],'money',$refund_amount,32,'商城取消订单退款'.$order['out_trade_no'],'unishop_order',$order_id);
  358. if($wallet_rs['status'] === false){
  359. Db::rollback();
  360. $this->error($wallet_rs['msg']);
  361. }
  362. }elseif($order['pay_type'] == 3 || $order['pay_type'] == 4){
  363. $refund_result = $this->old_refund($order,$refund_amount);
  364. if($refund_result !== true){
  365. Db::rollback();
  366. $this->error($refund_result);
  367. }
  368. }
  369. }
  370. }else{
  371. $params = [
  372. 'status' => \addons\unishop\model\Order::STATUS_CANCEL,
  373. ];
  374. $result = Db::name('unishop_order')->where(['id' => $order_id])->update($params);
  375. if ($result === false) {
  376. Db::rollback();
  377. $this->error('取消失败');
  378. }
  379. }
  380. Db::commit();
  381. $this->success('取消成功', true);
  382. }
  383. }
  384. // 退款
  385. public function old_refund($order, $refund_price)
  386. {
  387. $table = 'unishop_order';
  388. $remark = '订单退款';
  389. if($order['pay_type'] == 3){
  390. $order['pay_type'] = 'wechat';
  391. }
  392. if($order['pay_type'] == 4){
  393. $order['pay_type'] = 'alipay';
  394. }
  395. // 生成退款单
  396. $refund_data = [
  397. 'order_id' => $order['id'],
  398. 'out_refund_no'=> createUniqueNo('R'),
  399. 'pay_fee' => $order['total_price'],
  400. 'refund_price' => $refund_price,
  401. 'pay_type' => $order['pay_type'],
  402. 'status' => 0,
  403. 'createtime' => time(),
  404. 'updatetime' => time(),
  405. 'table' => $table,
  406. 'table_id' => $order['id'],
  407. ];
  408. $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
  409. if(!$refund_log_id){
  410. return '退款失败';
  411. }
  412. if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
  413. // 微信|支付宝退款
  414. // 退款数据
  415. $order_data = [
  416. 'out_trade_no' => $order['pay_out_trade_no']
  417. ];
  418. if ($order['pay_type'] == 'wechat') {
  419. $total_fee = $order['total_price'] * 100;
  420. $refund_fee = $refund_price * 100;
  421. $order_data = array_merge($order_data, [
  422. 'out_refund_no' => $refund_data['out_refund_no'],
  423. 'total_fee' => $total_fee,
  424. 'refund_fee' => $refund_fee,
  425. 'refund_desc' => $remark,
  426. ]);
  427. } else {
  428. $order_data = array_merge($order_data, [
  429. 'out_request_no' => $refund_data['out_refund_no'],
  430. 'refund_amount' => $refund_price,
  431. ]);
  432. }
  433. //
  434. if ($order['pay_type'] == 'wechat') {
  435. $wxpay = new \app\common\library\Wxpay;
  436. $result = $wxpay->WxPayRefund($order_data);
  437. // 微信通知回调 pay->notifyr
  438. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  439. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  440. return true;
  441. } else {
  442. return $result['return_msg'];
  443. }
  444. } else {
  445. $result = Service::submitRefund($order['total_price'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');
  446. if($result['code'] == '10000'){
  447. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  448. return true;
  449. }else{
  450. return $result['msg'];
  451. }
  452. /* return 'alipay wrong way';
  453. $alipay = new \app\common\library\AliPay;
  454. $result = $alipay->AliPayRefund($order_data);
  455. // 支付宝通知回调 pay->notifyx
  456. return $result;*/
  457. /*if ($result['code'] == "10000") {
  458. return true;
  459. } else {
  460. throw new \Exception($result['msg']);
  461. }*/
  462. }
  463. // { // 微信返回结果
  464. // "return_code":"SUCCESS",
  465. // "return_msg":"OK",
  466. // "appid":"wx39cd0799d4567dd0",
  467. // "mch_id":"1481069012",
  468. // "nonce_str":"huW9eIAb5BDPn0Ma",
  469. // "sign":"250316740B263FE53F5DFF50AF5A8FA1",
  470. // "result_code":"SUCCESS",
  471. // "transaction_id":"4200000497202004072822298902",
  472. // "out_trade_no":"202010300857029180027000",
  473. // "out_refund_no":"1586241595",
  474. // "refund_id":"50300603862020040700031444448",
  475. // "refund_channel":[],
  476. // "refund_fee":"1",
  477. // "coupon_refund_fee":"0",
  478. // "total_fee":"1",
  479. // "cash_fee":"1",
  480. // "coupon_refund_count":"0",
  481. // "cash_refund_fee":"1
  482. // }
  483. // { // 支付宝返回结果
  484. // "code": "10000",
  485. // "msg": "Success",
  486. // "buyer_logon_id": "157***@163.com",
  487. // "buyer_user_id": "2088902485164146",
  488. // "fund_change": "Y",
  489. // "gmt_refund_pay": "2020-08-15 16:11:45",
  490. // "out_trade_no": "202002460317545607015300",
  491. // "refund_fee": "0.01",
  492. // "send_back_fee": "0.00",
  493. // "trade_no": "2020081522001464141438570535"
  494. // }
  495. }
  496. return true;
  497. }
  498. /**
  499. * @ApiTitle (删除订单)
  500. * @ApiSummary (只能删除已取消或已退货的订单)
  501. * @ApiMethod (GET)
  502. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  503. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  504. * @ApiParams (name="order_id", type="string", description="订单id")
  505. * @ApiReturn ({"code":1,"msg":"删除成功","data":true})
  506. *
  507. */
  508. public function delete()
  509. {
  510. $order_id = input('order_id', 0);
  511. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  512. $orderModel = new \addons\unishop\model\Order();
  513. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  514. if (!$order) {
  515. $this->error(__('Order not exist'));
  516. }
  517. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL) {
  518. $this->error('只能删除已取消或已退货的订单');
  519. }
  520. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND && $order['refund_status'] == \addons\unishop\model\Order::REFUND_STATUS_APPLY) {
  521. $this->error('订单退款中,不可删除订单');
  522. }
  523. $order->delete();
  524. $this->success('删除成功', true);
  525. }
  526. /**
  527. * @ApiTitle (确认收货)
  528. * @ApiSummary (确认收货)
  529. * @ApiMethod (GET)
  530. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  531. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  532. * @ApiParams (name="order_id", type="string", description="订单id")
  533. * @ApiReturn ({"code":1,"msg":"已确认收货","data":true})
  534. *
  535. */
  536. public function received()
  537. {
  538. $order_id = input('order_id', 0);
  539. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  540. $orderModel = new \addons\unishop\model\Order();
  541. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  542. if (!$order) {
  543. $this->error(__('Order not exist'));
  544. }
  545. if ($order->have_delivered == 0) {
  546. $this->error('未发货,不能确认收货');
  547. }
  548. $order->have_received = time();
  549. $order->save();
  550. $this->success('已确认收货', true);
  551. }
  552. /**
  553. * @ApiTitle (发表评论/评价)
  554. * @ApiSummary (发表评论/评价)
  555. * @ApiMethod (POST)
  556. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  557. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  558. * @ApiParams (name="order_id", type="string", description="订单id")
  559. * @ApiParams (name="rate", type="integer", description="分数/星星")
  560. * @ApiParams (name="anonymous", type="integer", description="是否匿名")
  561. * @ApiParams (name="comment", type="string", description="评价内容")
  562. * @ApiParams (name="product_id", type="string", description="商品id")
  563. * @ApiReturn ({"code":1,"msg":"感谢评价","data":true})
  564. *
  565. */
  566. public function comment()
  567. {
  568. $rate = input('rate', 5);
  569. $anonymous = input('anonymous', 0);
  570. $comment = input('comment');
  571. $order_id = input('order_id', 0);
  572. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  573. $product_id = input('product_id');
  574. $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
  575. $orderProductModel = new \addons\unishop\model\OrderProduct();
  576. $orderProduct = $orderProductModel->where(['product_id' => $product_id, 'order_id' => $order_id, 'user_id' => $this->auth->id])->find();
  577. $orderModel = new \addons\unishop\model\Order();
  578. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  579. if (!$orderProduct || !$order) {
  580. $this->error(__('Order not exist'));
  581. }
  582. if ($order->have_received == $orderModel::RECEIVED_NO) {
  583. $this->error(__('未收货,不可评价'));
  584. }
  585. $result = false;
  586. try {
  587. $evaluate = new Evaluate();
  588. $evaluate->user_id = $this->auth->id;
  589. $evaluate->order_id = $order_id;
  590. $evaluate->product_id = $product_id;
  591. $evaluate->rate = $rate;
  592. $evaluate->anonymous = $anonymous;
  593. $evaluate->comment = $comment;
  594. $evaluate->spec = $orderProduct->spec;
  595. $result = $evaluate->save();
  596. if ($result) {
  597. $order->have_commented = time();
  598. $order->save();
  599. }
  600. } catch (Exception $e) {
  601. $this->error($e->getMessage());
  602. }
  603. if ($result !== false) {
  604. $this->success(__('Thanks for the evaluation'));
  605. } else {
  606. $this->error(__('Evaluation failure'));
  607. }
  608. }
  609. /**
  610. * @ApiTitle (获取订单数量)
  611. * @ApiSummary (获取订单数量)
  612. * @ApiMethod (POST)
  613. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  614. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  615. * @ApiReturn ({"code":1,"msg":"","data":{}})
  616. *
  617. * @ApiReturnParams (name="unpaid", type="integer", description="未支付数量")
  618. * @ApiReturnParams (name="undelivered", type="integer", description="未发货数量")
  619. * @ApiReturnParams (name="unreceived", type="integer", description="未收货数量")
  620. * @ApiReturnParams (name="uncomment", type="integer", description="未评价数量")
  621. * @ApiReturnParams (name="refund", type="integer", description="正在售后的数量")
  622. *
  623. */
  624. public function count()
  625. {
  626. if (!$this->auth->isLogin()) {
  627. $this->error('');
  628. }
  629. $order = new \addons\unishop\model\Order();
  630. $list = $order
  631. ->where([
  632. 'user_id' => $this->auth->id,
  633. ])
  634. ->where('status', '<>', \addons\unishop\model\Order::STATUS_CANCEL)
  635. ->where(function ($query) {
  636. $query
  637. ->whereOr([
  638. 'have_paid' => \addons\unishop\model\Order::PAID_NO,
  639. 'have_delivered' => \addons\unishop\model\Order::DELIVERED_NO,
  640. 'have_received' => \addons\unishop\model\Order::RECEIVED_NO,
  641. 'have_commented' => \addons\unishop\model\Order::COMMENTED_NO
  642. ])
  643. ->whereOr('refund_status', '>', \addons\unishop\model\Order::REFUND_STATUS_NONE);
  644. })
  645. ->field('have_paid,have_delivered,have_received,have_commented,refund_status,had_refund')
  646. ->select();
  647. $data = [
  648. 'unpaid' => 0,
  649. 'undelivered' => 0,
  650. 'unreceived' => 0,
  651. 'uncomment' => 0,
  652. 'refund' => 0
  653. ];
  654. foreach ($list as $item) {
  655. switch (true) {
  656. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] > 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  657. $data['uncomment']++;
  658. break;
  659. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  660. $data['unreceived']++;
  661. break;
  662. case $item['have_paid'] > 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  663. $data['undelivered']++;
  664. break;
  665. case $item['have_paid'] == 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  666. $data['unpaid']++;
  667. break;
  668. case $item['refund_status'] > 0 && $item['had_refund'] == 0 && $item['refund_status'] != 3:
  669. $data['refund']++;
  670. break;
  671. }
  672. }
  673. $this->success('', $data);
  674. }
  675. /**
  676. * @ApiTitle (订单详情细节)
  677. * @ApiSummary (订单详情细节)
  678. * @ApiMethod (GET)
  679. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  680. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  681. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  682. * @ApiReturn ({"code":1,"msg":"","data":{}})
  683. *
  684. * @ApiReturnParams (name="out_trade_no", type="string", description="商户订单号")
  685. * @ApiReturnParams (name="order_price", type="string", description="订单原价钱")
  686. * @ApiReturnParams (name="discount_price", type="string", description="优惠多少钱")
  687. * @ApiReturnParams (name="delivery_price", type="string", description="运费多少钱")
  688. * @ApiReturnParams (name="total_price", type="string", description="订单实价")
  689. * @ApiReturnParams (name="pay_type", type="integer", description="支付类型")
  690. * @ApiReturnParams (name="id", type="string", description="下单ip")
  691. * @ApiReturnParams (name="remark", type="string", description="订单备注")
  692. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  693. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  694. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  695. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  696. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  697. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  698. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  699. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  700. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  701. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  702. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  703. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  704. * @ApiReturnParams (name="products[].evaluate", type="integer", description="是否已评价")
  705. * @ApiReturnParams (name="products[].refund", type="integer", description="是否已退款")
  706. * @ApiReturnParams (name="express_number", type="integer", description="运单号")
  707. * @ApiReturnParams (name="pay_type_text", type="string", description="支付类型简述")
  708. * @ApiReturnParams (name="refund_status_text", type="string", description="退款状态简述")
  709. * @ApiReturnParams (name="delivery.username", type="string", description="收货人名称")
  710. * @ApiReturnParams (name="delivery.mobile", type="string", description="收货人电话")
  711. * @ApiReturnParams (name="delivery.address", type="string", description="收货人地址")
  712. *
  713. */
  714. public function detail()
  715. {
  716. $order_id = input('order_id', 0);
  717. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  718. try {
  719. $orderModel = new \addons\unishop\model\Order();
  720. $order = $orderModel
  721. ->with([
  722. 'products' => function ($query) {
  723. $query->field('id,order_id,image,number,price,spec,title,product_id');
  724. },
  725. 'extend' => function ($query) {
  726. $query->field('id,order_id,address_id,address_json,express_number,express_company');
  727. },
  728. 'evaluate' => function ($query) {
  729. $query->field('id,order_id,product_id');
  730. }
  731. ])
  732. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  733. if ($order) {
  734. $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
  735. // 快递单号
  736. $order['express_number'] = $order['extend']['express_number'];
  737. $order['express_company'] = '快递单号';
  738. $order['express'] = '';
  739. if (class_exists(\addons\expressquery\library\Expressquery::class)) {
  740. $expressInfo = Db::name('expressquery')->where(['express' => $order['extend']['express_company']])->find();
  741. $order['express_company'] = $expressInfo['name'] ?? '快递单号';
  742. $order['express'] = $expressInfo['express'] ?? '';
  743. }
  744. // 送货地址
  745. $address = json_decode($order['extend']['address_json'], true);
  746. $delivery['username'] = $address['name'];
  747. $delivery['mobile'] = $address['mobile'];
  748. $delivery['address'] = $address['province_name'] . ' ' . $address['city_name'] . ' ' . $address['area_name'] . ' ' . $address['address'];
  749. $order['delivery'] = $delivery;
  750. // 是否已评论
  751. $evaluate = array_column($order['evaluate'], 'product_id');
  752. foreach ($order['products'] as &$product) {
  753. $product['image'] = Config::getImagesFullUrl($product['image']);
  754. if (in_array($product['id'], $evaluate)) {
  755. $product['evaluate'] = true;
  756. } else {
  757. $product['evaluate'] = false;
  758. }
  759. }
  760. unset($order['evaluate']);
  761. unset($order['extend']);
  762. }
  763. } catch (Exception $e) {
  764. $this->error($e->getMessage());
  765. }
  766. $this->success('', $order);
  767. }
  768. /**
  769. * @ApiTitle (申请售后信息)
  770. * @ApiSummary (申请售后信息)
  771. * @ApiMethod (POST)
  772. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  773. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  774. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  775. * @ApiReturn ({"code":1,"msg":"","data":{}})
  776. *
  777. * @ApiReturnParams (name="status", type="string", description="订单状态:-1=退货,0=取消订单,1=正常啊")
  778. * @ApiReturnParams (name="total_price", type="string", description="订单总价钱")
  779. * @ApiReturnParams (name="delivery_price", type="string", description="订单运费")
  780. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  781. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  782. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  783. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  784. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  785. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  786. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  787. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  788. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  789. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  790. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  791. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  792. * @ApiReturnParams (name="products[].choose", type="integer", description="是否选中")
  793. * @ApiReturnParams (name="refund_status_text", type="string", description="退款状态简述")
  794. * @ApiReturnParams (name="refund.id", type="string", description="退款信息id")
  795. * @ApiReturnParams (name="refund.amount", type="float",required=true, description="退款金额")
  796. * @ApiReturnParams (name="refund.service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  797. * @ApiReturnParams (name="refund.receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  798. * @ApiReturnParams (name="refund.reason_type", type="string",required=true, description="换货原因")
  799. * @ApiReturnParams (name="refund.refund_explain", type="string",required=true, description="退款说明")
  800. * @ApiReturnParams (name="refund.express_number", type="string",required=true, description="寄货物流单号")
  801. *
  802. */
  803. public function refundInfo()
  804. {
  805. $order_id = input('order_id');
  806. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  807. $orderModel = new \addons\unishop\model\Order();
  808. $order = $orderModel
  809. ->with([
  810. 'products' => function ($query) {
  811. $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
  812. },
  813. 'refund',
  814. 'refundProducts'
  815. ])
  816. ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
  817. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  818. if (!$order) {
  819. $this->error(__('Order not exist'));
  820. }
  821. $order = $order->append(['refund_status_text'])->toArray();
  822. foreach ($order['products'] as &$product) {
  823. $product['image'] = Config::getImagesFullUrl($product['image']);
  824. $product['choose'] = 0;
  825. // 如果是已提交退货的全选
  826. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
  827. foreach ($order['refund_products'] as $refundProduct) {
  828. if ($product['order_product_id'] == $refundProduct['order_product_id']) {
  829. $product['choose'] = 1;
  830. }
  831. }
  832. }
  833. }
  834. unset($order['refund_products']);
  835. $this->success('', $order);
  836. }
  837. /**
  838. * @ApiTitle (提交申请售后)
  839. * @ApiSummary (提交申请售后)
  840. * @ApiMethod (POST)
  841. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  842. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  843. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  844. * @ApiParams (name="amount", type="float",required=true, description="退款金额")
  845. * @ApiParams (name="service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  846. * @ApiParams (name="receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  847. * @ApiParams (name="reason_type", type="string",required=true, description="换货原因")
  848. * @ApiParams (name="refund_explain", type="string",required=true, description="退款说明")
  849. * @ApiParams (name="order_product_id", type="integer",required=true, description="订单的商品的id")
  850. * @ApiReturn ({"code":1,"msg":"提交","data":1})
  851. *
  852. */
  853. public function refund()
  854. {
  855. $order_id = input('order_id');
  856. $order_id = Hashids::decodeHex($order_id);
  857. $orderModel = new \addons\unishop\model\Order();
  858. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  859. if (!$order) {
  860. $this->error(__('Order not exist'));
  861. }
  862. /*if ($order['have_paid'] == 0) {
  863. $this->error(__('订单未支付,可直接取消,无需申请售后'));
  864. }*/
  865. if ($order['have_delivered'] == 0) {
  866. $this->error(__('订单未发货,可直接取消,无需申请售后'));//发货的才能走售后,未发货的走取消
  867. }
  868. if($order->status == 0){
  869. $this->error('当前订单不能申请退款');
  870. }
  871. if($order->status == -1){
  872. $this->error('当前订单已经申请过退款');
  873. }
  874. //$amount = input('amount', 0);
  875. $amount = $order->total_price;
  876. $serviceType = input('service_type',1);
  877. $receivingStatus = input('receiving_status',1);
  878. $reasonType = input('reason_type','');
  879. $refundExplain = input('refund_explain','');
  880. $images = input('images','');
  881. // $orderProductId = input('order_product_id');
  882. $orderProductId = Db::name('unishop_order_product')->where('order_id',$order_id)->column('product_id');
  883. $orderProductId = implode(',', $orderProductId);
  884. if (!$orderProductId) {
  885. $this->error(__('Please select goods'));
  886. }
  887. if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
  888. $this->error(__('Please select goods status'));
  889. }
  890. if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
  891. $this->error(__('Please select service type'));
  892. }
  893. if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
  894. if (!$amount) {
  895. $this->error(__('Please fill in the refund amount'));
  896. }
  897. }
  898. try {
  899. Db::startTrans();
  900. $orderRefund = new OrderRefund();
  901. $orderRefund->user_id = $this->auth->id;
  902. $orderRefund->order_id = $order_id;
  903. $orderRefund->receiving_status = $receivingStatus;
  904. $orderRefund->service_type = $serviceType;
  905. $orderRefund->reason_type = $reasonType;
  906. $orderRefund->amount = $amount;
  907. $orderRefund->refund_explain = $refundExplain;
  908. $orderRefund->images = $images;
  909. $orderRefund->save();
  910. $productIdArr = explode(',', $orderProductId);
  911. $refundProduct = [];
  912. foreach ($productIdArr as $orderProductId) {
  913. $tmp['order_product_id'] = $orderProductId;
  914. $tmp['order_id'] = $order_id;
  915. $tmp['user_id'] = $this->auth->id;
  916. $tmp['refund_id'] = $orderRefund['id'];
  917. $tmp['createtime'] = time();
  918. $refundProduct[] = $tmp;
  919. }
  920. (new OrderRefundProduct)->insertAll($refundProduct);
  921. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  922. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  923. $order->save();
  924. Db::commit();
  925. } catch (Exception $e) {
  926. Db::rollback();
  927. $this->error($e->getMessage());
  928. }
  929. $this->success('已申请', 1);
  930. }
  931. /**
  932. * @ApiTitle (售后发货)
  933. * @ApiSummary (售后发货)
  934. * @ApiMethod (POST)
  935. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  936. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  937. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  938. * @ApiParams (name="express_number", type="string",required=true, description="寄货物流单号")
  939. * @ApiReturn ({"code":1,"msg":"","data":1})
  940. *
  941. */
  942. public function refundDelivery()
  943. {
  944. $orderId = input('order_id');
  945. $expressNumber = input('express_number');
  946. if (!$expressNumber) {
  947. $this->error(__('Please fill in the express number'));
  948. }
  949. $orderId = Hashids::decodeHex($orderId);
  950. $orderModel = new \addons\unishop\model\Order();
  951. $order = $orderModel
  952. ->where(['id' => $orderId, 'user_id' => $this->auth->id])
  953. ->with(['refund'])->find();
  954. if (!$order || !$order->refund) {
  955. $this->error(__('Order not exist'));
  956. }
  957. try {
  958. Db::startTrans();
  959. $order->refund->express_number = $expressNumber;
  960. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  961. if ($order->refund->save() && $order->save()) {
  962. Db::commit();
  963. } else {
  964. throw new Exception(__('Operation failed'));
  965. }
  966. } catch (Exception $e) {
  967. Db::rollback();
  968. $this->success($e->getMessage());
  969. }
  970. $this->success('', 1);
  971. }
  972. /**
  973. * @ApiTitle (快递查询)
  974. * @ApiSummary (快递查询)
  975. * @ApiMethod (POST)
  976. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  977. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  978. * @ApiParams (name="express", type="string",required=true, description="寄货物流公司")
  979. * @ApiParams (name="expresssn", type="string",required=true, description="寄货物流单号")
  980. * @ApiReturn ({"code":1,"msg":"","data":1})
  981. *
  982. */
  983. public function express() {
  984. $params = input();
  985. if (!class_exists(\addons\expressquery\library\Expressquery::class)) {
  986. $this->error('请先安装插件《物流信息接口》', []);
  987. }
  988. $info = get_addon_info('expressquery');
  989. if ($info['state'] == 0) {
  990. $this->error('请开启插件《物流信息接口》', []);
  991. }
  992. $expModle = new \addons\expressquery\library\Expressquery();
  993. $list = $expModle->getExpressList($params['express'], $params['expresssn']);
  994. if ($list['code'] == 0) {
  995. $this->error($list['msg']);
  996. }
  997. $expressInfo = Db::name('expressquery')->where(['express' => $params['express']])->find();
  998. $this->success('', [
  999. 'message' => $list['data'] ?? [],
  1000. 'company' => $expressInfo['name'] ?? '快递单号'
  1001. ]);
  1002. }
  1003. /*
  1004. * 申请退货原因
  1005. * */
  1006. public function refund_reason(){
  1007. $list = Db::name('unishop_refundreason')->order('weigh asc')->select();
  1008. $this->success(1,$list);
  1009. }
  1010. }