|
|
<?php
|
|
|
|
|
|
namespace app;
|
|
|
|
|
|
use Godruoyi\Snowflake\Snowflake;
|
|
|
use think\Model;
|
|
|
|
|
|
class BaseModel extends Model
|
|
|
{
|
|
|
|
|
|
protected $mid;
|
|
|
protected $userAgent; //客户端 weixin--微信公众号 mp_weixin--微信小程序 mp_qq--QQ小程序 app--APP
|
|
|
// 用户ID
|
|
|
protected $userId;
|
|
|
|
|
|
/**
|
|
|
* 构造函数
|
|
|
* @date 2020-07-27
|
|
|
*/
|
|
|
public function __construct(array $data = [])
|
|
|
{
|
|
|
parent::__construct($data);
|
|
|
|
|
|
// UID
|
|
|
if (isset($data['uid'])) {
|
|
|
$this->mid = $data['uid'];
|
|
|
} else {
|
|
|
if (defined('UID')) {
|
|
|
$this->mid = UID;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// USER_AGENT
|
|
|
if (isset($data['user_agent'])) {
|
|
|
$this->userAgent = $data['user_agent'];
|
|
|
} else {
|
|
|
if (defined('USER_AGENT')) {
|
|
|
$this->userAgent = USER_AGENT;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// USER_ID
|
|
|
if (isset($data['user_id'])) {
|
|
|
$this->userId = $data['user_id'];
|
|
|
} else {
|
|
|
if (defined('USER_ID')) {
|
|
|
$this->userId = USER_ID;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查找单条数据或单个字段
|
|
|
* @param array $where 条件
|
|
|
* @param string $field 获取字段
|
|
|
* @param string $order 排序条件
|
|
|
* @param array $with_table 关联查询
|
|
|
* @date 2020-07-27
|
|
|
*/
|
|
|
public function getOneData($where = [], $field = '*', $order = '', $with_table = [])
|
|
|
{
|
|
|
$primary_key_name = $this->getPk();
|
|
|
|
|
|
if (empty($order)) {
|
|
|
$order = (empty($data[$primary_key_name]) ? 'id' : $data[$primary_key_name]) . ' desc';
|
|
|
}
|
|
|
|
|
|
$array = explode(',', $field);
|
|
|
if ($field != '*') {
|
|
|
if (count($array) <= 1) {
|
|
|
return $this->with($with_table)->where($where)->order($order)->value($field);
|
|
|
} else {
|
|
|
return $this->with($with_table)->field($field)->where($where)->order($order)->find();
|
|
|
}
|
|
|
} else {
|
|
|
return $this->with($with_table)->where($where)->order($order)->find();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查找全部数据
|
|
|
* @param array $where 查询条件
|
|
|
* @param string $field 查询字段
|
|
|
* @param string $order 排序方式
|
|
|
* @param int $limit 查询前几条
|
|
|
* @param array $with_table 关联查询
|
|
|
* @date 2020-09-09
|
|
|
*/
|
|
|
public function getAllData($where, $field = '*', $order = '', $limit = 0, $with_table = [])
|
|
|
{
|
|
|
$primary_key_name = $this->getPk();
|
|
|
|
|
|
if (empty($order)) {
|
|
|
$order = (empty($data[$primary_key_name]) ? 'id' : $data[$primary_key_name]) . ' desc';
|
|
|
}
|
|
|
|
|
|
return $this->with($with_table)->field($field)->where($where)->order($order)->limit($limit)->select();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查找全部数据--带分页
|
|
|
* @param array $where 查询条件
|
|
|
* @param string $field 查询字段
|
|
|
* @param string $order 排序方式
|
|
|
* @param int $page 当前页数
|
|
|
* @param int $per_page_number 每页条数
|
|
|
* @date 2020-09-10
|
|
|
*/
|
|
|
public function getDataList($where, $page, $field = '*', $order = '', $per_page_number = 10)
|
|
|
{
|
|
|
$primary_key_name = $this->getPk();
|
|
|
|
|
|
if (empty($order)) {
|
|
|
$order = (empty($data[$primary_key_name]) ? 'id' : $data[$primary_key_name]) . ' desc';
|
|
|
}
|
|
|
|
|
|
$dataList = $this->field($field)->where($where)->order($order)
|
|
|
->paginate(['list_rows' => $per_page_number, 'page' => $page], false);
|
|
|
|
|
|
return $dataList;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新单条数据
|
|
|
* @param array $data 需要更新的信息
|
|
|
* @param array $where 条件
|
|
|
* @date 2020-07-27
|
|
|
*/
|
|
|
public function dataUpdate($data, $where = [])
|
|
|
{
|
|
|
if (empty($data)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
//有条件更新
|
|
|
$primary_key_name = $this->getPk();
|
|
|
if (!empty($where)) {
|
|
|
$model = $this->where($where)->find();
|
|
|
$res = $model->save($data);
|
|
|
if (!$res) {
|
|
|
return false;
|
|
|
}
|
|
|
$result[$primary_key_name] = $model[$primary_key_name];
|
|
|
} else {
|
|
|
if (array_key_exists($primary_key_name, $data) && !empty($data[$primary_key_name])) { // 数据修改
|
|
|
$result = self::update($data);
|
|
|
} else { // 数据插入
|
|
|
$snow_flake = new Snowflake();
|
|
|
$data[$primary_key_name] = $snow_flake->id();
|
|
|
$result = self::create($data);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if ($result !== false) {
|
|
|
return $result[$primary_key_name] ? $result[$primary_key_name] : true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 数据软删除
|
|
|
* @param array|string $where 条件
|
|
|
* @date 2020-09-16
|
|
|
*/
|
|
|
public function dataDel($where)
|
|
|
{
|
|
|
$res = $this->where($where)->useSoftDelete('delete_time', time())->delete();
|
|
|
|
|
|
if (!$res) {
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 数据删除
|
|
|
* @param array|string $where 条件
|
|
|
* @date 2021-03-25
|
|
|
*/
|
|
|
public function dataDestory($where)
|
|
|
{
|
|
|
$res = $this->where($where)->delete();
|
|
|
|
|
|
if (!$res) {
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 统计数量
|
|
|
* @param array $where 条件
|
|
|
* @return int 没有返回0 有返回数量
|
|
|
* @date 2020-11-30
|
|
|
*/
|
|
|
public function getNumber($where = [])
|
|
|
{
|
|
|
return $this->where($where)->count();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 统计总数
|
|
|
* @param string $field 字段
|
|
|
* @param array $where 条件
|
|
|
* @date 2020-11-30
|
|
|
*/
|
|
|
public function getSum($field, $where)
|
|
|
{
|
|
|
return $this->where($where)->sum($field);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 生成订单编号
|
|
|
* @param string $char 订单编号首位标识
|
|
|
* @param int $length 订单编号末位随机数长度
|
|
|
* @date 2020-11-30
|
|
|
*/
|
|
|
public function createOrderNumber($char = "", $length = 4)
|
|
|
{
|
|
|
$count = 0;
|
|
|
do {
|
|
|
$order_number = $char . date('YmdHis');
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
$order_number .= rand(0, 9);
|
|
|
}
|
|
|
$count = $this->getNumber(['order_number' => $order_number]); //保证生成的订单编号不会重复
|
|
|
} while ($count > 0);
|
|
|
|
|
|
return $order_number;
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* 修改搜索条件数组,始搜索条件数组key加表名
|
|
|
*/
|
|
|
public function whereChange($where)
|
|
|
{
|
|
|
$temp_where = array();
|
|
|
foreach ($where as $key => $value) {
|
|
|
if (strpos($key, $this->name . ".") == false) {
|
|
|
$value[0] = $this->name . "." . $value[0];
|
|
|
$temp_where[] = $value;
|
|
|
} else {
|
|
|
$temp_where[$key] = $value;
|
|
|
}
|
|
|
}
|
|
|
return $temp_where;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取无限级数据
|
|
|
* @param array $where 条件
|
|
|
* @param string $order 排序
|
|
|
* @param int $limit_level 查询几级结束
|
|
|
* @param string $relation_id 关联ID字段名
|
|
|
* @param string $keyword 返回数组中当期级别的名称
|
|
|
* @return array
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\DbException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
*/
|
|
|
public function getInfinite($where, $order = '', $limit_level = 0, $relation_id = 'pid', $keyword = 'level')
|
|
|
{
|
|
|
return $this->getInfiniteData($where, $order, $limit_level, $relation_id, $keyword, 0, []);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 无限级数据查询方法
|
|
|
* @param $where
|
|
|
* @param string $order
|
|
|
* @param int $limit_level
|
|
|
* @param string $relation_id
|
|
|
* @param string $keyword
|
|
|
* @param int $now_level
|
|
|
* @param array $return_data
|
|
|
* @return array
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\DbException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
*/
|
|
|
public function getInfiniteData($where, $order = '', $limit_level = 0, $relation_id = 'pid', $keyword = 'level', $now_level = 0, $return_data = [])
|
|
|
{
|
|
|
if (!empty($limit_level)) {
|
|
|
if ($limit_level <= $now_level) {
|
|
|
return $return_data;
|
|
|
}
|
|
|
}
|
|
|
if ($now_level == 0) {
|
|
|
$where[$relation_id] = ['elt', 0];
|
|
|
}
|
|
|
if (empty($order)) {
|
|
|
$order = [$this->getPk() => 'desc'];
|
|
|
|
|
|
}
|
|
|
$array = $this->where($where)->order($order)->select();
|
|
|
if (empty($array)) {
|
|
|
return $return_data;
|
|
|
}
|
|
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
$where[$relation_id] = $value[$this->getPk()];
|
|
|
$value[$keyword] = $now_level;
|
|
|
$return_data[] = $value;
|
|
|
$return_data = $this->getInfiniteData($where, $order, $limit_level, $relation_id, $keyword, $now_level + 1, $return_data);
|
|
|
}
|
|
|
return $return_data;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|