Subscribe.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | 订阅消息
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Wechat_applet\Src;
  8. use Dever;
  9. use Token\Lib\Wechat;
  10. use Token\Lib\Core;
  11. class Subscribe
  12. {
  13. # 发送模板消息
  14. public function test()
  15. {
  16. $page = 'pages/index/index';
  17. $data = array
  18. (
  19. 'keyword1' => array
  20. (
  21. 'value' => '测试活动',
  22. ),
  23. 'keyword2' => array
  24. (
  25. 'value' => date('Y-m-d H:i:s'),
  26. ),
  27. 'keyword3' => array
  28. (
  29. 'value' => '欢迎参加',
  30. ),
  31. );
  32. $data = json_encode($data);
  33. //Dever::setInput('touser', 'oIZ895bO9a_TT_ouYCQLPFMod57c');
  34. //Dever::setInput('touser', 'oIZ895bO9a_TT_ouYCQLPFMod57c');
  35. Dever::setInput('page', $page);
  36. Dever::setInput('data', $data);
  37. //Dever::setInput('form_id', '1526295575657');
  38. Dever::setInput('key', 'act');
  39. Dever::setInput('project_id', '1');
  40. $type = Dever::input('type', 1);
  41. if ($type == 1) {
  42. return $this->send();
  43. } else {
  44. return $this->sendMul();
  45. }
  46. }
  47. # 发送单条模板消息
  48. public function send()
  49. {
  50. $key = Dever::input('key');
  51. $project_id = Dever::input('project_id');
  52. $touser = Dever::input('touser');
  53. $page = Dever::input('page');
  54. //$data = json_encode(Dever::input('data'));
  55. $data = Dever::input('data');
  56. $miniprogram_state = Dever::input('type', 'formal');
  57. $this->sendOne($key, $project_id, $touser, $page, $data, $miniprogram_state);
  58. return 'ok';
  59. }
  60. # 发送单条模板消息
  61. public function sendOne($key, $project_id, $touser, $path, $data, $miniprogram_state = '', $state = false)
  62. {
  63. if ($project_id > 0 && $key && $touser && $path && $data) {
  64. $info = Dever::db('wechat_applet/subscribe')->one(array('option_key' => $key, 'option_project_id' => $project_id));
  65. if ($info) {
  66. $update = array();
  67. $update['project_id'] = $info['project_id'];
  68. $update['subscribe_id'] = $info['id'];
  69. $update['touser'] = $touser;
  70. $update['path'] = $path;
  71. $update['data'] = $data;
  72. $update['status'] = 1;
  73. $update['miniprogram_state'] = $miniprogram_state;
  74. $where = array();
  75. $where['option_subscribe_id'] = $info['id'];
  76. $where['option_touser'] = $update['touser'];
  77. $where['option_path'] = $update['path'];
  78. $id = Dever::upinto('wechat_applet/subscribe_log', $where, $update);
  79. Core::run($info['project_id'], 'send_subscribe', 'subscribe.sendAction', 'subscribe.sendLog', 'wechat_applet', $id, $state);
  80. }
  81. }
  82. }
  83. public function submit()
  84. {
  85. # 立刻发送
  86. $id = Dever::input('id');
  87. if ($id > 0) {
  88. $info = Dever::db('wechat_applet/subscribe_send')->one($id);
  89. if ($info) {
  90. $project = explode(',', $info['project_id']);
  91. Dever::setInput('key', $info['key']);
  92. Dever::setInput('page', $info['page']);
  93. Dever::setInput('miniprogram_state', $info['miniprogram_state']);
  94. $send = $this->getData($info['content']);
  95. foreach ($project as $k => $v) {
  96. Dever::setInput('project_id', $v);
  97. $this->sendMul();
  98. }
  99. }
  100. }
  101. Dever::alert('提交成功,请到订阅消息日志中查看发送记录');
  102. return;
  103. }
  104. # 解析data
  105. public function getData($data = false)
  106. {
  107. $send = Dever::input('data');
  108. if (!$send && $data) {
  109. $send = array();
  110. $data = json_decode(base64_decode($data), true);
  111. if ($data) {
  112. foreach ($data as $k => $v) {
  113. $send[$v['key']]['value'] = $v['value'];
  114. }
  115. $send = json_encode($send, JSON_UNESCAPED_UNICODE);
  116. Dever::setInput('data', $send);
  117. }
  118. }
  119. return $send;
  120. }
  121. # 发送多条模板消息 入队
  122. public function sendMul()
  123. {
  124. $key = Dever::input('key');
  125. $project_id = Dever::input('project_id');
  126. $page = Dever::input('page');
  127. $data = Dever::input('data');
  128. $miniprogram_state = Dever::input('miniprogram_state');
  129. if ($project_id > 0 && $key && $page && $data) {
  130. $param = array();
  131. $param['key'] = $key;
  132. $param['project_id'] = $project_id;
  133. $param['touser'] = $v['openid'];
  134. $param['page'] = $page;
  135. $param['data'] = $data;
  136. $param['miniprogram_state'] = $miniprogram_state;
  137. if (Dever::project('queue')) {
  138. Dever::import('queue');
  139. Dever::push($param);
  140. } else {
  141. $this->sendOne($param['key'], $param['project_id'], $param['touser'], $param['page'], $param['data'], $param['miniprogram_state']);
  142. }
  143. }
  144. return 'ok';
  145. }
  146. # 发送多条模板消息 出队 每小时执行一次
  147. public function cron()
  148. {
  149. Dever::import('queue');
  150. $state = true;//同步发送
  151. $total = 100;
  152. $num = 0;
  153. while($num < $total) {
  154. $data = Dever::pop();
  155. if ($data) {
  156. $num++;
  157. $state = true;
  158. $this->sendOne($data['key'], $data['project_id'], $data['touser'], $data['page'], $data['data'], $data['miniprogram_state'], $state);
  159. } else {
  160. $num = $total+1;
  161. }
  162. }
  163. return 'ok';
  164. }
  165. public function sendAction($id)
  166. {
  167. $info = Dever::db('wechat_applet/subscribe_log')->one($id);
  168. if ($info) {
  169. $info['page'] = $info['path'];
  170. $msg = Dever::db('wechat_applet/subscribe')->one($info['subscribe_id']);
  171. $info['template_id'] = $msg['template_id'];
  172. if ($info['data']) {
  173. $info['data'] = json_decode($info['data'], true);
  174. }
  175. return $info;
  176. } else {
  177. die;
  178. }
  179. }
  180. public function sendLog($project_id, $data, $id = false)
  181. {
  182. $info = Dever::db('wechat_applet/subscribe_log')->one($id);
  183. if ($info) {
  184. $update['where_id'] = $info['id'];
  185. $update['result'] = json_encode($data);
  186. if ($data['errmsg'] == 'ok') {
  187. $update['status'] = 2;
  188. } else {
  189. $update['status'] = 3;
  190. }
  191. $update['num'] = $info['num'] + 1;
  192. Dever::db('wechat_applet/subscribe_log')->update($update);
  193. }
  194. }
  195. # 同步微信消息模板到本地
  196. public function sync()
  197. {
  198. $project = Dever::db('token/project')->state(array('option_type' => 3));
  199. if ($project) {
  200. foreach ($project as $k => $v) {
  201. Core::run($v['id'], 'get_subscribe', '', 'subscribe.load', 'wechat_applet');
  202. }
  203. }
  204. return 'ok';
  205. }
  206. public function load($project_id, $data, $param = false)
  207. {
  208. if (isset($data['data']) && $data['data']) {
  209. Dever::db('wechat_applet/msg')->updates(array('option_project_id' => $project_id, 'set_state' => 2));
  210. foreach ($data['data'] as $k => $v) {
  211. $update = array();
  212. $update['project_id'] = $project_id;
  213. $update['template_id'] = $v['priTmplId'];
  214. $update['content'] = $v['content'];
  215. $update['name'] = $v['title'];
  216. $update['example'] = $v['example'];
  217. $update['type'] = $v['type'];
  218. $update['state'] = 1;
  219. $id = Dever::upinto('wechat_applet/subscribe', array('option_project_id' => $project_id, 'option_template_id' => $update['template_id']), $update);
  220. }
  221. }
  222. return array();
  223. }
  224. # 获取模板列表
  225. public function getList()
  226. {
  227. $key = Dever::input('key');
  228. $project_id = Dever::input('project_id');
  229. $where['project_id'] = $project_id;
  230. if ($key) {
  231. $where['key'] => $key;
  232. }
  233. $data = Dever::db('wechat_applet/subscribe')->getAll($where);
  234. return $data;
  235. }
  236. }