Order.php 54 KB

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