Msg.php 6.1 KB

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