Msg.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | 模板消息
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Applet\Src;
  8. use Dever;
  9. use Main\Lib\Wechat;
  10. use Main\Lib\Core;
  11. class Msg
  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', '2');
  40. return $this->send();
  41. }
  42. # 发送单条模板消息
  43. public function send()
  44. {
  45. $key = Dever::input('key');
  46. $project_id = Dever::input('project_id');
  47. $touser = Dever::input('touser');
  48. $page = Dever::input('page');
  49. $data = Dever::input('data');
  50. $form_id = Dever::input('form_id');
  51. $emphasis_keyword = Dever::input('emphasis_keyword');
  52. $this->sendOne($key, $project_id, $touser, $page, $data, $form_id, $emphasis_keyword);
  53. return 'ok';
  54. }
  55. # 发送单条模板消息
  56. public function sendOne($key, $project_id, $touser, $page, $data, $form_id, $emphasis_keyword = '')
  57. {
  58. if ($project_id > 0 && $key && $touser && $page && $data) {
  59. $info = Dever::db('applet/msg')->one(array('option_key' => $key, 'option_project_id' => $project_id));
  60. if ($info) {
  61. $update = array();
  62. $update['project_id'] = $info['project_id'];
  63. $update['msg_id'] = $info['id'];
  64. $update['touser'] = $touser;
  65. $update['page'] = $page;
  66. $update['data'] = $data;
  67. $update['form_id'] = $form_id;
  68. $update['emphasis_keyword'] = $emphasis_keyword;
  69. if (!$update['form_id']) {
  70. $update['form_id'] = $this->getFormId($info['project_id'], $update['touser']);
  71. }
  72. $where = array();
  73. $where['option_msg_id'] = $info['id'];
  74. $where['option_touser'] = $update['touser'];
  75. //$where['option_data'] = $update['data'];
  76. //$where['option_form_id'] = $update['form_id'];
  77. $where['option_page'] = $update['page'];
  78. $id = Dever::upinto('applet/msg_log', $where, $update);
  79. Core::run($info['project_id'], 'send_msg', 'msg.sendAction', 'msg.sendLog', 'applet', $id);
  80. }
  81. }
  82. }
  83. # 发送多条模板消息 入队
  84. public function sendMul()
  85. {
  86. $key = Dever::input('key');
  87. $project_id = Dever::input('project_id');
  88. $page = Dever::input('page');
  89. $data = Dever::input('data');
  90. $emphasis_keyword = Dever::input('emphasis_keyword');
  91. if ($project_id > 0 && $key && $page && $data) {
  92. # 获取所有拥有formid的openid,并去重
  93. $data = Dever::db('applet/msg_form')->getAllGroupByOpenid(array('option_project_id' => $project_id));
  94. print_r($data);die;
  95. if ($data) {
  96. Dever::import('queue');
  97. foreach ($data as $k => $v) {
  98. if (is_numeric($v['form_id'])) {
  99. $param = array();
  100. $param['key'] = $key;
  101. $param['project_id'] = $project_id;
  102. $param['touser'] = $v['openid'];
  103. $param['page'] = $page;
  104. $param['data'] = $data;
  105. $param['form_id'] = $v['form_id'];
  106. $param['emphasis_keyword'] = $emphasis_keyword;
  107. Dever::push($param);
  108. }
  109. }
  110. }
  111. }
  112. return 'ok';
  113. }
  114. # 发送多条模板消息 出队 每小时执行一次
  115. public function cron()
  116. {
  117. Dever::import('queue');
  118. while ($data = Dever::pop()) {
  119. $this->sendOne($data['key'], $data['project_id'], $data['touser'], $data['page'], $data['data'], $data['form_id'], $data['emphasis_keyword']);
  120. }
  121. }
  122. public function getFormId($project_id, $openid)
  123. {
  124. # 获取一个form_id,取最新的100条,然后随机取
  125. $info = Dever::db('applet/msg_form')->getAll(array('option_openid' => $openid, 'option_project_id' => $project_id));
  126. if ($info) {
  127. $key = array_rand($info, 1);
  128. $info = $info[$key];
  129. Dever::db('applet/msg_form')->delete($info['id']);
  130. return $info['form_id'];
  131. } else {
  132. Dever::alert('错误的form_id');
  133. }
  134. }
  135. # 记录form_id 之后加上清理过期的form_id 七天过期
  136. public function write_form_id()
  137. {
  138. $id = Dever::input('project_id');
  139. if ($id > 0) {
  140. $update = array();
  141. $update['project_id'] = $id;
  142. $update['form_id'] = Dever::input('form_id');
  143. $update['openid'] = Dever::input('openid');
  144. $where = array();
  145. $where['option_openid'] = $update['openid'];
  146. $where['option_project_id'] = $update['project_id'];
  147. $where['option_form_id'] = $update['form_id'];
  148. $id = Dever::upinto('applet/msg_form', $where, $update);
  149. }
  150. return 'ok';
  151. }
  152. public function sendAction($id)
  153. {
  154. $info = Dever::db('applet/msg_log')->one($id);
  155. if ($info) {
  156. $msg = Dever::db('applet/msg')->one($info['msg_id']);
  157. $info['template_id'] = $msg['template_id'];
  158. if ($info['data']) {
  159. $info['data'] = json_decode($info['data'], true);
  160. }
  161. return $info;
  162. } else {
  163. die;
  164. }
  165. }
  166. public function sendLog($project_id, $data, $id = false)
  167. {
  168. $info = Dever::db('applet/msg_log')->one($id);
  169. if ($info) {
  170. $update['where_id'] = $info['id'];
  171. $update['result'] = json_encode($data);
  172. if ($data['errmsg'] == 'ok') {
  173. $update['status'] = 2;
  174. } else {
  175. $update['status'] = 3;
  176. }
  177. $update['num'] = $info['num'] + 1;
  178. Dever::db('applet/msg_log')->update($update);
  179. }
  180. }
  181. # 同步微信消息模板到本地
  182. public function sync()
  183. {
  184. $project = Dever::db('main/project')->state(array('option_type' => 3));
  185. if ($project) {
  186. foreach ($project as $k => $v) {
  187. Core::run($v['id'], 'get_msg', '', 'msg.load', 'applet');
  188. }
  189. }
  190. return 'ok';
  191. }
  192. public function load($project_id, $data, $param = false)
  193. {
  194. if (isset($data['list']) && $data['list']) {
  195. foreach ($data['list'] as $k => $v) {
  196. $update = array();
  197. $update['project_id'] = $project_id;
  198. $update['template_id'] = $v['template_id'];
  199. $update['content'] = $v['content'];
  200. $update['name'] = $v['title'];
  201. $update['example'] = $v['example'];
  202. $id = Dever::upinto('applet/msg', array('option_project_id' => $project_id, 'option_template_id' => $v['template_id']), $update);
  203. }
  204. }
  205. return array();
  206. }
  207. }