/** * 设置缓存文件前缀 * * @return void */ public function boot() { $this->filePrefix = Wechat::getOption('appId'); }
/** * 设置图片 * * @param string $path * * @return Image */ public function media($path) { $this->setAttribute('media_id', Wechat::media()->image($path)); return $this; }
/** * 获取用户所在的组 * * @param string $openId * * @return integer */ public function getGroup($openId) { $params = array('openid' => $openId); $response = Wechat::request('POST', self::API_GROUP, $params); return $response['groupid']; }
/** * 删除菜单 * * @return boolean */ public function delete() { Wechat::request('GET', self::API_DELETE); return true; }
/** * 发送消息 * * @param string $openId * * @return boolean */ public function to($openId) { if (empty($this->message)) { throw new Exception("未设置要发送的消息"); } $this->message->to = $openId; Wechat::request('POST', self::API_MESSAGE_SEND, $this->message->buildForStaff()); return true; }
/** * 下载媒体文件 * * @param string $mediaId * @param string $filename * * @return mixed */ public function download($mediaId, $filename = '') { $params = array('media_id' => $mediaId); $contents = Wechat::request('GET', self::API_GET, $params); return $filename ? $contents : file_put_contents($filename, $contents); }
/** * 批量移动用户 * * @param array $openIds * @param integer $groupId * * @return boolean */ public function moveUsers(array $openIds, $groupId) { $params = array('openid_list' => $openIds, 'to_groupid' => $groupId); Wechat::request('POST', self::API_MEMBER_BATCH_UPDATE, $params); return true; }
/** * 设置音乐消息封面图 * * @param string $path * * @return Music */ public function thumb($path) { $this->setAttribute('thumb_media_id', Wechat::media()->thumb($path)); return $this; }
/** * 静态访问 * * @param string $method * @param array $args * * @return mixed */ public static function __callStatic($method, $args) { self::requireInstance(); return self::$instance->__call($method, $args); }
/** * 检验消息的真实性,并且获取解密后的明文. * <ol> * <li>利用收到的密文生成安全签名,进行签名验证</li> * <li>若验证通过,则提取xml中的加密消息</li> * <li>对消息进行解密</li> * </ol> * * @param string $msgSignature 签名串,对应URL参数的msg_signature * @param string $timestamp 时间戳 对应URL参数的timestamp * @param string $nonce 随机串,对应URL参数的nonce * @param string $postXML 密文,对应POST请求的数据 * @param string &$msg 解密后的原文,当return返回0时有效 * * @return array */ public function decryptMsg($msgSignature, $nonce, $timestamp, $postXML) { //提取密文 $array = XML::parse($postXML); if (empty($array)) { throw new Exception('Invalid xml.', self::ERROR_PARSE_XML); } $encrypted = $array['Encrypt']; //验证安全签名 $signature = $this->getSHA1(Wechat::getOption('token'), $timestamp, $nonce, $encrypted); if ($signature != $msgSignature) { throw new Exception('Invalid Signature.', self::ERROR_INVALID_SIGNATURE); } return XML::parse($this->decrypt($encrypted, Wechat::getOption('appId'))); }
/** * 通过code授权 * * @param string $code * * @return array */ protected function authorize($code) { if ($this->authResult) { return $this->authResult; } // 关闭自动加access_token参数 Wechat::autoRequestToken(false); $params = array('appid' => Wechat::getOption('appId'), 'secret' => Wechat::getOption('secret'), 'code' => $code, 'grant_type' => 'authorization_code'); $authResult = Wechat::request('GET', self::API_TOKEN_GET, $params); // 开启自动加access_token参数 Wechat::autoRequestToken(true); //TODO:refresh_token机制 return $this->authResult = $authResult; }