diff --git a/app/Constants/Common/UserCode.php b/app/Constants/Common/UserCode.php index 034b16e..34a7829 100644 --- a/app/Constants/Common/UserCode.php +++ b/app/Constants/Common/UserCode.php @@ -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; } \ No newline at end of file diff --git a/app/Controller/Api/OrderController.php b/app/Controller/Api/OrderController.php index abe1051..ff9e1ba 100644 --- a/app/Controller/Api/OrderController.php +++ b/app/Controller/Api/OrderController.php @@ -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(); } } diff --git a/app/Controller/Api/SystemController.php b/app/Controller/Api/SystemController.php index cc397a6..d25fdb1 100644 --- a/app/Controller/Api/SystemController.php +++ b/app/Controller/Api/SystemController.php @@ -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(); } } diff --git a/app/Controller/Api/UserController.php b/app/Controller/Api/UserController.php index 9825c60..2a9e656 100644 --- a/app/Controller/Api/UserController.php +++ b/app/Controller/Api/UserController.php @@ -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(); + } } diff --git a/app/Model/User.php b/app/Model/User.php index f2a787b..b691a54 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -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; + } } diff --git a/app/Model/UserInvite.php b/app/Model/UserInvite.php new file mode 100644 index 0000000..8d31304 --- /dev/null +++ b/app/Model/UserInvite.php @@ -0,0 +1,47 @@ + '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; + } +} diff --git a/app/Service/Api/Login/LoginBaseService.php b/app/Service/Api/Login/LoginBaseService.php index 47a1664..a8cf5e1 100644 --- a/app/Service/Api/Login/LoginBaseService.php +++ b/app/Service/Api/Login/LoginBaseService.php @@ -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(); } \ No newline at end of file diff --git a/app/Service/Api/Login/WxFastLoginService.php b/app/Service/Api/Login/WxFastLoginService.php index d94a0dc..c568040 100644 --- a/app/Service/Api/Login/WxFastLoginService.php +++ b/app/Service/Api/Login/WxFastLoginService.php @@ -77,7 +77,10 @@ class WxFastLoginService extends LoginBaseService $this->addAccount(); - //todo 邀请过来的 邀请方有没有奖励 被邀请方有没有奖励 + $this->addInviteCode(); + + // 邀请过来的 邀请方有没有奖励 被邀请方有没有奖励 + $this->checkInvite(); // 有没有注册奖励 $this->addCoupon(); diff --git a/app/Service/Api/Order/OrderListService.php b/app/Service/Api/Order/OrderListService.php new file mode 100644 index 0000000..ad57e4e --- /dev/null +++ b/app/Service/Api/Order/OrderListService.php @@ -0,0 +1,49 @@ +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) { +// +// } + } +} \ No newline at end of file diff --git a/app/Service/Api/System/CityListService.php b/app/Service/Api/System/CityListService.php index e3b8dc0..d87b46e 100644 --- a/app/Service/Api/System/CityListService.php +++ b/app/Service/Api/System/CityListService.php @@ -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]); } } \ No newline at end of file diff --git a/app/Service/Api/User/InviteListService.php b/app/Service/Api/User/InviteListService.php new file mode 100644 index 0000000..7db43c5 --- /dev/null +++ b/app/Service/Api/User/InviteListService.php @@ -0,0 +1,61 @@ +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']]); + } +} \ No newline at end of file diff --git a/app/Service/ServiceTrait/Common/UserTrait.php b/app/Service/ServiceTrait/Common/UserTrait.php new file mode 100644 index 0000000..98890c1 --- /dev/null +++ b/app/Service/ServiceTrait/Common/UserTrait.php @@ -0,0 +1,32 @@ +