以下代碼中 , 分別在開(kāi)始和結(jié)尾出現(xiàn)了兩次 switch ($paytype) 判斷,如何進(jìn)行優(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, '驗(yàn)簽錯(cuò)誤');
}
$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, '支付類(lèi)型錯(cuò)誤');
}
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)) {
//如果是第一次買(mǎi)精品區(qū)
$order->user->first_buy_goods_time = time();
#給自己增加貢獻(xiàn)值
UserService::changeUserGoodsDevote($order->goods->spec->devote, $order->user_id, '購(gòu)買(mǎi)精品區(qū)商品');
if ($order->user->parent) {
$parent = $order->user->parent;
#給上級(jí)反貢獻(xiàn)值
UserService::changeUserGoodsDevote($order->goods->spec->devote, $parent->id, '好友購(gòu)買(mǎi)精品商品');
}
}
if ($order->user->parent) {
$parent = $order->user->parent;
#給上級(jí)反推薦獎(jiǎng)
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ū)推薦獎(jiǎng)');
}
}
#給自己增加精品區(qū)分紅額度
UserService::changeUserGoodsQuota($order->goods->spec->quota, $order->user_id, '購(gòu)買(mǎi)商品獎(jiǎng)勵(lì)');
$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, '購(gòu)買(mǎi)優(yōu)選商品');
if ($order->user->parent) {
$parent = $order->user->parent;
#給上級(jí)反推薦獎(jiǎng)
UserService::changeUserMoney($order->pay_amount * 0.01, $parent->id, '優(yōu)選區(qū)推薦獎(jiǎng)');
}
$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();
#給自己增加消費(fèi)者分紅額度
UserService::changeUserShopQuota($order->pay_amount * 4, $order->user_id, '購(gòu)買(mǎi)商家優(yōu)惠券');
#增加商家分紅額度
UserService::changeUserShopQuota($order->pay_amount, $order->shop->user_id, '用戶購(gòu)買(mǎi)優(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, '下級(jí)商鋪訂單提成');
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)錯(cuò)誤');
}
switch ($paytype) {
case 'wechat':
if ($order->pay_source == 'wechatmini') {
#默認(rèn)發(fā)貨
Platform::uploadShippingInfo($transaction_id, $mchid, $openid);
}
$pay->success();
break;
case 'alipay':
$pay->success();
break;
case 'balance':
return true;
default:
$this->writeJson(0, '支付類(lèi)型錯(cuò)誤');
}
}
AI的回答
您的代碼看起來(lái)是一個(gè)處理支付通知回調(diào)的函數(shù),它根據(jù)支付類(lèi)型和附加信息執(zhí)行不同的操作。為了優(yōu)化這段代碼,我們可以從以下幾個(gè)方面進(jìn)行改進(jìn):
錯(cuò)誤處理和日志記錄:
代碼復(fù)用:
數(shù)據(jù)庫(kù)操作優(yōu)化:
代碼結(jié)構(gòu)和可讀性:
安全性:
基于以上建議,以下是一個(gè)簡(jiǎn)化后的代碼示例,僅作為優(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)錯(cuò)誤');
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ù)支付類(lèi)型提取附加數(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)
{
// 處理支付成功邏輯...
}
請(qǐng)注意,上述代碼僅為示例,并未包含具體的實(shí)現(xiàn)細(xì)節(jié)。在實(shí)際優(yōu)化過(guò)程中,您需要根據(jù)您的業(yè)務(wù)邏輯和框架特性進(jìn)行具體的調(diào)整和優(yōu)化。同時(shí),確保在修改代碼后進(jìn)行充分的測(cè)試,以確保功能的正確性和穩(wěn)定性。