Wx.php 3.21 KB
<?php

namespace addons\qingdong\controller;

use addons\qingdong\model\Staff;
use addons\qingdong\model\AdminConfig;
use think\Log;
use WeWork\Crypt\WXBizMsgCrypt;
use addons\qingdong\library\Wechat;

/**
 *  * 操作文档:https://doc.fastadmin.net/qingdong
 * 软件介绍:https://www.fastadmin.net/store/qingdong.html
 * 售后微信:qingdong_crm
 * 企业微信接口
 */
class Wx extends StaffApi
{
    protected $noNeedLogin = ['userid','template','token'];
    protected $noNeedRight = [];


    public function _initialize()
    {
        parent::_initialize();
    }

    //验证
    public function token()
    {
        $wechat = new Wechat('wxOfficialAccount');

        $app = $wechat->getApp();
        // $current = $app->menu->current();
        // halt($current);
        // $buttons = [
        //     [
        //         "type" => "click",
        //         "name" => "绑定帐号",
        //         "key"  => "V1001_BIND"
        //     ]
        // ];
        // $app->menu->create($buttons);
        // return;

        $app->server->push(function ($message)use($app) {

            // \think\Cache::set('logs',11);
            if($message['MsgType']=='event'){
                // \think\Cache::set('logs',22);
                if($message['Event']=='CLICK' && $message['EventKey']=='V1001_BIND'){
                    // \think\Cache::set('logs',33);
                    $user = $app->user->get($message['FromUserName']);
                    $staff = \think\Db::name('qingdong_staff')->where('unionid',$user['unionid'])->find();
                    if($staff){
                        \think\Db::name('qingdong_staff')->where('unionid',$user['unionid'])->update(['wx_openid'=>$user['openid']]);
                        return "你好{$staff['name']},小程序帐号【{$staff['mobile']}】绑定成功";
                    }else{
                        return "未查询到小程序帐号,请重新登陆小程序";
                    }
                }
            }
            return "您好!欢迎使用 瑄玑云超融合平台";
        });
        $response = $app->server->serve();
        $response->send();
        return $response;


        halt($app);




        return;
        $corpId = AdminConfig::getConfigValue('wechat', 'corpid');
        $token = 'crm';
        $encodingAesKey = '';
        Log::write('a---' . json_encode(input()));
        //{"signature":"a5e65529fbfb664f861123bf788d3148c2f81410","echostr":"8516206395270299499","timestamp":"1730084704","nonce":"787142174"}
        $sVerifyMsgSig = input('signature');
        $sVerifyTimeStamp = input('timestamp');
        $sVerifyNonce = input('nonce');
        $sVerifyEchoStr = input('echostr');
        return $sVerifyEchoStr;
        if (empty($sVerifyMsgSig) || empty($sVerifyTimeStamp) || empty($sVerifyNonce) || empty($sVerifyEchoStr)) {
            $this->error('参数不正确');
        }
        $sEchoStr = "";
        $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
        
        $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
        // halt($errCode); 
        if ($errCode == 0) {
            return $sEchoStr;
        } else {
            return false;
        }
    }


}