Blame view

app-wx/controllers/BaseController.php 6.85 KB
62d73041   xu   app-wx
1
2
3
4
5
<?php

namespace app\wx\controllers;

use Yii;
62d73041   xu   app-wx
6
7
8
9
10
11
12
13
14
use yii\helpers\Url;
use yii\web\BadRequestHttpException;
use yii\db\Query;
use app\wx\models\Engineer;
use common\helpers\WxHelper;
use common\helpers\ImageManager;
use common\exts\wechat\Log as WxLog;
use common\helpers\Log as AppLog;
use common\helpers\PerformanceUtils;
62d73041   xu   app-wx
15
16
use common\models\SysSetting;
use domain\engineer\EngineerRole;
62d73041   xu   app-wx
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
use domain\engineer\EngineerStatus;
use domain\engineer\EngineerSkillTagsRepository;
use stdClass;
use function header;
use function in_array;
use function md5;
use function str_replace;
use function urlencode;


/**
 * 只有通过 微信 OAuth 验证后才能访问整个App,
 * 这是所有应用控制器的基类
 * Class BaseController
 * @package app\wx\controllers
 * @author lee.li <34923862@qq.com>
 * @date 2015/07/01
 */
class BaseController extends AppController
{
    /**
     * CSR验证,关闭后可以在不提交CSR验证码的情况下通过POST方式提交数据
     * @var bool
     */
    public $enableCsrfValidation = false;
    /** @var **/
    protected $wx;
    /** @var **/
    protected $site;

