public function actionIndex() { $province = "北京市"; $city = "北京市"; $district = "海淀区"; $province = $this->removeLastCharacter($province); $city = $this->removeLastCharacter($city); $district = $this->removeLastCharacter($district); $smartWeatherLocationObj = new SmartWeatherLocation(); $locationJson = $smartWeatherLocationObj->getAreaIdFromAreaName($province, $city, $district); if ($locationJson == null) { } else { $locationId = $locationJson['area_id']; $smartWeatherObj = new SmartWeather(); $weatherDataJson = $smartWeatherObj->getWeatherData($locationId, SmartWeather::TYPE_FORECAST_V); $suggestionDataJson = $smartWeatherObj->getWeatherData($locationId, SmartWeather::TYPE_INDEX_V); echo "<pre>"; var_dump($weatherDataJson); var_dump($suggestionDataJson); echo "<pre>"; $threeDayWeatherArray = $smartWeatherObj->parseForecastData($weatherDataJson); $todaySuggestionArray = $smartWeatherObj->parseIndexData($suggestionDataJson); $replyStr = ""; $todayWeather = $threeDayWeatherArray[0]; $todayWeather['eveningTemperature']; $replyStr .= "白天: " . $todayWeather['morningWeather'] . ", 温度: " . $todayWeather['morningTemperature'] . ", 晚上: " . $todayWeather['eveningWeather'] . ", 温度: " . $todayWeather['eveningTemperature'] . "\r\n"; foreach ($todaySuggestionArray as $oneSuggestion) { $tempStr = $oneSuggestion["type"] . ": " . $oneSuggestion["level"] . " | " . $oneSuggestion["description"] . "\r\n"; $replyStr .= $tempStr; } $replyStr .= "小天祝您身体健康,生活快乐!"; echo $replyStr; } }
public function actionIndex() { $request = \Yii::$app->request; if ($request->isPost) { $postStr = file_get_contents("php://input"); $postArray = (array) simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); \Yii::info($postStr); } $wechat = $this->wechatObj; $type = $wechat->getRev()->getRevType(); $wechat->getRevGeo(); $wechat->checkAuth(); //todo: 根据消息的type以及内容去数据库里面找,然后回复相应的内容。 switch ($type) { case Wechat::MSGTYPE_TEXT: $this->wechatObj->text("hello, I'm wechat")->reply(); exit; break; case Wechat::MSGTYPE_LOCATION: $x = $postArray['Location_X']; $y = $postArray['Location_Y']; $label = $postArray['Label']; // $this->wechatObj->text('hello')->reply(); $amapObj = new AmapMap(); $responseJson = $amapObj->reGeo($x, $y); Yii::info($responseJson); if ($responseJson['status'] == "0") { $this->wechatObj->text("对不起主人,小天实在不能理解您的地理位置")->reply(); } else { $amapObj->initLocationData($responseJson); $smartWeatherLocationObj = new SmartWeatherLocation(); $locationJson = $smartWeatherLocationObj->getAreaIdFromAreaName($amapObj->province, $amapObj->city, $amapObj->district); Yii::info($locationJson, "debug_ang"); if ($locationJson == null) { $this->wechatObj->text("help info")->reply(); exit; } else { $locationId = $locationJson['area_id']; $smartWeatherObj = new SmartWeather(); $weatherDataJson = $smartWeatherObj->getWeatherData($locationId, SmartWeather::TYPE_FORECAST_V); $suggestionDataJson = $smartWeatherObj->getWeatherData($locationId, SmartWeather::TYPE_INDEX_V); $threeDayWeatherArray = $smartWeatherObj->parseForecastData($weatherDataJson); $todaySuggestionArray = $smartWeatherObj->parseIndexData($suggestionDataJson); Yii::info($weatherDataJson); Yii::info($suggestionDataJson); $replyStr = ""; $locationStr = $amapObj->province . '|' . $amapObj->city . '|' . $amapObj->district . "\r\n"; $replyStr .= $locationStr; $todayWeather = $threeDayWeatherArray[0]; $todayWeather['eveningTemperature']; $replyStr .= "白天: " . $todayWeather['morningWeather'] . ", 温度: " . $todayWeather['morningTemperature'] . ", 晚上: " . $todayWeather['eveningWeather'] . ", 温度: " . $todayWeather['eveningTemperature'] . "\r\n"; foreach ($todaySuggestionArray as $oneSuggestion) { $tempStr = $oneSuggestion["type"] . ": " . $oneSuggestion["level"] . " | " . $oneSuggestion["description"] . "\r\n"; $replyStr .= $tempStr; } $replyStr .= "小天祝您身体健康,生活快乐!"; $this->wechatObj->text($replyStr)->reply(); } } exit; break; case Wechat::MSGTYPE_EVENT: $wechat->getRevGeo(); break; case Wechat::MSGTYPE_IMAGE: break; default: $this->wechatObj->text("help info")->reply(); } }
/** * @param $province String 对应数据表的province_cn * @param $city String 对应数据表的district_cn * @param $district String 对应数据表的name_cn * @return array|null|\yii\db\ActiveRecord */ public function getAreaIdFromAreaName($province, $city, $district) { $result = SmartWeatherLocation::find()->where(["province_cn" => $province, "district_cn" => $city, "name_cn" => $district])->asArray()->one(); return $result; }