存客宝应用接口初始化

This commit is contained in:
Ghost
2026-01-05 10:25:32 +08:00
parent fa37227954
commit 02797ba50a
414 changed files with 85530 additions and 75 deletions

View File

@@ -0,0 +1,64 @@
<?php
namespace app\store\model;
use think\Model;
class FlowPackageModel extends Model
{
protected $name = 'flow_package';
// 定义字段自动转换
protected $type = [
// 将特权字段从多行文本转换为数组
'privileges' => 'array',
];
/**
* 特权字段获取器 - 将多行文本转换为数组
* @param $value
* @return array
*/
public function getPrivilegesAttr($value)
{
if (empty($value)) {
return [];
}
// 如果已经是数组则直接返回
if (is_array($value)) {
return $value;
}
// 按行分割文本
return array_filter(explode("\n", $value));
}
/**
* 折扣获取器 - 根据原价和售价计算折扣
* @param $value
* @param $data
* @return string
*/
public function getDiscountAttr($value, $data)
{
if (empty($data['originalPrice']) || $data['originalPrice'] <= 0) {
return '原价';
}
$discount = round(($data['price'] / $data['originalPrice']) * 10, 1);
return $discount . '折';
}
/**
* 总流量获取器 - 计算套餐总流量
* @param $value
* @param $data
* @return int
*/
public function getTotalFlowAttr($value, $data)
{
return isset($data['monthlyFlow']) && isset($data['duration']) ?
intval($data['monthlyFlow']) * intval($data['duration']) : 0;
}
}

View File

