| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- namespace common\components\delivery\platform\shansong;
- use common\components\delivery\helpers\HttpClient;
- use common\components\delivery\helpers\SignHelper;
- use Yii;
- /**
- * Class Shop
- * 门店接口实现
- */
- class Shop extends Shansong
- {
- /**
- * 查询账号额度
- *
- * 接口文档:/openapi/developer/v5/getUserAccount
- *
- * 返回账户余额,单位为分
- *
- * @return array 返回格式:['success' => true/false, 'balance' => 0, 'message' => '']
- * balance: 账户余额,单位为分
- */
- public function getUserAccount()
- {
- $url = $this->baseUrl . '/openapi/developer/v5/getUserAccount';
-
- // 获取毫秒级时间戳
- $timestamp = (string) (int) (microtime(true) * 1000);
-
- // 构建签名参数(不包括 sign)
- $params = [
- 'clientId' => $this->clientId,
- 'accessToken' => $this->accessToken,
- 'timestamp' => $timestamp,
- ];
-
- // 计算签名(闪送使用 MD5 签名)
- $sign = SignHelper::makeSign(
- $params,
- $this->appSecret,
- 'md5',
- true,
- 'shansong'
- );
- $params['sign'] = $sign;
-
- $headers = ['Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'];
- $response = HttpClient::post($url, $params, $headers);
- Yii::info("[ShansongUserAccount] Response: " . json_encode($response));
-
- // 检查API响应状态
- if (isset($response['status']) && $response['status'] == 200) {
- return [
- 'success' => true,
- 'balance' => $response['data']['balance'],
- 'message' => '查询余额成功',
- ];
- }
-
- return [
- 'success' => false,
- 'balance' => 0,
- 'message' => '查询余额失败',
- ];
- }
- /**
- * 新增店铺
- *
- * 接口文档:/openapi/merchants/v5/storeOperation
- * 接口说明:新增店铺接口不支持并发调用,一个请求返回结果后才能进行下一次请求
- *
- * @param array $data 店铺信息,包括:
- * - storeName: 店铺名称 (必传, String(80))
- * - cityName: 城市名称 (必传, String(20))
- * - address: 店铺地址 (必传, String(80))
- * - addressDetail: 店铺详细地址 (必传, String(80))
- * - latitude: 店铺纬度-百度坐标系 (必传, String(20))
- * - longitude: 店铺经度-百度坐标系 (必传, String(20))
- * - phone: 店铺联系人手机号/座机 (必传, String(30))
- * - goodType: 店铺业务类型 (必传, Integer(8), 枚举: 1文件、3数码、5蛋糕、6餐饮、7鲜花、9汽配、10其他、12母婴、13医药健康、15商超、16水果)
- *
- * @return array 返回格式:['success' => true/false, 'storeId' => 0, 'message' => '']
- * storeId: 新增店铺ID (只在success=true时有效)
- */
- public function addShop($data)
- {
- $url = $this->baseUrl . '/openapi/merchants/v5/storeOperation';
-
- // 获取毫秒级时间戳
- $timestamp = (int) (microtime(true) * 1000);
-
- // 补充操作类型:1表示新增
- $data['operationType'] = 1;
-
- // 构建完整payload
- $payload = [
- 'clientId' => $this->clientId,
- 'shopId' => $this->accessToken, // 使用 accessToken 作为 shopId
- 'timestamp' => $timestamp,
- 'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
- ];
-
- // 计算签名
- $sign = SignHelper::makeSign($payload, $this->appSecret, 'md5', true, 'shansong');
- $payload['sign'] = $sign;
- $headers = ['Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'];
- $response = HttpClient::post($url, $payload, $headers);
- Yii::info("[ShansongAddShop] Response: " . json_encode($response));
-
- // 检查API响应状态
- if (isset($response['status']) && $response['status'] == 200 && isset($response['data'])) {
- return [
- 'success' => true,
- 'storeId' => $response['data'],
- 'message' => $response['msg'] ?? '店铺创建成功',
- ];
- }
-
- return [
- 'success' => false,
- 'storeId' => 0,
- 'message' => $response['msg'] ?? '店铺创建失败',
- ];
- }
- /**
- * 批量新增店铺
- *
- * 接口文档:/openapi/merchants/v5/addStores
- * 接口说明:每次最多批量新增10个店铺
- *
- * @param array $storeList 店铺列表数组,每个元素包括:
- * - thirdStoreId: 第三方店铺ID (非必传, String(50))
- * - storeName: 店铺名称 (必传, String(80))
- * - cityName: 城市名称 (必传, String(20))
- * - address: 店铺地址 (必传, String(80))
- * - addressDetail: 店铺详细地址 (必传, String(80))
- * - latitude: 店铺纬度-百度坐标系 (必传, String(20))
- * - longitude: 店铺经度-百度坐标系 (必传, String(20))
- * - phone: 店铺联系人手机号/座机 (必传, String(30))
- * - goodType: 店铺业务类型 (必传, Integer(8))
- *
- * @return array 返回格式:['success' => true/false, 'failList' => [], 'successList' => []]
- * failList: 失败列表,每项包含 dto 和 reason
- * successList: 成功列表,每项包含 dto 和 storeId
- */
- public function batchAddShops($storeList)
- {
- $url = $this->baseUrl . '/openapi/merchants/v5/addStores';
-
- // 获取毫秒级时间戳
- $timestamp = (int) (microtime(true) * 1000);
-
- // 构建完整payload
- $payload = [
- 'clientId' => $this->clientId,
- 'shopId' => $this->accessToken, // 使用 accessToken 作为 shopId
- 'timestamp' => $timestamp,
- 'data' => json_encode($storeList, JSON_UNESCAPED_UNICODE),
- ];
-
- // 计算签名
- $sign = SignHelper::makeSign($payload, $this->appSecret, 'md5', true, 'shansong');
- $payload['sign'] = $sign;
- $headers = ['Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'];
- $response = HttpClient::post($url, $payload, $headers);
- Yii::info("[ShansongBatchAddShops] Response: " . json_encode($response));
-
- // 检查API响应状态
- if (isset($response['status']) && $response['status'] == 200 && isset($response['data'])) {
- $respData = $response['data'];
- return [
- 'success' => true,
- 'failList' => $respData['failList'] ?? [],
- 'successList' => $respData['successList'] ?? [],
- ];
- }
-
- return [
- 'success' => false,
- 'failList' => [],
- 'successList' => [],
- ];
- }
- /**
- * 修改店铺
- *
- * 接口文档:/openapi/merchants/v5/storeOperation
- *
- * @param array $data 店铺信息,包括:
- * - storeId: 店铺ID (必传, Long(20))
- * - storeName: 店铺名称 (必传, String(80))
- * - cityName: 城市名称 (必传, String(20))
- * - address: 店铺地址 (必传, String(80))
- * - addressDetail: 店铺详细地址 (必传, String(80))
- * - latitude: 店铺纬度-百度坐标系 (必传, String(20))
- * - longitude: 店铺经度-百度坐标系 (必传, String(20))
- * - phone: 店铺联系人手机号/座机 (必传, String(30))
- * - goodType: 店铺业务类型 (必传, Integer(8))
- *
- * @return array 返回格式:['success' => true/false, 'storeId' => 0, 'message' => '']
- * storeId: 修改后的店铺ID (只在success=true时有效)
- */
- public function updateShop($data)
- {
- $url = $this->baseUrl . '/openapi/merchants/v5/storeOperation';
-
- // 获取毫秒级时间戳
- $timestamp = (int) (microtime(true) * 1000);
-
- // 补充操作类型:2表示修改
- $data['operationType'] = 2;
-
- // 构建完整payload
- $payload = [
- 'clientId' => $this->clientId,
- 'shopId' => $this->accessToken, // 使用 accessToken 作为 shopId
- 'timestamp' => $timestamp,
- 'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
- ];
-
- // 计算签名
- $sign = SignHelper::makeSign($payload, $this->appSecret, 'md5', true, 'shansong');
- $payload['sign'] = $sign;
- $headers = ['Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'];
- $response = HttpClient::post($url, $payload, $headers);
- Yii::info("[ShansongUpdateShop] Response: " . json_encode($response));
-
- // 检查API响应状态
- if (isset($response['status']) && $response['status'] == 200 && isset($response['data'])) {
- return [
- 'success' => true,
- 'storeId' => $response['data'],
- 'message' => $response['msg'] ?? '更新店铺成功',
- ];
- }
-
- return [
- 'success' => false,
- 'storeId' => 0,
- 'message' => $response['msg'] ?? '更新店铺失败',
- ];
- }
- /**
- * 分页查询商户店铺
- *
- * 接口文档:/openapi/merchants/v5/queryAllStores
- * 接口说明:分页查询商户创建店铺,供订单计费使用。仅状态为审核通过的店铺可计费使用
- *
- * @param array $params 查询参数,包括:
- * - pageNo: 页码 (必传, Integer(8), 最小值为1)
- * - pageSize: 每页数量 (必传, Integer(8), 最小值为1, 最大值为100)
- * - storeName: 店铺名称 (非必传, String(20), 模糊搜索)
- *
- * @return array 返回格式:['success' => true/false, 'data' => [], 'message' => '']
- * data: 店铺列表数组,每项包含:
- * - storeId: 店铺ID
- * - storeName: 店铺名称
- * - cityId: 城市ID
- * - cityName: 城市名称
- * - contactsMobile: 店铺联系人手机号
- * - address: 店铺地址
- * - addressDetail: 店铺地址详情
- * - latitude: 纬度
- * - longitude: 经度
- * - goodType: 店铺类型标签
- * - goodName: 店铺类型标签名称
- * - status: 店铺状态 (0-禁用、1-待审核、2-驳回、3-审核通过)
- * - totalNum: 店铺总条数
- */
- public function queryShops($params)
- {
- $url = $this->baseUrl . '/openapi/merchants/v5/queryAllStores';
-
- // 获取毫秒级时间戳
- $timestamp = (int) (microtime(true) * 1000);
-
- // 验证必传参数
- if (!isset($params['pageNo']) || !isset($params['pageSize'])) {
- return [
- 'success' => false,
- 'data' => [],
- 'message' => '缺少必传参数:pageNo 或 pageSize',
- ];
- }
-
- // 构建完整payload
- $payload = [
- 'clientId' => $this->clientId,
- 'shopId' => $this->accessToken, // 使用 accessToken 作为 shopId
- 'timestamp' => $timestamp,
- 'data' => json_encode($params, JSON_UNESCAPED_UNICODE),
- ];
-
- // 计算签名
- $sign = SignHelper::makeSign($payload, $this->appSecret, 'md5', true, 'shansong');
- $payload['sign'] = $sign;
- $headers = ['Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'];
- $response = HttpClient::post($url, $payload, $headers);
- Yii::info("[ShansongQueryShops] Response: " . json_encode($response));
-
- // 检查API响应状态
- if (isset($response['status']) && $response['status'] == 200 && isset($response['data'])) {
- return [
- 'success' => true,
- 'data' => $response['data'],
- 'message' => '查询店铺成功',
- ];
- }
-
- return [
- 'success' => false,
- 'data' => [],
- 'message' => $response['msg'] ?? '查询店铺失败',
- ];
- }
- }
|