[积分商城]优化对接API

This commit is contained in:
2026-03-30 11:47:32 +08:00
parent b30ef21780
commit 4a42899bfe
55 changed files with 835 additions and 1241 deletions

View File

@@ -45,8 +45,8 @@ class MallAddress extends Model
return $cityNames ? implode(',', $cityNames) : '';
}
public function mallUser(): \think\model\relation\BelongsTo
public function playxUserAsset(): \think\model\relation\BelongsTo
{
return $this->belongsTo(\app\common\model\MallUser::class, 'mall_user_id', 'id');
return $this->belongsTo(\app\common\model\MallPlayxUserAsset::class, 'playx_user_asset_id', 'id');
}
}

View File

@@ -19,8 +19,8 @@ class MallPintsOrder extends Model
protected $autoWriteTimestamp = true;
public function mallUser(): \think\model\relation\BelongsTo
public function playxUserAsset(): \think\model\relation\BelongsTo
{
return $this->belongsTo(\app\common\model\MallUser::class, 'mall_user_id', 'id');
return $this->belongsTo(\app\common\model\MallPlayxUserAsset::class, 'playx_user_asset_id', 'id');
}
}

View File

@@ -4,10 +4,11 @@ declare(strict_types=1);
namespace app\common\model;
use ba\Random;
use support\think\Model;
/**
* PlayX 用户资产
* PlayX 用户资产(积分商城用户主表,含登录账号字段)
*/
class MallPlayxUserAsset extends Model
{
@@ -16,11 +17,72 @@ class MallPlayxUserAsset extends Model
protected bool $autoWriteTimestamp = true;
protected array $type = [
'create_time' => 'integer',
'update_time' => 'integer',
'locked_points' => 'integer',
'available_points' => 'integer',
'today_limit' => 'integer',
'today_claimed' => 'integer',
'create_time' => 'integer',
'update_time' => 'integer',
'locked_points' => 'integer',
'available_points' => 'integer',
'today_limit' => 'integer',
'today_claimed' => 'integer',
'admin_id' => 'integer',
];
/**
* H5 临时登录按用户名查找或创建资产行playx_user_id 使用 mall_{id}
*/
public static function ensureForUsername(string $username): self
{
$username = trim($username);
$existing = self::where('username', $username)->find();
if ($existing) {
return $existing;
}
$phone = self::allocateUniquePhone();
if ($phone === null) {
throw new \RuntimeException('Failed to allocate unique phone');
}
$pwd = hash_password(Random::build('alnum', 16));
$now = time();
$temporaryPlayxId = 'tmp_' . bin2hex(random_bytes(16));
$created = self::create([
'playx_user_id' => $temporaryPlayxId,
'username' => $username,
'phone' => $phone,
'password' => $pwd,
'admin_id' => 0,
'locked_points' => 0,
'available_points' => 0,
'today_limit' => 0,
'today_claimed' => 0,
'today_limit_date' => null,
'create_time' => $now,
'update_time' => $now,
]);
if (!$created) {
throw new \RuntimeException('Failed to create mall_playx_user_asset');
}
$id = intval($created->getKey());
$finalPlayxId = 'mall_' . $id;
if (self::where('playx_user_id', $finalPlayxId)->where('id', '<>', $id)->find()) {
$finalPlayxId = 'mall_' . $id . '_' . bin2hex(random_bytes(4));
}
$created->playx_user_id = $finalPlayxId;
$created->save();
return $created;
}
private static function allocateUniquePhone(): ?string
{
for ($i = 0; $i < 8; $i++) {
$candidate = '13' . str_pad(strval(mt_rand(0, 999999999)), 9, '0', STR_PAD_LEFT);
if (!self::where('phone', $candidate)->find()) {
return $candidate;
}
}
return null;
}
}

View File

@@ -19,9 +19,9 @@ class MallRedemptionOrder extends Model
protected $autoWriteTimestamp = true;
public function mallUser(): \think\model\relation\BelongsTo
public function playxUserAsset(): \think\model\relation\BelongsTo
{
return $this->belongsTo(\app\common\model\MallUser::class, 'mall_user_id', 'id');
return $this->belongsTo(\app\common\model\MallPlayxUserAsset::class, 'playx_user_asset_id', 'id');
}
public function mallItem(): \think\model\relation\BelongsTo

View File

@@ -1,31 +0,0 @@
<?php
namespace app\common\model;
use app\common\model\traits\TimestampInteger;
use support\think\Model;
/**
* 商城用户模型
*/
class MallUser extends Model
{
use TimestampInteger;
protected string $name = 'mall_user';
protected bool $autoWriteTimestamp = true;
public function admin(): \think\model\relation\BelongsTo
{
return $this->belongsTo(\app\admin\model\Admin::class, 'admin_id', 'id');
}
/**
* 重置密码(加密存储)
*/
public function resetPassword(int $id, string $newPassword): bool
{
return $this->where(['id' => $id])->update(['password' => hash_password($newPassword)]) !== false;
}
}