Api基类

基类

class BaseController extends Controller{
    public $outData = ['code'=>0,'msg'=>'ok'];
    public $param;
    public $timeNow;
    public function __construct(){
        parent::__construct();
        header('Access-Control-Allow-Origin:*');
        $this->param = I('post.');
        $this->timeNow = date('Y-m-d H:i:s');
    }

    //验证参数
    public function regArguments($needParam,&$arguments){
        foreach($needParam as $key=>$val){
            if(!isset($arguments[$key]) || $arguments[$key]===''){
                $this->outData = ['code'=>1,'msg'=>$val['msg']];
            }else{
                $arguments[$key] = $this->formatArguments($arguments[$key],$val['type']);
            }
        }
        return;
    }
    protected function formatArguments($val,$type){
        switch ($type) {
            case 'int':
                return intval($val);break;
            case 'float':
                return round($val, 2);break;
            case 'tel':
                if (!is_numeric($val) || !preg_match('/^1[\d]{10}$/', $val)) {
                    $this->outData = ['code'=>2,'msg'=>'手机号格式不正确'];
                }
                return $val;break;
            default:
                return trim($val);
        }
    }
    //验证token
    public function regToken($token,$pstr){
        if($token != sha1(md5($pstr.APP_SECRET))){
            $this->outData = ['code'=>1,'msg'=>'非法请求'];
        }
    }

    // 获取token
    protected function getHashToken($pstr) {
        return sha1(md5($pstr.APP_SECRET));
    }

    //输出json结果
    public function printOut($data=[]){
        header('Content-type: application/json');
        $data ? $this->outData = $data : '';
        echo json_encode($this->outData,JSON_UNESCAPED_UNICODE);exit;
    }

    // 赋值并输出
    public function printInAndPrintOut($msg = "ok",$code = "0",$data = [],$ext = []) {
        header('Content-type: application/json');
        $this->outData['code'] = $code;
        $this->outData['msg'] = $msg;
        if ($data) {
            $this->outData['data'] = $data;
        }

        if ($ext && is_array($ext)) {
            foreach ($ext as $k => $v) {
                if ($k != 'msg' && $k != 'code' && $k != 'data') {
                    $this->outData[$k] = $v;
                }
            }
        }

        echo json_encode($this->outData,JSON_UNESCAPED_UNICODE);exit;
    }

    public function notfound(){
        $this->display('empty/index');exit;
    }

}

运用

/**
     * 领取详情
     * @auhtor jim
     * @date   2018/2/28
     */
    public function myGetDepositInfo() {
        /*$this->param = [
            'openId' => 'xxx',
            'hashToken' => 'xxx',
            'id' => 'xxx',
        ];*/
        $needParam = array(
            'openId'=>array('msg'=>'openId参数异常','type'=>'str'),
            'hashToken'=>array('msg'=>'hashToken参数异常','type'=>'str'),
            'id'=>array('msg'=>'id参数异常','type'=>'int'),
        );

        $this->regArguments($needParam,$this->param);
        $this->regToken($this->param['hashToken'],$this->param['openId']);
        if ($this->outData['code']) {
            $this->printOut();
        }

        // 获取用户uid
        $uModel = new UserModel();
        $uid = $uModel->getUidByOpenId($this->param['openId']);

        $udcModel = new UserDepositCashinfoModel();
        $udcInfo = $udcModel->findData(['uid'=>$uid,['id'=>$this->param['id']]]);

        if (!$udcInfo) {
            $this->printInAndPrintOut("领取记录不存在",1);
        }

        // 统计之前领取的笔数
        $beforeBackCount = $udcModel->sumData(['udid'=>$udcInfo['udid'],'addtime'=>['lt',$udcInfo['addtime']]],'back_percent');
        if(!$beforeBackCount) {
            $beforeBackCount = 0;
        }

        $udcInfo['beginCount'] = $beforeBackCount + 1;
        $udcInfo['endCount'] = $beforeBackCount + $udcInfo['back_percent'];

        $this->printInAndPrintOut('ok',0,$udcInfo);
    }
posted @ 2018-03-26 18:59  TBHacker  阅读(218)  评论(0编辑  收藏  举报