mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
[更新]修改微信消息接口
This commit is contained in:
parent
b6af934a3a
commit
6c76ab26ce
@ -16,10 +16,10 @@ namespace app\wechat\controller;
|
|||||||
|
|
||||||
use service\DataService;
|
use service\DataService;
|
||||||
use service\WechatService;
|
use service\WechatService;
|
||||||
use Wechat\WechatReceive;
|
|
||||||
use think\Controller;
|
use think\Controller;
|
||||||
use think\Log;
|
|
||||||
use think\Db;
|
use think\Db;
|
||||||
|
use think\Log;
|
||||||
|
use Wechat\WechatReceive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信接口控制器
|
* 微信接口控制器
|
||||||
@ -35,14 +35,12 @@ class Api extends Controller {
|
|||||||
*/
|
*/
|
||||||
protected $openid;
|
protected $openid;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信消息对象
|
* 微信消息对象
|
||||||
* @var WechatReceive
|
* @var WechatReceive
|
||||||
*/
|
*/
|
||||||
protected $wechat;
|
protected $wechat;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信消息接口
|
* 微信消息接口
|
||||||
* @return string
|
* @return string
|
||||||
@ -59,7 +57,7 @@ class Api extends Controller {
|
|||||||
// 获取消息来源用户OPENID
|
// 获取消息来源用户OPENID
|
||||||
$this->openid = $this->wechat->getRev()->getRevFrom();
|
$this->openid = $this->wechat->getRev()->getRevFrom();
|
||||||
// 获取并同步粉丝信息到数据库
|
// 获取并同步粉丝信息到数据库
|
||||||
$this->_syncFans(true);
|
$this->_updateFansInfo(true);
|
||||||
// 分别执行对应类型的操作
|
// 分别执行对应类型的操作
|
||||||
switch ($this->wechat->getRev()->getRevType()) {
|
switch ($this->wechat->getRev()->getRevType()) {
|
||||||
case WechatReceive::MSGTYPE_TEXT:
|
case WechatReceive::MSGTYPE_TEXT:
|
||||||
@ -77,40 +75,37 @@ class Api extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 关键字处理
|
* 关键字处理
|
||||||
* @param string $keys 关键字(常规或规格关键字)
|
* @param string $keys
|
||||||
* @param bool $isDefaultMode 是否启用默认模式
|
* @param bool $isForce
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function _keys($keys, $isDefaultMode = false) {
|
private function _keys($keys, $isForce = false) {
|
||||||
list($table, $field, $value) = explode('#', $keys . '##');
|
list($table, $field, $value) = explode('#', $keys . '##');
|
||||||
$info = Db::name($table)->where($field, $value)->find();
|
if (is_array($info = Db::name($table)->where($field, $value)->find()) && isset($info['type'])) {
|
||||||
if ($info && is_array($info) && isset($info['type'])) {
|
// 数据状态检查
|
||||||
// 转发给多客服
|
|
||||||
if (!empty($info['type']) && $info['type'] === 'customservice') {
|
|
||||||
$this->wechat->sendCustomMessage(['touser' => $this->openid, 'msgtype' => 'text', 'text' => ['content' => $info['content']]]);
|
|
||||||
return $this->wechat->transfer_customer_service()->reply(false, true);
|
|
||||||
}
|
|
||||||
// 无法给出回复时调用默认回复机制
|
|
||||||
if (array_key_exists('status', $info) && empty($info['status'])) {
|
if (array_key_exists('status', $info) && empty($info['status'])) {
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
switch ($info['type']) {
|
switch ($info['type']) {
|
||||||
case 'keys': /* 关键字 */
|
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'])) {
|
if (empty($info['content']) && empty($info['name'])) {
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
return $this->_keys('wechat_keys#keys#' . (empty($info['content']) ? $info['name'] : $info['content']));
|
return $this->_keys('wechat_keys#keys#' . (empty($info['content']) ? $info['name'] : $info['content']));
|
||||||
case 'text': /* 文本消息 */
|
case 'text': // 文本消息
|
||||||
if (empty($info['content']) && empty($info['name'])) {
|
if (empty($info['content']) && empty($info['name'])) {
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
return $this->wechat->text($info['content'])->reply(false, true);
|
return $this->wechat->text($info['content'])->reply(false, true);
|
||||||
case 'news': /* 图文消息 */
|
case 'news': // 图文消息
|
||||||
if (empty($info['news_id'])) {
|
if (empty($info['news_id'])) {
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
return $this->_news($info['news_id']);
|
return $this->_news($info['news_id']);
|
||||||
case 'music': /* 音频消息 */
|
case 'music': // 音频消息
|
||||||
if (empty($info['music_url']) || empty($info['music_title']) || empty($info['music_desc'])) {
|
if (empty($info['music_url']) || empty($info['music_title']) || empty($info['music_desc'])) {
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
@ -119,7 +114,7 @@ class Api extends Controller {
|
|||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
return $this->wechat->music($info['music_title'], $info['music_desc'], $info['music_url'], $info['music_url'], $media_id)->reply(false, true);
|
return $this->wechat->music($info['music_title'], $info['music_desc'], $info['music_url'], $info['music_url'], $media_id)->reply(false, true);
|
||||||
case 'voice': /* 语音消息 */
|
case 'voice': // 语音消息
|
||||||
if (empty($info['voice_url'])) {
|
if (empty($info['voice_url'])) {
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
@ -128,7 +123,7 @@ class Api extends Controller {
|
|||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
return $this->wechat->voice($media_id)->reply(false, true);
|
return $this->wechat->voice($media_id)->reply(false, true);
|
||||||
case 'image': /* 图文消息 */
|
case 'image': // 图文消息
|
||||||
if (empty($info['image_url'])) {
|
if (empty($info['image_url'])) {
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
@ -137,7 +132,7 @@ class Api extends Controller {
|
|||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
return $this->wechat->image($media_id)->reply(false, true);
|
return $this->wechat->image($media_id)->reply(false, true);
|
||||||
case 'video': /* 视频消息 */
|
case 'video': // 视频消息
|
||||||
if (empty($info['video_url']) || empty($info['video_desc']) || empty($info['video_title'])) {
|
if (empty($info['video_url']) || empty($info['video_desc']) || empty($info['video_title'])) {
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
@ -146,7 +141,7 @@ class Api extends Controller {
|
|||||||
return $this->wechat->video($media_id, $info['video_title'], $info['video_desc'])->reply(false, true);
|
return $this->wechat->video($media_id, $info['video_title'], $info['video_desc'])->reply(false, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($isDefaultMode) {
|
if ($isForce) {
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
return $this->_keys('wechat_keys#keys#default', true);
|
return $this->_keys('wechat_keys#keys#default', true);
|
||||||
@ -179,23 +174,17 @@ class Api extends Controller {
|
|||||||
protected function _event() {
|
protected function _event() {
|
||||||
$event = $this->wechat->getRevEvent();
|
$event = $this->wechat->getRevEvent();
|
||||||
switch (strtolower($event['event'])) {
|
switch (strtolower($event['event'])) {
|
||||||
/* 粉丝关注事件 */
|
case 'subscribe': // 粉丝关注事件
|
||||||
case 'subscribe':
|
$this->_updateFansInfo(true);
|
||||||
$this->_syncFans(true);
|
$this->_spread($event['key']);
|
||||||
if (!empty($event['key']) && stripos($event['key'], 'qrscene_') !== false) {
|
return $this->_keys('wechat_keys#keys#subscribe', true);
|
||||||
$this->_spread(preg_replace('|^.*?(\d+).*?$|', '$1', $event['key']));
|
case 'unsubscribe':// 粉丝取消关注
|
||||||
}
|
$this->_updateFansInfo(false);
|
||||||
return $this->_keys('wechat_keys#keys#subscribe');
|
|
||||||
/* 粉丝取消关注 */
|
|
||||||
case 'unsubscribe':
|
|
||||||
$this->_syncFans(false);
|
|
||||||
return 'success';
|
return 'success';
|
||||||
/* 点击菜单事件 */
|
case 'click': // 点击菜单事件
|
||||||
case 'click':
|
|
||||||
return $this->_keys($event['key']);
|
return $this->_keys($event['key']);
|
||||||
/* 扫码推事件 */
|
|
||||||
case 'scancode_push':
|
case 'scancode_push':
|
||||||
case 'scancode_waitmsg':
|
case 'scancode_waitmsg': // 扫码推事件
|
||||||
$scanInfo = $this->wechat->getRev()->getRevScanInfo();
|
$scanInfo = $this->wechat->getRev()->getRevScanInfo();
|
||||||
if (isset($scanInfo['ScanResult'])) {
|
if (isset($scanInfo['ScanResult'])) {
|
||||||
return $this->_keys($scanInfo['ScanResult']);
|
return $this->_keys($scanInfo['ScanResult']);
|
||||||
@ -212,10 +201,11 @@ class Api extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 推荐好友扫码关注
|
* 推荐好友扫码关注
|
||||||
* @param string $key
|
* @param string $event
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function _spread($key) {
|
private function _spread($event) {
|
||||||
|
$key = preg_replace('|^.*?(\d+).*?$|', '$1', "{$event}");
|
||||||
// 检测推荐是否有效
|
// 检测推荐是否有效
|
||||||
$fans = Db::name('WechatFans')->where('id', $key)->find();
|
$fans = Db::name('WechatFans')->where('id', $key)->find();
|
||||||
if (!is_array($fans) || !isset($fans['openid']) || $fans['openid'] === $this->openid) {
|
if (!is_array($fans) || !isset($fans['openid']) || $fans['openid'] === $this->openid) {
|
||||||
@ -246,7 +236,7 @@ class Api extends Controller {
|
|||||||
* 同步粉丝状态
|
* 同步粉丝状态
|
||||||
* @param bool $subscribe 关注状态
|
* @param bool $subscribe 关注状态
|
||||||
*/
|
*/
|
||||||
protected function _syncFans($subscribe = true) {
|
protected function _updateFansInfo($subscribe = true) {
|
||||||
if ($subscribe) {
|
if ($subscribe) {
|
||||||
$fans = WechatService::getFansInfo($this->openid);
|
$fans = WechatService::getFansInfo($this->openid);
|
||||||
if (empty($fans) || empty($fans['subscribe'])) {
|
if (empty($fans) || empty($fans['subscribe'])) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user