easywechat处理ticket通知事件

现在的小程序电商系统一般都会用到第三方开放平台,因为这样可以方便管理小程序,还有一个品牌的标志

当然第三方平台需要开发的代码量也是可观的,这里我个人推荐使用PHP的微信开发SDK EasyWeChat,这个SDK虽然文档写的不完整,但是它的功能基本覆盖了所有微信开发的供能

其中,我用搭建第三方平台的时候遇到的两个处理ticket的url问题

官方文档会有两个接收事件的地址

1 授权事件接收URL

用于接收取消授权通知、授权成功通知、授权更新通知,也用于接收ticket,ticket是验证平台方的重要凭据。

2 消息与事件接收URL

通过该URL接收公众号或小程序消息和事件推送,该参数按规则填写(需包含/$APPID$,如www.abc.com/$APPID$/callback),实际接收消息时$APPID$将被替换为公众号或小程序AppId。

使用easywechat的时候这两个地址可以调用同一个消息处理事件,然后分别做返回success的处理,当然也可以把两个接收事件地址写成一样,统一处理。

授权通知的代码如下

/**
 * 授权接收
 * @return string
 */
public function ticket()
{
    (new TicketEvent())->event(); //统一处理的事件

    return 'success';
}

消息推送的代码如下

/**
 * 事件推送
 *
 * @return void
 */
public function ticket()
{
    (new TicketEvent())->event(); //统一处理的事件

    return 'success';
}

咋一看其实是一样的

以下是统一处理事件的类

class TicketEvent
{
    public function event()
    {
        $server = OpenApp::getApp()->server;

        $message = $server->getMessage();

        ///授权成功事件
        if (isset($message['InfoType'])&&$message['InfoType'] == 'authorized') {
            // TODO
        }
        //授权更新事件
        if (isset($message['InfoType'])&&$message['InfoType'] == 'updateauthorized') {
            // TODO
        }
        //授权取消事件
        if (isset($message['InfoType'])&&$message['InfoType'] == 'unauthorized') {
            (new AuthLogic())->authEvent($message);
        }
        //快速注册小程序事件
        if(isset($message['InfoType'])&&$message['InfoType'] == 'notify_third_fasteregister'){
            (new ComponentLogic())->registerNoteEvent($message);
        }
        //代码审核推送
        if (isset($message['MsgType'])&&$message['MsgType'] == 'event') {
            $appid = ltrim(request()->param('appid'), '/');
            //审核通过
            if (isset($message['Event'])&&$message['Event'] == 'weapp_audit_success') {
                (new CodeLogic())->codeEvent($appid);
            }
            //审核不通过
            if (isset($message['Event'])&&$message['Event'] == 'weapp_audit_fail') {
                $reason = $message['Reason'] ? $message['Reason'] : '未知原因';
                (new CodeLogic())->codeEvent($appid, $reason);
            }
        }
        // 其他事件另行开发,目前就只有这几个
        return $server->serve();
    }
}

OpenApp::getApp() 是我写的一个获取对象的单例,获取配置的暂时还没有用到

Class OpenApp
{
    protected static $_instance;

    private function __construct()
    {

    }

    public static function getApp($agent_id = false, $open_app_id = false)
    {
        if (is_null(self::$_instance)) {
            $config = self::getConfig($agent_id, $open_app_id);
            self::$_instance = Factory::openPlatform($config);
        }

        return self::$_instance;
    }

    protected static function getConfig($agent_id = false, $open_app_id = false)
    {
        $where = [];
        if ($agent_id) {
            $where['agent_id'] = $agent_id;
        }
        if ($open_app_id) {
            $where['open_app_id'] = $open_app_id;
        }
        $agent = (new Agent())->where($where)->find();
        $defaultConfig = [
            'app_id'   => '开放平台appid',
            'secret'   => '开放平台秘钥',
            'token'    => '开放平台token',
            'aes_key'  => '开放平台key'
        ];
        if (!$agent) {
            $config = $defaultConfig;
        } else {
            if ($agent['is_open'] == 1) {
                $config = [
                    'app_id'   => $agent['open_app_id'],
                    'secret'   => $agent['open_secret'],
                    'token'    => $agent['open_token'],
                    'aes_key'  => $agent['open_aes_key']
                ];
            } else {
                $config = $defaultConfig;
            }
        }

        return $config;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值