    /**
     * 初始化应用控制器,确保 mall id 的存在,有效启动程序,否则抛出异常。
     * @throws yii\web\BadRequestHttpException
     * @throws yii\web\BadRequestHttpException
     */
    public function init()
    {

        $this->initTempFilePaths();

        parent::init();

        $wxArray = $this->getWxArray();

        //重置应用的名称
        Yii::$app->name = $wxArray['name'];

        /**
         * 微信服务号配置
be5105bd   xu   app-wx(v0.1.0 bui...
66
         */
f3ed8f51   xu   app-wx(v0.1.0 bui...
67
        $this->wx = new stdClass();
62d73041   xu   app-wx
68
        $this->site = new stdClass();
f3ed8f51   xu   app-wx(v0.1.0 bui...
69
70

        $this->formatWx($wxArray);
62d73041   xu   app-wx
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
        $this->formatSite($wxArray);

        $view = Yii::$app->view;
        $view->params['site']= $this->site;
    }

    /**
     * @return array|bool
     */
    private function getWxArray()
    {
        $wxArray = [
            'name' => '配件维修',
            'intro' => '报修',
            'logo_path' => '',
            'subscribe_title' => '报修',
            'subscribe_desc' => '报修',
            'subscribe_img'  => '',
            'subscribe_url' => '',
            'appid' => 'appid',
            'appsecret' => 'appsecret',
            'token' => 'token'

        ];
        return $wxArray;
    }

    /**
     * @param $wxArray
     */
    private function formatWx($wxArray)
    {
        /**
         * 工程师微信服务号信息
         */
3a892ee0   xu   app-wx(v0.1.0 bui...
106
        $this->wx->name = $wxArray['name'];//名称
62d73041   xu   app-wx
107
        $this->wx->intro = $wxArray['intro'];//简介
3a892ee0   xu   app-wx(v0.1.0 bui...
108
109
110
111
112
        $this->wx->logo_url = $wxArray['logo_path'];//路径
        $this->wx->subscribe_title = $wxArray['subscribe_title']; // 公众号关注图文标题
        $this->wx->subscribe_desc = $wxArray['subscribe_desc']; // 公众号关注图文描述
        $this->wx->subscribe_img = $wxArray['subscribe_img']; // 公众号关注图文消息图片
        $this->wx->subscribe_url = $wxArray['subscribe_url']; // 公众号关注图文url
62d73041   xu   app-wx
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

        $this->wx->appid  = $wxArray['appid'];// APPID
        $this->wx->appsecret = $wxArray['appsecret'];//APPSECRET
        $this->wx->token = $wxArray['token'];//微信TOKEN
    }

    /**
     * 获取 ID
     * @return int|string
     */
    public function getUserId()
    {
        if (isset(Yii::$app->user)) {
            return Yii::$app->user->id;
        } else {
            return 0;
        }
    }

    /**
     * @param $wxArray
     */
    private function formatSite($wxArray)
    {
        /**
         * 站点信息
         */
        $appUser = Yii::$app->getUser();
        $url = Yii::$app->request->getHostInfo() . Yii::$app->request->url;
        $this->site->url = $url;
        if (YII_ENV_DEV) {
            $this->site->base_url = Yii::$app->request->getHostInfo() . Yii::$app->request->baseUrl;
        } else {
            $this->site->base_url = Yii::$app->request->getHostInfo();
        }

        $am = Yii::$app->view->getAssetManager();
        $assets = $am->getBundle('app\wx\assets\AppAsset');

        $this->site->is_android = (int)$this->isAndroid();
        $this->site->assets_url = $assets->baseUrl;
1de3211f   xu   app-wx(v0.1.0 bui...
154
        $this->site->is_login = !$appUser->isGuest;
62d73041   xu   app-wx
155
156
157
158
159
160
161
        $this->site->title = $wxArray['name'];
        $search = array("\t", "\n", "\r");
        $this->site->desc = str_replace($search, '<br/>', $wxArray['intro']);
        $this->site->icon =  $wxArray['logo_path'];

        // 携带当前用户的邀请码参数,实现推荐用户功能
        $sn = '';
62d73041   xu   app-wx
162

3a892ee0   xu   app-wx(v0.1.0 bui...
163
164
165
166
167
168
169
        $baseUrl = $this->site->base_url;
        $this->site->user_bridge_url = $sn ? ($baseUrl . '/wechat/bridging?sn=' . $sn . '&tourl=' . urlencode($this->site->url)) : '';
        $this->site->sn = $sn;

        $this->site->appid = 'appid';
        $this->site->timestamp = time();
        $this->site->noncestr = 'noncestr';
7d60e23d   xu   app-wx(v0.1.0 bui...
170
171
172
173
174
175
176
177
178
179
180
        $this->site->signature = 'signature';
    }

    /**
     * @return yii\web\Response
     */
    protected function handleMallAccessCtrl()
    {
        $appUser = Yii::$app->getUser();

        if (YII_ENV_DEV) {
62d73041   xu   app-wx
181
            $baseURL = Yii::$app->request->getHostInfo() . Yii::$app->request->baseUrl;
3a892ee0   xu   app-wx(v0.1.0 bui...
182
183
        } else {
            $baseURL = Yii::$app->request->getHostInfo();
62d73041   xu   app-wx
184
        }
3a892ee0   xu   app-wx(v0.1.0 bui...
185

62d73041   xu   app-wx
186
        if(YII_ENV_DEV && $appUser->isGuest){
3a892ee0   xu   app-wx(v0.1.0 bui...
187
188
            //header("Location: " .$baseURL . "/user#login" );
            //exit;
62d73041   xu   app-wx
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
        }
        //test和 prod 环境
        if ($appUser->isGuest){
            header("Location: " . $baseURL . "/user#login" );
            exit;
        }

    }

    /**
     * @return yii\web\Response
     */
    private function handleOAuthRedirect()
    {
        $sn = Yii::$app->request->get("sn");

        //Yii::$app->response->redirect($OAuthUrl)->send();
        //Yii::$app->end();
    }


    /**
     * 是否是IOS端
     * 是否通过微信客户端来访问
     * LOG 记录时发现,
     * 在微信公众号里面第一次访问: $_SERVER ['HTTP_USER_AGENT'] 为 Mozilla/4.0
     * 第二次访问: $_SERVER ['HTTP_USER_AGENT'] 为
     * 苹果:
     * Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13E238 MicroMessenger/6.3.16 NetType/WIFI Language/zh_CN
     * 安卓(小米NOTE):
     * Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9b4) Gecko/2008030317 Firefox/3.0b4
     * @return bool
     */
    protected function isAndroid()
    {
//        if(false !== strpos($_SERVER ['HTTP_USER_AGENT'], 'Android') || !isset($_SERVER ['HTTP_USER_AGENT'])){
//            return true;
//        } else {
62d73041   xu   app-wx
227
228
229
230
231
232
233
234
235
236
237
238
239
//            return false;
//        }
        return true;
    }

    /**
     * 初始化web目录文件夹, 完成自动创建
     */
    private function initTempFilePaths()
    {
        // 临时文件
        $webRoot = Yii::getAlias('@webroot');
        $tmpFilePath = $webRoot . '/tmp';