Yii2框架封装sdk

<?php
namespace common\sdks\brandDoctor;

use common\libs\CryptoTools;

class BrandDoctorSdk
{	
	
	public static function getInstance()
    {
        if (static::$_instance === null) {
            return new static;
        }
        return static::$_instance;
    }
	
    protected function __construct()
    {  
        $this->domain = \Yii::$app->params['api']['brandDoctor']['url'];
        $this->appkey = \Yii::$app->params['api']['brandDoctor']['appkey'];
        $this->appid = \Yii::$app->params['api']['brandDoctor']['appid'];
        $this->os = \Yii::$app->params['api']['brandDoctor']['os'];
        $this->version = \Yii::$app->params['api']['brandDoctor']['version'];
        //设置私钥
        CryptoTools::setKey($this->appkey);
        //基础参数拼接
        $this->baseParams = [
            'appid' => $this->appid,
            'noncestr' => $this->getRandChar(6),
            'os' => $this->os,
            'version' => $this->version,
            'time' => time(),
        ];
    }

    /**
     * 路由映射
     */
    private $routeMap = [
        'get-patient-list' => 'account/interrogation/patient-list', //获取就诊人列表
        'get-patient-detail' => 'account/interrogation/get-patient', //获取就诊人详情
        'save-patient' => 'account/interrogation/submit-patient', //添加、编辑就诊人信息
        'del-patient' => 'account/interrogation/del-patient',//删除就诊人
    ];

    /**
     * 删除就诊人信息(不参与业务)
     */
    public function deletePatient($uc_login_key = '', $patient_id = 0)
    {
        //请求参数
        $params = [];
        $params['uc_login_key'] = $uc_login_key;
        $params['patient_id'] = $patient_id;
        //非必填参数
        $params['is_card'] = 1;//是否检验实名认证 0 不需要 1 需要

        //获取签名
        $data = CryptoTools::getEncryptArray($params);
        $this->baseParams['data'] = $data['data'];
        $url = $this->domain. $this->routeMap['del-patient'] . '?' . http_build_query($this->baseParams);

        $result = $this->requestUrl($url, $this->baseParams);

        if(is_array($result) && $result['code'] == 200){
            return $result;
        }else{
            $errMsg = is_array($result) && isset($result['msg']) ? $result['msg'] : '';
            YiiLog::warning("删除就诊人失败! ErrMsg:" . $errMsg, 'brandDoctor-sdk');
            return $result;
        }
    }



    /**
     * 就诊人添加、编辑
     */
    public function savePatient($uc_login_key = '', $formData = [], $patient_id = 0)
    {
        //将关系key => value位置调换
        $brandDoctorRelationMap = array_flip(MedicalEscortOrder::$relationMap);

        if (array_key_exists($formData['relationship'], $brandDoctorRelationMap)) {
            $relationCode = $brandDoctorRelationMap[$formData['relationship']];
        } else {
            $relationCode = 0;
        }
        //请求参数
        $params = [];
        $params['uc_login_key'] = $uc_login_key;
        $params['realname'] = $formData['realname'];
        $params['id_card'] = $formData['id_card'];
        $params['tel'] = $formData['tel'];
        $params['relationship'] = $relationCode;
        $params['platform'] = 2;//默认参数,隶属华电
        //编辑时追加就诊人id
        if ($patient_id != 0) {
            $params['patient_id'] = $patient_id;
        }
        //获取签名
        $data = CryptoTools::getEncryptArray($params);

        $this->baseParams['data'] = $data['data'];
        $url = $this->domain . $this->routeMap['save-patient'] . '?' . http_build_query($this->baseParams);

        $result = $this->requestUrl($url, $this->baseParams);
        if(is_array($result) && $result['code'] == 200){
            return $result;
        }else{
            $errMsg = is_array($result) && isset($result['msg']) ? $result['msg'] : '';
            YiiLog::warning("更新就诊人信息失败! ErrMsg:" . $errMsg, 'brandDoctor-sdk');
            return $result;
        }
    }

