Model.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. *****************************************************************
  4. * 联系QQ: 290802026 *
  5. * 版 本: V2.0 *
  6. * 开发者:雨中磐石工作室 *
  7. * 邮 箱: admin@rockoa.com *
  8. * 网 址: http://www.rockoa.com/ *
  9. * 说 明: 数据模型 *
  10. * 备 注: 未经允许不得商业出售,代码欢迎参考纠正 *
  11. *****************************************************************
  12. */
  13. abstract class Model{
  14. public $perfix = PREFIX;
  15. public $rock;
  16. public $db;
  17. public $table;
  18. public $adminname;
  19. public $adminid;
  20. public $tempxinxi = array();
  21. public function __construct($table='')
  22. {
  23. $this->rock = $GLOBALS['rock'];
  24. $this->db = $GLOBALS['db'];
  25. $this->adminid = $this->rock->adminid;
  26. $this->adminname = $this->rock->adminname;
  27. $this->settable($table);
  28. $this->initModel();
  29. }
  30. public function settable($table, $qzbo=true)
  31. {
  32. $this->table = ''.$this->perfix.''.$table.'';
  33. if(!$qzbo)$this->table = $table;
  34. }
  35. public function initModel(){}
  36. public function getmou($fields, $where, $order='')
  37. {
  38. return $this->db->getmou($this->table, $fields, $where, $order);
  39. }
  40. public function getone($where, $fields='*', $order='')
  41. {
  42. return $this->db->getone($this->table, $where, $fields, $order);
  43. }
  44. public function getrows($where, $fields='*', $order='', $limit='')
  45. {
  46. return $this->db->getrows($this->table, $where, $fields, $order, $limit);
  47. }
  48. public function getall($where, $fields='*', $order='', $limit='')
  49. {
  50. $sql = $this->db->getsql(array(
  51. 'fields' => $fields,
  52. 'table' => $this->table,
  53. 'where' => $where,
  54. 'order' => $order,
  55. 'limit' => $limit
  56. ));
  57. return $this->db->getall($sql);
  58. }
  59. public function getarr($where, $fields='*', $kfied='id')
  60. {
  61. return $this->db->getarr($this->table, $where, $fields, $kfied);
  62. }
  63. public function rows($where)
  64. {
  65. return $this->db->rows($this->table, $where);
  66. }
  67. public function query($where, $fields='*', $order='', $limit='')
  68. {
  69. $sql = $this->db->getsql(array(
  70. 'fields' => $fields,
  71. 'table' => $this->table,
  72. 'where' => $where,
  73. 'order' => $order,
  74. 'limit' => $limit
  75. ));
  76. return $this->db->query($sql);
  77. }
  78. public function record($arr, $where='')
  79. {
  80. return $this->db->record($this->table, $arr, $where);
  81. }
  82. public function update($arr,$where)
  83. {
  84. return $this->record($arr, $where);
  85. }
  86. public function insert($arr)
  87. {
  88. $nid = 0;
  89. if($this->record($arr, ''))$nid = $this->db->insert_id();
  90. return $nid;
  91. }
  92. public function insertAll($arr)
  93. {
  94. $name = $values = '';
  95. foreach($arr as $k=>$rs){
  96. $cont = '';
  97. foreach($rs as $i=>$v){
  98. if($k==0)$name.=',`'.$i.'`';
  99. $cont.=",".$this->db->toaddval($v)."";
  100. }
  101. $cont = substr($cont, 1);
  102. if($k>0)$values.=',';
  103. $values.='('.$cont.')';
  104. }
  105. return $this->db->insert($this->table, substr($name, 1),'values '.$values.'', true);
  106. }
  107. /*
  108. * $colArr 字段名
  109. * $valArr 插入的值
  110. * $checkColArr 检查用的字段
  111. * */
  112. public function insertOrUpdate($valArr, $checkColArr){
  113. if ($valArr == null) {
  114. return '错误:数据有误!';
  115. }
  116. $sqlStrFirst = "INSERT INTO `$this->table` (";
  117. $sqlStr = "";
  118. for ($i = 0; $i < count($valArr); $i++) {
  119. $valArrChild = $valArr[$i];
  120. $j = 0;
  121. $sqlStr .= "(";
  122. foreach ($valArrChild as $key=>$val) {
  123. if ($i == 0) {
  124. $sqlStrFirst .= "`$key`";
  125. $sqlStrFirst .= $j != count($valArrChild)-1 ? ',' : ') VALUES ';
  126. }
  127. $val = $valArrChild[$key];
  128. $sqlStr .= "'".$val."'";
  129. $sqlStr .= $j++ == count($valArrChild)-1 ? ' ' : ',';
  130. }
  131. $sqlStr .= ")";
  132. $sqlStr .= $i+1 == count($valArr) ? ' ' : ',';
  133. }
  134. $sqlStrFirst .= $sqlStr;
  135. $sqlStrFirst .= "ON DUPLICATE KEY UPDATE ";
  136. $sqlLast = "";
  137. for ($k = 0; $k < count($checkColArr); $k++) {
  138. $sqlLast.="`$checkColArr[$k]` = values(`$checkColArr[$k]`)";
  139. $sqlLast .= $k+1 == count($checkColArr) ? ';' : ',';
  140. }
  141. $sqlStr = $sqlStrFirst.$sqlLast;
  142. return $this->db->query($sqlStr);
  143. }
  144. public function getwhere($where='')
  145. {
  146. return $this->db->getwhere($where);
  147. }
  148. public function getfields()
  149. {
  150. return $this->db->getallfields($this->table);
  151. }
  152. public function delete($where)
  153. {
  154. return $this->db->delete($this->table, $where);
  155. }
  156. public function getlimit($where, $page=1, $fields='*', $order='', $limit=20, $table='')
  157. {
  158. if($order != '')$order = 'order by '.$order.'';
  159. $where = $this->getwhere($where);
  160. if($table == '')$table = $this->table;
  161. $sql = "select $fields from $table where $where $order ";
  162. $count = $this->db->rows($table, $where);
  163. if($page <= 0)$page=1;
  164. $sql .= "limit ".($page-1)*$limit.",$limit";
  165. $rows = $this->db->getall($sql);
  166. $maxpage = ceil($count/$limit);
  167. return array(
  168. 'rows' => $rows,
  169. 'count' => $count,
  170. 'maxpage' => $maxpage,
  171. 'page' => $page,
  172. 'limit' => $limit,
  173. 'prevpage' => $page-1,
  174. 'nextpage' => $page+1,
  175. 'url' => ''
  176. );
  177. }
  178. public function isempt($str)
  179. {
  180. return $this->rock->isempt($str);
  181. }
  182. public function contain($str, $s1)
  183. {
  184. return $this->rock->contain($str, $s1);
  185. }
  186. public function getLastSql()
  187. {
  188. return $this->db->getLastSql();
  189. }
  190. public function count($where='1=1')
  191. {
  192. return $this->rows($where);
  193. }
  194. public function getXinxi($id,$fields='*')
  195. {
  196. if(isset($this->tempxinxi[$id]))return $this->tempxinxi[$id];
  197. $rs = $this->getone($id,$fields);
  198. $this->tempxinxi[$id] = $rs;
  199. return $rs;
  200. }
  201. // Post请求
  202. public function sendPostRequest($url, $data, $token='') {
  203. // 初始化 cURL
  204. $ch = curl_init();
  205. $url = preg_replace('#(?<!:)//+#', '/', $url);
  206. // 设置 cURL 选项
  207. curl_setopt($ch, CURLOPT_URL, $url);
  208. curl_setopt($ch, CURLOPT_POST, 1);
  209. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  210. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  211. // 添加 Authorization 头部
  212. $headers = array(
  213. 'Authorization:' . $token,
  214. );
  215. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  216. // 忽略 SSL 证书验证
  217. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  218. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  219. // 执行 cURL 请求
  220. $response = curl_exec($ch);
  221. // 检查 cURL 请求是否出错
  222. if(curl_errno($ch)){
  223. $error_msg = curl_error($ch);
  224. // 返回错误信息
  225. $resp = [
  226. "code"=> -1,
  227. "msg"=> "cURL Error: " . $error_msg,
  228. "data"=> null
  229. ];
  230. return $resp;
  231. }
  232. // 关闭 cURL 资源
  233. curl_close($ch);
  234. return json_decode($response, true);
  235. }
  236. }
  237. class sModel extends Model{}