223 lines
7.2 KiB
Plaintext
223 lines
7.2 KiB
Plaintext
<?php
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use \think\Controller;
|
|
use \think\Db;
|
|
use \think\Log;
|
|
use \think\Session;
|
|
use Wechat\WechatReceive;
|
|
use service\DataService;
|
|
use service\WechatService;
|
|
/**
|
|
* 微信接口控制器
|
|
* Class Api
|
|
|
|
*/
|
|
class Api extends Controller
|
|
{
|
|
protected $uid;
|
|
/**
|
|
* 微信openid
|
|
* @var string
|
|
*/
|
|
protected $openid;
|
|
|
|
/**
|
|
* 微信消息对象
|
|
* @var WechatReceive
|
|
*/
|
|
protected $wechat;
|
|
|
|
/**
|
|
* 微信消息接口
|
|
* @return string
|
|
*/
|
|
public function index()
|
|
{
|
|
$this->uid = $this->request->param('uid');
|
|
|
|
// 实例接口对象
|
|
$this->wechat = &load_wechat($this->uid,'Receive');
|
|
|
|
// 验证接口请求
|
|
if ($this->wechat->valid() === false) {
|
|
$msg = "{$this->wechat->errMsg}[{$this->wechat->errCode}]";
|
|
Log::error($msg);
|
|
return $msg;
|
|
}
|
|
// 获取消息来源用户OPENID
|
|
$this->openid = $this->wechat->getRev()->getRevFrom();
|
|
|
|
// 获取并同步粉丝信息到数据库
|
|
$this->_updateFansInfo(true);
|
|
|
|
// 分别执行对应类型的操作
|
|
switch ($this->wechat->getRev()->getRevType()) {
|
|
case WechatReceive::MSGTYPE_TEXT:
|
|
return $this->_keys("WechatKeys#keys#" . $this->wechat->getRevContent());
|
|
// 事件类型处理
|
|
case WechatReceive::MSGTYPE_EVENT:
|
|
return $this->_event();
|
|
// 图片类型处理
|
|
case WechatReceive::MSGTYPE_IMAGE:
|
|
return $this->_image();
|
|
// 其它类型的处理,比如卡卷领取、卡卷转赠
|
|
default:
|
|
return 'success';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 关键字处理
|
|
* @param string $keys
|
|
* @param bool $isForce
|
|
* @return string
|
|
*/
|
|
|
|
private function _keys($keys, $isForce = false)
|
|
{
|
|
list($table, $field, $value) = explode('#', $keys . '##');
|
|
if (is_array($info = Db::name($table)->where($field, $value)->find()) && isset($info['type'])) {
|
|
// 数据状态检查
|
|
if (array_key_exists('status', $info) && empty($info['status'])) {
|
|
return 'success';
|
|
}
|
|
switch ($info['type']) {
|
|
case 'customservice': // 多客服
|
|
$this->wechat->sendCustomMessage(['touser' => $this->openid, 'msgtype' => 'text', 'text' => ['content' => $info['content']]]);
|
|
return $this->wechat->transfer_customer_service()->reply(false, true);
|
|
case 'keys': // 关键字
|
|
if (empty($info['content']) && empty($info['name'])) {
|
|
return 'success';
|
|
}
|
|
|
|
return $this->_keys('wechat_keys#keys#' . (empty($info['content']) ? $info['name'] : $info['content']));
|
|
case 'text': // 文本消息
|
|
if (empty($info['content']) && empty($info['name'])) {
|
|
return 'success';
|
|
}
|
|
return $this->wechat->text($info['content'])->reply(false, true);
|
|
case 'news': // 图文消息
|
|
if (empty($info['news_id'])) {
|
|
return 'success';
|
|
}
|
|
return $this->_news($info['news_id'],$info['volumeid']);
|
|
}
|
|
}
|
|
if ($isForce) {
|
|
return 'success';
|
|
}
|
|
return $this->_keys('wechat_keys#keys#default', true);
|
|
}
|
|
/**
|
|
* 回复图文
|
|
* @param int $news_id
|
|
* @return bool|string
|
|
*/
|
|
protected function _news($news_id = 0,$volumeid=0)
|
|
{
|
|
if (is_array($newsinfo = Db::name('cartoon')->find($news_id)) && !empty($newsinfo['name'])) {
|
|
$volume = Db::name('volume')->find($volumeid);
|
|
|
|
$newsdata[] = [
|
|
'Title' => $volume['cartoonname']." ".$volume['volumename'],
|
|
'Description' => $newsinfo['intro'],
|
|
'PicUrl' => "http://".$_SERVER['HTTP_HOST'].get_cover($newsinfo['cartoonlpic'],'path'),
|
|
'Url' => url("@wechat/review", '', true, true) . "?content={$newsinfo['id']}&type=article",
|
|
];
|
|
return $this->wechat->news($newsdata)->reply(false, true);
|
|
}
|
|
return 'success';
|
|
}
|
|
|
|
/**
|
|
* 事件处理
|
|
*/
|
|
protected function _event()
|
|
{
|
|
$event = $this->wechat->getRevEvent();
|
|
Log::info($event);
|
|
switch (strtolower($event['event'])) {
|
|
case 'subscribe': // 粉丝关注事件
|
|
$this->_updateFansInfo(true);
|
|
//$this->_spread($event['key']);
|
|
return $this->_keys('wechat_keys#keys#subscribe', true);
|
|
case 'unsubscribe':// 粉丝取消关注
|
|
$this->_updateFansInfo(false);
|
|
return 'success';
|
|
case 'click': // 点击菜单事件
|
|
Log::info($event['key']);
|
|
//客服事件
|
|
if($event['key'] == "kefu_qrcode_url"){
|
|
$PicUrl = "http://wap.lveshu.com/uploads/picture/20171121/f5b0c0a800737db110433775ffdb69f7.jpg";
|
|
return $this->wechat->text($PicUrl)->reply();
|
|
}else{
|
|
return $this->_keys($event['key']);
|
|
}
|
|
case 'scancode_push':
|
|
case 'scancode_waitmsg': // 扫码推事件
|
|
$scanInfo = $this->wechat->getRev()->getRevScanInfo();
|
|
if (isset($scanInfo['ScanResult'])) {
|
|
return $this->_keys($scanInfo['ScanResult']);
|
|
}
|
|
return 'success';
|
|
case 'scan':
|
|
if (!empty($event['key'])) {
|
|
return $this->_spread($event['key']);
|
|
}
|
|
return 'success';
|
|
}
|
|
return 'success';
|
|
}
|
|
|
|
/**
|
|
* 推荐好友扫码关注
|
|
* @param string $event
|
|
* @return mixed
|
|
*/
|
|
private function _spread($event)
|
|
{
|
|
$key = preg_replace('|^.*?(\d+).*?$|', '$1', "{$event}");
|
|
// 检测推荐是否有效
|
|
$fans = Db::name('WechatFans')->where('id', $key)->find();
|
|
if (!is_array($fans) || !isset($fans['openid']) || $fans['openid'] === $this->openid) {
|
|
return false;
|
|
}
|
|
// 标识推荐关系
|
|
$data = ['spread_openid' => $fans['openid'], 'spread_at' => date('Y-m-d H:i:s')];
|
|
Db::name('WechatFans')->where("openid='{$this->openid}' and (spread_openid is null or spread_openid='')")->setField($data);
|
|
// @todo 推荐成功的奖励
|
|
}
|
|
|
|
/**
|
|
* 图片事件处理
|
|
*/
|
|
private function _image()
|
|
{
|
|
return 'success';
|
|
}
|
|
|
|
/**
|
|
* 同步粉丝状态
|
|
* @param bool $subscribe 关注状态
|
|
*/
|
|
protected function _updateFansInfo($subscribe = true)
|
|
{
|
|
if ($subscribe) {
|
|
|
|
$fans = WechatService::getFansInfo($this->openid);
|
|
if (empty($fans) || empty($fans['subscribe'])) {
|
|
$wechat = &load_wechat($this->uid,'User');
|
|
$userInfo = $wechat->getUserInfo($this->openid);
|
|
$userInfo['subscribe'] = intval($subscribe);
|
|
WechatService::setFansInfo($userInfo, $wechat->appid);
|
|
}
|
|
} else {
|
|
$data = ['subscribe' => '0', 'appid' => $this->wechat->appid, 'openid' => $this->openid];
|
|
DataService::save('WechatFans', $data, ['appid', 'openid']);
|
|
}
|
|
}
|
|
|
|
}
|