miniUtil.php 55 KB

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