国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

像大家寫代碼經(jīng)常遇到這種問題都是怎么解決的呢

阿沁

問題描述

以下代碼中 , 分別在開始和結(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, '支付類型錯誤');
        }

    }
773 1 0
1個回答

yuyu

AI的回答
您的代碼看起來是一個處理支付通知回調(diào)的函數(shù),它根據(jù)支付類型和附加信息執(zhí)行不同的操作。為了優(yōu)化這段代碼,我們可以從以下幾個方面進行改進:

  1. 錯誤處理和日志記錄

    • 當(dāng)前的錯誤處理只是簡單地返回了錯誤信息,但沒有記錄日志。在生產(chǎn)環(huán)境中,記錄日志對于排查問題至關(guān)重要。
    • 可以考慮使用統(tǒng)一的錯誤處理機制,如自定義異常類,以便更好地管理和記錄錯誤信息。
  2. 代碼復(fù)用

    • 代碼中多次使用了類似的查詢和更新邏輯,例如查詢訂單、更新訂單狀態(tài)等。這些操作可以封裝成單獨的函數(shù)或方法,以減少代碼重復(fù)。
  3. 數(shù)據(jù)庫操作優(yōu)化

    • 在更新訂單狀態(tài)時,可以考慮使用數(shù)據(jù)庫事務(wù)來確保數(shù)據(jù)的一致性。
    • 如果訂單查詢和更新操作頻繁,可以考慮使用緩存來減少數(shù)據(jù)庫訪問次數(shù)。
  4. 代碼結(jié)構(gòu)和可讀性

    • 當(dāng)前的代碼結(jié)構(gòu)較為冗長,可以通過提取方法和使用更清晰的變量名來提高可讀性。
    • 盡量避免在代碼中直接使用硬編碼的字符串或數(shù)字,而是使用常量或配置文件來管理這些值。
  5. 安全性

    • 在處理支付回調(diào)時,應(yīng)驗證回調(diào)數(shù)據(jù)的完整性和真實性,以防止惡意請求。
    • 對于敏感操作(如修改用戶余額),應(yīng)確保有相應(yīng)的權(quá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)錯誤');
        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)定性。

  • 暫無評論
年代過于久遠,無法發(fā)表回答
??