Browse Source

平台帐号

shish 6 years ago
parent
commit
fea0ff47f5

+ 165 - 166
app/www/controllers/AuthController.php

@@ -16,170 +16,169 @@ use common\components\wxUtil;
 
 class AuthController extends BaseController
 {
-
-    public $withoutLogin = ['prepare', 'get-user-info'];
-
-    //获取权限树 shish 2019.11.24
-    public function actionTree()
-    {
-        $get = Yii::$app->request->get();
-        $endId = isset($get['endId']) ? $get['endId'] : 1;
-        $tree = AuthService::getTree($endId);
-        util::success($tree);
-    }
-
-    //添加权限 shish 2019.11.24
-    public function actionAdd()
-    {
-        $post = Yii::$app->request->post();
-        $endId = isset($post['endId']) ? $post['endId'] : 1;
-        $authName = isset($post['authName']) ? $post['authName'] : '';
-        $list = AuthService::getNameList($endId);
-        if (in_array($authName, $list)) {
-            util::fail('名称已经存在');
-        }
-        AuthService::add($_POST);
-        util::ok();
-    }
-
-    //修改 shish 2019.11.24
-    public function actionUpdate()
-    {
-        $post = Yii::$app->request->post();
-        $id = isset($post['id']) ? $post['id'] : 0;
-        $auth = AuthService::getById($id);
-        if (empty($auth)) {
-            util::fail('没有找到权限');
-        }
-        $endId = $auth['endId'];
-        $nameList = AuthService::getNameList($endId);
-        $authName = isset($post['authName']) ? $post['authName'] : '';
-        if ($authName != $auth['authName'] && in_array($authName, $nameList)) {
-            util::fail('名称已经存在');
-        }
-        AuthService::updateById($id, $_POST);
-        util::ok();
-    }
-
-    //删除权限 shish 2020.1.7
-    public function actionDelete()
-    {
-        $id = Yii::$app->request->get('id');
-        util::ok('删除成功');
-    }
-
-    //准备授权 shish 2019.11.19
-    public function actionPrepare()
-    {
-        $get = Yii::$app->request->get();
-        $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
-        if (empty($url)) {
-            util::fail('没有网址');
-        }
-        $account = httpUtil::getAccount($url);
-        $account = '12362';
-        if (empty($account)) {
-            util::fail('没有商家帐号');
-        }
-        $merchant = MerchantService::getById($account);
-        if (empty($merchant)) {
-            util::fail('商家无效');
-        }
-        /**
-         * authScope 参数的说明
-         * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
-         * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
-         * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
-         * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
-         */
-        $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
-        $urlEncode = stringUtil::urlSafeB64Encode($url);
-        //微信授权获取code shish 2019.11.24
-        wxUtil::getCode($urlEncode, $merchant, $authScope);
-        util::end();
-    }
-
-    //微信授权获取用户信息 shish 2019.11.20
-    public function actionGetUserInfo()
-    {
-        $get = Yii::$app->request->get();
-        $code = isset($get['code']) ? $get['code'] : '';
-        if (empty($code)) {
-            util::fail('没有获取用户code');
-        }
-        if (isset($get['state']) && $get['state'] == 'authdeny') {
-            util::fail('auth fail');
-        }
-        $urlEncode = $get['url'];
-        $url = stringUtil::urlSafeBase64Decode($urlEncode);
-
-        $open = WxOpenService::getById(1);
-        if (empty($open)) {
-            util::fail('没有找到平台信息');
-        }
-        $account = isset($open['merchantId']) ? $open['merchantId'] : 0;
-        if (empty($account)) {
-            util::stop('平台没有绑定公众号');
-        }
-        $merchant = xhMerchantService::getByAccount($account);
-        if (empty($merchant)) {
-            util::stop('商店无效');
-        }
-        $merchantId = $merchant['id'];
-        $authScope = isset($get['authScope']) ? $get['authScope'] : '';
-        if (empty($authScope)) {
-            util::stop('没有授权类型');
-        }
-        $data = wxUtil::getOpenId($code, $merchant);
-        if ($data === false) {
-            //微信授权获取code shish 2019.11.24
-            wxUtil::getCode($urlEncode, $merchant, $authScope);
-            util::end();
-        }
-        Yii::info('abc');
-        $openId = $data['openid'];
-        $access_token = $data['access_token'];
-        $user = UserService::getByOpenId($openId, $merchantId);
-        //用户来源
-        $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
-        $source = $userSource['name'];
-        if (empty($user)) {
-            //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
-            $originalInfo = [];
-            $originalInfo['openId'] = $openId;
-            $originalInfo['merchantId'] = $merchantId;
-
-            //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
-            if ($authScope == 'snsapi_userinfo') {
-                $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
-                $originalInfo = array_merge($baseInfo, $originalInfo);
-                $originalInfo['isFull'] = 1;
-                $originalInfo['unionId'] = $baseInfo['unionid'];
-                $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
-                $originalInfo['nickName'] = $baseInfo['nickname'];
-            }
-            $user = UserService::replaceUser($originalInfo, $source, $merchantId);
-        } else {
-            if ($user['isFull'] == 0) {
-                if ($authScope == 'snsapi_userinfo') {
-                    $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
-                    $originalInfo = $baseInfo;
-                    $originalInfo['isFull'] = 1;
-                    $originalInfo['unionId'] = $baseInfo['unionid'];
-                    $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
-                    $originalInfo['openId'] = $baseInfo['openid'];
-                    $originalInfo['nickName'] = $baseInfo['nickname'];
-                    $originalInfo['merchantId'] = $merchantId;
-                    $user = UserService::replaceUser($originalInfo, $source, $merchantId);
-                }
-            }
-        }
-        //这个的基类没有设置统一的全局变量,这里进行设置一次
-        $userId = $user['id'];
-        //获取token
-        $token = jwt::getNewToken($userId);
-        $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
-        $this->redirect($url);
-    }
-
+	
+	public $withoutLogin = ['prepare', 'get-user-info'];
+	
+	//获取权限树 shish 2019.11.24
+	public function actionTree()
+	{
+		$get = Yii::$app->request->get();
+		$endId = isset($get['endId']) ? $get['endId'] : 1;
+		$tree = AuthService::getTree($endId);
+		util::success($tree);
+	}
+	
+	//添加权限 shish 2019.11.24
+	public function actionAdd()
+	{
+		$post = Yii::$app->request->post();
+		$endId = isset($post['endId']) ? $post['endId'] : 1;
+		$authName = isset($post['authName']) ? $post['authName'] : '';
+		$list = AuthService::getNameList($endId);
+		if (in_array($authName, $list)) {
+			util::fail('名称已经存在');
+		}
+		AuthService::add($_POST);
+		util::ok();
+	}
+	
+	//修改 shish 2019.11.24
+	public function actionUpdate()
+	{
+		$post = Yii::$app->request->post();
+		$id = isset($post['id']) ? $post['id'] : 0;
+		$auth = AuthService::getById($id);
+		if (empty($auth)) {
+			util::fail('没有找到权限');
+		}
+		$endId = $auth['endId'];
+		$nameList = AuthService::getNameList($endId);
+		$authName = isset($post['authName']) ? $post['authName'] : '';
+		if ($authName != $auth['authName'] && in_array($authName, $nameList)) {
+			util::fail('名称已经存在');
+		}
+		AuthService::updateById($id, $_POST);
+		util::ok();
+	}
+	
+	//删除权限 shish 2020.1.7
+	public function actionDelete()
+	{
+		$id = Yii::$app->request->get('id');
+		util::ok('删除成功');
+	}
+	
+	//准备授权 shish 2019.11.19
+	public function actionPrepare()
+	{
+		$get = Yii::$app->request->get();
+		$url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
+		if (empty($url)) {
+			util::fail('没有网址');
+		}
+		$account = WxOpenService::getPlatformAccount();
+		if (empty($account)) {
+			util::fail('没有商家帐号');
+		}
+		$merchant = MerchantService::getById($account);
+		if (empty($merchant)) {
+			util::fail('商家无效');
+		}
+		/**
+		 * authScope 参数的说明
+		 * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
+		 * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
+		 * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
+		 * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
+		 */
+		$authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
+		$urlEncode = stringUtil::urlSafeB64Encode($url);
+		//微信授权获取code shish 2019.11.24
+		wxUtil::getCode($urlEncode, $merchant, $authScope);
+		util::end();
+	}
+	
+	//微信授权获取用户信息 shish 2019.11.20
+	public function actionGetUserInfo()
+	{
+		$get = Yii::$app->request->get();
+		$code = isset($get['code']) ? $get['code'] : '';
+		if (empty($code)) {
+			util::fail('没有获取用户code');
+		}
+		if (isset($get['state']) && $get['state'] == 'authdeny') {
+			util::fail('auth fail');
+		}
+		$urlEncode = $get['url'];
+		$url = stringUtil::urlSafeBase64Decode($urlEncode);
+		
+		$open = WxOpenService::getById(1);
+		if (empty($open)) {
+			util::fail('没有找到平台信息');
+		}
+		$account = isset($open['merchantId']) ? $open['merchantId'] : 0;
+		if (empty($account)) {
+			util::stop('平台没有绑定公众号');
+		}
+		$merchant = xhMerchantService::getByAccount($account);
+		if (empty($merchant)) {
+			util::stop('商店无效');
+		}
+		$merchantId = $merchant['id'];
+		$authScope = isset($get['authScope']) ? $get['authScope'] : '';
+		if (empty($authScope)) {
+			util::stop('没有授权类型');
+		}
+		$data = wxUtil::getOpenId($code, $merchant);
+		if ($data === false) {
+			//微信授权获取code shish 2019.11.24
+			wxUtil::getCode($urlEncode, $merchant, $authScope);
+			util::end();
+		}
+		Yii::info('abc');
+		$openId = $data['openid'];
+		$access_token = $data['access_token'];
+		$user = UserService::getByOpenId($openId, $merchantId);
+		//用户来源
+		$userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
+		$source = $userSource['name'];
+		if (empty($user)) {
+			//$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
+			$originalInfo = [];
+			$originalInfo['openId'] = $openId;
+			$originalInfo['merchantId'] = $merchantId;
+			
+			//没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
+			if ($authScope == 'snsapi_userinfo') {
+				$baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
+				$originalInfo = array_merge($baseInfo, $originalInfo);
+				$originalInfo['isFull'] = 1;
+				$originalInfo['unionId'] = $baseInfo['unionid'];
+				$originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
+				$originalInfo['nickName'] = $baseInfo['nickname'];
+			}
+			$user = UserService::replaceUser($originalInfo, $source, $merchantId);
+		} else {
+			if ($user['isFull'] == 0) {
+				if ($authScope == 'snsapi_userinfo') {
+					$baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
+					$originalInfo = $baseInfo;
+					$originalInfo['isFull'] = 1;
+					$originalInfo['unionId'] = $baseInfo['unionid'];
+					$originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
+					$originalInfo['openId'] = $baseInfo['openid'];
+					$originalInfo['nickName'] = $baseInfo['nickname'];
+					$originalInfo['merchantId'] = $merchantId;
+					$user = UserService::replaceUser($originalInfo, $source, $merchantId);
+				}
+			}
+		}
+		//这个的基类没有设置统一的全局变量,这里进行设置一次
+		$userId = $user['id'];
+		//获取token
+		$token = jwt::getNewToken($userId);
+		$url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
+		$this->redirect($url);
+	}
+	
 }

