request->get('id', 0);
$open = WxOpenService::getOpen();
if (isset($open['merchantId']) && !empty($open['merchantId'])) {
if (!empty($merchantId)) {
util::stop('平台已经关联的公众号,无需重复关联');
}
}
$pre = wxUtil::getPreAuthCode($open);
$appId = $open['appId'];
$oData = [];
$oData['preAuthCodeTime'] = date("Y-m-d H:i:s");
//设置预授码过期,让下个用户可以获取新的预授码
xhWxOpenService::updateById(1, $oData);
$url = Yii::$app->params['wHost'] . '/wx-open-callback/' . $merchantId;
$authType = Yii::$app->request->get('authType', 3);
echo "授权";
}
//平台没有绑定商家公众号时初始化一个商家 shish 2020.2.11
public function actionAddMerchant()
{
CategoryClass::initCategoryGoods(12362);
$return = MerchantService::initOpenMerchant();
dd($return);
}
public function actionCheckMerchantName()
{
$get = Yii::$app->request->get();
$merchantName = isset($get['merchantName']) ? $get['merchantName'] : '';
$m = xhMerchantService::getByMerchantName($merchantName);
if (!empty($m)) {
util::fail('公众号名称已经使用');
}
util::ok();
}
/**
* 下载微信里的图片到服务器
*/
public function actionDownloadWxImg()
{
$get = Yii::$app->request->get();
$mediaId = isset($get['mediaId']) ? $get['mediaId'] : '';
$imgList = wxUtil::downloadmediaIdImg($this->merchant, $mediaId);
if (!empty($imgList)) {
util::success($imgList);
}
util::fail();
}
//创建平台使用的公众号
public function actionCallback()
{
$get = Yii::$app->request->get();
if (isset($get['auth_code']) == false) {
util::stop('公众号授权回调未成功');
}
$id = isset($get['id']) ? $get['id'] : 0;
$code = $get['auth_code'];
//使用授权码换取公众号的接口调用凭据和授权信息
$authorize = wxUtil::getAuthorizer($code);
if (isset($authorize['authorization_info']) == false) {
util::stop('未换取公众号的接口调用凭据和授权信息');
}
$info = $authorize['authorization_info'];
$authorizeAppId = $info['authorizer_appid'];
$authorize_access_token = $info['authorizer_access_token'];
$expires_in = $info['expires_in'];//7200
$authorize_refresh_token = $info['authorizer_refresh_token'];
//获取授权方的公众号帐号基本信息
$authorizeDetailInfo = wxUtil::getAuthorizerInfo($authorizeAppId);
if (isset($authorizeDetailInfo['authorizer_info']) == false) {
util::stop('没有获取到公众号帐号基本信息');
}
$authorize_info = $authorizeDetailInfo['authorizer_info'];
//$nick_name = $authorize_info['nick_name'];
$head_img = $authorize_info['head_img'];
$service_type_info = $authorize_info['service_type_info'];
if ($service_type_info['id'] != 2) {
util::stop('公众号不是服务号');
}
$verify_type_info = $authorize_info['verify_type_info'];
if ($verify_type_info['id'] != 0) {
util::stop('公众号还没有认证哦');
}
$user_name = $authorize_info['user_name'];
$alias = $authorize_info['alias'];
$qrCodeUrl = $authorize_info['qrcode_url'];
$accessTokenTime = date("Y-m-d H:i:s", (time() + $expires_in - 100));//安全起见,将过期时间设置得比微信里还短100秒
//开放平台还没有关联公众号,先将开放平台关联公众号
$open = WxOpenService::getOpen();
if (isset($open['merchantId']) && empty($open['merchantId'])) {
WxOpenService::updateById(1, ['merchantId' => $id]);
}
$merchant = MerchantService::getMerchantById($id);
if (!empty($merchant['wxAppId']) && $merchant['wxAppId'] != $authorizeAppId) {
util::stop('授权的公众号与商家不对应');
}
//小程序
if (isset($authorize_info['MiniProgramInfo']) && !empty($authorize_info['MiniProgramInfo'])) {
$merchant = xhMerchantService::getByCondition(['miniAppId' => $authorizeAppId]);
if (empty($merchant)) {
Yii::info('没有小程序信息 appId:' . $authorizeAppId);
util::stop('没有小程序信息 appId:' . $authorizeAppId);
}
$merchantId = $merchant['id'];
xhMerchantExtendService::updateByMerchantId($merchantId, ['miniAccessToken' => $authorize_access_token, 'miniAccessTokenTime' => $accessTokenTime, 'miniRefreshToken' => $authorize_refresh_token]);
Yii::info('小程序信息更新成功:' . json_encode($authorize_info));
util::stop("小程序信息更新成功");
}
$upData = [];
$appData['wxUserName'] = $user_name;
$appData['wxAliasName'] = $alias;
$upData['logo'] = xhMerchantService::logoSave($head_img, $authorizeAppId);//公众号logo
$upData['wxQrcodeUrl'] = xhMerchantService::qrCodeSave($qrCodeUrl, $authorizeAppId);//公众号二维码
$upData['wxAppId'] = $authorizeAppId;
$upData['wxAccessToken'] = $authorize_access_token;
$upData['wxAccessTokenTime'] = $accessTokenTime;
$upData['wxRefreshToken'] = $authorize_refresh_token;
$upData['status'] = 1;//审核通过
xhMerchantService::updateById($id, $upData);
//还没有完成初始化先进行初始化
$extend = MerchantExtendClass::getByMerchantId($id);
if (isset($extend['init']) && $extend['init'] == 0) {
$data = ['merchantId' => $merchantId];
$producer = Yii::$app->rabbitmq->getProducer('openShopInitProducer');
$msg = serialize($data);
$producer->publish($msg, 'openShopInitExchange', 'openShopInitRoute');
}
echo 'ok';
}
public function actionResult()
{
$get = Yii::$app->request->get();
$id = isset($get['id']) ? $get['id'] : 0;
$merchant = xhMerchantService::getById($id);
$status = configDict::getConfig('merchantStatusName');
$orMechantId = configDict::getConfig('merchantId');
$originalMerchant = xhMerchantService::getById($orMechantId);
return $this->renderPartial('result', ['originalMerchant' => $originalMerchant, 'merchant' => $merchant, 'status' => $status]);
}
public function actionCheckInviteCode()
{
$get = Yii::$app->request->get();
$code = isset($get['code']) ? $get['code'] : '';
$invite = xhInviteService::getByCode($code);
if (empty($invite)) {
util::fail('邀请码错误');
}
$now = time();
if ($now > $invite['deadline']) {
util::fail('邀请码已经失效');
}
if (!empty($invite['takeStatus'])) {
util::fail('邀请码已经使用');
}
util::success($invite);
}
public function actionSelect()
{
$isWx = util::isWx();
if ($isWx == false) {
return $this->renderPartial('notWeixin');
}
return $this->renderPartial('select');
}
public function actionApi()
{
$openData = file_get_contents('php://input');
if (isset ($openData) == false || empty($openData)) {
util::stop('has no post data');
}
$weixinSecret = Yii::getAlias("@vendor/weixinSecret");
require_once($weixinSecret . '/wxBizMsgCrypt.php');
$get = Yii::$app->request->get();
$encryptMsg = $openData;
$openId = configDict::getConfig('openId');
$open = xhWxOpenService::getById($openId);
$encodingAesKey = $open['aesKey'];
$token = $open['token'];
$appId = $open['appId'];
$signature = isset($get['signature']) ? $get['signature'] : '';
$timestamp = isset($get['timestamp']) ? $get['timestamp'] : '';
$nonce = isset($get['nonce']) ? $get['nonce'] : '';
$encrypt_type = isset($get['encrypt_type']) ? $get['encrypt_type'] : '';
$msg_signature = isset($get['msg_signature']) ? $get['msg_signature'] : '';
$xml_tree = new \DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$encrypt = $array_e->item(0)->nodeValue;
$format = "";
$from_xml = sprintf($format, $encrypt);
$formString = '1:' . $token . ' 2:' . $encodingAesKey . ' 3:' . $appId . ' 4:' . $msg_signature . ' 5:' . $timestamp . ' 6:' . $nonce . ' 7:' . $from_xml;
Yii::warning("formString:" . $formString);
//第三方收到公众号平台发送的消息
$pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
$msg = '';//解密后的消息
$errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
if ($errCode != 0) {
Yii::warning("解密未成功,错误代码:" . $errCode);
Yii::$app->end();
}
$postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
$infoType = $postObj->InfoType;
switch ($infoType) {
//推送 componentVerifyTicket
case 'component_verify_ticket':
$componentVerifyTicket = $postObj->ComponentVerifyTicket;
if (!empty($componentVerifyTicket)) {
$oData = [];
$oData['verifyTicket'] = (string)$componentVerifyTicket;
$oData['verifyTicketTime'] = date("Y-m-d H:i:s", $timestamp + 600);//每10分钟(600秒)推一次
xhWxOpenService::updateById($openId, $oData);
Yii::warning("component_verify_ticket:" . (string)$componentVerifyTicket);
util::stop('success');
}
break;
//取消授权通知
case 'unauthorized':
$AppId = $postObj->AppId;
$authorizeAppid = $postObj->AuthorizerAppid;
$merchant = xhMerchantService::getByAppId($authorizeAppid);
if (!empty($merchant)) {
$merchantId = $merchant['id'];
//自主冻结
xhMerchantService::updateById($merchantId, ['status' => 5]);
}
break;
//授权成功通知
case 'authorized':
$AppId = $postObj->AppId;//第三方平台appid
$CreateTime = $postObj->CreateTime;//公众号
$authorizeAppid = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
$AuthorizationCode = $postObj->AuthorizationCode;
$AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;//授权码过期时间
break;
//授权更新通知
case 'updateauthorized':
$AppId = $postObj->AppId;
$CreateTime = $postObj->CreateTime;
$authorizeAppid = $postObj->AuthorizerAppid;
$AuthorizationCode = $postObj->AuthorizationCode;
$AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;
$authorize = wxUtil::getAuthorizer($AuthorizationCode);
if (isset($authorize['authorization_info']) == false) {
$msg = '授权回调获取到auth_code,但未换取公众号的接口调用凭据和授权信息!!';
Yii::warning($msg, __METHOD__);
Yii::$app->end();
}
$info = $authorize['authorization_info'];
$authorizeAppId = $info['authorizer_appid'];
$authorize_access_token = $info['authorizer_access_token'];
$expires_in = $info['expires_in'];//7200
$authorize_refresh_token = $info['authorizer_refresh_token'];
$merchant = xhMerchantService::getByAppId($authorizeAppId);
if (empty($merchant)) {
return false;
}
$merchantId = $merchant['id'];
$wxData = [];
$wxData['wxAccessToken'] = $authorize_access_token;
$wxData['wxRefreshToken'] = $authorize_refresh_token;
$wxData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
$wxData['status'] = 1;
xhMerchantService::updateById($merchantId, $wxData);
break;
default:
}
Yii::warning("---wx open data end--- \n");
}
}