以下代碼中 , 分別在開始和結(jié)尾出現(xiàn)了兩次 switch ($paytype) 判斷,如何進行優(yōu)化代碼,只需一次判斷呢
public function returnNotify()
{
$paytype = $this->request->param('paytype');
$config = config('pay');
switch ($paytype) {
case 'wechat':
$pay = Pay::wechat($config);
try {
$res = $pay->callback();
} catch (\Exception $e) {
$this->writeJson(0, '驗簽錯誤');
}
$res = $res->resource;
$res = $res['ciphertext'];
$out_trade_no = $res['out_trade_no'];
$attach = $res['attach'];
$mchid = $res['mchid'];
$transaction_id = $res['transaction_id'];
$openid = $res['payer']['openid'] ?? '';
break;
case 'alipay':
$pay = Pay::alipay($config);
try {
$res = $pay->callback();
} catch (\Exception $e) {
$this->writeJson(0, $e->getMessage());
}
$out_trade_no = $res->out_trade_no;
$attach = $res->passback_params;
break;
case 'balance':
$out_trade_no = $this->request->param('out_trade_no');
$attach = $this->request->param('attach');
break;
default:
$this->writeJson(0, '支付類型錯誤');
}
switch ($attach) {
case 'goods':
$order = GoodsOrders::where(['order_sn' => $out_trade_no, 'status' => 1])->find();
if (!$order) {
$this->writeJson(0, '訂單不存在');
}
$order->status = 2;
$order->paytime = time();
$order->goods->sales += 1;
if (empty($order->user->first_buy_goods_time)) {
//如果是第一次買精品區(qū)
$order->user->first_buy_goods_time = time();
#給自己增加貢獻值
UserService::changeUserGoodsDevote($order->goods->spec->devote, $order->user_id, '購買精品區(qū)商品');
if ($order->user->parent) {
$parent = $order->user->parent;
#給上級反貢獻值
UserService::changeUserGoodsDevote($order->goods->spec->devote, $parent->id, '好友購買精品商品');
}
}
if ($order->user->parent) {
$parent = $order->user->parent;
#給上級反推薦獎
if ($parent->goods_quota > 0) {
$money = $order->pay_amount * 0.2;
if ($money > $parent->goods_quota) {
$money = $parent->goods_quota;
}
UserService::changeUserGoodsQuota(-$money, $parent->id, '精品區(qū)推薦分紅扣除');
UserService::changeUserMoney($money, $parent->id, '精品區(qū)推薦獎');
}
}
#給自己增加精品區(qū)分紅額度
UserService::changeUserGoodsQuota($order->goods->spec->quota, $order->user_id, '購買商品獎勵');
$order->together(['goods', 'user'])->save();
break;
case 'super':
$order = SuperOrders::where(['order_sn' => $out_trade_no, 'status' => 1])->find();
if (!$order) {
$this->writeJson(0, '訂單不存在');
}
$supply_price = 0;
foreach ($order->detail as $item) {
$supply_price += $item->spec->supply_price;
}
$order->status = 2;
$order->supply_price = $supply_price;
$order->paytime = time();
#給自己增加優(yōu)選區(qū)分紅額度
UserService::changeUserSuperQuota($order->pay_amount * 1.3, $order->user_id, '購買優(yōu)選商品');
if ($order->user->parent) {
$parent = $order->user->parent;
#給上級反推薦獎
UserService::changeUserMoney($order->pay_amount * 0.01, $parent->id, '優(yōu)選區(qū)推薦獎');
}
$order->save();
break;
case 'shop':
$order = ShopOrders::where(['order_sn' => $out_trade_no, 'status' => 1])->find();
if (!$order) {
$this->writeJson(0, '訂單不存在');
}
$order->status = 2;
$order->paytime = time();
$order->save();
#給自己增加消費者分紅額度
UserService::changeUserShopQuota($order->pay_amount * 4, $order->user_id, '購買商家優(yōu)惠券');
#增加商家分紅額度
UserService::changeUserShopQuota($order->pay_amount, $order->shop->user_id, '用戶購買優(yōu)惠券');
#5%分給 商家推廣員
UserService::changeUserMoney($order->pay_amount * 0.05, $order->shop->parent->id, '推廣員商鋪訂單提成');
#5%分給 推薦人
UserService::changeUserMoney($order->pay_amount * 0.05, $order->shop->user->parent->id, '下級商鋪訂單提成');
break;
case 'recharge':
$order = UserRechargeLog::where(['order_sn' => $out_trade_no, 'status' => 1])->find();
$order->status = 2;
$order->paytime = time();
$order->save();
#增加余額
UserService::changeUserMoney($order->amount, $order->user_id, ($paytype == 'alipay' ? '支付寶' : '微信') . '充值余額');
break;
default:
$this->writeJson(0, '回調(diào)錯誤');
}
switch ($paytype) {
case 'wechat':
if ($order->pay_source == 'wechatmini') {
#默認發(fā)貨
Platform::uploadShippingInfo($transaction_id, $mchid, $openid);
}
$pay->success();
break;
case 'alipay':
$pay->success();
break;
case 'balance':
return true;
default:
$this->writeJson(0, '支付類型錯誤');
}
}
AI的回答
您的代碼看起來是一個處理支付通知回調(diào)的函數(shù),它根據(jù)支付類型和附加信息執(zhí)行不同的操作。為了優(yōu)化這段代碼,我們可以從以下幾個方面進行改進:
錯誤處理和日志記錄:
代碼復(fù)用:
數(shù)據(jù)庫操作優(yōu)化:
代碼結(jié)構(gòu)和可讀性:
安全性:
基于以上建議,以下是一個簡化后的代碼示例,僅作為優(yōu)化思路的參考:
public function returnNotify()
{
$paytype = $this->request->param('paytype');
$config = config('pay');
$attach = $this->getAttachData($paytype); // 提取附加數(shù)據(jù)
if (!$attach) {
$this->writeJson(0, '回調(diào)錯誤');
return;
}
$order = $this->getOrderByAttach($attach); // 根據(jù)附加數(shù)據(jù)獲取訂單
if (!$order) {
$this->writeJson(0, '訂單不存在');
return;
}
$this->updateOrderStatus($order, $paytype); // 更新訂單狀態(tài)
$this->handleUserQuotaAndMoney($order, $attach); // 處理用戶分紅和余額
$this->handlePaySuccess($paytype); // 處理支付成功邏輯
return true;
}
private function getAttachData($paytype)
{
// 根據(jù)支付類型提取附加數(shù)據(jù)邏輯...
}
private function getOrderByAttach($attach)
{
// 根據(jù)附加數(shù)據(jù)獲取訂單邏輯...
}
private function updateOrderStatus($order, $paytype)
{
// 更新訂單狀態(tài)邏輯...
}
private function handleUserQuotaAndMoney($order, $attach)
{
// 處理用戶分紅和余額邏輯...
}
private function handlePaySuccess($paytype)
{
// 處理支付成功邏輯...
}
請注意,上述代碼僅為示例,并未包含具體的實現(xiàn)細節(jié)。在實際優(yōu)化過程中,您需要根據(jù)您的業(yè)務(wù)邏輯和框架特性進行具體的調(diào)整和優(yōu)化。同時,確保在修改代碼后進行充分的測試,以確保功能的正確性和穩(wěn)定性。