发货及后台文件上传优化
This commit is contained in:
@@ -4,9 +4,8 @@ namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\exception\UploadException;
|
||||
use app\common\library\OssUpload;
|
||||
use app\common\library\Upload;
|
||||
use app\common\service\OssService;
|
||||
use fast\Random;
|
||||
use think\addons\Service;
|
||||
use think\Cache;
|
||||
use think\Config;
|
||||
@@ -143,7 +142,6 @@ class Ajax extends Backend
|
||||
|
||||
//必须还原upload配置,否则分片及cdnurl函数计算错误
|
||||
Config::load(APP_PATH . 'extra/upload.php', 'upload');
|
||||
$ossService = OssService::instance();
|
||||
$chunkid = $this->request->post("chunkid");
|
||||
if ($chunkid) {
|
||||
if (!Config::get('upload.chunking')) {
|
||||
@@ -158,15 +156,12 @@ class Ajax extends Backend
|
||||
$attachment = null;
|
||||
//合并分片文件
|
||||
try {
|
||||
$upload = new Upload();
|
||||
$upload = new OssUpload();
|
||||
$attachment = $upload->merge($chunkid, $chunkcount, $filename);
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
// 上传到OSS
|
||||
$objectUrl = 'uploads/' . date('Ymd/') . Random::alnum(16) . '/' . $attachment->filename;
|
||||
$this->uploadToOss($attachment, $ossService, $objectUrl);
|
||||
$this->success(__('Uploaded successful'), '', ['url' => cdnurl('/' . $objectUrl), 'fullurl' => cdnurl('/' . $objectUrl)]);
|
||||
$this->success(__('Uploaded successful'), '', ['url' => cdnurl($attachment->url), 'fullurl' => cdnurl($attachment->url)]);
|
||||
} elseif ($method == 'clean') {
|
||||
//删除冗余的分片文件
|
||||
try {
|
||||
@@ -193,15 +188,12 @@ class Ajax extends Backend
|
||||
//默认普通上传文件
|
||||
$file = $this->request->file('file');
|
||||
try {
|
||||
$upload = new Upload($file);
|
||||
$upload = new OssUpload($file);
|
||||
$attachment = $upload->upload();
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
// 上传到OSS
|
||||
$objectUrl = 'uploads/' . date('Ymd/') . Random::alnum(16) . '/' . $attachment->filename;
|
||||
$this->uploadToOss($attachment, $ossService, $objectUrl);
|
||||
$this->success(__('Uploaded successful'), '', ['url' => cdnurl('/' . $objectUrl), 'fullurl' => cdnurl('/' . $objectUrl)]);
|
||||
$this->success(__('Uploaded successful'), '', ['url' => cdnurl($attachment->url), 'fullurl' => cdnurl($attachment->url)]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -398,19 +390,4 @@ class Ajax extends Backend
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attachment
|
||||
* @param OssService $ossService
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
protected function uploadToOss($attachment, OssService $ossService, $objectUrl): string
|
||||
{
|
||||
$filePath = ROOT_PATH . 'public' . str_replace('/', DS, $attachment->url);
|
||||
$url = $ossService->uploadFile($filePath, $objectUrl);
|
||||
@unlink($filePath);
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -176,7 +176,17 @@ class Order extends Api
|
||||
'limitGoodsList' => null,
|
||||
'abnormalDeliveryGoodsList' => null,
|
||||
'invoiceSupport' => 1,
|
||||
// 订单类型:未传 orderNo 为新建软件订单;带 orderNo 为已有订单(需求首付款/尾款或软件订单重新支付)
|
||||
'orderType' => OrderService::ORDER_TYPE_SOFTWARE,
|
||||
];
|
||||
$orderNo = $this->request->post('orderNo', '');
|
||||
if ($orderNo) {
|
||||
$order = OrderService::getOne(['order_no' => $orderNo, 'user_id' => $this->auth->id]);
|
||||
if (!$order) {
|
||||
$this->error('订单不存在');
|
||||
}
|
||||
$resp['orderType'] = (int)$order->order_type;
|
||||
}
|
||||
$this->success('', $resp);
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof HttpResponseException) {
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\library;
|
||||
|
||||
use app\common\exception\UploadException;
|
||||
use app\common\model\Attachment;
|
||||
use app\common\service\OssService;
|
||||
use fast\Random;
|
||||
use think\File;
|
||||
use think\Hook;
|
||||
|
||||
/**
|
||||
* OSS 文件上传
|
||||
* 本地只落地到 runtime/temp(PHP 运行用户可写,不依赖 public/uploads 的写权限),
|
||||
* 上传到 OSS 后删除临时文件,附件记录保存 OSS 路径
|
||||
*/
|
||||
class OssUpload extends Upload
|
||||
{
|
||||
/**
|
||||
* 上传到 OSS(普通上传与分片合并共用)
|
||||
* @param string|null $savekey 未使用,OSS 路径固定为 uploads/{Ymd}/{随机串}/{原文件名}
|
||||
* @return \app\common\model\attachment|\think\Model
|
||||
* @throws UploadException
|
||||
*/
|
||||
public function upload($savekey = null)
|
||||
{
|
||||
if (empty($this->file)) {
|
||||
throw new UploadException(__('No file upload or server upload limit exceeded'));
|
||||
}
|
||||
|
||||
$this->checkSize();
|
||||
$this->checkExecutable();
|
||||
$this->checkMimetype();
|
||||
$this->checkImage();
|
||||
|
||||
$tempDir = RUNTIME_PATH . 'temp' . DS;
|
||||
if (!is_dir($tempDir) && !@mkdir($tempDir, 0775, true) && !is_dir($tempDir)) {
|
||||
throw new UploadException('上传临时目录创建失败或无写权限:' . $tempDir);
|
||||
}
|
||||
$tempName = Random::alnum(16) . '.' . $this->fileInfo['suffix'];
|
||||
$tempFile = $tempDir . $tempName;
|
||||
|
||||
$sha1 = $this->file->hash();
|
||||
|
||||
if ($this->merging) {
|
||||
if (!$this->file->check()) {
|
||||
throw new UploadException($this->file->getError());
|
||||
}
|
||||
$sourceFile = $this->file->getRealPath() ?: $this->file->getPathname();
|
||||
$info = $this->file->getInfo();
|
||||
$this->file = null;
|
||||
if (!@rename($sourceFile, $tempFile)) {
|
||||
throw new UploadException('分片文件合并失败');
|
||||
}
|
||||
$file = new File($tempFile);
|
||||
$file->setSaveName($tempName)->setUploadInfo($info);
|
||||
} else {
|
||||
$file = $this->file->move($tempDir, $tempName);
|
||||
if (!$file) {
|
||||
throw new UploadException($this->file->getError());
|
||||
}
|
||||
}
|
||||
$this->file = $file;
|
||||
|
||||
$filename = mb_substr(htmlspecialchars(strip_tags($this->fileInfo['name'])), 0, 100);
|
||||
$object = 'uploads/' . date('Ymd/') . Random::alnum(16) . '/' . $filename;
|
||||
try {
|
||||
OssService::instance()->uploadFile($tempFile, $object);
|
||||
} catch (\Exception $e) {
|
||||
throw new UploadException($e->getMessage());
|
||||
} finally {
|
||||
@unlink($tempFile);
|
||||
}
|
||||
|
||||
$category = request()->post('category');
|
||||
$category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : '';
|
||||
$auth = Auth::instance();
|
||||
$params = [
|
||||
'admin_id' => (int)session('admin.id'),
|
||||
'user_id' => (int)$auth->id,
|
||||
'filename' => $filename,
|
||||
'category' => $category,
|
||||
'filesize' => $this->fileInfo['size'],
|
||||
'imagewidth' => $this->fileInfo['imagewidth'],
|
||||
'imageheight' => $this->fileInfo['imageheight'],
|
||||
'imagetype' => $this->fileInfo['suffix'],
|
||||
'imageframes' => 0,
|
||||
'mimetype' => $this->fileInfo['type'],
|
||||
'url' => '/' . $object,
|
||||
'uploadtime' => time(),
|
||||
'storage' => 'oss',
|
||||
'sha1' => $sha1,
|
||||
'extparam' => '',
|
||||
];
|
||||
$attachment = new Attachment();
|
||||
$attachment->data(array_filter($params));
|
||||
$attachment->save();
|
||||
|
||||
Hook::listen('upload_after', $attachment);
|
||||
return $attachment;
|
||||
}
|
||||
}
|
||||
@@ -658,8 +658,12 @@ class OrderService
|
||||
*/
|
||||
public static function orderShip($id)
|
||||
{
|
||||
Db::transaction(function () use ($id) {
|
||||
$order = OrderService::getOne(['id' => $id], ['orderGoods']);
|
||||
$order = Db::transaction(function () use ($id) {
|
||||
//加锁读取,防止并发重复发货、重复分配激活码
|
||||
$order = OrderService::where(['id' => $id])->lock(true)->find();
|
||||
if (!$order) {
|
||||
exception('订单不存在');
|
||||
}
|
||||
if ($order->order_status != OrderService::ORDER_STATUS_WAIT_SHIP) {
|
||||
exception('订单状态异常,不能发货');
|
||||
}
|
||||
@@ -673,8 +677,15 @@ class OrderService
|
||||
if (!$saveRes) {
|
||||
exception('更新发货状态失败');
|
||||
}
|
||||
//发送订单发货通知
|
||||
|
||||
return $order;
|
||||
});
|
||||
|
||||
//发送订单发货通知(事务提交后发送,失败只记日志,不影响发货结果)
|
||||
try {
|
||||
$msgData = [
|
||||
'page' => SubscribeMessageService::PAGE_ORDER_DETAIL,
|
||||
'param' => ['orderNo' => $order->order_no],
|
||||
'values' => [
|
||||
$order->order_no,
|
||||
mb_substr($order->orderGoods[0]->goods_name, 0, 20),
|
||||
@@ -683,7 +694,12 @@ class OrderService
|
||||
]
|
||||
];
|
||||
(new EasyWeChatService())->sendMsg($order->user_id, SubscribeMessageService::MSG_TEMP_ORDER_SHIPPED, $msgData);
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
Log::error(__CLASS__ . ' 订单发货通知发送失败:' . json_encode([
|
||||
'order_id' => $id,
|
||||
'error' => $e->getMessage(),
|
||||
], JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,6 +20,7 @@ class SubscribeMessageService
|
||||
const PAGE_DEMAND_LIST = 'pages/demand/list/index';
|
||||
const PAGE_DEMAND_DETAIL = 'pages/demand/detail/index';
|
||||
const PAGE_ORDER_LIST = 'pages/order/order-list/index';
|
||||
const PAGE_ORDER_DETAIL = 'pages/order/order-detail/index';
|
||||
const PAGE_HOME = 'pages/home/home';
|
||||
|
||||
const MSG_MAP = [
|
||||
|
||||
@@ -4,6 +4,10 @@ import { commitPay, wechatPayOrder } from './pay';
|
||||
import { getAddressPromise } from '../../usercenter/address/list/util';
|
||||
|
||||
const stripeImg = `https://cdn-we-retail.ym.tencent.com/miniapp/order/stripe.png`;
|
||||
// 与后端 OrderService::ORDER_TYPE_SOFTWARE 一致
|
||||
const ORDER_TYPE_SOFTWARE = 0;
|
||||
// 订单发货通知模板,与后端 SubscribeMessageService::MSG_TEMP_ORDER_SHIPPED 一致
|
||||
const MSG_TEMP_ORDER_SHIPPED = 'jGxNQ11on1yD0d5tGwk4S8jP_CEdddfAkrp9-xVeeZY';
|
||||
|
||||
Page({
|
||||
data: {
|
||||
@@ -73,8 +77,8 @@ Page({
|
||||
},
|
||||
// 处理不同情况下跳转到结算页时需要的参数
|
||||
handleOptionsParams(options, couponList) {
|
||||
console.log(options);
|
||||
let orderNo = options?.orderNo;
|
||||
//选优惠券、改数量等重新结算时不会再传 orderNo,沿用首次进入时的订单号
|
||||
const orderNo = options?.orderNo || this.data.orderNo;
|
||||
this.setData({
|
||||
orderNo: orderNo
|
||||
});
|
||||
@@ -112,6 +116,7 @@ Page({
|
||||
storeInfoList,
|
||||
userAddressReq,
|
||||
couponList,
|
||||
orderNo,
|
||||
};
|
||||
fetchSettleDetail(params).then(
|
||||
(res) => {
|
||||
@@ -360,6 +365,23 @@ Page({
|
||||
},
|
||||
// 提交订单
|
||||
submitOrder() {
|
||||
const { settleDetailData } = this.data;
|
||||
if (this.payLock || !settleDetailData.settleType || !settleDetailData.totalAmount) {
|
||||
return;
|
||||
}
|
||||
// 软件订单需授权「订单发货」通知;requestSubscribeMessage 必须在点击事件中同步调用,不能放到接口回调里
|
||||
if (settleDetailData.orderType === ORDER_TYPE_SOFTWARE) {
|
||||
wx.requestSubscribeMessage({
|
||||
tmplIds: [MSG_TEMP_ORDER_SHIPPED],
|
||||
complete: () => {
|
||||
this.doSubmitOrder();
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.doSubmitOrder();
|
||||
},
|
||||
doSubmitOrder() {
|
||||
const {
|
||||
settleDetailData,
|
||||
// userAddressReq,
|
||||
@@ -429,16 +451,6 @@ Page({
|
||||
});
|
||||
return;
|
||||
}
|
||||
//订阅发货通知
|
||||
if (data.order_type == 0) {
|
||||
wx.requestSubscribeMessage({
|
||||
tmplIds: ['jGxNQ11on1yD0d5tGwk4S8jP_CEdddfAkrp9-xVeeZY'],
|
||||
complete: (re) => {
|
||||
this.handlePay(data, settleDetailData);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.handlePay(data, settleDetailData);
|
||||
} else {
|
||||
Toast({
|
||||
|
||||
Reference in New Issue
Block a user