| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | 
							- <?php
 
- /**
 
-  * CommonModel.php UTF-8
 
-  * 公共Model
 
-  *
 
-  * @date    : 2020/9/14 17:03
 
-  *
 
-  * @license 这不是一个自由软件,未经授权不许任何使用和传播。
 
-  * @author  : chenbingling <cbl@huosdk.com>
 
-  * @version : H5IOS 1.0
 
-  */
 
- namespace huosdk\h5ios\core\model;
 
- use huolib\constant\CommonConst;
 
- use huosdk\h5ios\core\constant\CacheConst;
 
- use think\Cache;
 
- use think\Model;
 
- class CommonModel extends Model {
 
-     protected $pk = 'id';
 
-     //  关联模型过滤
 
-     protected $relationFilter         = [];
 
-     protected $table_cache_key_prefix = CacheConst::CACHE_TABLE_FIELD_PREFIX;
 
-     /**
 
-      * 懒人函数
 
-      *
 
-      * @access public
 
-      *
 
-      * @param string $string 字符串
 
-      *
 
-      * @return
 
-      */
 
-     public function strToArr($string) {
 
-         return is_string($string) ? explode(',', $string) : (array)$string;
 
-     }
 
-     /**
 
-      * 懒人函数
 
-      *
 
-      * @access public
 
-      *
 
-      * @param array $arr 数组
 
-      *
 
-      * @return string
 
-      */
 
-     public function arrToStr($arr) {
 
-         return is_array($arr) ? implode(',', $arr) : (string)$arr;
 
-     }
 
-     /**
 
-      * 计算在哪张表
 
-      *
 
-      * @return \Think\Model
 
-      */
 
-     public function computeTable() {
 
-         return $this;
 
-     }
 
-     /**
 
-      * @param      $data
 
-      * @param bool $replace
 
-      * @param bool $get_last_insert_id
 
-      *
 
-      * @return bool|int|string
 
-      */
 
-     public function insertLog($data, $replace = false, $get_last_insert_id = true) {
 
-         if ($_id = $this->computeTable()->insert($data, $replace, $get_last_insert_id)) {
 
-             return $_id;
 
-         } else {
 
-             return false;
 
-         }
 
-     }
 
-     /**
 
-      * 添加数据
 
-      *
 
-      * @param array $data 需要添加的数据
 
-      *
 
-      * @return false|int  添加失败返回 false 添加成功 返回添加的ID
 
-      */
 
-     public function addData($data) {
 
-         $this->checkField();
 
-         if (empty($data)) {
 
-             return false;
 
-         }
 
-         $_data = $data;
 
-         $_model = new static();
 
-         $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
 
-         if (false !== $_rs) {
 
-             return $_model->getLastInsID();
 
-         }
 
-         return false;
 
-     }
 
-     /**
 
-      * 通过ID获取信息
 
-      *
 
-      * @param int $id 主键ID
 
-      *
 
-      * @return array|false
 
-      */
 
-     public function getInfoById($id) {
 
-         $this->checkField();
 
-         $_map[$this->pk] = $id;
 
-         $_info = $this->useGlobalScope(false)->where($_map)->find();
 
-         if (false === $_info) {
 
-             return false;
 
-         }
 
-         if (is_object($_info)) {
 
-             return $_info->toArray();
 
-         } else {
 
-             return $_info;
 
-         }
 
-     }
 
-     /**
 
-      * 更新数据
 
-      *
 
-      * @param array     $data 数据
 
-      * @param int|array $id   主集ID
 
-      *
 
-      * @return bool
 
-      */
 
-     public function updateData($data, $id) {
 
-         $this->checkField();
 
-         if (is_array($id)) {
 
-             $_map[$this->pk] = ['in', $id];
 
-         } else {
 
-             $_map[$this->pk] = $id;
 
-         }
 
-         $_data = $data;
 
-         $_model = new static();
 
-         $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
 
-         if (false === $_rs) {
 
-             return false;
 
-         } else {
 
-             return true;
 
-         }
 
-     }
 
-     /**
 
-      * 删除数据
 
-      *
 
-      * @param array|int $ids         ID合集
 
-      * @param bool      $is_complete 是否完成删除
 
-      *
 
-      * @return bool|int
 
-      */
 
-     public function deleteData($ids, $is_complete = false) {
 
-         $this->checkField();
 
-         if (true == is_array($ids)) {
 
-             $_ids = $ids;
 
-         } else {
 
-             $_ids = [$ids];
 
-         }
 
-         $_map[$this->pk] = ['in', $_ids];
 
-         if (true == $is_complete) {
 
-             /* 彻底删除 */
 
-             return $this->useGlobalScope(false)->where($_map)->delete();
 
-         } else {
 
-             $_data['is_delete'] = CommonConst::CONST_DELETED;
 
-             $_data['delete_time'] = time();
 
-             foreach ($_ids as $_id) {
 
-                 $_rs = self::updateData($_data, $_id);
 
-                 if (false === $_rs) {
 
-                     return false;
 
-                 }
 
-             }
 
-             return true;
 
-         }
 
-     }
 
-     /**当前模型名称(数据库表名,不带前缀)
 
-      *
 
-      * @return string
 
-      */
 
-     public function getName() {
 
-         return $this->name;
 
-     }
 
-     /**
 
-      * 通过条件获取数量
 
-      *
 
-      * @param array $where 条件
 
-      *
 
-      * @return int
 
-      */
 
-     public function getCnt($where = []) {
 
-         $_map = $where;
 
-         $_cnt = $this->useGlobalScope(false)->where($_map)->count();
 
-         if (empty($_cnt)) {
 
-             return 0;
 
-         }
 
-         return $_cnt;
 
-     }
 
-     /**
 
-      * 通过条件获取汇总
 
-      *
 
-      * @param string $field 字段
 
-      * @param array  $where 条件
 
-      *
 
-      * @return double
 
-      */
 
-     public function getSum($field, $where = []) {
 
-         $_map = $where;
 
-         $_sum = $this->useGlobalScope(false)->where($_map)->sum($field);
 
-         if (false == $_sum) {
 
-             return false;
 
-         }
 
-         return $_sum;
 
-     }
 
-     /**
 
-      * 判断表中字段是否存在
 
-      *
 
-      * @param string $table  表名
 
-      * @param string $column 字段名
 
-      *
 
-      * @return bool
 
-      */
 
-     public function isColumnExist($table, $column) {
 
-         $_cache_key = $this->table_cache_key_prefix.strtolower($table.$column);
 
-         $_data = Cache::get($_cache_key);
 
-         if (1 == $_data) {
 
-             return true;
 
-         }
 
-         $_has_it = false;//是否存在该字段
 
-         $_columns = db()->getTableFields($table);
 
-         if (!empty($_columns)) {
 
-             foreach ($_columns as $_k => $_v) {
 
-                 if ($_v == $column) {
 
-                     $_has_it = true;
 
-                     break;
 
-                 }
 
-             }
 
-         }
 
-         if (true == $_has_it) {
 
-             /* 永不过期 */
 
-             Cache::set($_cache_key, 1, CommonConst::CONST_MAX_INT);
 
-         }
 
-         return $_has_it;
 
-     }
 
-     /**
 
-      * 校验字段
 
-      *
 
-      * @return bool
 
-      */
 
-     protected function checkField() {
 
-         return true;
 
-     }
 
- }
 
 
  |