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

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

阿沁

問(wèn)題描述

以下代碼中 , 分別在開(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ò)誤');
        }

    }
726 1 0
1個(gè)回答

yuyu

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

  1. 錯(cuò)誤處理和日志記錄

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

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

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

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

    • 在處理支付回調(diào)時(shí),應(yīng)驗(yàn)證回調(diào)數(shù)據(jù)的完整性和真實(shí)性,以防止惡意請(qǐng)求。
    • 對(duì)于敏感操作(如修改用戶余額),應(yīng)確保有相應(yīng)的權(quán)限驗(yàn)證和審計(jì)機(jī)制。

基于以上建議,以下是一個(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)定性。

  • 暫無(wú)評(píng)論
年代過(guò)于久遠(yuǎn),無(wú)法發(fā)表回答
??