ThinkPHP(FastAdmin)快递100订阅快递信息

快递信息订阅后快递100的服务器会请求callback地址来传递快递信息示例代码如下:

<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\KuaidiLog;

class Kuaidi extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
     * 测试绑定快递
     * @return void
     *
     */
    public function poll(){
        $res = KuaidiLog::poll('yuantong','YT********818','156*******');
        $this->success('获取成功',$res);
    }

    /**
     * 快递100的通知推送
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function callback(){
        $res = KuaidiLog::callBack($_POST);
        if($res){
            //给快递100返回处理成功结果
            return new \think\response\Json(['result'=>true,'returnCode'=>200,'message'=>'成功']);
        }
        exit();
    }
}
<?php

namespace app\common\model;

use think\Model;
use fast\Http;
class KuaidiLog extends Model
{
    /**
     * 订阅接口逻辑
     * @param $company
     * @param $number
     * @param $phone
     * @return mixed
     */
    public static function poll($company,$number,$phone){
        $param['company'] = $company;
        $param['number'] = $number;
        $param['key'] = 'MA*******207';//快递100授权码,请申请企业版获取
        //快递100推送地址
        $param['parameters']['callbackurl'] = request()->domain()."/api/kuaidi/callback";
        $param['parameters']['phone'] = $phone;
        $param['parameters']['resultv2'] = '4';

        // 请求参数
        $post_data = array();
        $post_data['schema'] = 'json';
        $post_data['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);

        $url = 'https://poll.kuaidi100.com/poll';    // 订阅请求地址

        // 发送post请求
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        $data = json_decode($result, true);

        return $data;
    }

    /**
     * 通知推送逻辑
     * @param $post
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function callBack($post){
        $post = json_decode($post['param'],true);
        $kuaidigongsi = $post['lastResult']['com'];
        $kuaididanhao = $post['lastResult']['nu'];
        $lastResult = json_encode($post);
        $log = self::where('kuaididanhao',$kuaididanhao)
            ->where('kuaidigongsi',$kuaidigongsi)
            ->find();
        if(empty($log)){
            self::create([
                'kuaidigongsi'=>$kuaidigongsi,
                'kuaididanhao'=>$kuaididanhao,
                'lastResult'=>$lastResult,
                'createtime'=>time(),
                'updatetime'=>time(),
            ]);
        }else{
            $log->lastResult = $lastResult;
            $log->updatetime = time();
            $log->save();
        }
        return true;
    }
}

KuaidiLog对应的数据表

CREATE TABLE `fa_kuaidi_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '11',
  `kuaidigongsi` varchar(255) DEFAULT NULL COMMENT '快递公司:yuantong=圆通,shentong=申通,jtexpress=极兔速递,zhongtong=中通快递,yunda=韵达快递,youzhengguonei=邮政快递包裹,shunfeng=顺丰速运,jd=京东物流',
  `kuaididanhao` varchar(255) DEFAULT NULL COMMENT '快递单号',
  `lastResult` text COMMENT '最新查询结果',
  `createtime` bigint(11) DEFAULT NULL,
  `updatetime` bigint(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COMMENT='快递推送记录';

 这样快递就完成了订阅,快递动态就被保存在了fa_kuaidi_log表中,可通过快递单号获取动态用于项目中的展示。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
关于 FastAdmin 或者 ThinkPHP 的 iOS 支付代码,可以按照以下步骤进行: 1. 在苹果开发者中心创建 App ID,并开通 In-App Purchase 功能。 2. 在 Xcode 中创建一个新的 iOS 项目,并在项目设置中配置好 Bundle ID 和开发者账号。 3. 添加 StoreKit.framework 和 StoreKit 库文件到项目中。 4. 在代码中导入 StoreKit 框架,并实现 SKPaymentTransactionObserver 协议和 SKProductsRequestDelegate 协议。 5. 调用 SKProductsRequest 请求商品信息,并在回调中获取到商品信息。 6. 根据商品信息调用 SKPaymentQueue 发起支付请求,并在回调中处理支付结果。 以下是一个简单的示例代码: ```swift import UIKit import StoreKit class ViewController: UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate { override func viewDidLoad() { super.viewDidLoad() // 监听支付状态 SKPaymentQueue.default().add(self) } // 请求商品信息 func requestProductsInfo() { let productIdentifiers = Set(["com.example.product1"]) let request = SKProductsRequest(productIdentifiers: productIdentifiers) request.delegate = self request.start() } // 获取商品信息回调 func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { if response.products.count > 0 { let product = response.products[0] let payment = SKPayment(product: product) SKPaymentQueue.default().add(payment) } } // 支付状态回调 func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { for transaction in transactions { switch transaction.transactionState { case .purchased: // 支付成功 SKPaymentQueue.default().finishTransaction(transaction) break case .failed: // 支付失败 SKPaymentQueue.default().finishTransaction(transaction) break case .restored: // 恢复购买 SKPaymentQueue.default().finishTransaction(transaction) break default: break } } } } ``` 这个示例代码中,通过实现 SKPaymentTransactionObserver 协议和 SKProductsRequestDelegate 协议,请求商品信息并发起支付请求,然后在支付状态回调中处理支付结果。需要注意的是,这个示例代码中的商品 ID 需要根据实际情况进行替换。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值