You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\base\controller\mpweixin\platform;
use app\base\model\mpweixin\MpweixinUser;
use EasyWeChat\Factory;
use think\App;
class Notice extends Base
{
/**
* 公众平台和服务器对接
* @date 2020-07-23
*/
public function serveConfig()
{
$mp_weixin_param = get_mp_weixin_config();
$notice_class = new \tencent\wechat\mpweixin\Notice($mp_weixin_param);
$mp_weixin_config = $notice_class->getMpWeixinConfig();
$app = Factory::miniProgram($mp_weixin_config);
$app->server->push(function ($message) {
platformLog('', $message, 'mp_weixin_get_user_message_' . UID);
$result = Notice::messageResponse($message);
return $result;
});
$response = $app->server->serve();
// 将响应输出
$response->send();
exit;
}
/**
* 接收普通消息并处理
* @param array $message 普通微信用户向公众账号发消息时,微信服务器请求的服务器携带参数 xml已转array
* @date 2020-07-23
*/
protected function messageResponse($message = [])
{
switch ($message['MsgType']) {
case 'event':
if ($message['Event'] == 'user_authorization_revoke') { //用户撤回 user_info_modified用户资料变更user_authorization_revoke用户撤回
$this->replyUserAuthorizationRevoke($message);
return 'success';
} else if ($message['Event'] == 'user_info_modified') {
return '';
} else if ($message['Event'] == 'subscribe_msg_sent_event') { //发送订阅消息之后
return '';
}
return '收到事件消息';
break;
default:
return '收到其它消息';
break;
}
}
/**
* 回复用户消息 - 用户撤回授权信息
* @param array $message 普通微信用户向公众账号发消息时,微信服务器请求的服务器携带参数 xml已转array
* @param string $message ['ToUserName'] 小程序的UserName
* @param string $message ['FromUserName'] 平台推送服务UserName
* @param string $message ['MsgType'] 消息类型Event
* @param string $message ['Event'] user_info_modified用户资料变更user_authorization_revoke用户撤回
* @param int $message ['CreateTime'] 消息创建时间 (整型)
* @param string $message ['OpenID'] 撤回或变更资料的用户OpenID
* @param string $message ['AppID'] 小程序的AppID
* @param string $message ['RevokeInfo'] 用户撤回的授权信息1:车牌号,2:地址,3:发票信息,4:蓝牙,5:麦克风,6:昵称和头像,7:摄像头,8:手机号,12:微信运动步数,13:位置信息,14:选中的图片或视频,15:选中的文件,16:邮箱地址
* @param string $message ['PluginID'] 插件场景用户撤回插件的AppID
* @param string $message ['OpenPID'] 插件场景用户撤回撤回用户的OpenPID
* @date 2020-07-27
*/
protected function replyUserAuthorizationRevoke($message)
{
if ($message['RevokeInfo'] == 6) {
$openid = $message['OpenID'];
$mpweixin_user_model = new MpweixinUser();
//删除用户头像和昵称
$data = [
'openid' => $openid,
'nickname' => '',
'headimgurl' => '',
];
$res = $mpweixin_user_model->updateUser($data);
// TODO 是否需要删除userinfo表 以及是否给微信服务器正常返回需测试
}
}
}