request->params([ ['openid', ''], ['login_type', 'member'] ]); $this->validate($data, [ 'openid' => 'require', 'login_type' => 'require' ]); try { $service = new UnifiedLoginService(); $result = $service->wechatLogin($data); return success($result, '微信登录成功'); } catch (CommonException $e) { if ($e->getCode() === 10001) { // 需要绑定的特殊情况 return fail($e->getMessage(), [], 10001); } return fail($e->getMessage()); } catch (\Exception $e) { return fail($e->getMessage()); } } /** * 微信账号绑定 * @return Response */ public function bind() { $data = $this->request->params([ ['mini_openid', ''], ['wechat_openid', ''], ['phone', ''], ['code', ''] ]); $this->validate($data, [ 'mini_openid' => 'require', 'wechat_openid' => 'require', 'phone' => 'require|mobile', 'code' => 'require' ]); try { $service = new UnifiedLoginService(); $result = $service->wechatBind($data); return success($result, '绑定成功'); } catch (\Exception $e) { return fail($e->getMessage()); } } /** * 获取微信公众号授权URL * @return Response */ public function getAuthUrl() { try { $miniOpenid = $this->request->param('mini_openid', ''); if (empty($miniOpenid)) { return fail('小程序openid不能为空'); } $service = new WechatService(); $result = $service->getAuthUrl($miniOpenid); return success($result, '获取授权URL成功'); } catch (\Exception $e) { return fail($e->getMessage()); } } /** * 微信公众号授权回调 * @return Response */ public function callback() { try { $code = $this->request->param('code', ''); $state = $this->request->param('state', ''); if (empty($code)) { return fail('授权失败'); } $service = new WechatService(); $result = $service->handleCallback($code, $state); return success($result, '授权成功'); } catch (\Exception $e) { return fail($e->getMessage()); } } }