User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace app\admin\controller\weixin;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use addons\weixin\library\WechatService;
  9. use app\admin\model\weixin\Reply as WechatReply;
  10. /**
  11. * 微信用户管理
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class User extends Backend
  16. {
  17. /**
  18. * User模型对象
  19. * @var \app\admin\model\weixin\User
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\weixin\User;
  26. }
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. //当前是否为关联查询
  33. $this->relationSearch = true;
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags', 'trim']);
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $total = $this->model
  43. ->with(['fauser'])
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->count();
  47. $list = $this->model
  48. ->with(['fauser'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->limit($offset, $limit)
  52. ->select();
  53. $list = collection($list)->toArray();
  54. $list_tag = $this->model->getTag();
  55. $list_group = $this->model->getGroup();
  56. foreach ($list as &$row) {
  57. if (isset($list_group[$row['groupid']])) {
  58. $row['groupid_text'] = $list_group[$row['groupid']]['name'];
  59. }
  60. if (!empty($row['tagid_list'])) {
  61. $tagid_list_arr = explode(',', $row['tagid_list']);
  62. $tagid_list = [];
  63. foreach ($tagid_list_arr as $v) {
  64. if (isset($list_tag[$v])) {
  65. $tagid_list[] = $list_tag[$v]['name'];
  66. }
  67. }
  68. $row['tagid_list_text'] = $tagid_list;
  69. } else {
  70. $row['tagid_list_text'] = ['无'];
  71. }
  72. }
  73. unset($row);
  74. $result = array("total" => $total, "rows" => $list);
  75. return json($result);
  76. }
  77. return $this->view->fetch();
  78. }
  79. /**
  80. * 推送消息
  81. * @author Created by Xing <464401240@qq.com>
  82. */
  83. public function sendmsg($ids = null)
  84. {
  85. $ids = trim($ids, ',');
  86. if ($this->request->isAjax()) {
  87. $params = $this->request->post("row/a");
  88. $params['ids'] = $ids;
  89. if ($params) {
  90. $errorLog = [];//发送失败的用户
  91. $list = $this->model->where(['uid' => ['in', $ids]])->select();
  92. if (empty($list)) {
  93. $this->error('发送失败,参数不正确');
  94. }
  95. $model = new \app\admin\model\weixin\Reply;
  96. foreach ($list as $v) {
  97. if ($v['subscribe'] && $v['openid']) {
  98. try {
  99. switch ($params['type']) {
  100. case 'text':
  101. $message = WechatService::textMessage($params['content']);
  102. WechatService::staffService()->message($message)->to($v['openid'])->send();
  103. break;
  104. case 'image':
  105. $message = json_decode($model->where('id', $params['image'])->value('data'), true);
  106. $message = WechatService::materialMessage('image', $message['media_id']);
  107. WechatService::staffService()->message($message)->to($v['openid'])->send();
  108. break;
  109. case 'news':
  110. $message = $model->where('id', $params['news'])->value('data');
  111. $message = WechatService::newsMessage(json_decode($message, true));
  112. WechatService::staffService()->message($message)->to($v['openid'])->send();
  113. break;
  114. default:
  115. $message = json_decode($model->where('id', $params['voice'])->value('data'), true);
  116. $message = WechatService::voiceMessage($message['media_id']);
  117. WechatService::staffService()->message($message)->to($v['openid'])->send();
  118. break;
  119. }
  120. } catch (\Exception $e) {
  121. $errorLog[] = $v['nickname'] . '发送失败,或因为超48小时未互动。';
  122. }
  123. } else {
  124. $errorLog[] = $v['nickname'] . '没有关注发送失败(不是微信公众号用户)';
  125. }
  126. }
  127. if (!count($errorLog)) {
  128. $this->success('全部发送成功');
  129. }
  130. $this->success(implode(',', $errorLog) . ',剩余的发送成功');
  131. }
  132. }
  133. //加载当前控制器语言包
  134. $this->loadlang('weixin/reply/keyword');
  135. $model = new \app\admin\model\weixin\Reply;
  136. $this->view->assign("typeList", $model->getTypeList());
  137. return $this->view->fetch();
  138. }
  139. /**
  140. * 修改用户标签
  141. */
  142. public function edit_user_tag($ids = null)
  143. {
  144. $row = $this->model->get($ids);
  145. if (!$row) {
  146. $this->error(__('No Results were found'));
  147. }
  148. if ($this->request->isAjax()) {
  149. $tagId = $this->request->post('tagid_list');
  150. if (!$tagId) {
  151. $this->error('请选择用户标签!');
  152. }
  153. if ($tagId) {
  154. $params['tagid_list'] = $tagId;
  155. $result = false;
  156. Db::startTrans();
  157. try {
  158. $result = $row->allowField(true)->save($params);
  159. Db::commit();
  160. } catch (ValidateException $e) {
  161. Db::rollback();
  162. $this->error($e->getMessage());
  163. } catch (PDOException $e) {
  164. Db::rollback();
  165. $this->error($e->getMessage());
  166. } catch (Exception $e) {
  167. Db::rollback();
  168. $this->error($e->getMessage());
  169. }
  170. if ($result !== false) {
  171. $tagId = explode(',', $tagId) ?: [];
  172. $tagList = explode(',', $row['tagid_list']) ?: [];
  173. foreach ($tagList as $tag) {
  174. if ($tag) {
  175. WechatService::userTagService()->batchUntagUsers([$row['openid']], $tag);
  176. }
  177. }
  178. foreach ($tagId as $tag) {
  179. WechatService::userTagService()->batchTagUsers([$row['openid']], $tag);
  180. }
  181. $this->success();
  182. } else {
  183. $this->error(__('No rows were updated'));
  184. }
  185. }
  186. }
  187. $this->view->assign("row", $row);
  188. return $this->view->fetch();
  189. }
  190. /**
  191. * 修改用户分组
  192. */
  193. public function edit_user_group($ids = null)
  194. {
  195. $row = $this->model->get($ids);
  196. if (!$row) {
  197. $this->error(__('No Results were found'));
  198. }
  199. if ($this->request->isAjax()) {
  200. $group_id = $this->request->post('group_id');
  201. if (!$group_id) {
  202. $this->error('请选择用户分组!');
  203. }
  204. if ($group_id) {
  205. $params['groupid'] = $group_id;
  206. $result = false;
  207. Db::startTrans();
  208. try {
  209. $result = $row->allowField(true)->save($params);
  210. Db::commit();
  211. } catch (ValidateException $e) {
  212. Db::rollback();
  213. $this->error($e->getMessage());
  214. } catch (PDOException $e) {
  215. Db::rollback();
  216. $this->error($e->getMessage());
  217. } catch (Exception $e) {
  218. Db::rollback();
  219. $this->error($e->getMessage());
  220. }
  221. if ($result !== false) {
  222. WechatService::userGroupService()->moveUser($row['openid'], $group_id);
  223. $this->success();
  224. } else {
  225. $this->error(__('No rows were updated'));
  226. }
  227. }
  228. }
  229. $this->view->assign("row", $row);
  230. return $this->view->fetch();
  231. }
  232. /**
  233. * 标签列表
  234. */
  235. public function tag()
  236. {
  237. if ($this->request->isAjax()) {
  238. $list = $this->model->getTag();
  239. $result = array("total" => count($list), "rows" => array_values($list));
  240. return json($result);
  241. }
  242. return $this->view->fetch();
  243. }
  244. /**
  245. * 添加标签
  246. */
  247. public function tagadd()
  248. {
  249. if ($this->request->isPost()) {
  250. $tagName = $this->request->post('name');
  251. if (!$tagName) {
  252. $this->error('请输入标签名称!');
  253. }
  254. try {
  255. WechatService::userTagService()->create($tagName);
  256. } catch (\Exception $e) {
  257. $this->error($e->getMessage());
  258. }
  259. Db::name('weixin_cache')->where('key', 'wechat_tag')->delete();
  260. $this->success();
  261. }
  262. return $this->view->fetch();
  263. }
  264. /**
  265. * 修改标签
  266. */
  267. public function tagedit($ids = null)
  268. {
  269. $list = $this->model->getTag();
  270. if (!isset($list[$ids])) {
  271. $this->error(__('No Results were found'));
  272. } else {
  273. $row = $list[$ids];
  274. }
  275. if ($this->request->isPost()) {
  276. $tagName = $this->request->post('name');
  277. if (!$tagName) {
  278. $this->error('请输入标签名称!');
  279. }
  280. try {
  281. WechatService::userTagService()->update($ids, $tagName);
  282. } catch (\Exception $e) {
  283. $this->error($e->getMessage());
  284. }
  285. Db::name('weixin_cache')->where('key', 'wechat_tag')->delete();
  286. $this->success('修改标签成功!');
  287. }
  288. $this->view->assign("row", $row);
  289. return $this->view->fetch();
  290. }
  291. /**
  292. * 删除标签
  293. */
  294. public function tagdel($ids = null)
  295. {
  296. if ($this->request->isPost()) {
  297. try {
  298. WechatService::userTagService()->delete($ids);
  299. } catch (\Exception $e) {
  300. $this->error($e->getMessage());
  301. }
  302. Db::name('weixin_cache')->where('key', 'wechat_tag')->delete();
  303. $this->success('删除标签成功!');
  304. }
  305. }
  306. /**
  307. * 分组列表
  308. */
  309. public function group()
  310. {
  311. if ($this->request->isAjax()) {
  312. $list = $this->model->getGroup();
  313. $result = array("total" => count($list), "rows" => array_values($list));
  314. return json($result);
  315. }
  316. return $this->view->fetch();
  317. }
  318. /**
  319. * 添加分组
  320. */
  321. public function groupadd()
  322. {
  323. if ($this->request->isPost()) {
  324. $tagName = $this->request->post('name');
  325. if (!$tagName) {
  326. $this->error('请输入标签名称!');
  327. }
  328. try {
  329. WechatService::userGroupService()->create($tagName);
  330. } catch (\Exception $e) {
  331. $this->error($e->getMessage());
  332. }
  333. Db::name('weixin_cache')->where('key', 'wechat_group')->delete();
  334. $this->success();
  335. }
  336. return $this->view->fetch();
  337. }
  338. /**
  339. * 修改分组
  340. */
  341. public function groupedit($ids = null)
  342. {
  343. $list = $this->model->getGroup();
  344. if (!isset($list[$ids])) {
  345. $this->error(__('No Results were found'));
  346. } else {
  347. $row = $list[$ids];
  348. }
  349. if ($this->request->isPost()) {
  350. $tagName = $this->request->post('name');
  351. if (!$tagName) {
  352. $this->error('请输入标签名称!');
  353. }
  354. try {
  355. WechatService::userGroupService()->update($ids, $tagName);
  356. } catch (\Exception $e) {
  357. $this->error($e->getMessage());
  358. }
  359. Db::name('weixin_cache')->where('key', 'wechat_group')->delete();
  360. $this->success('修改标签成功!');
  361. }
  362. $this->view->assign("row", $row);
  363. return $this->view->fetch();
  364. }
  365. /**
  366. * 删除分组
  367. */
  368. public function groupdel($ids = null)
  369. {
  370. if ($this->request->isPost()) {
  371. try {
  372. WechatService::userGroupService()->delete($ids);
  373. } catch (\Exception $e) {
  374. $this->error($e->getMessage());
  375. }
  376. Db::name('weixin_cache')->where('key', 'wechat_group')->delete();
  377. $this->success('删除标签成功!');
  378. }
  379. }
  380. }