User.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. foreach ($list as &$row) {
  56. if (!empty($row['tagid_list'])) {
  57. $tagid_list_arr = explode(',', $row['tagid_list']);
  58. $tagid_list = [];
  59. foreach ($tagid_list_arr as $v) {
  60. if (isset($list_tag[$v])) {
  61. $tagid_list[] = $list_tag[$v]['name'];
  62. }
  63. }
  64. $row['tagid_list_text'] = $tagid_list;
  65. } else {
  66. $row['tagid_list_text'] = ['无'];
  67. }
  68. }
  69. unset($row);
  70. $result = array("total" => $total, "rows" => $list);
  71. return json($result);
  72. }
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 推送消息
  77. * @author Created by Xing <464401240@qq.com>
  78. */
  79. public function sendmsg($ids = null)
  80. {
  81. $ids = trim($ids, ',');
  82. if ($this->request->isAjax()) {
  83. $params = $this->request->post("row/a");
  84. $params['ids'] = $ids;
  85. if ($params) {
  86. $errorLog = [];//发送失败的用户
  87. $list = $this->model->where(['uid' => ['in', $ids]])->select();
  88. if (empty($list)) {
  89. $this->error('发送失败,参数不正确');
  90. }
  91. $model = new \app\admin\model\weixin\Reply;
  92. foreach ($list as $v) {
  93. if ($v['subscribe'] && $v['openid']) {
  94. try {
  95. switch ($params['type']) {
  96. case 'text':
  97. $message = WechatService::textMessage($params['content']);
  98. WechatService::staffService()->message($message)->to($v['openid'])->send();
  99. break;
  100. case 'image':
  101. $message = json_decode($model->where('id', $params['image'])->value('data'), true);
  102. $message = WechatService::materialMessage('image', $message['media_id']);
  103. WechatService::staffService()->message($message)->to($v['openid'])->send();
  104. break;
  105. case 'news':
  106. $message = $model->where('id', $params['news'])->value('data');
  107. $message = WechatService::newsMessage(json_decode($message, true));
  108. WechatService::staffService()->message($message)->to($v['openid'])->send();
  109. break;
  110. default:
  111. $message = json_decode($model->where('id', $params['voice'])->value('data'), true);
  112. $message = WechatService::voiceMessage($message['media_id']);
  113. WechatService::staffService()->message($message)->to($v['openid'])->send();
  114. break;
  115. }
  116. } catch (\Exception $e) {
  117. $errorLog[] = $v['nickname'] . '发送失败,或因为超48小时未互动。';
  118. }
  119. } else {
  120. $errorLog[] = $v['nickname'] . '没有关注发送失败(不是微信公众号用户)';
  121. }
  122. }
  123. if (!count($errorLog)) {
  124. $this->success('全部发送成功');
  125. }
  126. $this->success(implode(',', $errorLog) . ',剩余的发送成功');
  127. }
  128. }
  129. //加载当前控制器语言包
  130. $this->loadlang('weixin/reply/keyword');
  131. $model = new \app\admin\model\weixin\Reply;
  132. $this->view->assign("typeList", $model->getTypeList());
  133. return $this->view->fetch();
  134. }
  135. /**
  136. * 修改用户标签
  137. */
  138. public function edit_user_tag($ids = null)
  139. {
  140. $row = $this->model->get($ids);
  141. if (!$row) {
  142. $this->error(__('No Results were found'));
  143. }
  144. if ($this->request->isAjax()) {
  145. $tagId = $this->request->post('tagid_list');
  146. if (!$tagId) {
  147. $this->error('请选择用户标签!');
  148. }
  149. if ($tagId) {
  150. $params['tagid_list'] = $tagId;
  151. $result = false;
  152. Db::startTrans();
  153. try {
  154. $result = $row->allowField(true)->save($params);
  155. Db::commit();
  156. } catch (ValidateException $e) {
  157. Db::rollback();
  158. $this->error($e->getMessage());
  159. } catch (PDOException $e) {
  160. Db::rollback();
  161. $this->error($e->getMessage());
  162. } catch (Exception $e) {
  163. Db::rollback();
  164. $this->error($e->getMessage());
  165. }
  166. if ($result !== false) {
  167. $tagId = explode(',', $tagId) ?: [];
  168. $tagList = explode(',', $row['tagid_list']) ?: [];
  169. foreach ($tagList as $tag) {
  170. if ($tag) {
  171. WechatService::userTagService()->untagUsers([$row['openid']], $tag);
  172. }
  173. }
  174. foreach ($tagId as $tag) {
  175. WechatService::userTagService()->tagUsers([$row['openid']], $tag);
  176. }
  177. $this->success();
  178. } else {
  179. $this->error(__('No rows were updated'));
  180. }
  181. }
  182. }
  183. $this->view->assign("row", $row);
  184. return $this->view->fetch();
  185. }
  186. /**
  187. * 标签列表
  188. */
  189. public function tag()
  190. {
  191. if ($this->request->isAjax()) {
  192. $list = $this->model->getTag();
  193. $result = array("total" => count($list), "rows" => array_values($list));
  194. return json($result);
  195. }
  196. return $this->view->fetch();
  197. }
  198. /**
  199. * 添加标签
  200. */
  201. public function tagadd()
  202. {
  203. if ($this->request->isPost()) {
  204. $tagName = $this->request->post('name');
  205. if (!$tagName) {
  206. $this->error('请输入标签名称!');
  207. }
  208. try {
  209. WechatService::userTagService()->create($tagName);
  210. } catch (\Exception $e) {
  211. $this->error($e->getMessage());
  212. }
  213. Db::name('weixin_cache')->where('key', 'wechat_tag')->delete();
  214. $this->success();
  215. }
  216. return $this->view->fetch();
  217. }
  218. /**
  219. * 修改标签
  220. */
  221. public function tagedit($ids = null)
  222. {
  223. $list = $this->model->getTag();
  224. if (!isset($list[$ids])) {
  225. $this->error(__('No Results were found'));
  226. } else {
  227. $row = $list[$ids];
  228. }
  229. if ($this->request->isPost()) {
  230. $tagName = $this->request->post('name');
  231. if (!$tagName) {
  232. $this->error('请输入标签名称!');
  233. }
  234. try {
  235. WechatService::userTagService()->update($ids, $tagName);
  236. } catch (\Exception $e) {
  237. $this->error($e->getMessage());
  238. }
  239. Db::name('weixin_cache')->where('key', 'wechat_tag')->delete();
  240. $this->success('修改标签成功!');
  241. }
  242. $this->view->assign("row", $row);
  243. return $this->view->fetch();
  244. }
  245. /**
  246. * 删除标签
  247. */
  248. public function tagdel($ids = null)
  249. {
  250. if ($this->request->isPost()) {
  251. try {
  252. WechatService::userTagService()->delete($ids);
  253. } catch (\Exception $e) {
  254. $this->error($e->getMessage());
  255. }
  256. Db::name('weixin_cache')->where('key', 'wechat_tag')->delete();
  257. $this->success('删除标签成功!');
  258. }
  259. }
  260. }