Files
lgp-admin-plus-api/app/BaseApp/BaseWxService.php

86 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace App\BaseApp;
2026-08-19 08:16:49 +08:00
use App\Models\business\WxUserModel;
use App\Service\common\UtilsService;
use App\Service\wx\WxTokenService;
/**
* 小程序业务基类
*
* BaseService 的区别只在身份来源:后台是 nl_admin小程序是 cc_wx_user。
* 分页与查询能力复用 BaseNotAuthService但不走它的构造那里解的是后台 token
*/
class BaseWxService extends BaseNotAuthService
{
/**
* @var bool 是否要求已登录
*/
protected bool $needLogin = true;
public function __construct()
{
$this->utils = UtilsService::getInstance();
$token = WxTokenService::getInstance();
if ($this->needLogin) {
$this->userInfo = $token->requireUser();
} else {
$this->userInfo = $token->resolveUser(request()->bearerToken()) ?? [];
}
$this->userId = (int) ($this->userInfo['id'] ?? 0);
}
/**
* 当前用户是否可见价格
*/
protected function canSeePrice(): bool
{
return (bool) ($this->userInfo['show_price'] ?? 0);
}
/**
* 当前用户的价格倍率
*/
protected function priceMultiplier(): mixed
{
return $this->userInfo['price_number'] ?? 1;
}
2026-08-19 08:16:49 +08:00
/**
* 打开分享链接时继承代理商倍率
*
* 商品详情、套餐详情共用:只有「还没有上级、自己也不是代理商」的已登录用户会被绑定;
* 已绑同一上级时同步最新倍率。未登录不写库,只影响本次会话价。
*/
protected function inheritAgentPrice(int $shareUserId): void
{
if ($shareUserId <= 0) {
return;
}
$isAgent = (int) ($this->userInfo['is_p'] ?? 0) === 1;
$pid = (int) ($this->userInfo['pid'] ?? 0);
if ($isAgent) {
return;
}
if ($pid !== 0 && $pid !== $shareUserId) {
return;
}
$agent = WxUserModel::where('id', $shareUserId)->where('deleted_at', 0)->first();
if (empty($agent) || (int) $agent['is_p'] !== 1) {
return;
}
$this->userInfo['show_price'] = 1;
$this->userInfo['price_number'] = $agent['price_number'];
if ($this->userId <= 0) {
return;
}
WxUserModel::where('id', $this->userId)->update([
'pid' => $shareUserId,
'show_price' => 1,
'price_number' => $agent['price_number'],
'updated_at' => time(),
]);
}
}