Files
MBTI_wang/api/app/common/service/ThirdPartyChannelService.php
Ghost 1a66e77ab7 feat: 第三方渠道上下文与开放平台服务接入
1、修复了企业版/个人版在第三方入口下的人脸与测评跳转问题。

2、新增 ThirdPartyChannel/OpenPlatformService 及上下文 thirdPartyContext,补充相关迁移。

3、调整 Analyze/Auth/Test 等接口与小程序入口逻辑。

Made-with: Cursor
2026-04-03 14:18:34 +08:00

114 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\service;
use think\facade\Db;
/**
* 第三方渠道透传userid / phone / tid 解析与 wechat_users 写入(仅存用户表)
*/
class ThirdPartyChannelService
{
public static function parseFromRequestArray(array $input): array
{
$tp = $input['thirdParty'] ?? null;
if (!is_array($tp)) {
return ['userid' => '', 'phone' => '', 'tid' => ''];
}
$userid = isset($tp['userid']) ? trim((string) $tp['userid']) : '';
$phone = isset($tp['phone']) ? trim((string) $tp['phone']) : '';
$tid = isset($tp['tid']) ? trim((string) $tp['tid']) : '';
return [
'userid' => $userid,
'phone' => $phone,
'tid' => $tid,
];
}
public static function normalizePhone(?string $raw): ?string
{
if ($raw === null || $raw === '') {
return null;
}
$digits = preg_replace('/\D+/', '', (string) $raw);
if ($digits === '') {
return null;
}
if (strlen($digits) > 11 && substr($digits, 0, 2) === '86') {
$digits = substr($digits, -11);
}
if (strlen($digits) < 5 || strlen($digits) > 20) {
return null;
}
return $digits;
}
/**
* 按主手机号列查找用户(规范化后比对)
*
* @return array|null 表行
*/
public static function findUserByNormalizedPhone(string $norm): ?array
{
if ($norm === '') {
return null;
}
$row = Db::name('wechat_users')->where('phone', $norm)->find();
if ($row) {
return $row;
}
$candidates = Db::name('wechat_users')
->whereNotNull('phone')
->where('phone', '<>', '')
->where('phone', 'like', '%' . $norm)
->limit(50)
->select()
->toArray();
foreach ($candidates as $r) {
if (self::normalizePhone($r['phone'] ?? '') === $norm) {
return $r;
}
}
return null;
}
/**
* 合并第三方字段到指定用户行(有传则写)
* - 有渠道 phone始终更新 third_party_phone含老用户命中手机号与透传一致时
* - 主字段 phone 为空时:用渠道号码写入 phone新号、未绑手机用户
*/
public static function applyToUserId(int $userId, array $thirdParty): void
{
if ($userId <= 0) {
return;
}
$data = [];
if ($thirdParty['userid'] !== '') {
$data['ext_uid'] = mb_substr($thirdParty['userid'], 0, 191);
}
if ($thirdParty['tid'] !== '') {
$data['third_party_tid'] = mb_substr($thirdParty['tid'], 0, 191);
}
$norm = null;
if ($thirdParty['phone'] !== '') {
$norm = self::normalizePhone($thirdParty['phone']);
}
if ($norm !== null) {
$data['third_party_phone'] = mb_substr($norm, 0, 32);
$row = Db::name('wechat_users')->where('id', $userId)->field('phone')->find();
$mainPhone = trim((string) ($row['phone'] ?? ''));
if ($mainPhone === '') {
$data['phone'] = mb_substr($norm, 0, 20);
}
}
if (empty($data)) {
return;
}
$data['updatedAt'] = time();
Db::name('wechat_users')->where('id', $userId)->update($data);
}
}