智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

134 lines
3.6 KiB

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace app\api\controller\login;
use app\service\api\login\UnifiedLoginService;
use app\service\api\login\WechatService;
use core\base\BaseController;
use core\exception\CommonException;
use think\Response;
/**
* 微信登录控制器
*/
class WechatLogin extends BaseController
{
/**
* 微信openid登录
* @return Response
*/
public function login()
{
$data = $this->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());
}
}
}