Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
ic_server_welfare
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
df1a04f1
authored
Aug 29, 2019
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
添加兑换红包码的逻辑
parent
0ffc8533
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
141 additions
and
0 deletions
app/Http/Controllers/CodesController.php
app/Models/Code.php
app/Http/Controllers/CodesController.php
0 → 100644
View file @
df1a04f1
<?php
namespace
App\Http\Controllers
;
use
App\Models\Code
;
use
App\Models\Integral
;
use
Illuminate\Http\Request
;
class
CodesController
extends
Controller
{
public
function
store
(
Request
$request
,
Code
$code
)
{
$data
=
[
//兑换的用户Id
'user_id'
=>
$request
->
get
(
'user_id'
),
'add_time'
=>
time
(),
'code'
=>
$request
->
get
(
'code'
),
];
//判断是否可以输入红包码,因为每个人只能输入一次红包码,同时被兑换的人每天只能被兑换固定次数
$canAddCode
=
$code
->
checkCanExchangeCode
(
$data
);
if
(
$canAddCode
)
{
$res
=
$code
->
addCode
(
$data
);
if
(
$res
)
{
return
$this
->
Export
(
0
,
'ok'
);
}
else
{
return
$this
->
Export
(
ErrorCode
(
21
,
5
),
'红包码兑换失败'
);
}
}
return
$this
->
Export
(
0
,
'ok'
);
}
}
\ No newline at end of file
app/Models/Code.php
0 → 100644
View file @
df1a04f1
<?php
namespace
App\Models
;
use
Common\Model\RedisModel
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Support\Facades\DB
;
use
Illuminate\Support\Facades\Log
;
class
Code
extends
Model
{
public
$timestamps
=
false
;
//判断是否可以输入红包码,因为每个人只能输入一次红包码
public
function
checkCanExchangeCode
(
$data
=
[])
{
$userId
=
$data
[
'user_id'
];
$code
=
$data
[
'code'
];
//每个人只能输入一次红包码
//去redis里面去判断是否已经兑换过
$redis
=
new
RedisModel
();
$exist
=
$redis
->
sismember
(
'ic_welfare_code_limit'
,
$userId
);
//如果是存在的话,代表这个人已经输入过红包码,就返回false
if
(
$exist
)
{
return
false
;
}
else
{
//还要去判断这个code的主人今天的被兑换次数是否超过限额
//code只是一个很简单的规则生成的,前面两个字母加上UID,所以从第3位开始取到结尾就是红包持有者的ID
$codeUserId
=
substr
(
$code
,
2
);
$integral
=
new
Integral
();
$canExchangeCode
=
$integral
->
checkIntegralLimit
(
$codeUserId
,
Integral
::
INTEGRAL_TYPE_INTEGRAL_CODE
);
if
(
!
$canExchangeCode
)
{
return
false
;
}
}
return
true
;
}
//添加红包码兑换记录
public
function
addCode
(
$data
)
{
$result
=
DB
::
transaction
(
function
()
use
(
$data
)
{
$userId
=
$data
[
'user_id'
];
$code
=
$data
[
'code'
];
$data
[
'add_time'
]
=
time
();
if
(
empty
(
$userId
)
||
empty
(
$code
))
{
return
false
;
}
$result
=
DB
::
table
(
'codes'
)
->
insert
(
$data
);
if
(
!
$result
)
{
return
false
;
}
//要将红包码更新到ic_data数据库里面的ic_user表里面去
$result
=
DB
::
connection
(
'ic_data'
)
->
table
(
'user'
)
->
where
(
'user_id'
,
$userId
)
->
update
([
'invite_code'
=>
$code
,
'update_time'
=>
time
(),
]);
if
(
!
$result
)
{
throw
new
\Exception
(
'更新红包到ic_data的ic_user表失败'
);
}
//同时要修改redis里面ic_user的用户数据,把红包码放到redis里面去
$redis
=
new
RedisModel
();
$user
=
json_decode
(
$redis
->
hget
(
'ic_user'
,
$userId
),
true
);
$user
[
'invite_code'
]
=
$code
;
$result
=
$redis
->
hset
(
'ic_user'
,
$userId
,
json_encode
(
$user
));
if
(
$result
===
false
)
{
return
false
;
}
//要将此次的兑换人放到限制兑换列表里
//这个列表主要是存放谁兑换过红包码的,因为一个人一辈子只能兑换一次红包码
//不存在的话就放到限制列表里
$redis
=
new
RedisModel
();
$result
=
$redis
->
sadd
(
'ic_welfare_code_limit'
,
$userId
);
if
(
!
$result
)
{
return
false
;
}
//当上面的步骤都没问题的时候
//使用异步任务去添加流水
$integralBill
=
new
IntegralBill
();
$data
[
'integral_id'
]
=
Integral
::
INTEGRAL_TYPE_INTEGRAL_CODE
;
$result
=
$integralBill
->
createIntegralBill
(
$data
);
if
(
!
$result
)
{
return
false
;
}
else
{
$codeUserId
=
substr
(
$code
,
2
);
//两方都会添加红包,所以要走第二次流水
$data
[
'user_id'
]
=
$codeUserId
;
//传入code的拥有者即可对他的流水进行添加
$result
=
$integralBill
->
createIntegralBill
(
$data
);
if
(
!
$result
)
{
return
false
;
}
}
return
true
;
});
return
$result
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment