文件
xionggeshop/addons/shopro/controller/Share.php

152 行
2.0 KiB
PHP

<?php
namespace addons\shopro\controller;
use addons\shopro\model\Share as ShareModel;
class Share extends Base
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
/**
* 获取分享记录
*
* @return void
*/
public function index()
{
$params = $this->request->get();
$shares = ShareModel::getList($params);
return $this->success('获取成功', $shares);
}
public function add()
{
$spm = $this->request->post('spm');
if (empty($spm) || !$this->auth->isLogin()) {
return;
}
$spmParts = explode('.', $spm);
$shareId = intval($spmParts[0] ?? 0);
if (!$shareId || $shareId === intval($this->auth->id)) {
return;
}
$bound = false;
\think\Db::transaction(function () use ($spm, $shareId, &$bound) {
try {
$shareLog = ShareModel::add($spm);
if ($shareLog) {
\think\Hook::listen('share_after', $shareLog);
$bound = $this->checkUserBound($shareId);
}
} catch (\Exception $e) {
// 分享记录异常时仍尝试绑定
}
if (!$bound) {
$agent = new \addons\shopro\library\commission\Agent($this->auth->id);
if ($agent->bindUserRelation('share', $shareId)) {
$agent->asyncAgentUpgrade($agent->user->id);
$bound = true;
}
}
});
if ($bound) {
$this->success('识别成功');
}
}
/**
* 检查用户是否已绑定指定上级
*/
private function checkUserBound($shareId)
{
$user = \addons\shopro\model\User::where('id', $this->auth->id)->field('id,parent_user_id')->find();
return $user && intval($user->parent_user_id) === intval($shareId);
}
}