Message.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | message.php 群发消息
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Weixin\Src;
  8. use Dever;
  9. use Weixin\Src\Lib\Core;
  10. class Message
  11. {
  12. /**
  13. * 操作
  14. *
  15. * @return mixed
  16. */
  17. public function oper($param = false)
  18. {
  19. Dever::load('manage/auth.init');
  20. $id = $param ? $param : Dever::input('update_where_id');
  21. $oper = Dever::input('update_oper');
  22. $config = Dever::load('weixin/message-one', $id);
  23. if ($config && $config['state'] == 1) {
  24. if ($config['oper'] == 1) {
  25. # 待机状态,无需任何操作
  26. } else {
  27. $this->push($config);
  28. }
  29. }
  30. }
  31. /**
  32. * 立刻发送 直接回复客服文本
  33. *
  34. * @return mixed
  35. */
  36. public function send($info = false)
  37. {
  38. //Dever::load('manage/auth.init');
  39. $id = Dever::input('id');
  40. $content = Dever::input('content');
  41. if ($id && $content) {
  42. $info = $info ? $info : Dever::load('weixin/usermsg-one', $id);
  43. //$info['from'] = 'ow29VvzICifpOTq_9EDGT3AhYpqg';
  44. if ($info && $info['from']) {
  45. $info['id'] = $config['id'] = -1;
  46. $info['state'] = 1;
  47. $info['type'] = 1;
  48. $info['info'] = $content;
  49. $config['name'] = '客服点对点回复';
  50. $config['user'] = 2;
  51. $config['openid'] = $info['from'];
  52. $config['type'] = 2;
  53. $media['mid'] = 1;
  54. $result = $this->add_2($config, $info, $media);
  55. # 记录点对点聊天记录
  56. $update['add_state'] = (isset($result['data']['errcode']) && $result['data']['errcode'] == 0) ? 1 : 2;
  57. $update['add_openid'] = $info['from'];
  58. $update['add_usermsg_id'] = $id;
  59. $update['add_account'] = 1;
  60. $update['add_type'] = 'text';
  61. $update['add_info'] = $content;
  62. Dever::load('weixin/account_msg-insert', $update);
  63. }
  64. }
  65. return '发送成功';
  66. }
  67. /**
  68. * 入队
  69. *
  70. * @return mixed
  71. */
  72. public function push($config = array())
  73. {
  74. Dever::load('manage/auth.init');
  75. $id = Dever::input('where_id');
  76. $config = $config ? $config : Dever::load('weixin/message-one', $id);
  77. if ($config && $config['state'] == 1) {
  78. $time = time();
  79. $interface = 'message.sendCall?where_id=' . $config['id'];
  80. if ($config['oper'] == 3) {
  81. Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_oper' => 1, 'set_state' => 2, 'set_sdate' => date('Y-m-d H:i:s', $time)));
  82. Dever::daemon($interface, 'weixin');
  83. } elseif ($config['oper'] == 2 && $config['sdate'] >= $time) {
  84. Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_state' => 2));
  85. //加入到cron中
  86. Dever::cron($config['name'], $config['sdate'], $interface, 0, 'weixin');
  87. }
  88. }
  89. //Dever::abert('已发送');
  90. }
  91. /**
  92. * 发送消息
  93. *
  94. * @return mixed
  95. */
  96. public function sendCall($param = false)
  97. {
  98. $id = Dever::input('where_id');
  99. if ($id > 0) {
  100. $config = Dever::load('weixin/message-one', $id);
  101. if ($config && $config['state'] == 2) {
  102. Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_state' => 3));
  103. if ($config['type'] > 0) {
  104. $id = $config['content_' . $config['type']];
  105. if ($id > 0) {
  106. $info = Dever::load('weixin/content-one', $id);
  107. if ($info && $info['type'] <= 2) {
  108. $media['mid'] = 1;
  109. } else {
  110. $media = Dever::load('weixin/media-use', array('where_type' => 'thead', 'where_content_id' => $id));
  111. }
  112. #判断是否有素材
  113. if ($info && $media && $media['mid']) {
  114. $method = 'add_' . $config['type'];
  115. Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_state' => 3));
  116. $this->$method($config, $info, $media);
  117. Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_state' => 4));
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. /**
  125. * 发送客服消息
  126. *
  127. * @return mixed
  128. */
  129. private function add_2($config, $info, $media)
  130. {
  131. $result = array();
  132. if ($info['state'] == 1 && $media['mid']) {
  133. # 判断是什么消息
  134. switch ($info['type']) {
  135. case 1:
  136. $type = 'text';
  137. if ($info['info']) {
  138. $param = array
  139. (
  140. 'content' => $info['info']
  141. );
  142. }
  143. break;
  144. case 2:
  145. case 3:
  146. $info['content'] = json_decode(base64_decode($info['content_2']), true);
  147. $type = 'news';
  148. if ($info['content']) {
  149. foreach ($info['content'] as $k => $v) {
  150. $param['articles'][$k] = array
  151. (
  152. 'title' => $v['title'],
  153. 'description' => $v['info'],
  154. 'url' => $v['link'],
  155. 'picurl' => $v['pic'],
  156. );
  157. }
  158. }
  159. break;
  160. case 4:
  161. $type = 'voice';
  162. $param = array
  163. (
  164. 'media_id' => $media['mid']
  165. );
  166. break;
  167. case 5:
  168. $type = 'video';
  169. if ($info['title']) {
  170. $param = array
  171. (
  172. 'media_id' => $media['mid'],
  173. 'title' => $info['title'],
  174. 'description' => $info['info'],
  175. );
  176. }
  177. break;
  178. case 6:
  179. $type = 'image';
  180. $param = array
  181. (
  182. 'media_id' => $media['mid']
  183. );
  184. break;
  185. case 7:
  186. $type = 'music';
  187. if ($info['title']) {
  188. $param = array
  189. (
  190. 'thumb_media_id' => $media['mid'],
  191. 'title' => $info['title'],
  192. 'description' => $info['info'],
  193. 'music_url' => $info['music_url'],
  194. 'hqmusicurl' => $info['music_hq_url'],
  195. );
  196. }
  197. break;
  198. case 8:
  199. $type = 'wxcard';
  200. if ($info['card']) {
  201. $param = array
  202. (
  203. 'card_id' => $info['card']
  204. );
  205. }
  206. break;
  207. }
  208. if (!isset($param)) {
  209. return;
  210. }
  211. $send = array
  212. (
  213. 'access_token' => 'token',
  214. 'msgtype' => $type,
  215. $type => $param,
  216. );
  217. # 寻找当前的发送方式
  218. if ($config['user'] == 2 && $config['openid']) {
  219. $user = explode("\r\n", $config['openid']);
  220. } elseif ($config['user'] == 1 && $config['group']) {
  221. //$group = Dever::load('weixin/group-one', $config['group']);
  222. if ($config['group']) {
  223. $user = Dever::load('weixin/user-getByGroup', array('where_groupid' => $config['group']));
  224. } else {
  225. return;
  226. }
  227. } elseif ($config['user'] == 3) {
  228. $uid = str_replace("\r\n", ',', $config['uid']);
  229. # 根据uid获取openid
  230. $user = Dever::load('weixin/user-getByUid', array('where_uid' => $uid));
  231. } elseif ($config['user'] == 4) {
  232. $user = Dever::load('weixin/user-getAll');
  233. } else {
  234. return;
  235. }
  236. if (isset($user)) {
  237. foreach ($user as $k => $v) {
  238. if (is_array($v)) {
  239. $v = $v['openid'];
  240. }
  241. $send['touser'] = $v;
  242. # 如果需要客服
  243. if (isset($account)) {
  244. $send['customservice'] = array
  245. (
  246. 'kf_account' => $account['account'],
  247. );
  248. }
  249. //print_r($send);die;
  250. Dever::$global['base']['temp_message'] = $send;
  251. $result = Dever::load('weixin/data.send_custom', $send);
  252. # 记录日志 必须是群发消息才记录
  253. if ($result && $result['data']) {
  254. $update['add_msg'] = $result['data']['errcode'] == 0 ? '发送成功' : '发送失败';
  255. $update['add_weixin_log'][0] = $result['data'];
  256. $update['add_message_id'] = $config['id'];
  257. $update['add_content_id'] = $info['id'];
  258. $update['add_name'] = $config['name'];
  259. $update['add_type'] = $config['type'];
  260. $update['add_config'][0] = $send;
  261. $update['add_openid'] = $v;
  262. Dever::load('weixin/message_log-insert', $update);
  263. }
  264. }
  265. }
  266. }
  267. return $result;
  268. }
  269. /**
  270. * 发送订阅消息
  271. *
  272. * @return mixed
  273. */
  274. private function add_1($config, $info, $media)
  275. {
  276. if ($info['state'] == 1 && $media['mid']) {
  277. # 判断是什么消息
  278. switch ($info['type']) {
  279. case 1:
  280. $type = 'text';
  281. $param = array
  282. (
  283. 'content' => $info['info']
  284. );
  285. break;
  286. case 2:
  287. case 3:
  288. $type = 'mpnews';
  289. $param = array
  290. (
  291. 'media_id' => $media['mid']
  292. );
  293. break;
  294. case 4:
  295. $type = 'voice';
  296. $param = array
  297. (
  298. 'media_id' => $media['mid']
  299. );
  300. break;
  301. case 5:
  302. # 为毛就video的参数不一样呢,还得加个判断
  303. $type = 'mpvideo';
  304. if($info['user'] == 2)
  305. {
  306. $type = 'video';
  307. }
  308. $param = array
  309. (
  310. 'media_id' => $media['mid'],
  311. 'title' => $info['title'],
  312. 'description' => $info['info'],
  313. );
  314. Dever::setInput('media_id', $param['media_id']);
  315. Dever::setInput('title', $param['title']);
  316. Dever::setInput('description', $param['description']);
  317. # 拿这个信息去换取新的media_id吧
  318. $video = Dever::load('weixin/data.media_add_video');
  319. if($video && isset($video['data']) && isset($video['data']['media_id']))
  320. {
  321. $param['media_id'] = $video['data']['media_id'];
  322. }
  323. else
  324. {
  325. return;
  326. }
  327. break;
  328. case 6:
  329. $type = 'image';
  330. $param = array
  331. (
  332. 'media_id' => $media['mid']
  333. );
  334. break;
  335. case 7:
  336. $type = 'wxcard';
  337. $param = array
  338. (
  339. 'card_id' => $info['card']
  340. );
  341. break;
  342. }
  343. $send = array
  344. (
  345. 'access_token' => 'token',
  346. 'msgtype' => $type,
  347. $type => $param,
  348. );
  349. # 寻找当前的发送方式
  350. if ($config['user'] == 2 && $config['openid']) {
  351. $method = 'send_openid';
  352. $send['touser'] = explode("\r\n", $config['openid']);
  353. } elseif ($config['user'] == 1 && $config['group']) {
  354. $method = 'send_all';
  355. //$group = Dever::load('weixin/group-one', $config['group']);
  356. if ($config['group']) {
  357. $send['filter'] = array
  358. (
  359. 'is_to_all' => false,
  360. 'group_id' => $config['group'],
  361. );
  362. } else {
  363. return;
  364. }
  365. } elseif ($config['user'] == 3) {
  366. $uid = str_replace("\r\n", ',', $config['uid']);
  367. # 根据uid获取openid
  368. $user = Dever::load('weixin/user-getByUid', array('where_uid' => $uid));
  369. if(!$user)
  370. {
  371. return;
  372. }
  373. $send['touser'] = array_keys($user);
  374. } elseif ($config['user'] == 4) {
  375. $method = 'send_all';
  376. # 所有用户
  377. $send['filter'] = array
  378. (
  379. 'is_to_all' => true,
  380. );
  381. } else {
  382. return;
  383. }
  384. }
  385. Dever::$global['base']['temp_message'] = $send;
  386. $result = Dever::load('weixin/data.' . $method, $send);
  387. # 记录日志
  388. if ($result && $result['data']) {
  389. /*
  390. 这里是否需要记录这两个返回值呢,日志中已经记录了,看有没有用处了,暂时不记录了
  391. $set['set_msg_id'] = $result['data']['msg_id'];
  392. $set['set_msg_data_id'] = $result['data']['msg_data_id'];
  393. $set['where_id'] = $config['id'];
  394. Dever::load('weixin/message-update', $set);
  395. */
  396. $update['add_msg'] = $result['data']['errcode'] == 0 ? '推送成功' : '推送失败';
  397. $update['add_weixin_log'][0] = $result['data'];
  398. $update['add_message_id'] = $config['id'];
  399. $update['add_content_id'] = $info['id'];
  400. $update['add_name'] = $config['name'];
  401. $update['add_type'] = $config['type'];
  402. $update['add_config'][0] = $send;
  403. //$update['add_openid'] = $v;
  404. Dever::load('weixin/message_log-insert', $update);
  405. }
  406. }
  407. /**
  408. * 发送消息的数据
  409. *
  410. * @return mixed
  411. */
  412. public function data()
  413. {
  414. return isset(Dever::$global['base']['temp_message']) ? Dever::$global['base']['temp_message'] : array();
  415. }
  416. }