/** * 有redis做队列就好了,这样就不用这样处理了 * 木办法,现在的主机配置有点差 */ public function actionRun() { $date_now = date("Y-m-d H:i:s"); $id = $this->getCurId($this->cur_path); $rich_media_list = WxHistory::find()->where(['type' => ["image", "voice", "video"]])->andWhere([">", "id", $id])->orderBy("id asc")->limit(10)->all(); if (!$rich_media_list) { return $this->echoLog("{$date_now} no data"); } foreach ($rich_media_list as $_rich_info) { $thumb_url = ""; $src_url = ""; $type = $_rich_info['type']; $this->setCurId($this->cur_path, $_rich_info['id']); //只处理图片 switch ($type) { case "image": $src_url = $_rich_info["content"]; break; case "voice": case "video": list($src_url, $thumb_url) = $this->getMediaInfo($_rich_info["text"]); break; } if (!$src_url) { continue; } $this->saveRichMedia($type, $src_url, $thumb_url); } }
public static function add($xml, $source = "") { $data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $type = trim($data->MsgType); $from_openid = $data->FromUserName; $to_openid = $data->ToUserName; $date_now = date("Y-m-d H:i:s"); switch ($type) { case "location": $content = trim($data->Label); break; case "voice": $content = trim($data->Recognition); break; case "image": $content = trim($data->PicUrl); break; case "link": $content = trim($data->Title); break; case "shortvideo": $content = trim($data->ThumbMediaId); break; case "event": $content = trim($data->Event); break; default: $content = trim($data->Content); break; } $model_wx_history = new WxHistory(); $model_wx_history->from_openid = $from_openid; $model_wx_history->to_openid = $to_openid; $model_wx_history->type = $type; $model_wx_history->content = $content; $model_wx_history->text = $xml; $model_wx_history->source = $source; $model_wx_history->created_time = $date_now; $model_wx_history->save(0); if (filter_var($content, FILTER_VALIDATE_URL) !== FALSE) { SpiderService::add($content); } if (in_array($type, ["text"]) && substr($content, 0, 1) == "#") { $bind_info = UserOpenidUnionid::findOne(['other_openid' => strval($from_openid)]); if (!$bind_info) { $unique_name = md5($from_openid); $user_info = User::findOne(['unique_name' => $unique_name]); if (!$user_info) { $model_user = new User(); $model_user->nickname = "微信用户" . substr($from_openid, -10); $model_user->unique_name = $unique_name; $model_user->updated_time = $date_now; $model_user->created_time = $date_now; $model_user->save(0); $user_info = $model_user; } $model_bind = new UserOpenidUnionid(); $model_bind->uid = $user_info['uid']; $model_bind->openid = $from_openid; $model_bind->unionid = ''; $model_bind->other_openid = $from_openid; $model_bind->updated_time = $date_now; $model_bind->created_time = $date_now; $model_bind->save(0); } if ($bind_info) { $model_message = new UserMessageHistory(); $model_message->uid = $bind_info['uid']; $model_message->type = 1; $model_message->content = ltrim($content, "#"); $model_message->status = 1; $model_message->updated_time = $date_now; $model_message->created_time = $date_now; $model_message->save(0); } } }