|
@@ -0,0 +1,246 @@
|
|
|
+<?php
|
|
|
+/*
|
|
|
+|--------------------------------------------------------------------------
|
|
|
+| 订阅消息
|
|
|
+|--------------------------------------------------------------------------
|
|
|
+*/
|
|
|
+namespace Wechat_applet\Src;
|
|
|
+
|
|
|
+use Dever;
|
|
|
+use Token\Lib\Wechat;
|
|
|
+use Token\Lib\Core;
|
|
|
+
|
|
|
+class Subscribe
|
|
|
+{
|
|
|
+ # 发送模板消息
|
|
|
+ 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', '1');
|
|
|
+
|
|
|
+ $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 = json_encode(Dever::input('data'));
|
|
|
+ $data = Dever::input('data');
|
|
|
+ $miniprogram_state = Dever::input('type', 'formal');
|
|
|
+ $this->sendOne($key, $project_id, $touser, $page, $data, $miniprogram_state);
|
|
|
+
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ # 发送单条模板消息
|
|
|
+ public function sendOne($key, $project_id, $touser, $path, $data, $miniprogram_state = '', $state = false)
|
|
|
+ {
|
|
|
+ if ($project_id > 0 && $key && $touser && $path && $data) {
|
|
|
+ $info = Dever::db('wechat_applet/subscribe')->one(array('option_key' => $key, 'option_project_id' => $project_id));
|
|
|
+ if ($info) {
|
|
|
+ $update = array();
|
|
|
+ $update['project_id'] = $info['project_id'];
|
|
|
+ $update['subscribe_id'] = $info['id'];
|
|
|
+ $update['touser'] = $touser;
|
|
|
+ $update['path'] = $path;
|
|
|
+ $update['data'] = $data;
|
|
|
+ $update['status'] = 1;
|
|
|
+ $update['miniprogram_state'] = $miniprogram_state;
|
|
|
+
|
|
|
+ $where = array();
|
|
|
+ $where['option_subscribe_id'] = $info['id'];
|
|
|
+ $where['option_touser'] = $update['touser'];
|
|
|
+ $where['option_path'] = $update['path'];
|
|
|
+
|
|
|
+ $id = Dever::upinto('wechat_applet/subscribe_log', $where, $update);
|
|
|
+
|
|
|
+ Core::run($info['project_id'], 'send_subscribe', 'subscribe.sendAction', 'subscribe.sendLog', 'wechat_applet', $id, $state);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function submit()
|
|
|
+ {
|
|
|
+ # 立刻发送
|
|
|
+ $id = Dever::input('id');
|
|
|
+ if ($id > 0) {
|
|
|
+ $info = Dever::db('wechat_applet/subscribe_send')->one($id);
|
|
|
+ if ($info) {
|
|
|
+ $project = explode(',', $info['project_id']);
|
|
|
+ Dever::setInput('key', $info['key']);
|
|
|
+ Dever::setInput('page', $info['page']);
|
|
|
+ Dever::setInput('miniprogram_state', $info['miniprogram_state']);
|
|
|
+ $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');
|
|
|
+ $miniprogram_state = Dever::input('miniprogram_state');
|
|
|
+ if ($project_id > 0 && $key && $page && $data) {
|
|
|
+ $param = array();
|
|
|
+ $param['key'] = $key;
|
|
|
+ $param['project_id'] = $project_id;
|
|
|
+ $param['touser'] = $v['openid'];
|
|
|
+ $param['page'] = $page;
|
|
|
+ $param['data'] = $data;
|
|
|
+ $param['miniprogram_state'] = $miniprogram_state;
|
|
|
+ if (Dever::project('queue')) {
|
|
|
+ Dever::import('queue');
|
|
|
+ Dever::push($param);
|
|
|
+ } else {
|
|
|
+ $this->sendOne($param['key'], $param['project_id'], $param['touser'], $param['page'], $param['data'], $param['miniprogram_state']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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['miniprogram_state'], $state);
|
|
|
+ } else {
|
|
|
+ $num = $total+1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ public function sendAction($id)
|
|
|
+ {
|
|
|
+ $info = Dever::db('wechat_applet/subscribe_log')->one($id);
|
|
|
+ if ($info) {
|
|
|
+ $info['page'] = $info['path'];
|
|
|
+ $msg = Dever::db('wechat_applet/subscribe')->one($info['subscribe_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/subscribe_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/subscribe_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_subscribe', '', 'subscribe.load', 'wechat_applet');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ public function load($project_id, $data, $param = false)
|
|
|
+ {
|
|
|
+ if (isset($data['data']) && $data['data']) {
|
|
|
+ foreach ($data['list'] as $k => $v) {
|
|
|
+ $update = array();
|
|
|
+ $update['project_id'] = $project_id;
|
|
|
+ $update['template_id'] = $v['priTmplId'];
|
|
|
+ $update['content'] = $v['content'];
|
|
|
+ $update['name'] = $v['title'];
|
|
|
+ $update['example'] = $v['example'];
|
|
|
+ $update['type'] = $v['type'];
|
|
|
+ $id = Dever::upinto('wechat_applet/subscribe', array('option_project_id' => $project_id, 'option_template_id' => $v['template_id']), $update);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return array();
|
|
|
+ }
|
|
|
+}
|