| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace common\components\delivery\platform\fengniao;
- use Yii;
- use common\components\delivery\helpers\HttpClient;
- use bizGhs\express\classes\DeliveryAuthTokenClass;
- /**
- * Class Shop
- * 门店接口实现
- */
- class Shop extends Fengniao
- {
- /**
- * 门店详情查询接口
- */
- public function chainstoreQuery()
- {
- }
- /**
- * 门店批量查询接口
- *
- * 根据蜂鸟官方文档实现门店批量查询功能。
- * 可按商户ID查询所有门店信息,支持分页。
- *
- * @param array $data 查询参数,结构参考:
- * [
- * // ========== 必填参数 ==========
- * 'merchant_id' => '商户ID(Long类型)',
- *
- * // ========== 可选参数 ==========
- * 'page_no' => '分页页码(默认从1开始)',
- * 'page_size' => '每页条数(默认1000)',
- * ]
- */
- public function chainstoreQueryList($data = [])
- {
- // 验证 merchant_id 必填
- $merchantId = $data['merchant_id'] ?? $this->merchantId;
- if (empty($merchantId)) {
- Yii::warning("[FengniaoAdapter] chainstoreQueryList failed: merchant_id is required");
- return [
- 'code' => -1,
- 'msg' => 'merchant_id is required',
- 'data' => null
- ];
- }
- // 构建业务数据
- $businessData = [];
- // 可选参数 - 分页信息
- if (isset($data['page_no']) && $data['page_no'] !== null) {
- $businessData['page_no'] = (int)$data['page_no'];
- }
- $businessData['page_size'] = 1000; // 设置每页获取数为 1000
- if (isset($data['page_size']) && $data['page_size'] !== null) {
- $businessData['page_size'] = (int)$data['page_size'];
- }
- // 构建请求参数
- $payload = $this->buildRequestPayload('chainstoreQueryList', $businessData);
- // 发送请求
- $url = $this->baseUrl . '/chainstoreQueryList';
- $resp = HttpClient::post($url, $payload, [
- 'Content-Type' => 'application/json',
- ]);
- // 处理响应
- if (isset($resp['code']) && $resp['code'] == '200' && isset($resp['business_data'])) {
- // business_data 是 JSON 字符串,需要解析
- $respData = is_string($resp['business_data']) ? json_decode($resp['business_data'], true) : $resp['business_data'];
- if (is_null($respData)) {
- Yii::error("[FengniaoAdapter] Failed to parse business_data: " . $resp['business_data']);
- return [
- 'code' => -1,
- 'msg' => 'Failed to parse response data',
- 'data' => null
- ];
- }
- // 解析门店列表
- $chainStoreList = [];
- if (!empty($respData['list']) && is_array($respData['list'])) {
- foreach ($respData['list'] as $store) {
- $chainStoreList[] = [
- 'chain_store_id' => (int)($store['chain_store_id'] ?? 0),
- 'name' => $store['name'] ?? '',
- 'branch_name' => $store['branch_name'] ?? '',
- 'address' => $store['address'] ?? '',
- 'latitude' => (double)($store['latitude'] ?? 0),
- 'longitude' => (double)($store['longitude'] ?? 0),
- 'merchant_id' => (int)($store['merchant_id'] ?? 0),
- 'out_shop_code' => $store['out_shop_code'] ?? '',
- 'chainstore_type' => (int)($store['chainstore_type'] ?? 1),
- 'chainstore_type_desc' => $store['chainstore_type_desc'] ?? '',
- 'position_source' => (int)($store['position_source'] ?? 0),
- 'position_source_desc' => $store['position_source_desc'] ?? '',
- 'status' => (int)($store['status'] ?? 0),
- 'status_desc' => $store['status_desc'] ?? '',
- 'modify_status' => (int)($store['modify_status'] ?? 0),
- 'modify_status_desc' => $store['modify_status_desc'] ?? '',
- ];
- }
- }
- return [
- 'code' => 0,
- 'data' => [
- 'page_no' => (int)($respData['page_no'] ?? 0),
- 'page_size' => (int)($respData['page_size'] ?? 0),
- 'total_page' => (int)($respData['total_page'] ?? 0),
- 'total_count' => (int)($respData['total_count'] ?? 0),
- 'list' => $chainStoreList,
- ]
- ];
- }
- return [
- 'code' => (int)($resp['code'] ?? -1),
- 'msg' => $resp['msg'] ?? 'Unknown error',
- 'data' => null
- ];
- }
- public function bindStore($mainId, $storeId)
- {
- $deliveryAuthToken = DeliveryAuthTokenClass::getByCondition(['platform'=>'fengniao', 'mainId'=>$mainId], true);
- if(!$deliveryAuthToken){
- Yii::error('蜂鸟平台未授权');
- return false;
- }
- $deliveryAuthToken->shopId = $storeId;
- $deliveryAuthToken->save();
- return true;
- }
- public function unbindStore($mainId, $storeId)
- {
- $deliveryAuthToken = DeliveryAuthTokenClass::getByCondition(['platform'=>'fengniao', 'mainId'=>$mainId], true);
- if(!$deliveryAuthToken){
- Yii::error('蜂鸟平台未授权');
- return false;
- }
- $deliveryAuthToken->shopId = '';
- $deliveryAuthToken->save();
- return true;
- }
- /**
- * 查询商户余额接口
- */
- public function getShopAccountBalance()
- {
- $businessData = [
- 'merchant_id' => $this->merchantId,
- ];
- $payload = $this->buildRequestPayload('getAmount', $businessData);
- $url = $this->baseUrl . '/getAmount';
- $resp = HttpClient::post($url, $payload);
- if(isset($resp['code'])&&$resp['code']==200){
- $data = json_decode($resp['business_data'], true);
- return [
- 'success' => true,
- 'balance' => $data['balance_amount_cent'],
- 'message' => '查询余额成功',
- ];
- }
- return [
- 'success' => false,
- 'balance' => 0,
- 'message' => '查询余额失败',
- ];
- }
- }
|