| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- namespace addons\Printer\backend\controllers;
- use addons\Mch\common\models\Mch;
- use addons\Printer\common\logic\PrintOrder;
- use addons\Printer\common\models\Printer;
- use addons\Printer\common\models\PrinterTemplate;
- use addons\Printer\common\enums\PrinterEnum;
- use addons\Store\common\models\Store;
- use common\enums\AddonsEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- use Exception;
- use Yii;
- use addons\Printer\common\logic\PrinterBase;
- use common\listeners\order\OrderCreateListener;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Printer\backend\controllers
- */
- class DefaultController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- * @author huangpei
- * @return mixed
- */
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax && Yii::$app->request->isGet){
- $where = [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]];
- $params = [
- 'limit' => 10,
- 'where' => $where,
- 'select' => ['id', 'type', 'name', 'status'],
- 'order' => 'created_at desc'
- ];
- $list = Printer::listPage($params);
- foreach($list['list'] as &$val){
- $val['type'] = json_decode($val['type'],true);
- }
- $data['list'] = $list;
- $data['type'] = PrinterEnum::getPrinterType();
- return $this->success('操作成功', $data);
- }
- return $this->render('index');
- }
- /**
- * 编辑
- * @author huangpei
- * @return string
- */
- public function actionEdit(){
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name','set_type'], 'required'],
- [['id', 'status','printer_type', 'type','store_id','mch_id', 'terminal','secret_key','api_key', 'user_id', 'url', 'source','client_id'], 'safe'],
- [['terminal','secret_key','api_key', 'user_id', 'url', 'source'],'default', 'value'=>''],
- [['order_printer_config','pay_printer_config','confirm_printer_config','system_printer_config'],'default','value'=>'{"is_printer":0,"template_id":0,"num":0}']
- ]);
- $params['mall_id'] = $this->mall_id;
- if(empty($params['printer_type'])) unset($params['printer_type']);
- if(empty($params['store_id'])) unset($params['store_id']);
- if(empty($params['mch_id'])) unset($params['mch_id']);
- if(empty($params['client_id'])) unset($params['client_id']);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- if(!empty($params['client_id'])&&!empty($params['terminal'])&&!empty($params['api_key'])){
- $result = Printer::getPrintWidth($params['client_id'],$params['terminal'],$params['api_key']);
- if($result->error==0){
- $params['print_width'] = $result->body->print_width;
- }
- }
- $res = Printer::setData($params);
- if (!$res) return $this->error(Printer::getStaticError());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功');
- }
- /**
- * 删除
- * @author huangpei
- * @return string
- */
- public function actionDeletePrinter(){
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id','status'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $res = Printer::setData($params);
- if (!$res) return $this->error(Printer::getStaticError());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功');
- }
- /**
- * 添加打印机
- * @author huangpei
- * @return mixed
- */
- public function actionAddPrinter(){
- return $this->render('add');
- }
- /**
- * 编辑打印机
- * @author huangpei
- * @return mixed
- */
- public function actionEditPrinter(){
- if(Yii::$app->request->isAjax && Yii::$app->request->isGet){
- $printer_info = Printer::getOne([['id' => Yii::$app->request->get('id')]],true);
- $template_list = PrinterTemplate::getTemplateAllByMallId($this->mall_id);
- $printer_info['source'] = json_decode($printer_info['source'],true);
- $printer_info['pay_printer_config'] = json_decode($printer_info['pay_printer_config'],true);
- $printer_info['order_printer_config'] = json_decode($printer_info['order_printer_config'],true);
- $printer_info['system_printer_config'] = json_decode($printer_info['system_printer_config'],true);
- $printer_info['confirm_printer_config'] = json_decode($printer_info['confirm_printer_config'],true);
- if(empty($printer_info['store_id'])) $printer_info['store_id'] = '';
- if(empty($printer_info['mch_id'])) $printer_info['mch_id'] = '';
- return $this->success('操作成功',['printer_info'=>$printer_info,'template_list'=>$template_list]);
- }
- return $this->render('edit');
- }
- /**
- * 获取门店列表
- * @author huangpei
- * @return mixed
- */
- public function actionStoreList(){
- $data = [];
- if(Yii::$app->request->isAjax && Yii::$app->request->isGet){
- if(MallAddons::isInstall($this->mall_id, PrinterEnum::ADDONS_STORE) != 0){
- $where = [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]];
- $params['where'] = $where;
- $params['order'] = 'id desc';
- $params['order'] = 'id,store_name';
- $data = Store::lists($params,true);
- }
- }
- return $this->success('操作成功',$data);
- }
- public function actionMchList(){
- $data = [];
- if(Yii::$app->request->isAjax && Yii::$app->request->isGet){
- if(MallAddons::isInstall($this->mall_id, PrinterEnum::ADDONS_MCH) != 0){
- $where = [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]];
- $params['where'] = $where;
- $params['order'] = 'id desc';
- $params['order'] = 'id,store_name';
- $data = Mch::lists($params,true);
- }
- }
- return $this->success('操作成功',$data);
- }
- /**
- * 获取打印机模板
- * @author huangpei
- * @return mixed
- */
- public function actionGetTemplateAllByMallId(){
- $template_list = PrinterTemplate::getTemplateAllByMallId($this->mall_id);
- return $this->success('操作成功',$template_list);
- }
- /**
- * 获取打印机模板
- * @author huangpei
- * @return mixed
- */
- public function actionTemplate(){
- if(Yii::$app->request->isAjax && Yii::$app->request->isGet){
- $where = [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]];
- $params = [
- 'limit' => 10,
- 'where' => $where,
- 'select' => ['id', 'name', 'ticket_nam', 'status'],
- 'order' => 'created_at desc'
- ];
- $list = PrinterTemplate::listPage($params);
- return $this->success('操作成功', $list);
- }
- return $this->render('template');
- }
- /**
- * 设置状态
- * @author huangpei
- * @return mixed
- */
- public function actionSetTemplateStatus(){
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['status','id'], 'required'],
- [['id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- if($params['status']==-1){
- if(Printer::checkTemplateIsUse($this->mall_id,$params['id'])){
- return $this->error('模板已被使用,不可删除');
- }
- //var_dump(Printer::checkTemplateIsUse($this->mall_id,$params['id']),55);
- }
- $res = PrinterTemplate::setData($params);
- if (!$res) return $this->error(Printer::getStaticError());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功');
- }
- /**
- * 添加模板
- * @author huangpei
- * @return mixed
- */
- public function actionAddTemplate(){
- return $this->render('add_template');
- }
- /**
- * 添加与编辑模板
- * @author huangpei
- * @return mixed
- */
- public function actionEditTemplate(){
- if(Yii::$app->request->isAjax){
- if(Yii::$app->request->isGet){
- $template_info = PrinterTemplate::getOne([['id' => Yii::$app->request->get('id')]],true);
- $template_info['goods_info'] = $template_info['goods_info'] ? json_decode($template_info['goods_info'], true) : [];
- $template_info['remarks_info'] = $template_info['remarks_info'] ? json_decode($template_info['remarks_info'], true) : [];
- $template_info['buyer_info'] = $template_info['buyer_info'] ? json_decode($template_info['buyer_info'], true) : [];
- $template_info['mall_info'] = $template_info['mall_info'] ? json_decode($template_info['mall_info'], true) : [];
- $template_info['head_info'] = empty($template_info['head_info']) ? [] : [1];
- $template_info['price_info'] = empty($template_info['price_info']) ? [] : [$template_info['price_info']];
- $template_info['pickup_code'] = empty($template_info['pickup_code']) ? [] : [1];
- return $this->success('操作成功',['template_info'=>$template_info]);
- }else{
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name','ticket_nam'], 'required'],
- [['id','head_info', 'pickup_code', 'remarks_info','buyer_info','goods_info','price_info','mall_info','qrcode_url','bottom_info'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['head_info'] = empty($params['head_info']) ? 0 : 1;
- $params['pickup_code'] = empty($params['pickup_code']) ? 0 : 1;
- $params['goods_info'] = empty($params['goods_info']) ? '[]' : json_encode($params['goods_info']);
- $params['price_info'] = empty($params['price_info']) ? 0 : $params['price_info'][0];
- $params['remarks_info'] = empty($params['remarks_info']) ? '[]' : json_encode($params['remarks_info']);
- $params['buyer_info'] = empty($params['buyer_info']) ? '[]' : json_encode($params['buyer_info']);
- $params['mall_info'] = empty($params['mall_info']) ? '[]' : json_encode($params['mall_info']);
- try {
- $params['mall_id'] = $this->mall_id;
- $res = PrinterTemplate::setData($params);
- if (!$res) return $this->error(Printer::getStaticError());
- return $this->success('操作成功',$params);
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
- return $this->render('edit_template',['id' => Yii::$app->request->get('id')]);
- }
- public function actionTest(){
- $res = Printer::checkPrinterStatus(5);
- return $this->success('操作成功',$res);
- //7814
- //$obj = new PrinterBase($this->mall_id, PrinterBase::ORDER);
- //$res = $obj->getPrinterData(1);
- //
- // $obj = new OrderCreateListener();
- // $print_data = [
- // 'store_id' => 0,
- // 'print_type' => 1,
- // 'order_id' => 1,
- // 'user_id' => 1,
- // 'mall_id' => 5
- // ];
- // $obj->printTicket($print_data);
- }
- /**
- * 打印机项目
- * @Author: xuane
- * @DateTime: 2021/12/2 0002 14:58
- * @Copyright: copyright (c) 2021
- * @return mixed|string|void
- */
- public function actionPrinter(){
- if (\Yii::$app->request->isAjax){
- if (\Yii::$app->request->isPost){
- $params = Yii::$app->request->post();
- $rules = [
- [['order_id'], 'required'],
- [['order_id'], 'integer'],
- [['order_type'], 'safe'],
- [['print_type'], 'required'],
- [['print_type'], 'integer'],
- ];
- $res = Yii::$app->services->validate->validate($params, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 提交到异步处理
- //Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['order_id' => $params['order_id'],'type'=>PrintOrder::PRINT_TYPE_SYSTEM], PrinterBase::class, 'async_handle');
- $order_type = null;
- if(isset($params['order_type']))$order_type = $params['order_type'];
- PrinterBase::async_handle(['order_id' => $params['order_id'],'type' => PrintOrder::PRINT_TYPE_SYSTEM,'order_type'=>$order_type, 'print_type' => $params['print_type']]);
- return $this->success('操作成功');
- }
- }
- }
- }
|