AuthController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 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. * 静默授权
  32. * @param $code
  33. * @param string $spread_code
  34. * @param string $spread_spid
  35. * @return mixed
  36. * @throws \Psr\SimpleCache\InvalidArgumentException
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function silenceAuth($code, $spread_code = '', $spread_spid = '')
  41. {
  42. $token = $this->services->silenceAuth($code, $spread_code, $spread_spid);
  43. if ($token && isset($token['key'])) {
  44. return app('json')->success(410022, $token);
  45. } else if ($token) {
  46. return app('json')->success(410001, ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'new_user' => $token['new_user']]);
  47. } else
  48. return app('json')->fail(410019);
  49. }
  50. /**
  51. * 授权获取小程序用户手机号 直接绑定
  52. * @param string $code
  53. * @param string $iv
  54. * @param string $encryptedData
  55. * @param string $spread_code
  56. * @param string $spread_spid
  57. * @param string $key
  58. * @return mixed
  59. * @throws \Psr\SimpleCache\InvalidArgumentException
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function authBindingPhone($code = '', $iv = '', $encryptedData = '', $spread_code = '', $spread_spid = '', $key = '')
  64. {
  65. if (!$code || !$iv || !$encryptedData)
  66. return app('json')->fail(100100);
  67. $token = $this->services->authBindingPhone($code, $iv, $encryptedData, $spread_code, $spread_spid, $key);
  68. if ($token) {
  69. return app('json')->success(410001, $token);
  70. } else
  71. return app('json')->fail(410019);
  72. }
  73. /** 以下方法该版本暂未使用 */
  74. /**
  75. * 小程序授权登录
  76. * @param Request $request
  77. * @return mixed
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function auth(Request $request)
  82. {
  83. [$code, $spid, $spread, $iv, $encryptedData] = $request->postMore([
  84. ['code', ''],
  85. ['spread_spid', 0],
  86. ['spread_code', ''],
  87. ['iv', ''],
  88. ['encryptedData', ''],
  89. ], true);
  90. $token = $this->services->newAuth($code, $spid, $spread, $iv, $encryptedData);
  91. if ($token) {
  92. if (isset($token['key']) && $token['key']) {
  93. return app('json')->success(410022, $token);
  94. } else {
  95. return app('json')->success(410001, ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  96. }
  97. } else
  98. return app('json')->fail(410019);
  99. }
  100. /**
  101. * 静默授权 不登录
  102. * @param $code
  103. * @param string $spread_code
  104. * @param string $spread_spid
  105. * @return mixed
  106. * @throws \Psr\SimpleCache\InvalidArgumentException
  107. */
  108. public function silenceAuthNoLogin($code, $spread_code = '', $spread_spid = '')
  109. {
  110. $token = $this->services->silenceAuthNoLogin($code, $spread_code, $spread_spid);
  111. if ($token && isset($token['auth_login'])) {
  112. return app('json')->success(410023);
  113. } else if ($token) {
  114. return app('json')->success(410001, ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  115. } else
  116. return app('json')->fail(410019);
  117. }
  118. /**
  119. * 静默授权
  120. * @param string $code
  121. * @param string $spread_code
  122. * @param string $spread_spid
  123. * @param string $phone
  124. * @param string $captcha
  125. * @return mixed
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function silenceAuthBindingPhone($code = '', $spread_code = '', $spread_spid = '', $phone = '', $captcha = '')
  130. {
  131. //验证验证码
  132. $verifyCode = CacheService::get('code_' . $phone);
  133. if (!$verifyCode)
  134. return app('json')->fail(410009);
  135. $verifyCode = substr($verifyCode, 0, 6);
  136. if ($verifyCode != $captcha) {
  137. CacheService::delete('code_' . $phone);
  138. return app('json')->fail(410010);
  139. }
  140. CacheService::delete('code_' . $phone);
  141. $token = $this->services->silenceAuthBindingPhone($code, $spread_code, $spread_spid, $phone);
  142. if ($token) {
  143. return app('json')->success(410001, ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'new_user' => $token['new_user']]);
  144. } else
  145. return app('json')->fail(410019);
  146. }
  147. /**
  148. * 更新用户信息
  149. * @param Request $request
  150. * @param $userInfo
  151. * @return mixed
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\DbException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. */
  156. public function updateInfo(Request $request, $userInfo)
  157. {
  158. if (!$userInfo) {
  159. return app('json')->fail(100100);
  160. }
  161. $uid = (int)$request->uid();
  162. $re = $this->services->updateUserInfo($uid, $userInfo);
  163. if ($re) {
  164. return app('json')->success(100012);
  165. } else
  166. return app('json')->fail(100013);
  167. }
  168. }