|
@@ -0,0 +1,295 @@
|
|
|
|
+<?php
|
|
|
|
+/*
|
|
|
|
+|--------------------------------------------------------------------------
|
|
|
|
+| 模板消息
|
|
|
|
+|--------------------------------------------------------------------------
|
|
|
|
+*/
|
|
|
|
+namespace Wechat_applet\Src;
|
|
|
|
+
|
|
|
|
+use Dever;
|
|
|
|
+use Token\Lib\Wechat;
|
|
|
|
+use Token\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');
|
|
|
|
+
|
|
|
|
+ $type = Dever::input('type', 1);
|
|
|
|
+ if ($type == 1) {
|
|
|
|
+ return $this->send();
|
|
|
|
+ } else {
|
|
|
|
+ return $this->sendMul();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 发送单条模板消息
|
|
|
|
+ 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 = false, $emphasis_keyword = '', $state = false)
|
|
|
|
+ {
|
|
|
|
+ if ($project_id > 0 && $key && $touser && $page && $data) {
|
|
|
|
+ $info = Dever::db('wechat_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['status'] = 1;
|
|
|
|
+ $update['emphasis_keyword'] = $emphasis_keyword;
|
|
|
|
+
|
|
|
|
+ if (!$update['form_id']) {
|
|
|
|
+ $update['form_id'] = $this->getFormId($info['project_id'], $update['touser']);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!$update['form_id']) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $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('wechat_applet/msg_log', $where, $update);
|
|
|
|
+
|
|
|
|
+ Core::run($info['project_id'], 'send_msg', 'msg.sendAction', 'msg.sendLog', 'applet', $id, $state);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function submit()
|
|
|
|
+ {
|
|
|
|
+ # 立刻发送
|
|
|
|
+ $id = Dever::input('id');
|
|
|
|
+ if ($id > 0) {
|
|
|
|
+ $info = Dever::db('wechat_applet/msg_send')->one($id);
|
|
|
|
+ if ($info) {
|
|
|
|
+ $project = explode(',', $info['project_id']);
|
|
|
|
+ Dever::setInput('key', $info['key']);
|
|
|
|
+ Dever::setInput('page', $info['page']);
|
|
|
|
+ $send = $this->getData($info['content']);
|
|
|
|
+
|
|
|
|
+ foreach ($project as $k => $v) {
|
|
|
|
+ Dever::setInput('project_id', $v);
|
|
|
|
+ $this->sendMul();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Dever::alert('提交成功,请到模板消息日志中查看发送记录');
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 解析data
|
|
|
|
+ public function getData($data = false)
|
|
|
|
+ {
|
|
|
|
+ $send = Dever::input('data');
|
|
|
|
+ if (!$send && $data) {
|
|
|
|
+ $send = array();
|
|
|
|
+ $data = json_decode(base64_decode($data), true);
|
|
|
|
+ if ($data) {
|
|
|
|
+ foreach ($data as $k => $v) {
|
|
|
|
+ $send[$v['key']]['value'] = $v['value'];
|
|
|
|
+ }
|
|
|
|
+ $send = json_encode($send, JSON_UNESCAPED_UNICODE);
|
|
|
|
+ Dever::setInput('data', $send);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $send;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 发送多条模板消息 入队
|
|
|
|
+ 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,并去重
|
|
|
|
+ $list = Dever::db('wechat_applet/msg_form')->getAllGroupByOpenid(array('option_project_id' => $project_id));
|
|
|
|
+ if ($list) {
|
|
|
|
+ Dever::import('queue');
|
|
|
|
+ foreach ($list as $k => $v) {
|
|
|
|
+ if ($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::db('wechat_applet/msg_form')->delete($v['id']);
|
|
|
|
+ Dever::push($param);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 'ok';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 发送多条模板消息 出队 每小时执行一次
|
|
|
|
+ public function cron()
|
|
|
|
+ {
|
|
|
|
+ Dever::import('queue');
|
|
|
|
+ $state = true;//同步发送
|
|
|
|
+ $total = 100;
|
|
|
|
+ $num = 0;
|
|
|
|
+ while($num < $total) {
|
|
|
|
+ $data = Dever::pop();
|
|
|
|
+ if ($data) {
|
|
|
|
+ $num++;
|
|
|
|
+ $state = true;
|
|
|
|
+ $this->sendOne($data['key'], $data['project_id'], $data['touser'], $data['page'], $data['data'], $data['form_id'], $data['emphasis_keyword'], $state);
|
|
|
|
+ } else {
|
|
|
|
+ $num = $total+1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return 'ok';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function getFormId($project_id, $openid)
|
|
|
|
+ {
|
|
|
|
+ # 获取一个form_id,取最新的100条,然后随机取
|
|
|
|
+ $info = Dever::db('wechat_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('wechat_applet/msg_form')->delete($info['id']);
|
|
|
|
+ return $info['form_id'];
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 记录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('wechat_applet/msg_form', $where, $update);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 'ok';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function sendAction($id)
|
|
|
|
+ {
|
|
|
|
+ $info = Dever::db('wechat_applet/msg_log')->one($id);
|
|
|
|
+ if ($info) {
|
|
|
|
+ $msg = Dever::db('wechat_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('wechat_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('wechat_applet/msg_log')->update($update);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ # 同步微信消息模板到本地
|
|
|
|
+ public function sync()
|
|
|
|
+ {
|
|
|
|
+ $project = Dever::db('token/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('wechat_applet/msg', array('option_project_id' => $project_id, 'option_template_id' => $v['template_id']), $update);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return array();
|
|
|
|
+ }
|
|
|
|
+}
|