miniUtil.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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. print_r($result);
  1272. die;
  1273. $respond = Json::decode($result);
  1274. echo "<pre>";
  1275. print_r($respond);
  1276. }
  1277. //小程序加急审核 shish 2020.4.20
  1278. public static function speedupAudit($merchant, $id, $isOpen = 0)
  1279. {
  1280. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1281. $url = "https://api.weixin.qq.com/wxa/speedupaudit?access_token=" . $accessToken;
  1282. $curl = new curl\Curl();
  1283. $data = [
  1284. "auditid" => $id
  1285. ];
  1286. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1287. $respond = Json::decode($result);
  1288. print_r($respond);
  1289. }
  1290. //获取小程序的第三方提交代码的页面配置
  1291. public static function getMiniPage($merchant, $isOpen = 0)
  1292. {
  1293. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1294. $url = "https://api.weixin.qq.com/wxa/get_page?access_token=" . $accessToken;
  1295. $curl = new curl\Curl();
  1296. $result = $curl->get($url);
  1297. $respond = Json::decode($result);
  1298. echo "<pre>";
  1299. print_r($respond);
  1300. }
  1301. //将第三方提交的代码包提交审核
  1302. public static function miniSubmitAudit($merchant, $page, $isOpen = 0)
  1303. {
  1304. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1305. $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $accessToken;
  1306. $curl = new curl\Curl();
  1307. if ($isOpen == 1) {
  1308. $data = [
  1309. "item_list" => [
  1310. [
  1311. "address" => $page,
  1312. "tag" => "花卉宝 花店",
  1313. "first_class" => "IT科技",
  1314. "second_class" => "软件服务提供商",
  1315. "first_id" => 210,
  1316. "second_id" => 413,
  1317. "title" => "花卉宝",
  1318. ],
  1319. ],
  1320. "feedback_info" => "",
  1321. "feedback_stuff" => "",
  1322. ];
  1323. } else {
  1324. $data = [
  1325. "item_list" => [
  1326. [
  1327. "address" => $page,
  1328. "tag" => "鲜花 花店",
  1329. "first_class" => "商家自营",
  1330. "second_class" => "鲜花/园艺/工艺品",
  1331. "first_id" => 304,
  1332. "second_id" => 323,
  1333. "title" => "商城",
  1334. ],
  1335. ],
  1336. "feedback_info" => "",
  1337. "feedback_stuff" => "",
  1338. ];
  1339. }
  1340. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1341. $respond = Json::decode($result);
  1342. echo "<pre>";
  1343. print_r($respond);
  1344. }
  1345. //查询最新一次提交的审核状态
  1346. public static function miniGetLatestAuditStatus($merchant, $isOpen = 0)
  1347. {
  1348. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1349. $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $accessToken;
  1350. $curl = new curl\Curl();
  1351. $result = $curl->get($url);
  1352. $respond = Json::decode($result);
  1353. echo "<pre>";
  1354. print_r($respond);
  1355. }
  1356. //发布已通过审核的小程序
  1357. public static function miniRelease($merchant, $isOpen = 0)
  1358. {
  1359. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1360. $url = "https://api.weixin.qq.com/wxa/release?access_token=" . $accessToken;
  1361. $curl = new curl\Curl();
  1362. $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
  1363. $respond = Json::decode($result);
  1364. return $respond;
  1365. }
  1366. //撤回审核
  1367. public static function rollbackCheck($merchant, $isOpen = 0)
  1368. {
  1369. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1370. $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=" . $accessToken;
  1371. $curl = new curl\Curl();
  1372. $result = $curl->get($url);
  1373. $respond = Json::decode($result);
  1374. echo "<pre>";
  1375. print_r($respond);
  1376. }
  1377. //申请会员的小程序码
  1378. public static function generateMemberCode($merchant, $shopId, $isOpen = 0)
  1379. {
  1380. $fileName = $merchant['id'] . '_' . $shopId . '_apply_member.jpg';
  1381. $behind = '/retail/mimiMemberCode/';
  1382. $filePath = dirUtil::getImgDir() . $behind;
  1383. if (!file_exists($filePath)) {
  1384. mkdir($filePath, 0777, true);
  1385. }
  1386. $file = $filePath . $fileName;
  1387. if (file_exists($file)) {
  1388. return $behind . $fileName;
  1389. }
  1390. $path = 'pages/home/user?scan=1&shopId=' . $shopId;
  1391. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1392. $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
  1393. $curl = new curl\Curl();
  1394. $data = [
  1395. "path" => $path,
  1396. "width" => 1000,
  1397. ];
  1398. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1399. file_put_contents($file, $result);
  1400. return $behind . $fileName;
  1401. }
  1402. //小程序码生成,数量不限 shish 2020.4.17
  1403. public static function generateUnlimitedMiniCode($merchant, $page, $scene = '', $isOpen = 0)
  1404. {
  1405. $id = isset($merchant['id']) ? $merchant['id'] : 0;
  1406. $fileName = stringUtil::shortUniqueId($id) . '.jpg';
  1407. $behind = '/retail/mimiCode/' . date("Ym") . '/' . date("d") . '/' . $id . '/';
  1408. $filePath = dirUtil::getImgDir() . $behind;
  1409. if (!file_exists($filePath)) {
  1410. mkdir($filePath, 0777, true);
  1411. }
  1412. $file = $filePath . $fileName;
  1413. if (file_exists($file)) {
  1414. return $behind . $fileName;
  1415. }
  1416. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1417. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
  1418. $curl = new curl\Curl();
  1419. $data = [];
  1420. if (!empty($page)) {
  1421. $data["page"] = $page;
  1422. }
  1423. $data["scene"] = $scene;
  1424. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1425. file_put_contents($file, $result);
  1426. return $behind . $fileName;
  1427. }
  1428. //微信开放平台获取component_access_token
  1429. public static function getComponentAccessToken($open)
  1430. {
  1431. $id = $open['id'];
  1432. $componentAccessToken = $open['componentAccessToken'];
  1433. $componentAccessTokenTime = $open['componentAccessTokenTime'];
  1434. $now = time();
  1435. if ($now > strtotime($componentAccessTokenTime)) {
  1436. $appId = $open['appId'];
  1437. $appSecret = $open['appSecret'];
  1438. $verifyTicket = $open['verifyTicket'];
  1439. $data = ['component_appid' => $appId, 'component_appsecret' => $appSecret, 'component_verify_ticket' => $verifyTicket];
  1440. $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
  1441. $curl = new curl\Curl();
  1442. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1443. if (empty($result)) {
  1444. util::stop('没有取到ComponentAccessToken');
  1445. }
  1446. $result = Json::decode($result);
  1447. if (isset($result['component_access_token']) == false || empty($result['component_access_token'])) {
  1448. echo '<pre>';
  1449. print_r($result);
  1450. Yii::info('get component access token error:' . json_encode($result));
  1451. Yii::$app->end();
  1452. }
  1453. $component_access_token = $result['component_access_token'];
  1454. $expiresIn = $result['expires_in'];
  1455. $oData = [];
  1456. $oData['componentAccessToken'] = $component_access_token;
  1457. $oData['componentAccessTokenTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短
  1458. xhWxOpenService::updateById($id, $oData);
  1459. return $component_access_token;
  1460. } else {
  1461. return $componentAccessToken;
  1462. }
  1463. }
  1464. /**
  1465. * 微信开放平台获取预授权码pre_auth_code
  1466. */
  1467. public static function getPreAuthCode($open)
  1468. {
  1469. $id = $open['id'];
  1470. $preAuthCode = $open['preAuthCode'];
  1471. $preAuthCodeTime = $open['preAuthCodeTime'];
  1472. $now = time();
  1473. if ($now > strtotime($preAuthCodeTime)) {
  1474. $token = self::getComponentAccessToken($open);
  1475. $appId = $open['appId'];
  1476. $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token={$token}";
  1477. $curl = new curl\Curl();
  1478. $data = ['component_appid' => $appId];
  1479. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1480. $result = Json::decode($result);
  1481. Yii::info($result);
  1482. if (isset($result['pre_auth_code']) == false || empty($result['pre_auth_code'])) {
  1483. echo "没有取到pre_auth_code,原因:" . json_encode($result) . " appid:" . $appId . " token:" . $token;
  1484. Yii::$app->end();
  1485. }
  1486. $preAuthCode = $result['pre_auth_code'];
  1487. $expiresIn = $result['expires_in'];
  1488. $oData = [];
  1489. $oData['preAuthCode'] = $preAuthCode;
  1490. $oData['preAuthCodeTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短100秒
  1491. xhWxOpenService::updateById($id, $oData);
  1492. return $preAuthCode;
  1493. } else {
  1494. return $preAuthCode;
  1495. }
  1496. }
  1497. /**
  1498. * 微信开放平台使用授权码换取公众号的接口调用凭据和授权信息
  1499. * authorizer_access_token authorizer_refresh_token
  1500. */
  1501. public static function getAuthorizer($code)
  1502. {
  1503. $id = configDict::getConfig('openId');
  1504. $open = xhWxOpenService::getById($id);
  1505. $appId = $open['appId'];
  1506. $accessToken = self::getComponentAccessToken($open);
  1507. $url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={$accessToken}";
  1508. $curl = new curl\Curl();
  1509. $data = ['authorization_code' => $code, 'component_appid' => $appId];
  1510. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1511. $result = Json::decode($result);
  1512. return $result;
  1513. }
  1514. //微信开放平台获取授权方的公众号帐号基本信息 shish 2020.3.11
  1515. public static function getAuthorizerInfo($authorizerAppId)
  1516. {
  1517. $open = WxOpenClass::getOpen();
  1518. $appId = $open['appId'];
  1519. $accessToken = self::getComponentAccessToken($open);
  1520. $url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token={$accessToken}";
  1521. $curl = new curl\Curl();
  1522. $data = ['component_appid' => $appId, 'authorizer_appid' => $authorizerAppId];
  1523. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1524. $result = Json::decode($result);
  1525. return $result;
  1526. }
  1527. //开通即时配送权限
  1528. public static function openLocalExpress($merchant, $isOpen = 0)
  1529. {
  1530. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1531. $url = "https://api.weixin.qq.com/cgi-bin/express/local/business/open?access_token=" . $accessToken;
  1532. $curl = new curl\Curl();
  1533. $result = $curl->get($url);
  1534. $respond = Json::decode($result);
  1535. echo "<pre>";
  1536. print_r($respond);
  1537. }
  1538. }