CardSecretController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace addons\CardSecret\backend\controllers;
  3. use addons\CardSecret\common\listeners\CardSecretListener;
  4. use addons\CardSecret\common\models\CardSecret;
  5. use addons\CardSecret\common\models\CardSecretImportLog;
  6. use common\enums\StatusEnum;
  7. use common\enums\RabbitMqEnum;
  8. use Yii;
  9. use yii\db\Exception;
  10. use common\components\export\CsvTool;
  11. use common\enums\DownloadEnum;
  12. /**
  13. * 默认控制器
  14. *
  15. * Class DefaultController
  16. * @package addons\CardSecret\backend\controllers
  17. */
  18. class CardSecretController extends BaseController
  19. {
  20. public $enableCsrfValidation = false;
  21. /**
  22. * 首页
  23. *
  24. * @return string
  25. */
  26. public function actionIndex()
  27. {
  28. $retuest = Yii::$app->request;
  29. if ($retuest->isAjax) {
  30. if ($retuest->isGet) {
  31. $get = Yii::$app->request->get();
  32. // 检查提交的参数
  33. $res = Yii::$app->services->validate->validate($get,[
  34. [['cate_id'],'required'],
  35. [['id','keyword'], 'safe'],
  36. [['limit'],'default','value' => '15'],
  37. ]);
  38. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  39. $where = [];
  40. if(!empty($get['keyword'])) $where[] = ['like', 'name', $get['keyword']];
  41. $where[] = ['=', 'cate_id', $get['cate_id']];
  42. $where[] = ['=', 'mall_id', $this->mall_id];
  43. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  44. $order = 'id desc';
  45. $params = array('where' => $where, 'order'=>$order, 'limit' => $get['limit']);
  46. $list = CardSecret::listPage($params, false, true);
  47. $this->successStr('获取成功', $list);
  48. }
  49. } else {
  50. return $this->render($this->action->id);
  51. }
  52. }
  53. /**
  54. * 等级编辑
  55. * @Author: ltm
  56. * @DateTime: 2022/5/6 0021 11:29
  57. * @Copyright: copyright (c) 2021 广东七件事集团
  58. * @return mixed|string|void
  59. */
  60. public function actionEdit()
  61. {
  62. try {
  63. $request = Yii::$app->request;
  64. if ($request->isAjax) {
  65. // 提交等级内容
  66. if ($request->isPost) {
  67. $params = Yii::$app->request->post();
  68. $res = Yii::$app->services->validate->validate($params, [
  69. [['name','id','password','status','cate_id'], 'safe'],
  70. ]);
  71. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  72. $params = CardSecret::exceptNullVal($params);
  73. if(!empty($params['id'])) $params['updated_at'] = time();//更新
  74. $params['create_at'] = time();
  75. if(!$params['name']) unset($params['name']);
  76. $params['mall_id'] = $this->mall_id;
  77. if(isset($params['status']) && !in_array($params['status'],[StatusEnum::DISABLED,StatusEnum::ENABLED,StatusEnum::DELETE])) throw new Exception("参数错误");;
  78. $res = CardSecret::setData($params);
  79. if ($res === false) throw new Exception(CardSecret::getStaticError());
  80. return $this->success('操作成功');
  81. }
  82. }
  83. } catch (\Exception $e) {
  84. return $this->error($e->getMessage(), []);
  85. }
  86. return $this->render($this->action->id);
  87. }
  88. /**
  89. * @Author: ltm
  90. * @DateTime: 2022/5/6 0021 11:29
  91. * @Copyright: copyright (c) 2021 广东七件事集团
  92. * @param $data
  93. * @return string
  94. */
  95. public function actionDownloadTemplate()
  96. {
  97. $csv = new CsvTool();
  98. $csv->filename = '卡密导入模板';
  99. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap(DownloadEnum::TYPE_HAND_BOUNS);
  100. $csv->export_mode = CsvTool::EXPORT_MODE_SYNC;
  101. $csv->header = ['卡号', '卡密'];
  102. $csv->export(true);
  103. }
  104. public function actionImport(){
  105. if (\Yii::$app->request->isAjax) {
  106. if (\Yii::$app->request->isPost) {
  107. $params = Yii::$app->request->post();
  108. $rules = [
  109. [['file','cate_id'], 'required'],
  110. [['file'], 'string'],
  111. ];
  112. $res = Yii::$app->services->validate->validate($params, $rules);
  113. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  114. try {
  115. $params['operator_id'] = $this->admin->id;
  116. $params['operator_name'] = $this->admin->account;
  117. $params['mall_id'] = $this->mall_id;
  118. $model = CardSecretImportLog::setData($params);
  119. if ($model == false) throw new Exception(CardSecretImportLog::getStaticError());
  120. // 提交到异步处理
  121. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['id' => $model->id], CardSecretListener::class, 'async_handle');
  122. $this->success('提交成功,正在处理异步处理');
  123. } catch (Exception $e) {
  124. $this->error('提交失败:' . $e->getMessage());
  125. }
  126. }
  127. }
  128. }
  129. }