| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- /**
- *****************************************************************
- * 联系QQ: 290802026 *
- * 版 本: V2.0 *
- * 开发者:雨中磐石工作室 *
- * 邮 箱: admin@rockoa.com *
- * 网 址: http://www.rockoa.com/ *
- * 说 明: 数据模型 *
- * 备 注: 未经允许不得商业出售,代码欢迎参考纠正 *
- *****************************************************************
- */
- abstract class Model{
-
- public $perfix = PREFIX;
- public $rock;
- public $db;
- public $table;
- public $adminname;
- public $adminid;
- public $tempxinxi = array();
-
- public function __construct($table='')
- {
- $this->rock = $GLOBALS['rock'];
- $this->db = $GLOBALS['db'];
- $this->adminid = $this->rock->adminid;
- $this->adminname = $this->rock->adminname;
- $this->settable($table);
- $this->initModel();
- }
-
- public function settable($table, $qzbo=true)
- {
- $this->table = ''.$this->perfix.''.$table.'';
- if(!$qzbo)$this->table = $table;
- }
-
- public function initModel(){}
-
- public function getmou($fields, $where, $order='')
- {
- return $this->db->getmou($this->table, $fields, $where, $order);
- }
- public function getone($where, $fields='*', $order='')
- {
- return $this->db->getone($this->table, $where, $fields, $order);
- }
-
- public function getrows($where, $fields='*', $order='', $limit='')
- {
- return $this->db->getrows($this->table, $where, $fields, $order, $limit);
- }
-
- public function getall($where, $fields='*', $order='', $limit='')
- {
- $sql = $this->db->getsql(array(
- 'fields' => $fields,
- 'table' => $this->table,
- 'where' => $where,
- 'order' => $order,
- 'limit' => $limit
- ));
- return $this->db->getall($sql);
- }
-
- public function getarr($where, $fields='*', $kfied='id')
- {
- return $this->db->getarr($this->table, $where, $fields, $kfied);
- }
- public function rows($where)
- {
- return $this->db->rows($this->table, $where);
- }
- public function query($where, $fields='*', $order='', $limit='')
- {
- $sql = $this->db->getsql(array(
- 'fields' => $fields,
- 'table' => $this->table,
- 'where' => $where,
- 'order' => $order,
- 'limit' => $limit
- ));
- return $this->db->query($sql);
- }
-
- public function record($arr, $where='')
- {
- return $this->db->record($this->table, $arr, $where);
- }
-
- public function update($arr,$where)
- {
- return $this->record($arr, $where);
- }
-
- public function insert($arr)
- {
- $nid = 0;
- if($this->record($arr, ''))$nid = $this->db->insert_id();
- return $nid;
- }
-
- public function insertAll($arr)
- {
- $name = $values = '';
- foreach($arr as $k=>$rs){
- $cont = '';
- foreach($rs as $i=>$v){
- if($k==0)$name.=',`'.$i.'`';
- $cont.=",".$this->db->toaddval($v)."";
- }
- $cont = substr($cont, 1);
- if($k>0)$values.=',';
- $values.='('.$cont.')';
- }
- return $this->db->insert($this->table, substr($name, 1),'values '.$values.'', true);
- }
- /*
- * $colArr 字段名
- * $valArr 插入的值
- * $checkColArr 检查用的字段
- * */
- public function insertOrUpdate($valArr, $checkColArr){
- if ($valArr == null) {
- return '错误:数据有误!';
- }
- $sqlStrFirst = "INSERT INTO `$this->table` (";
- $sqlStr = "";
- for ($i = 0; $i < count($valArr); $i++) {
- $valArrChild = $valArr[$i];
- $j = 0;
- $sqlStr .= "(";
- foreach ($valArrChild as $key=>$val) {
- if ($i == 0) {
- $sqlStrFirst .= "`$key`";
- $sqlStrFirst .= $j != count($valArrChild)-1 ? ',' : ') VALUES ';
- }
- $val = $valArrChild[$key];
- $sqlStr .= "'".$val."'";
- $sqlStr .= $j++ == count($valArrChild)-1 ? ' ' : ',';
- }
- $sqlStr .= ")";
- $sqlStr .= $i+1 == count($valArr) ? ' ' : ',';
- }
- $sqlStrFirst .= $sqlStr;
- $sqlStrFirst .= "ON DUPLICATE KEY UPDATE ";
- $sqlLast = "";
- for ($k = 0; $k < count($checkColArr); $k++) {
- $sqlLast.="`$checkColArr[$k]` = values(`$checkColArr[$k]`)";
- $sqlLast .= $k+1 == count($checkColArr) ? ';' : ',';
- }
- $sqlStr = $sqlStrFirst.$sqlLast;
- return $this->db->query($sqlStr);
- }
-
- public function getwhere($where='')
- {
- return $this->db->getwhere($where);
- }
-
- public function getfields()
- {
- return $this->db->getallfields($this->table);
- }
-
- public function delete($where)
- {
- return $this->db->delete($this->table, $where);
- }
-
- public function getlimit($where, $page=1, $fields='*', $order='', $limit=20, $table='')
- {
- if($order != '')$order = 'order by '.$order.'';
- $where = $this->getwhere($where);
- if($table == '')$table = $this->table;
- $sql = "select $fields from $table where $where $order ";
- $count = $this->db->rows($table, $where);
- if($page <= 0)$page=1;
- $sql .= "limit ".($page-1)*$limit.",$limit";
- $rows = $this->db->getall($sql);
- $maxpage = ceil($count/$limit);
- return array(
- 'rows' => $rows,
- 'count' => $count,
- 'maxpage' => $maxpage,
- 'page' => $page,
- 'limit' => $limit,
- 'prevpage' => $page-1,
- 'nextpage' => $page+1,
- 'url' => ''
- );
- }
-
- public function isempt($str)
- {
- return $this->rock->isempt($str);
- }
-
- public function contain($str, $s1)
- {
- return $this->rock->contain($str, $s1);
- }
-
- public function getLastSql()
- {
- return $this->db->getLastSql();
- }
-
- public function count($where='1=1')
- {
- return $this->rows($where);
- }
-
- public function getXinxi($id,$fields='*')
- {
- if(isset($this->tempxinxi[$id]))return $this->tempxinxi[$id];
- $rs = $this->getone($id,$fields);
- $this->tempxinxi[$id] = $rs;
- return $rs;
- }
- // Post请求
- public function sendPostRequest($url, $data, $token='') {
- // 初始化 cURL
- $ch = curl_init();
- $url = preg_replace('#(?<!:)//+#', '/', $url);
- // 设置 cURL 选项
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // 添加 Authorization 头部
- $headers = array(
- 'Authorization:' . $token,
- );
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- // 忽略 SSL 证书验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- // 执行 cURL 请求
- $response = curl_exec($ch);
- // 检查 cURL 请求是否出错
- if(curl_errno($ch)){
- $error_msg = curl_error($ch);
- // 返回错误信息
- $resp = [
- "code"=> -1,
- "msg"=> "cURL Error: " . $error_msg,
- "data"=> null
- ];
- return $resp;
- }
- // 关闭 cURL 资源
- curl_close($ch);
- return json_decode($response, true);
- }
- }
- class sModel extends Model{}
|