feat : user

This commit is contained in:
2025-03-03 16:21:38 +08:00
parent e92a1cd8c0
commit d6f9f348da
12 changed files with 361 additions and 12 deletions

View File

@@ -7,12 +7,24 @@ class UserCode
/**
* 是否绑定手机号 1=是 2=否
*/
const int IS_BIND_PHONE = 1;
const int IS_NOT_BIND_PHONE = 2;
CONST INT IS_BIND_PHONE = 1;
CONST INT IS_NOT_BIND_PHONE = 2;
/**
* 是否默认头像 1=不是 2=是
*/
const int IS_NOT_DEFAULT_AVATAR = 1;
const int IS_DEFAULT_AVATAR = 2;
CONST INT IS_NOT_DEFAULT_AVATAR = 1;
CONST INT IS_DEFAULT_AVATAR = 2;
/**
* 邀请渠道 1=用户 2=管理员
*/
CONST INT INVITE_CHANNEL_USER = 1;
CONST INT INVITE_CHANNEL_ADMIN = 2;
/**
* 邀请状态 1=已注册 2=已下单
*/
CONST INT INVITE_STATUS_REGISTER = 1;
CONST INT INVITE_STATUS_ORDERED = 2;
}

View File

@@ -9,6 +9,7 @@ use App\Middleware\Api\JwtAuthMiddleware;
use App\Service\Api\Order\CancelOrderService;
use App\Service\Api\Order\CheckCartService;
use App\Service\Api\Order\ConfirmationOrderService;
use App\Service\Api\Order\OrderListService;
use App\Service\Api\Order\PlaceOrderService;
use App\Service\Api\Order\RefundOrderService;
use Hyperf\HttpServer\Annotation\Controller;
@@ -86,8 +87,10 @@ class OrderController extends AbstractController
return (new RefundOrderService)->handle();
}
#[RequestMapping(path: 'order_list',methods: 'post')]
#[Scene(scene: 'order_list')]
public function orderList()
{
return (new OrderListService)->handle();
}
}

View File

@@ -6,6 +6,7 @@ namespace App\Controller\Api;
use App\Controller\AbstractController;
use App\Middleware\Api\JwtAuthMiddleware;
use App\Service\Api\System\CityListService;
use App\Service\Api\System\SiteListService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
@@ -23,8 +24,10 @@ class SystemController extends AbstractController
return (new SiteListService)->handle();
}
#[RequestMapping(path: 'city',methods: 'GET')]
#[Scene(scene: 'city')]
public function getCityList()
{
return (new SiteListService)->handle();
return (new CityListService)->handle();
}
}

View File

@@ -8,6 +8,7 @@ use App\Controller\AbstractController;
use App\Middleware\Api\JwtAuthMiddleware;
use App\Request\Api\UserRequest;
use App\Service\Api\User\BindPhoneByWxService;
use App\Service\Api\User\InviteListService;
use App\Service\Api\User\SiteService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
@@ -68,4 +69,14 @@ class UserController extends AbstractController
{
return (new SiteService)->del();
}
/**
* @return array
*/
#[RequestMapping(path: 'invite/list',methods: 'GET')]
#[Scene(scene: 'invite_list')]
public function inviteList()
{
return (new InviteListService)->handle();
}
}

View File

@@ -21,7 +21,8 @@ use Hyperf\DbConnection\Model\Model;
* @property string $token
* @property string $last_ip
* @property string $reg_ip
* @property string $last_time
* @property string $last_time
* @property string $invite_code
* @property string $create_time
* @property string $update_time
* @property int $is_del
@@ -56,4 +57,37 @@ class User extends Model
{
return $this->where('id', $id)->first();
}
/**
* @param array $ids
* @return array
*/
public function getInfoByIds(array $ids): array
{
$data = $this->whereIn('id', $ids)->get();
if ($data->isEmpty()) return [];
$data = $data->toArray();
return array_column($data,null,'id');
}
/**
* @param string $code
* @return int
*/
public function checkInviteCodeIsUse(string $code): int
{
return $this->where('invite_code', $code)->count() ?? 0;
}
/**
* @param string $code
* @return int
*/
public function getUserIdByInviteCode(string $code): int
{
return $this->where('invite_code', $code)->value('id') ?? 0;
}
}

47
app/Model/UserInvite.php Normal file
View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property int $user_id
* @property int $invitee_user_id
* @property int $status
* @property int $channel
* @property string $create_time
* @property string $update_time
*/
class UserInvite extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'user_invite';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'user_id' => 'integer', 'invitee_user_id' => 'integer', 'status' => 'integer', 'channel' => 'integer'];
const string CREATED_AT = 'create_time';
const string UPDATED_AT = 'update_time';
/**
* @param int $userId
* @return int
*/
public function checkIsInvite(int $userId): int
{
return $this->where('user_id',$userId)->count() ?? 0;
}
}

View File

