123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | 模板消息
- |--------------------------------------------------------------------------
- */
- namespace Applet\Src;
- use Dever;
- use Main\Lib\Wechat;
- use Main\Lib\Core;
- class Msg
- {
- # 发送模板消息
- public function test()
- {
- $page = 'pages/index/index';
- $data = array
- (
- 'keyword1' => array
- (
- 'value' => '测试活动',
- ),
- 'keyword2' => array
- (
- 'value' => date('Y-m-d H:i:s'),
- ),
- 'keyword3' => array
- (
- 'value' => '欢迎参加',
- ),
- );
- $data = json_encode($data);
- //Dever::setInput('touser', 'oIZ895bO9a_TT_ouYCQLPFMod57c');
-
- //Dever::setInput('touser', 'oIZ895bO9a_TT_ouYCQLPFMod57c');
- Dever::setInput('page', $page);
- Dever::setInput('data', $data);
- //Dever::setInput('form_id', '1526295575657');
- Dever::setInput('key', 'act');
- Dever::setInput('project_id', '2');
- return $this->send();
- }
- # 发送单条模板消息
- public function send()
- {
- $key = Dever::input('key');
- $project_id = Dever::input('project_id');
- $touser = Dever::input('touser');
- $page = Dever::input('page');
- $data = Dever::input('data');
- $form_id = Dever::input('form_id');
- $emphasis_keyword = Dever::input('emphasis_keyword');
- $this->sendOne($key, $project_id, $touser, $page, $data, $form_id, $emphasis_keyword);
- return 'ok';
- }
- # 发送单条模板消息
- public function sendOne($key, $project_id, $touser, $page, $data, $form_id, $emphasis_keyword = '')
- {
- if ($project_id > 0 && $key && $touser && $page && $data) {
- $info = Dever::db('applet/msg')->one(array('option_key' => $key, 'option_project_id' => $project_id));
- if ($info) {
- $update = array();
- $update['project_id'] = $info['project_id'];
- $update['msg_id'] = $info['id'];
- $update['touser'] = $touser;
- $update['page'] = $page;
- $update['data'] = $data;
- $update['form_id'] = $form_id;
- $update['emphasis_keyword'] = $emphasis_keyword;
- if (!$update['form_id']) {
- $update['form_id'] = $this->getFormId($info['project_id'], $update['touser']);
- }
- $where = array();
- $where['option_msg_id'] = $info['id'];
- $where['option_touser'] = $update['touser'];
- //$where['option_data'] = $update['data'];
- //$where['option_form_id'] = $update['form_id'];
- $where['option_page'] = $update['page'];
- $id = Dever::upinto('applet/msg_log', $where, $update);
- Core::run($info['project_id'], 'send_msg', 'msg.sendAction', 'msg.sendLog', 'applet', $id);
- }
- }
- }
- # 发送多条模板消息 入队
- public function sendMul()
- {
- $key = Dever::input('key');
- $project_id = Dever::input('project_id');
- $page = Dever::input('page');
- $data = Dever::input('data');
- $emphasis_keyword = Dever::input('emphasis_keyword');
- if ($project_id > 0 && $key && $page && $data) {
- # 获取所有拥有formid的openid,并去重
- $data = Dever::db('applet/msg_form')->getAllGroupByOpenid(array('option_project_id' => $project_id));
- print_r($data);die;
- if ($data) {
- Dever::import('queue');
- foreach ($data as $k => $v) {
- if (is_numeric($v['form_id'])) {
- $param = array();
- $param['key'] = $key;
- $param['project_id'] = $project_id;
- $param['touser'] = $v['openid'];
- $param['page'] = $page;
- $param['data'] = $data;
- $param['form_id'] = $v['form_id'];
- $param['emphasis_keyword'] = $emphasis_keyword;
- Dever::push($param);
- }
- }
- }
- }
- return 'ok';
- }
- # 发送多条模板消息 出队 每小时执行一次
- public function cron()
- {
- Dever::import('queue');
- while ($data = Dever::pop()) {
- $this->sendOne($data['key'], $data['project_id'], $data['touser'], $data['page'], $data['data'], $data['form_id'], $data['emphasis_keyword']);
- }
- }
- public function getFormId($project_id, $openid)
- {
- # 获取一个form_id,取最新的100条,然后随机取
- $info = Dever::db('applet/msg_form')->getAll(array('option_openid' => $openid, 'option_project_id' => $project_id));
- if ($info) {
- $key = array_rand($info, 1);
- $info = $info[$key];
- Dever::db('applet/msg_form')->delete($info['id']);
- return $info['form_id'];
- } else {
- Dever::alert('错误的form_id');
- }
- }
- # 记录form_id 之后加上清理过期的form_id 七天过期
- public function write_form_id()
- {
- $id = Dever::input('project_id');
- if ($id > 0) {
- $update = array();
- $update['project_id'] = $id;
- $update['form_id'] = Dever::input('form_id');
- $update['openid'] = Dever::input('openid');
- $where = array();
- $where['option_openid'] = $update['openid'];
- $where['option_project_id'] = $update['project_id'];
- $where['option_form_id'] = $update['form_id'];
- $id = Dever::upinto('applet/msg_form', $where, $update);
- }
- return 'ok';
- }
- public function sendAction($id)
- {
- $info = Dever::db('applet/msg_log')->one($id);
- if ($info) {
- $msg = Dever::db('applet/msg')->one($info['msg_id']);
- $info['template_id'] = $msg['template_id'];
- if ($info['data']) {
- $info['data'] = json_decode($info['data'], true);
- }
- return $info;
- } else {
- die;
- }
- }
- public function sendLog($project_id, $data, $id = false)
- {
- $info = Dever::db('applet/msg_log')->one($id);
- if ($info) {
- $update['where_id'] = $info['id'];
- $update['result'] = json_encode($data);
- if ($data['errmsg'] == 'ok') {
- $update['status'] = 2;
- } else {
- $update['status'] = 3;
- }
- $update['num'] = $info['num'] + 1;
- Dever::db('applet/msg_log')->update($update);
- }
- }
- # 同步微信消息模板到本地
- public function sync()
- {
- $project = Dever::db('main/project')->state(array('option_type' => 3));
- if ($project) {
- foreach ($project as $k => $v) {
- Core::run($v['id'], 'get_msg', '', 'msg.load', 'applet');
- }
- }
- return 'ok';
- }
- public function load($project_id, $data, $param = false)
- {
- if (isset($data['list']) && $data['list']) {
- foreach ($data['list'] as $k => $v) {
- $update = array();
- $update['project_id'] = $project_id;
- $update['template_id'] = $v['template_id'];
- $update['content'] = $v['content'];
- $update['name'] = $v['title'];
- $update['example'] = $v['example'];
- $id = Dever::upinto('applet/msg', array('option_project_id' => $project_id, 'option_template_id' => $v['template_id']), $update);
- }
- }
- return array();
- }
- }
|