初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,298 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\library\app\msgpush;
|
||||
|
||||
use think\Db;
|
||||
use think\Cookie;
|
||||
use app\common\library\Wechat;
|
||||
|
||||
class Msg
|
||||
{
|
||||
protected $sence_config = [];
|
||||
protected $msg_type = '';
|
||||
protected $remark = '';
|
||||
|
||||
protected $createtime = '';
|
||||
|
||||
|
||||
/**
|
||||
* 事件收集器
|
||||
* @param $msgType
|
||||
* @param $data
|
||||
* @return void
|
||||
*/
|
||||
public static function collect($msgType,$data=[]){
|
||||
try{
|
||||
$config = (new \app\admin\model\app\msgpush\Config())->getSenceConfig($msgType);
|
||||
|
||||
if(!$config || $config['status'] == 0){
|
||||
return false;
|
||||
}
|
||||
|
||||
\app\admin\model\app\msgpush\Log::insert([
|
||||
'msg_type'=>$msgType,
|
||||
'uniacid'=>UNIACID,
|
||||
'data'=>json_encode($data),
|
||||
'createtime'=>time(),
|
||||
'status'=>0
|
||||
]);
|
||||
}catch (\Exception $e){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return false|void
|
||||
*/
|
||||
public function handle(){
|
||||
|
||||
$data = \app\admin\model\app\msgpush\Log::where([
|
||||
'status'=>0
|
||||
])->order('id','asc')->field([
|
||||
'data','msg_type','id','createtime'
|
||||
])->find();
|
||||
|
||||
if(!$data){
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->sence_config = (new \app\admin\model\app\msgpush\Config())->getSenceConfig($data['msg_type']);
|
||||
|
||||
if(!$this->sence_config || $this->sence_config['status'] == 0){
|
||||
$this->remark = '未获取到消息配置';
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->msg_type = $data['msg_type'];
|
||||
$this->createtime = $data['createtime'];
|
||||
|
||||
$result = $this->event($data['msg_type'],json_decode($data['data'],true));
|
||||
|
||||
\app\admin\model\app\msgpush\Log::where([
|
||||
'id'=>$data['id']
|
||||
])->update([
|
||||
'status'=>$result ? 1 : 2,
|
||||
'remark'=>$this->remark,
|
||||
'action_time'=>time()
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function event($msgType,$data){
|
||||
|
||||
$msgBody = [];
|
||||
$toUsers = [];
|
||||
$detailJump = [];
|
||||
|
||||
switch ($msgType){
|
||||
case 'order_success':
|
||||
//下单成功通知
|
||||
$order = (new \app\common\model\order\Order())->getOrderDetail($data['order_no']);
|
||||
if(!$order){
|
||||
$this->remark = '未查询到相关订单';
|
||||
return false;
|
||||
}
|
||||
$params = [
|
||||
'course_name'=>$order['goodsList'][0]['snapshoot']['name'],
|
||||
'time'=>date('Y-m-d H:i:s',$order['createtime']),
|
||||
'price'=>'¥'.$order['real_price']
|
||||
];
|
||||
$toUsers = $this->getToUser(['user_id'=>$order['user_id']]);
|
||||
|
||||
$detailJump = $this->getDetailJump('order',['order_no'=>$data['order_no']]);
|
||||
break;
|
||||
|
||||
case 'course_update':
|
||||
//课程更新通知
|
||||
$course = \app\admin\model\course\Course::where([
|
||||
'id'=>$data['course_id']
|
||||
])->field(['name','updatetime'])->find();
|
||||
if(!$course){
|
||||
$this->remark = '未查询到相关课程';
|
||||
return false;
|
||||
}
|
||||
|
||||
$params = [
|
||||
'course_name'=>$course['name'],
|
||||
'time'=>date('Y-m-d H:i:s',$course['updatetime'])
|
||||
];
|
||||
$subUsers = \app\common\model\user\Subscription::where([
|
||||
'course_id'=>$data['course_id'],
|
||||
'validity_time'=>['>',time()]
|
||||
])->column('user_id');
|
||||
$toUsers = $this->getToUser(['user_id'=>['in',$subUsers]]);
|
||||
|
||||
$detailJump = $this->getDetailJump('course',['id'=>$data['course_id']]);
|
||||
break;
|
||||
|
||||
case 'activity_sing_in':
|
||||
//活动签到通知
|
||||
$ticketData = \app\admin\model\app\activity\Userticket::with(['activity'])->where([
|
||||
'ticket_no'=>$data['ticket_no']
|
||||
])->find();
|
||||
|
||||
if(!$ticketData){
|
||||
$this->remark = '未查询到相关活动票信息';
|
||||
return false;
|
||||
}
|
||||
$params = [
|
||||
'activity_name'=>$ticketData->activity->name,
|
||||
'time'=>date('Y-m-d H:i:s',$this->createtime)
|
||||
];
|
||||
|
||||
$toUsers = $this->getToUser(['user_id'=>$ticketData['user_id']]);
|
||||
$detailJump = $this->getDetailJump('activity',['id'=>$ticketData['activity_id']]);
|
||||
|
||||
break;
|
||||
|
||||
case 'activity_audit':
|
||||
//活动报名审核结果
|
||||
$ticketData = \app\admin\model\app\activity\Userticket::with(['activity'])->where([
|
||||
'ticket_no'=>$data['ticket_no']
|
||||
])->find();
|
||||
|
||||
if(!$ticketData){
|
||||
$this->remark = '未查询到相关活动票信息';
|
||||
return false;
|
||||
}
|
||||
$params = [
|
||||
'activity_name'=>$ticketData->activity->name,
|
||||
'result'=>$data['status'] == 1 ? '审核通过' : '审核不通过',
|
||||
'time'=>date('Y-m-d H:i:s',$this->createtime)
|
||||
];
|
||||
$toUsers = $this->getToUser(['user_id'=>$ticketData['user_id']]);
|
||||
$detailJump = $this->getDetailJump('activity',['id'=>$ticketData['activity_id']]);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if(!$toUsers){
|
||||
$this->remark = '未获取到接收消息用户的公众号关联信息';
|
||||
return false;
|
||||
}
|
||||
|
||||
$msgBody = $this->mergeMessageBody($params);
|
||||
|
||||
if(!$msgBody){
|
||||
$this->remark = '未获取到消息内容';
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->send($toUsers,$msgBody,$detailJump);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
* @param $toUsers
|
||||
* @param $msgBody
|
||||
* @return bool
|
||||
*/
|
||||
public function send($toUsers,$msgBody,$jumpDetail=[]){
|
||||
$wechat = new Wechat('wxOfficialAccount');
|
||||
|
||||
foreach ($toUsers as $user){
|
||||
|
||||
$params = [
|
||||
'touser' => $user,
|
||||
'template_id' => $this->sence_config['template_id'],
|
||||
'data' => $msgBody
|
||||
];
|
||||
|
||||
if(!empty($jumpDetail)){
|
||||
$params = array_merge($params,$jumpDetail);
|
||||
}
|
||||
|
||||
$wechat->getApp()->template_message->send($params);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取消息详情页跳转信息
|
||||
* @return void
|
||||
*/
|
||||
public function getDetailJump($type,$params = []){
|
||||
$config = \app\common\model\app\Config::getConfig('msg_push_config');
|
||||
|
||||
if($config['detail_jump'] == 'close'){
|
||||
return false;
|
||||
}
|
||||
|
||||
$detailPath = [];
|
||||
|
||||
try {
|
||||
if($config['detail_jump'] == 'h5'){
|
||||
$detailPath['url'] = \app\common\library\share\Links::getLink($type,$params);
|
||||
}else{
|
||||
$miniConfig = \app\common\model\config\System::getConfig('wxMiniProgram');
|
||||
$pagepath = \app\common\library\share\Links::getMpLink($type,$params);
|
||||
$detailPath['miniprogram'] = [
|
||||
'appid'=>$miniConfig['app_id'],
|
||||
'pagepath'=>$pagepath['link'] . "?" .$pagepath['params']
|
||||
];
|
||||
}
|
||||
}catch (\Exception $e){
|
||||
return [];
|
||||
}
|
||||
|
||||
return $detailPath;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取接受消息的用户
|
||||
* @return void
|
||||
*/
|
||||
public function getToUser($condition=[]){
|
||||
$toUser = \app\common\model\user\Oauth::where([
|
||||
'platform'=>'wxOfficialAccount'
|
||||
])->where($condition)->COLUMN('openid');
|
||||
|
||||
|
||||
return $toUser;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取消息体
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function getMessageBody($data)
|
||||
{
|
||||
switch ($this->msg_type) {
|
||||
case 'order_success':
|
||||
$params = [
|
||||
'course_name'=>$data['goodsList'][0]['snapshoot']['name'],
|
||||
'time'=>date('Y-m-d H:i:s',$data['createtime']),
|
||||
'price'=>'¥'.$data['real_price']
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
return $this->mergeMessageBody($params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 合并消息参数
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function mergeMessageBody($params)
|
||||
{
|
||||
$mergedArray = array();
|
||||
foreach ($this->sence_config['config'] as $key => $value) {
|
||||
if (isset($params[$key])) {
|
||||
$mergedArray[$value] = $params[$key];
|
||||
}
|
||||
}
|
||||
return $mergedArray;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user