You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
6.0 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\adminapi\logic\business;
use app\common\model\business\Redeems;
use app\common\logic\BaseLogic;
use think\facade\Db;
/**
* Redeems逻辑
* Class RedeemsLogic
* @package app\adminapi\logic\business
*/
class RedeemsLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/03/29 09:40
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
if ($params['type'] == 1) {
if (!$params['code']) {
self::setError('通用礼包,需自定义兑换码');
return false;
}
$exist = Redeems::where('code', $params['code'])->find();
if ($exist) {
self::setError('兑换码已存在,请更换');
return false;
}
} elseif ($params['type'] == 2) {
if (!$params['num'] || !$params['batch']) {
self::setError('单次礼包,需输入生成数量和批次');
return false;
}
if ($params['num'] > 1000) {
self::setError('单次最多只能生成1000个兑换码');
return false;
}
}
if (strtotime($params['endtime']) < strtotime('+1 month')) {
self::setError('兑换截止时间需超过30天');
return false;
}
$rewards = json_decode($params['rewards'], true);
if (!$rewards || !is_array($rewards)) {
self::setError('奖励JSON格式错误请检查');
return false;
}
$uid = 0;
if ($params['type'] == 2 && $params['num'] == 1 && $params['uid']) {
$uid = $params['uid'];
}
$data = [];
if ($params['type'] == 1) {
$data[] = [
'type' => $params['type'],
'code' => $params['code'],
'batch' => $params['batch'],
'endtime' => $params['endtime'],
'rewards' => $params['rewards'],
'createtime' => date('Y-m-d H:i:s')
];
} else {
// 批量生成
while (count($data) < $params['num']) {
$code = self::makeRedeem();
$exist = Redeems::where('code', $code)->find();
if ($exist) {
continue;
}
$data[] = [
'type' => $params['type'],
'code' => $code,
'uid' => $uid,
'batch' => $params['batch'],
'endtime' => $params['endtime'],
'rewards' => $params['rewards'],
'createtime' => date('Y-m-d H:i:s')
];
}
}
Redeems::insertAll($data);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
protected static function makeRedeem($len = 6)
{
$code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$rand = $code[rand(0, 25)]
. strtoupper(dechex(date('m')))
. date('d') . substr(time(), -5)
. substr(microtime(), 2, 5)
. sprintf('%02d', rand(0, 99));
for (
$a = md5($rand, true),
$s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
$d = '',
$f = 0;
$f < $len;
$g = ord($a[$f]),
$d .= $s[($g ^ ord($a[$f + $len])) - $g & 0x1F],
$f++
);
return $d;
}
/**
* @notes 编辑
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/03/29 09:40
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
Redeems::where('id', $params['id'])->update([
'type' => $params['type'],
'code' => $params['code'],
'batch' => $params['batch'],
'endtime' => $params['endtime'],
'rewards' => $params['rewards'],
'uid' => $params['uid'],
'endtime' => $params['endtime']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/03/29 09:40
*/
public static function delete(array $params): bool
{
return Redeems::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/03/29 09:40
*/
public static function detail($params): array
{
return Redeems::findOrEmpty($params['id'])->toArray();
}
}