| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603 |
- <?php
- namespace common\components;
- use common\services\xhMerchantExtendService;
- use Yii;
- use yii\helpers\Json;
- use linslin\yii2\curl;
- use common\services\xhWxOpenService;
- use common\services\xhMerchantService;
- /**
- * 微信交互接口
- */
- class weixinUtil
- {
- public $access_token;
-
- /**
- * 微信服务器全局返回码
- */
- public static $errCodes = array(
- '-1' => '系统繁忙',
- '0' => '请求成功',
- '40001' => '获取access_token时AppSecret错误,或者access_token无效',
- '40002' => '不合法的凭证类型',
- '40003' => '不合法的OpenID',
- '40004' => '不合法的媒体文件类型',
- '40005' => '不合法的文件类型',
- '40006' => '不合法的文件大小',
- '40007' => '不合法的媒体文件id',
- '40008' => '不合法的消息类型',
- '40009' => '不合法的图片文件大小',
- '40010' => '不合法的语音文件大小',
- '40011' => '不合法的视频文件大小',
- '40012' => '不合法的缩略图文件大小',
- '40013' => '不合法的APPID',
- '40014' => '不合法的access_token',
- '40015' => '不合法的菜单类型',
- '40016' => '不合法的按钮个数',
- '40017' => '不合法的按钮个数',
- '40018' => '不合法的按钮名字长度',
- '40019' => '不合法的按钮KEY长度',
- '40020' => '不合法的按钮URL长度',
- '40021' => '不合法的菜单版本号',
- '40022' => '不合法的子菜单级数',
- '40023' => '不合法的子菜单按钮个数',
- '40024' => '不合法的子菜单按钮类型',
- '40025' => '不合法的子菜单按钮名字长度',
- '40026' => '不合法的子菜单按钮KEY长度',
- '40027' => '不合法的子菜单按钮URL长度',
- '40028' => '不合法的自定义菜单使用用户',
- '40029' => '不合法的oauth_code',
- '40030' => '不合法的refresh_token',
- '40031' => '不合法的openid列表',
- '40032' => '不合法的openid列表长度',
- '40033' => '不合法的请求字符,不能包含\uxxxx格式的字符',
- '40035' => '不合法的参数',
- '40038' => '不合法的请求格式',
- '40039' => '不合法的URL长度',
- '40050' => '不合法的分组id',
- '40051' => '分组名字不合法',
- '41001' => '缺少access_token参数',
- '41002' => '缺少appId参数',
- '41003' => '缺少refresh_token参数',
- '41004' => '缺少secret参数',
- '41005' => '缺少多媒体文件数据',
- '41006' => '缺少media_id参数',
- '41007' => '缺少子菜单数据',
- '41008' => '缺少oauth code',
- '41009' => '缺少openid',
- '42001' => 'access_token超时',
- '42002' => 'refresh_token超时',
- '42003' => 'oauth_code超时',
- '43001' => '需要GET请求',
- '43002' => '需要POST请求',
- '43003' => '需要HTTPS请求',
- '43004' => '需要接收者关注',
- '43005' => '需要好友关系',
- '44001' => '多媒体文件为空',
- '44002' => 'POST的数据包为空',
- '44003' => '图文消息内容为空',
- '44004' => '文本消息内容为空',
- '45001' => '多媒体文件大小超过限制',
- '45002' => '消息内容超过限制',
- '45003' => '标题字段超过限制',
- '45004' => '描述字段超过限制',
- '45005' => '链接字段超过限制',
- '45006' => '图片链接字段超过限制',
- '45007' => '语音播放时间超过限制',
- '45008' => '图文消息超过限制',
- '45009' => '接口调用超过限制',
- '45010' => '创建菜单个数超过限制',
- '45015' => '回复时间超过限制',
- '45016' => '系统分组,不允许修改',
- '45017' => '分组名字过长',
- '45018' => '分组数量超过上限',
- '45056' => '创建的标签数过多,请注意不能超过100个',
- '45057' => '该标签下粉丝数超过10w,不允许直接删除',
- '45058' => '不能修改0/1/2这三个系统默认保留的标签',
- '45059' => '有粉丝身上的标签数已经超过限制',
- '45157' => '标签名非法,请注意不能和其他标签重名',
- '45158' => '标签名长度超过30个字节',
- '45159' => '非法的标签',
- '46001' => '不存在媒体数据',
- '46002' => '不存在的菜单版本',
- '46003' => '不存在的菜单数据',
- '46004' => '不存在的用户',
- '47001' => '解析JSON/XML内容错误',
- '48001' => 'api功能未授权',
- '49003' => '传入的openid不属于此AppID',
- '50001' => '用户未授权该api',
- '50002' => '用户受限,可能是违规后接口被封禁',
- '61451' => '参数错误(invalid parameter)',
- '61452' => '无效客服账号(invalid kf_account)',
- '61453' => '客服帐号已存在(kf_account exsited)',
- '61454' => '客服帐号名长度超过限制(仅允许10个英文字符,不包括@及@后的公众号的微信号)(invalid kf_acount length)',
- '61455' => '客服帐号名包含非法字符(仅允许英文+数字)(illegal character in kf_account)',
- '61456' => '客服帐号个数超过限制(10个客服账号)(kf_account count exceeded)',
- '61457' => '无效头像文件类型(invalid file type)',
- '61450' => '系统错误(system error)',
- '61500' => '日期格式错误',
- '65301' => '不存在此menuid对应的个性化菜单',
- '65302' => '没有相应的用户',
- '65303' => '没有默认菜单,不能创建个性化菜单',
- '65304' => 'MatchRule信息为空',
- '65305' => '个性化菜单数量受限',
- '65306' => '不支持个性化菜单的帐号',
- '65307' => '个性化菜单信息为空',
- '65308' => '包含没有响应类型的button',
- '65309' => '个性化菜单开关处于关闭状态',
- '65310' => '填写了省份或城市信息,国家信息不能为空',
- '65311' => '填写了城市信息,省份信息不能为空',
- '65312' => '不合法的国家信息',
- '65313' => '不合法的省份信息',
- '65314' => '不合法的城市信息',
- '65316' => '该公众号的菜单设置了过多的域名外跳(最多跳转到3个域名的链接)',
- '65317' => '不合法的URL',
- '9001001' => 'POST数据参数不合法',
- '9001002' => '远端服务不可用',
- '9001003' => 'Ticket不合法',
- '9001004' => '获取摇周边用户信息失败',
- '9001005' => '获取商户信息失败',
- '9001006' => '获取OpenID失败',
- '9001007' => '上传文件缺失',
- '9001008' => '上传素材的文件类型不合法',
- '9001009' => '上传素材的文件尺寸不合法',
- '9001010' => '上传失败',
- '9001020' => '帐号不合法',
- '9001021' => '已有设备激活率低于50%,不能新增设备',
- '9001022' => '设备申请数不合法,必须为大于0的数字',
- '9001023' => '已存在审核中的设备ID申请',
- '9001024' => '一次查询设备ID数量不能超过50',
- '9001025' => '设备ID不合法',
- '9001026' => '页面ID不合法',
- '9001027' => '页面参数不合法',
- '9001028' => '一次删除页面ID数量不能超过10',
- '9001029' => '页面已应用在设备中,请先解除应用关系再删除',
- '9001030' => '一次查询页面ID数量不能超过50',
- '9001031' => '时间区间不合法',
- '9001032' => '保存设备与页面的绑定关系参数错误',
- '9001033' => '门店ID不合法',
- '9001034' => '设备备注信息过长',
- '9001035' => '设备申请参数不合法',
- '9001036' => '查询起始值begin不合法',
- );
-
- public function __construct($merchant = '')
- {
- $this->access_token = !empty($merchant) ? self::getAuthorizerAccessToken($merchant) : '';
- }
-
- /**
- * 获取用户基本信息
- * @param $accessToken
- * @param $openId
- * @return array
- */
- public static function userInfo($openId, $merchant)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$accessToken&openid=$openId";
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $result = Json::decode($result);
- if (isset($result['errcode'])) {
- //TODO 异常
- } else {
- return $result;
- }
- }
-
- //创建开放平台帐号
- public static function createOpenAccount($appId, $merchant)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/open/create?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $data = ['appid' => $appId];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- //绑定到开放平台
- public static function bindOpenAccount($appId, $openAppId, $merchant, $isMiniProgram = false)
- {
- if ($isMiniProgram) {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- } else {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- }
- $url = 'https://api.weixin.qq.com/cgi-bin/open/bind?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = ['appid' => $appId, 'open_appid' => $openAppId];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- //获取公众号、小程序绑定的开放平台帐号
- public static function getOpenAccount($appId, $merchant, $isMiniProgram = false)
- {
- if ($isMiniProgram) {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- } else {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- }
- $url = "https://api.weixin.qq.com/cgi-bin/open/get?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $data = ['appid' => $appId];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- //解绑公众号绑定的平台 shish 2019.9.15
- public static function unbindAccount($merchant)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/open/unbind?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $openAppId = $merchant['openAppId'];
- $appId = $merchant['wxAppId'];
- $data = ['appid' => $appId, 'open_appid' => $openAppId];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- //解绑小程序绑定的平台 shish 2019.9.15
- public static function unbindMiniProgram($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/open/unbind?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $openAppId = $merchant['openAppId'];
- $appId = $merchant['miniAppId'];
- $data = ['appid' => $appId, 'open_appid' => $openAppId];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- public static function getSubscribeUserList($merchant, $nextOpenId = '')
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$accessToken&next_openid=$nextOpenId";
- $curl = new curl\Curl();
- $result = $curl->get($url);
- return Json::decode($result);
- }
-
- public static function batchGetUserInfo($merchant, $openIdList)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=" . $accessToken;
- $format = [];
- foreach ($openIdList as $val) {
- $format[] = ['openid' => $val, 'lang' => "zh-CN"];
- }
- $newOpenIdList = ["user_list" => $format];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($newOpenIdList))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 查询所有分组
- * @param unknown $accessToken
- */
- public static function groupList($accessToken)
- {
- $url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=$accessToken";
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $result = Json::decode($result);
- if (isset($result['errcode'])) {
- return null;
- } else {
- return $result;
- }
- }
-
- /**
- * 提交自定义菜单到微信平台
- * @param $merchant
- * @param $menu
- * @return mixed
- */
- public function userMenuToWeiXin($menu)
- {
- $jsonData = urldecode(json_encode($menu, JSON_UNESCAPED_UNICODE));
- $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $response = $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
- return $response;
- }
-
- /**
- * 上传图片多媒体文件
- * @param unknown_type $type
- * @param unknown_type $access_token
- * @param unknown_type $data
- */
- public static function uploadImgMedia($merchant, $fullFile)
- {
- $access_token = self::getAuthorizerAccessToken($merchant);
- $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" . $access_token . "&type=image";
- $curl = curl_init();
- if (class_exists('\CURLFile')) {//关键是判断curlfile,官网推荐php5.5或更高的版本使用curlfile来实例文件
- $data = array('media' => new \CURLFile($fullFile));
- } else {
- $data = array('media' => file_get_contents($fullFile));
- }
- if (class_exists('\CURLFile')) {
- curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
- } else {
- if (defined('CURLOPT_SAFE_UPLOAD')) {
- curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
- }
- }
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- $result = curl_exec($curl);
- curl_close($curl);
- return json_decode($result);
- }
-
- /**
- * 上传视频文件
- * @param unknown_type
- * @param $merchant
- * @param $fullFile
- * @return mixed
- */
- public function uploadVideoMedia($merchant, $fullFile)
- {
- $access_token = self::getAuthorizerAccessToken($merchant);
- $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" . $access_token . "&type=video";
- $curl = curl_init();
- if (class_exists('\CURLFile')) {//关键是判断curlfile,官网推荐php5.5或更高的版本使用curlfile来实例文件
- $data = array('media' => new \CURLFile($fullFile));
- } else {
- $data = array('media' => file_get_contents($fullFile));
- }
- if (class_exists('\CURLFile')) {
- curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
- } else {
- if (defined('CURLOPT_SAFE_UPLOAD')) {
- curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
- }
- }
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- $result = curl_exec($curl);
- curl_close($curl);
- return json_decode($result);
- }
-
- /**
- * 下载多媒体文件
- * @param unknown_type $access_token
- * @param unknown_type $media_id
- */
- public static function uploadMedia($media_id, $access_token)
- {
- $url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $access_token . '&media_id=' . $media_id;
- $result = CurlUtil::httpsGet($url);
- if (isset($result['errcode'])) {
- return false;
- } else {
- return $result;
- }
- }
-
- /**
- * 获取素材总数
- * 图片和图文消息素材(包括单图文和多图文)的总数上限为5000,其他素材的总数上限为1000
- */
- public function getMaterialCount($merchant)
- {
- $token = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token={$token}";
- $curl = new curl\Curl();
- $response = $curl->get($url);
- return $response;
- }
-
- /**
- * 获取素材列表 图文列表
- * $data = ['type' => 'image','offset' => 0,'count' => 100];
- * type:news image video voice
- */
- public function batchgetMaterial($merchant, $data)
- {
- $token = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={$token}";
- $curl = new curl\Curl();
- $response = $curl->setOption(CURLOPT_POSTFIELDS, json_encode($data))->post($url);
- return json_decode($response, true);
- }
-
- /**
- * 解除绑定删除自定义菜单
- * @param string $accessToken
- */
- public static function menuDelete($merchant)
- {
- $token = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=$token";
- $curl = new curl\Curl();
- $response = $curl->get($url);
- return $response;
- }
-
- /**
- * 微信api不支持中文转义的json结构
- * @param array $arr
- */
- static function json_encode($arr)
- {
- if (count($arr) == 0) return "[]";
- $parts = array();
- $is_list = false;
- //Find out if the given array is a numerical array
- $keys = array_keys($arr);
- $max_length = count($arr) - 1;
- if (($keys [0] === 0) && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
- $is_list = true;
- for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
- if ($i != $keys [$i]) { //A key fails at position check.
- $is_list = false; //It is an associative array.
- break;
- }
- }
- }
- foreach ($arr as $key => $value) {
- if (is_array($value)) { //Custom handling for arrays
- if ($is_list)
- $parts [] = self::json_encode($value); /* :RECURSION: */
- else
- $parts [] = '"' . $key . '":' . self::json_encode($value); /* :RECURSION: */
- } else {
- $str = '';
- if (!$is_list)
- $str = '"' . $key . '":';
- //Custom handling for multiple data types
- if (!is_string($value) && is_numeric($value) && $value < 2000000000)
- $str .= $value; //Numbers
- elseif ($value === false)
- $str .= 'false'; //The booleans
- elseif ($value === true)
- $str .= 'true';
- else
- $str .= '"' . addslashes($value) . '"'; //All other things
- // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
- $parts [] = $str;
- }
- }
- $json = implode(',', $parts);
- if ($is_list)
- return '[' . $json . ']'; //Return numerical JSON
- return '{' . $json . '}'; //Return associative JSON
- }
-
- /**
- * 创建临时二维码 shish 2019.9.4
- */
- public static function qrcodeCreate($merchant, $sceneId, $expireSeconds = null)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$accessToken";
- //有效期默认一天
- $expireSeconds = isset($expireSeconds) ? $expireSeconds : 86400;
- $post = array('expire_seconds' => $expireSeconds, 'action_name' => 'QR_STR_SCENE', "action_info" => array('scene' => array('scene_str' => $sceneId)));
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 创建永久二维码
- */
- public static function qrcodePermanentCreate($merchant, $sceneId)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$accessToken";
- $post = array('action_name' => 'QR_LIMIT_STR_SCENE', "action_info" => array('scene' => array('scene_str' => $sceneId)));
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 获取Token
- */
- public static function getToken($REDIRECT_URI, $merchant, $scope)
- {
- $appId = $merchant['wxAppId'];
- $id = configDict::getConfig('openId');
- $open = xhWxOpenService::getById($id);
- $component_appid = $open['appId'];
- $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appId . "&redirect_uri=" . urlencode($REDIRECT_URI) . "&response_type=code&scope=" . $scope . "&state=1&component_appid={$component_appid}#wechat_redirect";
- header("Location:" . $url);
- //如果这里没有exit(),在apache服务器下无法实现跳转
- exit();
- }
-
- /**
- * 获取openId
- */
- public static function getOpenId($code, $merchant)
- {
- $appId = $merchant['wxAppId'];
- $id = configDict::getConfig('openId');
- $open = xhWxOpenService::getById($id);
- $component_appid = $open['appId'];
- $componentAccessToken = weixinOpenUtil::getComponentAccessToken($open);
- $url = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={$appId}&code={$code}&grant_type=authorization_code&component_appid={$component_appid}&component_access_token={$componentAccessToken}";
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $data = Json::decode($result);
- if (isset($data['openid']) == false) {
- Yii::warning('error:' . json_encode($data) . ' code:' . $code, __METHOD__);
- return false;
- }
- return $data;
- }
-
- /**
- * 授权获取自己的信息
- */
- public function authGetUserInfo($openid, $access_token)
- {
- $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid";
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $data = Json::decode($result);
- return $data;
- }
-
- /**
- * 自动获取自己的信息(公众号的粉丝才能获取)
- */
- public static function autoGetUserInfo($openid, $merchant)
- {
- $access_token = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $data = Json::decode($result);
- return $data;
- }
-
- /**
- * 网址接入验证
- */
- public static function signatureValidation()
- {
- $signature = $_GET['signature'];
- $timestamp = $_GET['timestamp'];
- $nonce = $_GET['nonce'];
- $account = $_GET['account'];
- $merchant = xhMerchantService::getByAccount($account);
- if ($merchant) {
- $token = $merchant['wxToken'];
- // 3个数值组成数组验证
- $tmpArr = array(
- $token,
- $timestamp,
- $nonce
- );
- // 字典排序
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode($tmpArr);
- // sha1加密
- $tmpStr = sha1($tmpStr);
- if ($signature == $tmpStr) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
-
- /**
- * 客服接口发消息
- * @param unknown_type $content
- * @param unknown_type $appId
- * @param unknown_type $openId
- * @return unknown
- */
- public static function sendMsg($content, $merchant, $openId)
- {
- $token = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
- $data = ['touser' => $openId, 'msgtype' => 'text', 'text' => ['content' => $content]];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- public static function sendImgMsg($mediaId, $merchant, $openId)
- {
- $token = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
- $data = ['touser' => $openId, 'msgtype' => 'image', 'image' => ['media_id' => $mediaId]];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- public static function sendVideoMsg($mediaId, $merchant, $openId)
- {
- $token = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
- $data = ['touser' => $openId, 'msgtype' => 'video', 'video' => ['media_id' => $mediaId,
- 'thumb_media_id' => $mediaId,
- 'title' => '因爱而生',
- 'description' => "情定原生,爱在今生;\n坐标:浙江,绍兴上虞;\n看到过一句话:“一个城市不过是几条巷道上,几间房子和几个人的组合。如果没有了这些,一个城市如同陨落,只剩下悲凉的记忆;”\n万事万物持续流变,珍爱的东西尤其不可能一直存在,如朝霞,如春花,如爱情;\n无论什么时候,不管天气好坏,都能拿出某些个让人喜欢的东西。",
- ]];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 消息模板,发送任务通知
- * @param unknown $data
- * @return mixed
- */
- public static function sendTaskInform($data, $merchant)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$accessToken}";
- $data = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
- $curl = new curl\Curl();
- $response = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $arr = json_decode($response);
- return $arr;
- }
-
- /**
- * 创建模板消息
- * @param unknown $accessToken
- * @param unknown $template_id_short
- * @return unknown
- */
- public static function tMessageCreate($merchant, $shortTemplateId)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token={$accessToken}";
- $post = array('template_id_short' => $shortTemplateId);
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 删除模板消息
- * @param unknown $accessToken
- * @param unknown $templateId
- * @return unknown
- */
- public function delTMessage($merchant, $templateId)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token={$accessToken}";
- $post = array('template_id' => $templateId);
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 取所有模板消息
- * @param unknown_type $appId
- * @return unknown
- */
- public function getAllTMessage($merchant)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token={$accessToken}";
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 设置模板消息的行业
- * @param unknown $accessToken
- * @param unknown $templateId
- * @return unknown
- */
- public function setTMIndustry($merchant, $industry_id1, $industry_id2)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token={$accessToken}";
- $post = array('industry_id1' => $industry_id1, 'industry_id2' => $industry_id2);
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 获取公众号的accessToken 公众号调用凭据
- * @param unknown $appId
- * @return unknown|string
- */
- public static function getAuthorizerAccessToken($merchant)
- {
- $merchantId = $merchant['id'];
- $id = configDict::getConfig('openId');
- $open = xhWxOpenService::getById($id);
- $component_appid = $open['appId'];
- $componentAccessToken = weixinOpenUtil::getComponentAccessToken($open);
- $appId = $merchant['wxAppId'];
- $wxAccessToken = $merchant['wxAccessToken'];
- $wxAccessTokenTime = $merchant['wxAccessTokenTime'];
- $authorizer_refresh_token = $merchant['wxRefreshToken'];
- $now = time();
- if ($now > strtotime($wxAccessTokenTime)) {
- $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
- $data = ['component_appid' => $component_appid, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $authorizer_refresh_token];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- if (isset($result['authorizer_access_token'])) {
- $authorizer_access_token = $result['authorizer_access_token'];
- $expires_in = $result['expires_in'];
- $authorizer_refresh_token = $result['authorizer_refresh_token'];
- $uData['wxRefreshToken'] = $authorizer_refresh_token;;
- $uData['wxAccessToken'] = $authorizer_access_token;
- $uData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
- xhMerchantService::updateById($merchantId, $uData);
- return $authorizer_access_token;
- }
- return;
- } else {
- return $wxAccessToken;
- }
- }
-
- /**
- * 获取小程序的accessToken
- */
- public static function getMiniProgramAccessToken($merchant)
- {
- $merchantId = $merchant['id'];
- $id = configDict::getConfig('openId');
- $open = xhWxOpenService::getById($id);
- $component_appid = $open['appId'];
- $componentAccessToken = weixinOpenUtil::getComponentAccessToken($open);
- $appId = $merchant['miniAppId'];
- $extend = xhMerchantExtendService::getByMerchantId($merchantId);
- $miniAccessToken = $extend['miniAccessToken'];
- $miniAccessTokenTime = $extend['miniAccessTokenTime'];
- $miniRefreshToken = $extend['miniRefreshToken'];
- $now = time();
- if ($now > strtotime($miniAccessTokenTime)) {
- $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
- $data = ['component_appid' => $component_appid, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $miniRefreshToken];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- Yii::info('重新获取mini accesstoken:' . json_encode($result));
- if (isset($result['authorizer_access_token'])) {
- $expires_in = $result['expires_in'];
- $miniAccessToken = $result['authorizer_access_token'];
- $uData['miniRefreshToken'] = $result['authorizer_refresh_token'];
- $uData['miniAccessToken'] = $miniAccessToken;
- $uData['miniAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
- xhMerchantExtendService::updateByMerchantId($merchantId, $uData);
- return $miniAccessToken;
- }
- Yii::info('accessToken失效:merchantId:' . $merchantId);
- }
- return $miniAccessToken;
- }
-
- public static function saveWeixinImg($userId, $url, $class = 0)
- {
- $className = [
- '0' => 'weixinUserAvatar',//微信头像
- '1' => 'weixinUserImg',//微信公众号二维码
- '2' => 'weixinMpQrcode',
- '3' => 'weixinMpAvatar',
- ];
- $folder = Yii::getAlias('@webroot') . '/../../images';
- $pre = '/' . $className[$class] . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
- if (!file_exists($folder . $pre))
- mkdir($folder . $pre, 0777, true);
- $randString = stringUtil::buildOrderNo();
- $name = md5($userId . $randString) . '.jpg';
- $fullFileName = $folder . $pre . $name;
- $fileName = $pre . $name;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $fp2 = @fopen($fullFileName, 'a');//文件大小
- fwrite($fp2, $result);
- fclose($fp2);
- return $fileName;
- }
-
- /**
- * 下载学员作品图片
- */
- public static function downloadStudentWorksImg($merchant, $mediaId, $savePathType = 'wxUserImg')
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $body = curl_exec($ch);
- $httpinfo = curl_getinfo($ch);
- curl_close($ch);
- preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
- $fileExt = $extmatches[1];
- $imgSavePath = configDict::getConfig('imgSavePath');
- $comImgSavePath = $imgSavePath[$savePathType];
- $folder = Yii::getAlias('@webroot') . '/../../images';
- $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
- if (!file_exists($folder . $pre)) {
- mkdir($folder . $pre, 0777, true);
- }
- $randString = stringUtil::buildOrderNo();
- $fullFileName = $folder . $pre . $randString . '.jpg';
- $fileName = $pre . $randString . '.jpg';
- $fp2 = @fopen($fullFileName, 'a');//文件大小
- fwrite($fp2, $body);
- fclose($fp2);
- $srcImg = $fullFileName;
-
- $dstImg300 = $folder . $pre . $randString . "_300.jpg";//生成300px缩略图,不保存到数据库
- util::img2thumb($srcImg, $dstImg300, $width = 300, 0, 0, 0);
-
- $dstImg100 = $folder . $pre . $randString . "_100.jpg";//生成50px缩略图,不保存到数据库
- util::img2thumb($srcImg, $dstImg100, $width = 100, 0, 0, 0);
- return $fileName;
- }
-
- /**
- * 获取media_id里的图片保存到服务器
- */
- public static function downloadmediaIdImg($merchant, $mediaId, $savePathType = 'wxUserImg')
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $body = curl_exec($ch);
- $httpinfo = curl_getinfo($ch);
- curl_close($ch);
- preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
- $fileExt = $extmatches[1];
- $imgSavePath = configDict::getConfig('imgSavePath');
- $comImgSavePath = $imgSavePath[$savePathType];
- $folder = Yii::getAlias('@webroot') . '/../../images';
- $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
- if (!file_exists($folder . $pre)) {
- mkdir($folder . $pre, 0777, true);
- }
- $randString = stringUtil::buildOrderNo();
- $fullFileName = $folder . $pre . $randString . '.jpg';
- $fileName = $pre . $randString . '.jpg';
- $fp2 = @fopen($fullFileName, 'a');//文件大小
- fwrite($fp2, $body);
- fclose($fp2);
- $srcImg = $fullFileName;
-
- $mediumImg = $pre . $randString . "_200.jpg";
- $dstImg200 = $folder . $mediumImg;//生成200px缩略图,不保存到数据库
- util::img2thumb($srcImg, $dstImg200, $width = 200, 0, 0, 0);
-
- $smallImg = $pre . $randString . "_50.jpg";
- $dstImg50 = $folder . $smallImg;//生成50px缩略图,不保存到数据库
- util::img2thumb($srcImg, $dstImg50, $width = 50, 0, 0, 0);
- return ['large' => $fileName, 'medium' => $mediumImg, 'small' => $smallImg];
- }
-
- public static function createTag($merchant, $name)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token={$accessToken}";
- $post = ['tag' => ['name' => $name]];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- public static function delTag($merchant, $id)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={$accessToken}";
- $post = ['tag' => ['id' => $id]];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- public static function modTag($merchant, $id, $name)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token={$accessToken}";
- $post = ['tag' => ['id' => $id, 'name' => $name]];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 获取商家所有标签
- */
- public static function getTag($merchant)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token={$accessToken}";
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $result = Json::decode($result);
- return $result;
- }
-
- public function getUserTag($tagId, $nextOpenId = '')
- {
- $url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=" . $this->access_token;
- $post = ['tagid' => $tagId, 'next_openid' => $nextOpenId];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 批量为用户打标签
- */
- public static function membersBatchTag($merchant, $openIdList, $tagId)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$accessToken}";
- $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 批量取消用户标签
- * @param unknown_type $appId
- * @param unknown_type $openIdList
- * @param unknown_type $tagId
- * @return unknown
- */
- public static function membersCancelTag($merchant, $openIdList, $tagId)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token={$accessToken}";
- $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 新增临时素材
- * @param unknown_type $appId
- * @return unknown
- */
- public static function uploadTempMedia($merchant, $imgUrl, $type = 'image')
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$accessToken}&type=" . $type;
- $postData = ['media' => '@' . $imgUrl];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $response = curl_exec($ch);
- $result = Json::decode($response);
- return $result;
- }
-
- /**
- * 获取临时素材
- * @param unknown_type $appId
- * @param unknown_type $mediaId
- */
- public static function getTempMaterial($merchant, $mediaId)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant);
- $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id=" . $mediaId;
- $fileInfo = self::getTempMaterialInfo($url);
- if (empty($fileInfo) || isset($fileInfo['errcode'])) {
- $errCodes = $fileInfo['errcode'];
- $errCodesList = self::$errCodes;
- $msg = isset($errCodesList[$errCodes]) ? $errCodesList[$errCodes] : '未错误哦~';
- return ['code' => 'A0002', 'msg' => $msg];
- }
- $fileName = substr(md5($mediaId), -4) . stringUtil::buildOrderNo() . '.amr';
- $preFolder = Yii::getAlias('@webroot') . '/../../images';
- $folder = '/weixinChatVoice/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
- if (!file_exists($preFolder . $folder))
- mkdir($preFolder . $folder, 0777, true);
- $saveFile = $preFolder . $folder . $fileName;
- if (file_exists($saveFile) == false) {
- self::readTempMaterial($saveFile, $fileInfo["body"]);
- }
- return ['code' => 'A0001', 'msg' => 'success', 'data' => ['url' => $folder . $fileName]];
- }
-
- /**
- * 获取临时素材的头信息等
- * @param unknown_type $url
- * @return unknown
- */
- public static function getTempMaterialInfo($url)
- {
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_NOBODY, 0);//只取body头
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $package = curl_exec($ch);
- $httpinfo = curl_getinfo($ch);
- curl_close($ch);
- $imageAll = ['header' => $httpinfo, 'body' => $package];
- return $imageAll;
- }
-
- /**
- * 读取临时素材的资源
- * @param unknown_type $filename
- * @param unknown_type $fileContent
- */
- function readTempMaterial($filename, $fileContent)
- {
- $localFile = fopen($filename, 'w');
- if (false !== $localFile) {
- if (false !== fwrite($localFile, $fileContent)) {
- fclose($localFile);
- }
- }
- }
-
- /**
- * 创建门店
- * @param $merchant
- * @param $menu
- * @return mixed
- */
- public function createStore($info)
- {
- $jsonData = urldecode(json_encode($info, JSON_UNESCAPED_UNICODE));
- $url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
- }
-
- /**
- * 查询门店信息
- * @param $poi_id
- */
- public function getStoreInfo($poi_id)
- {
- $data = ['poi_id' => $poi_id];
- $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
- $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoi?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
- }
-
- /**
- * 查询门店列表
- * @param int $begin 开始数据
- * @param int $count 返回数据条数,最大允许50,默认为20
- */
- public function getStoreList($begin = 0, $count = 20)
- {
- $data = ["begin" => $begin, "limit" => $count];
- $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
- $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
- }
-
- /**
- * 查询门店列表 -- 重复 可删除
- * @param $data
- * @return mixed
- */
- function getShopList($data)
- {
- $url = "https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 修改门店服务信息
- */
- public function updateStore(array $buffer)
- {
- $jsonData = urldecode(json_encode($buffer, JSON_UNESCAPED_UNICODE));
- $url = "http://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
- }
-
- /**
- * 删除门店
- * @param $poi_id
- * @return mixed
- */
- public function delStore($poi_id)
- {
- $data = ['poi_id' => $poi_id];
- $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
- $url = "http://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
- }
-
- /**
- * 门店类目表
- */
- public function storeTypeList()
- {
- $url = "http://api.weixin.qq.com/cgi-bin/poi/getwxcategory?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- return $curl->get($url);
- }
-
- /******************************* wifi *************************************/
- /**
- * 获取wifi门店列表
- * @param $pageindex 开始页
- * @param $pagesize 每页数据条数(最大20)
- * @return mixed
- */
- function getShopWifiList($pageindex = 1, $pagesize = 10)
- {
- $data = ["pageindex" => $pageindex, "pagesize" => $pagesize];
- $url = "https://api.weixin.qq.com/bizwifi/shop/list?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 添加密码型设备
- * @param $shopId 门店ID
- * @param $ssid 无线网络设备的ssid
- * @param $pwd 无线网络设备的密码
- * @return mixed
- */
- function addPasswordDevice($shopId, $ssid, $pwd)
- {
- $data = ["shop_id" => $shopId, "ssid" => $ssid, "password" => $pwd];
- $url = "https://api.weixin.qq.com/bizwifi/device/add?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 修改门店Wi-Fi(修改门店网络信息)
- * @param $shopId
- * @param $oldSsid
- * @param $ssid
- * @param string $pwd
- * @return mixed
- */
- function updateDevice($shopId, $oldSsid, $ssid, $pwd = '')
- {
- $data = ["shop_id" => $shopId, "old_ssid" => $oldSsid, "ssid" => $ssid];
- if (!empty($pwd)) {
- $data["password"] = $pwd;
- }
- $url = "https://api.weixin.qq.com/bizwifi/shop/update?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 删除密码型设备
- * @param $bssid 无线网络设备无线mac地址
- * @return mixed
- */
- function delPasswordDevice($bssid)
- {
- $data = ["bssid" => $bssid];
- $url = "https://api.weixin.qq.com/bizwifi/device/delete?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 查询门店Wi-Fi信息
- * @param $shopId 门店ID
- * @return mixed
- */
- function getShopWifi($shopId)
- {
- $data = ["shop_id" => $shopId];
- $url = "https://api.weixin.qq.com/bizwifi/shop/get?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- /**
- * 获取物料二维码
- * @param $shopId 门店ID
- * @param $ssid 无线网络设备的ssid
- * @param $img_id 物料样式编号(0-纯二维码,可用于自由设计宣传材料;1-二维码物料,155mm×215mm(宽×高),可直接张贴)
- * @return mixed
- */
- function getWifiQrCode($shopId, $ssid, $img_id)
- {
- //$data = ["shop_id" => 429620, "ssid" => "WX567", "img_id" => $img_id];
- $data = ["shop_id" => $shopId, "ssid" => $ssid, "img_id" => $img_id];
- $url = "https://api.weixin.qq.com/bizwifi/qrcode/get?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
-
- function getWifiUrl()
- {
- $url = "https://api.weixin.qq.com/bizwifi/account/get_connecturl?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $result = Json::decode($result);
- return $result;
- }
-
- function generateShortUrl($longUrl)
- {
- $data = ["action" => "long2short", "long_url" => $longUrl,];
- $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $this->access_token;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- if (isset($result['errcode']) && $result['errcode'] == 0) {
- return ['code' => 'A0001', 'data' => ['shortUrl' => $result['short_url']]];
- }
- return ['code' => 'A0002', 'data' => []];
- }
-
- //长链接转短链接 shish 2019.9.26
- public static function urlLongToShort($longUrl, $merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $data = ["action" => "long2short", "long_url" => $longUrl,];
- $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- $url = '';
- if (isset($result['errcode']) && $result['errcode'] == 0) {
- $url = $result['short_url'];
- }
- return ['url' => $url];
- }
- /**
- * 拉取门店所有的设备信息
- * @return array
- */
- public function getShopWifiListAll()
- {
- $startPage = 1;
- $list = [];
- while (true) {
- $reList = $this->getShopWifiList($startPage, 20);
- if ($startPage <= $reList['data']['pagecount']) {
- $list = array_merge($list, $reList['data']['records']);
- ++$startPage;
- } else {
- break;
- }
- }
-
- return $list;
- }
-
- /**
- * 通过 poi_id 从Wi-Fi门店列表中 获取 shopId
- * @param $poiId
- * @return bool
- */
- public function getShopId($poiId, $list = [])
- {
- if (empty($list)) {
- $list = $this->getShopWifiListAll();
- }
- foreach ($list as $shop) {
- if ($shop['poi_id'] == $poiId)
- return (string)$shop['shop_id'];
- }
-
- return false;
- }
-
- //设置小程序服务器域名
- public static function setDomain($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $accessToken;
- $curl = new curl\Curl();
- if ($merchant['merchantName'] == '花美灵') {
- $data = [
- "action" => "set",
- "requestdomain" => ['https://mini.huaml.com'],
- "wsrequestdomain" => ['https://mini.huaml.com'],
- "uploaddomain" => ['https://mini.huaml.com'],
- "downloaddomain" => ['https://mini.huaml.com'],
- ];
- } else {
- $data = [
- "action" => "set",
- "requestdomain" => ['https://mini.huahb.com'],
- "wsrequestdomain" => ['https://mini.huahb.com'],
- "uploaddomain" => ['https://mini.huahb.com'],
- "downloaddomain" => ['https://mini.huahb.com'],
- ];
- }
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $respond = Json::decode($result);
- print_r($respond);
- }
-
- //设置业务域名
- public static function setWebViewDomain($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $accessToken;
- $curl = new curl\Curl();
- if ($merchant['merchantName'] == '花美灵') {
- $data = [
- "action" => "set",
- "webviewdomain" => ['https://mini.huaml.com'],
- ];
- } else {
- $data = [
- "action" => "set",
- "webviewdomain" => ['https://mini.huahb.com'],
- ];
- }
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $respond = Json::decode($result);
- print_r($respond);
- }
-
- //为授权的小程序帐号上传小程序代码
- public static function miniCommit($merchant, $templateId)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = 'https://api.weixin.qq.com/wxa/commit?access_token=' . $accessToken;
- $curl = new curl\Curl();
-
- $ext = [
- 'extAppid' => $merchant['miniAppId'],
- 'ext' => [
- 'account' => $merchant['id'],
- 'host' => Yii::$app->params['miniUrl'],
- 'miniUrl' => Yii::$app->params['miniUrl'],
- 'imgUrl' => Yii::$app->params['imgUrl'],
- 'test' => Yii::$app->params['imgUrl'],
- 'merchantName' => $merchant['merchantName'],
- ],
-
- 'tabBar' => [
- 'custom' => true,
- 'color' => '#7A7E83',
- 'selectedColor' => '#3cc51f',
- 'borderStyle' => 'black',
- 'backgroundColor' => '#ffffff',
- 'list' => [
- [
- 'pagePath' => 'pages/home/index',
- 'iconPath' => 'images/tab/home.png',
- 'selectedIconPath' => 'images/tab/green/home-active.png',
- 'text' => '首页'
- ],
- [
- 'pagePath' => 'pages/goods/category',
- 'iconPath' => 'images/tab/order.png',
- 'selectedIconPath' => 'images/tab/green/order-active.png',
- 'text' => '分类'
- ],
- [
- 'pagePath' => 'pages/goods/cart',
- 'iconPath' => 'images/tab/cart.png',
- 'selectedIconPath' => 'images/tab/green/cart-active.png',
- 'text' => '购物车'
- ],
- [
- 'pagePath' => 'pages/customer/my',
- 'iconPath' => 'images/tab/user.png',
- 'selectedIconPath' => 'images/tab/green/user-active.png',
- 'text' => "我的"
- ]
- ]
- ],
- ];
-
- Yii::info("ext:" . json_encode($ext));
-
- $extJson = json_encode($ext);
-
- $data = [
- "template_id" => $templateId,
- "ext_json" => $extJson,
- "user_version" => date("mdHis"),
- "user_desc" => date("mdHis")
- ];
-
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $respond = Json::decode($result);
- print_r($respond);
- }
-
- //获取体验小程序的体验二维码
- public static function getExperienceQrCode($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $path = urlencode("pages/home/index");
- $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token={$accessToken}&path=" . $path;
- $curl = new curl\Curl();
- $result = $curl->get($url);
-
- $file = util::getRootDir() . '/resource/images/miniExperience/' . $merchant['id'] . '.jpg';
-
- file_put_contents($file, $result);
- echo "<img src='" . Yii::$app->params['imgUrl'] . "/miniExperience/" . $merchant['id'] . ".jpg' />";
- }
-
- //获取授权小程序帐号已设置的类目
- public static function getMiniCategory($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = "https://api.weixin.qq.com/wxa/get_category?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $respond = Json::decode($result);
- echo "<pre>";
- print_r($respond);
- }
-
- //获取小程序的第三方提交代码的页面配置
- public static function getMiniPage($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = "https://api.weixin.qq.com/wxa/get_page?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $respond = Json::decode($result);
- echo "<pre>";
- print_r($respond);
- }
-
- //将第三方提交的代码包提交审核
- public static function miniSubmitAudit($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $data = [
- "item_list" => [
- [
- "address" => "pages/home/index",
- "tag" => "鲜花 花店",
- "first_class" => "商家自营",
- "second_class" => "鲜花/园艺/工艺品",
- "first_id" => 304,
- "second_id" => 323,
- "title" => "商城",
- ],
- ],
- "feedback_info" => "",
- "feedback_stuff" => "",
- ];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $respond = Json::decode($result);
- echo "<pre>";
- print_r($respond);
- }
-
- //查询最新一次提交的审核状态
- public static function miniGetLatestAuditStatus($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $respond = Json::decode($result);
- echo "<pre>";
- print_r($respond);
- }
-
-
- //发布已通过审核的小程序
- public static function miniRelease($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = "https://api.weixin.qq.com/wxa/release?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
- $respond = Json::decode($result);
- echo "<pre>";
- print_r($respond);
- }
-
- //撤回审核
- public static function rollbackCheck($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $respond = Json::decode($result);
- echo "<pre>";
- print_r($respond);
- }
-
- //生成申请会员页的小程序码
- public static function generateMemberCode($merchant)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant);
- //$path = urlencode("pages/home/index");
- $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
- $curl = new curl\Curl();
- $data = [
- "path" => 'pages/customer/my',
- "width" => 1000,
- ];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $file = util::getRootDir() . '/resource/images/miniExperience/' . $merchant['id'] . '_member.jpg';
- file_put_contents($file, $result);
- echo "<img src='" . Yii::$app->params['imgUrl'] . "/miniExperience/" . $merchant['id'] . "_member.jpg' />";
- }
-
- }
|