1 line
5.7 KiB
PHP
1 line
5.7 KiB
PHP
<?php
|
||
namespace app\common\model;
|
||
|
||
use hema\wechat\Driver;
|
||
|
||
/**
|
||
* 微信群发消息模型
|
||
*/
|
||
class WechatBatchSend extends BaseModel
|
||
{
|
||
protected $name = 'wechat_batch_send';
|
||
// 定义主键
|
||
protected $pk = 'wechat_batch_send_id';
|
||
// 追加字段
|
||
protected $append = [];
|
||
|
||
/**
|
||
* 消息类型
|
||
*/
|
||
public function getMsgTypeAttr($value)
|
||
{
|
||
$status = [
|
||
'mpnews' => '图文',
|
||
'news' => '图文',
|
||
'text' => '文本',
|
||
'image' => '图片',
|
||
'video' => '视频',
|
||
'voice' => '语音',
|
||
'music' => '音乐',
|
||
'wxcard' => '卡券'
|
||
];
|
||
return ['text' => $status[$value], 'value' => $value];
|
||
}
|
||
|
||
/**
|
||
* 群发状态
|
||
*/
|
||
public function getStatusAttr($value)
|
||
{
|
||
$status = [
|
||
0 => '未知错误',
|
||
10 => '待群发',
|
||
20 => '群发中',
|
||
30 => '群发成功',
|
||
40 => '群发失败',
|
||
10001 => '涉嫌广告',
|
||
20001 => '涉嫌政治',
|
||
20004 => '涉嫌社会',
|
||
20002 => '涉嫌色情',
|
||
20006 => '涉嫌违法犯罪',
|
||
20008 => '涉嫌欺诈',
|
||
20013 => '涉嫌版权',
|
||
22000 => '涉嫌互推',
|
||
21000 => '涉嫌其他',
|
||
30001 => '原创不能群发',
|
||
30002 => '原创不能群发',
|
||
30003 => '原创不能群发',
|
||
40001 => '管理员拒绝',
|
||
40002 => '管理员超时'
|
||
];
|
||
return ['text' => $status[$value], 'value' => $value];
|
||
}
|
||
|
||
/**
|
||
* 获取列表
|
||
*/
|
||
public function getList($applet_id = 0)
|
||
{
|
||
// 筛选条件
|
||
$filter = [];
|
||
$filter['applet_id'] = $applet_id;
|
||
// 执行查询
|
||
return $this->where($filter)
|
||
->order('wechat_batch_send_id','desc')
|
||
->paginate(['list_rows'=>15,'query' => request()->param()]);
|
||
}
|
||
|
||
/**
|
||
* 添加
|
||
*/
|
||
public function add(array $data, $applet_id = 0)
|
||
{
|
||
//发送预览记录
|
||
$open_id = '';
|
||
if($applet_id == 0){
|
||
$open_id = Setting::getItem('ability')['open_id'];//平台管理员
|
||
}else{
|
||
//商家管理员
|
||
}
|
||
if(!empty($open_id)){
|
||
$wx = new Driver;
|
||
$result= $wx->previewMsg($data,$open_id,$applet_id);
|
||
if($result['errcode'] != 0){
|
||
$this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg'];
|
||
return false;
|
||
}
|
||
}
|
||
$data['applet_id'] = $applet_id;
|
||
return $this->save($data);
|
||
}
|
||
|
||
/**
|
||
* 编辑
|
||
*/
|
||
public function edit(array $data, $applet_id = 0)
|
||
{
|
||
//发送预览记录
|
||
$open_id = '';
|
||
if($applet_id == 0){
|
||
$open_id = Setting::getItem('ability')['open_id'];//平台管理员
|
||
}else{
|
||
//商家管理员
|
||
}
|
||
if(!empty($open_id)){
|
||
$wx = new Driver;
|
||
$result= $wx->previewMsg($data,$open_id,$applet_id);
|
||
if($result['errcode'] != 0){
|
||
$this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg'];
|
||
return false;
|
||
}
|
||
}
|
||
return $this->save($data) !== false;
|
||
}
|
||
|
||
/**
|
||
* 删除
|
||
*/
|
||
public function remove($applet_id=0)
|
||
{
|
||
if(!empty($this->msg_id)){
|
||
$wx = new Driver;
|
||
$result = $wx->deleteMass($this->msg_id,$applet_id);
|
||
if($result['errcode'] != 0){
|
||
$this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg'];
|
||
return false;
|
||
}
|
||
}
|
||
return $this->delete();
|
||
}
|
||
|
||
/**
|
||
* 根据OpenID列表群发
|
||
*/
|
||
public function send($applet_id=0)
|
||
{
|
||
if(!empty($this->msg_id)){
|
||
$this->error = '群发任务已存在';
|
||
return false;
|
||
}
|
||
$filter = [
|
||
'is_subscribe' => 1,
|
||
'applet_id' => $applet_id
|
||
];
|
||
$open_id = User::where($filter)->column('open_id');
|
||
$wx = new Driver;
|
||
$result = $wx->sendMass($this,$open_id,$applet_id);
|
||
if($result['errcode'] != 0){
|
||
$this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg'];
|
||
return false;
|
||
}
|
||
$data = [
|
||
'msg_id' => $result['msg_id'],
|
||
'fans_count' => sizeof($open_id),
|
||
'status' => 20
|
||
];
|
||
return $this->save($data) !== false;
|
||
}
|
||
|
||
/**
|
||
* 根据群发任务msg_id获取详情 - 公众号群发后回调调用
|
||
*/
|
||
public static function getSend($msg_id)
|
||
{
|
||
return self::where('msg_id',$msg_id)->find();
|
||
}
|
||
|
||
/**
|
||
* 公众号群发后更新状态 - 回调调用
|
||
*/
|
||
public function backEdit(array $data)
|
||
{
|
||
if($data['Status'] == 'send success'){
|
||
$status = 30;
|
||
}elseif($data['Status'] == 'send fail'){
|
||
$status = 40;
|
||
}else{
|
||
switch($data['Status'])
|
||
{
|
||
case 'err(21000)':
|
||
$status = 21000;
|
||
break;
|
||
case 'err(30001)':
|
||
$status = 30001;
|
||
break;
|
||
case 'err(30002)':
|
||
$status = 30002;
|
||
break;
|
||
case 'err(30003)':
|
||
$status = 30003;
|
||
break;
|
||
case 'err(40001)':
|
||
$status = 40001;
|
||
break;
|
||
case 'err(40002)':
|
||
$status = 40002;
|
||
break;
|
||
case 'err(22000)':
|
||
$status = 22000;
|
||
break;
|
||
case 'err(20013)':
|
||
$status = 20013;
|
||
break;
|
||
case 'err(20008)':
|
||
$status = 20008;
|
||
break;
|
||
case 'err(20006)':
|
||
$status = 20006;
|
||
break;
|
||
case 'err(20002)':
|
||
$status = 20002;
|
||
break;
|
||
case 'err(20004)':
|
||
$status = 20004;
|
||
break;
|
||
case 'err(20001)':
|
||
$status = 20001;
|
||
break;
|
||
case 'err(10001)':
|
||
$status = 10001;
|
||
break;
|
||
default:
|
||
$status = 0;
|
||
}
|
||
}
|
||
return $this->save([
|
||
'status' => $status,
|
||
'send_count' => $data['SentCount']
|
||
]) !== false;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|