Blame view

app-wx/modules/user/controllers/DefaultController.php 2.6 KB
62d73041   xu   app-wx
1
2
<?php

ab931159   xu   app-wx(v0.1.0 bui...
3
namespace app\wx\modules\user\controllers;
62d73041   xu   app-wx
4

3a892ee0   xu   app-wx(v0.1.0 bui...
5

62d73041   xu   app-wx
6
use Yii;
3a892ee0   xu   app-wx(v0.1.0 bui...
7
use common\helpers\ImageManager;
62d73041   xu   app-wx
8
9
10
11
12
13
14
15
16
17
18
19
20
use stdClass;

/**
 * 控制器
 */
class DefaultController extends BaseController
{

    /**
     * 首页
     */
    public function actionIndex()
    {
3a892ee0   xu   app-wx(v0.1.0 bui...
21
22
        $params = ['isGuest' => Yii::$app->getUser()->isGuest];
        return $this->render('index', $params);
62d73041   xu   app-wx
23
    }
cf6f0119   xu   app-wx(v0.1.0 bui...
24
25
26
27
28
29
30
31
32
33
34
35

    /**
     * 上传文件
     * @return string
     */
    public function actionUploadFile()
    {
        $e = new stdClass();
        $e->success = false;
        $e->message = 'ok';
        $userId = 0;

1de3211f   xu   app-wx(v0.1.0 bui...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        if (isset($_POST["uploadType"]) && 'base64' == $_POST["uploadType"]) {
            $e = $this->uploadFileStr();
        } else {
            $e = $this->uploadFile();
        }
        return $this->renderJson($e);
    }

    private function uploadFile()
    {
        $e = new stdClass();
        $e->success = false;
        $e->message = 'ok';

cf6f0119   xu   app-wx(v0.1.0 bui...
50
51
        if (empty($_FILES["file"])) {
            $e->message = '文件为空';
1de3211f   xu   app-wx(v0.1.0 bui...
52
            return $e;
cf6f0119   xu   app-wx(v0.1.0 bui...
53
        }
1de3211f   xu   app-wx(v0.1.0 bui...
54

cf6f0119   xu   app-wx(v0.1.0 bui...
55
56
        if (empty($_FILES["file"]['tmp_name'])) {
            $e->message = '文件为空';
1de3211f   xu   app-wx(v0.1.0 bui...
57
            return $e;
cf6f0119   xu   app-wx(v0.1.0 bui...
58
59
60
61
62
        }
        $type = $_FILES["file"]["type"];
        $typeArr = explode('/', $type);
        if ('image' !== $typeArr[0]) {
            $e->message = '只能上传 png, jpg 等文件';
1de3211f   xu   app-wx(v0.1.0 bui...
63
            return $e;
cf6f0119   xu   app-wx(v0.1.0 bui...
64
65
        }

3a892ee0   xu   app-wx(v0.1.0 bui...
66
67
68
69
        $uploadInfo = ImageManager::uploadImg($_FILES["file"]);
        $filename = $uploadInfo[0];
        $tmpUrl = $this->site->base_url.'/'.$uploadInfo[2];
        $tmpMinFile = $this->site->base_url.'/'.$uploadInfo[3];
cf6f0119   xu   app-wx(v0.1.0 bui...
70
71
72
73
74
75
76

        $e->success = true;
        $e->tmpFile = $filename;
        $e->tmpMinUrl = $tmpMinFile;
        $e->tmpUrl = $tmpUrl;
        $e->message = 'ok';

1de3211f   xu   app-wx(v0.1.0 bui...
77
        return $e;
cf6f0119   xu   app-wx(v0.1.0 bui...
78
79
    }

1de3211f   xu   app-wx(v0.1.0 bui...
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
    private function uploadFileStr()
    {
        $e = new stdClass();
        $e->success = false;
        $e->message = 'fail';
        $e->tmpFile = '';
        $e->tmpMinUrl = '';
        $e->tmpUrl = '';
        $imgData = $this->request->post("file");
        $imgData = str_replace('data:image/jpeg;base64,', '', $imgData);
        $imageStr = base64_decode($imgData);
        if ($imageStr) {
            $uploadInfo = ImageManager::saveBase64ImgFileToLocal($imageStr);
            $filename = $uploadInfo[0];
            $tmpUrl = $this->site->base_url.'/'.$uploadInfo[2];
            $tmpMinFile = $this->site->base_url.'/'.$uploadInfo[3];

            $e->success = true;
            $e->tmpFile = $filename;
            $e->tmpMinUrl = $tmpMinFile;
            $e->tmpUrl = $tmpUrl;
            $e->message = 'ok';
        }

        return $e;
    }
62d73041   xu   app-wx
106
}