You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
6.8 KiB

<?php
namespace app\appapi\controller\v1;
use app\appapi\ApiController;
use think\facade\Db;
use app\common\shipping\ShippingStrategy;
class Shipping extends ApiController
{
public function getShippings(){
$res=Db::name('shipping')
->where('enabled',1)
->select();
if(!$res){
return $this->Error('錯誤請求');
}
return $this->Success($res);
}
public function getUserDefaultCvs(){
//從user_cvs取得預設資料
$res=Db::name('user_cvs')
->where('user_id',getIdBySsoId($this->uid))
->where('is_default',1)
->find();
if(!$res){
return $this->Success('沒有預設的門市',201);
}
return $this->Success($res);
}
public function getMyCvs(){
//從user_cvs取得資料
$res=Db::name('user_cvs')
->where('user_id',getIdBySsoId($this->uid))
->select();
//將$res用foreach的方式以type分類轉成陣列
$default_type = 1;
$default_id = '';
$data = [];
$default = [];
foreach($res as $key => $value){
$data[$value['type']][] = $value;
if($value['is_default'] == 1){
$default = $value;
}
}
return $this->Success(['default'=>$default,'data'=>$data]);
}
public function setDefaultStoreId(){
//更新預設的store_id
$store_id = input('post.store_id');
try{
Db::name('user_cvs')
->where('user_id',getIdBySsoId($this->uid))
->update(['is_default'=>0]);
Db::name('user_cvs')
->where('user_id',getIdBySsoId($this->uid))
->where('store_id',$store_id)
->update(['is_default'=>1]);
}catch(\Exception $e){
return $this->Error('請求失敗');
}
return $this->Success('請求成功');
}
public function process(){
// $data = input('post.');
$data = [];
$data=array_merge($data,[
'sn'=>input('order_sn'),
'amount'=>1000,
'phone'=>'0912345678',
'name'=>'王小明',
'email'=>'wwayne.hsu@gmail.com',
'zip_code'=>'123',
]);
$shipping = new ShippingStrategy('ecpay');
$result = $shipping->request($data);
// print_r($result);
return $this->Success($result['data']);
}
public function map(){
switch(input('post.type')){
case '1':
$logisticsType = 'UNIMARTC2C';
break;
case '2':
$logisticsType = 'FAMIC2C';
break;
case '3':
$logisticsType = 'HILIFEC2C';
break;
case '4':
$logisticsType = 'OKMARTC2C';
break;
default:
return $this->Error('錯誤請求');
break;
}
$data = [
'device'=>'1',
'ExtraData'=>$this->uid,
'LogisticsSubType'=>$logisticsType
];
try{
$shipping = new ShippingStrategy('ecpay');
$result = $shipping->map($data);
}catch(\Exception $e){
return $this->Error('請求失敗,請連絡客服人員');
}
return $this->Success($result);
}
public function mapResponse(){
$data = input();
$shipping = new ShippingStrategy('ecpay');
$result = $shipping->mapResponse($data);
if(!$result){
return '錯誤請求';
}
return redirect($result['redirect_url']);
}
//刪除使用者的門市
public function deleteLogistic(){
$id = input('id');
try{
Db::name('user_cvs')
->where('id',$id)
->delete();
}catch(\Exception $e){
return $this->Error('請求失敗');
}
return $this->Success('請求成功');
}
public function response(){
$data = input('post.');
switch(input('shippingcode')){
case 'ecpay':
$shipping = new ShippingStrategy('ecpay');
$result = $shipping->response($data);
if($result['code']==200){
$order = Db::name('order_info')
->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();
}
//將會員的is_validated欄位改為1
Db::name('users')->where('user_id', $order['user_id'])->update(['is_validated'=>1]);
} catch (\Exception $e) {
$code = 200;
$result['msg'] .= ',分潤失敗';
}
}
return '1|OK';
break;
default:
return $this->Error('錯誤請求');
break;
}
}
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;
}
}