/** * 自定义创建菜单栏 * */ public function createMenusOp() { if (request()->isAjax() && request()->isPost()) { $openid = input('post.openid'); if ($openid == '') { return jsons(['status' => '0', 'msg' => '操作异常']); } $custom_menu = model('custom_menu')->where(['open_id' => $openid, 'parent_id' => '0'])->order('id abs')->select()->toArray(); $result = []; $param = model('parameter')->getval(); $this->grant = new grant($param->token, $param->encodingAesKey, $param->appid, $param->appsecret); $getctoken = model('parameter')->getctoken(); if ($getctoken['code'] == '0') { return jsons(['status' => '0', 'msg' => $getctoken['msg']]); } else { $component_access_token = $getctoken['component_access_token']; } $gettoken = $this->grant->gettoken($openid, $component_access_token, $this->userinfo['uid']); if ($gettoken['code'] == '0') { return jsons(['status' => '0', 'msg' => $gettoken['msg']]); } else { $access_token = $gettoken['access_token']; } $del = $this->grant->custommenu($access_token, '', 2); foreach ($custom_menu as $key => $value) { $data = model('custom_menu')->where(['open_id' => $openid, 'parent_id' => $value['id']])->select()->toArray(); if (empty($data)) { $submenu = model('submenu')->where(['open_id' => $openid, 'key' => $value['key']])->field('type,content,media_id')->find(); if ($submenu) { switch ($submenu['type']) { case 'view': $result['button'][$key]['name'] = $value['name']; $result['button'][$key]['type'] = $submenu['type']; $result['button'][$key]['url'] = $submenu['content']; break; case 'text': $result['button'][$key]['name'] = $value['name']; $result['button'][$key]['type'] = 'click'; $result['button'][$key]['key'] = $value['key']; break; case 'image': $result['button'][$key]['name'] = $value['name']; $result['button'][$key]['type'] = 'media_id'; $result['button'][$key]['media_id'] = $submenu['media_id']; break; case 'news': $submenu = model('management')->where(['open_id' => $openid, 'id' => $submenu['content']])->field('content,media_id')->find(); $result['button'][$key]['name'] = $value['name']; $result['button'][$key]['type'] = 'view_limited'; $result['button'][$key]['media_id'] = $submenu['media_id']; break; default: break; } } else { $result['button'][$key]['name'] = $value['name']; $result['button'][$key]['key'] = $value['key']; $result['button'][$key]['type'] = 'click'; } } else { $result['button'][]['name'] = $value['name']; foreach ($data as $k => $v) { $submenu = model('submenu')->where(['open_id' => $openid, 'key' => $v['key']])->field('type,content,media_id')->find(); if ($submenu) { switch ($submenu['type']) { case 'view': $result['button'][$key]['sub_button'][$k]['name'] = $v['name']; $result['button'][$key]['sub_button'][$k]['type'] = $submenu['type']; $result['button'][$key]['sub_button'][$k]['url'] = $submenu['content']; break; case 'text': $result['button'][$key]['sub_button'][$k]['name'] = $v['name']; $result['button'][$key]['sub_button'][$k]['type'] = 'click'; $result['button'][$key]['sub_button'][$k]['key'] = $v['key']; break; case 'image': $result['button'][$key]['sub_button'][$k]['name'] = $v['name']; $result['button'][$key]['sub_button'][$k]['type'] = 'media_id'; $result['button'][$key]['sub_button'][$k]['media_id'] = $submenu['media_id']; break; case 'news': $submenu = model('management')->where(['open_id' => $openid, 'id' => $submenu['content']])->field('content,media_id')->find(); $result['button'][$key]['sub_button'][$k]['name'] = $v['name']; $result['button'][$key]['sub_button'][$k]['type'] = 'view_limited'; $result['button'][$key]['sub_button'][$k]['media_id'] = $submenu['media_id']; break; default: break; } } } } } $dataType = json_decode($this->grant->custommenu($access_token, $result), true); if (!in_array('ok', $dataType)) { return jsons(['status' => '0', 'msg' => '保存失败']); } return jsons(['status' => '1', 'msg' => '保存成功']); } }
/** * 添加*菜单 */ public function addmenu_ziOp() { if (request()->isPost() && request()->isAjax()) { $open_id = input('open_id'); $menuName = input('menuName'); //菜单名字 if (strlen($menuName) > 16) { return jsons(['status' => '0', 'msg' => '菜单标题过长']); } $need = input('need'); //是否有子菜单 $parent_id = input('parent_id'); $key = input('name_key'); $order = input('order'); $type = 'click'; $id = model('wx_authorization_information')->where(['uid' => $this->userinfo['uid'], 'user_name' => $open_id])->value('id'); if ($id == '') { return jsons(['status' => '0', 'msg' => '参数异常']); } $customMenuId = model('custom_menu')->where(['uid' => $this->userinfo['uid'], 'open_id' => $open_id, 'key' => $key])->value('id'); if ($customMenuId == '') { $customMenu_array = [ 'uid' => $this->userinfo['uid'], 'name' => $menuName, 'type' => $type, 'open_id' => $open_id, 'parent_id' => '0', 'key' => $key, 'order_type' => $order, 'switch' => 9, ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(false)->save($customMenu_array); if ($customMenu === false) { return jsons(['status' => '0', 'msg' => '保存失败']); } } else { $customMenu_array = [ 'name' => $menuName, 'type' => $type, 'switch' => 9, ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(false)->save($customMenu_array, ['key' => $key]); if ($customMenu === false) { return jsons(['status' => '0', 'msg' => '保存失败']); } } $submenu = model('submenu')->where(['key' => $key])->field('id,type,media_id,content')->find(); if (!empty($submenu)) { if ($submenu['type'] == 'image') { $param = model('parameter')->getval(); $this->grant = new grant($param->token, $param->encodingAesKey, $param->appid, $param->appsecret); $getctoken = model('parameter')->getctoken(); if ($getctoken['code'] == '0') { return jsons(['status' => '0', 'msg' => $getctoken['msg']]); } else { $component_access_token = $getctoken['component_access_token']; } $gettoken = $this->grant->gettoken($open_id, $component_access_token, $this->userinfo['uid']); if ($gettoken['code'] == '0') { return jsons(['status' => '0', 'msg' => $gettoken['msg']]); } else { $access_token = $gettoken['access_token']; } if (!$this->delmaterial($submenu, $access_token)) { return jsons(['status' => 0, 'msg' => '保存失败']); } } $del = model('submenu')->destroy($submenu['id']); if ($del === false) { return jsons(['status' => 0, 'msg' => '保存失败']); } } return jsons(['status' => '1', 'msg' => '保存成功']); } }
/** * * 添加自定义菜单内容 * */ public function addmenuOp() { if (request()->isPost() && request()->isAjax()) { $key = input('post.key'); //菜单自定义键 $parentId = input('post.parentId'); //一级菜单的标识 $menuAction = input('post.menuAction'); //菜单动作 $menuType = input('post.menuType'); //菜单类型 $content = input('post.content'); //菜单内容 $menuName = input('post.menuName'); //菜单名字 $openId = input('post.open_id'); //微信id $order = input('post.order'); //微信id if (empty($openId) || empty($menuName) || empty($key)) { return jsons(['status' => '0', 'msg' => '系统异常请重新填写']); } if (strlen($menuName) > 16) { return jsons(['status' => '0', 'msg' => '菜单标题过长']); } if ($parentId == '0') { $this->delMenu($openId, $parentId, $order); } $count = model('custom_menu')->where(['parent_id' => $parentId, 'open_id' => $openId, 'order_type' => $order])->count(); if ($count >= 6) { return jsons(['status' => '0', 'msg' => '二级菜单上限']); } $id = model('wx_token')->where(['to_user_name' => $openId])->value('id'); if (!$id) { return jsons(['status' => '0', 'msg' => '数据不存在']); } $param = model('parameter')->getval(); $this->grant = new grant($param->token, $param->encodingAesKey, $param->appid, $param->appsecret); // $component_access_token = model('parameter')->getctoken(); $getctoken = model('parameter')->getctoken(); if ($getctoken['code'] == '0') { return jsons(['status' => '0', 'msg' => $getctoken['msg']]); } else { $component_access_token = $getctoken['component_access_token']; } $gettoken = $this->grant->gettoken($openId, $component_access_token, $this->userinfo['uid']); if ($gettoken['code'] == '0') { return jsons(['status' => '0', 'msg' => $gettoken['msg']]); } else { $access_token = $gettoken['access_token']; } $cid = model('custom_menu')->where(['key' => $key])->value('id'); switch ($menuType) { case 'view': $preg = "/^http(s)?:\\/\\/.+/"; if (!preg_match($preg, $content)) { return jsons(['status' => '0', 'msg' => '请正确输入网址']); } if (empty($cid)) { $data = [ 'uid' => $this->userinfo['uid'], 'name' => $menuName, 'type' => $menuType, 'open_id' => $openId, 'parent_id' => $parentId, 'key' => $key, 'order_type' => $order, 'switch' => '0', ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(false)->save($data); if ($customMenu === false) { return jsons(['status' => '0', 'msg' => '保存失败']); } $submenuData = [ 'type' => $menuType, 'content' => $content, 'datetime' => time(), 'open_id' => $openId, 'key' => $key, ]; $submenu = model('submenu')->allowField(true)->isUpdate(false)->save($submenuData); if ($submenu === false) { return jsons(['status' => '0', 'msg' => '保存失败']); } } else { $result = model('submenu')->where(['key' => $key])->field('type,content,media_id')->find(); $data = [ 'name' => $menuName, 'type' => $menuType, 'open_id' => $openId, 'parent_id' => $parentId, 'switch' => '0', ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(true)->save($data, ['key' => $key]); if ($customMenu === false) { return jsons(['status' => '0', 'msg' => '保存失败']); } $submenuData = [ 'type' => $menuType, 'content' => $content, 'datetime' => time(), 'open_id' => $openId, 'key' => $key, ]; if ($result['type'] == 'image') { if (!$this->delmaterial($result, $access_token)) { return jsons(['status' => 0, 'msg' => '保存失败']); } $submenuData['media_id'] = ''; } if ($result) { $submenu = model('submenu')->allowField(true)->isUpdate(true)->save($submenuData, ['key' => $key]); } else { $submenu = model('submenu')->allowField(true)->isUpdate(false)->save($submenuData); } if ($submenu === false) { return jsons(['status' => '0', 'msg' => '保存失败']); } } return jsons(['status' => '1', 'msg' => '保存成功']); break; case 'image': $file = request()->file('upfile'); $file_name = $this->uploadimg($file); if ($file_name == '') { return jsons(['status' => '0', 'msg' => '上传文件失败']); } $img_path = UPLOADS_WE_PATH . $file_name; $param = model('parameter')->getval(); $this->grant = new grant($param->token, $param->encodingAesKey, $param->appid, $param->appsecret); // $component_access_token = model('parameter')->getctoken(); $getctoken = model('parameter')->getctoken(); if ($getctoken['code'] == '0') { return jsons(['status' => '0', 'msg' => $getctoken['msg']]); } else { $component_access_token = $getctoken['component_access_token']; } $gettoken = $this->grant->gettoken($openId, $component_access_token, $this->userinfo['uid']); if ($gettoken['code'] == '0') { return jsons(['status' => '0', 'msg' => $gettoken['msg']]); } else { $access_token = $gettoken['access_token']; } $media_id = json_decode($this->grant->uploadImg($access_token, $img_path, $order = 2), true); if (!array_key_exists('media_id', $media_id)) { return ['status' => 0, 'msg' => '上传永久图片失败']; } if ($cid) { $result = model('submenu')->where(['key' => $key])->field('type,content,media_id')->find(); if ($result['type'] == 'image') { if (!$this->delmaterial($result, $access_token)) { return jsons(['status' => 0, 'msg' => '上传永久图文失败']); } } $data = [ 'name' => $menuName, 'type' => $menuType, 'parent_id' => $parentId, 'switch' => '0', ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(true)->save($data, ['key' => $key]); if ($customMenu === false) { return jsons(['status' => 0, 'msg' => '保存失败']); } $submenu_array = [ 'datetime' => time(), 'media_id' => $media_id['media_id'], 'type' => $menuType, 'content' => $file_name ]; if ($result) { $submenu = model('submenu')->allowField(true)->isUpdate(true)->save($submenu_array, ['key' => $key]); } else { $submenu_array['open_id'] = $openId; $submenu_array['key'] = $key; $submenu = model('submenu')->allowField(true)->isUpdate(false)->save($submenu_array); } } else { $customMenu_array = [ 'uid' => $this->userinfo['uid'], 'name' => $menuName, 'type' => $menuType, 'open_id' => $openId, 'parent_id' => $parentId, 'key' => $key, 'time' => time(), 'order_type' => $order, 'switch' => '0', ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(false)->save($customMenu_array); if ($customMenu === false) { return jsons(['status' => '0', 'msg' => '保存失败']); } $submenu_array = [ 'datetime' => time(), 'media_id' => $media_id['media_id'], 'type' => $menuType, 'content' => $file_name, 'key' => $key, 'open_id' => $openId ]; $submenu = model('submenu')->allowField(true)->isUpdate(false)->save($submenu_array); } if ($submenu === false) { return jsons(['status' => '0', 'msg' => '保存失败']); } return jsons(['status' => '1', 'msg' => '保存成功']); break; case 'news': $management_data = model('management')->where(['id' => $content])->find(); if (empty($management_data)) { return jsons(['status' => 0, 'msg' => '素材不存在']); } if ($management_data['media_id'] == '') { $imgpath = UPLOADS_WE_PATH . $management_data['cover']; if (!$management_data['cover'] && !file_exists($imgpath)) { return jsons(['status' => 0, 'msg' => '素材不存在']); } $media = json_decode($this->grant->uploadImg($access_token, $imgpath, $order = 2), true); if ($media == '') { return jsons(['status' => 0, 'msg' => '保存失败']); } if (!array_key_exists('media_id', $media)) { return jsons(['status' => 0, 'msg' => '保存失败']); } $preg = '/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/'; preg_match_all($preg, $management_data['content'], $imgArr); foreach ($imgArr[1] as $key => $val) { $img_path = UPLOADS_WE_PATH . $val; $media_url = json_decode($this->grant->uploadImg($access_token, $img_path, $order = 2), true); if ($media_url == '') { return jsons(['status' => 0, 'msg' => '保存失败']); } $management_data['content'] = str_replace($val, $media_url['url'], $management_data['content']); } $data_material = [ 'articles' => [ [ 'title' => urlencode($management_data['title']), 'thumb_media_id' => $media['media_id'], 'author' => urlencode($management_data['author']), 'digest' => urlencode($management_data['digest']), 'content' => $management_data['content'], 'show_cover_pic' => '1', 'content_source_url' => urlencode($management_data['content_source_url']), ] ] ]; $material = json_decode($this->grant->material($access_token, $data_material), true); if (!array_key_exists('media_id', $material)) { return jsons(['status' => 0, 'msg' => '保存失败']); } $management_data['media_id'] = $material['media_id']; $management_data['id'] = $content; $data = [ 'id' => $content, 'media_id' => $material['media_id'], 'picurl' => $media['url'], ]; $management = model('management')->allowField(true)->isUpdate(true)->save($data); if ($management === false) { return jsons(['status' => 0, 'msg' => '保存失败']); } } if ($cid) { $result = model('submenu')->where(['key' => $key])->field('type,content,media_id')->find(); if ($result['type'] == 'image') { if (!$this->delmaterial($result, $access_token)) { return jsons(['status' => 0, 'msg' => '保存失败']); } model('submenu')->destroy(['media_id' => $result['media_id']]); } $customMenu_array = [ 'name' => $menuName, 'type' => $menuType, 'switch' => '0', ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(true)->save($customMenu_array, ['key' => $key]); $submenu_array = [ 'type' => $menuType, 'content' => $content, 'datetime' => time(), 'media_id' => $management_data['media_id'], ]; if ($result) { $submenu = model('submenu')->allowField(true)->isUpdate(true)->save($submenu_array, ['key' => $key]); } else { $submenu_array['open_id'] = $openId; $submenu_array['key'] = $key; $submenu = model('submenu')->allowField(true)->isUpdate(false)->save($submenu_array); } } else { $customMenu_array = [ 'uid' => $this->userinfo['uid'], 'name' => $menuName, 'type' => $menuType, 'open_id' => $openId, 'parent_id' => $parentId, 'key' => $key, 'time' => time(), 'order_type' => $order, 'switch' => '0', ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(false)->save($customMenu_array); if ($customMenu === false) { return jsons(['status' => 0, 'msg' => '保存失败']); } $submenu_array = [ 'type' => $menuType, 'content' => $content, 'datetime' => time(), 'open_id' => $openId, 'key' => $key, 'media_id' => $management_data['media_id'], ]; $submenu = model('submenu')->allowField(true)->isUpdate(false)->save($submenu_array); } if ($submenu === false) { return jsons(['status' => 0, 'msg' => '保存失败']); } return jsons(['status' => 1, 'msg' => '保存成功']); break; default: if (!$cid) { $data = [ 'uid' => $this->userinfo['uid'], 'name' => $menuName, 'type' => $menuType, 'open_id' => $openId, 'parent_id' => $parentId, 'key' => $key, 'order_type' => $order, ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(false)->save($data); if ($customMenu === false) { return jsons(['status' => 0, 'msg' => '保存失败']); } $submenu_array = [ 'datetime' => time(), 'open_id' => $openId, 'key' => $key, 'content' => $content, 'type' => $menuType, ]; $submenu = model('submenu')->data($submenu_array, true)->allowField(true)->isUpdate(false)->save(); } else { $result = model('submenu')->where(['key' => $key])->field('type,content,media_id')->find(); if ($result['type'] == 'image') { if (!$this->delmaterial($result, $access_token)) { return jsons(['status' => 0, 'msg' => '保存失败']); } } $data = [ 'name' => $menuName, 'type' => $menuType, 'parent_id' => $parentId, 'order_type' => $order, 'switch' => 0, ]; $customMenu = model('custom_menu')->allowField(true)->isUpdate(true)->save($data, ['key' => $key]); $sid = model('submenu')->where(['key' => $key])->value('id'); $submenu_array = [ 'datetime' => time(), 'content' => $content, 'type' => $menuType, ]; if ($sid) { $submenu = model('submenu')->allowField(true)->isUpdate(true)->save($submenu_array, ['key' => $key]); } else { $submenu_array['open_id'] = $openId; $submenu_array['key'] = $key; $submenu = model('submenu')->allowField(true)->isUpdate(false)->save($submenu_array); } if ($submenu === false) { return jsons(['status' => 0, 'msg' => '保存失败']); } } return jsons(['status' => 1, 'msg' => '保存成功']); break; } } }
<?php
namespace Wechat;
class grant {
private $token;
private $encodingAesKey;
private $appid;
private $appsecret;
/**
* 构造函数
* @param $token string 公众平台上,开发者设置的token
* @param $encodingAesKey string 公众平台上,开发者设置的EncodingAESKey
* @param $appId string 公众平台的appId
*/
public function __construct($token, $encodingAesKey, $appid, $appsecret)
{
$this->token = $token;
$this->encodingAesKey = $encodingAesKey;
$this->appid = $appid;
$this->appsecret = $appsecret;
}
public function rqcode($component_access_token)
{
$preAuthCode = $this->preAuthCode($component_access_token);
$redirect_uri = "http://chat.wenidc.com/kefu/wechat/callback"; //回调地址
$url = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=$this->appid&pre_auth_code=${preAuthCode}&redirect_uri=${redirect_uri}";
return $url;
}
//2,获取第三方平台component_access_token
public function componentAccessToken($component_verify_ticket)
{
$url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
$data = array(
"component_appid" => $this->appid,
"component_appsecret" => $this->appsecret,
"component_verify_ticket" => $component_verify_ticket
);
return json_decode($this->https_request($url, json_encode($data)), true);
}
//3,获取预授权码pre_auth_code
public function preAuthCode($component_access_token)
{
$url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=$component_access_token";
$data = array(
"component_appid" => $this->appid,
);
$send_result = $this->https_request($url, json_encode($data));
$send_result = json_decode($send_result, true);
if (!array_key_exists('errcode', $send_result)) {
return $send_result['pre_auth_code'];// 微信预授权码
} else {
return '';
}
}
//4,使用授权码换取公众号或小程序的接口调用凭据和授权信息
public function apiQueryAuth($component_access_token, $auth_code)
{
$url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=$component_access_token";
$data = array(
"component_appid" => $this->appid,
"authorization_code" => $auth_code,
);
$send_result = json_decode($this->https_request($url, json_encode($data)), true);
return $send_result;
}
//获取授权公众号或小程序的接口调用凭据(令牌)
public function gettoken($tousername, $component_access_token,$userid)
{
$result = model('wx_token')->where(['uid' => $userid, 'to_user_name' => $tousername])->find();
if(empty($result)){
return ['code' =>'0','msg' =>'数据异常'];
}
$datetime = $result['datatime'] + $result['expires_in'];
if (time() > $datetime) {
$apiAuthorizerToken = $this->apiAuthorizerToken($component_access_token, $result, $userid);
if (array_key_exists('errcode', $apiAuthorizerToken)) {
return ['code' => '0', 'msg' => '数据异常'];
}
return ['code' => '1', 'msg' => '', 'access_token' =>$apiAuthorizerToken['authorizer_access_token']];
}
return ['code' => '1', 'msg' => '', 'access_token' => $result['authorizer_access_token']];
}
//5.获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
public function apiAuthorizerToken($component_access_token, $authInfo,$userid)
{
$url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=$component_access_token";
$data = array(
"component_appid" => $this->appid,
"authorizer_appid" => $authInfo['authorizer_appid'],
"authorizer_refresh_token" => $authInfo['authorizer_refresh_token'],
);
$send_result = $this->https_request($url, json_encode($data));
$result = json_decode($send_result, true);
if (!array_key_exists('errcode', $result)) {
$data = [
'authorizer_appid' => $authInfo['authorizer_appid'],
'authorizer_access_token' => $result['authorizer_access_token'],
'authorizer_refresh_token' => $result['authorizer_refresh_token'],
'expires_in' => $result['expires_in'],
'datatime' => time(),
];
$isupdate = array_key_exists('id', $authInfo);
if ($isupdate) {
$data['id'] = $authInfo['id'];
model('wx_token')->allowField(true)->isUpdate(true)->save($data);
} else {
$id = model('wx_token')->where(['uid' => $userid, 'to_user_name' => $authInfo['to_user_name']])->value('id');
if ($id) {
$data['id'] = $id;
model('wx_token')->allowField(true)->isUpdate(true)->save($data);
} else {
$data['uid'] = $userid;
$data['to_user_name'] = $authInfo['to_user_name'];
model('wx_token')->allowField(true)->isUpdate(false)->save($data);
}
}
}
return $result;
}
//6 .获取授权方的帐号基本信息
public function apiGetAuthorizerInfo($component_access_token, $authInfo)
{
$url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=$component_access_token";
$data = array(
'component_appid' => $this->appid,
'authorizer_appid' => $authInfo['authorizer_appid'],
);
$account_information = $this->https_request($url, json_encode($data)); //授权方的帐号基本信息
return json_decode($account_information, true);
}
//7.获取授权方的选项设置信息
public function apiGetAuthorizerOption($component_access_token, $account_information)
{
if (!array_key_exists('authorizer_info', $account_information)) {
return '使用授权码换取公众号信息失败';
}
$authorizer_info = $account_information['authorizer_info'];
$account_information = $account_information['authorization_info'];
$url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option?component_access_token=$component_access_token";
$data = array(
'component_appid' => $this->appid,
'authorizer_appid' => $account_information['authorizer_appid'], //授权公众号或小程序的appid
'option_name' => 'voice_recognize', //选项名称
);
$option_settings_information = $this->https_request($url, json_encode($data)); //获取授权方的选项设置信息
return json_decode($option_settings_information, true);
}
// 8、设置授权方的选项信息
public function apiSetAuthorizerOption($component_access_token, $option_gettings_information)
{
$url = "https://api.weixin.qq.com/cgi-bin/component/api_set_authorizer_option?component_access_token=$component_access_token";
$data = array(
"component_appid" => $this->appid,
"authorizer_appid" => $option_gettings_information['authorizer_appid'],
"option_name" => $option_gettings_information['option_name'],
"option_value" => $option_gettings_information['option_value'],
);
$apiSetAuthorizerOption = $this->https_request($url, json_encode($data)); //获取授权方的选项设置信息
return json_decode($apiSetAuthorizerOption, true);
}
//客服给粉丝回消息
public function send_message($userinfo,$data, $to_user_name, $component_access_token){
$gettoken = $this->gettoken($to_user_name, $component_access_token, $userinfo['uid']);
if ($gettoken['code'] == '0') {
return jsons(['status' => '0', 'msg' => $gettoken['msg']]);
} else {
$access_token = $gettoken['access_token'];
}
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token";
$resultData = $this->https_request($url, json_encode($data,JSON_UNESCAPED_UNICODE));
return $resultData;
}
//curl 获取文件数据
public function curl_file($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);//只取body头
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//curl_exec执行成功后返回执行的结果;不设置的话,curl_exec执行成功则返回true
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
//下载素材
public function getmedia($access_token, $media_id, $foldername)
{
$url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$media_id";
$files = $this->curl_file($url);
if (!file_exists($foldername)) {
mkdir($foldername, 0777, true);
}
$filename = uniqid('chat_img_' . false) . '.jpg'; //拼接文件名
$targetName = $foldername . '/' . $filename;
$saveName = $foldername . $filename;
$newFile = fopen($targetName, 'w'); // 打开写入
fwrite($newFile, $files); //写入二进制流到文件
fclose($newFile);
return $filename;
}
//获取发送人的信息
public function getinfo($access_token, $openid, $lang = 'zh_CN')
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=$lang";
$result = $this->https_request($url);
return $result;
}
//上传素材并获取唯一标识
public function uploadImg($access_token, $filepath,$type = '1')
{
if($type == '1'){
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=image"; // 1代表上传临时图片素材
} else if($type == '2') {
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=image"; // 2代表上传永久图片素材
}else if($type == '3'){
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=$access_token"; // 3代表上传永久图文返回url
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, 1);
$data = array('media' => new \CURLFile($filepath));//php5.6
//$data = array('media' => '@' . $filepath);//php5.5
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if (curl_errno($curl)) {
return 'Errno' . curl_error($curl);
}
curl_close($curl);
return $result;
}
/**
* 新增永久图文素材
* @param $title
*/
public function material($access_token, $data, $type = 1){
if ($type == 1) {
$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=$access_token";
} else if($type == 2) {
$url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=$access_token";
} else if($type == 3){
$url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=$access_token";
}
$resultData = $this->https_request($url, json_encode($data));
return $resultData;
}
/**
* 自定义菜单
*/
public function custommenu($access_token, $data ,$type=1){
if ($type == 1) {
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token";
} else if ($type == 2) {
$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=$access_token";
} else if ($type == 3){
$url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=$access_token";
}
$resultData = $this->https_request($url, jsons($data));
return $resultData;
}
private function https_request($url, $data = null, $outtime='30'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_TIMEOUT, $outtime); //只需要设置一个秒的数量就可以
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
// if (curl_exec($curl) === false) {
// echo 'Curl error: ' . curl_error($curl);
// }
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}