封装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();
    }
}

清晰明了。

posted @ 2019-04-25 11:22  TBHacker  阅读(279)  评论(0编辑  收藏  举报