ActivityController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace wechat\controllers;
  3. use wechat\models\ActivityApi;
  4. use wechat\models\GroupApi;
  5. use Yii;
  6. use wechat\helpers\Utils;
  7. /**
  8. * 活动
  9. */
  10. class ActivityController extends BaseController
  11. {
  12. public function beforeAction($action)
  13. {
  14. Yii::$app->session->open();
  15. return parent::beforeAction($action);
  16. }
  17. /**
  18. * 活动海报页面
  19. */
  20. public function actionIndex()
  21. {
  22. $info = $this->getDetailInfo();
  23. if (file_exists($info['file'])) {
  24. $regenerate = 0;
  25. $path = $info['path'];
  26. } else {
  27. $regenerate = 1;
  28. $path = '/static/images/ewm1.jpg';
  29. }
  30. return $this->render('index', [
  31. 'link' => $info['link'],
  32. 'path' => $path,
  33. 'regenerate' => $regenerate,
  34. ]);
  35. }
  36. /**
  37. * 活动详情页面
  38. */
  39. public function actionDetail()
  40. {
  41. return $this->render('detail');
  42. }
  43. /**
  44. * 群二维码页面
  45. */
  46. public function actionQrcode()
  47. {
  48. $params = [
  49. 'session_id' => Yii::$app->session->getId(),
  50. ];
  51. $api = new ActivityApi();
  52. $result = $api->getCurrentGroup($params);
  53. return $this->render('qrcode', ['group' => $result['data']]);
  54. }
  55. /**
  56. * 再次生成活动详情页的二维码,这是为了应对域名更换的情况
  57. */
  58. public function actionRegenerate()
  59. {
  60. $result = ['code' => 0, 'data' => [], 'message' => ''];
  61. $dataUrl = trim(Yii::$app->request->post('dataUrl'));
  62. if (!$dataUrl) {
  63. $result['message'] = '参数不能为空';
  64. return json_encode($result);
  65. }
  66. $file_arr = Utils::convertDataUrl($dataUrl);
  67. if (!$file_arr) {
  68. $result['message'] = '参数错误';
  69. return json_encode($result);
  70. }
  71. $info = $this->getDetailInfo();
  72. $res = file_put_contents($info['file'], $file_arr['content']);
  73. if ($res) {
  74. $result['code'] = 1;
  75. } else {
  76. $result['message'] = '生成失败';
  77. }
  78. return json_encode($result);
  79. }
  80. /**
  81. * 获取详情页网址和二维码文件地址
  82. * @return string
  83. */
  84. protected function getDetailInfo()
  85. {
  86. $link = Yii::$app->request->hostInfo . '/activity/detail';
  87. $path = '/static/images/activity_detail_' . md5($link) . '.png';
  88. $file = Yii::getAlias('@webroot') . $path;
  89. $info = [
  90. 'link' => $link,
  91. 'path' => $path,
  92. 'file' => $file,
  93. ];
  94. return $info;
  95. }
  96. }