Order.php 54 KB

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