Appearance
服务端图片上传逻辑(PHP)
$file = request()->file('file');
if($file){
$info = $file->validate(['size'=> 512000,'ext'=>'jpg,png,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
$data = [
'src' => request()->domain() . DS . 'uploads' . DS . $info->getSaveName(),
'title' => $info->getFilename(),
];
return json(['code' => 0, 'status' => true, 'msg' => '', 'data' => $data]);
}
}
return json(['status' => false, 'msg' => $file->getError()]);
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
tp5.0 上传文件到Oss
- Composer 安装OSS,项目目录下执行命令
composer require aliyuncs/oss-sdk-php
1
- 默认存储目录
项目名称/vender/
1
- 控制器中引用
use OSS\OssClient;
1
- 文件上传,OSS各参数阿里云后台获取
$ossClient = new OssClient($this->aliOos['KeyId'], $this->aliOos['KeySecret'], $this->aliOos['Endpoint']);
$file = request()->file('file');
if($file){
$file_name = $file->getInfo('name');
$file_ext = $this->getExt($file_name);
$file_path = '目录' . DIRECTORY_SEPARATOR . '自定义目录(没有会自动创建)' . DIRECTORY_SEPARATOR . date('Ymd') . DIRECTORY_SEPARATOR . time() . rand(10000000, 99999999) . '.' . $file_ext;
$imgpath = str_replace('\\', '/', $file_path);
$url = $ossClient->uploadFile($this->aliOos['Bucket'], $imgpath, $_FILES['file']['tmp_name']);
if ($url['info']) {
// oss网址替换已绑定域名
$ossImgUrl = str_replace('http://***.aliyuncs.com/', 'http://***erha.love/', $url['info']['url']);
return ['status' => true, 'code' => 200, 'msg'=>'上传成功', 'local_data' => $ossImgUrl];
}
}
// 上传失败获取错误信息
return ['status' => false, 'code' => 500, 'msg'=>'上传失败', 'local_data' => '', 'error_msg' => $file->getError()];
// 获取文件后缀
private function getExt($filename)
{
$arr = explode('.', $filename);
return array_pop($arr);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
tp5.0 自带分页函数paginate()
- 查询状态为1的用户数据 并且每页显示10条数据
# 保持地址栏携带的参数在点击分页时不被清空
['query' => request()->param()]
$lists = Db::name('user')->where('status',1)->paginate(10, false, ['query' => request()->param()]);
1
2
3
2
3
- 模板文件中分页及总条数输出代码如下
# 分页
{$lists->render()}
# 总条数
{$lists->total()}
1
2
3
4
2
3
4
tp5.0 extra文件夹 文件数据获取
- 在application/extra下的.php文件,控制器中可用助手函数config('文件名')获取内容
config('文件名')
1
thinkphp5.0 json()和原生json_encode()
- 按需所用
thinkphp5.0 自带的json()函数返回数据类型是json对象。
PHP原生的json_encode()返回的数据类型是json字符串。
1
2
2
微信JSAPI网页支付 商户支付应答(v3和v2)
非SDK接入的请求,判断版本是v3还是v2版看api的请求url,v3会专门标明是v3,未说明一般是v2版
v3(格式为json,接口最下面有说明)
https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtmlv2(格式为xml,接口最下面有说明)
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8
微信JSAPI网页支付V3(URL请求版)大致流程
该流程为V3版,V2版为
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_4产品介绍
https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter1_1_1.shtml接入前准备(支付所需参数申请,商户自行申请,需营业执照等资料,只支持企业,不支持个人申请),用到的参数
APPID(公众号或服务号ID)、mchid(商户号)、APIKey(微信商户API密钥)、API密钥(公众号或服务号密钥)
1
官方详细介绍
https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_1.shtml需绑定支付的域名及其他指引
https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_3.shtmlAPI列表介绍
https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_4.shtml网页授权获取用户openId
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html统一下单(统一下单前需先微信授权,以获取用户唯一值openid,同一微信用户用不同公众号或服务号ID获取的openid会不同)
https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml查询订单
https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtmlJSAPI调起API(先查询订单是否存在)
https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_4.shtml支付结果
https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml