1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252 |
- <?php
- namespace addons\unishop\controller;
- use addons\unishop\extend\Hashids;
- use addons\unishop\model\Area;
- use addons\unishop\model\Config;
- use addons\unishop\model\Evaluate;
- use addons\unishop\model\Product;
- use app\admin\model\unishop\Coupon as CouponModel;
- use addons\unishop\model\DeliveryRule as DeliveryRuleModel;
- use addons\unishop\model\OrderRefund;
- use app\admin\model\unishop\OrderRefundProduct;
- use think\Db;
- use think\Exception;
- use addons\unishop\model\Address as AddressModel;
- use think\Hook;
- use think\Loader;
- use addons\epay\library\Service;
- use app\common\server\WechatMiniExpressSendSyncServer;
- class Order extends Base
- {
-
- protected $frequently = ['getorders','getOrders','detail'];
- protected $noNeedLogin = ['count'];
-
- public function create()
- {
- $productId = input('id', 0);
- try {
- $user_id = $this->auth->id;
-
- if ($productId) {
- $number = input('number',1,'intval');
- $number = intval($number);
- $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
- $product = (new Product)->where(['id' => $productId, 'switch' => Product::SWITCH_ON, 'deletetime' => null])->find();
-
- $spec = input('spec', '');
- $productData[0] = $product->getDataOnCreateOrder($spec,$number);
- } else {
-
- $cart = input('cart');
- $carts = (new \addons\unishop\model\Cart)
- ->whereIn('id', $cart)
- ->with(['product'])
- ->order(['id' => 'desc'])
- ->select();
- foreach ($carts as $cart) {
- if ($cart->product instanceof Product) {
- $productData[] = $cart->product->getDataOnCreateOrder($cart->spec ? $cart->spec : '', $cart->number);
- }
- }
- }
- if (empty($productData) || !$productData) {
- $this->error(__('Product not exist'));
- }
-
-
-
-
-
-
- $order_price = '0';
- $delivery_price = '0';
- foreach ($productData as &$product) {
- $product['image'] = Config::getImagesFullUrl($product['image']);
- $product['sales_price'] = round($product['sales_price'], 2);
- $product['market_price'] = round($product['market_price'], 2);
- $order_price = bcadd($order_price,$product['total_price'],2);
- }
- $this->success('', [
- 'product' => $productData,
- 'order_price' => $order_price,
- 'delivery_price' => $delivery_price,
- 'total_price' => bcadd($order_price,$delivery_price,2),
- 'wallet' => Db::name('user_wallet')->where('user_id',$this->auth->id)->find(),
- ]);
- } catch (Exception $e) {
- $this->error($e->getMessage(), false);
- }
- }
-
- public function submit()
- {
- $data = input();
- $pay_type = input('pay_type','wechat');
- if(!in_array($pay_type,['wechat','alipay','wallet'])){
- $this->error();
- }
- try {
- $validate = Loader::validate('\\addons\\unishop\\validate\\Order');
- if (!$validate->check($data, [], 'submit')) {
- throw new Exception($validate->getError());
- }
- Db::startTrans();
-
- if (empty(Hook::get('create_order_before'))) {
- Hook::add('create_order_before', 'addons\\unishop\\behavior\\Order');
- }
-
- if (empty(Hook::get('create_order_after'))) {
- Hook::add('create_order_after', 'addons\\unishop\\behavior\\Order');
- }
- $orderModel = new \addons\unishop\model\Order();
- $result = $orderModel->createOrder($this->auth->id, $data);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage(), false);
- }
- $this->success('', $result);
- }
-
- public function getDelivery()
- {
- $cityId = input('city_id', 0);
- $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
- $this->success('', $delivery['list']);
- }
-
- public function getOrders()
- {
-
- $type = input('type', 0);
- $page = input('page', 1);
- $pagesize = input('pagesize', 10);
- try {
- $orderModel = new \addons\unishop\model\Order();
- $result = $orderModel->getOrdersByType($this->auth->id, $type, $page, $pagesize);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('', $result);
- }
-
- public function getOrders_num(){
-
- $status = [
- [
- 'status' => 1,
- 'status_text' => '待付款',
- 'number' => 0,
- ],
- [
- 'status' => 3,
- 'status_text' => '待核销',
- 'number' => 0,
- ],
- [
- 'status' => 4,
- 'status_text' => '待收货',
- 'number' => 0,
- ],
- [
- 'status' => 5,
- 'status_text' => '已完成',
- 'number' => 0,
- ],
- [
- 'status' => 40,
- 'status_text' => '待评价',
- 'number' => 0,
- ],
- ];
- $orderModel = new \addons\unishop\model\Order();
- foreach($status as $key => $val){
- $val['number'] = $orderModel->getOrdersByType_num($this->auth->id,$val['status']);
- $status[$key] = $val;
- }
- $this->success(1, $status);
- }
-
- public function cancel()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- switch ($order['status']) {
- case \addons\unishop\model\Order::STATUS_REFUND:
- $this->error('此订单已退款,无法取消');
- break;
- case \addons\unishop\model\Order::STATUS_CANCEL:
- $this->error('此订单已取消, 无需再取消');
- break;
- }
- if ($order['have_paid'] != \addons\unishop\model\Order::PAID_NO) {
- $this->error('此订单已支付,无法取消');
- }
- if ($order['have_hexiao'] != \addons\unishop\model\Order::HEXIAO_NO) {
- $this->error('此订单已核销,无法取消');
- }
- if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
- $order->status = \addons\unishop\model\Order::STATUS_CANCEL;
- $order->canceltime = time();
- $order->save();
-
-
- $this->success('取消成功', true);
- }
- }
-
- public function payed_cancel()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- switch ($order['status']) {
- case \addons\unishop\model\Order::STATUS_REFUND:
- $this->error('此订单已退款,无法取消');
- break;
- case \addons\unishop\model\Order::STATUS_CANCEL:
- $this->error('此订单已取消, 无需再取消');
- break;
- }
- if ($order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
- $this->error('未支付订单,直接取消即可');
- }
- if ($order['have_hexiao'] != \addons\unishop\model\Order::HEXIAO_NO) {
- $this->error('此订单已核销,无法退订');
- }
- Db::startTrans();
- if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_hexiao'] == \addons\unishop\model\Order::HEXIAO_NO) {
- $refund_amount = $order['total_price'];
- $refund_status = 3;
-
- $params = [
- 'refund_status' => $refund_status,
- 'updatetime' => time(),
- 'refund_amount' => $refund_amount,
- 'status' => \addons\unishop\model\Order::STATUS_CANCEL,
- 'canceltime' => time(),
- ];
- if($refund_status == 3) {
- $params['had_refund'] = time();
- }
- $result = Db::name('unishop_order')->where(['id' => $order_id])->update($params);
- if (!$result) {
- Db::rollback();
- $this->error('取消失败');
- }
-
- $coupon_user_id = Db::name('unishop_order_extend')->where('order_id',$order_id)->value('coupon_user_id');
- if(!empty($coupon_user_id)){
- Db::name('unishop_coupon_user')->where('id',$coupon_user_id)->where('user_id',$this->auth->id)->update(['status'=>0]);
- }
-
- if($refund_status == 3 && $refund_amount > 0){
- $order = Db::name('unishop_order')->where('id',$order_id)->find();
- if($order['pay_type'] == 2){
- $wallet = new \app\common\model\Wallet();
- $wallet_rs = $wallet->lockChangeAccountRemain($order['user_id'],'money',$refund_amount,32,'商城取消订单退款'.$order['out_trade_no'],'unishop_order',$order_id);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- }elseif($order['pay_type'] == 3){
- $refund_result = $this->old_refund($order,$refund_amount);
- if($refund_result !== true){
- Db::rollback();
- $this->error($refund_result);
- }
- }
- }
- }
- Db::commit();
- $this->success('退订成功', true);
- }
-
- private function old_refund($order, $refund_price)
- {
- $table = 'unishop_order';
- $remark = '订单退款';
- if($order['pay_type'] == 3){
- $order['pay_type'] = 'wechat';
- }
- if($order['pay_type'] == 4){
- $order['pay_type'] = 'alipay';
- }
-
- $refund_data = [
- 'order_id' => $order['id'],
- 'out_refund_no'=> createUniqueNo('R'),
- 'pay_fee' => $order['total_price'],
- 'refund_price' => $refund_price,
- 'pay_type' => $order['pay_type'],
- 'status' => 0,
- 'createtime' => time(),
- 'updatetime' => time(),
- 'table' => $table,
- 'table_id' => $order['id'],
- ];
- $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
- if(!$refund_log_id){
- return '退款失败';
- }
- if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
-
-
- $order_data = [
- 'out_trade_no' => $order['pay_out_trade_no']
- ];
- if ($order['pay_type'] == 'wechat') {
- $total_fee = $order['total_price'] * 100;
- $refund_fee = $refund_price * 100;
- $order_data = array_merge($order_data, [
- 'out_refund_no' => $refund_data['out_refund_no'],
- 'total_fee' => $total_fee,
- 'refund_fee' => $refund_fee,
- 'refund_desc' => $remark,
- ]);
- } else {
- $order_data = array_merge($order_data, [
- 'out_request_no' => $refund_data['out_refund_no'],
- 'refund_amount' => $refund_price,
- ]);
- }
-
- if ($order['pay_type'] == 'wechat') {
- $wxpay = new \app\common\library\Wxpay;
- $result = $wxpay->WxPayRefund($order_data);
-
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
- return true;
- } else {
- return $result['return_msg'];
- }
- } else {
- $result = Service::submitRefund($order['total_price'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');
- if($result['code'] == '10000'){
- Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
- return true;
- }else{
- return $result['msg'];
- }
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- return true;
- }
-
- public function delete()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL) {
- $this->error('只能删除已取消或已退货的订单');
- }
- if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND && $order['refund_status'] == \addons\unishop\model\Order::REFUND_STATUS_APPLY) {
- $this->error('订单退款中,不可删除订单');
- }
- $order->delete();
- $this->success('删除成功', true);
- }
-
- public function received()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- if ($order->have_hexiao == 0) {
- $this->error('未核销,不能确认收货');
- }
- $order->have_received = time();
- $order->save();
- $this->success('已确认收货');
- }
-
- public function wechatSyncCheck(){
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- $result = WechatMiniExpressSendSyncServer::wechatSyncCheck($order);
- if (! $result) {
- $this->success(1,'');
- }
- $this->success(1,$result);
- }
-
- public function comment()
- {
- $rate = input('rate', 5);
- $anonymous = input('anonymous', 0);
- $images = input('images','','trim');
- $comment = input('comment');
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $product_id = input('product_id');
- $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
- $orderProductModel = new \addons\unishop\model\OrderProduct();
- $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();
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$orderProduct || !$order) {
- $this->error(__('Order not exist'));
- }
- if ($order->have_received == $orderModel::RECEIVED_NO) {
- $this->error(__('未确认收货,不可评价'));
- }
- $images_arr = explode(',',$images);
- if(count($images_arr) > 5){
- $this->error('最多上传5张图片');
- }
- $result = false;
- try {
- $evaluate = new Evaluate();
- $evaluate->user_id = $this->auth->id;
- $evaluate->order_id = $order_id;
- $evaluate->order_product_id = $orderProduct->order_product_id;
- $evaluate->product_id = $product_id;
- $evaluate->rate = $rate;
- $evaluate->anonymous = $anonymous;
- $evaluate->images = $images;
- $evaluate->comment = $comment;
- $evaluate->spec = $orderProduct->spec;
- $result = $evaluate->save();
- $orderProduct = Db::name('unishop_order_product')->where(['order_id' => $order_id])->count();
- $orderevaluate = Db::name('unishop_evaluate')->where(['order_id' => $order_id])->count();
- if ($result && ($orderProduct == $orderevaluate)) {
- $order->have_commented = time();
- $order->save();
- }
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Thanks for the evaluation'));
- } else {
- $this->error(__('Evaluation failure'));
- }
- }
-
- public function count()
- {
- if (!$this->auth->isLogin()) {
- $this->error('');
- }
- $order = new \addons\unishop\model\Order();
- $list = $order
- ->where([
- 'user_id' => $this->auth->id,
- ])
- ->where('status', '<>', \addons\unishop\model\Order::STATUS_CANCEL)
- ->where(function ($query) {
- $query
- ->whereOr([
- 'have_paid' => \addons\unishop\model\Order::PAID_NO,
- 'have_delivered' => \addons\unishop\model\Order::DELIVERED_NO,
- 'have_received' => \addons\unishop\model\Order::RECEIVED_NO,
- 'have_commented' => \addons\unishop\model\Order::COMMENTED_NO
- ])
- ->whereOr('refund_status', '>', \addons\unishop\model\Order::REFUND_STATUS_NONE);
- })
- ->field('have_paid,have_delivered,have_received,have_commented,refund_status,had_refund')
- ->select();
- $data = [
- 'unpaid' => 0,
- 'undelivered' => 0,
- 'unreceived' => 0,
- 'uncomment' => 0,
- 'refund' => 0
- ];
- foreach ($list as $item) {
- switch (true) {
- case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] > 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
- $data['uncomment']++;
- break;
- case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
- $data['unreceived']++;
- break;
- case $item['have_paid'] > 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
- $data['undelivered']++;
- break;
- case $item['have_paid'] == 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
- $data['unpaid']++;
- break;
- case $item['refund_status'] > 0 && $item['had_refund'] == 0 && $item['refund_status'] != 3:
- $data['refund']++;
- break;
- }
- }
- $this->success('', $data);
- }
-
- public function detail()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- try {
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel
- ->with([
- 'products' => function ($query) {
- $query->field('id,id as order_product_id,title,image,number,price,spec,order_id,product_id');
- },
- 'evaluate' => function ($query) {
- $query->field('id,order_id,order_product_id,product_id');
- }
- ])
- ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if ($order) {
- $order = $order->append(['state', 'paidtime', 'deliveredtime', 'hexiaotime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
- $order['state_text'] = $orderModel->getStatetextAttr($order['state'],$order['refund_status']);
-
- $evaluate = array_column($order['evaluate'], 'order_product_id');
- foreach ($order['products'] as &$product) {
- $product['image'] = Config::getImagesFullUrl($product['image']);
- if (in_array($product['order_product_id'], $evaluate)) {
- $product['evaluate'] = true;
- } else {
- $product['evaluate'] = false;
- }
- }
- unset($order['evaluate']);
- unset($order['extend']);
- unset($order['pay_out_trade_no']);
-
- $qrcode_string = 'order_hexiao_no|' . $order['out_trade_no'];
- $order['order_hexiao_qrcode'] = httpurllocal($this->inviteimage($qrcode_string));
- }
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('', $order);
- }
-
- private function inviteimage($introcode) {
- $params['text'] = $introcode;
- $qrcode_service = \addons\qrcode\library\Service::qrcode($params);
- $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
- if (!is_dir($qrcodePath)) {
- @mkdir($qrcodePath);
- }
- if (is_really_writable($qrcodePath)) {
- $filename = md5(implode('', $params)) . '.png';
- $filePath = $qrcodePath . $filename;
- $qrcode_service->writeFile($filePath);
- }
- return '/uploads/qrcode/' . $filename;
- }
-
- public function refundInfo()
- {
- $order_id = input('order_id');
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel
- ->with([
- 'products' => function ($query) {
- $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
- },
- 'refund',
- 'refundProducts'
- ])
- ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
- ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- $order = $order->append(['refund_status_text'])->toArray();
- foreach ($order['products'] as &$product) {
- $product['image'] = Config::getImagesFullUrl($product['image']);
- $product['choose'] = 0;
-
- if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
- foreach ($order['refund_products'] as $refundProduct) {
- if ($product['order_product_id'] == $refundProduct['order_product_id']) {
- $product['choose'] = 1;
- }
- }
- }
- }
- unset($order['refund_products']);
- $this->success('', $order);
- }
-
- public function refund()
- {
- $order_id = input('order_id');
- $order_id = Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
-
- if ($order['have_delivered'] == 0) {
- $this->error(__('订单未发货,可直接取消,无需申请售后'));
- }
- if($order->status == 0){
- $this->error('当前订单不能申请退款');
- }
- if($order->status == -1){
- $this->error('当前订单已经申请过退款');
- }
-
- $amount = $order->total_price;
- $serviceType = input('service_type',1);
- $receivingStatus = input('receiving_status',1);
- $reasonType = input('reason_type','');
- $refundExplain = input('refund_explain','');
- $images = input('images','');
- $orderProductId = Db::name('unishop_order_product')->where('order_id',$order_id)->column('product_id');
- $orderProductId = implode(',', $orderProductId);
- if (!$orderProductId) {
- $this->error(__('Please select goods'));
- }
- if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
- $this->error(__('Please select goods status'));
- }
- if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
- $this->error(__('Please select service type'));
- }
- if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
- if (!$amount) {
- $this->error(__('Please fill in the refund amount'));
- }
- }
- try {
- Db::startTrans();
- $orderRefund = new OrderRefund();
- $orderRefund->user_id = $this->auth->id;
- $orderRefund->order_id = $order_id;
- $orderRefund->receiving_status = $receivingStatus;
- $orderRefund->service_type = $serviceType;
- $orderRefund->reason_type = $reasonType;
- $orderRefund->amount = $amount;
- $orderRefund->refund_explain = $refundExplain;
- $orderRefund->images = $images;
- $orderRefund->save();
- $productIdArr = explode(',', $orderProductId);
- $refundProduct = [];
- foreach ($productIdArr as $orderProductId) {
- $tmp['order_product_id'] = $orderProductId;
- $tmp['order_id'] = $order_id;
- $tmp['user_id'] = $this->auth->id;
- $tmp['refund_id'] = $orderRefund['id'];
- $tmp['createtime'] = time();
- $refundProduct[] = $tmp;
- }
- (new OrderRefundProduct)->insertAll($refundProduct);
- $order->status = \addons\unishop\model\Order::STATUS_REFUND;
- $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
- $order->save();
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('已申请', 1);
- }
-
- public function refundDelivery()
- {
- $orderId = input('order_id');
- $expressNumber = input('express_number');
- if (!$expressNumber) {
- $this->error(__('Please fill in the express number'));
- }
- $orderId = Hashids::decodeHex($orderId);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel
- ->where(['id' => $orderId, 'user_id' => $this->auth->id])
- ->with(['refund'])->find();
- if (!$order || !$order->refund) {
- $this->error(__('Order not exist'));
- }
- try {
- Db::startTrans();
- $order->refund->express_number = $expressNumber;
- $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
- if ($order->refund->save() && $order->save()) {
- Db::commit();
- } else {
- throw new Exception(__('Operation failed'));
- }
- } catch (Exception $e) {
- Db::rollback();
- $this->success($e->getMessage());
- }
- $this->success('', 1);
- }
-
- public function express() {
- $params = input();
- if (!class_exists(\addons\expressquery\library\Expressquery::class)) {
- $this->error('请先安装插件《物流信息接口》', []);
- }
- $info = get_addon_info('expressquery');
- if ($info['state'] == 0) {
- $this->error('请开启插件《物流信息接口》', []);
- }
- $expModle = new \addons\expressquery\library\Expressquery();
- $list = $expModle->getExpressList($params['express'], $params['expresssn']);
- if ($list['code'] == 0) {
- $this->error($list['msg']);
- }
- $expressInfo = Db::name('expressquery')->where(['express' => $params['express']])->find();
- $this->success('', [
- 'message' => $list['data'] ?? [],
- 'company' => $expressInfo['name'] ?? '快递单号'
- ]);
- }
- }
|