128 lines
4.0 KiB
PHP
128 lines
4.0 KiB
PHP
<?php
|
||
/*
|
||
* description:
|
||
* author:wh
|
||
* email:
|
||
* createTime:{2024/6/12} {11:53}
|
||
*/
|
||
|
||
namespace app\api\controller;
|
||
|
||
use app\common\model\TabConf;
|
||
use think\Db;
|
||
use wanghua\general_utility_tools_php\Mmodel;
|
||
use wanghua\general_utility_tools_php\SundryConfig;
|
||
use wanghua\general_utility_tools_php\tool\Tools;
|
||
|
||
/**
|
||
* Class Redeemcode 兑换码
|
||
* @package app\api\controller
|
||
*/
|
||
class Redeemcode extends BaseHttpApi
|
||
{
|
||
|
||
/**
|
||
* desc:新增一个兑换码
|
||
* 参数: num (默认1)
|
||
* author:wh
|
||
*/
|
||
function add(){
|
||
return Mmodel::catchJson(function (){
|
||
$num = input('num',1);//生成数量
|
||
for ($i=0;$i<$num;$i++){
|
||
Db::table(TabConf::$fa_redeem_code)
|
||
->insert(['create_time'=>Tools::get_now_date()]);
|
||
return Tools::set_ok();
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 查询一个没使用的兑换码
|
||
*/
|
||
function getOne(){
|
||
return Mmodel::catchJson(function (){
|
||
$res = Db::table(TabConf::$fa_redeem_code)
|
||
->where('is_use',0)
|
||
//->order('id','asc')
|
||
->find();
|
||
if($res){
|
||
return Tools::set_ok($res);
|
||
}
|
||
return Tools::set_fail('没有更多了');
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 兑换码使用
|
||
*
|
||
* 参数:code
|
||
*
|
||
* 返回用户获得的道具列表(可能获得多个道具,根据系统配置定)
|
||
*/
|
||
function use()
|
||
{
|
||
return Mmodel::catchJson(function () {
|
||
$code = input('code');
|
||
if (!$code) {
|
||
return Tools::set_fail('参数错误');
|
||
}
|
||
$res = Db::table(TabConf::$fa_redeem_code)
|
||
->where('id', $code)
|
||
->where('is_use', 0)
|
||
->find();
|
||
if (empty($res)) {
|
||
return Tools::set_fail('兑换码不存在或已使用');
|
||
}
|
||
Db::table(TabConf::$fa_redeem_code)
|
||
->where('id', $code)
|
||
->data(['is_use' => 1])
|
||
->update();
|
||
//计算奖励
|
||
$redeem_code_prize_config = SundryConfig::val('redeem_code_prize_config');
|
||
$prize_arr = explode(',', $redeem_code_prize_config);
|
||
|
||
$prop_arr = [];
|
||
foreach ($prize_arr as $prize_str){
|
||
$prize_id = explode('-',$prize_str)[0];
|
||
$prize_num = explode('-',$prize_str)[1];
|
||
//查询道具
|
||
$prop = Db::table(TabConf::$fa_gameprop)
|
||
->where('id', $prize_id)
|
||
->find();
|
||
if (empty($prop)) {
|
||
continue;
|
||
}
|
||
//查询道具
|
||
$prop_user = Db::table(TabConf::$fa_usergameprop)
|
||
->where('openid', api_user_openid())
|
||
->where('gameprop_id', $prize_id)
|
||
->find();
|
||
if($prop_user){
|
||
//更新
|
||
Db::table(TabConf::$fa_usergameprop)
|
||
->where('openid', api_user_openid())
|
||
->where('gameprop_id', $prize_id)
|
||
->setInc('num', $prize_num);
|
||
$prop_user['num'] = $prop_user['num'] + $prize_num;
|
||
$prop_arr[] = $prop_user;
|
||
}else{
|
||
$data = [
|
||
'openid'=>api_user_openid(),
|
||
'gameproptype_id'=>$prop['gameproptype_id'],
|
||
'name'=>$prop['name'],
|
||
'gameprop_id'=>$prize_id,
|
||
'num'=>$prize_num,
|
||
'image'=>$prop['image'],
|
||
];
|
||
//新增
|
||
$data_id = Db::table(TabConf::$fa_usergameprop)
|
||
->insertGetId($data);
|
||
$data['id'] = $data_id;
|
||
$prop_arr[] = $data;
|
||
}
|
||
}
|
||
return Tools::set_ok('使用成功,返回用户获得的道具.',$prop_arr);
|
||
});
|
||
}
|
||
} |