首页
友人帐
留言板
关于
Search
1
IDE Eval Resetter:JetBrains 全家桶无限试用插件
378 阅读
2
影视资源采集站收录大全
302 阅读
3
linux安装或升级protoc
223 阅读
4
VFM 3.7.5 源码 - 一个极简的 PHP 私人云盘!
161 阅读
5
批量采集美女写真等图片做图片站
149 阅读
谈天说地
程序源码
技术教程
成品源码
登录
Search
标签搜索
PHP
linux
源码
go
windows
centos
原创
mysql
微信
激活
采集
宝塔
绿色版
API
解析
SDK
图片
破解
域名
html
云青
累计撰写
150
篇文章
累计收到
78
条评论
首页
栏目
谈天说地
程序源码
技术教程
成品源码
页面
友人帐
留言板
关于
搜索到
40
篇与
的结果
2021-05-07
分享自己封装的mysql类 基于pdo
分享自己封装的mysql类 基于pdo由于经常开发小型系统用不到框架,用原生写又太麻烦干脆封装一个MySQL操作类用了连贯操作 用法和tp相似 上手快 Db.zip用法 $res = Db::table('tbl_user')->where('id', 1)->find(); // 获取单个数据 $res = Db::table('tbl_user')->order('id', 'desc')->select(); // 获取结果集 排序 $res = Db::instance()->getLastSql(); // 获取上次执行的sql $res = Db::table("tbl_user")->where('id', 2)->update(['name' => '云青']); // 更新数据 $res = Db::table("tbl_user")->insert(['account' => 'acc', 'name' => 123]); // 插入单个数据 并返回插入id $res = Db::table("tbl_user") ->insertAll( [ ['account' => 'test1', 'name' => 'test1'], ['account' => 'test2', 'name' => 'test2'] ] ); // 批量插入数据 $res = Db::table('tbl_user')->where('id', 5)->delete(); // 删除数据 $res = Db::table('tbl_user')->tbFields(); // 获取数据表字段 $res = Db::table('tbl_user')->count('id'); // 统计数据条数 $res = Db::table('tbl_user')->field('id')->sum(); // 统计id和 $res = Db::table('tbl_user')->doSql("select * from tbl_user"); // 自定义sql $res = Db::instance()->startTrans(); // 开启事务 $res = Db::table("tbl_user") ->insertAll( [ ['account' => 'test1', 'name' => 'test1'], ['account' => 'test2', 'test2' => 'test2'] ] ); // 批量插入数据 $res = Db::instance()->commit(); // 提交 $res = Db::instance()->rollback(); // 回滚 $res = Db::table('tbl_user') ->field('id') ->whereIn('account', 'test1,test2') ->where('id', '!=', 2) ->select(); // whereIn $res = Db::table('tbl_user') // whereIn ->field('id') ->whereIn('account', 'test1,test2') ->whereIn('name', ['test1', 'test2'], 'or') ->where('pmid', '=', 0) ->select();封装类<?php /** * @author anderyly * @email admin@aaayun.cc * @link http://vclove.cn/ * @copyright Copyright (c) 2018 */ namespace ay\lib; class Db { private static $db; private $dbType = 'mysql'; private $pConnect = true; private static $table; private $host; private $port; private $dbName; private $user; private $pass; private $group = ''; private static $sql = false; //最后一条sql语句 private $where = ''; private $order = ''; private $limit = ''; private $field = '*'; private $clear = 0; //状态,0表示查询条件干净,1表示查询条件污染 private static $trans = 0; //事务指令数 /** * 初始化 * Dc constructor. * @param null $conf 数据库配置 */ public function __construct($conf = null) { class_exists('PDO') or halt("PDO模块不存在"); if (is_null($conf)) { $conf = C(); } $this->dbType = $conf['DB_TYPE']; $this->host = $conf['DB_HOST']; $this->port = $conf['DB_PORT']; $this->user = $conf['DB_USER']; $this->pass = $conf['DB_PASS']; $this->dbName = $conf['DB_NAME']; //连接数据库 if (is_null(self::$db)) { $this->connect(); } } /** * PDO连接数据库 * @throws \Exception */ private function connect() { $dsn = $this->dbType . ':host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbName; $options = $this->pConnect ? [\PDO::ATTR_PERSISTENT => true] : []; try { $dbh = new \PDO($dsn, $this->user, $this->pass, $options); $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); } catch (\PDOException $e) { throw new \Exception('Connection failed: ' . $e->getMessage()); } $dbh->exec('SET NAMES utf8'); self::$db = $dbh; } /** * @return object */ public static function instance() { return new self(); } /** * 设置数据表名 * @param $table * @return object */ public static function name($table) { if (empty($table)) { halt('不能设置数据表名为空'); } $tableS = C('DB_PRE') . $table; self::$table = self::addChar($tableS); return new self(); } /** * 设置数据表名 * @param $table * @return object */ public static function table($table) { if (empty($table)) { halt('不能设置数据表名为空'); } self::$table = self::addChar($table); return new self(); } /** * 设置当前数据表别名 * @param $alias * @return object */ public function alias($alias): object { if (empty($alias) or empty(self::$table)) { halt('数据库表名设置别名为空'); } self::$table .= ' AS ' . $alias; return $this; } private static function addChar($value, $num = 0) { if (strpos($value, "`") === false and $num == 1 and !is_int($value)) { $data = "'" . trim($value) . "'"; } else if (strpos($value, "`") === false and !is_int($value) and $num != 1) { $data = "`" . trim($value) . "`"; } else { $data = trim($value); } return $data; } /** * 执行查询 主要针对 SELECT, SHOW 等指令 * @param string $sql * @return mixed */ public function doQuery($sql = '', $options = true) { self::$sql = $sql; if ($options) { $result = self::$db->query(self::$sql)->fetchAll(\PDO::FETCH_ASSOC); } else { $result = self::$db->query(self::$sql)->fetch(\PDO::FETCH_ASSOC); } return $result; } /** * 执行语句 针对 INSERT, UPDATE 以及DELETE,exec结果返回受影响的行数 * @param string $sql * @return mixed */ public function doExec($sql = '', $op = '') { self::$sql = $sql; $res = self::$db->exec(self::$sql); if ($op == 'insert') { $res = $this->getLastInsId(); } return $res; } /** * 执行sql语句,自动判断进行查询或者执行操作 * @param string $sql SQL指令 * @return mixed */ public function doSql($sql = '') { $queryIps = 'INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|SELECT .* INTO|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK'; if (preg_match('/^\s*"?(' . $queryIps . ')\s+/i', $sql)) { return $this->doExec($sql); } else { return $this->doQuery($sql); } } /** * 获取上一次查询的sql * @return string */ public function getLastSql() { return self::$sql; } /** * 插入方法 * @param array $data * @return mixed * @throws \Exception */ public function insert(array $data) { $data = $this->dataFormat($data); if (!$data) { exit('插入数据不能为空'); } $sql = 'INSERT INTO ' . self::$table . '(' . implode(',', array_keys($data)) . ') VALUES(' . implode(',', array_values($data)) . ')'; return $this->doExec($sql, 'insert'); } /** * 批量插入方法 * @param array $data * @return mixed * @throws \Exception */ public function insertAll(array $data) { $value = '('; foreach ($data as $k => $v) { foreach ($v as $k1 => $v1) { $value .= "'" . $v1 . "',"; } $value = rtrim($value, ','); $value .= '), ('; } $value = rtrim($value, ', ('); $sql = 'INSERT INTO ' . self::$table . '(' . implode(',', array_keys($data[0])) . ') VALUES ' . $value; return $this->doExec($sql, false); } /** * 获取最后一次插入时的ID * @return mixed */ public function getLastInsId() { $sql = 'SELECT LAST_INSERT_ID()'; $res = $this->doQuery($sql, false); return isset($res['LAST_INSERT_ID()']) ? $res['LAST_INSERT_ID()'] : 'false'; } /** * 删除方法 * @return mixed * @throws \Exception */ public function delete() { //安全考虑,阻止全表删除 if (!trim($this->where)) { exit('删除条件禁止为空'); } $sql = 'DELETE FROM ' . self::$table . ' ' . $this->where; $this->clear = 1; $this->clear(); return $this->doExec($sql); } /** * 更新方法 * @param array $data * @return mixed|void * @throws \Exception */ public function update(array $data) { //安全考虑,阻止全表更新 if (!trim($this->where)) { exit('更新条件禁止为空'); } $data = $this->dataFormat($data); if (!$data) { return; } $valArr = array(); foreach ($data as $k => $v) { $valArr[] = $k . '=' . $v; } $valStr = implode(',', $valArr); $sql = 'UPDATE ' . trim(self::$table) . ' SET ' . trim($valStr) . ' ' . trim($this->where); return $this->doExec($sql); } /** * 更新语句 字段值加 $number * @param array|string $field * @param null $number * @return mixed|void * @throws \Exception */ public function setInc($field, $number = null) { //安全考虑,阻止全表更新 if (!trim($this->where)) { exit('更新条件禁止为空'); } if (is_array($field)) { $data = $this->dataFormat($field); if (!$data) { return; } $valArr = array(); foreach ($data as $k => $v) { $valArr[] = $k . '=' . $k . '+' . $v; } $valStr = implode(',', $valArr); $sql = 'UPDATE ' . trim(self::$table) . ' SET ' . trim($valStr) . ' ' . trim($this->where); } else if (is_string($field) and is_null($number)) { $sql = 'UPDATE ' . trim(self::$table) . ' SET ' . self::addChar($field) . '=' . self::addChar($field) . '+1 ' . trim($this->where); } else { $sql = 'UPDATE ' . trim(self::$table) . ' SET ' . self::addChar($field) . '=' . self::addChar($field) . '+' . self::addChar($number, 1) . ' ' . trim($this->where); } return $this->doExec($sql); } /** * 更新语句 字段值减 $number * @param array|string $field * @param null $number * @return mixed|void * @throws \Exception */ public function setDec($field, $number = null) { //安全考虑,阻止全表更新 if (!trim($this->where)) { exit('更新条件禁止为空'); } if (is_array($field)) { $data = $this->dataFormat($field); if (!$data) { return; } $valArr = array(); foreach ($data as $k => $v) { $valArr[] = $k . '=' . $k . '-' . $v; } $valStr = implode(',', $valArr); $sql = 'UPDATE ' . trim(self::$table) . ' SET ' . trim($valStr) . ' ' . trim($this->where); } else if (is_string($field) and is_null($number)) { $sql = 'UPDATE ' . trim(self::$table) . ' SET ' . self::addChar($field) . '=' . self::addChar($field) . '-1 ' . trim($this->where); } else { $sql = 'UPDATE ' . trim(self::$table) . ' SET ' . self::addChar($field) . '=' . self::addChar($field) . '-' . self::addChar($number, 1) . ' ' . trim($this->where); } return $this->doExec($sql); } /** * 查询函数 * @return array|boolean 结果集 */ public function select() { $sql = 'SELECT ' . trim($this->field) . ' FROM ' . self::$table . ' ' . trim($this->where) . ' ' . trim($this->order) . ' ' . trim($this->limit) . " " . trim($this->group); $this->clear = 1; $this->clear(); $res = $this->doQuery(trim($sql)); return isset($res[0]) ? $res : []; } /** * 查询函数 * @return array|boolean 结果集 */ public function find() { $sql = "SELECT " . trim($this->field) . " FROM " . self::$table . " " . trim($this->where) . " " . trim($this->order) . " " . trim(" LIMIT 1") . " " . trim($this->group); $this->clear = 1; $this->clear(); return $this->doQuery(trim($sql), false); } /** * 设置条件 * @param string | array $option * @param string $where * @param string $value * @return $this */ public function where($option, $where = null, $value = null) { if ($this->clear > 0) { $this->clear(); } // 判断之前是否使用where语句 if (strpos($this->where, 'WHERE')) { $this->where .= ' AND '; $useWhere = 1; } else { $this->where = ' WHERE '; } // 判断第一个参数为字符 if (is_string($option)) { // 当三个参数都为字符串 if (!is_null($value)): $this->where .= $option . $this->whereTo($where) . $this->addChar($value, 1); endif; // 当第三个参数不存在 if (is_null($value)) { $this->where .= $option . '=' . $this->addChar($where, 1); } else if (is_null($where)) { // 当第二个参数不存在 $this->where .= $option; } } else if (is_array($option)) { // 当第一个参数为数组 $qz = "("; foreach ($option as $v) { // 当第一个参数循环后为数组 if (is_array($v)) { if (strstr($this->where, '(')) { $qz = ' '; } if (isset($useWhere)) { $qz = '('; unset($useWhere); } $condition = $qz . $this->addChar($v[0], 'where') . ' ' . $v[1] . ' ' . $this->addChar($v[2], 1); } else { $this->where .= ' ' . $this->addChar($option[0]) . $option[1] . $option[2]; $cc = 1; break; } $logIc = isset($v[3]) ? ' ' . $v[3] : ' AND'; $this->where .= isset($mark) ? $logIc . $condition : $condition; $mark = 1; } if (!isset($cc)) { $this->where .= ')'; } } return $this; } private function whereTo($field) { switch ($field) { case ($field == 'neq' or $field == '!='): return '!='; case ($field == 'eq' or $field == '='): return '='; case ($field == 'like' or $field == '%'): return 'LIKE'; default: return $field; } } /** * 设置条件 * @param string $option * @param string | array $value * @return $this */ public function whereIn($option, $value, $tj = 'AND') { if ($this->clear > 0) { $this->clear(); } // 判断之前是否使用where语句 if (strpos($this->where, 'WHERE')) { $this->where .= ' ' . $tj . ' '; $useWhere = 1; } else { $this->where = ' WHERE '; } $arr = explode(',', $value); $str = ''; foreach ($arr as $v) { $str .= $this->addChar($v, 1) . ','; } $str = rtrim($str, ','); // 判断第一个参数为字符 if (is_string($value)) { $this->where .= $option . " IN (" . $str . ")"; } else if (is_array($value)) { $str = ''; foreach ($value as $v) { $str .= $this->addChar($v, 1) . ','; } $str = rtrim($str, ','); $this->where .= $option . " IN (" . $str . ")"; } return $this; } /** * 设置条件 * @param $option * @param string $where * @param string $value * @return $this */ public function whereOr($option, $where = null, $value = null) { if ($this->clear > 0) { $this->clear(); } if (strpos($this->where, 'WHERE')) { $this->where .= ' OR '; $ss = 1; } else { $this->where = ' WHERE '; } if (is_string($option)) { if (!is_null($value)): $this->where .= $option . $this->whereTo($where) . $this->addChar($value, 1); endif; if (is_null($value)) { $this->where .= $option . '=' . $this->addChar($where, 1); } else if (is_null($where)) { $this->where .= $option; } } else if (is_array($option)) { $qz = '('; foreach ($option as $v) { $logIc = ' AND'; if (is_array($v)) { if (strstr($this->where, '(')) { $qz = ' '; } if (isset($ss)) { $qz = '('; unset($ss); } $condition = $qz . $this->addChar($v[0]) . ' ' . $v[1] . ' ' . $this->addChar($v[2], 1); } else { $this->where .= ' ' . $this->addChar($option[0]) . $option[1] . $option[2]; $cc = 1; break; } if (isset($v[3]) and ($v[3] == 'or' or $v[3] == 'OR')) { $logIc = ' OR'; $this->where .= isset($mark) ? $logIc . $condition : $condition; } else { $this->where .= isset($mark) ? $logIc . $condition : $condition; } $mark = 1; } if (!isset($cc)) { $this->where .= ')'; } } return $this; } /** * 取得数据表的字段信息 * @return array */ public function tbFields() { $sql = 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME="' . str_replace('`', '', self::$table) . '" AND TABLE_SCHEMA="' . $this->dbName . '"'; $res = $this->doQuery($sql); $ret = array(); foreach ($res as $key => $value) { $ret[] = $value['COLUMN_NAME']; } return $ret; } /** * 过滤并格式化数据表字段 * @param $data * @return array */ public function dataFormat($data) { if (!is_array($data)) { return []; } $table_column = $this->tbFields(); $ret = []; foreach ($data as $key => $val): if (!is_scalar($val)) { continue; } if (array_key_exists($key, $table_column)): $key = $this->addChar($key); if (is_int($val)) { $val = intval($val); } else if (is_float($val)) { $val = floatval($val); } else if (preg_match('/^\(\w*(\+|\-|\*|\/)?\w*\)$/i', $val)) { $val = $val; } elseif (is_string($val)) { $val = '"' . addslashes($val) . '"'; } $ret[$key] = $val; endif; endforeach; return $ret; } /** * 排序 * @param $option * @param null $sort * @return $this */ public function order($option, $sort = null) { if ($this->clear > 0) { $this->clear(); } $this->order = ' ORDER BY '; if (!is_null($sort)) { $this->order .= $option . ' ' . $sort; } else if (is_string($option)) { $this->order .= $option; } else if (is_array($option)) { foreach ($option as $k => $v) { $order = $k . " " . $v; $this->order .= isset($mark) ? ',' . $order : $order; $mark = 1; } } return $this; } /** * @param $option * @return $this */ public function group($option) { if ($this->clear > 0) { $this->clear(); } $this->group = ' GROUP BY ' . $option; return $this; } /** * 分页 * @param $page * @param null $pageSize * @return $this */ public function limit($page, $pageSize = null) { if ($this->clear > 0) { $this->clear(); } if ($pageSize === null) { $this->limit = 'LIMIT ' . $page; } else { $pagEval = intval(($page - 1) * $pageSize); $this->limit = 'LIMIT ' . $pagEval . ',' . $pageSize; } return $this; } /** * 设置字段名 * @param $field * @return $this */ public function field($field) { if ($this->clear > 0) { $this->clear(); } $this->field = $field; return $this; } /** * 查询 左右内全 * @param $tab * @param string $where * @param string $join * @return $this */ public function join($tab, $where = '', $join = 'INNER') { if ($this->clear > 0) { $this->clear(); } if (is_array($tab)) { foreach ($tab as $k => $v): $table = $k . ' AS ' . $v; endforeach; } else { $table = str_replace(' ', ' AS ', $tab); } self::$table .= ' ' . $join . ' JOIN ' . $table . ' ON(' . $where . ')'; return $this; } /** * 计数 * @return mixed */ public function count() { if ($this->clear > 0) { $this->clear(); } $sql = 'SELECT count(' . trim($this->field) . ') AS ' . trim($this->field) . ' FROM ' . self::$table . ' ' . trim($this->where) . " " . trim($this->order); $rows = $this->doQuery($sql, false); return $rows[trim($this->field)]; } /** * 统计 * @return mixed */ public function sum() { if ($this->clear > 0) { $this->clear(); } $sql = 'SELECT SUM(' . trim($this->field) . ') AS ' . trim($this->field) . ' FROM ' . self::$table . ' ' . trim($this->where) . " " . trim($this->order); $rows = $this->doQuery($sql, false); return $rows[trim($this->field)]; } /** * 清理标记函数 */ private function clear() { $this->where = ''; $this->order = ''; $this->limit = ''; $this->field = '*'; $this->clear = 0; } /** * 手动清理标记 * @return $this */ public function clearKey() { $this->clear(); return $this; } /** * 启动事务 */ public function startTrans() { //数据rollback 支持 if (self::$trans == 0) { self::$db->beginTransaction(); } self::$trans++; return; } /** * 用于非自动提交状态下面的查询提交 * @return bool */ public function commit() { $result = true; if (self::$trans > 0) { $result = self::$db->commit(); self::$trans = 0; } return $result; } /** * 事务回滚 * @return bool */ public function rollback() { $result = true; if (self::$trans > 0) { $result = self::$db->rollback(); self::$trans = 0; } return $result; } /** * 关闭连接 */ public function close() { if (!is_null(self::$db)) { self::$db = null; } } }
2021年05月07日
20 阅读
1 评论
0 点赞
2021-04-30
golang实现CC多线程测压
golang实现CC多线程攻击内含exe编译后的文件以及源代码由于需要大量ip 博主特地找了个收费很低的代理网址 一个月最低3.2 (推荐使用每秒更换ip)注册地址套餐选择 套餐4-IP不限量 (推荐使用每秒更换ip)套餐购买后请将你的ip加入网址的白名单即可使用使用方式:./main.exe -h隐藏内容,请前往内页查看详情
2021年04月30日
5 阅读
5 评论
0 点赞
2021-03-19
windows下Navicat15破解详细教程带注册机
windows下Navicat15破解详细教程带注册机去官网下载最新版本下载安装,激活Navicat Premium,下载激活软件, 无需断网运行激活软件 Navicat Keygen Patch v5.6.0(以管理员身份运行)点击Path选择安装Navicat路径下的navicat.exe显示下图则替换成功显示下图,则是以前破解失败残留的文件, 要去软件安装路径下找到下图2个文件删除点击确定,接下来修改注册信息生成注册码点击Generate生成注册码,打开将Navicat将注册码复制进注册框点击激活,选择手动激活将请求码复制到激活软件生成激活码,将生成的激活码复制到软件激活框激活激活成功
2021年03月19日
31 阅读
0 评论
0 点赞
2019-11-04
微营销系统
微营销平台主要用于产品的宣传推广,以订单处理为核心,帮助企业快速构建专属的全渠道营销订单平台。企业通过平台可以全面展示商品,快速引流客户,提升内外部协同效率,数据驱动获取更多利润。您可以在后台自由编辑产品页面,产品内容,业务员,各种订单数据的查看。更好的助力微信产品推广。主要功能1.所有页面都支持自定义编辑,可自定义具体产品商品内容。2.所有订单都支持导出,方便你的发货,快捷上传物流单号。3.各种统计信息一目了然,使你更清楚自己的业务范围。4.支持添加组长与员工,每个组长和员工都有独立后台与链接。5.支持链接,活动,产品,组长员工的无限次添加。6.支持短信发送、物流信息查询,售后订单处理,后台操作简单。7.支持微信支付,支付宝支付,货到付款等多种付款方式。下面为系统案例部分图片展示具体功能请联系博主本人 发测试站体验目前售价 8500 代码开源 包一个月更新 后续更新2000/年更新日志{timeline}{timeline-item color="#19be6b"}2021-8-13增加IfWin支付{/timeline-item}{timeline-item color="#19be6b"}2021-6-23修改金额显示错误修改后台清空缓存失效问题增加后台当前版本号显示增加后台用户入口增加后台手机端入口增加中战支付{/timeline-item}{timeline-item color="#19be6b"}2021-6-22修改短网址的样式增加随机立减金额增加底部订单查询增加所有手机端购买数统计增加后台产品链接{/timeline-item}{timeline-item color="#19be6b"}2021-6-21增加源铭支付优化商品页ui显示{/timeline-item}{timeline-item color="#19be6b"}2021-6-20增加订单导出时间字段优化订单汇总ui显示增加数据统计{/timeline-item}{timeline-item color="#19be6b"}2021-6-18增加产品页客服外链功能{/timeline-item}{timeline-item color="#19be6b"}2021-6-17修复后台售后处理modal弹窗问题更新框架缓存文件以及模板引擎增加异步时更新时间调整广告位{/timeline-item}{timeline-item color="#19be6b"}2021-6-16更新扩展包物流导入组长登入ui显示问题{/timeline-item}{timeline-item color="#19be6b"}2021-6-15更新泛域名解析增加订单导出增加布丁云支付等{/timeline-item}{timeline-item color="#19be6b"}2021-6-8更新短信监控补发等细节性问题{/timeline-item}{timeline-item color="#19be6b"}2021-6-3更新框架核心文件以及依赖包,现支持php8.0 运行速度提高40%{/timeline-item}{timeline-item color="#19be6b"}2021-5-21修复短链等问题增加星云支付更新在线更新接口{/timeline-item}{timeline-item color="#19be6b"}2021-5-20修复后台修改密码问题增加腾讯云存储功能增加用户自定义域{/timeline-item}{timeline-item color="#19be6b"}2021-5-19修复后台短网址修改错误添加授权版本{/timeline-item}{timeline-item color="#19be6b"}2021-4-28增加后台在线更新功能{/timeline-item}{timeline-item color="#19be6b"}2021-4-3从php7.x升级到php8.0{/timeline-item}{timeline-item color="#19be6b"}2021-3-10增加微信公众号支付{/timeline-item}{timeline-item color="#19be6b"}2020-12-10从旧版本完整升级至新版本,新版采用AY框架{/timeline-item}{timeline-item color="#19be6b"}2019-10-1禁止用户重复下单{/timeline-item}{timeline-item color="#19be6b"}2019-9-14增加商品页优惠卷功能{/timeline-item}{timeline-item color="#19be6b"}2019-9-1增加商品页广告功能{/timeline-item}{timeline-item color="#19be6b"}2019-8-10发布微营销系统{/timeline-item}{/timeline}图片预览
2019年11月04日
21 阅读
1 评论
0 点赞
2019-11-04
分享自用支付宝支付类
分享自用的支付宝支付类目前只封装了 订单查询以及app支付更多的支付 后期会集成异步主要商户订单号查询 并确定金额是否与数据库中一致以及判断该商户订单号是否处理过(谨记)个人觉得验签并不重要 有订单查询以及数据库订单验证就行了过多的就不介绍了 源码有注释 <?php /** * @author anderyly * @email admin@aaayun.cc * @link https://blog.aaayun.cc/ * @copyright Copyright (c) 2019 */ class AliPay { // 应用ID public $appId; // 私钥 public $privateKey; // 公钥 public $publicKey; // 异步地址 public $notify_url; // 同步地址 public $return_url; //返回数据格式 public $format = "json"; //版本 public $version = "1.0"; // 编码 public $charset = "UTF-8"; //签名类型 public $signType = "RSA2"; // 配置 public $config; // 网关 public $gatewayUrl = "https://openapi.alipay.com/gateway.do"; public function __construct($appid, $privateKey, $publicKey, $notify_url, $return_url) { $this->appId = $appid; $this->privateKey = $privateKey; $this->publicKey = $publicKey; $this->notify_url = $notify_url; $this->return_url = $return_url; $this->config = [ 'alipay_sdk' => 'alipay-sdk-php-20180705', 'app_id' => $this->appId, 'notify_url' => $this->notify_url, 'sign_type' => $this->signType, 'charset' => $this->charset, 'timestamp' => date('Y-m-d H:i:s', time()), 'version' => $this->version ]; } /** * app支付 * @param $out_trade_no 商户订单号 * @param $total_amount 金额 * @param $subject 标题 * @return arrray */ public function app($out_trade_no, $total_amount, $subject) { $method = 'alipay.trade.app.pay'; $config = $this->config; $config['method'] = $method; $biz = [ 'total_amount' => $total_amount, 'product_code' => 'QUICK_MSECURITY_PAY', 'subject' => $subject, 'out_trade_no' => $out_trade_no ]; $arr = [ 'subject' => $biz['subject'], 'out_trade_no' => $out_trade_no, 'total_amount' => $biz['total_amount'], 'appid' => $this->appId, 'notify_url' => $this->notify_url, 'sign_type' => $this->signType ]; $biz_content = json_encode($biz, JSON_UNESCAPED_UNICODE); $config['biz_content'] = $biz_content; ksort($config); $config['sign'] = $this->sign($config); $params_url = ''; foreach ($config as $k => $v) { if (!empty($v)) { $params_url .= $k . '=' . urlencode($v) . '&'; } } $params_url = rtrim($params_url, '&'); $arr['sign'] = $params_url; return $arr; } /** * 订单查询 * @param $out_trade_no * @param $total_amount * @return bool|arrray */ public function orderQuery($out_trade_no, $total_amount = '') { $method = 'alipay.trade.query'; $config = $this->config; $config['method'] = $method; unset($config['notify_url']); $biz = [ 'out_trade_no' => $out_trade_no ]; $biz_content = json_encode($biz, JSON_UNESCAPED_UNICODE); $config['biz_content'] = $biz_content; ksort($config); $config['sign'] = $this->sign($config); $res = $this->curl($config); $res = json_decode($res, true); $res = $res['alipay_trade_query_response']; if ($res['code'] == 10000 and $res['msg'] == 'Success' and $res['trade_status'] == 'TRADE_SUCCESS') { if (empty($total_amount)) { return $res; } else { if ($res['total_amount'] == $total_amount) { return $res['trade_no']; } else { return false; } } } else { return false; } } /** * curl请求 * @param $data * @return bool|int|mixed|string */ private function curl($data) { @header("Content-type: text/html;charset=gb2312"); $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSLVERSION, 1); curl_setopt($ch, CURLOPT_URL, $this->gatewayUrl); if (is_array($data)) { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); } elseif (is_string($data)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); $content = curl_exec($ch); curl_close($ch); return $content; } /** * 签名 * @param $data * @return bool|string */ private function sign($params) { $stringToBeSigned = ""; $i = 0; foreach ($params as $k => $v) { if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) { if ($i == 0) { $stringToBeSigned .= "$k" . "=" . "$v"; } else { $stringToBeSigned .= "&" . "$k" . "=" . "$v"; } $i++; } } unset ($k, $v); // 私钥处理 $res = "-----BEGIN RSA PRIVATE KEY-----" . PHP_EOL . wordwrap($this->privateKey, 64, PHP_EOL, true) . PHP_EOL . "-----END RSA PRIVATE KEY-----"; ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置'); openssl_sign($stringToBeSigned, $sign, $res, OPENSSL_ALGO_SHA256); $key = base64_encode($sign); return $key; } private function checkEmpty($value) { if (!isset($value)) return true; if ($value === null) return true; if (trim($value) === "") return true; return false; } }
2019年11月04日
7 阅读
0 评论
0 点赞
1
2
3
...
8