Order.php 53 KB

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