Order.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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('listrow', 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. try {
  750. $orderModel = new \addons\unishop\model\Order();
  751. $order = $orderModel
  752. ->with([
  753. 'products' => function ($query) {
  754. $query->field('id,order_id,image,number,price,spec,title,product_id');
  755. },
  756. /*'extend' => function ($query) {
  757. $query->field('id,order_id,address_id,address_json,express_number,express_company');
  758. },
  759. 'evaluate' => function ($query) {
  760. $query->field('id,order_id,product_id');
  761. }*/
  762. ])
  763. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  764. if ($order) {
  765. $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
  766. $order['state_text'] = $orderModel->getStatetextAttr($order['state'],$order['refund_status']);//状态中文
  767. // 快递单号
  768. /*$order['express_number'] = $order['extend']['express_number'];
  769. $order['express_company'] = '快递单号';
  770. $order['express'] = '';
  771. if (class_exists(\addons\expressquery\library\Expressquery::class)) {
  772. $expressInfo = Db::name('expressquery')->where(['express' => $order['extend']['express_company']])->find();
  773. $order['express_company'] = $expressInfo['name'] ?? '快递单号';
  774. $order['express'] = $expressInfo['express'] ?? '';
  775. }*/
  776. // 送货地址
  777. /*$address = json_decode($order['extend']['address_json'], true);
  778. $area = (new \addons\unishop\model\Area())
  779. ->whereIn('id', [$address['province_id'], $address['city_id'], $address['area_id']])
  780. ->column('name', 'id');
  781. $delivery['username'] = $address['name'];
  782. $delivery['mobile'] = $address['mobile'];
  783. $delivery['address'] = $area[$address['province_id']] . ' ' . $area[$address['city_id']] . ' ' . $area[$address['area_id']] . ' ' . $address['address'];
  784. $order['delivery'] = $delivery;*/
  785. // 是否已评论
  786. // $evaluate = array_column($order['evaluate'], 'product_id');
  787. foreach ($order['products'] as &$product) {
  788. $product['image'] = Config::getImagesFullUrl($product['image']);
  789. /*if (in_array($product['id'], $evaluate)) {
  790. $product['evaluate'] = true;
  791. } else {
  792. $product['evaluate'] = false;
  793. }*/
  794. $product_find = Db::name('unishop_product')->where('id', $product['product_id'])->find();
  795. $product['info'] = $product_find['info'];
  796. $product['activetime'] = $product_find['activetime'];
  797. }
  798. // unset($order['evaluate']);
  799. unset($order['extend']);
  800. unset($order['pay_out_trade_no']);
  801. //预约日期
  802. $week_data = [
  803. 0 => '周日',
  804. 1 => '周一',
  805. 2 => '周二',
  806. 3 => '周三',
  807. 4 => '周四',
  808. 5 => '周五',
  809. 6 => '周六',
  810. 7 => '周日',
  811. ];
  812. $order['bookdate'] = date('Y-m-d H:i',$order['booktime']) .' '. $week_data[date('w',$order['booktime'])];
  813. //追加退改规则
  814. $order['order_refund_rule'] = config('site.order_refund_rule');
  815. //电子凭证,核销码
  816. $qrcode_string = 'order_hexiao_no|' . $order['out_trade_no'];
  817. $order['order_hexiao_qrcode'] = httpurllocal($this->inviteimage($qrcode_string));
  818. }
  819. } catch (Exception $e) {
  820. $this->error($e->getMessage());
  821. }
  822. $this->success('', $order);
  823. }
  824. //生成核销码
  825. private function inviteimage($introcode) {
  826. $params['text'] = $introcode;
  827. $qrcode_service = \addons\qrcode\library\Service::qrcode($params);
  828. $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
  829. if (!is_dir($qrcodePath)) {
  830. @mkdir($qrcodePath);
  831. }
  832. if (is_really_writable($qrcodePath)) {
  833. $filename = md5(implode('', $params)) . '.png';
  834. $filePath = $qrcodePath . $filename;
  835. $qrcode_service->writeFile($filePath);
  836. }
  837. return '/uploads/qrcode/' . $filename;
  838. }
  839. /**
  840. * @ApiTitle (申请售后信息)
  841. * @ApiSummary (申请售后信息)
  842. * @ApiMethod (POST)
  843. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  844. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  845. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  846. * @ApiReturn ({"code":1,"msg":"","data":{}})
  847. *
  848. * @ApiReturnParams (name="status", type="string", description="订单状态:-1=退货,0=取消订单,1=正常啊")
  849. * @ApiReturnParams (name="total_price", type="string", description="订单总价钱")
  850. * @ApiReturnParams (name="delivery_price", type="string", description="订单运费")
  851. * @ApiReturnParams (name="have_paid", type="integer", description="是否支付")
  852. * @ApiReturnParams (name="have_delivered", type="integer", description="是否发货")
  853. * @ApiReturnParams (name="have_received", type="integer", description="是否收货")
  854. * @ApiReturnParams (name="have_commented", type="integer", description="是否评论")
  855. * @ApiReturnParams (name="refund_status", type="integer", description="退款状态")
  856. * @ApiReturnParams (name="products[].id", type="string", description="商品id")
  857. * @ApiReturnParams (name="products[].title", type="string", description="商品名称")
  858. * @ApiReturnParams (name="products[].image", type="string", description="商品图片")
  859. * @ApiReturnParams (name="products[].number", type="integer", description="商品数量")
  860. * @ApiReturnParams (name="products[].price", type="string", description="商品价钱")
  861. * @ApiReturnParams (name="products[].spec", type="string", description="选中的规格")
  862. * @ApiReturnParams (name="products[].order_product_id", type="integer", description="订单商品id")
  863. * @ApiReturnParams (name="products[].choose", type="integer", description="是否选中")
  864. * @ApiReturnParams (name="refund_status_text", type="string", description="退款状态简述")
  865. * @ApiReturnParams (name="refund.id", type="string", description="退款信息id")
  866. * @ApiReturnParams (name="refund.amount", type="float",required=true, description="退款金额")
  867. * @ApiReturnParams (name="refund.service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  868. * @ApiReturnParams (name="refund.receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  869. * @ApiReturnParams (name="refund.reason_type", type="string",required=true, description="换货原因")
  870. * @ApiReturnParams (name="refund.refund_explain", type="string",required=true, description="退款说明")
  871. * @ApiReturnParams (name="refund.express_number", type="string",required=true, description="寄货物流单号")
  872. *
  873. */
  874. public function refundInfo()
  875. {
  876. $order_id = input('order_id');
  877. // $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  878. $orderModel = new \addons\unishop\model\Order();
  879. $order = $orderModel
  880. ->with([
  881. 'products' => function ($query) {
  882. $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
  883. },
  884. 'refund',
  885. 'refundProducts'
  886. ])
  887. ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
  888. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  889. if (!$order) {
  890. $this->error(__('Order not exist'));
  891. }
  892. $order = $order->append(['refund_status_text'])->toArray();
  893. foreach ($order['products'] as &$product) {
  894. $product['image'] = Config::getImagesFullUrl($product['image']);
  895. $product['choose'] = 0;
  896. // 如果是已提交退货的全选
  897. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
  898. foreach ($order['refund_products'] as $refundProduct) {
  899. if ($product['order_product_id'] == $refundProduct['order_product_id']) {
  900. $product['choose'] = 1;
  901. }
  902. }
  903. }
  904. }
  905. unset($order['refund_products']);
  906. $this->success('', $order);
  907. }
  908. /**
  909. * @ApiTitle (提交申请售后)
  910. * @ApiSummary (提交申请售后)
  911. * @ApiMethod (POST)
  912. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  913. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  914. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  915. * @ApiParams (name="amount", type="float",required=true, description="退款金额")
  916. * @ApiParams (name="service_type", type="string",required=true, description="服务类型:0=我要退款无需退货,1=我要退货退款,2=换货")
  917. * @ApiParams (name="receiving_status", type="integer",required=true, description="货物状态:0=未收到,1=已收到")
  918. * @ApiParams (name="reason_type", type="string",required=true, description="换货原因")
  919. * @ApiParams (name="refund_explain", type="string",required=true, description="退款说明")
  920. * @ApiParams (name="order_product_id", type="integer",required=true, description="订单的商品的id")
  921. * @ApiReturn ({"code":1,"msg":"提交","data":1})
  922. *
  923. */
  924. public function refund()
  925. {
  926. $order_id = input('order_id');
  927. // $order_id = Hashids::decodeHex($order_id);
  928. $orderModel = new \addons\unishop\model\Order();
  929. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  930. if (!$order) {
  931. $this->error(__('Order not exist'));
  932. }
  933. if ($order['have_paid'] == 0) {
  934. $this->error(__('订单未支付,可直接取消,无需申请售后'));
  935. }
  936. $amount = input('amount', 0);
  937. $serviceType = input('service_type');
  938. $receivingStatus = input('receiving_status');
  939. $reasonType = input('reason_type');
  940. $refundExplain = input('refund_explain');
  941. $orderProductId = input('order_product_id');
  942. if (!$orderProductId) {
  943. $this->error(__('Please select goods'));
  944. }
  945. if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
  946. $this->error(__('Please select goods status'));
  947. }
  948. if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
  949. $this->error(__('Please select service type'));
  950. }
  951. if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
  952. if (!$amount) {
  953. $this->error(__('Please fill in the refund amount'));
  954. }
  955. }
  956. try {
  957. Db::startTrans();
  958. $orderRefund = new OrderRefund();
  959. $orderRefund->user_id = $this->auth->id;
  960. $orderRefund->order_id = $order_id;
  961. $orderRefund->receiving_status = $receivingStatus;
  962. $orderRefund->service_type = $serviceType;
  963. $orderRefund->reason_type = $reasonType;
  964. $orderRefund->amount = $amount;
  965. $orderRefund->refund_explain = $refundExplain;
  966. $orderRefund->save();
  967. $productIdArr = explode(',', $orderProductId);
  968. $refundProduct = [];
  969. foreach ($productIdArr as $orderProductId) {
  970. $tmp['order_product_id'] = $orderProductId;
  971. $tmp['order_id'] = $order_id;
  972. $tmp['user_id'] = $this->auth->id;
  973. $tmp['refund_id'] = $orderRefund['id'];
  974. $tmp['createtime'] = time();
  975. $refundProduct[] = $tmp;
  976. }
  977. (new OrderRefundProduct)->insertAll($refundProduct);
  978. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  979. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  980. $order->save();
  981. Db::commit();
  982. } catch (Exception $e) {
  983. Db::rollback();
  984. $this->error($e->getMessage());
  985. }
  986. $this->success('已申请', 1);
  987. }
  988. /**
  989. * @ApiTitle (售后发货)
  990. * @ApiSummary (售后发货)
  991. * @ApiMethod (POST)
  992. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  993. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  994. * @ApiParams (name="order_id", type="string",required=true, description="订单id")
  995. * @ApiParams (name="express_number", type="string",required=true, description="寄货物流单号")
  996. * @ApiReturn ({"code":1,"msg":"","data":1})
  997. *
  998. */
  999. public function refundDelivery()
  1000. {
  1001. $orderId = input('order_id');
  1002. $expressNumber = input('express_number');
  1003. if (!$expressNumber) {
  1004. $this->error(__('Please fill in the express number'));
  1005. }
  1006. // $orderId = Hashids::decodeHex($orderId);
  1007. $orderModel = new \addons\unishop\model\Order();
  1008. $order = $orderModel
  1009. ->where(['id' => $orderId, 'user_id' => $this->auth->id])
  1010. ->with(['refund'])->find();
  1011. if (!$order || !$order->refund) {
  1012. $this->error(__('Order not exist'));
  1013. }
  1014. try {
  1015. Db::startTrans();
  1016. $order->refund->express_number = $expressNumber;
  1017. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  1018. if ($order->refund->save() && $order->save()) {
  1019. Db::commit();
  1020. } else {
  1021. throw new Exception(__('Operation failed'));
  1022. }
  1023. } catch (Exception $e) {
  1024. Db::rollback();
  1025. $this->success($e->getMessage());
  1026. }
  1027. $this->success('', 1);
  1028. }
  1029. /**
  1030. * @ApiTitle (快递查询)
  1031. * @ApiSummary (快递查询)
  1032. * @ApiMethod (POST)
  1033. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  1034. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  1035. * @ApiParams (name="express", type="string",required=true, description="寄货物流公司")
  1036. * @ApiParams (name="expresssn", type="string",required=true, description="寄货物流单号")
  1037. * @ApiReturn ({"code":1,"msg":"","data":1})
  1038. *
  1039. */
  1040. public function express() {
  1041. $params = input();
  1042. if (!class_exists(\addons\expressquery\library\Expressquery::class)) {
  1043. $this->error('请先安装插件《物流信息接口》', []);
  1044. }
  1045. $info = get_addon_info('expressquery');
  1046. if ($info['state'] == 0) {
  1047. $this->error('请开启插件《物流信息接口》', []);
  1048. }
  1049. $expModle = new \addons\expressquery\library\Expressquery();
  1050. $list = $expModle->getExpressList($params['express'], $params['expresssn']);
  1051. if ($list['code'] == 0) {
  1052. $this->error($list['msg']);
  1053. }
  1054. $expressInfo = Db::name('expressquery')->where(['express' => $params['express']])->find();
  1055. $this->success('', [
  1056. 'message' => $list['data'] ?? [],
  1057. 'company' => $expressInfo['name'] ?? '快递单号'
  1058. ]);
  1059. }
  1060. //出示核销码
  1061. public function show_hexiao_qrcode(){
  1062. }
  1063. //退订
  1064. public function tuiding(){}
  1065. }