OpenendController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace openend\controllers;
  3. use common\services\xhMerchantExtendService;
  4. use common\services\xhWxOpenService;
  5. use Yii;
  6. use yii\web\Controller;
  7. use common\components\jsSDK;
  8. use common\components\weixinUtil;
  9. use common\components\configDict;
  10. use common\components\util;
  11. use common\services\xhWxOpenAdminService;
  12. use common\services\xhUserService;
  13. use common\services\xhMerchantService;
  14. /**
  15. * 前台次级基类,次于PublicController
  16. */
  17. class OpenendController extends PublicController{
  18. public $isLogin = false;
  19. public $withoutLogin = [];//不需要登陆的方法名
  20. public $userId,$user,$isWeixin,$signPackage,$merchant,$merchantId,$merchantExtend;
  21. public $wxOpen;
  22. public function beforeAction($action){
  23. $this->isWeixin = util::isWeixin();
  24. $get = Yii::$app->request->get();
  25. $account = isset($get['account']) ? $get['account'] : '';
  26. $one = xhMerchantService::getOne();
  27. $wxOpenId = configDict::getConfig('openId');
  28. $this->wxOpen = xhWxOpenService::getById($wxOpenId);
  29. $noNeedLogin = false;
  30. if(!empty($one)){
  31. if(empty($account)){
  32. $merchant = xhWxOpenService::getMerchant();
  33. }else{
  34. $merchant = xhMerchantService::getByAccount($account);
  35. }
  36. $account = $merchant['id'];
  37. $this->merchant = $merchant;
  38. $merchantId = $merchant['id'];
  39. $this->merchantId = $merchantId;
  40. $this->merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  41. if($this->isWeixin){
  42. $jssdk = new jsSDK();//微信分享
  43. $this->signPackage = $jssdk->GetSignPackage($merchant);
  44. }
  45. }else{
  46. $noNeedLogin = true;
  47. }
  48. if(in_array($action->id, $this->withoutLogin) || $this->isLogin == true){
  49. $noNeedLogin = true;
  50. }
  51. $session = Yii::$app->session;
  52. if(isset($session['visitingMerchantAccount']) && $session['visitingMerchantAccount'] != $account){
  53. xhUserService::logout();
  54. }
  55. $this->isLogin = xhUserService::isLogin();
  56. if($this->isLogin){
  57. $userId = xhUserService::getId();
  58. $user = xhUserService::getById($userId);
  59. $this->userId = $userId;
  60. $this->user = $user;
  61. $noNeedLogin = true;
  62. }
  63. if ($this->isWeixin) {//如果是微信,走微信授权取用户信息
  64. if($noNeedLogin == false){
  65. //snsapi_userinfo 需要用户授权获取用户信息,snsapi_base表示自动静默方式
  66. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_userinfo';
  67. $params = empty($_SERVER['REQUEST_URI']) ? '' : base64_encode($_SERVER['REQUEST_URI']);//取主域名后面的参数
  68. $url = 'http://'.$_SERVER['HTTP_HOST'].'/auth/get-user-info?params='.$params.'&authScope='.$authScope;
  69. weixinUtil::getToken($url,$merchant,$authScope);
  70. util::end();
  71. }
  72. }else{
  73. if($noNeedLogin == false){
  74. $this->redirect(['main/login']);
  75. util::stop();
  76. }
  77. }
  78. return parent::beforeAction($action);
  79. }
  80. }