From 35e2d9d62118a3a28ed28455b9e6fc97ab6c40c3 Mon Sep 17 00:00:00 2001 From: Wayne Date: Fri, 30 Jun 2023 17:24:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E9=8A=B7=E8=A8=88=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/adminapi/controller/v1/Order.php | 7 + app/appapi/controller/v1/Payment.php | 547 +++++++++++--------------- app/appapi/controller/v1/Shipping.php | 42 +- app/appapi/controller/v1/Test.php | 46 +-- app/appapi/controller/v1/User.php | 25 +- app/common/Affiliate.php | 65 +++ public/adm/includes/inc_menu.php | 2 +- 7 files changed, 345 insertions(+), 389 deletions(-) create mode 100644 app/common/Affiliate.php diff --git a/app/adminapi/controller/v1/Order.php b/app/adminapi/controller/v1/Order.php index 591f9d58..2688663f 100644 --- a/app/adminapi/controller/v1/Order.php +++ b/app/adminapi/controller/v1/Order.php @@ -483,6 +483,13 @@ class Order extends ApiController return $this->Error('更新失敗'); } } elseif ($params['op'] == 'finish') { + //訂單完成,計算分銷 + $res = \app\common\Affiliate::calculateProfitShare($order); + + if ($res['code'] > 0) { + return $this->Error('計算分銷失敗,請重新操作'); + } + $arr['order_id'] = $order['order_id']; $arr['order_status'] = OS_FINISHED; $arr['pay_status'] = PS_PAYED; diff --git a/app/appapi/controller/v1/Payment.php b/app/appapi/controller/v1/Payment.php index bd27019b..6956d159 100644 --- a/app/appapi/controller/v1/Payment.php +++ b/app/appapi/controller/v1/Payment.php @@ -1,310 +1,237 @@ -where('enabled', 1) - ->select(); - - - if (!$res) { - return $this->Error('錯誤請求'); - } - - return $this->Success($res); - } - - public function process() - { - //從order info 取出訂單資料 - $rtn = Db::name('order_info') - ->where('order_sn', input('order_sn')) - ->find(); - - if (!$rtn) { - return $this->Error('錯誤請求'); - } - $rtn['address'] = json_decode($rtn['address'], true); - $order = [ - 'order_sn' => input('order_sn'), - 'order_amount' => $rtn['order_amount'], - 'goods_amount' => $rtn['goods_amount'], - 'mobile' => $rtn['mobile'], - 'consignee' => $rtn['consignee'], - 'email' => $rtn['email'], - 'zipcode' => isset($rtn['address']['zipcode']) ? $rtn['address']['zipcode'] : '', - 'goods_list' => Db::name('order_goods')->where('order_id', $rtn['order_id'])->select()->toArray(), - ]; - - $payment = new PaymentStrategy(input('pay_code')); - - $result = $payment->pay($order); - - return $this->Success($result); - } - - //金流回傳網址 - public function response() - { - $data = input(); - - $payment = new PaymentStrategy(input('paycode')); - /* 傳入回傳資料,返回result - ** $result['code'] = 200 - ** $result['msg'] = '付款成功' - ** $result['order_sn'] = '訂單編號' - ** $result['paycode'] = '金流代碼' - */ - - $result = $payment->response($data); - - switch ($result['paycode']) { - case 'eccredit': - if ($result['code'] == 200) { - //判斷是否為超商取貨付款 - $order = Db::name('order_info') - ->where('order_sn', $result['order_sn']) - ->find(); - - $shipping_code = Db::name('shipping')->where('shipping_id', $order['shipping_id'])->value('shipping_code'); - - if ($shipping_code == 'ecpay') { - $rtn = \app\common\shipping\Shipping::createShipping('ecpay', $order['order_sn']); - if ($rtn['code'] != 200) { - $code = 200; - $result['msg'] .= ',建立物流單失敗'; - //TODO: 通知管理員物流單建立失敗 - } - } - - //判斷是否為SlashCard商品 - $is_main = Db::name('order_goods') - ->where('order_id', $order['order_id']) - ->where('goods_id', 1) - ->find(); - - if ($is_main) { - $rtn = \app\service\Card::addUser([ - 'order_sn' => $order['order_sn'], - 'user_id' => Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'), - ]); - if ($rtn['code'] != 200) { - $code = 200; - $result['msg'] .= ',建立卡片失敗'; - //TODO: 通知管理員建立卡片失敗 - } else { - //更改會員的狀態 - Db::name('users')->where('user_id', $order['user_id'])->update(['is_validated' => 1]); - $code = 200; - } - } - - //分潤獎金計算 - //取得介紹人user_id - //分銷金額 - try { - //找出第一二三四層介紹人 - $sso_user_id = Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'); - $parents = $this->getAffiliate($sso_user_id, 1); - - foreach ($parents as $key => $value) { - //TODO: 金額改為後台設定 - $money = [400, 50, 30, 20]; - - //寫入affiliate_log - $affiliate_log = [ - 'user_id' => Db::name('users')->where('sso_user_id', $value)->value('user_id'), - 'order_id' => $order['order_id'], - 'time' => time(), - 'user_name' => Db::name('users')->where('sso_user_id', $value)->value('real_name'), - 'money' => $money[$key], - ]; - Db::name('affiliate_log')->insert($affiliate_log); - //增加介紹人的獎金 - Db::name('users')->where('sso_user_id', $value)->inc('frozen_money', $money[$key])->update(); - } - } catch (\Exception $e) { - print_r($e->getMessage()); - $code = 200; - $result['msg'] .= ',分潤失敗'; - } - }else{ - //付款失敗 - $code = $result['code']; - $result['msg'] .= ',付款失敗'; - } - break; - default: - break; - } - //回傳成功並且不為超商支付及atm - - $query_string = http_build_query($result); - return redirect(getUrl() . '/m/cartFinish/?' . $query_string); - } - - //金流回傳網址 - public function callback() - { - Log::write(json_encode(input())); - $data = input('post.'); - - $payment = new PaymentStrategy(input('paycode')); - - /* 傳入回傳資料,返回result - ** $result['code'] = 200 - ** $result['msg'] = '付款成功' - ** $result['order_sn'] = '訂單編號' - ** $result['paycode'] = '金流代碼' - */ - - $result = $payment->callback($data); - - if ($result['code'] == 200) { - //判斷是否為超商取貨付款 - $order = Db::name('order_info') - ->where('order_sn', $result['order_sn']) - ->find(); - - $shipping_code = Db::name('shipping')->where('shipping_id', $order['shipping_id'])->value('shipping_code'); - - if ($shipping_code == 'ecpay') { - $rtn = \app\common\shipping\Shipping::createShipping('ecpay', $order['order_sn']); - if ($rtn['code'] != 200) { - $code = 200; - $result['msg'] .= ',建立物流單失敗'; - //TODO: 通知管理員物流單建立失敗 - } - } - - //判斷是否為SlashCard商品 - $is_main = Db::name('order_goods') - ->where('order_id', $order['order_id']) - ->where('goods_id', 1) - ->find(); - - if ($is_main) { - $rtn = \app\service\Card::addUser([ - 'order_sn' => $order['order_sn'], - 'user_id' => Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'), - ]); - if ($rtn['code'] != 200) { - $code = 200; - $result['msg'] .= ',建立卡片失敗'; - //TODO: 通知管理員建立卡片失敗 - } else { - //更改會員的狀態 - Db::name('users')->where('user_id', $order['user_id'])->update(['is_validated' => 1]); - $code = 200; - } - } - - //分潤獎金計算 - //取得介紹人user_id - //分銷金額 - try { - //找出第一二三四層介紹人 - $sso_user_id = Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'); - $parents = $this->getAffiliate($sso_user_id, 1); - - foreach ($parents as $key => $value) { - //TODO: 金額改為後台設定 - $money = [400, 50, 30, 20]; - - //寫入affiliate_log - $affiliate_log = [ - 'user_id' => Db::name('users')->where('sso_user_id', $value)->value('user_id'), - 'order_id' => $order['order_id'], - 'time' => time(), - 'user_name' => Db::name('users')->where('sso_user_id', $value)->value('real_name'), - 'money' => $money[$key], - ]; - Db::name('affiliate_log')->insert($affiliate_log); - //增加介紹人的獎金 - Db::name('users')->where('sso_user_id', $value)->inc('frozen_money', $money[$key])->update(); - } - } catch (\Exception $e) { - $code = 200; - $result['msg'] .= ',分潤失敗'; - } - } - return '1|OK'; - } - - public function cancel() - { - Log::write('金流取消'); - echo "金流取消"; - } - - public function otpError(){ - $data = input(); - $order_sn = $data['order_sn']; - - $order = Db::name('order_info') - ->where('order_sn', $order_sn) - ->find(); - - $result['pay_status'] = '3'; - $code = 500; - $message = 'OTP驗證失敗'; - - try { - //更新訂單狀態 - Db::name('order_info') - ->where('order_sn', $order_sn) - ->update($result); - - //更新訂單操縱紀錄 - $order_action = [ - 'order_id' => $order['order_id'], - 'action_user' => '綠界科技', - 'order_status' => $order['order_status'], - 'shipping_status' => $order['shipping_status'], - 'pay_status' => $order['pay_status'], - 'action_note' => '綠界金流: 信用卡支付,' . $message, - 'log_time' => time(), - ]; - Db::name('order_action')->insert($order_action); - } catch (\Exception $e) { - throw new \Exception($e->getMessage()); - } - - - $result = [ - 'code' => $code, - 'paycode' => 'eccredit', - 'msg' => $message, - 'order_sn' => $order_sn, - ]; - - $query_string = http_build_query($result); - return redirect(getUrl() . '/m/cartFinish/?' . $query_string); - } - - private function getAffiliate($sso_user_id, $level = 1) - { - $parents = []; - $parent_id = Db::name('users')->where('sso_user_id', $sso_user_id)->value('parent_id'); - - if ($parent_id) { - $parents[$level] = $parent_id; - $level++; - $return = $this->getAffiliate($parent_id, $level); - $parents = array_merge($parents, $return); - } - - return $parents; - } -} +where('enabled', 1) + ->select(); + + + if (!$res) { + return $this->Error('錯誤請求'); + } + + return $this->Success($res); + } + + public function process() + { + //從order info 取出訂單資料 + $rtn = Db::name('order_info') + ->where('order_sn', input('order_sn')) + ->find(); + + if (!$rtn) { + return $this->Error('錯誤請求'); + } + $rtn['address'] = json_decode($rtn['address'], true); + $order = [ + 'order_sn' => input('order_sn'), + 'order_amount' => $rtn['order_amount'], + 'goods_amount' => $rtn['goods_amount'], + 'mobile' => $rtn['mobile'], + 'consignee' => $rtn['consignee'], + 'email' => $rtn['email'], + 'zipcode' => isset($rtn['address']['zipcode']) ? $rtn['address']['zipcode'] : '', + 'goods_list' => Db::name('order_goods')->where('order_id', $rtn['order_id'])->select()->toArray(), + ]; + + $payment = new PaymentStrategy(input('pay_code')); + + $result = $payment->pay($order); + + return $this->Success($result); + } + + //金流回傳網址 + public function response() + { + $data = input(); + + $payment = new PaymentStrategy(input('paycode')); + /* 傳入回傳資料,返回result + ** $result['code'] = 200 + ** $result['msg'] = '付款成功' + ** $result['order_sn'] = '訂單編號' + ** $result['paycode'] = '金流代碼' + */ + + $result = $payment->response($data); + + switch ($result['paycode']) { + case 'eccredit': + if ($result['code'] == 200) { + //判斷是否為超商取貨付款 + $order = Db::name('order_info') + ->where('order_sn', $result['order_sn']) + ->find(); + + $shipping_code = Db::name('shipping')->where('shipping_id', $order['shipping_id'])->value('shipping_code'); + + if ($shipping_code == 'ecpay') { + $rtn = \app\common\shipping\Shipping::createShipping('ecpay', $order['order_sn']); + if ($rtn['code'] != 200) { + $code = 200; + $result['msg'] .= ',建立物流單失敗'; + //TODO: 通知管理員物流單建立失敗 + } + } + + //判斷是否為SlashCard商品 + $is_main = Db::name('order_goods') + ->where('order_id', $order['order_id']) + ->where('goods_id', 1) + ->find(); + + if ($is_main) { + $rtn = \app\service\Card::addUser([ + 'order_sn' => $order['order_sn'], + 'user_id' => Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'), + ]); + if ($rtn['code'] != 200) { + $code = 200; + $result['msg'] .= ',建立卡片失敗'; + //TODO: 通知管理員建立卡片失敗 + } else { + //更改會員的狀態 + Db::name('users')->where('user_id', $order['user_id'])->update(['is_slash' => 1]); + $code = 200; + } + } + } else { + //付款失敗 + $code = $result['code']; + $result['msg'] .= ',付款失敗'; + } + break; + default: + break; + } + //回傳成功並且不為超商支付及atm + + $query_string = http_build_query($result); + return redirect(getUrl() . '/m/cartFinish/?' . $query_string); + } + + //金流回傳網址 + public function callback() + { + Log::write(json_encode(input())); + $data = input('post.'); + + $payment = new PaymentStrategy(input('paycode')); + + /* 傳入回傳資料,返回result + ** $result['code'] = 200 + ** $result['msg'] = '付款成功' + ** $result['order_sn'] = '訂單編號' + ** $result['paycode'] = '金流代碼' + */ + + $result = $payment->callback($data); + + if ($result['code'] == 200) { + //判斷是否為超商取貨付款 + $order = Db::name('order_info') + ->where('order_sn', $result['order_sn']) + ->find(); + + $shipping_code = Db::name('shipping')->where('shipping_id', $order['shipping_id'])->value('shipping_code'); + + if ($shipping_code == 'ecpay') { + $rtn = \app\common\shipping\Shipping::createShipping('ecpay', $order['order_sn']); + if ($rtn['code'] != 200) { + $code = 200; + $result['msg'] .= ',建立物流單失敗'; + //TODO: 通知管理員物流單建立失敗 + } + } + + //判斷是否為SlashCard商品 + $is_main = Db::name('order_goods') + ->where('order_id', $order['order_id']) + ->where('goods_id', 1) + ->find(); + + if ($is_main) { + $rtn = \app\service\Card::addUser([ + 'order_sn' => $order['order_sn'], + 'user_id' => Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'), + ]); + if ($rtn['code'] != 200) { + $code = 200; + $result['msg'] .= ',建立卡片失敗'; + //TODO: 通知管理員建立卡片失敗 + } else { + //更改會員的狀態 + Db::name('users')->where('user_id', $order['user_id'])->update(['is_validated' => 1]); + $code = 200; + } + } + } + return '1|OK'; + } + + public function cancel() + { + Log::write('金流取消'); + echo "金流取消"; + } + + public function otpError() + { + $data = input(); + $order_sn = $data['order_sn']; + + $order = Db::name('order_info') + ->where('order_sn', $order_sn) + ->find(); + + $result['pay_status'] = '3'; + $code = 500; + $message = 'OTP驗證失敗'; + + try { + //更新訂單狀態 + Db::name('order_info') + ->where('order_sn', $order_sn) + ->update($result); + + //更新訂單操縱紀錄 + $order_action = [ + 'order_id' => $order['order_id'], + 'action_user' => '綠界科技', + 'order_status' => $order['order_status'], + 'shipping_status' => $order['shipping_status'], + 'pay_status' => $order['pay_status'], + 'action_note' => '綠界金流: 信用卡支付,' . $message, + 'log_time' => time(), + ]; + Db::name('order_action')->insert($order_action); + } catch (\Exception $e) { + throw new \Exception($e->getMessage()); + } + + + $result = [ + 'code' => $code, + 'paycode' => 'eccredit', + 'msg' => $message, + 'order_sn' => $order_sn, + ]; + + $query_string = http_build_query($result); + return redirect(getUrl() . '/m/cartFinish/?' . $query_string); + } +} diff --git a/app/appapi/controller/v1/Shipping.php b/app/appapi/controller/v1/Shipping.php index 58cf2da5..f23ff5ca 100644 --- a/app/appapi/controller/v1/Shipping.php +++ b/app/appapi/controller/v1/Shipping.php @@ -172,29 +172,12 @@ class Shipping extends ApiController ->where('order_sn', $result['order_sn']) ->find(); - //分潤獎金計算 - //取得介紹人user_id - //分銷金額 try { - //找出第一二三四層介紹人 - $sso_user_id = Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'); - $parents = $this->getAffiliate($sso_user_id, 1); - - foreach ($parents as $key => $value) { - //TODO: 金額改為後台設定 - $money = [400, 50, 30, 20]; - - //寫入affiliate_log - $affiliate_log = [ - 'user_id' => Db::name('users')->where('sso_user_id', $value)->value('user_id'), - 'order_id' => $order['order_id'], - 'time' => time(), - 'user_name' => Db::name('users')->where('sso_user_id', $value)->value('real_name'), - 'money' => $money[$key], - ]; - Db::name('affiliate_log')->insert($affiliate_log); - //增加介紹人的獎金 - Db::name('users')->where('sso_user_id', $value)->inc('frozen_money', $money[$key])->update(); + //計算分潤 + $res = \app\common\Affiliate::calculateProfitShare($order); + + if($res['code'] == 0){ + $result['msg'] .= ',分潤成功'; } //將會員的is_validated欄位改為1 @@ -213,19 +196,4 @@ class Shipping extends ApiController } } - private function getAffiliate($sso_user_id, $level = 1) - { - $parents = []; - $parent_id = Db::name('users')->where('sso_user_id', $sso_user_id)->value('parent_id'); - - if ($parent_id) { - $parents[$level] = $parent_id; - $level++; - $return = $this->getAffiliate($parent_id, $level); - $parents = array_merge($parents, $return); - } - - return $parents; - } - } diff --git a/app/appapi/controller/v1/Test.php b/app/appapi/controller/v1/Test.php index 003f746a..8f66db59 100644 --- a/app/appapi/controller/v1/Test.php +++ b/app/appapi/controller/v1/Test.php @@ -4,46 +4,16 @@ namespace app\appapi\controller\v1; use app\appapi\ApiController; use think\facade\Db; -use app\common\sms\Sms; - class Test extends ApiController { public function test(){ - Sms::createSms('smsking', - ['recipient'=>'0983214434','message'=>'簡訊測試'] - ); - } - - private function countLevelMembers($users,$deep=1){ - $level = array(); - - if($deep>3){ - return 0; - } - - if(!is_array($users)){ - $users = [$users]; - } - - $userDo = Db::name('users') - ->whereIn('parent_id',$users); - - $level_num=$userDo->count(); - - if($level_num>0){ - $level[$deep] = $level_num; - } - - if($level_num > 0){ - $user_list = $userDo->column('sso_user_id'); - $rtn = $this->countLevelMembers($user_list,$deep+1); - if($rtn){ - $level = $level + $rtn; - } - } - - return $level; - } - + $order = Db::name('order_info') + ->where('order_id', 1071) + ->find(); + + $res = \app\common\Affiliate::calculateProfitShare($order); + // print_r($order); + return json_encode($res); + } } diff --git a/app/appapi/controller/v1/User.php b/app/appapi/controller/v1/User.php index 854b3544..b854c7ff 100644 --- a/app/appapi/controller/v1/User.php +++ b/app/appapi/controller/v1/User.php @@ -14,9 +14,8 @@ class User extends ApiController { $data = input(); - $user = Db::name('users') - ->field('user_id,sso_user_id,headimg,user_money,frozen_money,reg_time,mobile_phone,real_name,refer_code,parent_id,is_validated') + ->field('user_id,sso_user_id,headimg,user_money,frozen_money,reg_time,mobile_phone,email,real_name,refer_code,parent_id,is_validated,is_slash,is_resale') ->where('sso_user_id', $this->uid) ->find(); @@ -72,6 +71,17 @@ class User extends ApiController return $this->Success($user); } + public function updateUserInfo() + { + $data = input('post.'); + + $user = Db::name('users') + ->where('sso_user_id', $this->uid) + ->update($data); + + return $this->Success('更新成功'); + } + public function getBank(){ //取得用戶銀行資料 $bank = Db::name('user_bank') @@ -514,6 +524,15 @@ class User extends ApiController } + public function updateUserResale() + { + $user = Db::name('users') + ->where('sso_user_id', $this->uid) + ->update(['is_resale' => 1]); + + return $this->Success('申請成功'); + } + public function getRecommandList(){ $page = input('page') ? intval(input('page')) : 0; @@ -526,7 +545,7 @@ class User extends ApiController ->toArray(); foreach($users as $key=>$val){ - $users[$key]['is_validated'] = $val['is_validated'] == 1 ? '訂單完成' : '未完成'; + $users[$key]['is_slash'] = $val['is_slash'] == 1 ? '訂單完成' : '未完成'; //real_name 只顯示頭尾,中間加* $users[$key]['real_name'] = mb_substr($val['real_name'],0,1,'utf-8') . str_repeat('*',mb_strlen($val['real_name'],'utf-8')-2) . mb_substr($val['real_name'],-1,1,'utf-8'); } diff --git a/app/common/Affiliate.php b/app/common/Affiliate.php new file mode 100644 index 00000000..a0d6b35d --- /dev/null +++ b/app/common/Affiliate.php @@ -0,0 +1,65 @@ +-1,'msg'=>'未開啟分銷']; + } + + //檢查是否已經計算過 + $is_calculate = Db::name('affiliate_log')->where('order_id', $order['order_id'])->count(); + if($is_calculate){ + return ['code'=>-1,'msg'=>'已經計算過']; + } + + + try { + //找出第一二三四層介紹人 + $sso_user_id = Db::name('users')->where('user_id', $order['user_id'])->value('sso_user_id'); + + $parents = self::getAffiliate($sso_user_id, 1); + + foreach ($parents as $key => $value) { + //計算分銷金額 + $money[$key] = $order['order_amount'] * $affiliate['item'][$key]['level_money'] / 100; + + //寫入affiliate_log + $affiliate_log = [ + 'user_id' => Db::name('users')->where('sso_user_id', $value)->value('user_id'), + 'order_id' => $order['order_id'], + 'time' => time(), + 'user_name' => Db::name('users')->where('sso_user_id', $value)->value('real_name'), + 'money' => $money[$key], + ]; + Db::name('affiliate_log')->insert($affiliate_log); + //增加介紹人的獎金 + Db::name('users')->where('sso_user_id', $value)->inc('frozen_money', $money[$key])->update(); + } + return true; + } catch (\Exception $e) { + print_r($e->getMessage()); + return false; + } + } + + private static function getAffiliate($sso_user_id, $level = 1) + { + $parents = []; + $parent_id = Db::name('users')->where('sso_user_id', $sso_user_id)->value('parent_id'); + + if ($parent_id) { + $parents[$level] = $parent_id; + $level++; + $return = self::getAffiliate($parent_id, $level); + $parents = array_merge($parents, $return); + } + + return $parents; + } +} diff --git a/public/adm/includes/inc_menu.php b/public/adm/includes/inc_menu.php index 4032eea9..abb14321 100644 --- a/public/adm/includes/inc_menu.php +++ b/public/adm/includes/inc_menu.php @@ -43,7 +43,7 @@ $modules['04_order']['02_order_list'] = 'order.html'; $modules['04_order']['05_edit_order_print'] = 'order.php?act=templates'; $modules['04_order']['06_undispose_booking'] = 'goods_booking.php?act=list_all'; //$modules['04_order']['07_repay_application'] = 'repay.php?act=list_all'; -$modules['04_order']['08_add_order'] = 'order.php?act=add'; +$modules['04_order']['08_add_order'] = 'order.html#/add'; $modules['04_order']['09_delivery_order'] = 'order.php?act=delivery_list'; //$modules['04_order']['10_back_order'] = 'order.php?act=back_list';//模板之家代码修改 退换货 $modules['04_order']['10_back_order'] = 'back.php?act=back_list'; //模板之家代码修改 退换货