Order.php 54 KB

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