Commit 0915a5ee by 杨树贤

修复好友助力获取和添加的错误

parent 17900568
......@@ -4,29 +4,59 @@
namespace App\Http\Controllers;
use App\Http\Filters\CheckInFilter;
use App\Http\Filters\AssistFilter;
use App\Models\Assist;
use App\Models\CheckIn;
use App\Models\ExchangeSetting;
use App\Models\Integral;
use App\Models\UserIntegral;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* 好友助力
* Class AssistsController
* @package App\Http\Controllers
*/
class AssistsController extends Controller
{
/**
* 好友助力列表
* @param Request $request
* @param Assist $assist
* @param AssistFilter $filter
* @return array
*/
public function index(Request $request, Assist $assist, AssistFilter $filter)
{
$page = $request->get('page', self::DEFAULT_PAGE);
$pageSize = $request->get('page_size', self::DEFAULT_PAGE_SIZE);
if ($request->has('is_api') && $request->is_api) {
$exchangeId = $request->get('exchange_id');
$result = $assist->getAssistListForApi($exchangeId);
} else {
$result = $assist->getAssistList($page, $pageSize, $filter);
}
return $this->Export(0, 'ok', ['data' => $result]);
}
/**
* 存储好友助力
* @param Request $request
* @param Assist $assist
* @return array
*/
public function store(Request $request, Assist $assist)
{
$exchangeId = $request->get('exchange_id');
$assistUserId = $request->get('assist_user_id');
$data = [
'user_id' => $request->user_id,
'add_time' => time(),
'exchange_id' => $exchangeId,
'user_id' => $request->user_id,
'assist_user_id' => $assistUserId,
'add_time' => time(),
'exchange_id' => $exchangeId,
];
$canAdd = $assist->checkCanAddAssist($exchangeId);
if ($canAdd) {
$res = $assist->addAssist($data);
if ($res) {
//因为最多是两个好友对一条兑换进行助力
$canAssist = $assist->checkCanAssist($exchangeId,$assistUserId);
if ($canAssist) {
$result = $assist->addAssist($data);
if ($result) {
return $this->Export(0, 'ok');
} else {
return $this->Export(ErrorCode(20, 5), '新增好友助力记录失败');
......
<?php
namespace App\Http\Filters;
class AssistFilter extends QueryFilter
{
public function user_id($userId = 0)
{
return $this->builder->where('user_id', $userId);
}
public function exchange_id($exchangeId = 0)
{
return $this->builder->where('exchange_id', $exchangeId);
}
}
\ No newline at end of file
......@@ -27,21 +27,63 @@ class Assist extends Model
return $filters->apply($query);
}
//从数据库里查出助力列表
public function getAssistList($page, $pageSize, $filter)
{
$assists = Assist::filter($filter)
->page($page, $pageSize)
->orderBy('id', 'desc')
->get()->toArray();
$count = Assist::filter($filter)->count();
return ['data' => $assists, 'count' => $count];
}
//这个方法只会返回用户最新一个兑换请求的好友助力情况
//而且每条兑换最多只有两个好友助力,即两条记录
public function getAssistListForApi($exchangeId)
{
$result = DB::table('assists')->where('exchange_id', $exchangeId)->get()->toArray();
return $result;
}
//添加助力记录
public function addAssist($data)
{
$result = DB::transaction(function () use ($data) {
$exchangeId = $data['exchange_id'];
$result = DB::table('assists')->insert($data);
if (!$result) {
return false;
}
//要先去判断当条兑换请求是否已经有两个好友助力
//如果有的话,才进入添加流水的流程
//使用异步任务去添加流水
$integralBill = new IntegralBill();
$data['integral_id'] = Integral::INTEGRAL_TYPE_CHECK_IN;
$result = $integralBill->createIntegralBill($data);
if (!$result) {
//todo:如果有的话,才会去走兑换流程,如果小于20块,自动转账,大于20要人工审核
$result = DB::table('user_exchanges')->where('id', $exchangeId)->increment('assist_count');
if ($result) {
//操作成功后,去redis里面添加数据
$redis = new RedisModel();
$assists = $redis->hget('ic_welfare_assists', $exchangeId);
if ($assists) {
$assists = json_decode($assists, true);
array_push($assists, $data);
} else {
$assists = [$data];
}
$result = $redis->hset('ic_welfare_assists', $exchangeId, json_encode($assists));
if ($result === false) {
return false;
}
//去判断是不是有两个好友助力了
$assistCount = DB::table('user_exchanges')->where('id', $exchangeId)
->value('assist_count');
//如果$assistCount=2,就代表是有两个好友助力,应该去判断是否自动提现
//todo:但是由于目前微信的红包自动转账还没有申请下来,所以目前先不做处理
if ($assistCount == 2) {
//todo:还有一个问题就是提现成功后,要清除对应ic_welfare_assists里面的exchange_id的数据
}
} else {
return false;
}
......@@ -51,14 +93,29 @@ class Assist extends Model
return $result;
}
protected function checkCanAddIntegralBill()
{
//先从redis里面取出对应的好友助攻情况
}
//检查是否可以添加助力记录,目前的是最多两个好友助力
public function checkCanAddAssist($exchangeId)
public function checkCanAssist($exchangeId, $assistUserId)
{
$redis = new RedisModel();
$assists = $redis->hget('ic_welfare_assists', $exchangeId);
if ($assists) {
$assists = json_decode($assists, true);
//判断是否已经有两个好友助力
if (count($assists) === 2) {
return false;
} else {
//还要去判断用户是否重复了,助力要不同的用户才能助力
foreach ($assists as $key => $assist) {
if ($assistUserId == $assist['assist_user_id']) {
return false;
}
}
}
return true;
}
//不存在记录的话就代表可以直接插入
return true;
}
}
\ No newline at end of file
......@@ -222,6 +222,7 @@ class Integral extends Model
}
//检查当前红包添加的操作是否已经达到限额,比如每个用户每日只能签到一次
//红包码每天只能被兑换10次等等操作
public function checkIntegralLimit($userId, $integralId)
{
//先去redis取出对应的判断类型,比如我要判断签到是否达到每日限额
......
......@@ -11,7 +11,14 @@
|
*/
use App\Tasks\IntegralBillTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
$router->get('/', function () use ($router) {
$task = new \App\Tasks\TestTask();
$task->delay(30);
$result = Task::deliver($task);
return $router->app->version();
});
......@@ -67,6 +74,15 @@ $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');
$router->post('/invites/list', 'InvitesController@index');
$router->post('/invites/add', 'InvitesController@store');
$router->post('/invites/info', 'InvitesController@info');
//红包码兑换(添加)
$router->post('/codes/add', 'CodesController@store');
//好友助力
$router->post('/assists/list', 'AssistsController@index');
$router->post('/assists/add', 'AssistsController@store');
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