business_id = $business_id; } /** * @param $business_id * @param $app * @return static * @throws AppException */ public static function current($business_id = 0, $app = null) { if (static::$current) { return static::$current; } static::$current = new static($business_id); if ($app) { static::$current->setApp($app); } else { $config = SettingService::getQyWxSetting($business_id); static::$current->setApp(Factory::work([ 'corp_id' => $config['corpid'], 'agent_id' => $config['agentid'], 'secret' => $config['contact_secret'], ])); } return static::$current; } public function setApp($app) { $this->agent_app = $app; } // 设置文本昵称 public function setNickname($nickname) { $this->nickname = $nickname; } public function filterName() { $this->filterName = true; } /** * Upload Image. * * @return mixed */ public function uploadImage(string $path) { return $this->upload('image', $path); } /** * Upload Voice. * * @return mixed */ public function uploadVoice(string $path) { return $this->upload('voice', $path); } /** * Upload Video. * * @return mixed */ public function uploadVideo(string $path) { return $this->upload('video', $path); } /** * Upload File. * * @return mixed */ public function uploadFile(string $path) { return $this->upload('file', $path); } /** * 组装请求 * @param string $type * @param string $url * @return mixed */ private function upload(string $type, string $url) { $query = ['type' => $type]; $multipart[0] = [ 'name' => "media", 'filename' => self::baseName($url), 'contents' => fopen($url, 'r'), ]; return $this->httpUpload($query, $multipart); } /** * 发送请求上传临时素材给企业微信 * @param string $query * @param array $multipart * @return mixed */ private function httpUpload(array $query, array $multipart) { $res = $this->agent_app->media->request(self::MEDIA_UPLOAD, 'POST', ['query' => $query, 'multipart' => $multipart, 'connect_timeout' => 30, 'timeout' => 30, 'read_timeout' => 30]); return $res; } /** * 返回文件名 * @param $filename * @return string */ public static function baseName($filename) : string { return preg_replace('/^.+[\\\\\\/]/', '', $filename); } public function handleContent($content_list) { foreach ($content_list as &$v) { switch ($v['type']) { case 'text': // PC后台编辑素材内容时换行有以下三种形式 $str = str_replace('