GroupController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace wechat\controllers;
  3. use wechat\models\ScanQrcodeRecord;
  4. use Yii;
  5. use wechat\models\WechatGroup;
  6. /**
  7. * 后台的接口
  8. * Class GroupController
  9. * @package wechat\controllers
  10. */
  11. class GroupController extends BaseController
  12. {
  13. /**
  14. * 管理员列表
  15. */
  16. public function actionList()
  17. {
  18. $get = Yii::$app->request->post();
  19. $group_name = isset($get['group_name']) ? trim($get['group_name']) : '';
  20. $create_time_start = isset($get['create_time_start']) ? trim($get['create_time_start']) : '';
  21. $create_time_end = isset($get['create_time_end']) ? trim($get['create_time_end']) : '';
  22. $update_time_start = isset($get['update_time_start']) ? trim($get['update_time_start']) : '';
  23. $update_time_end = isset($get['update_time_end']) ? trim($get['update_time_end']) : '';
  24. $qrcode_expire_start = isset($get['qrcode_expire_start']) ? trim($get['qrcode_expire_start']) : '';
  25. $qrcode_expire_end = isset($get['qrcode_expire_end']) ? trim($get['qrcode_expire_end']) : '';
  26. $pageSize = isset($get['pageSize']) && intval($get['pageSize']) > 0 ? intval($get['pageSize']) : 20;
  27. $pageNumber = isset($get['pageNumber']) ? $get['pageNumber'] : 1;
  28. $orderBy = isset($get['orderBy']) ? strtolower($get['orderBy']) : '';
  29. $order = isset($get['order']) ? strtolower($get['order']) : '';
  30. $where = ['and'];
  31. if ($group_name) {
  32. $where[] = ['=', 'group_name', $group_name];
  33. }
  34. if ($create_time_start) {
  35. $create_time_start_timestamp = strtotime($create_time_start);
  36. if ($create_time_start_timestamp) {
  37. $where[] = ['>=', 'create_time', $create_time_start_timestamp];
  38. }
  39. }
  40. if ($create_time_end) {
  41. $create_time_end_timestamp = strtotime($create_time_end);
  42. if ($create_time_end_timestamp) {
  43. $where[] = ['<', 'create_time', $create_time_end_timestamp + 86400];
  44. }
  45. }
  46. if ($update_time_start) {
  47. $update_time_start_timestamp = strtotime($update_time_start);
  48. if ($update_time_start_timestamp) {
  49. $where[] = ['>=', 'update_time', $update_time_start_timestamp];
  50. }
  51. }
  52. if ($update_time_end) {
  53. $update_time_end_timestamp = strtotime($update_time_end);
  54. if ($update_time_end_timestamp) {
  55. $where[] = ['<', 'update_time', $update_time_end_timestamp + 86400];
  56. }
  57. }
  58. if ($qrcode_expire_start) {
  59. $qrcode_expire_start_timestamp = strtotime($qrcode_expire_start);
  60. if ($qrcode_expire_start_timestamp) {
  61. $where[] = ['>=', 'qrcode_expire', $qrcode_expire_start_timestamp];
  62. }
  63. }
  64. if ($qrcode_expire_end) {
  65. $qrcode_expire_end_timestamp = strtotime($qrcode_expire_end);
  66. if ($qrcode_expire_end_timestamp) {
  67. $where[] = ['<', 'qrcode_expire', $qrcode_expire_end_timestamp + 86400];
  68. }
  69. }
  70. $query = WechatGroup::find()->where($where);
  71. $offset = $pageSize * ($pageNumber - 1);
  72. $query->offset($offset)->limit($pageSize);
  73. $totalCount = $query->count();
  74. $pageCount = ceil($totalCount / $pageSize);
  75. $data = [
  76. 'totalCount' => $totalCount,
  77. 'pageCount' => $pageCount,
  78. 'dataList' => [],
  79. ];
  80. if ($totalCount) {
  81. // 排序
  82. $allowOrderColumn = ['id', 'group_name', 'group_logo', 'group_link', 'group_qrcode', 'qrcode_expire', 'scan_count', 'sort', 'is_enable', 'is_current', 'create_time', 'update_time'];
  83. if (in_array($orderBy, $allowOrderColumn) && in_array($order, ['asc', 'desc'])) {
  84. if ($order == 'asc') {
  85. $orderCondition = [$orderBy => SORT_ASC];
  86. } else {
  87. $orderCondition = [$orderBy => SORT_DESC];
  88. }
  89. } else {
  90. $orderCondition = ['create_time' => SORT_DESC];
  91. }
  92. $dataList = $query->orderBy($orderCondition)->asArray()->all();
  93. $groupModel = WechatGroup::getCurrentGroup(true);
  94. foreach ($dataList as $k => $v) {
  95. $dataList[$k]['is_enable'] = $v['is_enable'] ? '启用' : '不启用';
  96. $dataList[$k]['qrcode_expire'] = $v['qrcode_expire'] ? date('Y-m-d', $v['qrcode_expire']) : '-';
  97. $dataList[$k]['create_time'] = $v['create_time'] ? date('Y-m-d H:i:s', $v['create_time']) : '-';
  98. $dataList[$k]['update_time'] = $v['update_time'] ? date('Y-m-d H:i:s', $v['update_time']) : '-';
  99. $dataList[$k]['is_current'] = 0;
  100. if ($groupModel && $groupModel->id == $v['id']) {
  101. $dataList[$k]['is_current'] = 1;
  102. }
  103. }
  104. $data['dataList'] = $dataList;
  105. return $this->outJson(1, $data);
  106. } else {
  107. return $this->outJson(1, $data, '没有数据');
  108. }
  109. }
  110. /**
  111. * 当前微信群
  112. */
  113. public function actionGetCurrentGroup()
  114. {
  115. $groupModel = WechatGroup::getCurrentGroup(true);
  116. if ($groupModel) {
  117. $group = $groupModel->toArray();
  118. $group['is_enable'] = $group['is_enable'] ? '启用' : '不启用';
  119. $group['qrcode_expire'] = $group['qrcode_expire'] ? date('Y-m-d', $group['qrcode_expire']) : '-';
  120. $group['create_time'] = $group['create_time'] ? date('Y-m-d', $group['create_time']) : '-';
  121. $group['update_time'] = $group['update_time'] ? date('Y-m-d', $group['update_time']) : '-';
  122. return $this->outJson(1, $group);
  123. } else {
  124. return $this->outJson(0, [], '没有数据');
  125. }
  126. }
  127. /**
  128. * 保存新增或编辑
  129. */
  130. public function actionSave()
  131. {
  132. $post = Yii::$app->request->post();
  133. $post['qrcode_expire'] = isset($post['qrcode_expire']) ? strtotime($post['qrcode_expire'] . ' 23:59:59') : strtotime('+7 days midnight');
  134. // 没有id为新增,有为编辑
  135. $time = time();
  136. if (!isset($post['id'])) {
  137. $model = new WechatGroup();
  138. $model->setAttributes($post);
  139. $count = WechatGroup::find()->where(['is_current' => 1])->count();
  140. $model->is_current = $count ? 0 : 1;
  141. $model->create_time = $time;
  142. $model->update_time = $time;
  143. $res = $model->save();
  144. } else {
  145. $post['id'] = (int) $post['id'];
  146. $model = WechatGroup::find()->where(['id' => $post['id']])->limit(1)->one();
  147. if (!$model) {
  148. return $this->outJson(0, [], '没有该条数据');
  149. }
  150. // 没有上传的话则不修改
  151. if (empty($post['group_logo'])) {
  152. unset($post['group_logo']);
  153. }
  154. $model->setAttributes($post);
  155. $res = $model->save();
  156. }
  157. if ($res) {
  158. return $this->outJson(1);
  159. } else {
  160. return $this->outJson(0, [], '保存失败');
  161. }
  162. }
  163. /**
  164. * 某个微信群
  165. */
  166. public function actionGetGroupById()
  167. {
  168. $id = (int) Yii::$app->request->post('id');
  169. if (!$id) {
  170. return $this->outJson(0, [], '参数错误');
  171. }
  172. $group = WechatGroup::find()->where(['id' => $id])->limit(1)->asArray()->one();
  173. if (!$group) {
  174. return $this->outJson(0, [], '没有该管理员');
  175. }
  176. $group['qrcode_expire'] = $group['qrcode_expire'] ? date('Y-m-d', $group['qrcode_expire']) : '-';
  177. $group['create_time'] = $group['create_time'] ? date('Y-m-d', $group['create_time']) : '-';
  178. $group['update_time'] = $group['update_time'] ? date('Y-m-d', $group['update_time']) : '-';
  179. return $this->outJson(1, $group);
  180. }
  181. /**
  182. * 删除微信群
  183. */
  184. public function actionDelete()
  185. {
  186. $ids = Yii::$app->request->post('ids');
  187. if (!$ids) {
  188. return $this->outJson(0, [], '参数错误');
  189. }
  190. WechatGroup::deleteAll(['id' => $ids]);
  191. ScanQrcodeRecord::deleteAll(['wechat_group_id' => $ids]);
  192. return $this->outJson(1);
  193. }
  194. }