123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973 |
- <?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;
- class Order extends Base
- {
-
- protected $frequently = ['getorders'];
- protected $noNeedLogin = ['count'];
-
- public function create()
- {
- $productId = input('id', 0);
- try {
- $user_id = $this->auth->id;
-
-
- $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);
-
- if (empty($productData) || !$productData) {
- $this->error(__('Product not exist'));
- }
-
-
-
-
-
- $order_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);
- }
- $coupon = Db::name('unishop_coupon_user')->alias('cu')
- ->field(['c.id','c.title','c.least','c.value','c.starttime','c.endtime'])
- ->join('unishop_coupon c','cu.coupon_id = c.id','LEFT')
- ->where('cu.user_id',$this->auth->id)
- ->where('cu.status',0)
- ->where('c.deletetime',NULL)
- ->where('c.switch',1)
- ->where('c.starttime','<',time())
- ->where('c.endtime','>',time())
- ->where('c.least','>=',$order_price)
- ->select();
- $this->success('', [
- 'product' => $productData,
- 'coupon' => $coupon,
- 'order_price' => $order_price,
- 'total_price' => $order_price
- ]);
- } catch (Exception $e) {
- $this->error($e->getMessage(), false);
- }
- }
-
- public function submit()
- {
- $data = input();
- 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 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['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->save();
- $this->success('取消成功', 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_delivered == 0) {
- $this->error('未发货,不能确认收货');
- }
- $order->have_received = time();
- $order->save();
- $this->success('已确认收货', true);
- }
-
- public function comment()
- {
- $rate = input('rate', 5);
- $anonymous = input('anonymous', 0);
- $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->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(__('未收货,不可评价'));
- }
- $result = false;
- try {
- $evaluate = new Evaluate();
- $evaluate->user_id = $this->auth->id;
- $evaluate->order_id = $order_id;
- $evaluate->product_id = $product_id;
- $evaluate->rate = $rate;
- $evaluate->anonymous = $anonymous;
- $evaluate->comment = $comment;
- $evaluate->spec = $orderProduct->spec;
- $result = $evaluate->save();
- if ($result) {
- $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,order_id,image,number,price,spec,title,product_id');
- },
- 'extend' => function ($query) {
- $query->field('id,order_id,address_id,address_json,express_number,express_company');
- },
-
- ])
- ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if ($order) {
- $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
-
-
-
-
-
- foreach ($order['products'] as &$product) {
- $product['image'] = Config::getImagesFullUrl($product['image']);
-
- }
- unset($order['extend']);
-
- $week_data = [
- 0 => '周日',
- 1 => '周一',
- 2 => '周二',
- 3 => '周三',
- 4 => '周四',
- 5 => '周五',
- 6 => '周六',
- 7 => '周日',
- ];
- $order['bookdate'] = date('Y-m-d H:i',$order['booktime']) .' '. $week_data[date('w',$order['booktime'])];
-
- $order['order_refund_rule'] = config('site.order_refund_rule');
-
- $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_paid'] == 0) {
- $this->error(__('订单未支付,可直接取消,无需申请售后'));
- }
- $amount = input('amount', 0);
- $serviceType = input('service_type');
- $receivingStatus = input('receiving_status');
- $reasonType = input('reason_type');
- $refundExplain = input('refund_explain');
- $orderProductId = input('order_product_id');
- 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->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'] ?? '快递单号'
- ]);
- }
-
- public function show_hexiao_qrcode(){
- }
-
- public function tuiding(){}
- }
|