From 6b7551bed4226280d6353f51b61eac2b854cf0fb Mon Sep 17 00:00:00 2001 From: weiz <736250432@qq.com> Date: Mon, 13 May 2024 16:03:16 +0800 Subject: [PATCH] update --- app/admin/controller/bank/BankController.php | 95 +++++ .../merchat/MerchantBankController.php | 24 ++ .../controller/merchat/MerchantController.php | 26 ++ app/admin/lists/bank/BankLists.php | 65 ++++ .../lists/merchant/MerchantBankLists.php | 75 ++++ .../lists/operation/OpurchaseclassLists.php | 6 +- app/admin/logic/bank/BankLogic.php | 96 +++++ .../logic/merchant/MerchantBankLogic.php | 77 ++++ app/admin/logic/merchant/MerchantLogic.php | 10 +- app/admin/validate/bank/BankValidate.php | 86 +++++ .../merchant/MerchantBankValidate.php | 75 ++++ app/api/controller/bank/BankController.php | 20 ++ .../merchant/MerchantController.php | 62 ++++ app/api/validate/MerchantBankValidate.php | 43 +++ app/common/model/bank/Bank.php | 22 ++ app/common/model/merchant/MerchantBank.php | 25 ++ app/common/service/Curl.php | 329 ++++++++++++++++++ app/common/service/ThinkApi.php | 17 + 18 files changed, 1149 insertions(+), 4 deletions(-) create mode 100644 app/admin/controller/bank/BankController.php create mode 100644 app/admin/controller/merchat/MerchantBankController.php create mode 100644 app/admin/lists/bank/BankLists.php create mode 100644 app/admin/lists/merchant/MerchantBankLists.php create mode 100644 app/admin/logic/bank/BankLogic.php create mode 100644 app/admin/logic/merchant/MerchantBankLogic.php create mode 100644 app/admin/validate/bank/BankValidate.php create mode 100644 app/admin/validate/merchant/MerchantBankValidate.php create mode 100644 app/api/controller/bank/BankController.php create mode 100644 app/api/validate/MerchantBankValidate.php create mode 100644 app/common/model/bank/Bank.php create mode 100644 app/common/model/merchant/MerchantBank.php create mode 100644 app/common/service/Curl.php create mode 100644 app/common/service/ThinkApi.php diff --git a/app/admin/controller/bank/BankController.php b/app/admin/controller/bank/BankController.php new file mode 100644 index 0000000..d01dda5 --- /dev/null +++ b/app/admin/controller/bank/BankController.php @@ -0,0 +1,95 @@ +dataLists(new BankLists()); + } + + + /** + * @notes 添加 + * @return \think\response\Json + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function add() + { + $params = (new BankValidate())->post()->goCheck('add'); + $result = BankLogic::add($params); + if (true === $result) { + return $this->success('添加成功', [], 1, 1); + } + return $this->fail(BankLogic::getError()); + } + + + /** + * @notes 编辑 + * @return \think\response\Json + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function edit() + { + $params = (new BankValidate())->post()->goCheck('edit'); + $result = BankLogic::edit($params); + if (true === $result) { + return $this->success('编辑成功', [], 1, 1); + } + return $this->fail(BankLogic::getError()); + } + + + /** + * @notes 删除 + * @return \think\response\Json + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function delete() + { + $params = (new BankValidate())->post()->goCheck('delete'); + BankLogic::delete($params); + return $this->success('删除成功', [], 1, 1); + } + + + /** + * @notes 获取详情 + * @return \think\response\Json + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function detail() + { + $params = (new BankValidate())->goCheck('detail'); + $result = BankLogic::detail($params); + return $this->data($result); + } + + +} \ No newline at end of file diff --git a/app/admin/controller/merchat/MerchantBankController.php b/app/admin/controller/merchat/MerchantBankController.php new file mode 100644 index 0000000..d9e168c --- /dev/null +++ b/app/admin/controller/merchat/MerchantBankController.php @@ -0,0 +1,24 @@ +dataLists(new MerchantBankLists()); + } + + public function check(){ + $params = (new MerchantBankValidate())->post()->goCheck('check'); + $result = MerchantBankLogic::check($params,$this->adminId); + if (true === $result) { + return $this->success('审核成功', [], 1, 1); + } + return $this->fail(MerchantBankLogic::getError()); + } + } \ No newline at end of file diff --git a/app/admin/controller/merchat/MerchantController.php b/app/admin/controller/merchat/MerchantController.php index 688ea9b..6f4bfe7 100644 --- a/app/admin/controller/merchat/MerchantController.php +++ b/app/admin/controller/merchat/MerchantController.php @@ -7,6 +7,9 @@ use app\admin\controller\BaseAdminController; use app\admin\lists\merchant\MerchantLists; use app\admin\logic\merchant\MerchantLogic; use app\admin\validate\merchant\MerchantValidate; +use app\common\model\goods\Goods; +use app\common\model\goods\GoodsLabel; +use think\facade\Db; /** @@ -99,4 +102,27 @@ class MerchantController extends BaseAdminController } return $this->fail(MerchantLogic::getError()); } + + public function bind_goods_lists(){ + $mer_id = $this->request->get('mer_id'); + $page_no = $this->request->get('page_no',1); + $page_size = $this->request->get('page_size',15); + if(empty($mer_id)){ + return $this->fail('参数错误'); + } + $data = Db::name('merchant_bind_goods')->where('mer_id',$mer_id)->page($page_no,$page_size)->select()->each(function($item){ + $item['goods_info'] = Goods::where('id',$item['goods_id'])->with(['className','brandName','unitName','warehouseName'])->findOrEmpty(); + if(!empty($item['goods_info']['sys_labels'])){ + $goodslabel = GoodsLabel::where('id','in',trim($item['goods_info']['sys_labels'],','))->column('name'); + $item['goods_info']['sys_labels_text'] = implode(',',$goodslabel); + }else{ + $item['goods_info']['sys_labels_text'] = ''; + } + return $item; + })->toArray(); + $count = Db::name('merchant_bind_goods')->where('mer_id',$mer_id)->count(); + return $this->success('请求成功',['lists'=>$data,'count'=>$count,'page_no'=>$page_no,'page_size'=>$page_size]); + } + + } \ No newline at end of file diff --git a/app/admin/lists/bank/BankLists.php b/app/admin/lists/bank/BankLists.php new file mode 100644 index 0000000..654304e --- /dev/null +++ b/app/admin/lists/bank/BankLists.php @@ -0,0 +1,65 @@ + ['name'], + ]; + } + + + /** + * @notes 获取列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function lists(): array + { + return Bank::where($this->searchWhere) + ->field(['id', 'name', 'image']) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select() + ->toArray(); + } + + + /** + * @notes 获取数量 + * @return int + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function count(): int + { + return Bank::where($this->searchWhere)->count(); + } + +} \ No newline at end of file diff --git a/app/admin/lists/merchant/MerchantBankLists.php b/app/admin/lists/merchant/MerchantBankLists.php new file mode 100644 index 0000000..95b420e --- /dev/null +++ b/app/admin/lists/merchant/MerchantBankLists.php @@ -0,0 +1,75 @@ + ['mer_id', 'bank_id', 'is_own', 'is_check'], + '%like%' => ['bank_code', 'bank_branch', 'name'], + ]; + } + + + /** + * @notes 获取列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2024/05/13 15:06 + */ + public function lists(): array + { + return MerchantBank::where($this->searchWhere) + ->field(['id', 'mer_id', 'bank_id', 'bank_code', 'bank_branch', 'name', 'id_card', 'phone', 'financial_img', 'is_own', 'is_check', 'fail_msg', 'admin_id']) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select()->each(function($data){ + $admin = Admin::field('name')->where('id',$data['admin_id'])->findOrEmpty(); + $bank = Bank::field('name,image')->where('id',$data['bank_id'])->findOrEmpty(); + $data['admin_name'] = !$admin->isEmpty() ? $admin['name'] : ''; + $data['bank_info'] = $bank; + $data['is_own_text'] = $data['is_own'] == 0 ? '个人账户' : '对公账户'; + $data['is_check_text'] = $data->is_check_text; + }) + ->toArray(); + } + + + /** + * @notes 获取数量 + * @return int + * @author likeadmin + * @date 2024/05/13 15:06 + */ + public function count(): int + { + return MerchantBank::where($this->searchWhere)->count(); + } + +} \ No newline at end of file diff --git a/app/admin/lists/operation/OpurchaseclassLists.php b/app/admin/lists/operation/OpurchaseclassLists.php index d3d2061..9ea6cd5 100644 --- a/app/admin/lists/operation/OpurchaseclassLists.php +++ b/app/admin/lists/operation/OpurchaseclassLists.php @@ -4,6 +4,7 @@ namespace app\admin\lists\operation; use app\admin\lists\BaseAdminDataLists; +use app\common\model\merchant\Merchant; use app\common\model\operation\Opurchaseclass; use app\common\lists\ListsSearchInterface; @@ -46,7 +47,10 @@ class OpurchaseclassLists extends BaseAdminDataLists implements ListsSearchInter ->field(['id', 'merchant', 'order_arr', 'cart_id', 'number', 'total', 'deduction_price', 'actual', 'money', 'paid']) ->limit($this->limitOffset, $this->limitLength) ->order(['id' => 'desc']) - ->select() + ->select()->each(function($data){ + $merchant = Merchant::field('mer_name')->where('mer_id',$data['merchant'])->findOrEmpty(); + $data['merchant_name'] = !$merchant->isEmpty() ? $merchant['mer_name'] : ''; + }) ->toArray(); } diff --git a/app/admin/logic/bank/BankLogic.php b/app/admin/logic/bank/BankLogic.php new file mode 100644 index 0000000..8ac95b6 --- /dev/null +++ b/app/admin/logic/bank/BankLogic.php @@ -0,0 +1,96 @@ + $params['name'], + 'image' => $params['image'] + ]); + + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 编辑 + * @param array $params + * @return bool + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public static function edit(array $params): bool + { + Db::startTrans(); + try { + Bank::where('id', $params['id'])->update([ + 'name' => $params['name'], + 'image' => $params['image'] + ]); + + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 删除 + * @param array $params + * @return bool + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public static function delete(array $params): bool + { + return Bank::destroy($params['id']); + } + + + /** + * @notes 获取详情 + * @param $params + * @return array + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public static function detail($params): array + { + return Bank::findOrEmpty($params['id'])->toArray(); + } +} \ No newline at end of file diff --git a/app/admin/logic/merchant/MerchantBankLogic.php b/app/admin/logic/merchant/MerchantBankLogic.php new file mode 100644 index 0000000..8984946 --- /dev/null +++ b/app/admin/logic/merchant/MerchantBankLogic.php @@ -0,0 +1,77 @@ +update([ + 'is_check' => $params['is_check'], + 'fail_msg' => $params['fail_msg'], + 'admin_id' => $admin_id, + 'update_time' => time() + ]); + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 删除 + * @param array $params + * @return bool + * @author likeadmin + * @date 2024/05/13 15:06 + */ + public static function delete(array $params): bool + { + return MerchantBank::destroy($params['id']); + } + + + /** + * @notes 获取详情 + * @param $params + * @return array + * @author likeadmin + * @date 2024/05/13 15:06 + */ + public static function detail($params): array + { + $data = MerchantBank::findOrEmpty($params['id']); + $admin = Admin::field('name')->where('id',$data['admin_id'])->findOrEmpty(); + $bank = Bank::field('name,image')->where('id',$data['bank_id'])->findOrEmpty(); + $data['admin_name'] = !$admin->isEmpty() ? $admin['name'] : ''; + $data['bank_info'] = $bank; + $data['is_own_text'] = $data['is_own'] == 0 ? '个人账户' : '对公账户'; + $data['is_check_text'] = $data->is_check_text; + return $data->toArray(); + } +} \ No newline at end of file diff --git a/app/admin/logic/merchant/MerchantLogic.php b/app/admin/logic/merchant/MerchantLogic.php index ab3597b..c618d55 100644 --- a/app/admin/logic/merchant/MerchantLogic.php +++ b/app/admin/logic/merchant/MerchantLogic.php @@ -129,15 +129,19 @@ class MerchantLogic extends BaseLogic { Db::startTrans(); try { - $data = []; foreach ($params['bind_data'] as $v){ - $data[] = [ + $data = [ 'mer_id' => $params['mer_id'], 'goods_id' => $v['goods_id'], 'nums' => $v['nums'] ]; + $has = Db::name('merchant_bind_goods')->where('mer_id',$params['mer_id'])->where('goods_id',$v['goods_id'])->findOrEmpty(); + if(!empty($has)){ + Db::name('merchant_bind_goods')->where('id',$has['id'])->update($data); + }else{ + Db::name('merchant_bind_goods')->insert($data); + } } - Db::name('merchant_bind_goods')->insertAll($data); Db::commit(); return true; } catch (\Exception $e) { diff --git a/app/admin/validate/bank/BankValidate.php b/app/admin/validate/bank/BankValidate.php new file mode 100644 index 0000000..38eab2d --- /dev/null +++ b/app/admin/validate/bank/BankValidate.php @@ -0,0 +1,86 @@ + 'require', + 'name' => 'require', + 'image' => 'require', + ]; + + + /** + * 参数描述 + * @var string[] + */ + protected $field = [ + 'id' => 'id', + 'name' => '银行名称', + 'image' => '银行图片', + ]; + + + /** + * @notes 添加场景 + * @return BankValidate + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function sceneAdd() + { + return $this->only(['name','image']); + } + + + /** + * @notes 编辑场景 + * @return BankValidate + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function sceneEdit() + { + return $this->only(['id','name','image']); + } + + + /** + * @notes 删除场景 + * @return BankValidate + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function sceneDelete() + { + return $this->only(['id']); + } + + + /** + * @notes 详情场景 + * @return BankValidate + * @author likeadmin + * @date 2024/05/13 11:09 + */ + public function sceneDetail() + { + return $this->only(['id']); + } + +} \ No newline at end of file diff --git a/app/admin/validate/merchant/MerchantBankValidate.php b/app/admin/validate/merchant/MerchantBankValidate.php new file mode 100644 index 0000000..a85b6c0 --- /dev/null +++ b/app/admin/validate/merchant/MerchantBankValidate.php @@ -0,0 +1,75 @@ + 'require', + 'is_check' => 'require|in:1,2', + 'fail_msg' => 'requireIf:is_check,2' + + ]; + + + /** + * 参数描述 + * @var string[] + */ + protected $field = [ + 'id' => 'id', + 'is_check' => '审核状态', + 'fail_msg' => '失败原因' + ]; + + + /** + * @notes 编辑场景 + * @return MerchantBankValidate + * @author likeadmin + * @date 2024/05/13 15:06 + */ + public function sceneEdit() + { + return $this->only(['id','is_check','fail_msg']); + } + + + /** + * @notes 删除场景 + * @return MerchantBankValidate + * @author likeadmin + * @date 2024/05/13 15:06 + */ + public function sceneDelete() + { + return $this->only(['id']); + } + + + /** + * @notes 详情场景 + * @return MerchantBankValidate + * @author likeadmin + * @date 2024/05/13 15:06 + */ + public function sceneDetail() + { + return $this->only(['id']); + } + +} \ No newline at end of file diff --git a/app/api/controller/bank/BankController.php b/app/api/controller/bank/BankController.php new file mode 100644 index 0000000..2b15477 --- /dev/null +++ b/app/api/controller/bank/BankController.php @@ -0,0 +1,20 @@ +dataLists(new BankLists()); + } + } \ No newline at end of file diff --git a/app/api/controller/merchant/MerchantController.php b/app/api/controller/merchant/MerchantController.php index ff532bd..66d1ebd 100644 --- a/app/api/controller/merchant/MerchantController.php +++ b/app/api/controller/merchant/MerchantController.php @@ -5,6 +5,10 @@ namespace app\api\controller\merchant; use app\api\lists\merchant\MerchantLists; use app\api\controller\BaseApiController; +use app\api\validate\MerchantBankValidate; +use app\common\service\ThinkApi; +use support\Cache; +use think\facade\Db; class MerchantController extends BaseApiController @@ -12,4 +16,62 @@ class MerchantController extends BaseApiController public function lists(){ return $this->dataLists(new MerchantLists()); } + + public function add_bank(){ + $params = (new MerchantBankValidate())->post()->goCheck('add'); + $has = Db::name('merchant_bank')->where('is_own',$params['is_own'])->where('is_check','<>',2)->findOrEmpty(); + if(!empty($has)){ + return $this->fail('已提交审核请勿重复提交'); + } + $expireAt = strtotime(date('Y-m-d 23:59:59')); // 当天结束时间戳 + $totalKey = $params['mer_id'].$params['bank_code']; + $checkKey = $params['mer_id'].'check'; + $check = Cache::get($totalKey) ?? 0; + if ($check && $check > 9) { + return $this->fail('超出绑定限制请明日再绑定'); + } + $wrongNum = Cache::get($checkKey) ?? 0; + if($wrongNum && $wrongNum > 2){ + return $this->fail('当日认证次数已超限制,请勿重复提交,请次日后再试'); + } + Cache::set($totalKey, (int)($check+1),$expireAt); + if($params['is_own'] == 0){ + $info = [ + 'name'=>$params['name'], + 'idNum'=>$params['id_card'], + 'cardNo'=>$params['bank_code'], + 'mobile'=>$params['phone'], + ]; + $thinkApi = new ThinkApi(); + $result = $thinkApi->request('bankcard/auth',$info); + if($result['code'] != 0 || empty($result['data'])){ + return $this->fail($result['message']); + } + //认证结果。01:一致 02:不一致 03:认证不确定 04:认证失败。01、02收费 + if($result['data']['result'] == 4 || $result['data']['result'] == 2){ + Cache::set($checkKey, (int)($wrongNum+1),$expireAt); + } + if($result['data']['result'] == 4 || $result['data']['result'] == 2){ + $wrongNum = Cache::get($checkKey); + $numMsg = ',当日剩余认证次数'.(3-$wrongNum); + return $this->fail('认证失败,请检查姓名,身份证,银行卡,银行预留手机号码是否正确'.$numMsg); + } + } + $save_data = [ + 'mer_id' => $params['mer_id'], + 'name' => $params['name'], + 'bank_id' => $params['bank_id'], + 'bank_code' => $params['bank_code'], + 'bank_branch' => $params['bank_branch'], + 'phone' => $params['phone'] ?? '', + 'id_card' => $params['id_card'] ?? '', + 'financial_img' => $params['financial_img'] ?? '', + 'is_own' => $params['is_own'], + 'is_check' => 0, + 'create_time' => time() + ]; + //写入数据 + $res = Db::name('merchant_bank')->insert($save_data); + return $res ? $this->success('添加成功') : $this->fail('添加失败'); + } } \ No newline at end of file diff --git a/app/api/validate/MerchantBankValidate.php b/app/api/validate/MerchantBankValidate.php new file mode 100644 index 0000000..cbeac7e --- /dev/null +++ b/app/api/validate/MerchantBankValidate.php @@ -0,0 +1,43 @@ + 'require|in:0,1', + 'mer_id|商户id' => 'require|checkMerchant', + 'name|姓名' => 'require', + 'bank_id|开户银行' => 'require|checkBank', + 'bank_code|银行账号' => 'require|integer', + 'bank_branch|开户网点' => 'require|max:32', + 'id_card|身份证' => 'requireIf:is_own,0|idCard', + 'phone|手机号' => 'requireIf:is_own,0|mobile', +// 'financial_img|相关凭证' => 'require' + ]; + + + public function sceneAdd() + { + return $this->only(['mer_id','is_own','bank_id','name','bank_code','bank_branch','id_card','phone','financial_img']); + } + + public function checkBank($value){ + $data = Db::name('bank')->where('id',$value)->findOrEmpty(); + if(empty($data)){ + return '开户银行不存在'; + } + return true; + } + + public function checkMerchant($value){ + $data = Db::name('merchant')->where('mer_id',$value)->findOrEmpty(); + if(empty($data)){ + return '商户不存在'; + } + return true; + } + } \ No newline at end of file diff --git a/app/common/model/bank/Bank.php b/app/common/model/bank/Bank.php new file mode 100644 index 0000000..7ef98b8 --- /dev/null +++ b/app/common/model/bank/Bank.php @@ -0,0 +1,22 @@ +'待审核', 1=>'审核通过', 2=>'审核未通过']; + return $arr[$data['is_check']]; + } +} \ No newline at end of file diff --git a/app/common/service/Curl.php b/app/common/service/Curl.php new file mode 100644 index 0000000..aec119d --- /dev/null +++ b/app/common/service/Curl.php @@ -0,0 +1,329 @@ + true, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HEADER => false, + CURLOPT_VERBOSE => true, + CURLOPT_AUTOREFERER => true, + CURLOPT_CONNECTTIMEOUT => 30, + CURLOPT_TIMEOUT => 30, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' + ); + + public static function mergeArray() + { + $args = func_get_args(); + $res = array_shift($args); + while (!empty($args)) { + $next = array_shift($args); + foreach ($next as $k => $v) { + if (is_array($v) && isset($res[$k]) && is_array($res[$k])) { + $res[$k] = self::mergeArray($res[$k], $v); + } elseif (is_numeric($k)) { + isset($res[$k]) ? $res[] = $v : $res[$k] = $v; + } else { + $res[$k] = $v; + } + } + } + return $res; + } + + public function getOptions() + { + $options = self::mergeArray($this->request_options, $this->options, $this->config); + return $options; + } + + public function setOption($key, $value, $default = false) + { + if ($default) { + $this->options[$key] = $value; + } else { + $this->request_options[$key] = $value; + } + + return $this; + } + + /** + * Clears Options + * This will clear only the request specific options. Default options cannot be cleared. + */ + + public function resetOptions() + { + $this->request_options = array(); + return $this; + } + + /** + * Resets the Option to Default option + * @param $key + * @return $this + */ + public function resetOption($key) + { + if (isset($this->request_options[$key])) { + unset($this->request_options[$key]); + } + return $this; + } + + public function setOptions($options, $default = false) + { + if ($default) { + $this->options = $options + $this->request_options; + } else { + $this->request_options = $options + $this->request_options; + } + + return $this; + } + + public function buildUrl($url, $data = array()) + { + $parsed = parse_url($url); + + isset($parsed['query']) ? parse_str($parsed['query'], $parsed['query']) : $parsed['query'] = array(); + + $params = isset($parsed['query']) ? $data + $parsed['query'] : $data; + $parsed['query'] = ($params) ? '?' . http_build_query($params) : ''; + if (!isset($parsed['path'])) { + $parsed['path']='/'; + } + + $parsed['port'] = isset($parsed['port'])?':'.$parsed['port']:''; + return $parsed['scheme'].'://'.$parsed['host'].$parsed['port'].$parsed['path'].$parsed['query']; + } + + public function exec($url, $options, $debug = false) + { + $this->error = null; + $this->header = null; + $this->headerMap = null; + $this->info = null; + $this->status = null; + + $ch = curl_init($url); + curl_setopt_array($ch, $options); + + $output = curl_exec($ch); + + $this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if (!$output) { + $this->error = curl_error($ch); + $this->info = curl_getinfo($ch); + } elseif ($debug) { + $this->info = curl_getinfo($ch); + } + + if (@$options[CURLOPT_HEADER] == true) { + list($header, $output) = $this->processHeader($output, curl_getinfo($ch, CURLINFO_HEADER_SIZE)); + $this->header = $header; + } + curl_close($ch); + + return $output; + } + + public function processHeader($response, $header_size) + { + return array(substr($response, 0, $header_size), substr($response, $header_size)); + } + + public function get($url, $params = array(), $debug = false) + { + $exec_url = $this->buildUrl($url, $params); + $options = $this->getOptions(); + return $this->exec($exec_url, $options, $debug); + } + + public function post($url, $data, $params = array(), $debug = false, $flag = false) + { + $url = $this->buildUrl($url, $params); +// if ($flag) { +// $this->setOption(CURLOPT_HTTPHEADER, Yii::app()->params['host']); +// } + $options = $this->getOptions(); + $options[CURLOPT_POST] = true; + $options[CURLOPT_POSTFIELDS] = $data; + + return $this->exec($url, $options, $debug); + } + + public function postJson($url, $data, $params = array(), $debug = false, $flag = false) + { + $url = $this->buildUrl($url, $params); +// if ($flag) { +// $this->setOption(CURLOPT_HTTPHEADER, Yii::app()->params['host']); +// } + $options = $this->getOptions(); + $options[CURLOPT_POST] = true; + $options[CURLOPT_POSTFIELDS] = $data; + $options[CURLOPT_HEADER] = 0; + $options[CURLOPT_HTTPHEADER] = + array('Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data)); + + return $this->exec($url, $options, $debug); + } + + public function put($url, $data = null, $params = array(), $debug = false) + { + $url = $this->buildUrl($url, $params); + + $f = fopen('php://temp', 'rw+'); + fwrite($f, $data); + rewind($f); + + $options = $this->getOptions(); + $options[CURLOPT_PUT] = true; + $options[CURLOPT_INFILE] = $f; + $options[CURLOPT_INFILESIZE] = strlen($data); + + return $this->exec($url, $options, $debug); + } + + public function patch($url, $data = array(), $params = array(), $debug = false) + { + $url = $this->buildUrl($url, $params); + + $options = $this->getOptions(); + $options[CURLOPT_CUSTOMREQUEST] = 'PATCH'; + $options[CURLOPT_POSTFIELDS] = $data; + + return $this->exec($url, $options, $debug); + } + + public function delete($url, $params = array(), $debug = false) + { + $url = $this->buildUrl($url, $params); + + $options = $this->getOptions(); + $options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; + + return $this->exec($url, $options, $debug); + } + + /** + * Gets header of the last curl call if header was enabled + */ + public function getHeaders() + { + if (!$this->header) { + return array(); + } + + if (!$this->headerMap) { + $headers = explode("\r\n", trim($this->header)); + $output = array(); + $output['http_status'] = array_shift($headers); + + foreach ($headers as $line) { + $params = explode(':', $line, 2); + + if (!isset($params[1])) { + $output['http_status'] = $params[0]; + } else { + $output[trim($params[0])] = trim($params[1]); + } + } + + $this->headerMap = $output; + } + return $this->headerMap; + } + + public function addHeader($header = array()) + { + $h = isset($this->request_options[CURLOPT_HTTPHEADER]) ? $this->request_options[CURLOPT_HTTPHEADER] : array(); + foreach ($header as $k => $v) { + $h[] = $k . ': ' . $v; + } + + $this->setHeaders($h); + return $this; + } + + public function getHeader($key) + { + $headers = array_change_key_case($this->getHeaders(), CASE_LOWER); + $key = strtolower($key); + + return @$headers[$key]; + } + + public function setHeaders($header = array(), $default = false) + { + if ($this->isAssoc($header)) { + $out = array(); + foreach ($header as $k => $v) { + $out[] = $k .': '.$v; + } + $header = $out; + } + + $this->setOption(CURLOPT_HTTPHEADER, $header, $default); + return $this; + } + + private function isAssoc($arr) + { + return array_keys($arr) !== range(0, count($arr) - 1); + } + + public function getError() + { + return $this->error; + } + + public function getInfo() + { + return $this->info; + } + + public function getStatus() + { + return $this->status; + } + + public function init() + { + return; + } + + public function download($fileUrl, $localFileName) + { + $ch = curl_init($fileUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($ch); + curl_close($ch); + $fp = fopen($localFileName, 'a'); + fwrite($fp, $result); + fclose($fp); + + return $localFileName; + } + } diff --git a/app/common/service/ThinkApi.php b/app/common/service/ThinkApi.php new file mode 100644 index 0000000..186009d --- /dev/null +++ b/app/common/service/ThinkApi.php @@ -0,0 +1,17 @@ +appCode; + $data = $curl->$method($this->domain . $route, $param); + return json_decode($data, true); + } + } \ No newline at end of file