* @version : HuoMp 1.0 */ namespace huoMpMsg\controller; use huo\controller\common\Base; use huoMpMsg\constant\OfficialAccountConst; use huoMpMsg\status\OfficialAccountStatus; class OfficialAccountApi extends Base { /** * 获取微信服务器IP地址 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140187 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * * @return array */ public function getCallbackIp($wx_app_id, $wx_app_secret) { $_url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'?access_token='.$_access_token; $_param = ''; $_ret = Common::curl($_url, $_param); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode'])) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } return $_rdata['ip_list']; } /** * 添加客服帐号 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * * @return array */ public function addFfAccount($wx_app_id, $wx_app_secret, $kf_account, $nickname, $password) { $_url = 'https://api.weixin.qq.com/customservice/kfaccount/add'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'?access_token='.$_access_token; $_param['kf_account'] = $kf_account; $_param['nickname'] = $nickname; $_param['password'] = $password; $_ret = Common::curl($_url, http_build_query($_param), 'POST'); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code)); } /** * 修改客服帐号 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * * @param string $kf_account 客服邮箱 * @param string $nickname 客服昵称 * @param string $password 客服密码 * * @return array */ public function updateKfAccount($wx_app_id, $wx_app_secret, $kf_account, $nickname, $password) { $_url = 'https://api.weixin.qq.com/customservice/kfaccount/update'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'?access_token='.$_access_token; $_param['kf_account'] = $kf_account; $_param['nickname'] = $nickname; $_param['password'] = $password; $_ret = Common::curl($_url, http_build_query($_param), 'POST'); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code)); } /** * 删除客服帐号 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * * @param string $kf_account 客服邮箱 * @param string $nickname 客服昵称 * @param string $password 客服密码 * * @return array */ public function delKfAccount($wx_app_id, $wx_app_secret, $kf_account, $nickname, $password) { $_url = 'https://api.weixin.qq.com/customservice/kfaccount/del'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'?access_token='.$_access_token; $_param['kf_account'] = $kf_account; $_param['nickname'] = $nickname; $_param['password'] = $password; $_ret = Common::curl($_url, http_build_query($_param), 'POST'); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code)); } /** * 设置客服帐号的头像 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * * @param string $kf_account 客服邮箱 * * @return array */ public function uploadKfHeadImg($wx_app_id, $wx_app_secret, $kf_account) { $_url = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg'; $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code)); } /** * 获取所有客服账号 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * * @return array */ public function getKfList($wx_app_id, $wx_app_secret) { $_url = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'?access_token='.$_access_token; $_ret = Common::curl($_url, ''); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata); } /** * 获取所有客服账号 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * * @param string $open_id * @param string $msgtype 消息类型 * @param mixd $data * * @return array */ public function sendMsg($wx_app_id, $wx_app_secret, $open_id, $msgtype, $data = null) { $_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'?access_token='.$_access_token; $_param['touser'] = $open_id; $_param['msgtype'] = $msgtype; switch ($msgtype) { case OfficialAccountConst::MSG_TYPE_TEXT: $_param[$msgtype] = $data; break; case OfficialAccountConst::MSG_TYPE_LINK: case OfficialAccountConst::MSG_TYPE_IMAGE: case OfficialAccountConst::MSG_TYPE_VOICE: $_param[$msgtype] = $data; break; case OfficialAccountConst::MSG_TYPE_VIDEO: break; case OfficialAccountConst::MSG_TYPE_MUSIC: break; case OfficialAccountConst::MSG_TYPE_NEWS: break; case OfficialAccountConst::MSG_TYPE_MPNEWS: break; case OfficialAccountConst::MSG_TYPE_WXCARD: break; case OfficialAccountConst::MSG_TYPE_MINIPROGRAMPAGE: break; default: break; } $_param_json = json_encode($_param, JSON_UNESCAPED_UNICODE); $_header = ['Content-Type: application/octet-stream; charset=utf-8']; $_ret = Common::curl($_url, $_param_json, 'POST', $_header); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code)); } /** * 发送模板消息 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * * @param string $open_id * @param string $template_id 模板ID * @param string $url 模板跳转链接 * @param array $miniprogram 跳小程序所需数据,不需跳小程序可不用传该数据 * @param mixed $data 模板数据 * * @return array */ public function sendTemplateMsg( $wx_app_id, $wx_app_secret, $open_id, $template_id, $url = '', $miniprogram = [], $data ) { $_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'?access_token='.$_access_token; $_param['touser'] = $open_id; $_param['template_id'] = $template_id; $_param['url'] = $url; $_param['miniprogram'] = $miniprogram; $_param['data'] = $data; $_param_json = json_encode($_param); $_header = ['Content-Type: application/octet-stream; charset=utf-8']; $_ret = Common::curl($_url, $_param_json, 'POST', $_header); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata); } /** * 发送模板消息 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * @param string $file_path 图片绝对路径 * * @return array */ public function imageUpload($wx_app_id, $wx_app_secret, $file_path) { $_url = 'https://api.weixin.qq.com/cgi-bin/media/upload'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_type = "image"; $_url = $_url.'?access_token='.$_access_token.'&type='.$_type; //这里声明文件的路径,使用绝对路径 $_file_data = array("media" => new \CURLFile($file_path)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $_file_data); $_ret = curl_exec($ch);//发送请求获取结果 curl_close($ch);//关闭会话 $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata); } }