Shop.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace common\components\delivery\platform\fengniao;
  3. use Yii;
  4. use common\components\delivery\helpers\HttpClient;
  5. use bizGhs\express\classes\DeliveryAuthTokenClass;
  6. /**
  7. * Class Shop
  8. * 门店接口实现
  9. */
  10. class Shop extends Fengniao
  11. {
  12. /**
  13. * 门店详情查询接口
  14. */
  15. public function chainstoreQuery()
  16. {
  17. }
  18. /**
  19. * 门店批量查询接口
  20. *
  21. * 根据蜂鸟官方文档实现门店批量查询功能。
  22. * 可按商户ID查询所有门店信息,支持分页。
  23. *
  24. * @param array $data 查询参数,结构参考:
  25. * [
  26. * // ========== 必填参数 ==========
  27. * 'merchant_id' => '商户ID(Long类型)',
  28. *
  29. * // ========== 可选参数 ==========
  30. * 'page_no' => '分页页码(默认从1开始)',
  31. * 'page_size' => '每页条数(默认1000)',
  32. * ]
  33. */
  34. public function chainstoreQueryList($data = [])
  35. {
  36. // 验证 merchant_id 必填
  37. $merchantId = $data['merchant_id'] ?? $this->merchantId;
  38. if (empty($merchantId)) {
  39. Yii::warning("[FengniaoAdapter] chainstoreQueryList failed: merchant_id is required");
  40. return [
  41. 'code' => -1,
  42. 'msg' => 'merchant_id is required',
  43. 'data' => null
  44. ];
  45. }
  46. // 构建业务数据
  47. $businessData = [];
  48. // 可选参数 - 分页信息
  49. if (isset($data['page_no']) && $data['page_no'] !== null) {
  50. $businessData['page_no'] = (int)$data['page_no'];
  51. }
  52. $businessData['page_size'] = 1000; // 设置每页获取数为 1000
  53. if (isset($data['page_size']) && $data['page_size'] !== null) {
  54. $businessData['page_size'] = (int)$data['page_size'];
  55. }
  56. // 构建请求参数
  57. $payload = $this->buildRequestPayload('chainstoreQueryList', $businessData);
  58. // 发送请求
  59. $url = $this->baseUrl . '/chainstoreQueryList';
  60. $resp = HttpClient::post($url, $payload, [
  61. 'Content-Type' => 'application/json',
  62. ]);
  63. // 处理响应
  64. if (isset($resp['code']) && $resp['code'] == '200' && isset($resp['business_data'])) {
  65. // business_data 是 JSON 字符串,需要解析
  66. $respData = is_string($resp['business_data']) ? json_decode($resp['business_data'], true) : $resp['business_data'];
  67. if (is_null($respData)) {
  68. Yii::error("[FengniaoAdapter] Failed to parse business_data: " . $resp['business_data']);
  69. return [
  70. 'code' => -1,
  71. 'msg' => 'Failed to parse response data',
  72. 'data' => null
  73. ];
  74. }
  75. // 解析门店列表
  76. $chainStoreList = [];
  77. if (!empty($respData['list']) && is_array($respData['list'])) {
  78. foreach ($respData['list'] as $store) {
  79. $chainStoreList[] = [
  80. 'chain_store_id' => (int)($store['chain_store_id'] ?? 0),
  81. 'name' => $store['name'] ?? '',
  82. 'branch_name' => $store['branch_name'] ?? '',
  83. 'address' => $store['address'] ?? '',
  84. 'latitude' => (double)($store['latitude'] ?? 0),
  85. 'longitude' => (double)($store['longitude'] ?? 0),
  86. 'merchant_id' => (int)($store['merchant_id'] ?? 0),
  87. 'out_shop_code' => $store['out_shop_code'] ?? '',
  88. 'chainstore_type' => (int)($store['chainstore_type'] ?? 1),
  89. 'chainstore_type_desc' => $store['chainstore_type_desc'] ?? '',
  90. 'position_source' => (int)($store['position_source'] ?? 0),
  91. 'position_source_desc' => $store['position_source_desc'] ?? '',
  92. 'status' => (int)($store['status'] ?? 0),
  93. 'status_desc' => $store['status_desc'] ?? '',
  94. 'modify_status' => (int)($store['modify_status'] ?? 0),
  95. 'modify_status_desc' => $store['modify_status_desc'] ?? '',
  96. ];
  97. }
  98. }
  99. return [
  100. 'code' => 0,
  101. 'data' => [
  102. 'page_no' => (int)($respData['page_no'] ?? 0),
  103. 'page_size' => (int)($respData['page_size'] ?? 0),
  104. 'total_page' => (int)($respData['total_page'] ?? 0),
  105. 'total_count' => (int)($respData['total_count'] ?? 0),
  106. 'list' => $chainStoreList,
  107. ]
  108. ];
  109. }
  110. return [
  111. 'code' => (int)($resp['code'] ?? -1),
  112. 'msg' => $resp['msg'] ?? 'Unknown error',
  113. 'data' => null
  114. ];
  115. }
  116. public function bindStore($mainId, $storeId)
  117. {
  118. $deliveryAuthToken = DeliveryAuthTokenClass::getByCondition(['platform'=>'fengniao', 'mainId'=>$mainId], true);
  119. if(!$deliveryAuthToken){
  120. Yii::error('蜂鸟平台未授权');
  121. return false;
  122. }
  123. $deliveryAuthToken->shopId = $storeId;
  124. $deliveryAuthToken->save();
  125. return true;
  126. }
  127. public function unbindStore($mainId, $storeId)
  128. {
  129. $deliveryAuthToken = DeliveryAuthTokenClass::getByCondition(['platform'=>'fengniao', 'mainId'=>$mainId], true);
  130. if(!$deliveryAuthToken){
  131. Yii::error('蜂鸟平台未授权');
  132. return false;
  133. }
  134. $deliveryAuthToken->shopId = '';
  135. $deliveryAuthToken->save();
  136. return true;
  137. }
  138. /**
  139. * 查询商户余额接口
  140. */
  141. public function getShopAccountBalance()
  142. {
  143. $businessData = [
  144. 'merchant_id' => $this->merchantId,
  145. ];
  146. $payload = $this->buildRequestPayload('getAmount', $businessData);
  147. $url = $this->baseUrl . '/getAmount';
  148. $resp = HttpClient::post($url, $payload);
  149. if(isset($resp['code'])&&$resp['code']==200){
  150. $data = json_decode($resp['business_data'], true);
  151. return [
  152. 'success' => true,
  153. 'balance' => $data['balance_amount_cent'],
  154. 'message' => '查询余额成功',
  155. ];
  156. }
  157. return [
  158. 'success' => false,
  159. 'balance' => 0,
  160. 'message' => '查询余额失败',
  161. ];
  162. }
  163. }