WapBasic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 basic;
  12. use app\wap\model\user\User;
  13. use app\wap\model\user\WechatUser;
  14. use behavior\wap\WapBehavior;
  15. use service\JsonService;
  16. use think\Controller;
  17. use behavior\wechat\UserBehavior;
  18. use service\HookService;
  19. use service\UtilService;
  20. use service\WechatService;
  21. use service\WechatSubscribe;
  22. use think\Cookie;
  23. use think\Request;
  24. use think\Session;
  25. use think\Url;
  26. class WapBasic extends Controller
  27. {
  28. protected function _initialize()
  29. {
  30. parent::_initialize(); // TODO: Change the autogenerated stub
  31. HookService::listen('wap_init',null,null,false,WapBehavior::class);
  32. }
  33. /**
  34. * 操作失败 弹窗提示
  35. * @param string $msg
  36. * @param int $url
  37. * @param string $title
  38. */
  39. protected function failed($msg = '操作失败', $url = 0, $title='信息提示')
  40. {
  41. if($this->request->isAjax()){
  42. exit(JsonService::fail($msg,$url)->getContent());
  43. }else {
  44. $this->assign(compact('title', 'msg', 'url'));
  45. exit($this->fetch('public/error'));
  46. }
  47. }
  48. /**
  49. * 操作成功 弹窗提示
  50. * @param $msg
  51. * @param int $url
  52. */
  53. protected function successful($msg = '操作成功', $url = 0, $title='成功提醒')
  54. {
  55. if($this->request->isAjax()){
  56. exit(JsonService::successful($msg,$url)->getContent());
  57. }else {
  58. $this->assign(compact('title', 'msg', 'url'));
  59. exit($this->fetch('public/success'));
  60. }
  61. }
  62. public function _empty($name)
  63. {
  64. $url = strtolower($name) == 'index' ? Url::build('Index/index','',true,true) : 0;
  65. return $this->failed('请求页面不存在!',$url);
  66. }
  67. /**判断redis状态
  68. * @param $msg
  69. */
  70. public function serRedisPwd($msg){
  71. if(strpos($msg,'username-password pair') !==false){
  72. return $this->failed('请检查redis密码!','http://help.crmeb.net/crmeb_zsff/2291680');
  73. }elseif (strpos($msg,'Connection refused') !==false){
  74. return $this->failed('请安装redis软件或连接被拒绝!');
  75. }elseif (strpos($msg,'NOAUTH Authentication required') !==false){
  76. return $this->failed('redis认证错误!','http://help.crmeb.net/crmeb_zsff/2000690');
  77. }elseif (strpos($msg,'not support: redis') !==false){
  78. return $this->failed('请安装redis扩展!','http://help.crmeb.net/crmeb_zsff/1863590');
  79. }else{
  80. return $this->failed('请设置redis密码!','http://help.crmeb.net/crmeb_zsff/1907223');
  81. }
  82. }
  83. /**
  84. * 微信用户自动登陆
  85. * @return string $openid
  86. */
  87. protected function oauth($spread_uid=0)
  88. {
  89. $openid = Session::get('loginOpenid','wap');
  90. if($openid) return $openid;
  91. if(!UtilService::isWechatBrowser()) exit($this->failed('请在微信客户端打开链接'));
  92. if($this->request->isAjax()) exit($this->failed('请登陆!'));
  93. $errorNum = (int)Cookie::get('_oen');
  94. if($errorNum && $errorNum > 3) exit($this->failed('微信用户信息获取失败!!'));
  95. try{
  96. $original=WechatService::oauthService()->getAccessToken($this->request->get('code'));
  97. if (!WechatUser::be(['openid' => $original->openid]) && $original->scope === 'snsapi_base'){
  98. exit(WechatService::oauthService()->scopes(['snsapi_userinfo'])
  99. ->redirect($this->request->url(true))->send());
  100. }
  101. $wechatInfo=WechatSubscribe::baseParseGet($original->access_token,$original->openid);//获取单个用户信息
  102. if(isset($wechatInfo['errcode']) && $wechatInfo['errcode']==48001){
  103. exit(WechatService::oauthService()->scopes(['snsapi_userinfo'])
  104. ->redirect($this->request->url(true))->send());
  105. }
  106. }catch (\Exception $e){
  107. Cookie::set('_oen',++$errorNum,900);
  108. exit(WechatService::oauthService()->scopes(['snsapi_base'])
  109. ->redirect($this->request->url(true))->send());
  110. }
  111. if(!isset($wechatInfo['nickname']) || $wechatInfo['nickname']==''){
  112. exit(WechatService::oauthService()->scopes(['snsapi_userinfo'])
  113. ->redirect($this->request->url(true))->send());
  114. }
  115. if(isset($wechatInfo['openid']) && $wechatInfo['openid']){
  116. $wechatInfoData = WechatService::getUserInfo($wechatInfo['openid']);
  117. $wechatInfo['subscribe'] =$wechatInfoData['subscribe'];
  118. $wechatInfo['subscribe_time'] =$wechatInfoData['subscribe_time'];
  119. $wechatInfo['groupid'] =$wechatInfoData['groupid'];
  120. if(isset($wechatInfoData['tagid_list'])) $wechatInfo['tagid_list'] = implode(',',$wechatInfoData['tagid_list']);
  121. }else{
  122. if(isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  123. if (!WechatUser::be(['openid' => $wechatInfo['openid']])) $wechatInfo['subscribe'] = 0;
  124. }
  125. Cookie::delete('_oen');
  126. $openid = $wechatInfo['openid'];
  127. $wechatInfo['spread_uid']=$spread_uid;
  128. HookService::afterListen('wechat_oauth',$openid,$wechatInfo,false,UserBehavior::class);
  129. Session::set('loginOpenid',$openid,'wap');
  130. Cookie::set('is_login',1);
  131. return $openid;
  132. }
  133. }