@@ -24,16 +24,20 @@ use App\Model\CouponTemplate;
use App\Model\User;
use App\Model\UserAccount;
use App\Model\UserCoupon;
use App\Model\UserInvite;
use App\Model\UserThird;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Api\GetUserInfoTrait;
use App\Service\ServiceTrait\Common\UserTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Random\RandomException;
abstract class LoginBaseService extends BaseService
{
use GetUserInfoTrait;
use GetUserInfoTrait,
UserTrait;
/**
* 注入缓存类
@@ -62,6 +66,12 @@ abstract class LoginBaseService extends BaseService
#[Inject]
protected ConfigCache $configCache;
/**
* @var UserInvite $userInviteModel
*/
#[Inject]
protected UserInvite $userInviteModel;
/**
* 锁定注册
@@ -240,5 +250,41 @@ abstract class LoginBaseService extends BaseService
$this->isNewcomerCouponPopUp = 1;
}
/**
* @return void
* @throws RandomException
*/
protected function addInviteCode(): void
{
$inviteCode = $this->generateStableCode($this->userId);
if ($this->userModel->checkInviteCodeIsUse($inviteCode) == 1) return;
if (!$this->userModel->where('id', $inviteCode)->update(['invite_code' => $inviteCode])) throw new ErrException('注册失败-00004');
}
/**
* @return void
*/
protected function checkInvite(): void
{
if (!$this->request->input('invite_code')) return;
$userId = $this->userModel->getUserIdByInviteCode($this->request->input('invite_code'));
if (empty($userId)) return;
if ($this->userInviteModel->checkIsInvite($this->userId) == 1) return;
$userInviteModel = new UserInvite();
$userInviteModel->user_id = $this->userId;
$userInviteModel->invitee_user_id = $userId;
$userInviteModel->channel = UserCode::INVITE_CHANNEL_USER; //todo 预先普通用户渠道
$userInviteModel->status = UserCode::INVITE_STATUS_REGISTER;
if (!$userInviteModel->save()) throw new ErrException('注册失败-00005');
}
abstract protected function register();
}

View File

@@ -77,7 +77,10 @@ class WxFastLoginService extends LoginBaseService
$this->addAccount();
//todo 邀请过来的 邀请方有没有奖励 被邀请方有没有奖励
$this->addInviteCode();
// 邀请过来的 邀请方有没有奖励 被邀请方有没有奖励
$this->checkInvite();
// 有没有注册奖励
$this->addCoupon();

View File

@@ -0,0 +1,49 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Order;
use App\Model\Order;
use App\Service\Api\BaseService;
use Hyperf\Di\Annotation\Inject;
class OrderListService extends BaseService
{
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
public function handle()
{
$limit = $this->request->input('limit', 20);
$orderList = $this->orderModel
->where('user_id', $this->userId)
->when($this->request->input('status'), function ($query) {
$query->where('status', $this->request->input('status'));
})
->orderByDesc('id')
->paginate($limit)
->toArray();
if (!empty($orderList['data'])) {
$this->buildData($orderList['data']);
}
}
private function buildData(array &$orderList)
{
// foreach ($orderList as &$order) {
//
// }
}
}

View File

@@ -10,10 +10,58 @@ declare(strict_types=1);
namespace App\Service\Api\System;
class CityListService extends
use App\Cache\Redis\Common\CityCache;
use App\Constants\Common\CityCode;
use App\Model\SystemCity;
use App\Model\SystemCityConfig;
use App\Service\Api\BaseService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CityListService extends BaseService
{
public function handle()
/**
* @var SystemCity
*/
#[Inject]
protected SystemCity $systemCityModel;
/**
* @var SystemCityConfig
*/
#[Inject]
protected SystemCityConfig $systemCityConfigModel;
/**
* @var CityCache
*/
#[Inject]
protected CityCache $cityCache;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
//todo Write logic
$allInfo = array_column($this->cityCache->getCityList(),'title','id');
$list = $this->systemCityModel
->where('is_del', CityCode::IS_NOT_DELETE)
->select(['id','title','status','city_id','province_id'])
->get();
if (empty($list)) return $this->return->success('success', ['list' => []]);
$list = $list->toArray();
foreach ($list as &$v) {
$v['city_name'] = $allInfo[$v['city_id']];
$v['province_name'] = $allInfo[$v['province_id']];
}
return $this->return->success('success', ['list' => $list]);
}
}

View File

@@ -0,0 +1,61 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\User;
use App\Model\User;
use App\Model\UserInvite;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Api\GetUserInfoTrait;
use Hyperf\Di\Annotation\Inject;
class InviteListService extends BaseService
{
use GetUserInfoTrait;
/**
* @var UserInvite $userInviteModel
*/
#[Inject]
protected UserInvite $userInviteModel;
/**
* @var User
*/
#[Inject]
protected User $userModel;
/**
* @return array
*/
public function handle(): array
{
$limit = $this->request->input('limit', 20);
$list = $this->userInviteModel
->where('invitee_user_id', $this->userId)
->orderBy('id', 'desc')
->paginate($limit)
->toArray();
if (!empty($list['data'])) {
$userIds = array_column($list['data'], 'user_id');
$userInfoArr = $this->userModel->getInfoByIds($userIds);
foreach ($list['data'] as &$item) {
$item['user_nick_name'] = $userInfoArr[$item['user_id']]['nick_name'];
}
}
$userInfo = $this->getUserInfo($this->userId);
return $this->return->success('success',['list' => $list,'invite_code'=> $userInfo['invite_code']]);
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Service\ServiceTrait\Common;
use Random\RandomException;
trait UserTrait
{
/**
* @param int $userId
* @return string
* @throws RandomException
*/
protected function generateStableCode(int $userId): string
{
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$seed = $userId . random_bytes(16) . microtime(true);
// 使用分段处理避免大数
$hash = hash('sha256', $seed, true);
$code = '';
for ($i=0; $i<8; $i++) {
// 每次取1字节数据0-255
$byte = ord($hash[$i]);
$code .= $chars[$byte % 62];
}
return $code;
}
}