160 行
5.2 KiB
PHP
可执行文件
160 行
5.2 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace Common\Business;
|
|
|
|
use Addons\Pay\WxPayV3;
|
|
|
|
//[1.5.52]
|
|
|
|
/**
|
|
* 上传至微信
|
|
* Class WxUpload
|
|
* [
|
|
* 上传图片到微信 wxUpload
|
|
* 上传图片至本地 localUpload
|
|
* 图片上传验证 uploadVerify
|
|
*
|
|
* ]
|
|
* @package Common\Business
|
|
*/
|
|
class WxUpload
|
|
{
|
|
/**
|
|
* 上传图片到微信
|
|
* @param $param
|
|
* [
|
|
* image 图片信息
|
|
* mch_id 商户号
|
|
* serial_no 整数序列号
|
|
* apiclient_key 私钥地址
|
|
* user_type 1普通模式 2服务商模式
|
|
* ]
|
|
* @return array
|
|
*/
|
|
public function wxUpload($param)
|
|
{
|
|
$verify = $this->uploadVerify($param);
|
|
if ($verify['status'] != 1) {
|
|
return $verify;
|
|
}
|
|
$image_path = $this->localUpload('./Uploads/Merchant/');
|
|
if (!$image_path) {
|
|
return callBack(statusCode()['error'], "上传失败");
|
|
}
|
|
switch ($param['user_type']) {
|
|
case 1:
|
|
$path = $image_path['label_img']['savepath'] . $image_path['label_img']['savename'];
|
|
$filename = $image_path['label_img']['savename'];
|
|
break;
|
|
case 2:
|
|
$path = $image_path['label_img_service']['savepath'] . $image_path['label_img_service']['savename'];
|
|
$filename = $image_path['label_img_service']['savename'];
|
|
break;
|
|
}
|
|
|
|
$PSize = filesize($path);
|
|
$binary = fread(fopen($path, "r"), $PSize);
|
|
$sign = hash('sha256', $binary);
|
|
$boundary = 'coupon_team';
|
|
$image_type = explode('/', $param['image']['type']);
|
|
$data = [
|
|
'boundary' => $boundary,
|
|
'mch_id' => $param['mch_id'],
|
|
'image_type' => "image/" . $image_type[1],
|
|
'serial_no' => $param['serial_no'],
|
|
'apiclient_key' => $param['apiclient_key'],
|
|
'data' => [
|
|
'file' => $binary,
|
|
'meta' => [
|
|
'filename' => $filename,
|
|
'sha256' => $sign
|
|
],
|
|
],
|
|
];
|
|
$WxPayV3Class = new WxPayV3($data);
|
|
$result = $WxPayV3Class->upload();
|
|
$result = json_decode($result, true);
|
|
if (!empty($result['media_url'])) {
|
|
return callBack(statusCode()['success'], "上传成功", ['media_url' => $result['media_url'], 'image_path' => $path]);
|
|
} else {
|
|
return callBack(statusCode()['success'], $result['message']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上传图片至本地
|
|
* @param $path 路径
|
|
* @return array|bool
|
|
*/
|
|
public function localUpload($path)
|
|
{
|
|
ini_set('max_execution_time', '0');
|
|
if (!is_dir($path)) {
|
|
mkdir($path, 0777, true);
|
|
}
|
|
// 上传文件配置
|
|
$config = array(
|
|
'maxSize' => 1048576, // 上传文件最大为2M
|
|
'rootPath' => './', // 文件上传保存的根路径
|
|
'savePath' => $path, // 文件上传的保存路径(相对于根路径)
|
|
'saveName' => array('uniqid', ''), // 上传文件的保存规则,支持数组和字符串方式定义
|
|
'autoSub' => true, // 自动使用子目录保存上传文件 默认为true
|
|
'exts' => array('jpg', '', 'png', 'jpeg', 'bmp'),
|
|
);
|
|
// 实例化上传
|
|
$upload = new \Think\Upload($config);
|
|
// 调用上传方法
|
|
$info = $upload->upload();
|
|
return $info;
|
|
}
|
|
|
|
/**
|
|
* 图片上传验证
|
|
* @param $param
|
|
* [
|
|
* image 图片信息
|
|
* mch_id 商户号
|
|
* serial_no 整数序列号
|
|
* apiclient_key 私钥地址
|
|
* ]
|
|
* @return mixed
|
|
*/
|
|
public function uploadVerify($param)
|
|
{
|
|
$image = $param['image'];
|
|
if (empty($_FILES) || $image['error'] != 0) {
|
|
return callBack(statusCode()['error'], "请上传图片");
|
|
}
|
|
$arr = ['image/jpg', 'image/jpeg', 'image/png', 'image/bmp'];
|
|
if (!in_array($image['type'], $arr)) {
|
|
return callBack(statusCode()['error'], "请上传正确的图片");
|
|
}
|
|
if ($image['size'] > 1048576) {
|
|
return callBack(statusCode()['error'], "图片不能超过1M");
|
|
}
|
|
if (empty($param['mch_id']) || empty($param['serial_no']) || empty($param['apiclient_key'])) {
|
|
return callBack(statusCode()['error'], "参数缺失");
|
|
}
|
|
return callBack(statusCode()['success'], "验证通过");
|
|
}
|
|
|
|
|
|
/**
|
|
* 发出请求
|
|
* @return bool|string
|
|
*/
|
|
public function curl($url, $arr)
|
|
{
|
|
$curl = curl_init();
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
//不需要验证ssl证书
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
//设置获取的信息以文件流的形式返回,而不是直接输出
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $arr);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
$output = curl_exec($curl);
|
|
return $output;
|
|
}
|
|
} |