Tools.php 4.1 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms;
use EasyWeChat\Factory;

/**
 * 工具接口
 */
class Tools extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * OCR识别
     * @ApiTitle (OCR识别)
     * @ApiParams   (name="image", type="string", description="行驶证图片")
     */
    public function image_ocr()
    {
        $car_image = $this->request->param('image');
//	    file_put_contents( 'shibie2.txt', print_r( $car_image, true ), FILE_APPEND );

//	    $car_image =$this->compressedImage($car_image,'');

	    if (empty($car_image)) {
            $this->error('请选择要识别的行驶证');
        }
        $token = getBuAccessToken();
        $url   = 'https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license?access_token=' . $token;
	    //$img       = file_get_contents($car_image);
	    //$img       = base64_encode($img);
        $bodys = array(
            'url'              => $car_image,
            'detect_direction' => 'true',
        );
        $res   = request_post($url, $bodys);

	    if (array_key_exists('error_msg', json_decode($res, true))) {
            $this->error('识别失败,请重新选择您的行驶证');
        }
        $this->success('ok', json_decode($res, true));
    }




    /**
     * 发送验证码
     * @ApiTitle (发送验证码)
     * @ApiParams   (name="mobile", type="string", description="电话号码")
     */
    public function send_sms()
    {
        $mobile = $this->request->param('mobile');
        if (!preg_match("/^1\d{10}$/", $mobile)) {
            $this->error('请输入正确的11位手机号');
        }
        $last = Sms::get($mobile);
//        if ($last && time() - $last['createtime'] < 60) {
//            $this->error(__('发送频繁'));
//        }
//        $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
//        if ($ipSendTotal >= 5) {
//            $this->error(__('发送频繁'));
//        }
        $result = Sms::send($mobile);
        if ($result) {
            $this->success('发送成功');
        }
        $this->error('发送失败');
    }

    /**
     * @ApiWeigh    (93)
     * @ApiTitle    (公众号-jssdk)
     * @ApiMethod   (POST)
     * @ApiParams   (name=uri, type=string, required=false, description="当前url")
     * @ApiReturn   ({
    "code": 1,
    "msg": "sdk",
    "time": "1644628208",
    "data": {
    "debug": false, //是否开启调试
    "beta": false,
    "jsApiList": [ //jsApiList
    "checkJsApi",
    "updateAppMessageShareData",
    "updateTimelineShareData",
    "onMenuShareTimeline",
    "onMenuShareAppMessage",
    ],
    "appId": "", //appId
    "nonceStr": "XClQcJvAGn", //随机字符串
    "timestamp": 1644628208, //时间戳
    "url": "", //当前url
    "signature": "" //签名
    }
    })
     */
    public function jssdk()
    {
        $params = $this->request->post();
        $apis   = [
            'checkJsApi',
            'updateAppMessageShareData', // 自定义“分享给朋友”及“分享到QQ”按钮的分享内容
            'updateTimelineShareData', // 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
            'onMenuShareTimeline', // 获取“分享到朋友圈”按钮点击状态及自定义分享内容接口(即将废弃)
            'onMenuShareAppMessage', // 获取“分享给朋友”按钮点击状态及自定义分享内容接口(即将废弃)
        ];
        $uri    = urldecode($params['uri']);

        $config = get_addon_config('wechat');
        $app    = Factory::officialAccount($config);
        $res    = $app->jssdk->setUrl($uri)->buildConfig($apis, $debug = false, $beta = false, $json = false);
        $this->success('sdk', $res);
    }

}