DefaultController.php 6.87 KB
<?php

namespace app\wx\modules\check\controllers;

use Yii;
use common\helpers\ImageManager;
use common\helpers\WxHelper;
use domain\smart\ScanRecords;
use domain\smart\SellerInputRecordRepository;
use stdClass;

/**
 * 校验-控制器
 */
class DefaultController extends BaseController
{
    /**
     * 校验首页
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * 查询是否激活 -(对应首页扫码验证接口)
     * @return string
     */
    public function actionCheckActive()
    {
        $e = new stdClass();
        $e->success = 2;
        $e->scan_count = 0;
        $e->message = "";
        $uuid = Yii::$app->request->post("uuid");
        if (empty($uuid)) {
            $e->message = '缺少必要参数!';
            return $this->renderJson($e);
        }
        // @todo 校验UUID是否有效 2 无效  0 有效未激活  1 有效已激活
        $hasRecord = SellerInputRecordRepository::findOne(["uuid" => $uuid]);
        if (empty($hasRecord)) {
            $e->success = 0;
            $e->message = '暂无激活!';
            return $this->renderJson($e);
        } else {
            // 记录扫码次数
            $scan_count = ScanRecords::updateCount($uuid);
            if ($scan_count) {
                $e->scan_count = (int) $scan_count;
            }
            $e->success = 1;
            $e->message = '已激活';
        }
        return $this->renderJson($e);

    }

    /**
     * 上传照片检查商品二维码真伪 -(对应提交图片校验submit-controller.js中的提交接口)
     * @return string
     */
    public function actionCheckActived()
    {
        $e = new stdClass();
        $e->success = false;
        $e->scan_count = 0;
        $e->message = "";
        $uploadFile = Yii::$app->request->post("upload_file");
        if (empty($uploadFile)) {
            $e->message = '缺少必要参数!';
            return $this->renderJson($e);
        }
        // 获取图片路径
        //$uploadFile = ImageManager::getUrl($uploadFile);

        // @todo 校验UUID是否有效 2 无效  0 有效未激活  1 有效已激活
        $hasRecord = true;
        if (!$hasRecord) {
            $e->success = false;
            $e->message = '暂无激活!';
            return $this->renderJson($e);
        } else {
            $e->success = true;
            $e->message = '已激活';
        }
        return $this->renderJson($e);

    }

    /**
     * 上传微信JSAPI提交的校验图片 -(对应submit-controller.js页面图片上传接口)
     * @return string
     */
    public function actionUpdateServiceid()
    {
        $e = new stdClass();
        $e->success = false;
        $e->img_path = "";
        $e->show_path = "";
        $e->message = '';
        $wechat = WxHelper::getWxPHPSDK();
        $uploadFile = $this->request->post('service_id');
        $uuid = $this->request->post('uuid');
        if (empty($uploadFile)) {
            $e->message = '请上传商品图片或视频!';
            return $this->renderJson($e);
        }
        if (empty($uuid)) {
            $e->message = '缺少必要参数!';
            return $this->renderJson($e);
        }
        $filename = $this->saveWxImgToRemoveServer($wechat, $uploadFile, $uuid);
        if (!$filename) {
            $e->message = '保存失败!';
            return $this->renderJson($e);
        }
        $e->show_path = ImageManager::getUrl($filename, ImageManager::$STYLE_180);
        $e->img_path = $filename;
        $e->success = true;
        $e->message = '上传成功';
        return $this->renderJson($e);
    }

    /**
     * 保存微信图片到远程服务器
     * @param $wechat 微信基础类
     * @param string $tr 微信公众号的 media_id
     * @return bool|string  返回保存到数据库里面的文件名称是的带URL的,同时会把图片传到阿里云的图片服务器上面
     * $type 这个是类型,根据图片是头像还是身份证来存放,$stat_path 必须根据$type 而改变
     */
    private function saveWxImgToRemoveServer($wechat, $tr = '', $uuid)
    {
        if (empty($tr)) {
            return false;
        }

        $qrcodeArr = $wechat->getMedia($tr, 1);
        if (empty($qrcodeArr['body'])) {
            return false;
        }
        $filename_root = $_SERVER['DOCUMENT_ROOT'];
        $path = $filename_root . '/tmp';

        if (!is_dir($path)) {
            @mkdir($path, 0777);
        }
        $dir = $path;
        $tt = time();
        $filename = 'smart_'.$tt.md5($tr).'.jpg';
        $saveFilePath = $dir.'/'.$filename;

        $local_file = fopen($saveFilePath, 'w');
        if (false !== $local_file) {
            if (false !== fwrite($local_file, $qrcodeArr['body'])) {
                fclose($local_file);
            }
        }
        $user_id = $userId = $this->getUserId();
        $stat_path = ImageManager::getSmartCheckImgPath($user_id, $uuid);
        ImageManager::add($saveFilePath, $stat_path);
        // 删除服务器文件
        @unlink($saveFilePath);

        return $stat_path;
    }

    /**
     * 获取消息 - (success-controller.js 中校验用到,info-controller.js 查询用到)
     * @return string
     */
    public function actionGetInfo()
    {
        $e = new stdClass();
        $e->success = false;
        $e->data = "";
        $e->message = '';
        $numberCode = $this->request->post('number_code');
        $uuid = $this->request->post('uuid');
        if (empty($numberCode) || empty($uuid)) {
            $e->message = '缺少必要参数!';
            return $this->renderJson($e);
        }
        $hasRecord = SellerInputRecordRepository::findOne(["uuid" => $uuid]);
        if (empty($hasRecord)) {
            $e->message = '该二维码暂未激活!';
            return $this->renderJson($e);
        }
        if ($hasRecord->extraction_code != $numberCode) {
            $e->message = '提取码错误!';
            return $this->renderJson($e);
        }
        $imageData = [];
        if ($hasRecord->product_image_path) {
            $imageData = json_decode($hasRecord->product_image_path, true);
            foreach ($imageData as $key => $imageItem) {
                if (isset($imageItem["path"]) && !empty($imageItem["path"])) {
                    $imageData[$key]["path"] = ImageManager::getUrl($imageItem["path"]);
                }
            }
        }
        $hasRecord->product_image_path = $imageData;
        $hasRecord->product_vedio_path = (!empty($hasRecord->product_vedio_path)) ? ImageManager::getUrl($hasRecord->product_vedio_path) : "";
        $dataReturn = $hasRecord->toArray();
        $dataReturn["first_product_image_path"] = !empty($imageData) && isset($imageData[0]["path"]) ? $imageData[0]["path"] : "";
        $e->success = true;
        $e->data = $dataReturn;
        $e->message = '获取成功!';
        return $this->renderJson($e);
    }
}