GroupController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace wechat\modules\cp\controllers;
  3. use Codeception\Util\Uri;
  4. use Yii;
  5. use yii\web\NotFoundHttpException;
  6. use wechat\models\GroupApi;
  7. use wechat\helpers\ImageUtil;
  8. use wechat\helpers\Utils;
  9. /**
  10. * 微信群活动
  11. */
  12. class GroupController extends BaseController
  13. {
  14. public $enableCsrfValidation = false;
  15. public $layout = false;
  16. public $qrcode = [
  17. 'name' => '草料二维码扫描器',
  18. 'link' => 'https://cli.im/deqr',
  19. ];
  20. /**
  21. * 群列表
  22. * @return string
  23. * @throws NotFoundHttpException
  24. */
  25. public function actionList()
  26. {
  27. $get = Yii::$app->request->get();
  28. $group_name = isset($get['group_name']) ? trim($get['group_name']) : '';
  29. $create_time_start = isset($get['create_time_start']) ? trim($get['create_time_start']) : '';
  30. $create_time_end = isset($get['create_time_end']) ? trim($get['create_time_end']) : '';
  31. $update_time_start = isset($get['update_time_start']) ? trim($get['update_time_start']) : '';
  32. $update_time_end = isset($get['update_time_end']) ? trim($get['update_time_end']) : '';
  33. $qrcode_expire_start = isset($get['qrcode_expire_start']) ? trim($get['qrcode_expire_start']) : '';
  34. $qrcode_expire_end = isset($get['qrcode_expire_end']) ? trim($get['qrcode_expire_end']) : '';
  35. $pageSize = isset($get['pageSize']) ? intval($get['pageSize']) : 20;
  36. $pageNumber = isset($get['pageNumber']) ? intval($get['pageNumber']) : 1;
  37. $orderBy = isset($get['orderBy']) ? trim($get['orderBy']) : '';
  38. $order = isset($get['order']) ? trim($get['order']) : '';
  39. $api = new GroupApi();
  40. $params = [
  41. 'group_name' => $group_name,
  42. 'create_time_start' => $create_time_start,
  43. 'create_time_end' => $create_time_end,
  44. 'update_time_start' => $update_time_start,
  45. 'update_time_end' => $update_time_end,
  46. 'qrcode_expire_start' => $qrcode_expire_start,
  47. 'qrcode_expire_end' => $qrcode_expire_end,
  48. 'pageSize' => $pageSize,
  49. 'pageNumber' => $pageNumber,
  50. 'orderBy' => $orderBy,
  51. 'order' => $order,
  52. ];
  53. $result = $api->getList($params);
  54. if ($result['code'] != 1) {
  55. throw new NotFoundHttpException($result['message']);
  56. }
  57. $data = $result['data'];
  58. return $this->render('list', [
  59. 'params' => $params,
  60. 'data' => $data,
  61. ]);
  62. }
  63. /**
  64. * 当前微信群
  65. */
  66. public function actionGetCurrentGroup()
  67. {
  68. $api = new GroupApi();
  69. $result = $api->getCurrentGroup([]);
  70. return json_encode($result);
  71. }
  72. /**
  73. * 添加微信群
  74. * @return string
  75. */
  76. public function actionAdd()
  77. {
  78. return $this->render('add');
  79. }
  80. /**
  81. * 保存添加或编辑
  82. */
  83. public function actionSave()
  84. {
  85. // 添加时一定要上传图片,编辑时可以不上传
  86. $group_logo = null;
  87. if (empty(Yii::$app->request->post('id')) && empty($_FILES['group_logo']['size'])) {
  88. return $this->redirect(['common/error', 'tip' => '群logo不能为空']);
  89. }
  90. if (!empty($_FILES['group_logo']['size'])) {
  91. try {
  92. $imageUtil = new ImageUtil();
  93. $imageUtil->echo_error = false;
  94. $group_logo = $imageUtil->upload($_FILES['group_logo']);
  95. } catch (\Exception $e) {
  96. return $this->redirect(['common/error', 'tip' => $e->getMessage()]);
  97. }
  98. }
  99. // dataURL
  100. $group_qrcode = $this->saveQrcode(Yii::$app->request->post('group_qrcode'));
  101. if ($group_qrcode === false) {
  102. return $this->redirect(['common/error', 'tip' => '保存二维码失败']);
  103. }
  104. $api = new GroupApi();
  105. $file_params = ['group_logo' => $group_logo, 'group_qrcode' => $group_qrcode];
  106. $params = array_merge(Yii::$app->request->post(), $file_params);
  107. $result = $api->save($params);
  108. if ($result['code'] == 1) {
  109. return $this->redirect('list');
  110. } else {
  111. return $this->redirect(['common/error', 'tip' => $result['message']]);
  112. }
  113. }
  114. /**
  115. * 编辑微信群
  116. * @return string
  117. * @throws NotFoundHttpException
  118. */
  119. public function actionEdit()
  120. {
  121. $id = (int) Yii::$app->request->get('id');
  122. if (!$id) {
  123. throw new NotFoundHttpException('参数错误');
  124. }
  125. $api = new GroupApi();
  126. $result = $api->getGroupById(['id' => $id]);
  127. if ($result['code'] != 1) {
  128. throw new NotFoundHttpException('参数错误.');
  129. }
  130. $group = $result['data'];
  131. return $this->render('edit', ['group' => $group]);
  132. }
  133. /**
  134. * 删除微信群
  135. */
  136. public function actionDelete()
  137. {
  138. $ids = Yii::$app->request->post('ids');
  139. if (!$ids) {
  140. return $this->outJson(0, [], '参数错误');
  141. }
  142. $api = new GroupApi();
  143. $result = $api->deleteGroup(['ids' => $ids]);
  144. return json_encode($result);
  145. }
  146. /**
  147. * 保存二维码
  148. * @param string $dataUrl
  149. * @return string|false
  150. */
  151. public function saveQrcode($dataUrl)
  152. {
  153. $file_arr = Utils::convertDataUrl($dataUrl);
  154. if (!$file_arr) {
  155. return false;
  156. }
  157. // 保存文件的时候是绝对路径,返回的是相对路径
  158. $baseDir = Yii::getAlias('@webroot');
  159. $prefix = '/upload/' .date('Ym') . '/';
  160. $filename = 'qrcode_' . md5($file_arr['content']) . '.' . $file_arr['type'];
  161. $absolutePath = $baseDir . $prefix . $filename;
  162. $relativePath = $prefix . $filename;
  163. $res = file_put_contents($absolutePath, $file_arr['content']);
  164. return $res ? $relativePath : false;
  165. }
  166. }