审查视图

addons/shopro/controller/Index.php 12.5 KB
何书鹏 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
<?php

namespace addons\shopro\controller;

use addons\shopro\model\Config;
use think\Db;
use think\Config as FaConfig;
use fast\Random;

class Index extends Base
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index()
    {

    }

    //初始化商城数据
    public function init()
    {
        $params = $this->request->get();
        $platform = $this->request->header('platform');

        $config = new \addons\shopro\model\Config;

        $platformConfig = json_decode($config->where(['name' => $platform])->value('value'), true);
        $paymentConfig = $config->where('group', 'payment')->select();
        $payment = [];
        foreach ($paymentConfig as $key => $config) {
            $val = json_decode($config->value, true);
            if ($val && in_array($platform, $val['platform'])) {
                $payment[] = $config->name;
            }
        }
        $data['payment'] = $payment;        // 平台支持的支付方式

        $shopro = json_decode(Config::where(['name' => 'shopro'])->value('value'), true);
        $data['info'] = $shopro;
        $data['info']['logo'] = cdnurl($data['info']['logo'], true);
        $autologin = false;
        if (isset($platformConfig['auto_login']) && $platformConfig['auto_login'] == 1) {
            $autologin = true;
        }
        $data['wechat'] = [
            'appid' => isset($platformConfig['app_id']) ? $platformConfig['app_id'] : '',
            'autologin' => $autologin,
        ];
        $share = json_decode(Config::where(['name' => 'share'])->value('value'), true);

        $data['share'] = [
            'title' => $share['title'],
            'image' => isset($share['image']) ? cdnurl($share['image'], true) : '',
            'goods_poster_bg' => isset($share['goods_poster_bg']) ? cdnurl($share['goods_poster_bg'], true) : '',
            'user_poster_bg' => isset($share['user_poster_bg']) ? cdnurl($share['user_poster_bg'], true) : '',
            'groupon_poster_bg' => isset($share['groupon_poster_bg']) ? cdnurl($share['groupon_poster_bg'], true) : '',
        ];
        $data['addons'] = array_keys(get_addon_list());
        $this->success('初始化数据', $data);

    }

    //商城模板数据
    public function template()
    {
        $get = $this->request->get();
        $platform = $this->request->header('platform');
        if (isset($get['shop_id'])) {
            $template = \addons\shopro\model\Decorate::getCurrentPlatformDecorate('preview', $get['shop_id']);
        } else {
            $template = \addons\shopro\model\Decorate::getCurrentPlatformDecorate($platform);
        }
        $this->success('模板数据', $template);
    }

    //自定义页面
    public function custom()
    {
        $get = $this->request->get();
        $decorate = \addons\shopro\model\Decorate::get($get['id']);
        if (!$decorate) {
            $this->error('未找到自定义页面');
        }
        $decorate->template = \addons\shopro\model\Decorate::getCustomDecorate($get['id']);
        $this->success('自定义模板数据', $decorate);
    }

    //富文本详情
    public function richtext()
    {
        $id = $this->request->get('id');
        $data = \addons\shopro\model\Richtext::get(['id' => $id]);
        $this->success($data->title, $data);
    }

    //同步前端所有页面链接
    public function asyncLink()
    {
        $post = $this->request->post();
        $newLink = $post['data'];
        $existLink = Db::name('shopro_link')->select();
        $newLinkPath = array_column($newLink, 'path');
        $existLinkPath = array_flip(array_column($existLink, 'path'));
        $insertData = [];
        $count = 1;
        foreach ($newLinkPath as $key => $item) {
            if (!isset($existLinkPath[$item]) && isset($newLink[$key]['meta']['async']) && $newLink[$key]['meta']['async']) {
                $insertData[] = [
                    'name' => isset($newLink[$key]['meta']['title']) ? $newLink[$key]['meta']['title'] : '新链接' . $count,
                    'path' => $item,
                    'group' => isset($newLink[$key]['meta']['group']) ? $newLink[$key]['meta']['group'] : '其它',
                    'createtime' => time(),
                    'updatetime' => time()
                ];
                $count++;
            }
        }
        if ($insertData !== []) {
            Db::name('shopro_link')->insertAll($insertData);
        }
    }

    //前端获取轮播图平均色
    //1.0.7以后此接口作废
    public function asyncBannerBgColor()
    {
        $params = $this->request->post();
        $platform = $this->request->header('platform');
        $data['template'] = \addons\shopro\model\Decorate::asyncBannerBgColor($platform, $params);
    }

    //店铺装修 截图商城首页
    public function asyncDecorateScreenShot()
    {
        $params = $this->request->post();
        \addons\shopro\model\Decorate::asyncDecorateScreenShot($params);
    }

    /**
     * 上传文件
     * @ApiMethod (POST)
     * @param File $file 文件流
     */
    public function upload()
    {
        $file = $this->request->file('file');
        if (empty($file)) {
            $this->error(__('No file upload or server upload limit exceeded'));
        }

        //判断是否已经存在附件
        $sha1 = $file->hash();

        $upload = FaConfig::get('upload');

        preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
        $type = strtolower($matches[2]);
        $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
        $size = (int)$upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
        $fileInfo = $file->getInfo();
        $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
        $suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';

        $mimetypeArr = explode(',', strtolower($upload['mimetype']));
        $typeArr = explode('/', $fileInfo['type']);

        //禁止上传PHP和HTML文件
        if (in_array($fileInfo['type'], ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm'])) {
            $this->error(__('Uploaded file format is limited'));
        }
        //验证文件后缀
        if ($upload['mimetype'] !== '*' &&
            (
                !in_array($suffix, $mimetypeArr)
                || (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
            )
        ) {
            $this->error(__('Uploaded file format is limited'));
        }
        //验证是否为图片文件
        $imagewidth = $imageheight = 0;
        if (in_array($fileInfo['type'], ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) {
            $imgInfo = getimagesize($fileInfo['tmp_name']);
            if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) {
                $this->error(__('Uploaded file is not a valid image'));
            }
            $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
            $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
        }
        $replaceArr = [
            '{year}' => date("Y"),
            '{mon}' => date("m"),
            '{day}' => date("d"),
            '{hour}' => date("H"),
            '{min}' => date("i"),
            '{sec}' => date("s"),
            '{random}' => Random::alnum(16),
            '{random32}' => Random::alnum(32),
            '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],
            '{suffix}' => $suffix,
            '{.suffix}' => $suffix ? '.' . $suffix : '',
            '{filemd5}' => md5_file($fileInfo['tmp_name']),
        ];
        $savekey = $upload['savekey'];
        $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);

        $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
        $fileName = substr($savekey, strripos($savekey, '/') + 1);
        //
        $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
        if ($splInfo) {
            $params = array(
                'admin_id' => 0,
                'user_id' => (int)$this->auth->id,
                'filesize' => $fileInfo['size'],
                'imagewidth' => $imagewidth,
                'imageheight' => $imageheight,
                'imagetype' => $suffix,
                'imageframes' => 0,
                'mimetype' => $fileInfo['type'],
                'url' => $uploadDir . $splInfo->getSaveName(),
                'uploadtime' => time(),
                'storage' => 'local',
                'sha1' => $sha1,
            );
            $attachment = new \app\common\model\Attachment;
            $attachment->data(array_filter($params));
            $attachment->save();
            \think\Hook::listen("upload_after", $attachment);
            $this->success(__('Upload successful'), [
                'url' => $uploadDir . $splInfo->getSaveName(),
                'full_url' => request()->domain() . $uploadDir . $splInfo->getSaveName()
            ]);
        } else {
            // 上传失败获取错误信息
            $this->error($file->getError());
        }
    }



    /**
     * 上传 base64 图片
     * @ApiMethod (POST)
     * @param base64 内容
     */
    public function uploadBase64()
    {
        $content = $this->request->post('content', '');
        
        if (empty($content) || !preg_match('/^(data:\s*image\/(\w+);base64,)/', $content, $matches)) {
            $this->error(__('No file upload or server upload limit exceeded'));
        }
        
        $upload = FaConfig::get('upload');

        $suffix = $matches[2]; //图片后缀

        $mimetypeArr = explode(',', strtolower($upload['mimetype']));

        // 判断是否允许的后缀
        if ($upload['mimetype'] !== '*' && !in_array($suffix, $mimetypeArr)) {
            $this->error(__('Uploaded file format is limited'));
        }

        $replaceArr = [
            '{year}' => date("Y"),
            '{mon}' => date("m"),
            '{day}' => date("d"),
            '{hour}' => date("H"),
            '{min}' => date("i"),
            '{sec}' => date("s"),
            '{random}' => Random::alnum(16),
            '{random32}' => Random::alnum(32),
            '{filename}' => "店铺装修截图",
            '{suffix}' => $suffix,
            '{.suffix}' => $suffix ? '.' . $suffix : '',
            '{filemd5}' => md5($content),
        ];
        $savekey = $upload['savekey'];
        $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);

        $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
        $fileName = substr($savekey, strripos($savekey, '/') + 1);
        $fullDir = ROOT_PATH . '/public' . $uploadDir;

        $uploadPath = $uploadDir . $fileName;
        $fullPath = $fullDir . $fileName;

        if (!is_dir($fullDir)) {
            mkdir($fullDir, 0777, true);
        }

        $imageContent = base64_decode(str_replace($matches[1], '', $content));
        if (file_put_contents($fullPath, $imageContent)) {
            $imgInfo = getimagesize($fullPath);

            $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : 0;
            $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : 0;

            $params = array(
                'admin_id' => 0,
                'user_id' => (int) $this->auth->id,
                'filesize' => strlen($content),
                'imagewidth' => $imagewidth,
                'imageheight' => $imageheight,
                'imagetype' => $suffix,
                'imageframes' => 0,
                'mimetype' => isset($imgInfo['mime']) ? $imgInfo['mime'] : 'image/' . $suffix,
                'url' => $uploadPath,
                'uploadtime' => time(),
                'storage' => 'local',
                'sha1' => hash_file('sha1', $fullPath),
            );
            $attachment = new \app\common\model\Attachment;
            $attachment->data(array_filter($params));
            $attachment->save();

            \think\Hook::listen("upload_after", $attachment);
            $this->success(__('Upload successful'), [
                'url' => $uploadPath,
                'full_url' => request()->domain() . $uploadPath
            ]);
        } else {
            $this->error("文件上传失败");
        }
    }

    public function debugLog()
    {
        $params = $this->request->post();
        \think\Log::write($params, 'notice');
    }

}