Routine.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 app\controller\api\wechat;
  12. use app\common\ApiBaseController;
  13. use app\Request;
  14. use app\services\wechat\RoutineServices;
  15. /**
  16. * 小程序相关
  17. * Class AuthController
  18. * @package app\controller\api\wechat
  19. */
  20. class Routine extends ApiBaseController
  21. {
  22. protected $services = NUll;
  23. /**
  24. * WechatController constructor.
  25. * @param Request $request
  26. * @param RoutineServices $service
  27. */
  28. public function __construct(Request $request, RoutineServices $service)
  29. {
  30. parent::__construct($request);
  31. $this->service = $service;
  32. }
  33. /**
  34. * 返回用户信息的缓存key,返回是否强制绑定手机号
  35. * @param $code
  36. * @param $spread_code
  37. * @param $spread_spid
  38. * @return \think\Response
  39. */
  40. public function authType($code, $spread_code = '', $spread_spid = '')
  41. {
  42. if (!$code) return $this->error('参数有误');
  43. $data = $this->services->authType($code, $spread_spid, $spread_code);
  44. return $this->success($data);
  45. }
  46. /**
  47. * 根据缓存获取token
  48. * @param $key
  49. * @return \think\Response
  50. */
  51. public function authLogin($key)
  52. {
  53. if (!$key) return $this->error('参数有误');
  54. $data = $this->services->authLogin($key);
  55. return $this->success($data);
  56. }
  57. /**
  58. * @param $key
  59. * @param $phone
  60. * @param $captcha
  61. * @param $spread_code
  62. * @param $spread_spid
  63. * @param $code
  64. * @return \think\Response
  65. * @throws \Psr\SimpleCache\InvalidArgumentException
  66. */
  67. public function phoneLogin($key = '', $phone = '', $captcha = '', $spread_code = '', $spread_spid = '', $code = '')
  68. {
  69. //验证验证码
  70. $verifyCode = CacheService::get('code_' . $phone);
  71. if (!$verifyCode)
  72. return $this->error('请先获取验证码');
  73. $verifyCode = substr($verifyCode, 0, 6);
  74. if ($verifyCode != $captcha) {
  75. return $this->error('验证码错误');
  76. }
  77. CacheService::delete('code_' . $phone);
  78. $data = $this->services->phoneLogin($key, $phone, $spread_spid, $spread_code, $code);
  79. return $this->success($data);
  80. }
  81. /**
  82. * 小程序绑定手机号
  83. * @param $code
  84. * @param $iv
  85. * @param $encryptedData
  86. * @return \think\Response
  87. */
  88. public function bindingPhone($code = '', $iv = '', $encryptedData = '')
  89. {
  90. if (!$code || !$iv || !$encryptedData) return $this->error('参数有误');
  91. $this->services->bindingPhone($code, $iv, $encryptedData);
  92. return $this->success(410016);
  93. }
  94. /**
  95. * 小程序授权登录
  96. * @param Request $request
  97. * @return mixed
  98. * @throws \Psr\SimpleCache\InvalidArgumentException
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. * @throws \think\exception\DbException
  102. */
  103. public function auth(Request $request)
  104. {
  105. [$code, $spread_spid, $spread_code, $iv, $encryptedData] = $request->postMore([
  106. ['code', ''],
  107. ['spread_spid', 0],
  108. ['spread_code', ''],
  109. ['iv', ''],
  110. ['encryptedData', ''],
  111. ], true);
  112. $token = $this->services->newAuth($code, $spread_spid, $spread_code, $iv, $encryptedData);
  113. if ($token) {
  114. if (isset($token['key']) && $token['key']) {
  115. return $this->successful('授权成功,请绑定手机号', $token);
  116. } else {
  117. return $this->successful('登录成功!', ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp'], 'store_user_avatar' => $token['store_user_avatar'] ?? 0]);
  118. }
  119. } else
  120. return $this->error('获取用户访问token失败!');
  121. }
  122. /**
  123. * 静默授权
  124. * @param $code
  125. * @param $spread
  126. * @return mixed
  127. */
  128. public function silenceAuth($code, $spread_code = '', $spread_spid = '')
  129. {
  130. $token = $this->services->silenceAuth($code, (int)$spread_code, (int)$spread_spid);
  131. if ($token && isset($token['key'])) {
  132. return $this->success('授权成功,请绑定手机号', $token);
  133. } else if ($token) {
  134. return $this->success('登录成功', ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'store_user_avatar' => $token['store_user_avatar'] ?? 0]);
  135. } else
  136. return $this->error('登录失败');
  137. }
  138. /**
  139. * 静默授权 不登录
  140. * @param $code
  141. * @param $spread
  142. * @return mixed
  143. */
  144. public function silenceAuthNoLogin($code, $spread_code = '', $spread_spid = '')
  145. {
  146. $token = $this->services->silenceAuth($code, (int)$spread_code, (int)$spread_spid, true);
  147. if ($token && isset($token['auth_login'])) {
  148. return $this->success('授权成功');
  149. } else if ($token) {
  150. return $this->success('登录成功', ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp'], 'store_user_avatar' => $token['store_user_avatar'] ?? 0]);
  151. } else
  152. return $this->error('登录失败');
  153. }
  154. /**
  155. * 静默授权
  156. * @param $code
  157. * @param $spread
  158. * @return mixed
  159. */
  160. public function silenceAuthBindingPhone($code = '', $spread_code = '', $spread_spid = '', $phone = '', $captcha = '')
  161. {
  162. //验证验证码
  163. $verifyCode = CacheService::get('code_' . $phone);
  164. if (!$verifyCode)
  165. return $this->error('请先获取验证码');
  166. $verifyCode = substr($verifyCode, 0, 6);
  167. if ($verifyCode != $captcha) {
  168. CacheService::delete('code_' . $phone);
  169. return $this->error('验证码错误');
  170. }
  171. $token = $this->services->silenceAuthBindingPhone($code, $spread_code, $spread_spid, $phone);
  172. if ($token) {
  173. CacheService::delete('code_' . $phone);
  174. return $this->success('登录成功', ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'store_user_avatar' => $token['store_user_avatar'] ?? 0]);
  175. } else
  176. return $this->error('登录失败');
  177. }
  178. /**
  179. * 授权获取小程序用户手机号 直接绑定
  180. * @param $code
  181. * @param $iv
  182. * @param $encryptedData
  183. * @return mixed
  184. */
  185. public function authBindingPhone($code = '', $iv = '', $encryptedData = '', $spread_code = '', $spread_spid = '', $key = '')
  186. {
  187. if (!$code || !$iv || !$encryptedData)
  188. return $this->error('参数有误');
  189. $token = $this->services->authBindingPhone($code, $iv, $encryptedData, $spread_code, $spread_spid, $key);
  190. if ($token) {
  191. return $this->success('登录成功', $token);
  192. } else
  193. return $this->error('登录失败');
  194. }
  195. /**
  196. * 更新用户信息
  197. * @param $userInfo
  198. * @return mixed
  199. */
  200. public function updateInfo(Request $request, $userInfo)
  201. {
  202. if (!$userInfo) {
  203. return $this->error('参数有误');
  204. }
  205. $uid = (int)$request->uid();
  206. $re = $this->services->updateUserInfo($uid, $userInfo);
  207. if ($re) {
  208. return $this->success('更新成功');
  209. } else
  210. return $this->error('更新失败');
  211. }
  212. }