|
@@ -0,0 +1,146 @@
|
|
|
+<?php
|
|
|
+/*
|
|
|
+|--------------------------------------------------------------------------
|
|
|
+| 模板消息
|
|
|
+|--------------------------------------------------------------------------
|
|
|
+*/
|
|
|
+namespace Applet\Src;
|
|
|
+
|
|
|
+use Dever;
|
|
|
+use Main\Lib\Wechat;
|
|
|
+use Main\Lib\Core;
|
|
|
+
|
|
|
+class Msg
|
|
|
+{
|
|
|
+ # 发送模板消息
|
|
|
+ public function send()
|
|
|
+ {
|
|
|
+ $id = Dever::input('id');
|
|
|
+ if ($id > 0) {
|
|
|
+ $info = Dever::db('applet/msg')->one($id);
|
|
|
+ if ($info) {
|
|
|
+ $update = array();
|
|
|
+ $update['project_id'] = $info['project_id'];
|
|
|
+ $update['msg_id'] = $info['id'];
|
|
|
+ $update['touser'] = Dever::input('touser');
|
|
|
+ $update['page'] = Dever::input('page');
|
|
|
+ $update['data'] = Dever::input('data');
|
|
|
+ $update['form_id'] = Dever::input('form_id');
|
|
|
+ $update['emphasis_keyword'] = Dever::input('emphasis_keyword');
|
|
|
+
|
|
|
+ if (!$update['form_id']) {
|
|
|
+ $update['form_id'] = $this->getFormId();
|
|
|
+ }
|
|
|
+
|
|
|
+ $where = array();
|
|
|
+ $where['msg_id'] = $info['id'];
|
|
|
+ $where['touser'] = $update['touser'];
|
|
|
+ $where['data'] = $update['data'];
|
|
|
+ $where['form_id'] = $update['form_id'];
|
|
|
+ $where['page'] = $update['page'];
|
|
|
+
|
|
|
+ $id = Dever::upinto('applet/msg_log', $where, $update);
|
|
|
+
|
|
|
+ Core::run($info['project_id'], 'send_msg', 'msg.sendAction', 'msg.sendLog', 'applet', $id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getFormId()
|
|
|
+ {
|
|
|
+ # 获取一个form_id,取最新的100条,然后随机取
|
|
|
+ $info = Dever::db('applet/msg_form')->getAll();
|
|
|
+ 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('id');
|
|
|
+ if ($id > 0) {
|
|
|
+ $info = Dever::db('applet/msg')->one($id);
|
|
|
+
|
|
|
+ $update = array();
|
|
|
+ $update['project_id'] = $info['project_id'];
|
|
|
+ $update['form_id'] = Dever::input('form_id');
|
|
|
+
|
|
|
+ $where = array();
|
|
|
+ $where['project_id'] = $info['project_id'];
|
|
|
+ $where['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();
|
|
|
+ }
|
|
|
+}
|