| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- <?php
- namespace app\controller\api;
- use app\common\enum\CommonEnum;
- use app\common\repositories\merchant\order\OrderMerchantRepository;
- use app\common\repositories\shared\SharedProsperityRecommendConfigRepository;
- use app\common\repositories\user\UserRepository;
- use app\services\ThirdParty\AlibabaAgent\OrderService;
- use app\services\ThirdParty\AlibabaAgent\ProductService;
- use applet\WXBizDataCrypt;
- use think\facade\Db;
- /**
- * 1688分销严选采购解决方案 - 接口调用示例
- *
- * 使用前请确保 config/third_party.php 中已配置 alibaba_agent 项:
- * 'alibaba_agent' => [
- * 'appKey' => '你的AppKey',
- * 'appSecret' => '你的AppSecret',
- * 'gatewayUrl' => 'https://gw.open.1688.com/openapi/',
- * ]
- *
- * access_token 通过 OAuth 2.0 授权获取,或由用户提供。
- */
- class Test
- {
- /**
- * 1688分销商品列表 - 调用示例
- *
- * API: com.alibaba.fenxiao:jxhy.product.getPageList-1
- * 文档: https://open.1688.com/api/apidocdetail.htm?id=com.alibaba.fenxiao%3Ajxhy.product.getPageList-1
- */
- public function test()
- {
- $json = [
- 'created_time' => '1775884033',
- 'data' => [
- 'app_id' => 'app_44108546-d27a-48ee-88c1-84b45b75114f',
- 'business_mode' => '00',
- 'created_time' => '20260411130713',
- 'description' => '',
- 'end_time' => '20260411130724',
- 'expend' => [
- 'buyer_logon_id' => '131****9459',
- 'cashPayAmt' => '298.00',
- 'open_id' => '2088702188085230',
- 'sub_open_id' => '2088702188085230',
- ],
- 'fee_amt' => '1.13',
- 'id' => '002212026041113071210888906215589441536',
- 'order_no' => 'ALI177588403255323243_466',
- 'out_trans_id' => '2026041123001485231459472633',
- 'party_order_id' => '02232604114723298914940',
- 'pay_amt' => '298.00',
- 'pay_channel' => 'alipay_pub',
- 'real_amt' => '298.00',
- 'status' => 'succeeded',
- 'trans_response_add_info' => [
- 'fund_bill_list' => [
- [
- 'amount' => '298.00',
- 'fund_channel' => 'ALIPAYACCOUNT',
- ],
- ],
- ],
- ],
- 'prod_mode' => 'true',
- 'sign' => 'fgbSc95NQDor1/jH3/L0G55l3XB7wl+sDt45Mtig/8eSvLIVo0uoyyD+jWaLkXV5xY2iAV3w8W4tiyVUcMQwVvwDIt9zQ44HTo05ng0e6CHCXLxFQbg/SHsjmK/3hin/2t6HkEjlitJYVikcPxMA0vnlR+lL/xMOYqNMDuq9oQQ=',
- 'id' => '002210888906265044500480',
- 'type' => 'HF',
- 'app_id' => 'app_44108546-d27a-48ee-88c1-84b45b75114f',
- 'object' => 'payment',
- ];
- /** @var OrderMerchantRepository $orderMerchantRepository */
- $orderMerchantRepository = app()->make(OrderMerchantRepository::class);
- $res = $orderMerchantRepository->doNotifyHF($json['data']);
- dd($res);
- die;
- $orderService = new OrderService();
- //
- //
- // $orderId = '3298535043727138963';
- // $payRes = $orderService->prepareProtocolPay(['orderId'=>$orderId]);
- // dd($payRes);
- $cargoParamList = '{"offerId":"1045816431082","specId":"2fc39c573acfab640d10bcb1b533f041","quantity":1}';
- $addressParam = '{"fullName":"测试","mobile":"17756507016","provinceText":"河北省","cityText":"廊坊市","areaText":"三河市","address":"兴远高科产业园23号楼2层210"}';
- $cargoParamList = json_decode($cargoParamList,true);
- $addressParam = json_decode($addressParam,true);
- $flow = 'general';
- $res = $orderService->previewOrder(['cargoParamList' => $cargoParamList, 'addressParam' => $addressParam,'flow'=>$flow]);
- dd($res);
- /** @var SharedProsperityRecommendConfigRepository $sharedProsperityRecommendConfigRepository */
- $sharedProsperityRecommendConfigRepository = app()->make(SharedProsperityRecommendConfigRepository::class);
- // 共富配置--获取最大的直推奖比例
- $sharedProsperityRecommendConfigEntityByMaxRewardRatio = $sharedProsperityRecommendConfigRepository->getSharedProsperityRecommendConfigEntityByMaxRewardRatio();
- // 共富配置--获取最大的分享奖比例
- $sharedProsperityRecommendConfigEntityByMaxShareRewardRatio = $sharedProsperityRecommendConfigRepository->getSharedProsperityRecommendConfigEntityByMaxShareRatio();
- // 利润 减去 顶格的直推奖,剩下的是PV值
- $sharPveRate = bcadd($sharedProsperityRecommendConfigEntityByMaxRewardRatio->getRewardRatio(),$sharedProsperityRecommendConfigEntityByMaxShareRewardRatio->getShareRatio(), CommonEnum::DECIMAL_PLACES_OF_THE_AMOUNT);
- $pvProportion = bcsub('1', bcdiv($sharPveRate, '100', 4), 2);
- dd($pvProportion);
- $orderShareUid = 6;
- $ShareUser = Db::name('shared_prosperity_user')->where(["user_id" => $orderShareUid])->find();
- if ($ShareUser) {
- $rate = Db::name('shared_prosperity_recommend_config')->where(["level" => $ShareUser['partner_level']])->value("share_ratio");
- $rate = $rate / 100;
- }else{
- // 计算 佣金
- $user_group_share_rate = Db::name("system_config_value")->where(["config_key" => "user_group_share_rate"])->value("value");
- $user_group_share_rate = json_decode($user_group_share_rate, true);
- $user_group_share_rate = array_column($user_group_share_rate, 'rate', 'user_groub');
- /** @var UserRepository $userRepository */
- $userRepository = app()->make(UserRepository::class);
- $userEntityByShareUserEntity = $userRepository->getUserEntityByUid($orderShareUid);
- $userGroup = $userEntityByShareUserEntity->getUserGroup();
- $rate = ($user_group_share_rate[$userGroup] ?? 0) / 100;
- }
- dd($rate);
- die;
- $data = [
- 'orderId' => '123', // 1688订单号,接口里
- 'orderEntryIds' => '123',// 1688子订单号,接口里
- 'disputeRequest' => 'refund', // 单选
- 'description' => '不想要了', // 填写的
- 'applyPayment' => '4.82', // 退款金额,接口里
- 'applyCarriage' => '0', // 运费金额,接口里
- 'applyReasonId' => '20006', // 退款原因,选的
- 'goodsStatus' => 'refundWaitBuyerReceive', // 货物状态,接口里
- 'refund_order_id' => 33 // 列表的refund_order_id
- ];
- $orderService = new OrderService();
- // 构建1688接口参数(前端 disputeRequest 映射为 1688 接口的 disputeReasonId)
- $params = [
- 'orderId' => (string)$data['orderId'],
- 'orderEntryIds' => $data['orderEntryIds'],
- 'disputeReasonId' => (string)$data['disputeRequest'],
- 'description' => (string)$data['description'],
- 'applyPayment' => (string)$data['applyPayment'] * 100,
- 'applyCarriage' => (string)$data['applyCarriage'] * 100,
- 'applyReasonId' => (string)$data['applyReasonId'],
- 'goodsStatus' => (string)$data['goodsStatus'],
- ];
- // 调用1688 API提交售后申请
- $result = $orderService->createRefund($params);
- // 记录到 store_refund_alibaba 表(不管成功与否都保存原始数据)
- $insertData = [
- 'refund_order_id' => (int)($data['refund_order_id'] ?? 0),
- 'alibaba_order_id' => (string)$data['orderId'],
- 'order_entry_ids' => $data['orderEntryIds'],
- 'dispute_reason_id' => (string)$data['disputeRequest'],
- 'description' => (string)$data['description'],
- 'goods_status' => (string)$data['goodsStatus'],
- 'apply_payment' => (string)$data['applyPayment'],
- 'apply_carriage' => (string)$data['applyCarriage'],
- 'result_data' => json_encode($result['rawData'] ?? [], JSON_UNESCAPED_UNICODE),
- ];
- if ($result['success']) {
- // 提交成功
- $insertData['status'] = 1;
- $insertData['alibaba_refund_id'] = $result['refundId'] ?? '';
- $insertData['alibaba_status'] = $result['status'] ?? '';
- Db::name('store_refund_alibaba')->insert($insertData);
- // 更新退款单的 after_sn 字段为1688售后单ID
- if (!empty($data['refund_order_id']) && !empty($result['refundId'])) {
- Db::name('store_refund_order')
- ->where('refund_order_id', (int)$data['refund_order_id'])
- ->update(['after_sn' => $result['refundId']]);
- }
- return app('json')->success([
- 'alibaba_refund_id' => $result['refundId'] ?? '',
- 'alibaba_status' => $result['status'] ?? '',
- ], '提交1688售后申请成功');
- } else {
- // 提交失败
- $insertData['status'] = 2;
- $insertData['fail_message'] = $result['status'] ?? '1688接口调用失败';
- Db::name('store_refund_alibaba')->insert($insertData);
- return app('json')->fail($result['status'] ?? '提交1688售后申请失败');
- }
- // $alibabaOrderId = '3300284931845138963';
- // $orderService = new OrderService();
- // $result = $orderService->getRefundReasonList(['orderId' => $alibabaOrderId]);
- // dd($result);
- die;
- // $id = $this->request->params(['pid'])['pid'];
- $id = request()->params(['pid'])['pid'];
- if (!$id) {
- return app('json')->fail('参数不正确');
- }
- dd(request());
- $appid = 'wx9082e5ac2fdb513f';
- $sessionKey =str_replace(' ','+', 'AIXeWyu4hRRg0EkD8Z23JA==');
- $encryptedData=str_replace(' ','+','7OCmD5\/4xylVdxYswuTrA6TKzmXNkDymIpdyx9GZIBS+qCIcsBpUnJ5U5GXOTn0JvfQYic9e+0idCTP7QJyJkjjlGWNwwiE0\/WnhBvhAWYlo+Hkh+v3ioiSvqJ7zC4bEJ3GWDNyts5fEX\/u+tL25DySDuUoDoCJs6k+ThlVO5NiecPIcgnmfrLG6nV4Xh3y9H6p\/loj71LRJ47hywX9OQg==');
- $iv =str_replace(' ','+', 'NbjIV0k1DyDWxU7JSSF44w==');
- $pc = new WXBizDataCrypt($appid, $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $data );
- dd($errCode);
- die;
- die;
- // ============================================================
- // 通过 ProductService 调用 1688 分销商品列表
- // ============================================================
- try {
- /** @var ProductService $productService */
- $productService = app()->make(ProductService::class);
- // 设置 access_token(由用户提供或通过OAuth获取)
- $productService->setAccessToken('dc8eab20-f10f-4688-9e45-9ebfa182c7c2');
- // 搜索参数
- $params = [
- 'page' => 1, // 页码
- 'limit' => 50, // 每页条数(最大50)
- ];
- $result = $productService->searchProducts($params);
- if ($result === false) {
- echo "请求失败,请查看日志获取详细信息。\n";
- } else {
- echo "=== 1688分销商品列表 ===\n";
- echo "总数: {$result['total']}\n";
- echo "页码: {$result['page']}\n";
- echo "每页: {$result['pageSize']}\n";
- echo "商品数: " . count($result['list']) . "\n\n";
- foreach ($result['list'] as $i => $product) {
- echo ($i + 1) . ". {$product['title']}\n";
- echo " 商品ID: {$product['productId']}\n";
- echo " 价格: ¥{$product['minPrice']} ~ ¥{$product['maxPrice']}\n";
- echo " 月销: {$product['monthSales']}\n";
- echo " 主图: {$product['mainImage']}\n\n";
- }
- }
- } catch (\Throwable $e) {
- echo "错误: " . $e->getMessage() . "\n";
- echo "文件: " . $e->getFile() . " (行 " . $e->getLine() . ")\n";
- }
- die;
- }
- public function posttest()
- {
- $sessionKey = request()->post(['sessionKey']);
- $encryptedData = request()->post(['encryptedData']);
- $iv = request()->post(['iv']);
- $appid = 'wx9082e5ac2fdb513f';
- $sessionKey =str_replace(' ','+', stripslashes($sessionKey['sessionKey']));
- $encryptedData=str_replace(' ','+',$encryptedData['encryptedData']);
- $iv =str_replace(' ','+', stripslashes($iv['iv']));
-
- // $sessionKey =str_replace(' ','+', $sessionKey['sessionKey']);
- // $encryptedData=str_replace(' ','+',$encryptedData['encryptedData']);
- // $iv =str_replace(' ','+', $iv['iv']);
- $pc = new WXBizDataCrypt($appid, $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $data );
- dd($errCode);
- }
- /**
- * 直接调用示例(不通过ProductService,展示完整流程)
- */
- public function direct()
- {
- $host = 'https://gw.open.1688.com/openapi/';
- $appKey = '6512262';
- $appSecret = 'uNPRZE7895';
- $accessToken = 'dc8eab20-f10f-4688-9e45-9ebfa182c7c2';
- $version = 1;
- $namespace = 'com.alibaba.fenxiao';
- $apiName = 'jxhy.product.getPageList';
- // 1. 构建URL
- $param = "param2/{$version}/";
- $path = "{$namespace}/{$apiName}/{$appKey}";
- $url = $host . $param . $path;
- // 2. 请求参数
- $data = [
- 'access_token' => $accessToken,
- 'pageNo' => 1,
- 'pageSize' => 10,
- ];
- // 3. 生成签名
- $aliParams = [];
- foreach ($data as $key => $val) {
- $aliParams[] = $key . $val;
- }
- sort($aliParams);
- $signStr = $param . $path . join('', $aliParams);
- $data['_aop_signature'] = strtoupper(bin2hex(
- hash_hmac("sha1", $signStr, $appSecret, true)
- ));
- // 4. 发送POST请求
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- $result = json_decode($response, true);
- echo "HTTP状态码: {$httpCode}\n";
- dump($result);
- die;
- }
- /**
- * 1688分销商品详情 - 调用示例
- *
- * API: com.alibaba.fenxiao:alibaba.pifatuan.product.detail.list-2
- * 文档: https://open.1688.com/api/apidocdetail.htm?id=com.alibaba.fenxiao%3Aalibaba.pifatuan.product.detail.list-2
- */
- public function detail()
- {
- try {
- /** @var ProductService $productService */
- $productService = app()->make(ProductService::class);
- // 设置 access_token
- $productService->setAccessToken('dc8eab20-f10f-4688-9e45-9ebfa182c7c2');
- // 商品ID(从列表接口获取到的 itemId)
- $productId = 642639328505;
- echo "=== 获取商品详情 ===\n";
- echo "商品ID: {$productId}\n\n";
- $result = $productService->getProductDetail($productId);
- echo "=== 原始返回 ===\n";
- var_dump($result);
- echo "\n";
- if ($result === false) {
- echo "请求失败,请查看日志获取详细信息。\n";
- } else {
- echo "=== 商品详情结果 ===\n";
- echo "标题: " . ($result['title'] ?? 'N/A') . "\n";
- echo "商品ID: " . ($result['productId'] ?? 'N/A') . "\n";
- echo "价格: ¥" . ($result['minPrice'] ?? 0) . " ~ ¥" . ($result['maxPrice'] ?? 0) . "\n";
- echo "库存: " . ($result['stock'] ?? 0) . "\n";
- echo "主图: " . ($result['mainImage'] ?? 'N/A') . "\n";
- echo "SKU数: " . (isset($result['skuList']) ? count($result['skuList']) : 0) . "\n";
- echo "图片数: " . (isset($result['imageList']) ? count($result['imageList']) : 0) . "\n";
- }
- } catch (\Throwable $e) {
- echo "错误: " . $e->getMessage() . "\n";
- echo "文件: " . $e->getFile() . " (行 " . $e->getLine() . ")\n";
- }
- die;
- }
- }
|