AuthController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. @file_put_contents('quanju.txt', $key."-微信手机登录auth\r\n", 8);
  78. if (!$code || !$iv || !$encryptedData)
  79. return app('json')->fail(100100);
  80. $data = $this->services->authBindingPhone($code, $iv, $encryptedData, $spread_code, $spread_spid, $key);
  81. if ($data) {
  82. return app('json')->success(410001, $data);
  83. } else
  84. return app('json')->fail(410019);
  85. }
  86. /**
  87. * 小程序手机号登录
  88. * @param string $key
  89. * @param string $phone
  90. * @param string $captcha
  91. * @param string $spread_code
  92. * @param string $spread_spid
  93. * @param string $code
  94. * @return \think\Response
  95. * @throws \Psr\SimpleCache\InvalidArgumentException
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. * @author: 吴汐
  100. * @email: 442384644@qq.com
  101. * @date: 2023/8/12
  102. */
  103. public function phoneLogin($key = '', $phone = '', $captcha = '', $spread_code = '', $spread_spid = '', $code = '')
  104. {
  105. //验证验证码
  106. $verifyCode = CacheService::get('code_' . $phone);
  107. if (!$verifyCode)
  108. return app('json')->fail(410009);
  109. $verifyCode = substr($verifyCode, 0, 6);
  110. if ($verifyCode != $captcha) {
  111. CacheService::delete('code_' . $phone);
  112. return app('json')->fail(410010);
  113. }
  114. CacheService::delete('code_' . $phone);
  115. $data = $this->services->phoneLogin($key, $phone, $spread_code, 0, $spread_spid, $code);
  116. return app('json')->success($data);
  117. }
  118. /**
  119. * 小程序绑定手机号
  120. * @param string $code
  121. * @param string $iv
  122. * @param string $encryptedData
  123. * @return \think\Response
  124. * @author 吴汐
  125. * @email 442384644@qq.com
  126. * @date 2023/02/24
  127. */
  128. public function bindingPhone($code = '', $iv = '', $encryptedData = '')
  129. {
  130. if (!$code || !$iv || !$encryptedData) return app('json')->fail(100100);
  131. $this->services->bindingPhone($code, $iv, $encryptedData);
  132. return app('json')->success(410016);
  133. }
  134. }