TP5封装一个API接口JSON返回类

本文转自: https://www.learnku.net/blog/articles/7

TP5封装一个API接口JSON返回类

文件名及存放目录

project —> extend —> Gucci —> ServerResponse.php

源码
<?php
/**
 * 统一返回处理类(ajax)允许跨域
 *
 * 使用: **
 *      use Gucci\ServerResponse;
 *      return ServerResponse::createBySuccess("成功");
 *      ServerResponse::createByError("断点失败了");
 */

namespace Gucci;

use think\facade\Config;
use think\exception\HttpResponseException;
use think\Response;

class ServerResponse
{
    public static $successCode = 1;
    public static $errorCode = 0;

    //构造函数
    public function __construct($status,$msg = "",$data = [])
    {

        $result['status'] = $status;
        $msg ? $result['msg'] = $msg : null;
        $data ? $result['data'] = $data : null;

        $type                                   = $this->getResponseType();
        $header['Access-Control-Allow-Origin']  = '*';
        $header['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type';
        $header['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE,OPTIONS';
        $response                               = Response::create($result, $type)->header($header);
        throw  new HttpResponseException($response);
    }

    /**
     * 成功
     * @param $msg
     * @param null $data
     * @return ServerResponse
     */
    public static function createBySuccess($msg, $data = null){
        return new ServerResponse(self::$successCode, $msg,$data);
    }

    /**
     * 失败
     * @param $msg
     * @return ServerResponse
     */
    public static function createByError($msg){
        return new ServerResponse(self::$errorCode, $msg);

    }

    /**
     * 获取当前的response 输出类型
     * @access protected
     * @return string
     */
    private function getResponseType()
    {
        return Config::get('default_ajax_return');
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
, file_header.bfOffBits, SEEK_SET); uint8_t* p = *data + row_size * (*height - 1); for (int i = 0; i < *height; ++i) { fread(p, row_size, 1,在TP5中使用PHP QR Code库生成二维码时,可以通过`base64_encode()`将二维码图片转换 fp); p -= row_size; } fclose(fp); } void sobel_edge_detection(uint8_t* data, int width,为base64编码的字符串,然后通过接口返回给前端。 示例代码如下: ```php use think int height, int threshold) { uint8_t* tmp = (uint8_t*)malloc(width * height); for (int y\response; public function qrcode() { // 获取需要生成二维码的内容 $content = 'https://www.example = 1; y < height - 1; ++y) { for (int x = 1; x < width - .com'; // 设置二维码参数 $size = 10; $margin = 1; $errorCorrectionLevel1; ++x) { int gx = -data[(y - 1) * width * 3 + (x - 1 = 'H'; // 生成二维码图片对象 $qrCode = new QrCode($content); $qr) * 3] + data[(y - 1) * width * 3 + (x + 1) * 3] Code->setSize($size); $qrCode->setMargin($margin); $qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel($ -2 * data[y * width * 3 + (x - 1) * 3] + 2 * data[y *errorCorrectionLevel)); // 将二维码图片转换为二进制字符串 $imageData = $qrCode->write width * 3 + (x + 1) * 3] -data[(y + 1) * width * 3String(); // 将二进制字符串转换为base64编码的字符串 $base64 = base64_encode($image + (x - 1) * 3] + data[(y + 1) * width * 3 + (x + Data); // 返回base64编码的字符串 return Response::create(['qrcode' => $base64], 'json'); 1) * 3]; int gy = -data[(y - 1) * width * 3 + (x - 1} ``` 在上述示例代码中,我们使用PHP QR Code库生成二维码图片对象,并将其转换为) * 3] - 2 * data[(y - 1) * width * 3 + x * 3] -二进制字符串,然后再通过`base64_encode()`函数将其转换为base64编码的字符串。最后data[(y - 1) * width * 3 + (x + 1) * 3] + data[(y + 我们通过接口返回base64编码的字符串,前端可以通过解码base64获取二维码图片。 注意:在1) * width * 3 + (x - 1) * 3] + 2 * data[(y + 1使用该方法返回二维码图片时,前端需要将base64编码的字符串解码为图片,并且在前端) * width * 3 + x * 3] + data[(y + 1) * width * 3 + (x +进行展示时,需要设置图片的MIME型为image/png。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值