GroupTemplate.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\work;
  12. use app\controller\admin\AuthController;
  13. use app\services\work\WorkClientServices;
  14. use app\services\work\WorkGroupChatServices;
  15. use app\services\work\WorkGroupMsgRelationServices;
  16. use app\services\work\WorkGroupMsgSendResultGroupChatServices;
  17. use app\services\work\WorkGroupMsgSendResultServices;
  18. use app\services\work\WorkGroupMsgTaskServices;
  19. use app\services\work\WorkGroupTemplateServices;
  20. use think\facade\App;
  21. /**
  22. * 群发模板
  23. * Class GroupTemplate
  24. * @package app\controller\admin\v1\work
  25. */
  26. class GroupTemplate extends AuthController
  27. {
  28. /**
  29. * GroupTemplate constructor.
  30. * @param App $app
  31. * @param WorkGroupTemplateServices $services
  32. */
  33. public function __construct(App $app, WorkGroupTemplateServices $services)
  34. {
  35. parent::__construct($app);
  36. $this->services = $services;
  37. }
  38. /**
  39. * @return mixed
  40. */
  41. public function index()
  42. {
  43. $where = $this->request->getMore([
  44. ['time', '', '', 'update_time'],
  45. ['client_type', ''],
  46. ['name', ''],
  47. ['type', 0]
  48. ]);
  49. return $this->success($this->services->getGroupTemplate($where));
  50. }
  51. /**
  52. * @param WorkClientServices $services
  53. * @return mixed
  54. */
  55. public function save(WorkClientServices $services)
  56. {
  57. $data = $this->request->postMore([
  58. ['type', 0],
  59. ['name', ''],
  60. ['userids', []],
  61. ['client_type', 0],
  62. ['where_time', 0],
  63. ['where_label', []],
  64. ['where_not_label', []],
  65. ['template_type', 0],
  66. ['send_time', 0],
  67. ['welcome_words', []],
  68. ]);
  69. if ($data['template_type']) {
  70. if (!$data['send_time']) {
  71. return $this->fail('请设置定时发送时间');
  72. }
  73. $data['send_time'] = strtotime($data['send_time']);
  74. }
  75. if (!$data['name']) {
  76. return $this->fail('请设置群发名称');
  77. }
  78. if ($data['type']) {
  79. if (!$data['userids']) {
  80. return $this->fail('请选择群主');
  81. }
  82. } else {
  83. if ($data['client_type']) {
  84. if (!$data['where_time'] && !$data['where_label'] && !$data['where_not_label']) {
  85. return $this->fail('请设置查询客户条件');
  86. }
  87. }
  88. }
  89. if (empty($data['welcome_words']['text']['content'])) {
  90. return $this->fail('请设置群发内容');
  91. }
  92. if (isset($data['userids'][0]['userid'])) {
  93. $data['userids'] = array_column($data['userids'], 'userid');
  94. }
  95. $this->services->saveGroupTemplate($data);
  96. return $this->success('保存成功');
  97. }
  98. /**
  99. * @param $id
  100. * @return mixed
  101. */
  102. public function read($id)
  103. {
  104. if (!$id) {
  105. return $this->fail('缺少参数');
  106. }
  107. return $this->success($this->services->getGroupTemplateInfo((int)$id));
  108. }
  109. /**
  110. * 删除
  111. * @param $id
  112. * @return mixed
  113. */
  114. public function delete($id)
  115. {
  116. if (!$id) {
  117. return $this->fail('缺少参数');
  118. }
  119. if ($this->services->deleteGroupTemplate($id)) {
  120. return $this->success('删除成功');
  121. } else {
  122. return $this->fail('删除失败');
  123. }
  124. }
  125. /**
  126. * 获取成员列表
  127. * @param WorkGroupMsgRelationServices $relationServices
  128. * @param WorkGroupMsgTaskServices $services
  129. * @param $id
  130. * @return mixed
  131. */
  132. public function memberList(WorkGroupMsgRelationServices $relationServices, WorkGroupMsgTaskServices $services, $id)
  133. {
  134. if (!$id) {
  135. return $this->fail('缺少参数');
  136. }
  137. $where = $this->request->getMore([
  138. ['user_name', ''],
  139. ['status', '']
  140. ]);
  141. $where['msg_id'] = $relationServices->getColumn(['template_id' => $id], 'msg_id');
  142. return $this->success($services->getTaksList($where));
  143. }
  144. /**
  145. * 获取客户列表
  146. * @param WorkGroupMsgRelationServices $relationServices
  147. * @param WorkGroupMsgSendResultServices $services
  148. * @param $id
  149. * @return mixed
  150. */
  151. public function clientList(WorkGroupMsgRelationServices $relationServices, WorkGroupMsgSendResultServices $services, $id)
  152. {
  153. if (!$id) {
  154. return $this->fail('缺少参数');
  155. }
  156. $where = $this->request->getMore([
  157. ['client_name', ''],
  158. ['status', '']
  159. ]);
  160. $where['msg_id'] = $relationServices->getColumn(['template_id' => $id], 'msg_id');
  161. return $this->success($services->getClientList($where));
  162. }
  163. /**
  164. * 客户群接受详情
  165. * @param WorkGroupMsgRelationServices $relationServices
  166. * @param WorkGroupChatServices $services
  167. * @param $id
  168. * @return mixed
  169. */
  170. public function groupChatList(WorkGroupMsgRelationServices $relationServices, WorkGroupChatServices $services, $id)
  171. {
  172. if (!$id) {
  173. return $this->fail('缺少参数');
  174. }
  175. $where = $this->request->getMore([
  176. ['owner', []],
  177. ['status', 0],
  178. ['name', '']
  179. ]);
  180. $where['msg_id'] = $relationServices->getColumn(['template_id' => $id], 'msg_id');
  181. if (!$where['owner']) {
  182. $userids = $this->services->value(['id' => $id], 'userids');
  183. $userids = is_string($userids) ? json_decode($userids, true) : [];
  184. $where['owner'] = $userids;
  185. }
  186. if (!$where['msg_id']) {
  187. $where['msg_id'] = [0];
  188. }
  189. return $this->success($services->groupChatList($where, ['ownerInfo' => function ($query) {
  190. $query->field(['userid', 'name']);
  191. }]));
  192. }
  193. /**
  194. * 获取群主发送详情
  195. * @param WorkGroupMsgRelationServices $relationServices
  196. * @param WorkGroupMsgSendResultServices $resultServices
  197. * @param $id
  198. * @return mixed
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. */
  203. public function groupChatOwnerList(WorkGroupMsgRelationServices $relationServices, WorkGroupMsgSendResultServices $resultServices, $id)
  204. {
  205. if (!$id) {
  206. return $this->fail('缺少参数');
  207. }
  208. $where = $this->request->getMore([
  209. ['owner', []],
  210. ['status', 0]
  211. ]);
  212. $where['msg_id'] = $relationServices->getColumn(['template_id' => $id], 'msg_id');
  213. if (!$where['owner']) {
  214. $userids = $this->services->value(['id' => $id], 'userids');
  215. $userids = is_string($userids) ? json_decode($userids, true) : [];
  216. $where['owner'] = $userids;
  217. }
  218. if (!$where['msg_id']) {
  219. $where['msg_id'] = [0];
  220. }
  221. return $this->success($resultServices->getGroupChatMsgList($where));
  222. }
  223. /**
  224. * 群发详情列表
  225. * @param WorkGroupMsgSendResultGroupChatServices $services
  226. * @return mixed
  227. * @throws \think\db\exception\DataNotFoundException
  228. * @throws \think\db\exception\DbException
  229. * @throws \think\db\exception\ModelNotFoundException
  230. */
  231. public function getOwnerChatList(WorkGroupMsgSendResultGroupChatServices $services)
  232. {
  233. $chatIds = $this->request->get('chat_id', '');
  234. if (!$chatIds) {
  235. return $this->fail('缺少群聊id');
  236. }
  237. $chatIds = explode(',', $chatIds);
  238. $status = $this->request->get('status', '');
  239. return $this->success($services->getList(['chat_id' => $chatIds, 'status' => $status]));
  240. }
  241. /**
  242. * 发送提醒消息
  243. * @return mixed
  244. * @throws \think\db\exception\DataNotFoundException
  245. * @throws \think\db\exception\DbException
  246. * @throws \think\db\exception\ModelNotFoundException
  247. */
  248. public function sendMessage()
  249. {
  250. $userid = $this->request->post('userid');
  251. $time = $this->request->post('time');
  252. $id = $this->request->post('id');
  253. if (!$id && !$userid) {
  254. return $this->fail('缺少参数');
  255. }
  256. if (!is_string($userid)) {
  257. return $this->fail('类型错误userid应为字符串');
  258. }
  259. $this->services->sendMessage((int)$id, $userid, $time);
  260. return $this->success('发送成功');
  261. }
  262. }