    /**
     * 获取就诊人详情
     */
    public function getPatientDetail($uc_login_key = '', $patient_id = 0)
    {
        //请求参数
        $params = [];
        $params['uc_login_key'] = $uc_login_key;
        $params['patient_id'] = $patient_id;
        //非必填参数
        $params['is_card'] = 1;//是否检验实名认证 0 不需要 1 需要

        //获取签名
        $data = CryptoTools::getEncryptArray($params);
        $this->baseParams['data'] = $data['data'];
        $url = $this->domain. $this->routeMap['get-patient-detail'] . '?' . http_build_query($this->baseParams);

        $result = $this->requestUrl($url);

        if(is_array($result) && $result['code'] == 200){
            return $result;
        }else{
            $errMsg = is_array($result) && isset($result['msg']) ? $result['msg'] : '';
            YiiLog::warning("获取就诊人详情失败! ErrMsg:" . $errMsg, 'brandDoctor-sdk');
            return $result;
        }
    }

    /**
     * 获取就诊人列表
     */
    public function getPatientList($uc_login_key = '', $page = 1, $pagesize = 20)
    {
        //请求参数
        $params = [];
        $params['uc_login_key'] = $uc_login_key;
        $params['page'] = $page;
        $params['pagesize'] = $pagesize;

        //获取签名
        $data = CryptoTools::getEncryptArray($params);
        $this->baseParams['data'] = $data['data'];

        $url = $this->domain . $this->routeMap['get-patient-list'] . '?' . http_build_query($this->baseParams);

        $result = $this->requestUrl($url);

        if(is_array($result) && $result['code'] == 200){
            return $result;
        }else{
            $errMsg = is_array($result) && isset($result['msg']) ? $result['msg'] : '';
            YiiLog::warning("获取就诊人列表失败! ErrMsg:" . $errMsg, 'brandDoctor-sdk');
            return $result;
        }
    }


    /**
     * 请求URL 有params为post请求无则get请求
     */
    public function requestUrl($url, $params = [], $timeout = 10)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($params)) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        }
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $this->result = curl_exec($ch);

        if (curl_errno($ch)) {
            $this->curlError(curl_error($ch), curl_errno($ch));
        }
        curl_close($ch);
        $data = json_decode($this->result, true);
        //解密数据并重新赋值data
        if (!empty($data['data'])) {
            $data['data'] = CryptoTools::AES256ECBDecrypt($data['data']);
        }
        return $data;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Yii2框架的Active Record是一个强大的ORM(对象关系映射)工具,它可以将数据库中的表映射成为Yii2框架中的PHP类,并且提供了一系列的API来进行对数据库的操作。 下面是一些常用的Active Record操作: 1. 查询单条记录: ```php $model = ModelName::findOne($id); ``` 2. 查询多条记录: ```php $models = ModelName::find()->where(['attribute' => 'value'])->all(); ``` 3. 插入记录: ```php $model = new ModelName(); $model->attribute = 'value'; $model->save(); ``` 4. 更新记录: ```php $model = ModelName::findOne($id); $model->attribute = 'new value'; $model->save(); ``` 5. 删除记录: ```php $model = ModelName::findOne($id); $model->delete(); ``` 6. 自定义查询: ```php $models = ModelName::find()->select(['attribute1', 'attribute2'])->asArray()->all(); ``` 上述代码中,`ModelName`是你定义的Active Record类名,`findOne()`方法用于查找单条记录,`find()`方法用于查找多条记录,`where()`方法用于设置查询条件,`all()`方法返回所有查询结果,`new`关键字用于创建新的Active Record实例,`save()`方法用于保存数据到数据库中,`delete()`方法用于删除数据,`select()`方法用于指定查询的列,`asArray()`方法用于返回查询结果数组而不是Active Record对象。 需要注意的是,Active Record还提供了一些高级的操作,如关联查询、批量操作、查询缓存等。详细的信息可以参考Yii2官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PHP开光程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值