+ 9 - 2
biz/weixin/classes/WxOpenClass.php

@@ -7,11 +7,18 @@ use biz\base\classes\BaseClass;
 
 class WxOpenClass extends BaseClass
 {
-
+	
 	public static $baseFile = '\biz\weixin\models\WxOpen';
-
+	
 	//平台管理员的手机号
 	const ADMIN_MOBILE = 15280215347;
+	const PLATFORM_ACCOUNT = 12359;
+	
+	//取平台的帐号
+	public static function getPlatformAccount()
+	{
+		return self::PLATFORM_ACCOUNT;
+	}
 
 	//获取开放平台 shish 2020.2.11
 	public static function getOpen()

+ 7 - 1
biz/weixin/services/WxOpenService.php

@@ -4,11 +4,11 @@ namespace biz\weixin\services;
 
 use biz\base\services\BaseService;
 use biz\weixin\classes\WxOpenClass;
-use common\components\httpUtil;
 use Yii;
 
 class WxOpenService extends BaseService
 {
+	
 	public static $baseFile = '\biz\weixin\classes\WxOpenClass';
 	
 	//获取当前开放平台信息 shish 2020.2.11
@@ -23,4 +23,10 @@ class WxOpenService extends BaseService
 		return WxOpenClass::getAdminMobile();
 	}
 	
+	//取平台帐号 shish 2020.3.7
+	public static function getPlatformAccount()
+	{
+		return WxOpenClass::getPlatformAccount();
+	}
+	
 }