test_lgq/niucloud/addon/carmi/app/service/CarmiService.php
2024-01-25 17:05:15 +08:00

71 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace addon\carmi\app\service;
use core\base\BaseAdminService;
use addon\carmi\app\model\Test;
use think\Response;
class CarmiService extends BaseAdminService
{
public function __construct()
{
parent::__construct();
$this->model = new Test();
}
// 添加
public function add(array $data)
{
$list = [];
for ($x=0; $x<$data['nums']; $x++) {
$item=[
"state"=>0,
"number"=>$this->generateOrderNumber(),
"code"=>$this->generateActivationCode(18),
"type"=>intval($data['type'])
];
array_push($list,$item);
}
return $this->model->saveAll($list);
}
// 获取列表
public function get(array $where = [])
{
$search_model = $this->model->withSearch(['number','create_time','state'],$where)->order('create_time desc');
return $this->pageQuery($search_model);
}
// 删除
public function del(int $id)
{
return $this->model->find($id)->delete();
}
// 生成订单号
function generateOrderNumber() {
$microtime = microtime(true);
$microtimeStr = str_replace('.', '', (string)$microtime); // 移除小数点
$uniqidPart = substr(uniqid(), -8); // 或者 substr(uniqid(), 7, 8) 去除前缀
return date('Ymd') . substr($microtimeStr, -6) . $uniqidPart;
}
// 生成激活码
// 使用 uniqid() 函数结合微秒时间戳获取基础唯一标识符
function generateActivationCode($length = 32) {
$uniqidPart = bin2hex(openssl_random_pseudo_bytes(16)); // 或者使用 uniqid('', true)
// 可选添加额外的安全性如用户ID或其它上下文信息如果适用
// $userId = get_current_user_id(); // 假设这是一个获取当前用户ID的方法
// $contextInfo = hash('sha256', $userId . time());
// $activationBase = $uniqidPart . $contextInfo;
// 直接使用 uniqidPart
$activationBase = $uniqidPart;
// 使用安全的哈希函数进一步处理,例如 SHA-256
$activationCode = hash('sha256', $activationBase);
// 如果需要特定长度的激活码,可以截取哈希值的一部分或者用更短的哈希函数
if ($length > 0 && $length < strlen($activationCode)) {
$activationCode = substr($activationCode, 0, $length);
}
return strtoupper($activationCode); // 返回大写字符串形式的激活码
}
}