AuthController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\wap\model\user\User;
  13. use app\wap\model\user\WechatUser;
  14. use basic\WapBasic;
  15. use service\JsonService;
  16. use service\SystemConfigService;
  17. use service\UtilService;
  18. use think\Cache;
  19. use think\cache\driver\Redis;
  20. use think\Cookie;
  21. use think\Session;
  22. use think\Url;
  23. use app\wap\model\user\MemberShip;
  24. use service\GroupDataService;
  25. use think\Request;
  26. class AuthController extends WapBasic
  27. {
  28. /**
  29. * 用户ID
  30. * @var int
  31. */
  32. protected $uid = 0;
  33. /**
  34. * 用户信息
  35. * @var
  36. */
  37. protected $userInfo;
  38. protected $phone;
  39. protected $force_binding;
  40. protected $isWechat = false;
  41. protected $redisModel;
  42. protected $subjectUrl = '';
  43. protected function _initialize()
  44. {
  45. parent::_initialize();
  46. $pc_on_display = SystemConfigService::get('pc_on_display');
  47. if (!request()->isMobile() && is_dir(APP_PATH . 'web') && $pc_on_display) {
  48. return $this->redirect(Url::build('web/index/index'));
  49. }
  50. try {
  51. $this->redisModel = new Redis();
  52. } catch (\Exception $e) {
  53. parent::serRedisPwd($e->getMessage());
  54. }
  55. $this->isWechat = UtilService::isWechatBrowser();
  56. $spread_uid = Request::instance()->param('spread_uid',0);
  57. $NoWechantVisitWhite = $this->NoWechantVisitWhite();
  58. $subscribe = false;
  59. $site_url = SystemConfigService::get('site_url');
  60. $this->subjectUrl = getUrlToDomain();
  61. try {
  62. $uid = User::getActiveUid();
  63. if (!empty($uid)) {
  64. $this->userInfo = User::getUserInfo($uid);
  65. if ($this->isWechat) {
  66. if ($this->userInfo['nickname'] == '' && $this->userInfo['avatar'] == '' || $this->userInfo['nickname'] == '' && $this->userInfo['avatar'] == '/system/images/user_log.jpg') {
  67. $url = $this->request->url(true);
  68. if (!$this->request->isAjax()) {
  69. return $this->redirect(Url::build('Login/index', ['spread_uid' => $spread_uid]) . '?ref=' . base64_encode(htmlspecialchars($url)));
  70. }
  71. }
  72. }
  73. MemberShip::memberExpiration($uid);
  74. if ($spread_uid) $spreadUserInfo = User::getUserData($spread_uid);
  75. $this->uid = $this->userInfo['uid'];
  76. $this->phone = User::getLogPhone($uid);
  77. //绑定推广人
  78. if ($spread_uid && $spreadUserInfo && $this->uid != $spread_uid && $spreadUserInfo['spread_uid'] != $this->uid && $this->userInfo['spread_uid'] != $spread_uid && !$this->userInfo['spread_uid']) {
  79. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  80. if ($storeBrokerageStatu == 1) {
  81. if ($spreadUserInfo['is_promoter']) User::edit(['spread_uid' => $spread_uid], $this->uid, 'uid');
  82. } else {
  83. User::edit(['spread_uid' => $spread_uid], $this->uid, 'uid');
  84. }
  85. }
  86. if (!isset($this->userInfo['uid'])) $this->userInfo['uid'] = 0;
  87. if (!isset($this->userInfo['is_promoter'])) $this->userInfo['is_promoter'] = 0;
  88. if (!isset($this->userInfo['avatar'])) $this->userInfo['avatar'] = '';
  89. if (!isset($this->userInfo['nickname'])) $this->userInfo['nickname'] = '';
  90. //是否关注公众号
  91. $subscribe = WechatUser::where('uid', $this->uid)->value('subscribe');
  92. if (!$NoWechantVisitWhite) {
  93. if (!$this->userInfo || !isset($this->uid)) return $this->failed('读取用户信息失败!');
  94. if (!$this->userInfo['status']) return $this->failed('已被禁止登陆!');
  95. }
  96. }
  97. } catch (\Exception $e) {
  98. Session::clear('wap');
  99. Cookie::delete('is_login');
  100. Cookie::delete('__login_phone');
  101. $url = $this->request->url(true);
  102. if (!$NoWechantVisitWhite) {
  103. if ($this->request->isAjax())
  104. return JsonService::fail('请登录再进行访问');
  105. else
  106. return $this->redirect(Url::build('Login/index', ['spread_uid' => $spread_uid]) . '?ref=' . base64_encode(htmlspecialchars($url)));
  107. }
  108. }
  109. if (Cache::has('__SYSTEM__')) {
  110. $overallShareWechat = Cache::get('__SYSTEM__');
  111. } else {
  112. $overallShareWechat = SystemConfigService::more(['wechat_share_img', 'wechat_share_title', 'wechat_share_synopsis']);
  113. Cache::set('__SYSTEM__', $overallShareWechat, 800);
  114. }
  115. $codeUrl = SystemConfigService::get('wechat_qrcode');
  116. $balance_switch = SystemConfigService::get('balance_switch');//余额开关
  117. $alipay_switch = SystemConfigService::get('alipay_switch');//支付宝开关
  118. $h5_wechat_payment_switch = SystemConfigService::get('h5_wechat_payment_switch');//h5端微信支付开关
  119. $official_account_switch = SystemConfigService::get('official_account_switch');//关注公众号开关
  120. $this->force_binding = SystemConfigService::get('force_binding');//微信端是否强制绑定手机号
  121. $share_display_switch = SystemConfigService::get('share_display_switch');//分享显示开关
  122. $now_money = isset($this->userInfo['now_money']) ? $this->userInfo['now_money'] : 0;
  123. $this->assign([
  124. 'callback_url' => $site_url . '/wap/callback/pay_success_synchro',
  125. 'code_url' => $codeUrl,
  126. 'is_yue' => $balance_switch,
  127. 'is_alipay' => $alipay_switch,
  128. 'is_h5_wechat_payment_switch' => $h5_wechat_payment_switch,
  129. 'is_official_account_switch' => $official_account_switch,
  130. 'is_share_display_switch' => $share_display_switch,
  131. 'subscribe' => $subscribe,
  132. 'subscribeQrcode' => SystemConfigService::get('wechat_qrcode'),
  133. 'userInfo' => $this->userInfo,
  134. 'uid' => isset($this->userInfo['uid']) ? $this->userInfo['uid'] : 0,
  135. 'business' => isset($this->userInfo['business']) ? $this->userInfo['business'] : 0,//是否是讲师
  136. 'now_money' => $now_money,
  137. 'phone' => $this->phone,
  138. 'isWechat' => $this->isWechat,
  139. 'overallShareWechat' => json_encode($overallShareWechat),
  140. 'Auth_site_name' => SystemConfigService::get('site_name'),
  141. 'menus' => GroupDataService::getData('bottom_navigation')
  142. ]);
  143. }
  144. /**
  145. * 检查白名单控制器方法 存在带名单返回 true 不存在则进行登录
  146. * @return bool
  147. */
  148. protected function NoWechantVisitWhite()
  149. {
  150. if ($this->isWechat) return false;
  151. list($module, $controller, $action, $className) = $this->getCurrentController();
  152. if (method_exists($className, 'WhiteList')) {
  153. $whitelist = $className::WhiteList();
  154. if (!is_array($whitelist)) return false;
  155. foreach ($whitelist as $item) {
  156. if (strtolower($module . '\\' . $controller . '\\' . $item) == strtolower($module . '\\' . $controller . '\\' . $action)) {
  157. return true;
  158. }
  159. }
  160. }
  161. return false;
  162. }
  163. /**
  164. * 获取当前的控制器名,模块名,方法名,类名并返回
  165. * @return array
  166. */
  167. protected function getCurrentController()
  168. {
  169. $module = $this->request->module();
  170. $controller = $this->request->controller();
  171. $action = $this->request->action();
  172. if (strstr($controller, '.'))
  173. $controllerv1 = str_replace('.', '\\', $controller);
  174. else
  175. $controllerv1 = $controller;
  176. $className = 'app\\' . $module . '\\controller\\' . $controllerv1;
  177. return [$module, $controller, $action, $className];
  178. }
  179. }