openUrl . '/join/callback'; $merchantId = $open['merchantId'];//如果平台没有绑定商家,先去创建第一个商家 if (empty($merchantId)) { $url = $this->openUrl . '/join/callback-create-first-merchant'; } $redirect = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$appId}&pre_auth_code={$pre}&redirect_uri={$url}"; return $this->renderPartial('apply', ['url' => $redirect]); } public function actionCheckMerchantName() { $get = Yii::$app->request->get(); $merchantName = isset($get['merchantName']) ? $get['merchantName'] : ''; $m = xhMerchantService::getByMerchantName($merchantName); if (!empty($m)) { util::failInfo('公众号名称已经使用'); } util::successInfo(); } /** * 下载微信里的图片到服务器 */ public function actionDownloadWxImg() { $get = Yii::$app->request->get(); $mediaId = isset($get['mediaId']) ? $get['mediaId'] : ''; $imgList = weixinUtil::downloadmediaIdImg($this->merchant, $mediaId); if (!empty($imgList)) { util::successInfo('操作成功', 'A0001', $imgList); } util::failInfo(); } /** * 创建第一个商家 */ public function actionCallbackCreateFirstMerchant() { $get = Yii::$app->request->get(); if (isset($get['auth_code']) == false) { return $this->renderPartial('hint', ['msg' => '公众号授权回调未成功']); } $code = $get['auth_code']; //使用授权码换取公众号的接口调用凭据和授权信息 $authorizer = weixinOpenUtil::getAuthorizer($code); if (isset($authorizer['authorization_info']) == false) { return $this->renderPartial('hint', ['msg' => '未换取公众号的接口调用凭据和授权信息']); } $info = $authorizer['authorization_info']; $authorizer_appid = $info['authorizer_appid']; $authorizer_access_token = $info['authorizer_access_token']; $expires_in = $info['expires_in'];//7200 $authorizer_refresh_token = $info['authorizer_refresh_token']; //获取授权方的公众号帐号基本信息 $authorizerDetailInfo = weixinOpenUtil::getAuthorizerInfo($authorizer_appid); if (isset($authorizerDetailInfo['authorizer_info']) == false) { return $this->renderPartial('hint', ['msg' => '没有获取到公众号帐号基本信息']); } $authorizer_info = $authorizerDetailInfo['authorizer_info']; $nick_name = $authorizer_info['nick_name']; $head_img = $authorizer_info['head_img']; $service_type_info = $authorizer_info['service_type_info']; if ($service_type_info['id'] != 2) { return $this->renderPartial('hint', ['msg' => '公众号不是服务号']); } $verify_type_info = $authorizer_info['verify_type_info']; if ($verify_type_info['id'] != 0) { return $this->renderPartial('hint', ['msg' => '公众号还没有认证哦']); } $user_name = $authorizer_info['user_name']; $alias = $authorizer_info['alias']; $qrcode_url = $authorizer_info['qrcode_url']; $business_info = $authorizer_info['business_info']; $open_pay = $business_info['open_pay']; $merchant = xhMerchantService::getByAppId($authorizer_appid); $wxAccessTokenTime = date("Y-m-d H:i:s", (time() + $expires_in - 100));//安全起见,将过期时间设置得比微信里还短100秒 if (!empty($merchant)) { return $this->renderPartial('hint', ['msg' => '平台已经关联公众号了']); } //引导生成商家 $date = date("Y-m-d H:i:s"); $trainDemo = configDict::getConfig('trainDemo'); $logo = xhMerchantService::logoSave($head_img, $authorizer_appid); $appData['logo'] = $logo; $wxQrcodeUrl = xhMerchantService::qrCodeSave($qrcode_url, $authorizer_appid); $appData['wxQrcodeUrl'] = $wxQrcodeUrl; $appData['wxAppId'] = $authorizer_appid; $appData['wxUserName'] = $user_name; $appData['wxAliasName'] = $alias; $appData['wxAccessToken'] = $authorizer_access_token; $appData['wxAccessTokenTime'] = $wxAccessTokenTime; $appData['wxRefreshToken'] = $authorizer_refresh_token; $appData['accountStyle'] = 0;//认证服务号 $appData['status'] = 0;//待审核 $appData['createTime'] = $date; $appData['updateTime'] = $date; $appData['shopLat'] = (string)0; $appData['shopLong'] = (string)0; $appData['shopPrecision'] = (string)0; $appData['account'] = $trainDemo['platform']['account']; $appData['adminId'] = 0; $appData['wxToken'] = stringUtil::buildWxToken(0); $appData['shopPhotoList'] = ''; $appData['merchantName'] = $nick_name; $appData['shopProvince'] = '福建'; $appData['shopCity'] = '厦门'; $appData['shopDist'] = '思明区'; $appData['alipayAccount'] = 'shishaohua8879@sina.com'; $appData['alipayAccountName'] = '石少华'; $appData['agentLevel'] = 1;//默认设置为代理商 $merchant = xhMerchantService::applyCreateMerchant($appData); $wxOpenId = configDict::getConfig('openId'); $merchantId = $merchant['id']; $account = $merchant['id']; $createTime = date("Y-m-d H:i:s"); $orderId = 'A' . stringUtil::buildOrderNo(); $orderData = ['id' => $orderId, 'userId' => 0, 'applyName' => $nick_name, 'email' => '', 'createTime' => $createTime, 'accountId' => 0, 'inviteCode' => '', 'merchantId' => $merchantId, 'invitedMerchantId' => $merchantId, 'prePrice' => 0, 'actPrice' => 0, 'deadline' => time() + 2592000, 'inviteCodeStatus' => 0, 'payStatus' => 1, ]; xhApplyOrderService::add($orderData);//保存订单信息 xhWxOpenService::updateById($wxOpenId, ['merchantId' => $merchantId]); xhWxMenuService::applyInit($merchant);//初始化微信菜单 $openMerchant = xhWxOpenService::getMerchant(); $openMerchantAccount = $openmerchant['id']; //将新生成的商家account加密传到后台绑定管理员,密钥有效期5分钟 $newAccount = urlencode(stringUtil::authcode($account, 'ENCODE', 300)); $this->redirect(Yii::$app->params['adminUrl'] . '/admin/generate?account=' . $openMerchantAccount . '&newAccount=' . $newAccount); } public function actionCallback() { $get = Yii::$app->request->get(); if (isset($get['auth_code']) == false) { return $this->renderPartial('hint', ['msg' => '公众号授权回调未成功']); } $code = $get['auth_code']; //使用授权码换取公众号的接口调用凭据和授权信息 $authorizer = weixinOpenUtil::getAuthorizer($code); if (isset($authorizer['authorization_info']) == false) { return $this->renderPartial('hint', ['msg' => '未换取公众号的接口调用凭据和授权信息']); } $info = $authorizer['authorization_info']; $authorizer_appid = $info['authorizer_appid']; $authorizer_access_token = $info['authorizer_access_token']; $expires_in = $info['expires_in'];//7200 $authorizer_refresh_token = $info['authorizer_refresh_token']; //获取授权方的基本信息 $authorizerDetailInfo = weixinOpenUtil::getAuthorizerInfo($authorizer_appid); print_r($authorizerDetailInfo); if (isset($authorizerDetailInfo['authorizer_info']) == false) { return $this->renderPartial('hint', ['msg' => '没有获取到公众号帐号基本信息']); } $authorizer_info = $authorizerDetailInfo['authorizer_info']; $nick_name = $authorizer_info['nick_name']; $head_img = $authorizer_info['head_img']; $service_type_info = $authorizer_info['service_type_info']; if ($service_type_info['id'] != 2) { //return $this->renderPartial('hint',['msg' => '公众号不是服务号,appid:'.$authorizer_appid.' refresh:'.$authorizer_refresh_token]); } $verify_type_info = $authorizer_info['verify_type_info']; if ($verify_type_info['id'] != 0) { //return $this->renderPartial('hint',['msg' => '公众号还没有认证哦']); } $user_name = $authorizer_info['user_name']; $alias = $authorizer_info['alias']; $qrcode_url = $authorizer_info['qrcode_url']; $business_info = $authorizer_info['business_info']; $open_pay = $business_info['open_pay']; $accessTokenTime = date("Y-m-d H:i:s", (time() + $expires_in - 100));//安全起见,将过期时间设置得比微信里还短100秒 //小程序 if (isset($authorizer_info['MiniProgramInfo']) && !empty($authorizer_info['MiniProgramInfo'])) { $merchant = xhMerchantService::getByCondition(['miniAppId' => $authorizer_appid]); if (empty($merchant)) { Yii::info('没有小程序信息 appid:' . $authorizer_appid); echo '没有小程序信息 appid:' . $authorizer_appid; Yii::$app->end(); } $merchantId = $merchant['id']; xhMerchantExtendService::updateByMerchantId($merchantId, ['miniAccessToken' => $authorizer_access_token, 'miniAccessTokenTime' => $accessTokenTime, 'miniRefreshToken' => $authorizer_refresh_token]); Yii::info('小程序信息更新成功:' . json_encode($authorizer_info)); echo "
";
echo "小程序信息更新成功";
} else {
$merchant = xhMerchantService::getByAppId($authorizer_appid);
Yii::info('appId:' . $authorizer_appid);
$set = xhSetMealService::getRandOne();
if (empty($set)) {
return $this->renderPartial('hint', ['msg' => '没有套餐信息']);
}
if (empty($merchant)) {
echo '这个商家没有生成过基本信息。appid:' . $authorizer_appid;
Yii::$app->end();
}
if (empty($merchant)) {
echo '新建商家能力暂时关闭';
Yii::$app->end();
//没有生成商家,引导进去设置
$session = Yii::$app->session;
$session['wxAppId'] = $authorizer_appid;
$session['wxUserName'] = $user_name;
$session['wxAliasName'] = $alias;
$session['wxAccessToken'] = $authorizer_access_token;
$session['wxAccessTokenTime'] = $accessTokenTime;
$session['wxRefreshToken'] = $authorizer_refresh_token;
$session['logo'] = $head_img;
$session['wxQrcodeUrl'] = $qrcode_url;
$session['allowGenerateMerchant'] = 'ok';//经过授权流程,允许允许生成商家
$trainDemo = configDict::getConfig('trainDemo');
$demoName = $trainDemo['merchant']['name'];//培训演示的公众号
return $this->renderPartial('callback', ['merchantName' => $nick_name, 'set' => $set, 'demoName' => $demoName]);
}
$cData = [];
$cData['wxAccessToken'] = $authorizer_access_token;
$cData['wxAccessTokenTime'] = $accessTokenTime;
$cData['wxRefreshToken'] = $authorizer_refresh_token;
//审核通过
$cData['status'] = 1;
$merchantId = $merchant['id'];
xhMerchantService::updateById($merchantId, $cData);
}
echo $authorizer_appid . ' ';
exit('ok');
$price = $set['price'];
$now = time();
$apply = xhApplyOrderService::getByMerchantId($merchantId);
if (empty($apply)) {
return $this->renderPartial('hint', ['msg' => '没有申请记录']);
}
$orderId = $apply['id'];
$price = $apply['actPrice'];
$openMerchant = xhWxOpenService::getMerchant();
$openMerchantAccount = $openmerchant['id'];
$account = $merchant['id'];
//将新生成的商家account加密传到后台绑定管理员,密钥有效期5分钟
$newAccount = urlencode(stringUtil::authcode($account, 'ENCODE', 300));
if ($apply['payStatus'] == 1) {
if (empty($merchant['adminId'])) {
$this->redirect(Yii::$app->params['adminUrl'] . '/admin/generate?account=' . $openMerchantAccount . '&newAccount=' . $newAccount);
util::end();
}
return $this->renderPartial('hint', ['msg' => '恭喜!您已申请成功。']);
}
if ($price > 0) {
$this->redirect(Yii::$app->params['openUrl'] . '/pay/apply?orderId=' . $apply['id']);
util::end();
} else {
$inviteCode = $order['inviteCode'];
$now = time();
if (!empty($inviteCode)) {
$invite = xhInviteService::getByCode($inviteCode);
if (empty($invite)) {
return $this->renderPartial('hint', ['msg' => '邀请码不存在']);
}
if ($invite['takeStatus'] == 1) {//邀请码未使用,并且没有过期
return $this->renderPartial('hint', ['msg' => '邀请码已经使用,请联系客服']);
}
$inviteDeadline = $invite['deadline'];
if ($now > $inviteDeadline) {
return $this->renderPartial('hint', ['msg' => '邀请码已经过期,请联系客服']);
}
xhInviteService::updateByCode($inviteCode, ['takeStatus' => 1, 'inviteUseTo' => 0, 'targetId' => $orderId, 'invitedMerchantId' => $merchantId]);
}
$updateData = ['inviteCodeStatus' => 1, 'payStatus' => 1, 'payWay' => 0, 'invitedMerchantId' => $merchantId];
xhApplyOrderService::updateById($orderId, $updateData);
$this->redirect(Yii::$app->params['adminUrl'] . '/admin/generate?account=' . $openMerchantAccount . '&newAccount=' . $newAccount);
util::end();
}
}
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 actionAddMerchant()
{
$post = Yii::$app->request->post();
$session = Yii::$app->session;
if (isset($session['allowGenerateMerchant']) == false) {
//util::failInfo('没有经过授权的操作');
}
unset($post['_csrf']);
if (isset($session['wxAppId']) == false || empty($session['wxAppId'])) {
util::failInfo('公众号appId不存在');
}
$wxAppId = $session['wxAppId'];
if (isset($session['logo'])) {
$logo = xhMerchantService::logoSave($session['logo'], $wxAppId);
$post['logo'] = $logo;
}
if (isset($session['wxQrcodeUrl'])) {
$wxQrcodeUrl = xhMerchantService::qrCodeSave($session['wxQrcodeUrl'], $wxAppId);
$post['wxQrcodeUrl'] = $wxQrcodeUrl;
}
$date = date("Y-m-d H:i:s");
$inviteCode = isset($post['inviteCode']) ? $post['inviteCode'] : '';
$accountStyle = configDict::getConfig('accountStyle');
$merchantStatus = configDict::getConfig('merchantStatus');
$serviceAccount = $accountStyle['serviceAccount'];//认证服务号
$checking = $merchantStatus['checking'];//审核中
$merchantName = !empty($post['merchantName']) ? $post['merchantName'] : '';
$exists = xhMerchantService::getByMerchantName($merchantName);
if (!empty($exists)) {
util::failInfo('花店名称已经存在了');
}
$post['wxAppId'] = $wxAppId;
$post['wxUserName'] = $session['wxUserName'];
$post['wxAliasName'] = $session['wxAliasName'];
$post['wxAccessToken'] = $session['wxAccessToken'];
$post['wxAccessTokenTime'] = $session['wxAccessTokenTime'];
$post['wxRefreshToken'] = $session['wxRefreshToken'];
$post['accountStyle'] = $serviceAccount;//认证服务号
$post['status'] = $checking;//待审
$post['createTime'] = $date;
$post['updateTime'] = $date;
$post['shopLat'] = isset($post['shopLat']) ? (string)$post['shopLat'] : (string)0;
$post['shopLong'] = isset($post['shopLong']) ? (string)$post['shopLong'] : (string)0;
$post['shopPrecision'] = isset($post['shopPrecision']) ? (string)$post['shopPrecision'] : (string)0;
$account = stringUtil::buildOrderNo(0);
$trainDemo = configDict::getConfig('trainDemo');
$demoName = $trainDemo['merchant']['name'];//培训演示的公众号
if (isset($post['merchantName']) && $post['merchantName'] == $demoName) {
$account = $trainDemo['merchant']['account'];
}
$post['account'] = $account;
$post['adminId'] = 0;
$post['wxToken'] = stringUtil::buildWxToken(0);
$shopPhotoListString = json_encode(["outdoor" => $post['shopImg'], 'indoor' => $post['shopImg2']]);//将门店照片组合成json
$post['shopPhotoList'] = $shopPhotoListString;
$merchant = xhMerchantService::applyCreateMerchant($post);
$id = $merchant['id'];
$account = $merchant['id'];
unset($session['allowGenerateMerchant']);
xhWxMenuService::applyInit($merchant);
$set = xhSetMealService::getRandOne();
$price = $set['price'];
$actPrice = $price;
$mainMerchantId = 0;
$now = time();
$orderDeadline = 0;
if (!empty($inviteCode)) {
$invite = xhInviteService::getByCode($inviteCode);
if (empty($invite)) {
util::failInfo('邀请码不存在');
}
if ($invite['takeStatus'] == 1) {//邀请码未使用,并且没有过期
util::failInfo('邀请码已经使用');
}
$inviteDeadline = $invite['deadline'];
if ($now > $inviteDeadline) {
util::failInfo('邀请码已经过期');
}
$mainMerchantId = isset($invite['merchantId']) ? $invite['merchantId'] : 0;
$actPrice = stringUtil::calcSub($set['price'], $invite['save']);//扣除邀请码里的钱
}
$toPay = $actPrice <= 0 ? 'no' : 'yes';//是否需要付款
$inviteCodeStatus = $actPrice <= 0 ? 1 : 0;//邀请码是否已使用
$payStatus = $actPrice <= 0 ? 1 : 0;//付款是否成功
$createTime = date("Y-m-d H:i:s");
$orderId = 'A' . stringUtil::buildOrderNo($id);
$orderData = ['id' => $orderId, 'userId' => $this->userId, 'applyName' => $merchantName, 'email' => '',
'createTime' => $createTime, 'accountId' => 0, 'inviteCode' => $inviteCode, 'merchantId' => $mainMerchantId, 'invitedMerchantId' => $id,
'prePrice' => $price, 'actPrice' => $actPrice, 'deadline' => $orderDeadline, 'inviteCodeStatus' => (int)$inviteCodeStatus, 'payStatus' => $payStatus,
];
xhApplyOrderService::add($orderData);//保存订单信息
if ($actPrice <= 0 && !empty($inviteCode)) {
xhInviteService::updateByCode($inviteCode, ['targetId' => $orderId, 'takeStatus' => 1, 'invitedMerchantId' => $id, 'inviteUseTo' => 0]);
}
$openMerchant = xhWxOpenService::getMerchant();
$openMerchantAccount = $openmerchant['id'];
$newAccount = urlencode(stringUtil::authcode($account, 'ENCODE', 300));
util::successInfo('提交成功', 'A0001', ['account' => $openMerchantAccount, 'newAccount' => $newAccount, 'toPay' => $toPay, 'orderId' => $orderId, 'actPrice' => $actPrice]);
}
public function actionCheckInviteCode()
{
$get = Yii::$app->request->get();
$code = isset($get['code']) ? $get['code'] : '';
$invite = xhInviteService::getByCode($code);
if (empty($invite)) {
util::failInfo('邀请码错误');
}
$now = time();
if ($now > $invite['deadline']) {
util::failInfo('邀请码已经失效');
}
if (!empty($invite['takeStatus'])) {
util::failInfo('邀请码已经使用');
}
util::successInfo('操作成功', 'A0001', $invite);
}
public function actionSelect()
{
$isWeixin = util::isWeixin();
if ($isWeixin == false) {
return $this->renderPartial('notWeixin');
}
return $this->renderPartial('select');
}
}