Commit 75e8e44f by 杨树贤

初步完成好友邀请那部分

parent 2a60fc2a
<?php
namespace App\Http\Controllers;
use App\Http\Filters\InviteFilter;
use App\Models\Invite;
use App\Models\Integral;
use Illuminate\Http\Request;
/**
* 用户邀请好友管理
* Class InvitesController
* @package App\Http\Controllers
*/
class InvitesController extends Controller
{
/**
* 获取邀请好友的列表
* @param Request $request
* @param Invite $invite
* @param InviteFilter $filter
* @return false|string
*/
public function index(Request $request, Invite $invite, InviteFilter $filter)
{
$page = $request->has('page') ? $request->page : self::DEFAULT_PAGE;
$pageSize = $request->has('page_size') ? $request->page_size : self::DEFAULT_PAGE_SIZE;
$invites = $invite->getInviteList($page, $pageSize, $filter);
return $this->Export(0, 'ok', $invites);
}
/**
* 添加邀请好友记录
* @param Request $request
* @param Invite $invite
* @param Integral $integral
* @return false|string
*/
public function store(Request $request, Invite $invite, Integral $integral)
{
$data = [
'user_id' => $request->get('user_id'),
'invited_user_Id' => $request->get('invited_user_id'),
'add_time' => time(),
];
$canInvite = $integral->checkIntegralLimit($data['user_id'], Integral::INTEGRAL_TYPE_INVITE_FRIEND);
if ($canInvite) {
$res = $invite->addInvite($data);
if ($res) {
return $this->Export(0, 'ok');
} else {
return $this->Export(ErrorCode(19, 5), '新增邀请好友记录失败');
}
}
return $this->Export(0, 'ok');
}
}
\ No newline at end of file
<?php
namespace App\Http\Filters;
class InviteFilter extends QueryFilter
{
public function user_id($userId = 0)
{
return $this->builder->where('user_id', $userId);
}
}
\ No newline at end of file
......@@ -231,7 +231,7 @@ class Integral extends Model
//先去判断对应项存不存在,不存在的话要先去添加对应限制项的redis哈希
$exist = $redis->exists($key);
if ($exist === 0) {
$redis->hset($key, $userId, 1);
$redis->hset($key, 'temp', 1);
//同时给予过期时间(今天晚上12点)
$endTimeOfToday = Carbon::now()->endOfDay()->timestamp;
$redis->expireAt($key, $endTimeOfToday);
......
......@@ -55,12 +55,28 @@ class IntegralBill extends Model
case Integral::INTEGRAL_TYPE_SHARE:
case Integral::INTEGRAL_TYPE_OFFER:
case Integral::INTEGRAL_TYPE_CHECK_IN:
case Integral::INTEGRAL_TYPE_INVITE_FRIEND:
$task = new IntegralBillTask($data);
$result = Task::deliver($task);
return $result ? true : false;
break;
case Integral::INTEGRAL_TYPE_INVITE_FRIEND:
//好友邀请部分,是需要同时添加两个人的流水的
//添加邀请人的红包
$task = new IntegralBillTask($data);
$result = Task::deliver($task);
if ($result) {
//添加受邀人的红包,把user_id改成受邀者的id即可
$data['user_id'] = $data['invited_user_Id'];
$task = new IntegralBillTask($data);
$result = Task::deliver($task);
if ($result) {
return true;
}
}
return false;
break;
}
}
......
<?php
namespace App\Models;
use App\Http\Filters\QueryFilter;
use App\Tasks\IntegralBillTask;
use Common\Model\RedisModel;
use Carbon\Carbon;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class Invite extends Model
{
public $timestamps = false;
public function scopePage($query, $page = 1, $pageSize = 10)
{
return $query->offset(($page - 1) * $pageSize)->limit($pageSize);
}
public function scopeFilter($query, QueryFilter $filters)
{
return $filters->apply($query);
}
//获取签到记录
public function getInviteList($page, $pageSize, $filter)
{
$invites = Invite::filter($filter)->page($page, $pageSize)
->orderBy('id', 'desc')->get()->toArray();
$count = Invite::filter($filter)->count();
return ['data' => $invites, 'count' => $count];
}
//添加签到记录
public function addInvite($data)
{
$result = DB::transaction(function () use ($data) {
$result = DB::table('invites')->insert($data);
if (!$result) {
return false;
}
//使用异步任务去添加流水
$integralBill = new IntegralBill();
$data['integral_id'] = Integral::INTEGRAL_TYPE_INVITE_FRIEND;
$result = $integralBill->createIntegralBill($data);
if (!$result) {
return false;
}
//添加流水成功后,要把签到记录放到redis
$date = Carbon::now()->toDateString();
$hashKey = 'ic_welfare_integral_limit_' . Integral::INTEGRAL_TYPE_INVITE_FRIEND;
$userId = $data['user_id'];
$redis = new RedisModel();
$redis->hincrby($hashKey, $userId, 1);
return true;
});
return $result;
}
}
\ No newline at end of file
......@@ -66,3 +66,7 @@ $router->post('/check_in/add', 'CheckInController@store');
//抢兑换名额
$router->post('/rob/exchange/quota', 'UserExchangesController@create');
//邀请好友
$router->post('/invites/list','InvitesController@index');
$router->post('/invites/add','InvitesController@store');
3271
\ No newline at end of file
14931
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment