| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964 |
- <?php
- namespace common\components;
- use bizHd\wx\services\WxBaseService;
- use bizHd\wx\services\WxMiniBaseService;
- use common\services\xhMerchantExtendService;
- use Yii;
- use yii\helpers\Json;
- use linslin\yii2\curl;
- use common\services\xhWxOpenService;
- use common\services\xhMerchantService;
- /**
- * 小程序开发文楼
- */
- class miniUtil
- {
- public $access_token;
- public function __construct($merchant = '', $ptStyle = 1)
- {
- $this->access_token = !empty($merchant) ? self::getMiniProgramAccessToken($merchant, $ptStyle) : '';
- }
- //获取用户基本信息
- public static function userInfo($openId, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
- $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'])) {
- } else {
- return $result;
- }
- }
- //创建开放平台帐号
- public static function createOpenAccount($appId, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
- $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, $ptStyle = 0)
- {
- if ($isMiniProgram) {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- } else {
- $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
- }
- $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, $ptStyle = 0)
- {
- if ($isMiniProgram) {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- } else {
- $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
- }
- $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;
- }
- //解绑公众号绑定的平台 ssh 2019.9.15
- public static function unbindAccount($merchant, $ptStyle = 0)
- {
- $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
- $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;
- }
- //解绑小程序绑定的平台 ssh 2019.9.15
- public static function unbindMiniProgram($merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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 = '', $ptStyle = 1)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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;
- }
- //查询所有分组
- 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;
- }
- }
- /**
- * 提交自定义菜单到微信平台
- */
- 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;
- }
- /**
- * 上传图片多媒体文件
- */
- public static function uploadImgMedia($merchant, $fullFile)
- {
- $access_token = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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::getMiniProgramAccessToken($merchant, $ptStyle);
- $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);
- }
- /**
- * 下载多媒体文件
- */
- 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, $ptStyle)
- {
- $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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);
- }
- /**
- * 解除绑定删除自定义菜单
- */
- public static function menuDelete($merchant, $ptStyle)
- {
- $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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结构
- 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
- $parts [] = $str;
- }
- }
- $json = implode(',', $parts);
- if ($is_list)
- return '[' . $json . ']'; //Return numerical JSON
- return '{' . $json . '}'; //Return associative JSON
- }
- /**
- * 创建临时二维码 ssh 2019.9.4
- */
- public static function qrcodeCreate($merchant, $sceneId, $expireSeconds = null, $ptStyle = 1)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle = 1)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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;
- }
- /**
- * 微信授权获取code
- */
- public static function getCode($urlEncode, $merchant, $scope, $ptStyle)
- {
- $host = Yii::$app->request->getHostInfo();
- $REDIRECT_URI = $host . '/auth/get-user-info?url=' . $urlEncode . '&authScope=' . $scope;
- Yii::info($REDIRECT_URI);
- $appId = $merchant['wxAppId'];
- $open = [];
- if ($ptStyle == 1) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
- }
- if ($ptStyle == 2) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
- }
- if ($ptStyle == 3) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
- }
- if (empty($open)) {
- util::fail('没有找到平台');
- }
- $component_app_id = $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_app_id}#wechat_redirect";
- header("Location:" . $url);
- //如果这里没有exit(),在apache服务器下无法实现跳转
- exit();
- }
- /**
- * 获取openId
- */
- public static function getOpenId($code, $merchant, $ptStyle)
- {
- $appId = $merchant['wxAppId'];
- $open = [];
- if ($ptStyle == 1) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
- }
- if ($ptStyle == 2) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
- }
- if ($ptStyle == 3) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
- }
- if (empty($open)) {
- util::fail('没有找到平台');
- }
- $component_app_id = $open['appId'];
- $componentAccessToken = wxUtil::getComponentAccessToken($open);
- $url = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={$appId}&code={$code}&grant_type=authorization_code&component_appid={$component_app_id}&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, $ptStyle)
- {
- $access_token = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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;
- }
- }
- //客服接口发消息
- public static function sendMsg($content, $merchant, $openId, $ptStyle)
- {
- $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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;
- }
- /**
- * 删除模板消息
- */
- public static function delTMessage($merchant, $templateId, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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;
- }
- /**
- * 取所有模板消息
- */
- public function getAllTMessage($merchant, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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;
- }
- /**
- * 设置模板消息的行业
- */
- public function setTMIndustry($merchant, $industry_id1, $industry_id2, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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 公众号调用凭据
- */
- public static function getAuthorizerAccessToken($merchant, $ptStyle = 0)
- {
- $sjId = $merchant['id'];
- $open = [];
- if ($ptStyle == 1) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
- }
- if ($ptStyle == 2) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
- }
- if ($ptStyle == 3) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
- }
- if (empty($open)) {
- util::fail('没有找到平台');
- }
- $component_app_id = $open['appId'];
- $componentAccessToken = wxUtil::getComponentAccessToken($open);
- $appId = $merchant['wxAppId'];
- $wxAccessToken = $merchant['wxAccessToken'];
- $wxAccessTokenTime = $merchant['wxAccessTokenTime'];
- $authorize_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_app_id, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $authorize_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'])) {
- $authorize_access_token = $result['authorizer_access_token'];
- $expires_in = $result['expires_in'];
- $authorize_refresh_token = $result['authorizer_refresh_token'];
- $uData['wxRefreshToken'] = $authorize_refresh_token;;
- $uData['wxAccessToken'] = $authorize_access_token;
- $uData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
- if ($ptStyle == 1) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
- $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
- if (!empty($wxBaseId)) {
- WxBaseService::updateById($wxBaseId, $uData);
- }
- } elseif ($ptStyle == 2) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
- $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
- if (!empty($wxBaseId)) {
- WxBaseService::updateById($wxBaseId, $uData);
- }
- } elseif ($ptStyle == 3) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
- $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
- if (!empty($wxBaseId)) {
- WxBaseService::updateById($wxBaseId, $uData);
- }
- } else {
- xhMerchantService::updateById($sjId, $uData);
- }
- return $authorize_access_token;
- }
- return;
- } else {
- return $wxAccessToken;
- }
- }
- /**
- * 获取小程序的accessToken
- */
- public static function getMiniProgramAccessToken($merchant, $ptStyle = 0)
- {
- $sjId = isset($merchant['id']) ? $merchant['id'] : 0;
- //$id = dict::getDict('openId');
- $open = [];
- if ($ptStyle == 1) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
- }
- if ($ptStyle == 2) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
- }
- if ($ptStyle == 3) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
- }
- if (empty($open)) {
- util::fail('没有找到平台信息');
- }
- $component_app_id = $open['appId'];
- $componentAccessToken = wxUtil::getComponentAccessToken($open);
- $appId = $merchant['miniAppId'];
- if ($ptStyle == 1) {
- $miniAccessToken = $merchant['miniAccessToken'];
- $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
- $miniRefreshToken = $merchant['miniRefreshToken'];
- } elseif ($ptStyle == 2) {
- $miniAccessToken = $merchant['miniAccessToken'];
- $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
- $miniRefreshToken = $merchant['miniRefreshToken'];
- } elseif ($ptStyle == 3) {
- $miniAccessToken = $merchant['miniAccessToken'];
- $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
- $miniRefreshToken = $merchant['miniRefreshToken'];
- } else {
- $extend = xhMerchantExtendService::getBySjId($sjId);
- $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_app_id, '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));
- if ($ptStyle == 1) {
- //零售
- $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
- if (!empty($wxMiniBaseId)) {
- WxMiniBaseService::updateById($wxMiniBaseId, $uData);
- }
- } elseif ($ptStyle == 2) {
- //供货商
- $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
- if (!empty($wxMiniBaseId)) {
- WxMiniBaseService::updateById($wxMiniBaseId, $uData);
- }
- } elseif ($ptStyle == 3) {
- //商城
- $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
- if (!empty($wxMiniBaseId)) {
- WxMiniBaseService::updateById($wxMiniBaseId, $uData);
- }
- } else {
- //其它
- xhMerchantExtendService::updateBySjId($sjId, $uData);
- }
- return $miniAccessToken;
- }
- Yii::info('accessToken失效:sjId:' . $sjId);
- }
- 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', $ptStyle = 1)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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 = dict::getDict('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', $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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 = dict::getDict('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, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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;
- }
- /**
- * 批量取消用户标签
- */
- public static function membersCancelTag($merchant, $openIdList, $tagId, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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;
- }
- /**
- * 新增临时素材
- */
- public static function uploadTempMedia($merchant, $fullPathImg, $ptStyle, $type = 'image')
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$accessToken}&type=" . $type;
- $ch = curl_init();
- if (class_exists('CURLFile')) {// 这里用特性检测判断php版本
- curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
- $postData = array('media' => new \CURLFile(realpath($fullPathImg)));//>=5.5
- } else {
- if (defined('CURLOPT_SAFE_UPLOAD')) {
- curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
- $postData = array('media' => '@' . realpath($fullPathImg));//<=5.5
- }
- }
- 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;
- }
- /**
- * 获取临时素材
- */
- public static function getTempMaterial($merchant, $mediaId, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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'])) {
- $msg = '未错误哦~';
- 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]];
- }
- /**
- * 获取临时素材的头信息等
- */
- 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;
- }
- /**
- * 读取临时素材的资源
- */
- function readTempMaterial($filename, $fileContent)
- {
- $localFile = fopen($filename, 'w');
- if (false !== $localFile) {
- if (false !== fwrite($localFile, $fileContent)) {
- fclose($localFile);
- }
- }
- }
- /**
- * 创建门店
- */
- 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);
- }
- /**
- * 查询门店信息
- */
- 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);
- }
- /**
- * 查询门店列表
- */
- 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);
- }
- /**
- * 查询门店列表 -- 重复 可删除
- */
- 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门店列表
- */
- 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' => []];
- }
- //长链接转短链接 ssh 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, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $mallHost = Yii::$app->params['mallHost'];
- if ($ptStyle == 1) {
- $mallHost = Yii::$app->params['hdHost'];
- }
- $has = strpos($mallHost, 'https');
- //如果不是https则换成https
- if ($has === false) {
- $mallHost = str_replace('http', 'https', $mallHost);
- }
- $data = [
- "action" => "set",
- "requestdomain" => [$mallHost],
- "wsrequestdomain" => [$mallHost],
- "uploaddomain" => [$mallHost],
- "downloaddomain" => [$mallHost],
- ];
- print_r($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $respond = Json::decode($result);
- print_r($respond);
- }
- //设置业务域名
- public static function setWebViewDomain($merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $mallHost = Yii::$app->params['mallHost'];
- if ($ptStyle == 1) {
- $mallHost = Yii::$app->params['hdHost'];
- }
- $has = strpos($mallHost, 'https');
- //如果不是https则换成https
- if ($has === false) {
- $mallHost = str_replace('http', 'https', $mallHost);
- }
- $data = [
- "action" => "set",
- "webviewdomain" => [$mallHost],
- ];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $respond = Json::decode($result);
- print_r($respond);
- }
- //为授权的小程序帐号上传小程序代码
- public static function miniCommit($merchant, $templateId, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/wxa/commit?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $apiUrl = Yii::$app->params['mallHost'];
- if ($ptStyle == 1) {
- $apiUrl = Yii::$app->params['hdHost'];
- }
- $imgUrl = imgUtil::getPrefix();
- //如果不是https则换成https
- $has = strpos($apiUrl, 'https');
- if ($has === false) {
- $apiUrl = str_replace('http', 'https', $apiUrl);
- }
- //如果不是https则换成https
- $has = strpos($imgUrl, 'https');
- if ($has === false) {
- $imgUrl = str_replace('http', 'https', $imgUrl);
- }
- $account = isset($merchant['id']) ? $merchant['id'] : 0;
- $name = isset($merchant['name']) ? $merchant['name'] : '';
- $open = [];
- if ($ptStyle == 1) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
- }
- if ($ptStyle == 2) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
- }
- if ($ptStyle == 3) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
- }
- if (empty($open)) {
- util::fail('没有找到平台');
- }
- $name = isset($open['name']) ? $open['name'] : '';
- $ext = [
- 'extAppid' => $merchant['miniAppId'],
- 'ext' => [
- 'account' => $account,
- 'apiHost' => $apiUrl,
- 'socketApiHost' => str_replace("https://", "", $apiUrl),
- 'imgHost' => $imgUrl,
- 'name' => $name,
- ],
- ];
- echo '<pre>';
- print_r($ext);
- 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")
- ];
- print_r($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $respond = Json::decode($result);
- print_r($respond);
- }
- //获取体验小程序的体验二维码
- public static function getExperienceQrCode($merchant, $page, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $path = urlencode($page);
- $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token={$accessToken}&path=" . $path;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $file = dirUtil::getImgDir() . 'retail/miniExperience/' . $merchant['id'] . '.jpg';
- file_put_contents($file, $result);
- echo "<b /><img src='" . imgUtil::getPrefix() . "/retail/miniExperience/" . $merchant['id'] . ".jpg' />";
- }
- //获取授权小程序帐号已设置的类目
- public static function getMiniCategory($merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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);
- }
- //小程序支付成功后获取唯一ID ssh 2020.5.12
- public static function getPaidUnionId($merchant, $miniOpenId, $mcId, $orderId, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/wxa/getpaidunionid?access_token={$accessToken}&openid={$miniOpenId}&mch_id={$mcId}&out_trade_no=" . (string)$orderId;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $respond = Json::decode($result);
- $unionId = isset($respond['unionid']) && !empty($respond['unionid']) ? $respond['unionid'] : '';
- return $unionId;
- }
- //小程序加急审核 ssh 2020.4.20
- public static function speedupAudit($merchant, $id, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/wxa/speedupaudit?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $data = [
- "auditid" => $id
- ];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $respond = Json::decode($result);
- print_r($respond);
- }
- //获取小程序的第三方提交代码的页面配置
- public static function getMiniPage($merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $page, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $accessToken;
- $curl = new curl\Curl();
- if ($ptStyle == 1) {
- $data = [
- "item_list" => [
- [
- "address" => $page,
- "tag" => "花卉宝 花店",
- "first_class" => "IT科技",
- "second_class" => "软件服务提供商",
- "first_id" => 210,
- "second_id" => 413,
- "title" => "花卉宝",
- ],
- ],
- "feedback_info" => "",
- "feedback_stuff" => "",
- ];
- } else {
- $data = [
- "item_list" => [
- [
- "address" => $page,
- "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, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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);
- return $respond;
- }
- //撤回审核
- public static function rollbackCheck($merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $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, $shopId, $ptStyle = 0)
- {
- $fileName = $merchant['id'] . '_' . $shopId . '_apply_member.jpg';
- $behind = 'retail/mimiMemberCode/';
- $filePath = dirUtil::getImgDir() . $behind;
- if (!file_exists($filePath)) {
- mkdir($filePath, 0777, true);
- }
- $file = $filePath . $fileName;
- if (file_exists($file)) {
- return $behind . $fileName;
- }
- $path = 'pages/home/user?scan=1&shopId=' . $shopId;
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
- $curl = new curl\Curl();
- $data = [
- "path" => $path,
- "width" => 1000,
- ];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- file_put_contents($file, $result);
- return $behind . $fileName;
- }
- public static function generateShortLink($merchant, $pageUrl, $pageTitle, $ptStyle)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/wxa/genwxashortlink?access_token={$accessToken}";
- $curl = new curl\Curl();
- $data = [
- "page_url" => $pageUrl,
- "page_title" => $pageTitle,
- 'is_permanent' => false,
- ];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
- //小程序码生成,永久有效,数量暂无限制 ssh 2020.4.17
- public static function generateUnlimitedMiniCode($merchant, $page, $scene = '', $ptStyle = 0)
- {
- $id = isset($merchant['id']) ? $merchant['id'] : 0;
- $fileName = md5($page . '_' . $scene) . '.jpg';
- $behind = 'retail/mimiCode/' . date("Ym") . '/' . date("d") . '/' . $id . '/';
- $filePath = dirUtil::getImgDir() . $behind;
- if (!file_exists($filePath)) {
- mkdir($filePath, 0777, true);
- }
- $file = $filePath . $fileName;
- if (file_exists($file)) {
- return $behind . $fileName;
- }
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
- $curl = new curl\Curl();
- $data = [];
- if (!empty($page)) {
- $data["page"] = $page;
- }
- $data["scene"] = $scene;
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- Yii::info('获取小程序图片资源:' . json_encode($result));
- file_put_contents($file, $result);
- //上传oss
- $obj = $behind . $fileName;
- $obj = ltrim($obj, "/");
- oss::uploadImage($obj, $file);
- return $behind . $fileName;
- }
- //生成充值码,永久有效,数量暂无限制 ssh 20250307
- public static function generateUnlimitedRechargeMiniCode($merchant, $page, $scene = '', $ptStyle = 0)
- {
- $id = isset($merchant['id']) ? $merchant['id'] : 0;
- $fileName = md5($page . '_' . $scene) . '.jpg';
- $behind = 'retail/rechargeCode/' . date("Ym") . '/' . date("d") . '/' . $id . '/';
- $filePath = dirUtil::getImgDir() . $behind;
- if (!file_exists($filePath)) {
- mkdir($filePath, 0777, true);
- }
- $file = $filePath . $fileName;
- if (file_exists($file)) {
- return $behind . $fileName;
- }
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
- $curl = new curl\Curl();
- $data = [];
- if (!empty($page)) {
- $data["page"] = $page;
- }
- $data["scene"] = $scene;
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- Yii::info('获取小程序图片资源:' . json_encode($result));
- file_put_contents($file, $result);
- //上传oss
- $obj = $behind . $fileName;
- $obj = ltrim($obj, "/");
- oss::uploadImage($obj, $file);
- return $behind . $fileName;
- }
- //微信开放平台获取component_access_token
- public static function getComponentAccessToken($open)
- {
- $id = $open['id'];
- $componentAccessToken = $open['componentAccessToken'];
- $componentAccessTokenTime = $open['componentAccessTokenTime'];
- $now = time();
- if ($now > strtotime($componentAccessTokenTime)) {
- $appId = $open['appId'];
- $appSecret = $open['appSecret'];
- $verifyTicket = $open['verifyTicket'];
- $data = ['component_appid' => $appId, 'component_appsecret' => $appSecret, 'component_verify_ticket' => $verifyTicket];
- $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
- $curl = new curl\Curl();
- $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- if (empty($result)) {
- util::stop('没有取到ComponentAccessToken');
- }
- $result = Json::decode($result);
- if (isset($result['component_access_token']) == false || empty($result['component_access_token'])) {
- Yii::info('get component access token error:' . json_encode($result));
- Yii::$app->end();
- }
- $component_access_token = $result['component_access_token'];
- $expiresIn = $result['expires_in'];
- $oData = [];
- $oData['componentAccessToken'] = $component_access_token;
- $oData['componentAccessTokenTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短
- xhWxOpenService::updateById($id, $oData);
- return $component_access_token;
- } else {
- return $componentAccessToken;
- }
- }
- /**
- * 微信开放平台获取预授权码pre_auth_code
- */
- public static function getPreAuthCode($open)
- {
- $id = $open['id'];
- $preAuthCode = $open['preAuthCode'];
- $preAuthCodeTime = $open['preAuthCodeTime'];
- $now = time();
- if ($now > strtotime($preAuthCodeTime)) {
- $token = self::getComponentAccessToken($open);
- $appId = $open['appId'];
- $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token={$token}";
- $curl = new curl\Curl();
- $data = ['component_appid' => $appId];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- Yii::info($result);
- if (isset($result['pre_auth_code']) == false || empty($result['pre_auth_code'])) {
- echo "没有取到pre_auth_code,原因:" . json_encode($result) . " appid:" . $appId . " token:" . $token;
- Yii::$app->end();
- }
- $preAuthCode = $result['pre_auth_code'];
- $expiresIn = $result['expires_in'];
- $oData = [];
- $oData['preAuthCode'] = $preAuthCode;
- $oData['preAuthCodeTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短100秒
- xhWxOpenService::updateById($id, $oData);
- return $preAuthCode;
- } else {
- return $preAuthCode;
- }
- }
- /**
- * 微信开放平台使用授权码换取公众号的接口调用凭据和授权信息
- * authorizer_access_token authorizer_refresh_token
- */
- public static function getAuthorizer($code, $ptStyle)
- {
- $open = [];
- if ($ptStyle == 1) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
- }
- if ($ptStyle == 2) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
- }
- if ($ptStyle == 3) {
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
- }
- if (empty($open)) {
- util::fail('没有找到平台');
- }
- $appId = $open['appId'];
- $accessToken = self::getComponentAccessToken($open);
- $url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={$accessToken}";
- $curl = new curl\Curl();
- $data = ['authorization_code' => $code, 'component_appid' => $appId];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
- //微信开放平台获取授权方的公众号帐号基本信息 ssh 2020.3.11
- public static function getAuthorizerInfo($authorizeAppId, $open)
- {
- $appId = $open['appId'];
- $accessToken = self::getComponentAccessToken($open);
- $url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token={$accessToken}";
- $curl = new curl\Curl();
- $data = ['component_appid' => $appId, 'authorizer_appid' => $authorizeAppId];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- $result = Json::decode($result);
- return $result;
- }
- /**************************即时配送相关接口 START *****************************************/
- //开通即时配送权限
- public static function openLocalExpress($merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = "https://api.weixin.qq.com/cgi-bin/express/local/business/open?access_token=" . $accessToken;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $respond = Json::decode($result);
- echo "<pre>";
- print_r($respond);
- }
- //异常件退回商家商家确认收货接口
- //场景:当订单配送异常,骑手把货物退还给商家,商家收货以后调用本接口返回确认收货
- /*
- * $data
- * {
- "shopid": "123456",
- "shop_order_id": "123456",
- "shop_no": "shop_no_111"
- "waybill_id": "123456",
- "remark": "remark",
- "delivery_sign": "123456"
- }
- */
- public static function abnormalConfirm($data, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/confirm_return?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = Json::encode($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //即时配送,拉取已绑定账号 ssh 2021.2.1
- public static function getBindAccount($merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/shop/get?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //获取已支持的配送公司列表接口 lqh 2021.2.2
- public static function getAllImmeDelivery($merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/delivery/getall?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //下配送单接口 lqh 2021.2.2
- //场景:
- /*
- * 商家可调用本接口向配送公司请求下配送单,配送公司会返回这一单的配送单号、配送费、预计骑手接单时间等信息。
- 如遇下单错误,请先确认一下编码方式,python建议 json.dumps(b, ensure_ascii=False),php建议 json_encode($arr, JSON_UNESCAPED_UNICODE) 可预约时间:达达:72小时内,闪送2小时以后,48小时以内
- {
- "resultcode": 0,
- "resultmsg": "ok",
- "fee": 10, 实际运费(单位:元),运费减去优惠券费用
- "deliverfee": 10, 运费(单位:元)
- "couponfee": 0, 优惠券费用(单位:元)
- "tips": 0, 小费(单位:元)
- "insurancfee": 0, 保价费(单位:元)
- "distance": 1000, 配送距离(整数单位:米)
- "waybill_id": "123456789", 配送单号
- "order_status": 101, 配送状态。
- "finish_code": 1024, 收货码
- "pickup_code": 2048, 取货码
- "dispatch_duration": 300 预计骑手接单时间,单位秒,比如5分钟,就填300, 无法预计填0
- }
- */
- public static function addOrder($data, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/add?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = Json::encode($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //可以对待接单状态的订单增加小费。需要注意:订单的小费,以最新一次加小费动作的金额为准,故下一次增加小费额必须大于上一次小费额
- /*
- * {
- "shopid": "123456",
- "shop_order_id": "123456",
- "waybill_id": "123456",
- "tips": 5,
- "remark": "gogogo",
- "delivery_sign": "123456",
- "shop_no": "shop_no_111"
- }
- */
- public static function addTip($data, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/addtips?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = Json::encode($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //取消配送单接口 lqh 2021.2.3
- public static function cancelOrder($data, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/cancel?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = Json::encode($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //拉取配送单信息 lqh 2021.2.3
- public static function getOrder($data, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/get?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = Json::encode($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //模拟配送公司更新配送单状态, 该接口只用于沙盒环境,即订单并没有真实流转到运力方
- public static function mockUpdateOrder($data, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/test_update_order?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = Json::encode($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //预下配送单接口
- //在用户提交外卖订单时,商家可调用本接口查询配送公司是否可接单、预计多久接单、运费预估等。
- //预估运费可作为展示给用户的运费参考值。
- public static function preAddOrder($data, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/pre_add?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = Json::encode($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //预取消配送单接口
- //在正式取消配送单前,商家可调用本接口查询该订单是否可以取消,取消订单配送公司需要扣除的费用是多少
- public static function preCancelOrder($data, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/precancel?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = Json::encode($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- //重新下单
- //在调用下配送单接口、订单被取消、过期或者投递异常的情况下,可调用此接口,重新下单,需要保持orderid一致。
- //备注:美团不支持重新下单接口,如果订单被取消商家需要重新下单,请修改orderid之后,调用下配送单接口下单。
- public static function reOrder($data, $merchant, $ptStyle = 0)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/readd?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = Json::encode($data);
- $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
- $respond = Json::decode($result);
- return $respond;
- }
- /**************************即时配送相关接口 END *****************************************/
- //发送订阅消息通知 ssh 2023511
- public static function commonSendSubscribeMessage($merchant, $ptStyle, $msgId, $miniOpenId, $page, $msg)
- {
- $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
- $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $accessToken;
- $curl = new curl\Curl();
- $data = ['template_id' => $msgId, 'page' => $page, 'touser' => $miniOpenId, 'data' => $msg];
- $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
- Yii::info('订阅消息通知返回:' . $result);
- $result = Json::decode($result);
- return $result;
- }
- }
|