文件
mashangyoujiu/Application/Common/Business/AuthMerchant.class.php

146 行
5.3 KiB
PHP
可执行文件

<?php
namespace Common\Business;
//[1.6.9]
use Think\Jssdk;
use Think\Jssdks;
class AuthMerchant
{
/**
* 授权给商户公众号
* @param $param
* [
* merchant_id
* code
* ]
* @return mixed
*/
Public function auth($param)
{
//定义全局门店id
if (empty($param['merchant_id'])) {
return callBack(statusCode()['error'], "非法访问");
}
$setResult = $this->checkSet($param);
if ($setResult['status'] != 1) {
return $setResult;
}
$set = $setResult['data']; //商户的公众号配置
if (strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger")) {
switch ($set['type']) {
case 1:
//商户自定义配置的公众号
//判断code是否存在
if (!empty($param['code'])) {
if ($param['code'] != 'authdeny') {
//用户授权
$options['appid'] = $set['appid'];
$options['appsecret'] = $set['appsercet'];
$wx = new \Util\Wx\Wechat($options);
$wx_call = $wx->getOauthAccessToken($param['code']);//获取access_token和openid
if (!$wx_call) {
return callBack(statusCode()['error'], "授权失败");
}else{
return callBack(statusCode()['success'], "获取成功", $wx_call['openid']);
}
} else {
return callBack(statusCode()['error'], "请授权");
}
} else {
$this->getCode([
'appid' => $set['appid'],
'appsecret' => $set['appsercet'],
'type' => 1
]);
}
break;
case 2:
//商户授权的公众号
$options = [
'appid'=>$set['authorizer_appid']
];
$appinfo = M('appweixin')->where(['id' => 1])->find();
//判断code是否存在
if (!empty($param['code'])) {
if ($param['code'] != 'authdeny') {
//用户授权
$wx = new \Util\Wx\Wechat($options);
$token = get_acc_token();
$wx_call = $wx->getOauthAccessTokens($param['code'], $appinfo['appid'], $token);//获取access_token和openid
if (!$wx_call) {
return callBack(statusCode()['error'], "获取失败");
}else{
return callBack(statusCode()['success'], "获取成功", $wx_call['openid']);
}
} else {
return callBack(statusCode()['error'], "请授权");
}
} else {
$this->getCode([
'appid' => $options['appid'],
'third_appid' => $appinfo['appid'],
'type' => 2
]);
}
break;
default:
return callBack(statusCode()['error'], "微信配置参数有误");
break;
}
} else {
return callBack(statusCode()['error'], "请使用微信浏览器访问本应用!");
}
}
/**
* 获取code
* @param $param
* [
* type 1 自定义公众号 2 授权的公众号
* appid 公众号app_id
* appsecret 公众号 secret
* third_appid 第三方appid
* ]
* @return mixed
*/
public function getCode($param)
{
$options['appid'] = $param['appid'];
if ($param['type'] == 1) {
$options['appsecret'] = $param['appsecret'];
}
if ($_SERVER["HTTPS"] == "on") {
$title = 'https';
} else {
$title = 'http';
}
$url = $title . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$wx = new \Util\Wx\Wechat($options);
if ($param['type'] == 1) {
$_url = $wx->getOauthRedirect($url, '1', 'snsapi_userinfo');
} else {
$_url = $wx->getOauthRedirects($url, '1', 'snsapi_userinfo', $param['third_appid']);
}
header("Location:" . $_url);
exit;
}
/**
* 设置全局变量 全局缓存SET
* @param $param
* @return mixed
*/
public function checkSet($param)
{
$set = M('wxconfig')->where(['merchantid' => $param['merchant_id']])->find();
if (!empty($set)) {
return callBack(statusCode()['success'], "系统全局设置已定义", $set);
} else {
return callBack(statusCode()['error'], "系统全局设置未定义");
}
}
}