QrcodeController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace addons\Wechat\merchant\controllers;
  3. use Yii;
  4. use yii\web\Response;
  5. use common\enums\StatusEnum;
  6. use common\models\base\SearchModel;
  7. use common\traits\MerchantCurd;
  8. use addons\Wechat\common\models\Qrcode;
  9. use addons\Wechat\common\enums\QrcodeModelTypeEnum;
  10. /**
  11. * 微信二维码管理
  12. *
  13. * Class QrcodeController
  14. * @package addons\Wechat\merchant\controllers
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class QrcodeController extends BaseController
  18. {
  19. use MerchantCurd;
  20. /**
  21. * @var Qrcode
  22. */
  23. public $modelClass = Qrcode::class;
  24. /**
  25. * 首页
  26. *
  27. * @return string
  28. * @throws \yii\web\NotFoundHttpException
  29. */
  30. public function actionIndex()
  31. {
  32. $searchModel = new SearchModel([
  33. 'model' => $this->modelClass,
  34. 'scenario' => 'default',
  35. 'partialMatchAttributes' => ['name'], // 模糊查询
  36. 'defaultOrder' => [
  37. 'id' => SORT_DESC,
  38. ],
  39. 'pageSize' => $this->pageSize,
  40. ]);
  41. $dataProvider = $searchModel
  42. ->search(Yii::$app->request->queryParams);
  43. $dataProvider->query
  44. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  45. ->andWhere(['addon_name' => Yii::$app->params['addon']['name']])
  46. ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);
  47. return $this->render($this->action->id, [
  48. 'dataProvider' => $dataProvider,
  49. 'searchModel' => $searchModel
  50. ]);
  51. }
  52. /**
  53. * @return mixed|string|Response
  54. * @throws \yii\base\ExitException
  55. */
  56. public function actionAjaxEdit()
  57. {
  58. $id = Yii::$app->request->get('id');
  59. /** @var Qrcode $model */
  60. $model = $this->findModel($id);
  61. // ajax 校验
  62. $this->activeFormValidate($model);
  63. if ($model->load(Yii::$app->request->post())) {
  64. $model->isNewRecord && $model = Yii::$app->wechatService->qrcode->syncCreate($model);
  65. return $model->save()
  66. ? $this->redirect(['index'])
  67. : $this->message($this->getError($model), $this->redirect(['index']), 'error');
  68. }
  69. return $this->renderAjax($this->action->id, [
  70. 'model' => $model,
  71. ]);
  72. }
  73. /**
  74. * 删除全部过期的二维码
  75. *
  76. * @return mixed
  77. */
  78. public function actionDeleteAll()
  79. {
  80. if (Qrcode::deleteAll([
  81. 'and',
  82. ['model_type' => QrcodeModelTypeEnum::TEM],
  83. ['<', 'end_time', time()],
  84. ['merchant_id' => $this->getMerchantId()]
  85. ])) {
  86. return $this->message("删除成功", $this->redirect(['index']));
  87. }
  88. return $this->message("删除失败", $this->redirect(['index']), 'error');
  89. }
  90. /**
  91. * 下载二维码
  92. */
  93. public function actionDown()
  94. {
  95. $id = Yii::$app->request->get('id');
  96. $model = Qrcode::findOne($id);
  97. $url = Yii::$app->wechat->app->qrcode->url($model['ticket']);
  98. header("Cache-control:private");
  99. header('content-type:image/jpeg');
  100. header('content-disposition: attachment;filename="' . $model['name'] . '_' . time() . '.jpg"');
  101. readfile($url);
  102. }
  103. /**
  104. * 二维码转换
  105. *
  106. * @return mixed
  107. * @throws \yii\base\InvalidConfigException
  108. */
  109. public function actionQr()
  110. {
  111. $getUrl = Yii::$app->request->get('shortUrl', Yii::$app->request->hostInfo);
  112. $qr = Yii::$app->get('qr');
  113. Yii::$app->response->format = Response::FORMAT_RAW;
  114. Yii::$app->response->headers->add('Content-Type', $qr->getContentType());
  115. return $qr->setText($getUrl)
  116. ->setSize(150)
  117. ->setMargin(7)
  118. ->writeString();
  119. }
  120. }
粤ICP备19079148号