代码初始化
This commit is contained in:
88
application/common/model/Area.php
Normal file
88
application/common/model/Area.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Cache;
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 地区数据模型
|
||||
*/
|
||||
class Area extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* 根据经纬度获取当前地区信息
|
||||
*
|
||||
* @param string $lng 经度
|
||||
* @param string $lat 纬度
|
||||
* @return Area 城市信息
|
||||
*/
|
||||
public static function getAreaFromLngLat($lng, $lat, $level = 3)
|
||||
{
|
||||
$namearr = [1 => 'geo:province', 2 => 'geo:city', 3 => 'geo:district'];
|
||||
$rangearr = [1 => 15000, 2 => 1000, 3 => 200];
|
||||
$geoname = $namearr[$level] ?? $namearr[3];
|
||||
$georange = $rangearr[$level] ?? $rangearr[3];
|
||||
// 读取范围内的ID
|
||||
$redis = Cache::store('redis')->handler();
|
||||
$georadiuslist = [];
|
||||
if (method_exists($redis, 'georadius')) {
|
||||
$georadiuslist = $redis->georadius($geoname, $lng, $lat, $georange, 'km', ['WITHDIST', 'COUNT' => 5, 'ASC']);
|
||||
}
|
||||
|
||||
if ($georadiuslist) {
|
||||
list($id, $distance) = $georadiuslist[0];
|
||||
}
|
||||
$id = isset($id) && $id ? $id : 3;
|
||||
return self::get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据经纬度获取省份
|
||||
*
|
||||
* @param string $lng 经度
|
||||
* @param string $lat 纬度
|
||||
* @return Area
|
||||
*/
|
||||
public static function getProvinceFromLngLat($lng, $lat)
|
||||
{
|
||||
$provincedata = null;
|
||||
$citydata = self::getCityFromLngLat($lng, $lat);
|
||||
if ($citydata) {
|
||||
$provincedata = self::get($citydata['pid']);
|
||||
}
|
||||
return $provincedata;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据经纬度获取城市
|
||||
*
|
||||
* @param string $lng 经度
|
||||
* @param string $lat 纬度
|
||||
* @return Area
|
||||
*/
|
||||
public static function getCityFromLngLat($lng, $lat)
|
||||
{
|
||||
$citydata = null;
|
||||
$districtdata = self::getDistrictFromLngLat($lng, $lat);
|
||||
if ($districtdata) {
|
||||
$citydata = self::get($districtdata['pid']);
|
||||
}
|
||||
return $citydata;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据经纬度获取地区
|
||||
*
|
||||
* @param string $lng 经度
|
||||
* @param string $lat 纬度
|
||||
* @return Area
|
||||
*/
|
||||
public static function getDistrictFromLngLat($lng, $lat)
|
||||
{
|
||||
$districtdata = self::getAreaFromLngLat($lng, $lat, 3);
|
||||
return $districtdata;
|
||||
}
|
||||
|
||||
}
|
||||
98
application/common/model/Attachment.php
Normal file
98
application/common/model/Attachment.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class Attachment extends Model
|
||||
{
|
||||
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = 'updatetime';
|
||||
// 定义字段类型
|
||||
protected $type = [
|
||||
];
|
||||
protected $append = [
|
||||
'thumb_style'
|
||||
];
|
||||
|
||||
protected static function init()
|
||||
{
|
||||
// 如果已经上传该资源,则不再记录
|
||||
self::beforeInsert(function ($model) {
|
||||
if (self::where('url', '=', $model['url'])->where('storage', $model['storage'])->find()) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
self::beforeWrite(function ($row) {
|
||||
if (isset($row['category']) && $row['category'] == 'unclassed') {
|
||||
$row['category'] = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function setUploadtimeAttr($value)
|
||||
{
|
||||
return is_numeric($value) ? $value : strtotime($value);
|
||||
}
|
||||
|
||||
public function getCategoryAttr($value)
|
||||
{
|
||||
return $value == '' ? 'unclassed' : $value;
|
||||
}
|
||||
|
||||
public function setCategoryAttr($value)
|
||||
{
|
||||
return $value == 'unclassed' ? '' : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取云储存的缩略图样式字符
|
||||
*/
|
||||
public function getThumbStyleAttr($value, $data)
|
||||
{
|
||||
if (!isset($data['storage']) || $data['storage'] == 'local') {
|
||||
return '';
|
||||
} else {
|
||||
$config = get_addon_config($data['storage']);
|
||||
if ($config && isset($config['thumbstyle'])) {
|
||||
return $config['thumbstyle'];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Mimetype列表
|
||||
* @return array
|
||||
*/
|
||||
public static function getMimetypeList()
|
||||
{
|
||||
$data = [
|
||||
"image/*" => __("Image"),
|
||||
"audio/*" => __("Audio"),
|
||||
"video/*" => __("Video"),
|
||||
"text/*" => __("Text"),
|
||||
"application/*" => __("Application"),
|
||||
"zip,rar,7z,tar" => __("Zip"),
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取定义的附件类别列表
|
||||
* @return array
|
||||
*/
|
||||
public static function getCategoryList()
|
||||
{
|
||||
$data = config('site.attachmentcategory') ?? [];
|
||||
foreach ($data as $index => &$datum) {
|
||||
$datum = __($datum);
|
||||
}
|
||||
$data['unclassed'] = __('Unclassed');
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
89
application/common/model/Category.php
Normal file
89
application/common/model/Category.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 分类模型
|
||||
*/
|
||||
class Category extends Model
|
||||
{
|
||||
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = 'updatetime';
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
'type_text',
|
||||
'flag_text',
|
||||
];
|
||||
|
||||
protected static function init()
|
||||
{
|
||||
self::afterInsert(function ($row) {
|
||||
if (!$row['weigh']) {
|
||||
$row->save(['weigh' => $row['id']]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function setFlagAttr($value, $data)
|
||||
{
|
||||
return is_array($value) ? implode(',', $value) : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取分类类型
|
||||
* @return array
|
||||
*/
|
||||
public static function getTypeList()
|
||||
{
|
||||
$typeList = config('site.categorytype');
|
||||
foreach ($typeList as $k => &$v) {
|
||||
$v = __($v);
|
||||
}
|
||||
return $typeList;
|
||||
}
|
||||
|
||||
public function getTypeTextAttr($value, $data)
|
||||
{
|
||||
$value = $value ? $value : $data['type'];
|
||||
$list = $this->getTypeList();
|
||||
return $list[$value] ?? '';
|
||||
}
|
||||
|
||||
public function getFlagList()
|
||||
{
|
||||
return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')];
|
||||
}
|
||||
|
||||
public function getFlagTextAttr($value, $data)
|
||||
{
|
||||
$value = $value ? $value : $data['flag'];
|
||||
$valueArr = explode(',', $value);
|
||||
$list = $this->getFlagList();
|
||||
return implode(',', array_intersect_key($list, array_flip($valueArr)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取分类列表
|
||||
* @param string $type 指定类型
|
||||
* @param string $status 指定状态
|
||||
* @return array
|
||||
*/
|
||||
public static function getCategoryArray($type = null, $status = null)
|
||||
{
|
||||
$list = collection(self::where(function ($query) use ($type, $status) {
|
||||
if (!is_null($type)) {
|
||||
$query->where('type', '=', $type);
|
||||
}
|
||||
if (!is_null($status)) {
|
||||
$query->where('status', '=', $status);
|
||||
}
|
||||
})->order('weigh', 'desc')->select())->toArray();
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
227
application/common/model/Config.php
Normal file
227
application/common/model/Config.php
Normal file
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 配置模型
|
||||
*/
|
||||
class Config extends Model
|
||||
{
|
||||
|
||||
// 表名,不含前缀
|
||||
protected $name = 'config';
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = false;
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = false;
|
||||
protected $updateTime = false;
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
'extend_html'
|
||||
];
|
||||
protected $type = [
|
||||
'setting' => 'json',
|
||||
];
|
||||
|
||||
/**
|
||||
* 读取配置类型
|
||||
* @return array
|
||||
*/
|
||||
public static function getTypeList()
|
||||
{
|
||||
$typeList = [
|
||||
'string' => __('String'),
|
||||
'password' => __('Password'),
|
||||
'text' => __('Text'),
|
||||
'editor' => __('Editor'),
|
||||
'number' => __('Number'),
|
||||
'date' => __('Date'),
|
||||
'time' => __('Time'),
|
||||
'datetime' => __('Datetime'),
|
||||
'datetimerange' => __('Datetimerange'),
|
||||
'select' => __('Select'),
|
||||
'selects' => __('Selects'),
|
||||
'image' => __('Image'),
|
||||
'images' => __('Images'),
|
||||
'file' => __('File'),
|
||||
'files' => __('Files'),
|
||||
'switch' => __('Switch'),
|
||||
'checkbox' => __('Checkbox'),
|
||||
'radio' => __('Radio'),
|
||||
'city' => __('City'),
|
||||
'selectpage' => __('Selectpage'),
|
||||
'selectpages' => __('Selectpages'),
|
||||
'array' => __('Array'),
|
||||
'custom' => __('Custom'),
|
||||
];
|
||||
return $typeList;
|
||||
}
|
||||
|
||||
public static function getRegexList()
|
||||
{
|
||||
$regexList = [
|
||||
'required' => '必选',
|
||||
'digits' => '数字',
|
||||
'letters' => '字母',
|
||||
'date' => '日期',
|
||||
'time' => '时间',
|
||||
'email' => '邮箱',
|
||||
'url' => '网址',
|
||||
'qq' => 'QQ号',
|
||||
'IDcard' => '身份证',
|
||||
'tel' => '座机电话',
|
||||
'mobile' => '手机号',
|
||||
'zipcode' => '邮编',
|
||||
'chinese' => '中文',
|
||||
'username' => '用户名',
|
||||
'password' => '密码'
|
||||
];
|
||||
return $regexList;
|
||||
}
|
||||
|
||||
public function getExtendHtmlAttr($value, $data)
|
||||
{
|
||||
$result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
|
||||
if (isset($data[$matches[1]])) {
|
||||
return $data[$matches[1]];
|
||||
}
|
||||
}, $data['extend']);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取分类分组列表
|
||||
* @return array
|
||||
*/
|
||||
public static function getGroupList()
|
||||
{
|
||||
$groupList = config('site.configgroup');
|
||||
foreach ($groupList as $k => &$v) {
|
||||
$v = __($v);
|
||||
}
|
||||
return $groupList;
|
||||
}
|
||||
|
||||
public static function getArrayData($data)
|
||||
{
|
||||
if (!isset($data['value'])) {
|
||||
$result = [];
|
||||
foreach ($data as $index => $datum) {
|
||||
$result['field'][$index] = $datum['key'];
|
||||
$result['value'][$index] = $datum['value'];
|
||||
}
|
||||
$data = $result;
|
||||
}
|
||||
$fieldarr = $valuearr = [];
|
||||
$field = $data['field'] ?? ($data['key'] ?? []);
|
||||
$value = $data['value'] ?? [];
|
||||
foreach ($field as $m => $n) {
|
||||
if ($n != '') {
|
||||
$fieldarr[] = $field[$m];
|
||||
$valuearr[] = $value[$m];
|
||||
}
|
||||
}
|
||||
return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串解析成键值数组
|
||||
* @param string $text
|
||||
* @return array
|
||||
*/
|
||||
public static function decode($text, $split = "\r\n")
|
||||
{
|
||||
$content = explode($split, $text);
|
||||
$arr = [];
|
||||
foreach ($content as $k => $v) {
|
||||
if (stripos($v, "|") !== false) {
|
||||
$item = explode('|', $v);
|
||||
$arr[$item[0]] = $item[1];
|
||||
}
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将键值数组转换为字符串
|
||||
* @param array $array
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($array, $split = "\r\n")
|
||||
{
|
||||
$content = '';
|
||||
if ($array && is_array($array)) {
|
||||
$arr = [];
|
||||
foreach ($array as $k => $v) {
|
||||
$arr[] = "{$k}|{$v}";
|
||||
}
|
||||
$content = implode($split, $arr);
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地上传配置信息
|
||||
* @return array
|
||||
*/
|
||||
public static function upload()
|
||||
{
|
||||
$uploadcfg = config('upload');
|
||||
|
||||
$uploadurl = request()->module() ? $uploadcfg['uploadurl'] : ($uploadcfg['uploadurl'] === 'ajax/upload' ? 'index/' . $uploadcfg['uploadurl'] : $uploadcfg['uploadurl']);
|
||||
|
||||
if (!preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $uploadurl) && substr($uploadurl, 0, 1) !== '/') {
|
||||
$uploadurl = url($uploadurl, '', false);
|
||||
}
|
||||
$uploadcfg['fullmode'] = isset($uploadcfg['fullmode']) && $uploadcfg['fullmode'];
|
||||
$uploadcfg['thumbstyle'] = $uploadcfg['thumbstyle'] ?? '';
|
||||
|
||||
$upload = [
|
||||
'cdnurl' => $uploadcfg['cdnurl'],
|
||||
'uploadurl' => $uploadurl,
|
||||
'bucket' => 'local',
|
||||
'maxsize' => $uploadcfg['maxsize'],
|
||||
'mimetype' => $uploadcfg['mimetype'],
|
||||
'chunking' => $uploadcfg['chunking'],
|
||||
'chunksize' => $uploadcfg['chunksize'],
|
||||
'savekey' => $uploadcfg['savekey'],
|
||||
'multipart' => [],
|
||||
'multiple' => $uploadcfg['multiple'],
|
||||
'fullmode' => $uploadcfg['fullmode'],
|
||||
'thumbstyle' => $uploadcfg['thumbstyle'],
|
||||
'storage' => 'local'
|
||||
];
|
||||
return $upload;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新配置文件
|
||||
*/
|
||||
public static function refreshFile()
|
||||
{
|
||||
//如果没有配置权限无法进行修改
|
||||
if (!\app\admin\library\Auth::instance()->check('general/config/edit')) {
|
||||
return false;
|
||||
}
|
||||
$config = [];
|
||||
$configList = self::all();
|
||||
foreach ($configList as $k => $v) {
|
||||
$value = $v->toArray();
|
||||
if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
|
||||
$value['value'] = explode(',', $value['value']);
|
||||
}
|
||||
if ($value['type'] == 'array') {
|
||||
$value['value'] = (array)json_decode($value['value'], true);
|
||||
}
|
||||
$config[$value['name']] = $value['value'];
|
||||
}
|
||||
file_put_contents(
|
||||
CONF_PATH . 'extra' . DS . 'site.php',
|
||||
'<?php' . "\n\nreturn " . var_export($config, true) . ";\n"
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
21
application/common/model/Ems.php
Normal file
21
application/common/model/Ems.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 邮箱验证码
|
||||
*/
|
||||
class Ems extends Model
|
||||
{
|
||||
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
];
|
||||
}
|
||||
23
application/common/model/MoneyLog.php
Normal file
23
application/common/model/MoneyLog.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 会员余额日志模型
|
||||
*/
|
||||
class MoneyLog extends Model
|
||||
{
|
||||
|
||||
// 表名
|
||||
protected $name = 'user_money_log';
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = '';
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
];
|
||||
}
|
||||
23
application/common/model/ScoreLog.php
Normal file
23
application/common/model/ScoreLog.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 会员积分日志模型
|
||||
*/
|
||||
class ScoreLog extends Model
|
||||
{
|
||||
|
||||
// 表名
|
||||
protected $name = 'user_score_log';
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = '';
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
];
|
||||
}
|
||||
21
application/common/model/Sms.php
Normal file
21
application/common/model/Sms.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 短信验证码
|
||||
*/
|
||||
class Sms extends Model
|
||||
{
|
||||
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
];
|
||||
}
|
||||
155
application/common/model/User.php
Normal file
155
application/common/model/User.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Db;
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 会员模型
|
||||
* @method static mixed getByUsername($str) 通过用户名查询用户
|
||||
* @method static mixed getByNickname($str) 通过昵称查询用户
|
||||
* @method static mixed getByMobile($str) 通过手机查询用户
|
||||
* @method static mixed getByEmail($str) 通过邮箱查询用户
|
||||
*/
|
||||
class User extends Model
|
||||
{
|
||||
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = 'updatetime';
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
'url',
|
||||
];
|
||||
|
||||
/**
|
||||
* 获取个人URL
|
||||
* @param string $value
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlAttr($value, $data)
|
||||
{
|
||||
return "/u/" . $data['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取头像
|
||||
* @param string $value
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public function getAvatarAttr($value, $data)
|
||||
{
|
||||
if (!$value) {
|
||||
//如果不需要启用首字母头像,请使用
|
||||
//$value = '/assets/img/avatar.png';
|
||||
$value = letter_avatar($data['nickname']);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员的组别
|
||||
*/
|
||||
public function getGroupAttr($value, $data)
|
||||
{
|
||||
return UserGroup::get($data['group_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取验证字段数组值
|
||||
* @param string $value
|
||||
* @param array $data
|
||||
* @return object
|
||||
*/
|
||||
public function getVerificationAttr($value, $data)
|
||||
{
|
||||
$value = array_filter((array)json_decode($value, true));
|
||||
$value = array_merge(['email' => 0, 'mobile' => 0], $value);
|
||||
return (object)$value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置验证字段
|
||||
* @param mixed $value
|
||||
* @return string
|
||||
*/
|
||||
public function setVerificationAttr($value)
|
||||
{
|
||||
$value = is_object($value) || is_array($value) ? json_encode($value) : $value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 变更会员余额
|
||||
* @param int $money 余额
|
||||
* @param int $user_id 会员ID
|
||||
* @param string $memo 备注
|
||||
*/
|
||||
public static function money($money, $user_id, $memo)
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$user = self::lock(true)->find($user_id);
|
||||
if ($user && $money != 0) {
|
||||
$before = $user->money;
|
||||
//$after = $user->money + $money;
|
||||
$after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
|
||||
//更新会员信息
|
||||
$user->save(['money' => $after]);
|
||||
//写入日志
|
||||
MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 变更会员积分
|
||||
* @param int $score 积分
|
||||
* @param int $user_id 会员ID
|
||||
* @param string $memo 备注
|
||||
*/
|
||||
public static function score($score, $user_id, $memo)
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$user = self::lock(true)->find($user_id);
|
||||
if ($user && $score != 0) {
|
||||
$before = $user->score;
|
||||
$after = $user->score + $score;
|
||||
$level = self::nextlevel($after);
|
||||
//更新会员信息
|
||||
$user->save(['score' => $after, 'level' => $level]);
|
||||
//写入日志
|
||||
ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据积分获取等级
|
||||
* @param int $score 积分
|
||||
* @return int
|
||||
*/
|
||||
public static function nextlevel($score = 0)
|
||||
{
|
||||
$lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
|
||||
$level = 1;
|
||||
foreach ($lv as $key => $value) {
|
||||
if ($score >= $value) {
|
||||
$level = $key;
|
||||
}
|
||||
}
|
||||
return $level;
|
||||
}
|
||||
}
|
||||
21
application/common/model/UserGroup.php
Normal file
21
application/common/model/UserGroup.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class UserGroup extends Model
|
||||
{
|
||||
|
||||
// 表名
|
||||
protected $name = 'user_group';
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = 'updatetime';
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
];
|
||||
|
||||
}
|
||||
21
application/common/model/UserRule.php
Normal file
21
application/common/model/UserRule.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class UserRule extends Model
|
||||
{
|
||||
|
||||
// 表名
|
||||
protected $name = 'user_rule';
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = 'updatetime';
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
];
|
||||
|
||||
}
|
||||
283
application/common/model/UserWallet.php
Normal file
283
application/common/model/UserWallet.php
Normal file
@@ -0,0 +1,283 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
use think\Db;
|
||||
use think\Model;
|
||||
|
||||
|
||||
/**
|
||||
* 会员钱包模型
|
||||
*/
|
||||
class UserWallet extends Model
|
||||
{
|
||||
protected $table = 'user_wallet';
|
||||
protected $autoWriteTimestamp = true;
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = 'updatetime';
|
||||
|
||||
//常量
|
||||
//钱包类型
|
||||
const MONEYTYPECOIN = 1; //金币
|
||||
const MONEYTYPEARNINGS = 2; //钻石
|
||||
|
||||
//资金操作
|
||||
//系统调节
|
||||
const OPERATION_SYSTEM = 1;
|
||||
//会员充值
|
||||
const OPERATION_RECHARGE = 2;
|
||||
//会员提现
|
||||
const OPERATION_WITHDRAW = 3;
|
||||
//金币转增(送出)
|
||||
const OPERATION_CONSUME = 4;
|
||||
//每日任务奖励
|
||||
const DAILY_TASKS_REWARD = 5;
|
||||
//推广用户充值返利
|
||||
const OPERATION_INVITE_REBATE = 6;
|
||||
//购买装扮
|
||||
const OPERATION_DECORATION = 7;
|
||||
//礼盒奖励
|
||||
const GIFT_BOX_REWARD = 8;
|
||||
//房间补贴
|
||||
const ROOM_SUBSIDY = 9;
|
||||
//购买礼物
|
||||
const OPERATION_GIFT = 10;
|
||||
//收礼增加收益
|
||||
const GIVE_GIFT_EARNING = 11;
|
||||
//工会补贴
|
||||
const GUILD_SUBSIDY = 12;
|
||||
//会员转赠(接收)
|
||||
const USER_RECEIVE = 13;
|
||||
//收益兑换
|
||||
const MONEY_CONVERSION = 14;
|
||||
//首充
|
||||
const FIRST_CHARGE = 15;
|
||||
//天降好礼充值
|
||||
const DROP_GIFT_REWARD = 16;
|
||||
//退出工会扣款
|
||||
const GUILD_EXIT = 17;
|
||||
//房主收益
|
||||
const ROOM_OWNER_EARNINGS = 18;
|
||||
//主持人收益
|
||||
const HOST_EARNINGS = 19;
|
||||
//抢头条
|
||||
const HEADLINE_REWARD = 20;
|
||||
//公会长收益
|
||||
const GUILD_EARNINGS = 21;
|
||||
//提现驳回或提现失败返还
|
||||
const WITHDRAW_FAILURE = 22;
|
||||
//财富等级奖励金币领取
|
||||
const FINANCE_LEVEL_REWARD = 23;
|
||||
//删除关系扣金币
|
||||
const DELETE_RELATION_COIN = 24;
|
||||
//赠送好友金币
|
||||
const TRANSFER_COIN = 25;
|
||||
//好友转赠所得金币
|
||||
const RECEIVE_COIN = 26;
|
||||
|
||||
|
||||
//金币支出类型数组
|
||||
public $coin_consumption_type_array = [
|
||||
self::OPERATION_CONSUME,
|
||||
self::OPERATION_DECORATION,
|
||||
self::OPERATION_GIFT,
|
||||
self::GUILD_EXIT,
|
||||
self::HEADLINE_REWARD,
|
||||
self::TRANSFER_COIN
|
||||
];
|
||||
//钻石支出类型数组
|
||||
public $diamond_consumption_type_array = [
|
||||
self::OPERATION_WITHDRAW,
|
||||
self::MONEY_CONVERSION
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->hasOne('User', 'id', 'user_id');
|
||||
}
|
||||
//钱包类型
|
||||
public static function getMoneyType($value)
|
||||
{
|
||||
$status = [
|
||||
self::MONEYTYPECOIN => '金币',
|
||||
self::MONEYTYPEARNINGS => '钻石'
|
||||
];
|
||||
return isset($status[$value]) ? $status[$value] : '';
|
||||
|
||||
}
|
||||
public static function ChangeTypeLable($type)
|
||||
{
|
||||
$status = [
|
||||
self::OPERATION_SYSTEM => '系统调节',
|
||||
self::OPERATION_RECHARGE => '会员充值',
|
||||
self::OPERATION_WITHDRAW => '会员提现',
|
||||
self::OPERATION_CONSUME => '金币转增(送出)',
|
||||
self::DAILY_TASKS_REWARD => '每日任务奖励',
|
||||
self::OPERATION_INVITE_REBATE => '邀请用户充值返利',
|
||||
self::OPERATION_DECORATION => '购买装扮',
|
||||
self::GIFT_BOX_REWARD => '礼盒奖励',
|
||||
self::ROOM_SUBSIDY => '房间补贴',
|
||||
self::OPERATION_GIFT => '购买礼物',
|
||||
self::GIVE_GIFT_EARNING => '送礼增加收益',
|
||||
self::GUILD_SUBSIDY => '工会补贴',
|
||||
self::USER_RECEIVE => '会员转赠(接收)',
|
||||
self::MONEY_CONVERSION => '钻石兑换金币',
|
||||
self::FIRST_CHARGE => '首充',
|
||||
self::DROP_GIFT_REWARD => '天降好礼充值',
|
||||
self::GUILD_EXIT => '退出工会扣款',
|
||||
self::ROOM_OWNER_EARNINGS => '房主收益',
|
||||
self::HOST_EARNINGS => '主持人收益',
|
||||
self::HEADLINE_REWARD => '抢头条',
|
||||
self::GUILD_EARNINGS => '公会长收益',
|
||||
self::WITHDRAW_FAILURE => '提现驳回或提现失败返还',
|
||||
self::FINANCE_LEVEL_REWARD => '财富等级奖励金币领取',
|
||||
self::DELETE_RELATION_COIN => '删除关系扣金币',
|
||||
self::TRANSFER_COIN => '赠送好友金币',
|
||||
self::RECEIVE_COIN => '好友转赠所得金币'
|
||||
];
|
||||
return $status[$type] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改用户资金
|
||||
* @param $user_id 用户ID
|
||||
* @param $change_value
|
||||
* @param $money_type
|
||||
* @param $change_type
|
||||
* @param $remarks
|
||||
* @param $from_uid
|
||||
* @param $from_id
|
||||
* @param $rid
|
||||
* @param $is_uid_search
|
||||
* @return array|void
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\exception\DbException
|
||||
*/
|
||||
public function change_user_money($user_id, $change_value, $money_type,$change_type, $remarks = "", $room_id=0,$from_uid = 0, $from_id = 0)
|
||||
{
|
||||
if (in_array($change_type, $this->coin_consumption_type_array) && $money_type==self::MONEYTYPECOIN) {//金币支出
|
||||
$change_value = $change_value * -1;
|
||||
}
|
||||
if (in_array($change_type, $this->diamond_consumption_type_array) && $money_type==self::MONEYTYPEARNINGS){//钻石支出
|
||||
$change_value = $change_value * -1;
|
||||
}
|
||||
$user_info = db::name('user')->find($user_id);
|
||||
if (empty($user_info['id'])) {
|
||||
return ['code' => 0, 'msg' => "用户信息错误", 'data' => null];
|
||||
}
|
||||
$user_wallet = db::name('user_wallet')->where(['user_id' => $user_id])->find();
|
||||
if (empty($user_wallet['id'])) {
|
||||
return ['code' => 0, 'msg' => "用户信息错误", 'data' => null];
|
||||
}
|
||||
$money_type_str = $this->getMoneyType($money_type);
|
||||
if (empty($money_type_str)) {
|
||||
return ['code' => 0, 'msg' => "非法资金类型", 'data' => null];
|
||||
}
|
||||
$after_coin = $user_wallet['coin'];
|
||||
$after_earnings = $user_wallet['earnings'];
|
||||
if ($money_type == 1) {
|
||||
$change_field = "coin";
|
||||
$after_coin += $change_value;
|
||||
if($after_coin > 99999999){
|
||||
return ['code' => 0, 'msg' => "当前用户金币已达上限", 'data' => null];
|
||||
}
|
||||
} elseif ($money_type == 2) {
|
||||
$change_field = "earnings";
|
||||
$after_earnings += $change_value;
|
||||
if($after_earnings > 99999999){
|
||||
return ['code' => 0, 'msg' => "当前用户钻石已达上限", 'data' => null];
|
||||
}
|
||||
} else {
|
||||
return ['code' => 0, 'msg' => "非法资金类型", 'data' => null];
|
||||
}
|
||||
$change_name = $this->ChangeTypeLable($change_type);
|
||||
if(empty($change_name)){
|
||||
return ['code' => 0, 'msg' => "非法资金变动类型", 'data' => null];
|
||||
}
|
||||
if (!is_numeric($change_value)) {
|
||||
return ['code' => 0, 'msg' => "变动的数值必须为数字", 'data' => null];
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$data['user_id'] = $user_id;
|
||||
$data['room_id'] = $room_id;
|
||||
$data['change_type'] = $change_type;
|
||||
$data['money_type'] = $money_type;
|
||||
$data['change_value'] = abs($change_value);
|
||||
$data['after_coin'] = $after_coin;
|
||||
$data['after_earnings'] = $after_earnings;
|
||||
$data['from_id'] = $from_id;
|
||||
$data['from_uid'] = $from_uid;
|
||||
$data['remarks'] = $remarks;
|
||||
$data['createtime'] = time();
|
||||
$data['updatetime'] = time();
|
||||
Db::startTrans();
|
||||
try {
|
||||
if($change_value < 0){
|
||||
$change_value_abs = abs($change_value);
|
||||
$change_value_up = $user_wallet[$change_field] - $change_value_abs;
|
||||
if($change_value_up<0){
|
||||
Db::rollback();
|
||||
return ['code' => 0, 'msg' => $money_type_str . "不足", 'data' => null];
|
||||
}
|
||||
}
|
||||
Db::name('user_wallet')->where('user_id', $user_id)->inc($change_field, $change_value)->update(['updatetime' => time()]);
|
||||
$reslut = Db::name('vs_user_money_log')->insert($data);
|
||||
if (!$reslut) {
|
||||
Db::rollback();
|
||||
return ['code' => 0, 'msg' => "请重试", 'data' => null];
|
||||
}
|
||||
// 提交事务
|
||||
Db::commit();
|
||||
return ['code' => 1, 'msg' => "操作成功", 'data' => null];
|
||||
} catch (\Exception $e) {
|
||||
// 回滚事务
|
||||
Db::rollback();
|
||||
return ['code' => 0, 'msg' => "请重试", 'data' => null];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 用户资金变动日志
|
||||
*/
|
||||
public function money_change_log($user_id, $money_type=0, $page=0,$page_limit=30){
|
||||
if($money_type){
|
||||
$where['money_type'] =$money_type;
|
||||
}
|
||||
$log['count'] = Db::name('vs_user_money_log')->where($where)->where('user_id',$user_id)->count();
|
||||
$log_select = Db::name('vs_user_money_log')
|
||||
->where($where)
|
||||
->where('user_id',$user_id)
|
||||
->order('log_id desc');
|
||||
if($page){
|
||||
$log_select->page($page,$page_limit);
|
||||
}
|
||||
$log['list'] = $log_select->select();
|
||||
foreach ($log['list'] as $key => &$value) {
|
||||
$value['money_type'] = $this->getMoneyType($value['money_type']);
|
||||
$change_type = $value['change_type'];
|
||||
$value['change_type'] = $this->ChangeTypeLable($value['change_type']);
|
||||
$value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
|
||||
if($money_type==1 ){
|
||||
if(in_array($change_type,$this->coin_consumption_type_array)){
|
||||
$value['change_in_out'] = "支出";
|
||||
$value['change_value'] = $value['change_value']*-1;
|
||||
}else{
|
||||
$value['change_in_out'] = "收入";
|
||||
}
|
||||
}else{
|
||||
if(in_array($change_type,$this->diamond_consumption_type_array)){
|
||||
$value['change_in_out'] = "支出";
|
||||
$value['change_value'] = $value['change_value']*-1;
|
||||
}else{
|
||||
$value['change_in_out'] = "收入";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return $log;
|
||||
}
|
||||
}
|
||||
50
application/common/model/Version.php
Normal file
50
application/common/model/Version.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class Version extends Model
|
||||
{
|
||||
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = 'updatetime';
|
||||
// 定义字段类型
|
||||
protected $type = [
|
||||
];
|
||||
|
||||
/**
|
||||
* 检测版本号
|
||||
*
|
||||
* @param string $version 客户端版本号
|
||||
* @return array
|
||||
*/
|
||||
public static function check($version)
|
||||
{
|
||||
$versionlist = self::where('status', 'normal')->cache('__version__')->order('weigh desc,id desc')->select();
|
||||
foreach ($versionlist as $k => $v) {
|
||||
// 版本正常且新版本号不等于验证的版本号且找到匹配的旧版本
|
||||
if ($v['status'] == 'normal' && $v['newversion'] !== $version && \fast\Version::check($version, $v['oldversion'])) {
|
||||
$updateversion = $v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isset($updateversion)) {
|
||||
$search = ['{version}', '{newversion}', '{downloadurl}', '{url}', '{packagesize}'];
|
||||
$replace = [$version, $updateversion['newversion'], $updateversion['downloadurl'], $updateversion['downloadurl'], $updateversion['packagesize']];
|
||||
$upgradetext = str_replace($search, $replace, $updateversion['content']);
|
||||
return [
|
||||
"enforce" => $updateversion['enforce'],
|
||||
"version" => $version,
|
||||
"newversion" => $updateversion['newversion'],
|
||||
"downloadurl" => $updateversion['downloadurl'],
|
||||
"packagesize" => $updateversion['packagesize'],
|
||||
"upgradetext" => $upgradetext
|
||||
];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user