• 封装json输出


    /**
     * 输出json
     * @param $msg
     * @param int $errno
     */
    protected function printOutError($msg,$errno = 10099) {
        $this->json->setErr($errno, $msg);
        $this->json->Send();
    }
    
    /**
     * 输出json
     * @param array $out_data
     * @param string $msg
     */
    protected function printOutSuccess($out_data = [],$msg='操作成功') {
        $this->json->setErr(0, $msg);
        if ($out_data) {
            $this->json->setAttr('data', $out_data);
        }
        $this->json->Send();
    }
    

    具体使用

    /**
     * 删除订单,已完成的可以删除
     */
    public function delOrder() {
        if (!$uid = $_POST['uid']) {
            $this->printOutError('缺少用户id',10001);
        }
    
        if (!$order_id = $_POST['order_id']) {
            $this->printOutError('缺少订单id',10001);
        }
    
        $order = M('order');
        $order_info = $order->where(['id'=>$order_id,'is_user_del'=>0,'status'=>['gt',0]])->find();
        if (!$order_info) {
            $this->printOutError('订单不存在,或已删除/取消',10002);
        }
    
        if ((int)$order_info['status'] !== 4) {
            $this->printOutError('订单状态不可删除',10003);
        }
    
        $edit_data = [
            'is_user_del'=> 1
        ];
        $edit_flag = $order->where(['id'=>$order_id])->save($edit_data);
        if (!$edit_flag) {
            $this->printOutError('操作失败,请重试');
        } else {
            $this->printOutSuccess();
        }
    }
    

    清晰明了。

  • 相关阅读:
    数据库相关
    linux相关
    类相关
    异常处理
    一些类的概念
    安装插件
    接口开发
    redis相关
    大数据-概览
    大数据-浅谈OLTP与OLAP
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10767273.html
Copyright © 2020-2023  润新知