@@ -0,0 +1,93 @@
<?php
namespace app\store\model;
use think\Model;
/**
* 流量订单模型
*/
class FlowPackageOrderModel extends Model
{
// 设置表名
protected $name = 'flow_package_order';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 类型转换
protected $type = [
'id' => 'integer',
'userId' => 'integer',
'packageId' => 'integer',
'amount' => 'float',
'duration' => 'integer',
'createTime' => 'timestamp',
'updateTime' => 'timestamp',
'payTime' => 'timestamp',
'status' => 'integer',
'payStatus' => 'integer',
'isDel' => 'integer',
];
/**
* 生成订单号
* 规则LL + 年月日时分秒 + 5位随机数
*
* @return string
*/
public static function generateOrderNo()
{
$prefix = 'LL';
$date = date('YmdHis');
$random = mt_rand(10000, 99999);
return $prefix . $date . $random;
}
/**
* 创建订单
*
* @param int $userId 用户ID
* @param int $packageId 套餐ID
* @param string $packageName 套餐名称
* @param float $amount 订单金额
* @param int $duration 购买时长(月)
* @param string $payType 支付类型 (wechat|alipay|nopay)
* @param string $remark 备注
* @return array|false
*/
public static function createOrder($userId, $packageId, $packageName, $amount, $duration, $payType = 'wechat', $remark = '')
{
// 生成订单号
$orderNo = self::generateOrderNo();
// 订单数据
$data = [
'userId' => $userId,
'packageId' => $packageId,
'packageName' => $packageName,
'orderNo' => $orderNo,
'amount' => $amount,
'duration' => $duration,
'payType' => $payType,
'createTime' => time(),
'status' => 0, // 0:待支付 1:已完成 2:已取消 3:已退款
'payStatus' => $payType == 'nopay' ? 10 : 0, // 0:未支付 1:已支付 10:无需支付
'remark' => $remark,
'isDel' => 0,
];
// 创建订单
$model = new self();
$result = $model->save($data);
if ($result) {
return $model->toArray();
} else {
return false;
}
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace app\store\model;
use think\Model;
class TrafficOrderModel extends Model
{
protected $name = 'traffic_order';
}

View File

@@ -0,0 +1,37 @@
<?php
namespace app\store\model;
use think\Model;
class TrafficPackage extends Model
{
protected $name = 'traffic_package';
// 自动转换特权文本为数组
public function getPrivilegesAttr($value)
{
return empty($value) ? [] : explode("\n", $value);
}
// 计算折扣
public function getDiscountAttr($value, $data)
{
if (empty($data['originalPrice']) || $data['originalPrice'] == 0) {
return '';
}
// 原价和售价相同时返回原价
if ($data['originalPrice'] == $data['price']) {
return '原价';
}
$discount = round(($data['price'] / $data['originalPrice']) * 100);
return $discount . '折';
}
// 计算总流量
public function getTotalTrafficAttr($value, $data)
{
return $data['monthlyTraffic'] * $data['duration'];
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace app\store\model;
use think\Model;
class TrafficPackageOrder extends Model
{
// 自动转换时间为时间戳
protected $type = [
'createTime' => 'timestamp',
'updateTime' => 'timestamp',
'expireTime' => 'timestamp',
];
}

View File

@@ -0,0 +1,14 @@
<?php
namespace app\store\model;
use think\Model;
class TrafficUsageLog extends Model
{
// 自动转换时间为时间戳
protected $type = [
'createTime' => 'timestamp',
'updateTime' => 'timestamp',
];
}

View File

@@ -0,0 +1,103 @@
<?php
namespace app\store\model;
use think\Model;
class UserFlowPackageModel extends Model
{
protected $name = 'user_flow_package';
/**
* 获取用户当前有效的流量套餐
*
* @param int $userId 用户ID
* @return array|null 用户套餐信息
*/
public static function getUserActivePackage($userId)
{
if (empty($userId)) {
return null;
}
return self::where('userId', $userId)
->where('status', 1) // 1表示有效
->where('expireTime', '>', time()) // 未过期
->order('expireTime', 'asc') // 按到期时间排序,最先到期的排在前面
->find();
}
/**
* 创建用户套餐订阅记录
*
* @param int $userId 用户ID
* @param int $packageId 套餐ID
* @param int $duration 套餐时长(月)
* @return bool 是否创建成功
*/
public static function createSubscription($userId, $packageId, $duration = 0)
{
if (empty($userId) || empty($packageId)) {
return false;
}
// 获取套餐信息
$package = FlowPackageModel::where('id', $packageId)->where('isDel', 0)->find();
if (empty($package)) {
return false;
}
// 如果未指定时长,则使用套餐默认时长
if (empty($duration)) {
$duration = $package['duration'];
}
// 计算开始时间和到期时间
$now = time();
$startTime = $now;
$expireTime = strtotime("+{$duration} month", $now);
// 创建新订阅
$data = [
'userId' => $userId,
'packageId' => $packageId,
'duration' => $duration,
'totalFlow' => $package->totalFlow,
'usedFlow' => 0,
'status' => 1, // 1表示有效
'startTime' => $startTime,
'expireTime' => $expireTime,
'createTime' => $now,
'updateTime' => $now
];
return self::create($data) ? true : false;
}
/**
* 更新用户已使用流量
*
* @param int $id 用户套餐ID
* @param int $usedFlow 已使用流量
* @return bool 是否更新成功
*/
public static function updateUsedFlow($id, $usedFlow)
{
if (empty($id)) {
return false;
}
$userPackage = self::where('id', $id)->find();
if (empty($userPackage)) {
return false;
}
// 确保使用量不超过总量
$maxFlow = $userPackage['totalFlow'];
$usedFlow = $usedFlow > $maxFlow ? $maxFlow : $usedFlow;
return self::where('id', $id)->update([
'usedFlow' => $usedFlow,
'updateTime' => time()
]) ? true : false;
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace app\store\model;
use think\Model;
/**
* 供应商模型
*/
class VendorModel extends Model
{
// 设置表名
protected $table = 's2_vendor';
// 主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 隐藏字段
protected $hidden = ['isDel'];
/**
* 与套餐的关联
*/
public function packages()
{
return $this->hasMany('VendorPackageModel', 'vendorId', 'id')
->where('isDel', 0);
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace app\store\model;
use think\Model;
/**
* 订单模型
*/
class VendorOrderModel extends Model
{
// 设置表名
protected $table = 'ck_vendor_order';
// 主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 状态常量
const STATUS_UNPAID = 0; // 待支付
const STATUS_PAID = 1; // 已支付
const STATUS_COMPLETED = 2; // 已完成
const STATUS_CANCELED = 3; // 已取消
/**
* 与套餐的关联
*/
public function package()
{
return $this->belongsTo('VendorPackageModel', 'packageId', 'id');
}
/**
* 生成唯一订单号
* @return string
*/
public static function generateOrderNo()
{
return date('YmdHis') . rand(1000, 9999);
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace app\store\model;
use think\Model;
/**
* 套餐模型
*/
class VendorPackageModel extends Model
{
// 设置表名
protected $table = 'ck_vendor_package';
// 主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 隐藏字段
protected $hidden = ['isDel'];
/**
* 与项目的关联
*/
public function projects()
{
return $this->hasMany('VendorProjectModel', 'packageId', 'id')
->where('isDel', 0);
}
/**
* 标签获取器
*/
public function getTagsAttr($value)
{
return $value ? explode(',', $value) : [];
}
/**
* 标签修改器
*/
public function setTagsAttr($value)
{
return is_array($value) ? implode(',', $value) : $value;
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace app\store\model;
use think\Model;
/**
* 套餐项目模型
*/
class VendorProjectModel extends Model
{
// 设置表名
protected $table = 'ck_vendor_project';
// 主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 隐藏字段
protected $hidden = ['isDel'];
/**
* 与套餐的关联
*/
public function package()
{
return $this->belongsTo('VendorPackageModel', 'packageId', 'id');
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace app\store\model;
use think\Model;
class WechatFriendModel extends Model
{
//protected $name = 'wechat_friendship';
protected $table = 's2_wechat_friend';
}

View File

@@ -0,0 +1,11 @@
<?php
namespace app\store\model;
use think\Model;
class WechatMessageModel extends Model
{
protected $table = 's2_wechat_message';
}