11111
This commit is contained in:
@@ -864,7 +864,7 @@ class ChannelController extends BaseController
|
||||
if ($type === 'h5') {
|
||||
// 生成H5二维码
|
||||
// 获取H5页面URL(需要根据实际项目配置)
|
||||
$h5BaseUrl = Env::get('h5.base_url', $this->request->domain());
|
||||
$h5BaseUrl = Env::get('rpc.H5_FORM_URL', 'https://h5.ckb.quwanzhi.com/#');
|
||||
// 确保URL格式正确(去除末尾斜杠)
|
||||
$h5BaseUrl = rtrim($h5BaseUrl, '/');
|
||||
$h5Url = $h5BaseUrl . '/pages/channel/add?token=' . urlencode($token);
|
||||
@@ -970,6 +970,144 @@ class ChannelController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成渠道登录二维码(H5或小程序码)
|
||||
* 通用登录二维码,不绑定特定渠道,用户扫码后输入手机号和密码登录
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function generateLoginQrCode()
|
||||
{
|
||||
try {
|
||||
// 获取参数
|
||||
$type = $this->request->param('type', 'h5'); // h5 或 miniprogram
|
||||
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
|
||||
// 参数验证
|
||||
if (!in_array($type, ['h5', 'miniprogram'])) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'success' => false,
|
||||
'msg' => '类型参数错误,必须为 h5 或 miniprogram',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
// 生成登录token(只包含公司ID,有效期1小时)
|
||||
// 用户扫码后需要自己输入手机号和密码
|
||||
$tokenData = [
|
||||
'companyId' => $companyId,
|
||||
'expireTime' => time() + 3600 // 1小时后过期
|
||||
];
|
||||
$token = base64_encode(json_encode($tokenData));
|
||||
|
||||
if ($type === 'h5') {
|
||||
// 生成H5登录二维码
|
||||
$h5BaseUrl = Env::get('rpc.H5_FORM_URL', 'https://h5.ckb.quwanzhi.com/#');
|
||||
$h5BaseUrl = rtrim($h5BaseUrl, '/');
|
||||
// H5登录页面路径,需要根据实际项目调整
|
||||
$h5Url = $h5BaseUrl . '/pages/channel/login?token=' . urlencode($token);
|
||||
|
||||
// 生成二维码
|
||||
$qrCode = new QrCode($h5Url);
|
||||
$qrCode->setSize(300);
|
||||
$qrCode->setMargin(10);
|
||||
$qrCode->setWriterByName('png');
|
||||
$qrCode->setEncoding('UTF-8');
|
||||
$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH);
|
||||
|
||||
// 转换为base64
|
||||
$qrCodeBase64 = 'data:image/png;base64,' . base64_encode($qrCode->writeString());
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'success' => true,
|
||||
'msg' => '生成H5登录二维码成功',
|
||||
'data' => [
|
||||
'type' => 'h5',
|
||||
'qrCode' => $qrCodeBase64,
|
||||
'url' => $h5Url
|
||||
]
|
||||
]);
|
||||
|
||||
} else {
|
||||
// 生成小程序登录码
|
||||
try {
|
||||
// 从环境变量获取小程序配置
|
||||
$miniProgramConfig = [
|
||||
'app_id' => Env::get('weChat.appidMiniApp', 'wx789850448e26c91d'),
|
||||
'secret' => Env::get('weChat.secretMiniApp', 'd18f75b3a3623cb40da05648b08365a1'),
|
||||
'response_type' => 'array'
|
||||
];
|
||||
|
||||
$app = Factory::miniProgram($miniProgramConfig);
|
||||
|
||||
// scene参数长度限制为32位,使用token的MD5值
|
||||
$scene = substr(md5($token), 0, 32);
|
||||
|
||||
// 调用接口生成小程序码
|
||||
// 小程序登录页面路径,需要根据实际项目调整
|
||||
$response = $app->app_code->getUnlimit($scene, [
|
||||
'page' => 'pages/channel/login', // 请确保小程序里存在该页面
|
||||
'width' => 430,
|
||||
]);
|
||||
|
||||
// 成功时返回的是 StreamResponse
|
||||
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
|
||||
$img = $response->getBody()->getContents();
|
||||
$imgBase64 = 'data:image/png;base64,' . base64_encode($img);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'success' => true,
|
||||
'msg' => '生成小程序登录码成功',
|
||||
'data' => [
|
||||
'type' => 'miniprogram',
|
||||
'qrCode' => $imgBase64,
|
||||
'scene' => $scene,
|
||||
'token' => $token
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// 如果不是流响应,而是数组(错误信息),则解析错误返回
|
||||
if (is_array($response) && isset($response['errcode']) && $response['errcode'] != 0) {
|
||||
$errMsg = isset($response['errmsg']) ? $response['errmsg'] : '微信接口返回错误';
|
||||
return json([
|
||||
'code' => 500,
|
||||
'success' => false,
|
||||
'msg' => '生成小程序登录码失败:' . $errMsg,
|
||||
'data' => $response
|
||||
]);
|
||||
}
|
||||
|
||||
// 其他未知格式
|
||||
return json([
|
||||
'code' => 500,
|
||||
'success' => false,
|
||||
'msg' => '生成小程序登录码失败:响应格式错误',
|
||||
'data' => $response
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'success' => false,
|
||||
'msg' => '生成小程序登录码失败:' . $e->getMessage(),
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
return json([
|
||||
'code' => $e->getCode() ?: 500,
|
||||
'success' => false,
|
||||
'msg' => '生成登录二维码失败:' . $e->getMessage(),
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫码提交渠道信息(H5和小程序共用)
|
||||
* GET请求:返回预填信息
|
||||
|
||||
Reference in New Issue
Block a user