WapBasic.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 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 think\Cookie;
  22. use think\Request;
  23. use think\Session;
  24. use think\Url;
  25. class WapBasic extends Controller
  26. {
  27. protected function _initialize()
  28. {
  29. parent::_initialize(); // TODO: Change the autogenerated stub
  30. $spreadUid = Request::instance()->route('spuid',0);
  31. if($spreadUid
  32. && ($userInfo = User::getUserInfo(WechatUser::openidToUid($this->oauth())))
  33. && !$userInfo['spread_uid']
  34. && $userInfo['uid'] != $spreadUid
  35. ) User::edit(['spread_uid'=>$spreadUid],$userInfo['uid'],'uid');
  36. HookService::listen('wap_init',null,null,false,WapBehavior::class);
  37. }
  38. /**
  39. * 操作失败 弹窗提示
  40. * @param string $msg
  41. * @param int $url
  42. * @param string $title
  43. */
  44. protected function failed($msg = '操作失败', $url = 0, $title='信息提示')
  45. {
  46. if($this->request->isAjax()){
  47. exit(JsonService::fail($msg,$url)->getContent());
  48. }else {
  49. $this->assign(compact('title', 'msg', 'url'));
  50. exit($this->fetch('public/error'));
  51. }
  52. }
  53. /**
  54. * 操作成功 弹窗提示
  55. * @param $msg
  56. * @param int $url
  57. */
  58. protected function successful($msg = '操作成功', $url = 0, $title='成功提醒')
  59. {
  60. if($this->request->isAjax()){
  61. exit(JsonService::successful($msg,$url)->getContent());
  62. }else {
  63. $this->assign(compact('title', 'msg', 'url'));
  64. exit($this->fetch('public/success'));
  65. }
  66. }
  67. public function _empty($name)
  68. {
  69. $url = strtolower($name) == 'index' ? Url::build('Index/index','',true,true) : 0;
  70. return $this->failed('请求页面不存在!',$url);
  71. }
  72. /**
  73. * 微信用户自动登陆
  74. * @return string $openid
  75. */
  76. protected function oauth($spread_uid=0)
  77. {
  78. $openid = Session::get('loginOpenid','wap');
  79. if($openid) return $openid;
  80. if(!UtilService::isWechatBrowser()) exit($this->failed('请在微信客户端打开链接'));
  81. if($this->request->isAjax()) exit($this->failed('请登陆!'));
  82. $errorNum = (int)Cookie::get('_oen');
  83. if($errorNum && $errorNum > 3) exit($this->failed('微信用户信息获取失败!!'));
  84. try{
  85. $wechatInfo = WechatService::oauthService()->user()->getOriginal();
  86. }catch (\Exception $e){
  87. Cookie::set('_oen',++$errorNum,900);
  88. exit(WechatService::oauthService()->scopes(['snsapi_base'])
  89. ->redirect($this->request->url(true))->send());
  90. }
  91. if(!isset($wechatInfo['nickname'])){
  92. $wechatInfo = WechatService::getUserInfo($wechatInfo['openid']);
  93. if(!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
  94. exit(WechatService::oauthService()->scopes(['snsapi_userinfo'])
  95. ->redirect($this->request->url(true))->send());
  96. if(isset($wechatInfo['tagid_list']))
  97. $wechatInfo['tagid_list'] = implode(',',$wechatInfo['tagid_list']);
  98. }else{
  99. if(isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  100. if (!WechatUser::be(['openid' => $wechatInfo['openid']])) $wechatInfo['subscribe'] = 0;
  101. }
  102. Cookie::delete('_oen');
  103. $openid = $wechatInfo['openid'];
  104. $wechatInfo['spread_uid']=$spread_uid;
  105. HookService::afterListen('wechat_oauth',$openid,$wechatInfo,false,UserBehavior::class);
  106. Session::set('loginOpenid',$openid,'wap');
  107. Cookie::set('is_login',1);
  108. return $openid;
  109. }
  110. }