AuthService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\api\login;
  12. use app\dict\common\ChannelDict;
  13. use app\dict\site\SiteDict;
  14. use app\model\member\Member;
  15. use app\Request;
  16. use app\service\api\member\MemberService;
  17. use app\service\core\channel\CoreH5Service;
  18. use app\service\core\channel\CorePcService;
  19. use app\service\core\site\CoreSiteService;
  20. use app\service\core\weapp\CoreWeappAuthService;
  21. use core\base\BaseApiService;
  22. use core\exception\ApiException;
  23. use core\exception\AuthException;
  24. /**
  25. * 登录服务层
  26. * Class AuthService
  27. * @package app\service\api\login
  28. */
  29. class AuthService extends BaseApiService
  30. {
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. $this->model = new Member();
  35. }
  36. public function checkSiteAuth(Request $request)
  37. {
  38. //如果登录信息非法就报错
  39. if ($this->member_id > 0) {
  40. $member_service = new MemberService();
  41. $member_info = $member_service->findMemberInfo([ 'member_id' => $this->member_id ]);
  42. if ($member_info->isEmpty())
  43. throw new AuthException('MEMBER_NOT_EXIST', 401);
  44. }
  45. return true;
  46. }
  47. /**
  48. * 校验渠道
  49. * @param Request $request
  50. * @return void
  51. */
  52. public function checkChannel(Request $request)
  53. {
  54. $channel = $request->getChannel();
  55. switch ($channel) {
  56. case ChannelDict::H5:
  57. $is_open = (int) ( new CoreH5Service() )->getH5(0)[ 'is_open' ];
  58. if (!$is_open) throw new AuthException('SITE_CLOSE_NOT_ALLOW', 402);
  59. break;
  60. case ChannelDict::PC:
  61. $is_open = (int) ( new CorePcService() )->getPc(0)[ 'is_open' ];
  62. if (!$is_open) throw new AuthException('SITE_CLOSE_NOT_ALLOW', 402);
  63. break;
  64. }
  65. }
  66. /**
  67. * 检测站点的合法性
  68. * @param Request $request
  69. * @return true
  70. */
  71. public function checkSite(Request $request)
  72. {
  73. $site_id = $request->apiSiteId();//todo 可以是依赖传值,也可以通过domain域名来获取site_id
  74. $site_info = ( new CoreSiteService() )->getSiteCache($site_id);
  75. if (empty($site_info)) throw new AuthException('SITE_NOT_EXIST', 403);
  76. $rule = strtolower(trim($request->rule()->getRule()));
  77. if ($rule != 'site') {
  78. if ($site_info[ 'status' ] == SiteDict::CLOSE || $site_info[ 'expire_time' ] < time()) throw new AuthException('SITE_CLOSE_NOT_ALLOW', 402);
  79. }
  80. $request->siteId($site_id);
  81. return true;
  82. }
  83. /**
  84. * 绑定手机号
  85. * @param string $mobile
  86. * @param string $mobile_code
  87. * @return array
  88. */
  89. public function bindMobile(string $mobile, string $mobile_code)
  90. {
  91. if (empty($mobile)) {
  92. $result = ( new CoreWeappAuthService() )->getUserPhoneNumber(0, $mobile_code);
  93. if (empty($result)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  94. if ($result[ 'errcode' ] != 0) throw new ApiException($result[ 'errmsg' ]);
  95. $phone_info = $result[ 'phone_info' ];
  96. $mobile = $phone_info[ 'purePhoneNumber' ];
  97. if (empty($mobile)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  98. } else {
  99. //todo 校验手机号验证码
  100. ( new LoginService() )->checkMobileCode($mobile);
  101. }
  102. $member_service = new MemberService();
  103. $member = $member_service->findMemberInfo([ 'member_id' => $this->member_id ]);
  104. if ($member->isEmpty()) throw new AuthException('MEMBER_NOT_EXIST');
  105. $o_mobile = $member[ 'mobile' ];//原始手机号
  106. if (!empty($o_mobile) && $o_mobile == $mobile) throw new AuthException('MOBILE_NOT_CHANGE');
  107. $mobile_member = $member_service->findMemberInfo([ 'mobile' => $mobile ]);
  108. if (!$mobile_member->isEmpty()) throw new AuthException('MOBILE_IS_EXIST');
  109. // if(empty($mobile)) throw new AuthException('MOBILE_NEEDED');//必须填写
  110. $member->save([
  111. 'mobile' => $mobile
  112. ]);
  113. return [
  114. 'mobile' => $mobile
  115. ];
  116. }
  117. /**
  118. * 获取手机号
  119. * @param string $mobile_code
  120. * @return array
  121. */
  122. public function getMobile(string $mobile_code)
  123. {
  124. $result = ( new CoreWeappAuthService() )->getUserPhoneNumber($this->site_id, $mobile_code);
  125. if (empty($result)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  126. if ($result[ 'errcode' ] != 0) throw new ApiException($result[ 'errmsg' ]);
  127. $phone_info = $result[ 'phone_info' ];
  128. $mobile = $phone_info[ 'purePhoneNumber' ];
  129. if (empty($mobile)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  130. $member_service = new MemberService();
  131. $mobile_member = $member_service->findMemberInfo([ 'mobile' => $mobile, 'site_id' => $this->site_id ]);
  132. if (!$mobile_member->isEmpty()) throw new AuthException('MOBILE_IS_EXIST');
  133. return [
  134. 'mobile' => $mobile
  135. ];
  136. }
  137. }