wxUtil.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. <?php
  2. namespace common\components;
  3. use biz\wx\classes\WxOpenClass;
  4. use biz\wx\services\WxBaseService;
  5. use biz\wx\services\WxMiniBaseService;
  6. use biz\wx\services\WxOpenService;
  7. use common\services\xhMerchantExtendService;
  8. use Yii;
  9. use yii\helpers\Json;
  10. use linslin\yii2\curl;
  11. use common\services\xhWxOpenService;
  12. use common\services\xhMerchantService;
  13. /**
  14. * 微信和微信开放平台交互接口
  15. */
  16. class wxUtil
  17. {
  18. public $access_token;
  19. public function __construct($merchant = '')
  20. {
  21. $this->access_token = !empty($merchant) ? self::getAuthorizerAccessToken($merchant) : '';
  22. }
  23. //获取用户基本信息
  24. public static function userInfo($openId, $merchant, $isOpen = 0)
  25. {
  26. $accessToken = self::getAuthorizerAccessToken($merchant, $isOpen);
  27. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$accessToken&openid=$openId";
  28. $curl = new curl\Curl();
  29. $result = $curl->get($url);
  30. $result = Json::decode($result);
  31. if (isset($result['errcode']) == false) {
  32. $result['nickName'] = isset($result['nickname']) ? $result['nickname'] : '';
  33. return $result;
  34. }
  35. return [];
  36. }
  37. //创建开放平台帐号
  38. public static function createOpenAccount($appId, $merchant, $isOpen = 0)
  39. {
  40. $accessToken = self::getAuthorizerAccessToken($merchant, $isOpen);
  41. $url = "https://api.weixin.qq.com/cgi-bin/open/create?access_token=" . $accessToken;
  42. $curl = new curl\Curl();
  43. $data = ['appid' => $appId];
  44. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  45. $result = Json::decode($result);
  46. return $result;
  47. }
  48. //绑定到开放平台
  49. public static function bindOpenAccount($appId, $openAppId, $merchant, $isMiniProgram, $isOpen = 0)
  50. {
  51. if ($isMiniProgram) {
  52. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  53. } else {
  54. $accessToken = self::getAuthorizerAccessToken($merchant, $isOpen);
  55. }
  56. $url = 'https://api.weixin.qq.com/cgi-bin/open/bind?access_token=' . $accessToken;
  57. $curl = new curl\Curl();
  58. $data = ['appid' => $appId, 'open_appid' => $openAppId];
  59. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  60. $result = Json::decode($result);
  61. return $result;
  62. }
  63. //获取公众号、小程序绑定的开放平台帐号
  64. public static function getOpenAccount($appId, $merchant, $isMiniProgram, $isOpen = 0)
  65. {
  66. if ($isMiniProgram) {
  67. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  68. } else {
  69. $accessToken = self::getAuthorizerAccessToken($merchant, $isOpen);
  70. }
  71. $url = "https://api.weixin.qq.com/cgi-bin/open/get?access_token=" . $accessToken;
  72. $curl = new curl\Curl();
  73. $data = ['appid' => $appId];
  74. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  75. $result = Json::decode($result);
  76. return $result;
  77. }
  78. //解绑公众号绑定的平台 shish 2019.9.15
  79. public static function unbindAccount($merchant, $isOpen = 0)
  80. {
  81. $accessToken = self::getAuthorizerAccessToken($merchant, $isOpen);
  82. $url = "https://api.weixin.qq.com/cgi-bin/open/unbind?access_token=" . $accessToken;
  83. $curl = new curl\Curl();
  84. $openAppId = $merchant['openAppId'];
  85. $appId = $merchant['wxAppId'];
  86. $data = ['appid' => $appId, 'open_appid' => $openAppId];
  87. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  88. $result = Json::decode($result);
  89. return $result;
  90. }
  91. //解绑小程序绑定的平台 shish 2019.9.15
  92. public static function unbindMiniProgram($merchant, $isOpen = 0)
  93. {
  94. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  95. $url = "https://api.weixin.qq.com/cgi-bin/open/unbind?access_token=" . $accessToken;
  96. $curl = new curl\Curl();
  97. $openAppId = $merchant['openAppId'];
  98. $appId = $merchant['miniAppId'];
  99. $data = ['appid' => $appId, 'open_appid' => $openAppId];
  100. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  101. $result = Json::decode($result);
  102. return $result;
  103. }
  104. public static function getSubscribeUserList($merchant, $nextOpenId = '')
  105. {
  106. $accessToken = self::getAuthorizerAccessToken($merchant);
  107. $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$accessToken&next_openid=$nextOpenId";
  108. $curl = new curl\Curl();
  109. $result = $curl->get($url);
  110. return Json::decode($result);
  111. }
  112. public static function batchGetUserInfo($merchant, $openIdList)
  113. {
  114. $accessToken = self::getAuthorizerAccessToken($merchant);
  115. $url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=" . $accessToken;
  116. $format = [];
  117. foreach ($openIdList as $val) {
  118. $format[] = ['openid' => $val, 'lang' => "zh-CN"];
  119. }
  120. $newOpenIdList = ["user_list" => $format];
  121. $curl = new curl\Curl();
  122. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($newOpenIdList))->post($url);
  123. $result = Json::decode($result);
  124. return $result;
  125. }
  126. //查询所有分组
  127. public static function groupList($accessToken)
  128. {
  129. $url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=$accessToken";
  130. $curl = new curl\Curl();
  131. $result = $curl->get($url);
  132. $result = Json::decode($result);
  133. if (isset($result['errcode'])) {
  134. return null;
  135. } else {
  136. return $result;
  137. }
  138. }
  139. /**
  140. * 提交自定义菜单到微信平台
  141. */
  142. public function userMenuToWeiXin($menu)
  143. {
  144. $jsonData = urldecode(json_encode($menu, JSON_UNESCAPED_UNICODE));
  145. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $this->access_token;
  146. $curl = new curl\Curl();
  147. $response = $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  148. return $response;
  149. }
  150. /**
  151. * 上传图片多媒体文件
  152. */
  153. public static function uploadImgMedia($merchant, $fullFile)
  154. {
  155. $access_token = self::getAuthorizerAccessToken($merchant);
  156. $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" . $access_token . "&type=image";
  157. $curl = curl_init();
  158. if (class_exists('\CURLFile')) {//关键是判断curlfile,官网推荐php5.5或更高的版本使用curlfile来实例文件
  159. $data = array('media' => new \CURLFile($fullFile));
  160. } else {
  161. $data = array('media' => file_get_contents($fullFile));
  162. }
  163. if (class_exists('\CURLFile')) {
  164. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
  165. } else {
  166. if (defined('CURLOPT_SAFE_UPLOAD')) {
  167. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
  168. }
  169. }
  170. curl_setopt($curl, CURLOPT_URL, $url);
  171. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  172. curl_setopt($curl, CURLOPT_POST, true);
  173. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  174. $result = curl_exec($curl);
  175. curl_close($curl);
  176. return json_decode($result);
  177. }
  178. /**
  179. * 上传视频文件
  180. * @param unknown_type
  181. * @param $merchant
  182. * @param $fullFile
  183. * @return mixed
  184. */
  185. public function uploadVideoMedia($merchant, $fullFile)
  186. {
  187. $access_token = self::getAuthorizerAccessToken($merchant);
  188. $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" . $access_token . "&type=video";
  189. $curl = curl_init();
  190. if (class_exists('\CURLFile')) {//关键是判断curlfile,官网推荐php5.5或更高的版本使用curlfile来实例文件
  191. $data = array('media' => new \CURLFile($fullFile));
  192. } else {
  193. $data = array('media' => file_get_contents($fullFile));
  194. }
  195. if (class_exists('\CURLFile')) {
  196. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
  197. } else {
  198. if (defined('CURLOPT_SAFE_UPLOAD')) {
  199. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
  200. }
  201. }
  202. curl_setopt($curl, CURLOPT_URL, $url);
  203. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  204. curl_setopt($curl, CURLOPT_POST, true);
  205. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  206. $result = curl_exec($curl);
  207. curl_close($curl);
  208. return json_decode($result);
  209. }
  210. /**
  211. * 下载多媒体文件
  212. * @param unknown_type $access_token
  213. * @param unknown_type $media_id
  214. */
  215. public static function uploadMedia($media_id, $access_token)
  216. {
  217. $url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $access_token . '&media_id=' . $media_id;
  218. $result = CurlUtil::httpsGet($url);
  219. if (isset($result['errcode'])) {
  220. return false;
  221. } else {
  222. return $result;
  223. }
  224. }
  225. /**
  226. * 获取素材总数
  227. * 图片和图文消息素材(包括单图文和多图文)的总数上限为5000,其他素材的总数上限为1000
  228. */
  229. public function getMaterialCount($merchant)
  230. {
  231. $token = self::getAuthorizerAccessToken($merchant);
  232. $url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token={$token}";
  233. $curl = new curl\Curl();
  234. $response = $curl->get($url);
  235. return $response;
  236. }
  237. /**
  238. * 获取素材列表 图文列表
  239. * $data = ['type' => 'image','offset' => 0,'count' => 100];
  240. * type:news image video voice
  241. */
  242. public function batchgetMaterial($merchant, $data)
  243. {
  244. $token = self::getAuthorizerAccessToken($merchant);
  245. $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={$token}";
  246. $curl = new curl\Curl();
  247. $response = $curl->setOption(CURLOPT_POSTFIELDS, json_encode($data))->post($url);
  248. return json_decode($response, true);
  249. }
  250. /**
  251. * 解除绑定删除自定义菜单
  252. * @param string $accessToken
  253. */
  254. public static function menuDelete($merchant)
  255. {
  256. $token = self::getAuthorizerAccessToken($merchant);
  257. $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=$token";
  258. $curl = new curl\Curl();
  259. $response = $curl->get($url);
  260. return $response;
  261. }
  262. //微信api不支持中文转义的json结构
  263. static function json_encode($arr)
  264. {
  265. if (count($arr) == 0) return "[]";
  266. $parts = array();
  267. $is_list = false;
  268. //Find out if the given array is a numerical array
  269. $keys = array_keys($arr);
  270. $max_length = count($arr) - 1;
  271. if (($keys [0] === 0) && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
  272. $is_list = true;
  273. for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
  274. if ($i != $keys [$i]) { //A key fails at position check.
  275. $is_list = false; //It is an associative array.
  276. break;
  277. }
  278. }
  279. }
  280. foreach ($arr as $key => $value) {
  281. if (is_array($value)) { //Custom handling for arrays
  282. if ($is_list)
  283. $parts [] = self::json_encode($value); /* :RECURSION: */
  284. else
  285. $parts [] = '"' . $key . '":' . self::json_encode($value); /* :RECURSION: */
  286. } else {
  287. $str = '';
  288. if (!$is_list)
  289. $str = '"' . $key . '":';
  290. //Custom handling for multiple data types
  291. if (!is_string($value) && is_numeric($value) && $value < 2000000000)
  292. $str .= $value; //Numbers
  293. elseif ($value === false)
  294. $str .= 'false'; //The booleans
  295. elseif ($value === true)
  296. $str .= 'true';
  297. else
  298. $str .= '"' . addslashes($value) . '"'; //All other things
  299. $parts [] = $str;
  300. }
  301. }
  302. $json = implode(',', $parts);
  303. if ($is_list)
  304. return '[' . $json . ']'; //Return numerical JSON
  305. return '{' . $json . '}'; //Return associative JSON
  306. }
  307. /**
  308. * 创建临时二维码 shish 2019.9.4
  309. */
  310. public static function qrcodeCreate($merchant, $sceneId, $expireSeconds = null)
  311. {
  312. $accessToken = self::getAuthorizerAccessToken($merchant);
  313. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$accessToken";
  314. //有效期默认一天
  315. $expireSeconds = isset($expireSeconds) ? $expireSeconds : 86400;
  316. $post = array('expire_seconds' => $expireSeconds, 'action_name' => 'QR_STR_SCENE', "action_info" => array('scene' => array('scene_str' => $sceneId)));
  317. $curl = new curl\Curl();
  318. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  319. $result = Json::decode($result);
  320. return $result;
  321. }
  322. /**
  323. * 创建永久二维码
  324. */
  325. public static function qrcodePermanentCreate($merchant, $sceneId)
  326. {
  327. $accessToken = self::getAuthorizerAccessToken($merchant);
  328. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$accessToken";
  329. $post = array('action_name' => 'QR_LIMIT_STR_SCENE', "action_info" => array('scene' => array('scene_str' => $sceneId)));
  330. $curl = new curl\Curl();
  331. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  332. $result = Json::decode($result);
  333. return $result;
  334. }
  335. /**
  336. * 微信授权获取code
  337. */
  338. public static function getCode($urlEncode, $merchant, $scope)
  339. {
  340. $host = Yii::$app->request->getHostInfo();
  341. $REDIRECT_URI = $host . '/auth/get-user-info?url=' . $urlEncode . '&authScope=' . $scope;
  342. Yii::info($REDIRECT_URI);
  343. $appId = $merchant['wxAppId'];
  344. $id = configDict::getConfig('openId');
  345. $open = xhWxOpenService::getById($id);
  346. $component_appid = $open['appId'];
  347. $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";
  348. header("Location:" . $url);
  349. //如果这里没有exit(),在apache服务器下无法实现跳转
  350. exit();
  351. }
  352. /**
  353. * 获取openId
  354. */
  355. public static function getOpenId($code, $merchant)
  356. {
  357. $appId = $merchant['wxAppId'];
  358. $id = configDict::getConfig('openId');
  359. $open = xhWxOpenService::getById($id);
  360. $component_appid = $open['appId'];
  361. $componentAccessToken = wxUtil::getComponentAccessToken($open);
  362. $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}";
  363. $curl = new curl\Curl();
  364. $result = $curl->get($url);
  365. $data = Json::decode($result);
  366. if (isset($data['openid']) == false) {
  367. Yii::warning('error:' . json_encode($data) . ' code:' . $code, __METHOD__);
  368. return false;
  369. }
  370. return $data;
  371. }
  372. /**
  373. * 授权获取自己的信息
  374. */
  375. public function authGetUserInfo($openid, $access_token)
  376. {
  377. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid";
  378. $curl = new curl\Curl();
  379. $result = $curl->get($url);
  380. $data = Json::decode($result);
  381. $data['nickName'] = isset($data['nickname']) ? $data['nickname'] : '';
  382. return $data;
  383. }
  384. /**
  385. * 自动获取自己的信息(公众号的粉丝才能获取)
  386. */
  387. public static function autoGetUserInfo($openid, $merchant)
  388. {
  389. $access_token = self::getAuthorizerAccessToken($merchant);
  390. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
  391. $curl = new curl\Curl();
  392. $result = $curl->get($url);
  393. $data = Json::decode($result);
  394. return $data;
  395. }
  396. /**
  397. * 网址接入验证
  398. */
  399. public static function signatureValidation()
  400. {
  401. $signature = $_GET['signature'];
  402. $timestamp = $_GET['timestamp'];
  403. $nonce = $_GET['nonce'];
  404. $account = $_GET['account'];
  405. $merchant = xhMerchantService::getByAccount($account);
  406. if ($merchant) {
  407. $token = $merchant['wxToken'];
  408. // 3个数值组成数组验证
  409. $tmpArr = array(
  410. $token,
  411. $timestamp,
  412. $nonce
  413. );
  414. // 字典排序
  415. sort($tmpArr, SORT_STRING);
  416. $tmpStr = implode($tmpArr);
  417. // sha1加密
  418. $tmpStr = sha1($tmpStr);
  419. if ($signature == $tmpStr) {
  420. return true;
  421. } else {
  422. return false;
  423. }
  424. } else {
  425. return false;
  426. }
  427. }
  428. //客服接口发消息
  429. public static function sendMsg($content, $merchant, $openId)
  430. {
  431. $token = self::getAuthorizerAccessToken($merchant);
  432. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  433. $data = ['touser' => $openId, 'msgtype' => 'text', 'text' => ['content' => $content]];
  434. $curl = new curl\Curl();
  435. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  436. $result = Json::decode($result);
  437. return $result;
  438. }
  439. public static function sendImgMsg($mediaId, $merchant, $openId)
  440. {
  441. $token = self::getAuthorizerAccessToken($merchant);
  442. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  443. $data = ['touser' => $openId, 'msgtype' => 'image', 'image' => ['media_id' => $mediaId]];
  444. $curl = new curl\Curl();
  445. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  446. $result = Json::decode($result);
  447. return $result;
  448. }
  449. public static function sendVideoMsg($mediaId, $merchant, $openId)
  450. {
  451. $token = self::getAuthorizerAccessToken($merchant);
  452. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  453. $data = ['touser' => $openId, 'msgtype' => 'video', 'video' => ['media_id' => $mediaId,
  454. 'thumb_media_id' => $mediaId,
  455. 'title' => '因爱而生',
  456. 'description' => "情定原生,爱在今生;\n坐标:浙江,绍兴上虞;\n看到过一句话:“一个城市不过是几条巷道上,几间房子和几个人的组合。如果没有了这些,一个城市如同陨落,只剩下悲凉的记忆;”\n万事万物持续流变,珍爱的东西尤其不可能一直存在,如朝霞,如春花,如爱情;\n无论什么时候,不管天气好坏,都能拿出某些个让人喜欢的东西。",
  457. ]];
  458. $curl = new curl\Curl();
  459. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  460. $result = Json::decode($result);
  461. return $result;
  462. }
  463. /**
  464. * 消息模板,发送任务通知
  465. * @param unknown $data
  466. * @return mixed
  467. */
  468. public static function sendTaskInform($data, $merchant, $isOpen = 0)
  469. {
  470. $accessToken = self::getAuthorizerAccessToken($merchant, $isOpen);
  471. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$accessToken}";
  472. $data = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  473. $curl = new curl\Curl();
  474. $response = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  475. $arr = json_decode($response);
  476. return $arr;
  477. }
  478. /**
  479. * 创建模板消息
  480. * @param unknown $accessToken
  481. * @param unknown $template_id_short
  482. * @return unknown
  483. */
  484. public static function tMessageCreate($merchant, $shortTemplateId, $isOpen = 0)
  485. {
  486. $accessToken = self::getAuthorizerAccessToken($merchant, $isOpen);
  487. $url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token={$accessToken}";
  488. $post = array('template_id_short' => $shortTemplateId);
  489. $curl = new curl\Curl();
  490. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  491. $result = Json::decode($result);
  492. return $result;
  493. }
  494. /**
  495. * 删除模板消息
  496. * @param unknown $accessToken
  497. * @param unknown $templateId
  498. * @return unknown
  499. */
  500. public static function delTMessage($merchant, $templateId)
  501. {
  502. $accessToken = self::getAuthorizerAccessToken($merchant);
  503. $url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token={$accessToken}";
  504. $post = array('template_id' => $templateId);
  505. $curl = new curl\Curl();
  506. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  507. $result = Json::decode($result);
  508. return $result;
  509. }
  510. /**
  511. * 取所有模板消息
  512. * @param unknown_type $appId
  513. * @return unknown
  514. */
  515. public function getAllTMessage($merchant)
  516. {
  517. $accessToken = self::getAuthorizerAccessToken($merchant);
  518. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token={$accessToken}";
  519. $curl = new curl\Curl();
  520. $result = $curl->get($url);
  521. $result = Json::decode($result);
  522. return $result;
  523. }
  524. /**
  525. * 设置模板消息的行业
  526. * @param unknown $accessToken
  527. * @param unknown $templateId
  528. * @return unknown
  529. */
  530. public function setTMIndustry($merchant, $industry_id1, $industry_id2)
  531. {
  532. $accessToken = self::getAuthorizerAccessToken($merchant);
  533. $url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token={$accessToken}";
  534. $post = array('industry_id1' => $industry_id1, 'industry_id2' => $industry_id2);
  535. $curl = new curl\Curl();
  536. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  537. $result = Json::decode($result);
  538. return $result;
  539. }
  540. /**
  541. * 获取公众号的accessToken 公众号调用凭据
  542. * @param unknown $appId
  543. * @return unknown|string
  544. */
  545. public static function getAuthorizerAccessToken($merchant, $isOpen = 0)
  546. {
  547. $merchantId = $merchant['id'];
  548. $id = configDict::getConfig('openId');
  549. $open = xhWxOpenService::getById($id);
  550. $component_appid = $open['appId'];
  551. $componentAccessToken = wxUtil::getComponentAccessToken($open);
  552. $appId = $merchant['wxAppId'];
  553. $wxAccessToken = $merchant['wxAccessToken'];
  554. $wxAccessTokenTime = $merchant['wxAccessTokenTime'];
  555. $authorizer_refresh_token = $merchant['wxRefreshToken'];
  556. $now = time();
  557. if ($now > strtotime($wxAccessTokenTime)) {
  558. $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
  559. $data = ['component_appid' => $component_appid, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $authorizer_refresh_token];
  560. $curl = new curl\Curl();
  561. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  562. $result = Json::decode($result);
  563. if (isset($result['authorizer_access_token'])) {
  564. $authorizer_access_token = $result['authorizer_access_token'];
  565. $expires_in = $result['expires_in'];
  566. $authorizer_refresh_token = $result['authorizer_refresh_token'];
  567. $uData['wxRefreshToken'] = $authorizer_refresh_token;;
  568. $uData['wxAccessToken'] = $authorizer_access_token;
  569. $uData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  570. if ($isOpen == 1) {
  571. $open = WxOpenService::getOpen();
  572. $wx_base_id = isset($open['wx_base_id']) ? $open['wx_base_id'] : 0;
  573. if (!empty($wx_base_id)) {
  574. WxBaseService::updateById($wx_base_id, $uData);
  575. }
  576. } else {
  577. xhMerchantService::updateById($merchantId, $uData);
  578. }
  579. return $authorizer_access_token;
  580. }
  581. return;
  582. } else {
  583. return $wxAccessToken;
  584. }
  585. }
  586. /**
  587. * 获取小程序的accessToken
  588. */
  589. public static function getMiniProgramAccessToken($merchant, $isOpen = 0)
  590. {
  591. $merchantId = isset($merchant['id']) ? $merchant['id'] : 0;
  592. $id = configDict::getConfig('openId');
  593. $open = xhWxOpenService::getById($id);
  594. $component_appid = $open['appId'];
  595. $componentAccessToken = wxUtil::getComponentAccessToken($open);
  596. $appId = $merchant['miniAppId'];
  597. if ($isOpen == 1) {
  598. $miniAccessToken = $merchant['miniAccessToken'];
  599. $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
  600. $miniRefreshToken = $merchant['miniRefreshToken'];
  601. } else {
  602. $extend = xhMerchantExtendService::getByMerchantId($merchantId);
  603. $miniAccessToken = $extend['miniAccessToken'];
  604. $miniAccessTokenTime = $extend['miniAccessTokenTime'];
  605. $miniRefreshToken = $extend['miniRefreshToken'];
  606. }
  607. $now = time();
  608. if ($now > strtotime($miniAccessTokenTime)) {
  609. $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
  610. $data = ['component_appid' => $component_appid, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $miniRefreshToken];
  611. $curl = new curl\Curl();
  612. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  613. $result = Json::decode($result);
  614. Yii::info('重新获取mini accesstoken:' . json_encode($result));
  615. if (isset($result['authorizer_access_token'])) {
  616. $expires_in = $result['expires_in'];
  617. $miniAccessToken = $result['authorizer_access_token'];
  618. $uData['miniRefreshToken'] = $result['authorizer_refresh_token'];
  619. $uData['miniAccessToken'] = $miniAccessToken;
  620. $uData['miniAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  621. if ($isOpen == 1) {
  622. $open = WxOpenService::getOpen();
  623. $wx_mini_base_id = isset($open['wx_mini_base_id']) ? $open['wx_mini_base_id'] : 0;
  624. if (!empty($wx_mini_base_id)) {
  625. WxMiniBaseService::updateById($wx_mini_base_id, $uData);
  626. }
  627. } else {
  628. xhMerchantExtendService::updateByMerchantId($merchantId, $uData);
  629. }
  630. return $miniAccessToken;
  631. }
  632. Yii::info('accessToken失效:merchantId:' . $merchantId);
  633. }
  634. return $miniAccessToken;
  635. }
  636. public static function saveWeixinImg($userId, $url, $class = 0)
  637. {
  638. $className = [
  639. '0' => 'weixinUserAvatar',//微信头像
  640. '1' => 'weixinUserImg',//微信公众号二维码
  641. '2' => 'weixinMpQrcode',
  642. '3' => 'weixinMpAvatar',
  643. ];
  644. $folder = Yii::getAlias('@webroot') . '/../../images';
  645. $pre = '/' . $className[$class] . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  646. if (!file_exists($folder . $pre))
  647. mkdir($folder . $pre, 0777, true);
  648. $randString = stringUtil::buildOrderNo();
  649. $name = md5($userId . $randString) . '.jpg';
  650. $fullFileName = $folder . $pre . $name;
  651. $fileName = $pre . $name;
  652. $curl = new curl\Curl();
  653. $result = $curl->get($url);
  654. $fp2 = @fopen($fullFileName, 'a');//文件大小
  655. fwrite($fp2, $result);
  656. fclose($fp2);
  657. return $fileName;
  658. }
  659. /**
  660. * 下载学员作品图片
  661. */
  662. public static function downloadStudentWorksImg($merchant, $mediaId, $savePathType = 'wxUserImg')
  663. {
  664. $accessToken = self::getAuthorizerAccessToken($merchant);
  665. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
  666. $ch = curl_init($url);
  667. curl_setopt($ch, CURLOPT_HEADER, 0);
  668. curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
  669. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  670. $body = curl_exec($ch);
  671. $httpinfo = curl_getinfo($ch);
  672. curl_close($ch);
  673. preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
  674. $fileExt = $extmatches[1];
  675. $imgSavePath = configDict::getConfig('imgSavePath');
  676. $comImgSavePath = $imgSavePath[$savePathType];
  677. $folder = Yii::getAlias('@webroot') . '/../../images';
  678. $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  679. if (!file_exists($folder . $pre)) {
  680. mkdir($folder . $pre, 0777, true);
  681. }
  682. $randString = stringUtil::buildOrderNo();
  683. $fullFileName = $folder . $pre . $randString . '.jpg';
  684. $fileName = $pre . $randString . '.jpg';
  685. $fp2 = @fopen($fullFileName, 'a');//文件大小
  686. fwrite($fp2, $body);
  687. fclose($fp2);
  688. $srcImg = $fullFileName;
  689. $dstImg300 = $folder . $pre . $randString . "_300.jpg";//生成300px缩略图,不保存到数据库
  690. util::img2thumb($srcImg, $dstImg300, $width = 300, 0, 0, 0);
  691. $dstImg100 = $folder . $pre . $randString . "_100.jpg";//生成50px缩略图,不保存到数据库
  692. util::img2thumb($srcImg, $dstImg100, $width = 100, 0, 0, 0);
  693. return $fileName;
  694. }
  695. /**
  696. * 获取media_id里的图片保存到服务器
  697. */
  698. public static function downloadmediaIdImg($merchant, $mediaId, $savePathType = 'wxUserImg')
  699. {
  700. $accessToken = self::getAuthorizerAccessToken($merchant);
  701. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
  702. $ch = curl_init($url);
  703. curl_setopt($ch, CURLOPT_HEADER, 0);
  704. curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
  705. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  706. $body = curl_exec($ch);
  707. $httpinfo = curl_getinfo($ch);
  708. curl_close($ch);
  709. preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
  710. $fileExt = $extmatches[1];
  711. $imgSavePath = configDict::getConfig('imgSavePath');
  712. $comImgSavePath = $imgSavePath[$savePathType];
  713. $folder = Yii::getAlias('@webroot') . '/../../images';
  714. $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  715. if (!file_exists($folder . $pre)) {
  716. mkdir($folder . $pre, 0777, true);
  717. }
  718. $randString = stringUtil::buildOrderNo();
  719. $fullFileName = $folder . $pre . $randString . '.jpg';
  720. $fileName = $pre . $randString . '.jpg';
  721. $fp2 = @fopen($fullFileName, 'a');//文件大小
  722. fwrite($fp2, $body);
  723. fclose($fp2);
  724. $srcImg = $fullFileName;
  725. $mediumImg = $pre . $randString . "_200.jpg";
  726. $dstImg200 = $folder . $mediumImg;//生成200px缩略图,不保存到数据库
  727. util::img2thumb($srcImg, $dstImg200, $width = 200, 0, 0, 0);
  728. $smallImg = $pre . $randString . "_50.jpg";
  729. $dstImg50 = $folder . $smallImg;//生成50px缩略图,不保存到数据库
  730. util::img2thumb($srcImg, $dstImg50, $width = 50, 0, 0, 0);
  731. return ['large' => $fileName, 'medium' => $mediumImg, 'small' => $smallImg];
  732. }
  733. public static function createTag($merchant, $name)
  734. {
  735. $accessToken = self::getAuthorizerAccessToken($merchant);
  736. $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token={$accessToken}";
  737. $post = ['tag' => ['name' => $name]];
  738. $curl = new curl\Curl();
  739. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  740. $result = Json::decode($result);
  741. return $result;
  742. }
  743. public static function delTag($merchant, $id)
  744. {
  745. $accessToken = self::getAuthorizerAccessToken($merchant);
  746. $url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={$accessToken}";
  747. $post = ['tag' => ['id' => $id]];
  748. $curl = new curl\Curl();
  749. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  750. $result = Json::decode($result);
  751. return $result;
  752. }
  753. public static function modTag($merchant, $id, $name)
  754. {
  755. $accessToken = self::getAuthorizerAccessToken($merchant);
  756. $url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token={$accessToken}";
  757. $post = ['tag' => ['id' => $id, 'name' => $name]];
  758. $curl = new curl\Curl();
  759. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  760. $result = Json::decode($result);
  761. return $result;
  762. }
  763. /**
  764. * 获取商家所有标签
  765. */
  766. public static function getTag($merchant)
  767. {
  768. $accessToken = self::getAuthorizerAccessToken($merchant);
  769. $url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token={$accessToken}";
  770. $curl = new curl\Curl();
  771. $result = $curl->get($url);
  772. $result = Json::decode($result);
  773. return $result;
  774. }
  775. public function getUserTag($tagId, $nextOpenId = '')
  776. {
  777. $url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=" . $this->access_token;
  778. $post = ['tagid' => $tagId, 'next_openid' => $nextOpenId];
  779. $curl = new curl\Curl();
  780. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  781. $result = Json::decode($result);
  782. return $result;
  783. }
  784. /**
  785. * 批量为用户打标签
  786. */
  787. public static function membersBatchTag($merchant, $openIdList, $tagId)
  788. {
  789. $accessToken = self::getAuthorizerAccessToken($merchant);
  790. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$accessToken}";
  791. $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
  792. $curl = new curl\Curl();
  793. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  794. $result = Json::decode($result);
  795. return $result;
  796. }
  797. /**
  798. * 批量取消用户标签
  799. * @param unknown_type $appId
  800. * @param unknown_type $openIdList
  801. * @param unknown_type $tagId
  802. * @return unknown
  803. */
  804. public static function membersCancelTag($merchant, $openIdList, $tagId)
  805. {
  806. $accessToken = self::getAuthorizerAccessToken($merchant);
  807. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token={$accessToken}";
  808. $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
  809. $curl = new curl\Curl();
  810. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  811. $result = Json::decode($result);
  812. return $result;
  813. }
  814. /**
  815. * 新增临时素材
  816. * @param unknown_type $appId
  817. * @return unknown
  818. */
  819. public static function uploadTempMedia($merchant, $imgUrl, $type = 'image')
  820. {
  821. $accessToken = self::getAuthorizerAccessToken($merchant);
  822. $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$accessToken}&type=" . $type;
  823. $postData = ['media' => '@' . $imgUrl];
  824. $ch = curl_init();
  825. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
  826. curl_setopt($ch, CURLOPT_URL, $url);
  827. curl_setopt($ch, CURLOPT_POST, 1);
  828. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  829. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  830. $response = curl_exec($ch);
  831. $result = Json::decode($response);
  832. return $result;
  833. }
  834. /**
  835. * 获取临时素材
  836. * @param unknown_type $appId
  837. * @param unknown_type $mediaId
  838. */
  839. public static function getTempMaterial($merchant, $mediaId)
  840. {
  841. $accessToken = self::getAuthorizerAccessToken($merchant);
  842. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id=" . $mediaId;
  843. $fileInfo = self::getTempMaterialInfo($url);
  844. if (empty($fileInfo) || isset($fileInfo['errcode'])) {
  845. $msg = '未错误哦~';
  846. return ['code' => 'A0002', 'msg' => $msg];
  847. }
  848. $fileName = substr(md5($mediaId), -4) . stringUtil::buildOrderNo() . '.amr';
  849. $preFolder = Yii::getAlias('@webroot') . '/../../images';
  850. $folder = '/weixinChatVoice/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  851. if (!file_exists($preFolder . $folder))
  852. mkdir($preFolder . $folder, 0777, true);
  853. $saveFile = $preFolder . $folder . $fileName;
  854. if (file_exists($saveFile) == false) {
  855. self::readTempMaterial($saveFile, $fileInfo["body"]);
  856. }
  857. return ['code' => 'A0001', 'msg' => 'success', 'data' => ['url' => $folder . $fileName]];
  858. }
  859. /**
  860. * 获取临时素材的头信息等
  861. * @param unknown_type $url
  862. * @return unknown
  863. */
  864. public static function getTempMaterialInfo($url)
  865. {
  866. $ch = curl_init($url);
  867. curl_setopt($ch, CURLOPT_HEADER, 0);
  868. curl_setopt($ch, CURLOPT_NOBODY, 0);//只取body头
  869. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  870. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  871. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  872. $package = curl_exec($ch);
  873. $httpinfo = curl_getinfo($ch);
  874. curl_close($ch);
  875. $imageAll = ['header' => $httpinfo, 'body' => $package];
  876. return $imageAll;
  877. }
  878. /**
  879. * 读取临时素材的资源
  880. * @param unknown_type $filename
  881. * @param unknown_type $fileContent
  882. */
  883. function readTempMaterial($filename, $fileContent)
  884. {
  885. $localFile = fopen($filename, 'w');
  886. if (false !== $localFile) {
  887. if (false !== fwrite($localFile, $fileContent)) {
  888. fclose($localFile);
  889. }
  890. }
  891. }
  892. /**
  893. * 创建门店
  894. * @param $merchant
  895. * @param $menu
  896. * @return mixed
  897. */
  898. public function createStore($info)
  899. {
  900. $jsonData = urldecode(json_encode($info, JSON_UNESCAPED_UNICODE));
  901. $url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token=" . $this->access_token;
  902. $curl = new curl\Curl();
  903. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  904. }
  905. /**
  906. * 查询门店信息
  907. * @param $poi_id
  908. */
  909. public function getStoreInfo($poi_id)
  910. {
  911. $data = ['poi_id' => $poi_id];
  912. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  913. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoi?access_token=" . $this->access_token;
  914. $curl = new curl\Curl();
  915. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  916. }
  917. /**
  918. * 查询门店列表
  919. * @param int $begin 开始数据
  920. * @param int $count 返回数据条数,最大允许50,默认为20
  921. */
  922. public function getStoreList($begin = 0, $count = 20)
  923. {
  924. $data = ["begin" => $begin, "limit" => $count];
  925. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  926. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
  927. $curl = new curl\Curl();
  928. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  929. }
  930. /**
  931. * 查询门店列表 -- 重复 可删除
  932. * @param $data
  933. * @return mixed
  934. */
  935. function getShopList($data)
  936. {
  937. $url = "https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
  938. $curl = new curl\Curl();
  939. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  940. $result = Json::decode($result);
  941. return $result;
  942. }
  943. /**
  944. * 修改门店服务信息
  945. */
  946. public function updateStore(array $buffer)
  947. {
  948. $jsonData = urldecode(json_encode($buffer, JSON_UNESCAPED_UNICODE));
  949. $url = "http://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token=" . $this->access_token;
  950. $curl = new curl\Curl();
  951. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  952. }
  953. /**
  954. * 删除门店
  955. * @param $poi_id
  956. * @return mixed
  957. */
  958. public function delStore($poi_id)
  959. {
  960. $data = ['poi_id' => $poi_id];
  961. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  962. $url = "http://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token=" . $this->access_token;
  963. $curl = new curl\Curl();
  964. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  965. }
  966. /**
  967. * 门店类目表
  968. */
  969. public function storeTypeList()
  970. {
  971. $url = "http://api.weixin.qq.com/cgi-bin/poi/getwxcategory?access_token=" . $this->access_token;
  972. $curl = new curl\Curl();
  973. return $curl->get($url);
  974. }
  975. /******************************* wifi *************************************/
  976. /**
  977. * 获取wifi门店列表
  978. * @param $pageindex 开始页
  979. * @param $pagesize 每页数据条数(最大20)
  980. * @return mixed
  981. */
  982. function getShopWifiList($pageindex = 1, $pagesize = 10)
  983. {
  984. $data = ["pageindex" => $pageindex, "pagesize" => $pagesize];
  985. $url = "https://api.weixin.qq.com/bizwifi/shop/list?access_token=" . $this->access_token;
  986. $curl = new curl\Curl();
  987. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  988. $result = Json::decode($result);
  989. return $result;
  990. }
  991. /**
  992. * 添加密码型设备
  993. * @param $shopId 门店ID
  994. * @param $ssid 无线网络设备的ssid
  995. * @param $pwd 无线网络设备的密码
  996. * @return mixed
  997. */
  998. function addPasswordDevice($shopId, $ssid, $pwd)
  999. {
  1000. $data = ["shop_id" => $shopId, "ssid" => $ssid, "password" => $pwd];
  1001. $url = "https://api.weixin.qq.com/bizwifi/device/add?access_token=" . $this->access_token;
  1002. $curl = new curl\Curl();
  1003. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1004. $result = Json::decode($result);
  1005. return $result;
  1006. }
  1007. /**
  1008. * 修改门店Wi-Fi(修改门店网络信息)
  1009. * @param $shopId
  1010. * @param $oldSsid
  1011. * @param $ssid
  1012. * @param string $pwd
  1013. * @return mixed
  1014. */
  1015. function updateDevice($shopId, $oldSsid, $ssid, $pwd = '')
  1016. {
  1017. $data = ["shop_id" => $shopId, "old_ssid" => $oldSsid, "ssid" => $ssid];
  1018. if (!empty($pwd)) {
  1019. $data["password"] = $pwd;
  1020. }
  1021. $url = "https://api.weixin.qq.com/bizwifi/shop/update?access_token=" . $this->access_token;
  1022. $curl = new curl\Curl();
  1023. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1024. $result = Json::decode($result);
  1025. return $result;
  1026. }
  1027. /**
  1028. * 删除密码型设备
  1029. * @param $bssid 无线网络设备无线mac地址
  1030. * @return mixed
  1031. */
  1032. function delPasswordDevice($bssid)
  1033. {
  1034. $data = ["bssid" => $bssid];
  1035. $url = "https://api.weixin.qq.com/bizwifi/device/delete?access_token=" . $this->access_token;
  1036. $curl = new curl\Curl();
  1037. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1038. $result = Json::decode($result);
  1039. return $result;
  1040. }
  1041. /**
  1042. * 查询门店Wi-Fi信息
  1043. * @param $shopId 门店ID
  1044. * @return mixed
  1045. */
  1046. function getShopWifi($shopId)
  1047. {
  1048. $data = ["shop_id" => $shopId];
  1049. $url = "https://api.weixin.qq.com/bizwifi/shop/get?access_token=" . $this->access_token;
  1050. $curl = new curl\Curl();
  1051. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1052. $result = Json::decode($result);
  1053. return $result;
  1054. }
  1055. /**
  1056. * 获取物料二维码
  1057. * @param $shopId 门店ID
  1058. * @param $ssid 无线网络设备的ssid
  1059. * @param $img_id 物料样式编号(0-纯二维码,可用于自由设计宣传材料;1-二维码物料,155mm×215mm(宽×高),可直接张贴)
  1060. * @return mixed
  1061. */
  1062. function getWifiQrCode($shopId, $ssid, $img_id)
  1063. {
  1064. //$data = ["shop_id" => 429620, "ssid" => "WX567", "img_id" => $img_id];
  1065. $data = ["shop_id" => $shopId, "ssid" => $ssid, "img_id" => $img_id];
  1066. $url = "https://api.weixin.qq.com/bizwifi/qrcode/get?access_token=" . $this->access_token;
  1067. $curl = new curl\Curl();
  1068. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1069. $result = Json::decode($result);
  1070. return $result;
  1071. }
  1072. function getWifiUrl()
  1073. {
  1074. $url = "https://api.weixin.qq.com/bizwifi/account/get_connecturl?access_token=" . $this->access_token;
  1075. $curl = new curl\Curl();
  1076. $result = $curl->get($url);
  1077. $result = Json::decode($result);
  1078. return $result;
  1079. }
  1080. function generateShortUrl($longUrl)
  1081. {
  1082. $data = ["action" => "long2short", "long_url" => $longUrl,];
  1083. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $this->access_token;
  1084. $curl = new curl\Curl();
  1085. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1086. $result = Json::decode($result);
  1087. if (isset($result['errcode']) && $result['errcode'] == 0) {
  1088. return ['code' => 'A0001', 'data' => ['shortUrl' => $result['short_url']]];
  1089. }
  1090. return ['code' => 'A0002', 'data' => []];
  1091. }
  1092. //长链接转短链接 shish 2019.9.26
  1093. public static function urlLongToShort($longUrl, $merchant)
  1094. {
  1095. $accessToken = self::getMiniProgramAccessToken($merchant);
  1096. $data = ["action" => "long2short", "long_url" => $longUrl,];
  1097. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $accessToken;
  1098. $curl = new curl\Curl();
  1099. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1100. $result = Json::decode($result);
  1101. $url = '';
  1102. if (isset($result['errcode']) && $result['errcode'] == 0) {
  1103. $url = $result['short_url'];
  1104. }
  1105. return ['url' => $url];
  1106. }
  1107. /**
  1108. * 拉取门店所有的设备信息
  1109. * @return array
  1110. */
  1111. public function getShopWifiListAll()
  1112. {
  1113. $startPage = 1;
  1114. $list = [];
  1115. while (true) {
  1116. $reList = $this->getShopWifiList($startPage, 20);
  1117. if ($startPage <= $reList['data']['pagecount']) {
  1118. $list = array_merge($list, $reList['data']['records']);
  1119. ++$startPage;
  1120. } else {
  1121. break;
  1122. }
  1123. }
  1124. return $list;
  1125. }
  1126. /**
  1127. * 通过 poi_id 从Wi-Fi门店列表中 获取 shopId
  1128. * @param $poiId
  1129. * @return bool
  1130. */
  1131. public function getShopId($poiId, $list = [])
  1132. {
  1133. if (empty($list)) {
  1134. $list = $this->getShopWifiListAll();
  1135. }
  1136. foreach ($list as $shop) {
  1137. if ($shop['poi_id'] == $poiId)
  1138. return (string)$shop['shop_id'];
  1139. }
  1140. return false;
  1141. }
  1142. //设置小程序服务器域名
  1143. public static function setDomain($merchant, $isOpen = 0)
  1144. {
  1145. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1146. $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $accessToken;
  1147. $curl = new curl\Curl();
  1148. $mallHost = Yii::$app->params['mallHost'];
  1149. if ($isOpen == 1) {
  1150. $mallHost = Yii::$app->params['shopHost'];
  1151. }
  1152. $has = strpos($mallHost, 'https');
  1153. //如果不是https则换成https
  1154. if ($has === false) {
  1155. $mallHost = str_replace('http', 'https', $mallHost);
  1156. }
  1157. $data = [
  1158. "action" => "set",
  1159. "requestdomain" => [$mallHost],
  1160. "wsrequestdomain" => [$mallHost],
  1161. "uploaddomain" => [$mallHost],
  1162. "downloaddomain" => [$mallHost],
  1163. ];
  1164. print_r($data);
  1165. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1166. $respond = Json::decode($result);
  1167. print_r($respond);
  1168. }
  1169. //设置业务域名
  1170. public static function setWebViewDomain($merchant, $isOpen = 0)
  1171. {
  1172. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1173. $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $accessToken;
  1174. $curl = new curl\Curl();
  1175. $mallHost = Yii::$app->params['mallHost'];
  1176. if ($isOpen == 1) {
  1177. $mallHost = Yii::$app->params['shopHost'];
  1178. }
  1179. $has = strpos($mallHost, 'https');
  1180. //如果不是https则换成https
  1181. if ($has === false) {
  1182. $mallHost = str_replace('http', 'https', $mallHost);
  1183. }
  1184. $data = [
  1185. "action" => "set",
  1186. "webviewdomain" => [$mallHost],
  1187. ];
  1188. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1189. $respond = Json::decode($result);
  1190. print_r($respond);
  1191. }
  1192. //为授权的小程序帐号上传小程序代码
  1193. public static function miniCommit($merchant, $templateId, $isOpen = 0)
  1194. {
  1195. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1196. $url = 'https://api.weixin.qq.com/wxa/commit?access_token=' . $accessToken;
  1197. $curl = new curl\Curl();
  1198. $apiUrl = Yii::$app->params['mallHost'];
  1199. if ($isOpen == 1) {
  1200. $apiUrl = Yii::$app->params['shopHost'];
  1201. }
  1202. $imgUrl = Yii::$app->params['imgHost'];
  1203. //如果不是https则换成https
  1204. $has = strpos($apiUrl, 'https');
  1205. if ($has === false) {
  1206. $apiUrl = str_replace('http', 'https', $apiUrl);
  1207. }
  1208. //如果不是https则换成https
  1209. $has = strpos($imgUrl, 'https');
  1210. if ($has === false) {
  1211. $imgUrl = str_replace('http', 'https', $imgUrl);
  1212. }
  1213. $account = isset($merchant['id']) ? $merchant['id'] : 0;
  1214. $name = isset($merchant['merchantName']) ? $merchant['merchantName'] : '';
  1215. if ($isOpen == 1) {
  1216. $open = WxOpenService::getOpen();
  1217. $name = isset($open['name']) ? $open['name'] : '';
  1218. }
  1219. $ext = [
  1220. 'extAppid' => $merchant['miniAppId'],
  1221. 'ext' => [
  1222. 'account' => $account,
  1223. 'apiHost' => $apiUrl,
  1224. 'imgHost' => $imgUrl,
  1225. 'merchantName' => $name,
  1226. ],
  1227. ];
  1228. echo '<pre>';
  1229. print_r($ext);
  1230. Yii::info("ext:" . json_encode($ext));
  1231. $extJson = json_encode($ext);
  1232. $data = [
  1233. "template_id" => $templateId,
  1234. "ext_json" => $extJson,
  1235. "user_version" => date("mdHis"),
  1236. "user_desc" => date("mdHis")
  1237. ];
  1238. print_r($data);
  1239. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1240. $respond = Json::decode($result);
  1241. print_r($respond);
  1242. }
  1243. //获取体验小程序的体验二维码
  1244. public static function getExperienceQrCode($merchant, $page, $isOpen = 0)
  1245. {
  1246. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1247. $path = urlencode($page);
  1248. $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token={$accessToken}&path=" . $path;
  1249. $curl = new curl\Curl();
  1250. $result = $curl->get($url);
  1251. $file = dirUtil::getImgDir() . '/retail/miniExperience/' . $merchant['id'] . '.jpg';
  1252. file_put_contents($file, $result);
  1253. echo "<b /><img src='" . Yii::$app->params['imgHost'] . "/retail/miniExperience/" . $merchant['id'] . ".jpg' />";
  1254. }
  1255. //获取授权小程序帐号已设置的类目
  1256. public static function getMiniCategory($merchant, $isOpen = 0)
  1257. {
  1258. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1259. $url = "https://api.weixin.qq.com/wxa/get_category?access_token=" . $accessToken;
  1260. $curl = new curl\Curl();
  1261. $result = $curl->get($url);
  1262. $respond = Json::decode($result);
  1263. echo "<pre>";
  1264. print_r($respond);
  1265. }
  1266. //小程序加急审核 shish 2020.4.20
  1267. public static function speedupAudit($merchant, $id, $isOpen = 0)
  1268. {
  1269. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1270. $url = "https://api.weixin.qq.com/wxa/speedupaudit?access_token=" . $accessToken;
  1271. $curl = new curl\Curl();
  1272. $data = [
  1273. "auditid" => $id
  1274. ];
  1275. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1276. $respond = Json::decode($result);
  1277. print_r($respond);
  1278. }
  1279. //获取小程序的第三方提交代码的页面配置
  1280. public static function getMiniPage($merchant, $isOpen = 0)
  1281. {
  1282. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1283. $url = "https://api.weixin.qq.com/wxa/get_page?access_token=" . $accessToken;
  1284. $curl = new curl\Curl();
  1285. $result = $curl->get($url);
  1286. $respond = Json::decode($result);
  1287. echo "<pre>";
  1288. print_r($respond);
  1289. }
  1290. //将第三方提交的代码包提交审核
  1291. public static function miniSubmitAudit($merchant, $page, $isOpen = 0)
  1292. {
  1293. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1294. $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $accessToken;
  1295. $curl = new curl\Curl();
  1296. if ($isOpen == 1) {
  1297. $data = [
  1298. "item_list" => [
  1299. [
  1300. "address" => $page,
  1301. "tag" => "花卉宝 花店",
  1302. "first_class" => "IT科技",
  1303. "second_class" => "软件服务提供商",
  1304. "first_id" => 210,
  1305. "second_id" => 413,
  1306. "title" => "花卉宝",
  1307. ],
  1308. ],
  1309. "feedback_info" => "",
  1310. "feedback_stuff" => "",
  1311. ];
  1312. } else {
  1313. $data = [
  1314. "item_list" => [
  1315. [
  1316. "address" => $page,
  1317. "tag" => "鲜花 花店",
  1318. "first_class" => "商家自营",
  1319. "second_class" => "鲜花/园艺/工艺品",
  1320. "first_id" => 304,
  1321. "second_id" => 323,
  1322. "title" => "商城",
  1323. ],
  1324. ],
  1325. "feedback_info" => "",
  1326. "feedback_stuff" => "",
  1327. ];
  1328. }
  1329. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1330. $respond = Json::decode($result);
  1331. echo "<pre>";
  1332. print_r($respond);
  1333. }
  1334. //查询最新一次提交的审核状态
  1335. public static function miniGetLatestAuditStatus($merchant, $isOpen = 0)
  1336. {
  1337. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1338. $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $accessToken;
  1339. $curl = new curl\Curl();
  1340. $result = $curl->get($url);
  1341. $respond = Json::decode($result);
  1342. echo "<pre>";
  1343. print_r($respond);
  1344. }
  1345. //发布已通过审核的小程序
  1346. public static function miniRelease($merchant, $isOpen = 0)
  1347. {
  1348. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1349. $url = "https://api.weixin.qq.com/wxa/release?access_token=" . $accessToken;
  1350. $curl = new curl\Curl();
  1351. $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
  1352. $respond = Json::decode($result);
  1353. return $respond;
  1354. }
  1355. //撤回审核
  1356. public static function rollbackCheck($merchant, $isOpen = 0)
  1357. {
  1358. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1359. $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=" . $accessToken;
  1360. $curl = new curl\Curl();
  1361. $result = $curl->get($url);
  1362. $respond = Json::decode($result);
  1363. echo "<pre>";
  1364. print_r($respond);
  1365. }
  1366. //申请会员的小程序码
  1367. public static function generateMemberCode($merchant, $shopId, $isOpen = 0)
  1368. {
  1369. $fileName = $merchant['id'] . '_' . $shopId . '_apply_member.jpg';
  1370. $behind = '/retail/mimiMemberCode/';
  1371. $filePath = dirUtil::getImgDir() . $behind;
  1372. if (!file_exists($filePath)) {
  1373. mkdir($filePath, 0777, true);
  1374. }
  1375. $file = $filePath . $fileName;
  1376. if (file_exists($file)) {
  1377. return $behind . $fileName;
  1378. }
  1379. $path = 'pages/home/user?scan=1&shopId=' . $shopId;
  1380. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1381. $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
  1382. $curl = new curl\Curl();
  1383. $data = [
  1384. "path" => $path,
  1385. "width" => 1000,
  1386. ];
  1387. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1388. file_put_contents($file, $result);
  1389. return $behind . $fileName;
  1390. }
  1391. //收款小程序码
  1392. public static function generateGatheringMiniCode($merchant, $shopId, $isOpen = 0)
  1393. {
  1394. $fileName = $merchant['id'] . '_' . $shopId . '_apply_member.jpg';
  1395. $behind = '/retail/gatheringMimiCode/';
  1396. $filePath = dirUtil::getImgDir() . $behind;
  1397. if (!file_exists($filePath)) {
  1398. mkdir($filePath, 0777, true);
  1399. }
  1400. $file = $filePath . $fileName;
  1401. if (file_exists($file)) {
  1402. return $behind . $fileName;
  1403. }
  1404. $path = 'pages/pay/index?scan=1&shopId=' . $shopId;
  1405. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1406. $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
  1407. $curl = new curl\Curl();
  1408. $data = [
  1409. "path" => $path,
  1410. "width" => 1000,
  1411. ];
  1412. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1413. file_put_contents($file, $result);
  1414. return $behind . $fileName;
  1415. }
  1416. //微信开放平台获取component_access_token
  1417. public static function getComponentAccessToken($open)
  1418. {
  1419. $id = $open['id'];
  1420. $componentAccessToken = $open['componentAccessToken'];
  1421. $componentAccessTokenTime = $open['componentAccessTokenTime'];
  1422. $now = time();
  1423. if ($now > strtotime($componentAccessTokenTime)) {
  1424. $appId = $open['appId'];
  1425. $appSecret = $open['appSecret'];
  1426. $verifyTicket = $open['verifyTicket'];
  1427. $data = ['component_appid' => $appId, 'component_appsecret' => $appSecret, 'component_verify_ticket' => $verifyTicket];
  1428. $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
  1429. $curl = new curl\Curl();
  1430. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1431. if (empty($result)) {
  1432. util::stop('没有取到ComponentAccessToken');
  1433. }
  1434. $result = Json::decode($result);
  1435. if (isset($result['component_access_token']) == false || empty($result['component_access_token'])) {
  1436. echo '<pre>';
  1437. print_r($result);
  1438. Yii::info('get component access token error:' . json_encode($result));
  1439. Yii::$app->end();
  1440. }
  1441. $component_access_token = $result['component_access_token'];
  1442. $expiresIn = $result['expires_in'];
  1443. $oData = [];
  1444. $oData['componentAccessToken'] = $component_access_token;
  1445. $oData['componentAccessTokenTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短
  1446. xhWxOpenService::updateById($id, $oData);
  1447. return $component_access_token;
  1448. } else {
  1449. return $componentAccessToken;
  1450. }
  1451. }
  1452. /**
  1453. * 微信开放平台获取预授权码pre_auth_code
  1454. */
  1455. public static function getPreAuthCode($open)
  1456. {
  1457. $id = $open['id'];
  1458. $preAuthCode = $open['preAuthCode'];
  1459. $preAuthCodeTime = $open['preAuthCodeTime'];
  1460. $now = time();
  1461. if ($now > strtotime($preAuthCodeTime)) {
  1462. $token = self::getComponentAccessToken($open);
  1463. $appId = $open['appId'];
  1464. $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token={$token}";
  1465. $curl = new curl\Curl();
  1466. $data = ['component_appid' => $appId];
  1467. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1468. $result = Json::decode($result);
  1469. Yii::info($result);
  1470. if (isset($result['pre_auth_code']) == false || empty($result['pre_auth_code'])) {
  1471. echo "没有取到pre_auth_code,原因:" . json_encode($result) . " appid:" . $appId . " token:" . $token;
  1472. Yii::$app->end();
  1473. }
  1474. $preAuthCode = $result['pre_auth_code'];
  1475. $expiresIn = $result['expires_in'];
  1476. $oData = [];
  1477. $oData['preAuthCode'] = $preAuthCode;
  1478. $oData['preAuthCodeTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短100秒
  1479. xhWxOpenService::updateById($id, $oData);
  1480. return $preAuthCode;
  1481. } else {
  1482. return $preAuthCode;
  1483. }
  1484. }
  1485. /**
  1486. * 微信开放平台使用授权码换取公众号的接口调用凭据和授权信息
  1487. * authorizer_access_token authorizer_refresh_token
  1488. */
  1489. public static function getAuthorizer($code)
  1490. {
  1491. $id = configDict::getConfig('openId');
  1492. $open = xhWxOpenService::getById($id);
  1493. $appId = $open['appId'];
  1494. $accessToken = self::getComponentAccessToken($open);
  1495. $url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={$accessToken}";
  1496. $curl = new curl\Curl();
  1497. $data = ['authorization_code' => $code, 'component_appid' => $appId];
  1498. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1499. $result = Json::decode($result);
  1500. return $result;
  1501. }
  1502. //微信开放平台获取授权方的公众号帐号基本信息 shish 2020.3.11
  1503. public static function getAuthorizerInfo($authorizerAppId)
  1504. {
  1505. $open = WxOpenClass::getOpen();
  1506. $appId = $open['appId'];
  1507. $accessToken = self::getComponentAccessToken($open);
  1508. $url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token={$accessToken}";
  1509. $curl = new curl\Curl();
  1510. $data = ['component_appid' => $appId, 'authorizer_appid' => $authorizerAppId];
  1511. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1512. $result = Json::decode($result);
  1513. return $result;
  1514. }
  1515. }