initTempFilePaths(); parent::init(); $wxArray = $this->getWxArray(); //重置应用的名称 Yii::$app->name = $wxArray['name']; /** * 微信服务号配置 */ $this->wx = new stdClass(); $this->site = new stdClass(); $this->formatWx($wxArray); $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) { /** * 工程师微信服务号信息 */ $this->wx->name = $wxArray['name'];//名称 $this->wx->intro = $wxArray['intro'];//简介 $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 $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; $this->site->is_login = !$appUser->isGuest; $this->site->title = $wxArray['name']; $search = array("\t", "\n", "\r"); $this->site->desc = str_replace($search, '
', $wxArray['intro']); $this->site->icon = $wxArray['logo_path']; // 携带当前用户的邀请码参数,实现推荐用户功能 $sn = ''; $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'; $this->site->signature = 'signature'; $this->site->servicePhone = Yii::$app->params['SERVICE_PHONE']; } /** * @return yii\web\Response */ protected function handleMallAccessCtrl() { $appUser = Yii::$app->getUser(); if (YII_ENV_DEV) { $baseURL = Yii::$app->request->getHostInfo() . Yii::$app->request->baseUrl; } else { $baseURL = Yii::$app->request->getHostInfo(); } $path = $this->request->get('j_path'); $model= $this->request->get('j_model'); $paramOne= $this->request->get('pOne'); if (!empty($path) && !empty($model)) { $locationUrl = Yii::$app->params["frontEndBaseUrl"].$model."#".$path; if (!empty($paramOne)) { $locationUrl .= "/".urldecode($paramOne); } header("Location: $locationUrl");exit; } if(YII_ENV_DEV && $appUser->isGuest){ //header("Location: " .$baseURL . "/user#login" ); //exit; } //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 { // return false; // } return true; } /** * 初始化web目录文件夹, 完成自动创建 */ private function initTempFilePaths() { // 临时文件 $webRoot = Yii::getAlias('@webroot'); $tmpFilePath = $webRoot . '/tmp'; if (!is_dir($tmpFilePath)) { mkdir($tmpFilePath, 0777, true); } } }