|
@@ -120,6 +120,46 @@ abstract class Model{
|
|
|
}
|
|
|
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='')
|
|
|
{
|
|
@@ -186,6 +226,50 @@ abstract class Model{
|
|
|
$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{}
|