AuthController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v2\wechat;
  12. use app\Request;
  13. use app\services\wechat\RoutineServices;
  14. use crmeb\services\CacheService;
  15. /**
  16. * Class AuthController
  17. * @package app\api\controller\v2\wechat
  18. */
  19. class AuthController
  20. {
  21. protected $services = NUll;
  22. /**
  23. * AuthController constructor.
  24. * @param RoutineServices $services
  25. */
  26. public function __construct(RoutineServices $services)
  27. {
  28. $this->services = $services;
  29. }
  30. /**
  31. * 返回用户信息的缓存key,返回是否强制绑定手机号
  32. * @param $code
  33. * @param string $spread_code
  34. * @param string $spread_spid
  35. * @return \think\Response
  36. * @author: 吴汐
  37. * @email: 442384644@qq.com
  38. * @date: 2023/8/12
  39. */
  40. public function authType($code, $spread_code = '', $spread_spid = '')
  41. {
  42. $data = $this->services->authType($code, $spread_code, $spread_spid);
  43. return app('json')->success($data);
  44. }
  45. /**
  46. * 根据缓存获取token
  47. * @param $key
  48. * @return \think\Response
  49. * @throws \Psr\SimpleCache\InvalidArgumentException
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @author: 吴汐
  54. * @email: 442384644@qq.com
  55. * @date: 2023/8/12
  56. */
  57. public function authLogin($key)
  58. {
  59. $data = $this->services->authLogin($key);
  60. return app('json')->success($data);
  61. }
  62. /**
  63. * 授权获取小程序用户手机号 直接绑定
  64. * @param string $code
  65. * @param string $iv
  66. * @param string $encryptedData
  67. * @param string $spread_code
  68. * @param string $spread_spid
  69. * @param string $key
  70. * @return mixed
  71. * @throws \Psr\SimpleCache\InvalidArgumentException
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function authBindingPhone($code = '', $iv = '', $encryptedData = '', $spread_code = '', $spread_spid = '', $key = '')
  76. {
  77. if (!$code || !$iv || !$encryptedData)
  78. return app('json')->fail(100100);
  79. $data = $this->services->authBindingPhone($code, $iv, $encryptedData, $spread_code, $spread_spid, $key);
  80. if ($data) {
  81. return app('json')->success(410001, $data);
  82. } else
  83. return app('json')->fail(410019);
  84. }
  85. /**
  86. * 小程序手机号登录
  87. * @param string $key
  88. * @param string $phone
  89. * @param string $captcha
  90. * @param string $spread_code
  91. * @param string $spread_spid
  92. * @param string $code
  93. * @return \think\Response
  94. * @throws \Psr\SimpleCache\InvalidArgumentException
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @author: 吴汐
  99. * @email: 442384644@qq.com
  100. * @date: 2023/8/12
  101. */
  102. public function phoneLogin($key = '', $phone = '', $captcha = '', $spread_code = '', $spread_spid = '', $code = '')
  103. {
  104. //验证验证码
  105. $verifyCode = CacheService::get('code_' . $phone);
  106. if (!$verifyCode)
  107. return app('json')->fail(410009);
  108. $verifyCode = substr($verifyCode, 0, 6);
  109. if ($verifyCode != $captcha) {
  110. CacheService::delete('code_' . $phone);
  111. return app('json')->fail(410010);
  112. }
  113. CacheService::delete('code_' . $phone);
  114. $data = $this->services->phoneLogin($key, $phone, $spread_code, $spread_spid, $code);
  115. return app('json')->success($data);
  116. }
  117. /**
  118. * 小程序绑定手机号
  119. * @param string $code
  120. * @param string $iv
  121. * @param string $encryptedData
  122. * @return \think\Response
  123. * @author 吴汐
  124. * @email 442384644@qq.com
  125. * @date 2023/02/24
  126. */
  127. public function bindingPhone($code = '', $iv = '', $encryptedData = '')
  128. {
  129. if (!$code || !$iv || !$encryptedData) return app('json')->fail(100100);
  130. $this->services->bindingPhone($code, $iv, $encryptedData);
  131. return app('json')->success(410016);
  132. }
  133. }