AuthController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\controller;
  12. use app\admin\model\wechat\WechatQrcode;
  13. use app\wap\model\store\StoreOrder;
  14. use app\wap\model\user\User;
  15. use app\wap\model\user\WechatUser;
  16. use basic\WapBasic;
  17. use service\JsonService;
  18. use service\SystemConfigService;
  19. use service\UtilService;
  20. use think\Cache;
  21. use think\cache\driver\Redis;
  22. use think\Cookie;
  23. use think\Session;
  24. use think\Url;
  25. use app\wap\model\user\MemberShip;
  26. use service\GroupDataService;
  27. class AuthController extends WapBasic
  28. {
  29. /**
  30. * 用户ID
  31. * @var int
  32. */
  33. protected $uid = 0;
  34. /**
  35. * 用户信息
  36. * @var
  37. */
  38. protected $userInfo;
  39. protected $phone;
  40. protected $isWechat = false;
  41. protected $redisModel;
  42. protected $subjectUrl='';
  43. protected function _initialize()
  44. {
  45. parent::_initialize();
  46. $this->redisModel = new Redis();
  47. $this->isWechat = UtilService::isWechatBrowser();
  48. $spread_uid = $this->request->get('spread_uid', 0);
  49. $NoWechantVisitWhite = $this->NoWechantVisitWhite();
  50. $subscribe = false;
  51. $site_url = SystemConfigService::get('site_url');
  52. $this->subjectUrl=getUrlToDomain($site_url);
  53. try {
  54. $uid = User::getActiveUid();
  55. if (!empty($uid)) {
  56. $this->userInfo = User::getUserInfo($uid);
  57. MemberShip::memberExpiration($uid);
  58. if($spread_uid) $spreadUserInfo = User::getUserInfo($spread_uid);
  59. $this->uid = $this->userInfo['uid'];
  60. $this->phone = User::getLogPhone($uid);
  61. //绑定临时推广人
  62. if ($spread_uid && $spreadUserInfo && $this->uid != $spread_uid && $spreadUserInfo['spread_uid']!=$this->uid && $this->userInfo['spread_uid'] != $spread_uid && !$this->userInfo['spread_uid']) {
  63. User::edit(['spread_uid' => $spread_uid], $this->uid, 'uid');
  64. }
  65. if (!isset($this->userInfo['uid'])) $this->userInfo['uid'] = 0;
  66. if (!isset($this->userInfo['is_promoter'])) $this->userInfo['is_promoter'] = 0;
  67. if (!isset($this->userInfo['avatar'])) $this->userInfo['avatar'] = '';
  68. if (!isset($this->userInfo['nickname'])) $this->userInfo['nickname'] = '';
  69. //提取二维码
  70. try {
  71. $codeUrl = WechatQrcode::getTemporaryQrcode('binding', $this->uid);
  72. if (empty($this->userInfo))
  73. $codeUrl = SystemConfigService::get('wechat_qrcode');
  74. else
  75. $codeUrl = $codeUrl['url'];
  76. } catch (\Throwable $e) {
  77. $codeUrl = '';
  78. }
  79. //是否关注公众号
  80. $subscribe = WechatUser::where('uid', $this->uid)->value('subscribe');
  81. if (!$NoWechantVisitWhite) {
  82. if (!$this->userInfo || !isset($this->userInfo['uid'])) return $this->failed('读取用户信息失败!');
  83. if (!$this->userInfo['status']) return $this->failed('已被禁止登陆!');
  84. }
  85. }
  86. } catch (\Exception $e) {
  87. Cookie::set('is_login', 0);
  88. Session::delete(['loginUid', 'loginOpenid']);
  89. $url = $this->request->url(true);
  90. if (!$NoWechantVisitWhite) {
  91. if ($this->request->isAjax())
  92. return JsonService::fail('请登录在进行访问');
  93. else
  94. return $this->redirect(Url::build('Login/index', ['spread_uid' => $spread_uid]) . '?ref=' . base64_encode(htmlspecialchars($url)));
  95. }
  96. }
  97. if (Cache::has('__SYSTEM__')) {
  98. $overallShareWechat = Cache::get('__SYSTEM__');
  99. } else {
  100. $overallShareWechat = SystemConfigService::more(['wechat_share_img', 'wechat_share_title', 'wechat_share_synopsis']);
  101. Cache::set('__SYSTEM__', $overallShareWechat, 800);
  102. }
  103. $codeUrl = SystemConfigService::get('wechat_qrcode');
  104. $balance_switch=SystemConfigService::get('balance_switch');//余额开关
  105. $alipay_switch=SystemConfigService::get('alipay_switch');//支付宝开关
  106. $this->assign([
  107. 'code_url' => $codeUrl,
  108. 'is_yue' => $balance_switch,
  109. 'is_alipay' => $alipay_switch,
  110. 'subscribe' => $subscribe,
  111. 'subscribeQrcode' => SystemConfigService::get('wechat_qrcode'),
  112. 'userInfo' => $this->userInfo,
  113. 'now_money' => $this->userInfo['now_money'] ? $this->userInfo['now_money'] : 0,
  114. 'phone' => $this->phone,
  115. 'isWechat' => $this->isWechat,
  116. 'overallShareWechat' => json_encode($overallShareWechat),
  117. 'Auth_site_name' => SystemConfigService::get('site_name'),
  118. 'menus'=>GroupDataService::getData('bottom_navigation')
  119. ]);
  120. }
  121. /**
  122. * 检查白名单控制器方法 存在带名单返回 true 不存在则进行登录
  123. * @return bool
  124. */
  125. protected function NoWechantVisitWhite()
  126. {
  127. if ($this->isWechat) return false;
  128. list($module, $controller, $action, $className) = $this->getCurrentController();
  129. if (method_exists($className, 'WhiteList')) {
  130. $whitelist = $className::WhiteList();
  131. if (!is_array($whitelist)) return false;
  132. foreach ($whitelist as $item) {
  133. if (strtolower($module . '\\' . $controller . '\\' . $item) == strtolower($module . '\\' . $controller . '\\' . $action)) {
  134. return true;
  135. }
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * 获取当前的控制器名,模块名,方法名,类名并返回
  142. * @return array
  143. */
  144. protected function getCurrentController()
  145. {
  146. $module = $this->request->module();
  147. $controller = $this->request->controller();
  148. $action = $this->request->action();
  149. if (strstr($controller, '.'))
  150. $controllerv1 = str_replace('.', '\\', $controller);
  151. else
  152. $controllerv1 = $controller;
  153. $className = 'app\\' . $module . '\\controller\\' . $controllerv1;
  154. return [$module, $controller, $action, $className];
  155. }
  156. }