miniUtil.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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_app_id = $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_app_id}#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_app_id = $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_app_id}&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_app_id = $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_app_id, '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. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  571. if (!empty($wxBaseId)) {
  572. WxBaseService::updateById($wxBaseId, $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_app_id = $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. } elseif ($isOpen == 2) {
  600. $miniAccessToken = $merchant['miniAccessToken'];
  601. $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
  602. $miniRefreshToken = $merchant['miniRefreshToken'];
  603. } else {
  604. $extend = xhMerchantExtendService::getByMerchantId($merchantId);
  605. $miniAccessToken = $extend['miniAccessToken'];
  606. $miniAccessTokenTime = $extend['miniAccessTokenTime'];
  607. $miniRefreshToken = $extend['miniRefreshToken'];
  608. }
  609. $now = time();
  610. if ($now > strtotime($miniAccessTokenTime)) {
  611. $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
  612. $data = ['component_appid' => $component_app_id, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $miniRefreshToken];
  613. $curl = new curl\Curl();
  614. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  615. $result = Json::decode($result);
  616. Yii::info('重新获取mini accesstoken:' . json_encode($result));
  617. if (isset($result['authorizer_access_token'])) {
  618. $expires_in = $result['expires_in'];
  619. $miniAccessToken = $result['authorizer_access_token'];
  620. $uData['miniRefreshToken'] = $result['authorizer_refresh_token'];
  621. $uData['miniAccessToken'] = $miniAccessToken;
  622. $uData['miniAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  623. //零售
  624. if ($isOpen == 1) {
  625. $open = WxOpenService::getOpen();
  626. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  627. if (!empty($wxMiniBaseId)) {
  628. WxMiniBaseService::updateById($wxMiniBaseId, $uData);
  629. }
  630. //供货商
  631. } elseif ($isOpen == 2) {
  632. $open = WxOpenService::getOpen();
  633. $wxMiniBaseId = isset($open['wxGhsMiniBaseId']) ? $open['wxGhsMiniBaseId'] : 0;
  634. if (!empty($wxMiniBaseId)) {
  635. WxMiniBaseService::updateById($wxMiniBaseId, $uData);
  636. }
  637. //其它
  638. } else {
  639. xhMerchantExtendService::updateByMerchantId($merchantId, $uData);
  640. }
  641. return $miniAccessToken;
  642. }
  643. Yii::info('accessToken失效:merchantId:' . $merchantId);
  644. }
  645. return $miniAccessToken;
  646. }
  647. public static function saveWeixinImg($userId, $url, $class = 0)
  648. {
  649. $className = [
  650. '0' => 'weixinUserAvatar',//微信头像
  651. '1' => 'weixinUserImg',//微信公众号二维码
  652. '2' => 'weixinMpQrcode',
  653. '3' => 'weixinMpAvatar',
  654. ];
  655. $folder = Yii::getAlias('@webroot') . '/../../images';
  656. $pre = '/' . $className[$class] . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  657. if (!file_exists($folder . $pre))
  658. mkdir($folder . $pre, 0777, true);
  659. $randString = stringUtil::buildOrderNo();
  660. $name = md5($userId . $randString) . '.jpg';
  661. $fullFileName = $folder . $pre . $name;
  662. $fileName = $pre . $name;
  663. $curl = new curl\Curl();
  664. $result = $curl->get($url);
  665. $fp2 = @fopen($fullFileName, 'a');//文件大小
  666. fwrite($fp2, $result);
  667. fclose($fp2);
  668. return $fileName;
  669. }
  670. /**
  671. * 下载学员作品图片
  672. */
  673. public static function downloadStudentWorksImg($merchant, $mediaId, $savePathType = 'wxUserImg')
  674. {
  675. $accessToken = self::getAuthorizerAccessToken($merchant);
  676. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
  677. $ch = curl_init($url);
  678. curl_setopt($ch, CURLOPT_HEADER, 0);
  679. curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
  680. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  681. $body = curl_exec($ch);
  682. $httpinfo = curl_getinfo($ch);
  683. curl_close($ch);
  684. preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
  685. $fileExt = $extmatches[1];
  686. $imgSavePath = configDict::getConfig('imgSavePath');
  687. $comImgSavePath = $imgSavePath[$savePathType];
  688. $folder = Yii::getAlias('@webroot') . '/../../images';
  689. $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  690. if (!file_exists($folder . $pre)) {
  691. mkdir($folder . $pre, 0777, true);
  692. }
  693. $randString = stringUtil::buildOrderNo();
  694. $fullFileName = $folder . $pre . $randString . '.jpg';
  695. $fileName = $pre . $randString . '.jpg';
  696. $fp2 = @fopen($fullFileName, 'a');//文件大小
  697. fwrite($fp2, $body);
  698. fclose($fp2);
  699. $srcImg = $fullFileName;
  700. $dstImg300 = $folder . $pre . $randString . "_300.jpg";//生成300px缩略图,不保存到数据库
  701. util::img2thumb($srcImg, $dstImg300, $width = 300, 0, 0, 0);
  702. $dstImg100 = $folder . $pre . $randString . "_100.jpg";//生成50px缩略图,不保存到数据库
  703. util::img2thumb($srcImg, $dstImg100, $width = 100, 0, 0, 0);
  704. return $fileName;
  705. }
  706. /**
  707. * 获取media_id里的图片保存到服务器
  708. */
  709. public static function downloadmediaIdImg($merchant, $mediaId, $savePathType = 'wxUserImg')
  710. {
  711. $accessToken = self::getAuthorizerAccessToken($merchant);
  712. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
  713. $ch = curl_init($url);
  714. curl_setopt($ch, CURLOPT_HEADER, 0);
  715. curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
  716. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  717. $body = curl_exec($ch);
  718. $httpinfo = curl_getinfo($ch);
  719. curl_close($ch);
  720. preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
  721. $fileExt = $extmatches[1];
  722. $imgSavePath = configDict::getConfig('imgSavePath');
  723. $comImgSavePath = $imgSavePath[$savePathType];
  724. $folder = Yii::getAlias('@webroot') . '/../../images';
  725. $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  726. if (!file_exists($folder . $pre)) {
  727. mkdir($folder . $pre, 0777, true);
  728. }
  729. $randString = stringUtil::buildOrderNo();
  730. $fullFileName = $folder . $pre . $randString . '.jpg';
  731. $fileName = $pre . $randString . '.jpg';
  732. $fp2 = @fopen($fullFileName, 'a');//文件大小
  733. fwrite($fp2, $body);
  734. fclose($fp2);
  735. $srcImg = $fullFileName;
  736. $mediumImg = $pre . $randString . "_200.jpg";
  737. $dstImg200 = $folder . $mediumImg;//生成200px缩略图,不保存到数据库
  738. util::img2thumb($srcImg, $dstImg200, $width = 200, 0, 0, 0);
  739. $smallImg = $pre . $randString . "_50.jpg";
  740. $dstImg50 = $folder . $smallImg;//生成50px缩略图,不保存到数据库
  741. util::img2thumb($srcImg, $dstImg50, $width = 50, 0, 0, 0);
  742. return ['large' => $fileName, 'medium' => $mediumImg, 'small' => $smallImg];
  743. }
  744. public static function createTag($merchant, $name)
  745. {
  746. $accessToken = self::getAuthorizerAccessToken($merchant);
  747. $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token={$accessToken}";
  748. $post = ['tag' => ['name' => $name]];
  749. $curl = new curl\Curl();
  750. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  751. $result = Json::decode($result);
  752. return $result;
  753. }
  754. public static function delTag($merchant, $id)
  755. {
  756. $accessToken = self::getAuthorizerAccessToken($merchant);
  757. $url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={$accessToken}";
  758. $post = ['tag' => ['id' => $id]];
  759. $curl = new curl\Curl();
  760. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  761. $result = Json::decode($result);
  762. return $result;
  763. }
  764. public static function modTag($merchant, $id, $name)
  765. {
  766. $accessToken = self::getAuthorizerAccessToken($merchant);
  767. $url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token={$accessToken}";
  768. $post = ['tag' => ['id' => $id, 'name' => $name]];
  769. $curl = new curl\Curl();
  770. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  771. $result = Json::decode($result);
  772. return $result;
  773. }
  774. /**
  775. * 获取商家所有标签
  776. */
  777. public static function getTag($merchant)
  778. {
  779. $accessToken = self::getAuthorizerAccessToken($merchant);
  780. $url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token={$accessToken}";
  781. $curl = new curl\Curl();
  782. $result = $curl->get($url);
  783. $result = Json::decode($result);
  784. return $result;
  785. }
  786. public function getUserTag($tagId, $nextOpenId = '')
  787. {
  788. $url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=" . $this->access_token;
  789. $post = ['tagid' => $tagId, 'next_openid' => $nextOpenId];
  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. */
  798. public static function membersBatchTag($merchant, $openIdList, $tagId)
  799. {
  800. $accessToken = self::getAuthorizerAccessToken($merchant);
  801. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$accessToken}";
  802. $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
  803. $curl = new curl\Curl();
  804. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  805. $result = Json::decode($result);
  806. return $result;
  807. }
  808. /**
  809. * 批量取消用户标签
  810. * @param unknown_type $appId
  811. * @param unknown_type $openIdList
  812. * @param unknown_type $tagId
  813. * @return unknown
  814. */
  815. public static function membersCancelTag($merchant, $openIdList, $tagId)
  816. {
  817. $accessToken = self::getAuthorizerAccessToken($merchant);
  818. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token={$accessToken}";
  819. $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
  820. $curl = new curl\Curl();
  821. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  822. $result = Json::decode($result);
  823. return $result;
  824. }
  825. /**
  826. * 新增临时素材
  827. * @param unknown_type $appId
  828. * @return unknown
  829. */
  830. public static function uploadTempMedia($merchant, $imgUrl, $type = 'image')
  831. {
  832. $accessToken = self::getAuthorizerAccessToken($merchant);
  833. $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$accessToken}&type=" . $type;
  834. $postData = ['media' => '@' . $imgUrl];
  835. $ch = curl_init();
  836. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
  837. curl_setopt($ch, CURLOPT_URL, $url);
  838. curl_setopt($ch, CURLOPT_POST, 1);
  839. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  840. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  841. $response = curl_exec($ch);
  842. $result = Json::decode($response);
  843. return $result;
  844. }
  845. /**
  846. * 获取临时素材
  847. * @param unknown_type $appId
  848. * @param unknown_type $mediaId
  849. */
  850. public static function getTempMaterial($merchant, $mediaId)
  851. {
  852. $accessToken = self::getAuthorizerAccessToken($merchant);
  853. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id=" . $mediaId;
  854. $fileInfo = self::getTempMaterialInfo($url);
  855. if (empty($fileInfo) || isset($fileInfo['errcode'])) {
  856. $msg = '未错误哦~';
  857. return ['code' => 'A0002', 'msg' => $msg];
  858. }
  859. $fileName = substr(md5($mediaId), -4) . stringUtil::buildOrderNo() . '.amr';
  860. $preFolder = Yii::getAlias('@webroot') . '/../../images';
  861. $folder = '/weixinChatVoice/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  862. if (!file_exists($preFolder . $folder))
  863. mkdir($preFolder . $folder, 0777, true);
  864. $saveFile = $preFolder . $folder . $fileName;
  865. if (file_exists($saveFile) == false) {
  866. self::readTempMaterial($saveFile, $fileInfo["body"]);
  867. }
  868. return ['code' => 'A0001', 'msg' => 'success', 'data' => ['url' => $folder . $fileName]];
  869. }
  870. /**
  871. * 获取临时素材的头信息等
  872. * @param unknown_type $url
  873. * @return unknown
  874. */
  875. public static function getTempMaterialInfo($url)
  876. {
  877. $ch = curl_init($url);
  878. curl_setopt($ch, CURLOPT_HEADER, 0);
  879. curl_setopt($ch, CURLOPT_NOBODY, 0);//只取body头
  880. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  881. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  882. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  883. $package = curl_exec($ch);
  884. $httpinfo = curl_getinfo($ch);
  885. curl_close($ch);
  886. $imageAll = ['header' => $httpinfo, 'body' => $package];
  887. return $imageAll;
  888. }
  889. /**
  890. * 读取临时素材的资源
  891. * @param unknown_type $filename
  892. * @param unknown_type $fileContent
  893. */
  894. function readTempMaterial($filename, $fileContent)
  895. {
  896. $localFile = fopen($filename, 'w');
  897. if (false !== $localFile) {
  898. if (false !== fwrite($localFile, $fileContent)) {
  899. fclose($localFile);
  900. }
  901. }
  902. }
  903. /**
  904. * 创建门店
  905. * @param $merchant
  906. * @param $menu
  907. * @return mixed
  908. */
  909. public function createStore($info)
  910. {
  911. $jsonData = urldecode(json_encode($info, JSON_UNESCAPED_UNICODE));
  912. $url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token=" . $this->access_token;
  913. $curl = new curl\Curl();
  914. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  915. }
  916. /**
  917. * 查询门店信息
  918. * @param $poi_id
  919. */
  920. public function getStoreInfo($poi_id)
  921. {
  922. $data = ['poi_id' => $poi_id];
  923. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  924. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoi?access_token=" . $this->access_token;
  925. $curl = new curl\Curl();
  926. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  927. }
  928. /**
  929. * 查询门店列表
  930. * @param int $begin 开始数据
  931. * @param int $count 返回数据条数,最大允许50,默认为20
  932. */
  933. public function getStoreList($begin = 0, $count = 20)
  934. {
  935. $data = ["begin" => $begin, "limit" => $count];
  936. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  937. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
  938. $curl = new curl\Curl();
  939. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  940. }
  941. /**
  942. * 查询门店列表 -- 重复 可删除
  943. * @param $data
  944. * @return mixed
  945. */
  946. function getShopList($data)
  947. {
  948. $url = "https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
  949. $curl = new curl\Curl();
  950. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  951. $result = Json::decode($result);
  952. return $result;
  953. }
  954. /**
  955. * 修改门店服务信息
  956. */
  957. public function updateStore(array $buffer)
  958. {
  959. $jsonData = urldecode(json_encode($buffer, JSON_UNESCAPED_UNICODE));
  960. $url = "http://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token=" . $this->access_token;
  961. $curl = new curl\Curl();
  962. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  963. }
  964. /**
  965. * 删除门店
  966. * @param $poi_id
  967. * @return mixed
  968. */
  969. public function delStore($poi_id)
  970. {
  971. $data = ['poi_id' => $poi_id];
  972. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  973. $url = "http://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token=" . $this->access_token;
  974. $curl = new curl\Curl();
  975. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  976. }
  977. /**
  978. * 门店类目表
  979. */
  980. public function storeTypeList()
  981. {
  982. $url = "http://api.weixin.qq.com/cgi-bin/poi/getwxcategory?access_token=" . $this->access_token;
  983. $curl = new curl\Curl();
  984. return $curl->get($url);
  985. }
  986. /******************************* wifi *************************************/
  987. /**
  988. * 获取wifi门店列表
  989. * @param $pageindex 开始页
  990. * @param $pagesize 每页数据条数(最大20)
  991. * @return mixed
  992. */
  993. function getShopWifiList($pageindex = 1, $pagesize = 10)
  994. {
  995. $data = ["pageindex" => $pageindex, "pagesize" => $pagesize];
  996. $url = "https://api.weixin.qq.com/bizwifi/shop/list?access_token=" . $this->access_token;
  997. $curl = new curl\Curl();
  998. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  999. $result = Json::decode($result);
  1000. return $result;
  1001. }
  1002. /**
  1003. * 添加密码型设备
  1004. * @param $shopId 门店ID
  1005. * @param $ssid 无线网络设备的ssid
  1006. * @param $pwd 无线网络设备的密码
  1007. * @return mixed
  1008. */
  1009. function addPasswordDevice($shopId, $ssid, $pwd)
  1010. {
  1011. $data = ["shop_id" => $shopId, "ssid" => $ssid, "password" => $pwd];
  1012. $url = "https://api.weixin.qq.com/bizwifi/device/add?access_token=" . $this->access_token;
  1013. $curl = new curl\Curl();
  1014. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1015. $result = Json::decode($result);
  1016. return $result;
  1017. }
  1018. /**
  1019. * 修改门店Wi-Fi(修改门店网络信息)
  1020. * @param $shopId
  1021. * @param $oldSsid
  1022. * @param $ssid
  1023. * @param string $pwd
  1024. * @return mixed
  1025. */
  1026. function updateDevice($shopId, $oldSsid, $ssid, $pwd = '')
  1027. {
  1028. $data = ["shop_id" => $shopId, "old_ssid" => $oldSsid, "ssid" => $ssid];
  1029. if (!empty($pwd)) {
  1030. $data["password"] = $pwd;
  1031. }
  1032. $url = "https://api.weixin.qq.com/bizwifi/shop/update?access_token=" . $this->access_token;
  1033. $curl = new curl\Curl();
  1034. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1035. $result = Json::decode($result);
  1036. return $result;
  1037. }
  1038. /**
  1039. * 删除密码型设备
  1040. * @param $bssid 无线网络设备无线mac地址
  1041. * @return mixed
  1042. */
  1043. function delPasswordDevice($bssid)
  1044. {
  1045. $data = ["bssid" => $bssid];
  1046. $url = "https://api.weixin.qq.com/bizwifi/device/delete?access_token=" . $this->access_token;
  1047. $curl = new curl\Curl();
  1048. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1049. $result = Json::decode($result);
  1050. return $result;
  1051. }
  1052. /**
  1053. * 查询门店Wi-Fi信息
  1054. * @param $shopId 门店ID
  1055. * @return mixed
  1056. */
  1057. function getShopWifi($shopId)
  1058. {
  1059. $data = ["shop_id" => $shopId];
  1060. $url = "https://api.weixin.qq.com/bizwifi/shop/get?access_token=" . $this->access_token;
  1061. $curl = new curl\Curl();
  1062. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1063. $result = Json::decode($result);
  1064. return $result;
  1065. }
  1066. /**
  1067. * 获取物料二维码
  1068. * @param $shopId 门店ID
  1069. * @param $ssid 无线网络设备的ssid
  1070. * @param $img_id 物料样式编号(0-纯二维码,可用于自由设计宣传材料;1-二维码物料,155mm×215mm(宽×高),可直接张贴)
  1071. * @return mixed
  1072. */
  1073. function getWifiQrCode($shopId, $ssid, $img_id)
  1074. {
  1075. //$data = ["shop_id" => 429620, "ssid" => "WX567", "img_id" => $img_id];
  1076. $data = ["shop_id" => $shopId, "ssid" => $ssid, "img_id" => $img_id];
  1077. $url = "https://api.weixin.qq.com/bizwifi/qrcode/get?access_token=" . $this->access_token;
  1078. $curl = new curl\Curl();
  1079. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1080. $result = Json::decode($result);
  1081. return $result;
  1082. }
  1083. function getWifiUrl()
  1084. {
  1085. $url = "https://api.weixin.qq.com/bizwifi/account/get_connecturl?access_token=" . $this->access_token;
  1086. $curl = new curl\Curl();
  1087. $result = $curl->get($url);
  1088. $result = Json::decode($result);
  1089. return $result;
  1090. }
  1091. function generateShortUrl($longUrl)
  1092. {
  1093. $data = ["action" => "long2short", "long_url" => $longUrl,];
  1094. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $this->access_token;
  1095. $curl = new curl\Curl();
  1096. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1097. $result = Json::decode($result);
  1098. if (isset($result['errcode']) && $result['errcode'] == 0) {
  1099. return ['code' => 'A0001', 'data' => ['shortUrl' => $result['short_url']]];
  1100. }
  1101. return ['code' => 'A0002', 'data' => []];
  1102. }
  1103. //长链接转短链接 shish 2019.9.26
  1104. public static function urlLongToShort($longUrl, $merchant)
  1105. {
  1106. $accessToken = self::getMiniProgramAccessToken($merchant);
  1107. $data = ["action" => "long2short", "long_url" => $longUrl,];
  1108. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $accessToken;
  1109. $curl = new curl\Curl();
  1110. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1111. $result = Json::decode($result);
  1112. $url = '';
  1113. if (isset($result['errcode']) && $result['errcode'] == 0) {
  1114. $url = $result['short_url'];
  1115. }
  1116. return ['url' => $url];
  1117. }
  1118. /**
  1119. * 拉取门店所有的设备信息
  1120. * @return array
  1121. */
  1122. public function getShopWifiListAll()
  1123. {
  1124. $startPage = 1;
  1125. $list = [];
  1126. while (true) {
  1127. $reList = $this->getShopWifiList($startPage, 20);
  1128. if ($startPage <= $reList['data']['pagecount']) {
  1129. $list = array_merge($list, $reList['data']['records']);
  1130. ++$startPage;
  1131. } else {
  1132. break;
  1133. }
  1134. }
  1135. return $list;
  1136. }
  1137. /**
  1138. * 通过 poi_id 从Wi-Fi门店列表中 获取 shopId
  1139. * @param $poiId
  1140. * @return bool
  1141. */
  1142. public function getShopId($poiId, $list = [])
  1143. {
  1144. if (empty($list)) {
  1145. $list = $this->getShopWifiListAll();
  1146. }
  1147. foreach ($list as $shop) {
  1148. if ($shop['poi_id'] == $poiId)
  1149. return (string)$shop['shop_id'];
  1150. }
  1151. return false;
  1152. }
  1153. //设置小程序服务器域名
  1154. public static function setDomain($merchant, $isOpen = 0)
  1155. {
  1156. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1157. $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $accessToken;
  1158. $curl = new curl\Curl();
  1159. $mallHost = Yii::$app->params['mallHost'];
  1160. if ($isOpen == 1) {
  1161. $mallHost = Yii::$app->params['shopHost'];
  1162. }
  1163. $has = strpos($mallHost, 'https');
  1164. //如果不是https则换成https
  1165. if ($has === false) {
  1166. $mallHost = str_replace('http', 'https', $mallHost);
  1167. }
  1168. $data = [
  1169. "action" => "set",
  1170. "requestdomain" => [$mallHost],
  1171. "wsrequestdomain" => [$mallHost],
  1172. "uploaddomain" => [$mallHost],
  1173. "downloaddomain" => [$mallHost],
  1174. ];
  1175. print_r($data);
  1176. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1177. $respond = Json::decode($result);
  1178. print_r($respond);
  1179. }
  1180. //设置业务域名
  1181. public static function setWebViewDomain($merchant, $isOpen = 0)
  1182. {
  1183. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1184. $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $accessToken;
  1185. $curl = new curl\Curl();
  1186. $mallHost = Yii::$app->params['mallHost'];
  1187. if ($isOpen == 1) {
  1188. $mallHost = Yii::$app->params['shopHost'];
  1189. }
  1190. $has = strpos($mallHost, 'https');
  1191. //如果不是https则换成https
  1192. if ($has === false) {
  1193. $mallHost = str_replace('http', 'https', $mallHost);
  1194. }
  1195. $data = [
  1196. "action" => "set",
  1197. "webviewdomain" => [$mallHost],
  1198. ];
  1199. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1200. $respond = Json::decode($result);
  1201. print_r($respond);
  1202. }
  1203. //为授权的小程序帐号上传小程序代码
  1204. public static function miniCommit($merchant, $templateId, $isOpen = 0)
  1205. {
  1206. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1207. $url = 'https://api.weixin.qq.com/wxa/commit?access_token=' . $accessToken;
  1208. $curl = new curl\Curl();
  1209. $apiUrl = Yii::$app->params['mallHost'];
  1210. if ($isOpen == 1) {
  1211. $apiUrl = Yii::$app->params['shopHost'];
  1212. }
  1213. $imgUrl = Yii::$app->params['imgHost'];
  1214. //如果不是https则换成https
  1215. $has = strpos($apiUrl, 'https');
  1216. if ($has === false) {
  1217. $apiUrl = str_replace('http', 'https', $apiUrl);
  1218. }
  1219. //如果不是https则换成https
  1220. $has = strpos($imgUrl, 'https');
  1221. if ($has === false) {
  1222. $imgUrl = str_replace('http', 'https', $imgUrl);
  1223. }
  1224. $account = isset($merchant['id']) ? $merchant['id'] : 0;
  1225. $name = isset($merchant['merchantName']) ? $merchant['merchantName'] : '';
  1226. if ($isOpen == 1) {
  1227. $open = WxOpenService::getOpen();
  1228. $name = isset($open['name']) ? $open['name'] : '';
  1229. }
  1230. $ext = [
  1231. 'extAppid' => $merchant['miniAppId'],
  1232. 'ext' => [
  1233. 'account' => $account,
  1234. 'apiHost' => $apiUrl,
  1235. 'imgHost' => $imgUrl,
  1236. 'merchantName' => $name,
  1237. ],
  1238. ];
  1239. echo '<pre>';
  1240. print_r($ext);
  1241. Yii::info("ext:" . json_encode($ext));
  1242. $extJson = json_encode($ext);
  1243. $data = [
  1244. "template_id" => $templateId,
  1245. "ext_json" => $extJson,
  1246. "user_version" => date("mdHis"),
  1247. "user_desc" => date("mdHis")
  1248. ];
  1249. print_r($data);
  1250. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1251. $respond = Json::decode($result);
  1252. print_r($respond);
  1253. }
  1254. //获取体验小程序的体验二维码
  1255. public static function getExperienceQrCode($merchant, $page, $isOpen = 0)
  1256. {
  1257. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1258. $path = urlencode($page);
  1259. $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token={$accessToken}&path=" . $path;
  1260. $curl = new curl\Curl();
  1261. $result = $curl->get($url);
  1262. $file = dirUtil::getImgDir() . '/retail/miniExperience/' . $merchant['id'] . '.jpg';
  1263. file_put_contents($file, $result);
  1264. echo "<b /><img src='" . Yii::$app->params['imgHost'] . "/retail/miniExperience/" . $merchant['id'] . ".jpg' />";
  1265. }
  1266. //获取授权小程序帐号已设置的类目
  1267. public static function getMiniCategory($merchant, $isOpen = 0)
  1268. {
  1269. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1270. $url = "https://api.weixin.qq.com/wxa/get_category?access_token=" . $accessToken;
  1271. $curl = new curl\Curl();
  1272. $result = $curl->get($url);
  1273. $respond = Json::decode($result);
  1274. echo "<pre>";
  1275. print_r($respond);
  1276. }
  1277. //小程序支付成功后获取唯一ID shish 2020.5.12
  1278. public static function getPaidUnionId($merchant, $miniOpenId, $mcId, $orderId, $isOpen = 0)
  1279. {
  1280. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1281. $url = "https://api.weixin.qq.com/wxa/getpaidunionid?access_token={$accessToken}&openid={$miniOpenId}&mch_id={$mcId}&out_trade_no=" . (string)$orderId;
  1282. $curl = new curl\Curl();
  1283. $result = $curl->get($url);
  1284. $respond = Json::decode($result);
  1285. $unionId = isset($respond['unionid']) && !empty($respond['unionid']) ? $respond['unionid'] : '';
  1286. return $unionId;
  1287. }
  1288. //小程序加急审核 shish 2020.4.20
  1289. public static function speedupAudit($merchant, $id, $isOpen = 0)
  1290. {
  1291. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1292. $url = "https://api.weixin.qq.com/wxa/speedupaudit?access_token=" . $accessToken;
  1293. $curl = new curl\Curl();
  1294. $data = [
  1295. "auditid" => $id
  1296. ];
  1297. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1298. $respond = Json::decode($result);
  1299. print_r($respond);
  1300. }
  1301. //获取小程序的第三方提交代码的页面配置
  1302. public static function getMiniPage($merchant, $isOpen = 0)
  1303. {
  1304. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1305. $url = "https://api.weixin.qq.com/wxa/get_page?access_token=" . $accessToken;
  1306. $curl = new curl\Curl();
  1307. $result = $curl->get($url);
  1308. $respond = Json::decode($result);
  1309. echo "<pre>";
  1310. print_r($respond);
  1311. }
  1312. //将第三方提交的代码包提交审核
  1313. public static function miniSubmitAudit($merchant, $page, $isOpen = 0)
  1314. {
  1315. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1316. $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $accessToken;
  1317. $curl = new curl\Curl();
  1318. if ($isOpen == 1) {
  1319. $data = [
  1320. "item_list" => [
  1321. [
  1322. "address" => $page,
  1323. "tag" => "花卉宝 花店",
  1324. "first_class" => "IT科技",
  1325. "second_class" => "软件服务提供商",
  1326. "first_id" => 210,
  1327. "second_id" => 413,
  1328. "title" => "花卉宝",
  1329. ],
  1330. ],
  1331. "feedback_info" => "",
  1332. "feedback_stuff" => "",
  1333. ];
  1334. } else {
  1335. $data = [
  1336. "item_list" => [
  1337. [
  1338. "address" => $page,
  1339. "tag" => "鲜花 花店",
  1340. "first_class" => "商家自营",
  1341. "second_class" => "鲜花/园艺/工艺品",
  1342. "first_id" => 304,
  1343. "second_id" => 323,
  1344. "title" => "商城",
  1345. ],
  1346. ],
  1347. "feedback_info" => "",
  1348. "feedback_stuff" => "",
  1349. ];
  1350. }
  1351. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1352. $respond = Json::decode($result);
  1353. echo "<pre>";
  1354. print_r($respond);
  1355. }
  1356. //查询最新一次提交的审核状态
  1357. public static function miniGetLatestAuditStatus($merchant, $isOpen = 0)
  1358. {
  1359. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1360. $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $accessToken;
  1361. $curl = new curl\Curl();
  1362. $result = $curl->get($url);
  1363. $respond = Json::decode($result);
  1364. echo "<pre>";
  1365. print_r($respond);
  1366. }
  1367. //发布已通过审核的小程序
  1368. public static function miniRelease($merchant, $isOpen = 0)
  1369. {
  1370. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1371. $url = "https://api.weixin.qq.com/wxa/release?access_token=" . $accessToken;
  1372. $curl = new curl\Curl();
  1373. $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
  1374. $respond = Json::decode($result);
  1375. return $respond;
  1376. }
  1377. //撤回审核
  1378. public static function rollbackCheck($merchant, $isOpen = 0)
  1379. {
  1380. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1381. $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=" . $accessToken;
  1382. $curl = new curl\Curl();
  1383. $result = $curl->get($url);
  1384. $respond = Json::decode($result);
  1385. echo "<pre>";
  1386. print_r($respond);
  1387. }
  1388. //申请会员的小程序码
  1389. public static function generateMemberCode($merchant, $shopId, $isOpen = 0)
  1390. {
  1391. $fileName = $merchant['id'] . '_' . $shopId . '_apply_member.jpg';
  1392. $behind = '/retail/mimiMemberCode/';
  1393. $filePath = dirUtil::getImgDir() . $behind;
  1394. if (!file_exists($filePath)) {
  1395. mkdir($filePath, 0777, true);
  1396. }
  1397. $file = $filePath . $fileName;
  1398. if (file_exists($file)) {
  1399. return $behind . $fileName;
  1400. }
  1401. $path = 'pages/home/user?scan=1&shopId=' . $shopId;
  1402. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1403. $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
  1404. $curl = new curl\Curl();
  1405. $data = [
  1406. "path" => $path,
  1407. "width" => 1000,
  1408. ];
  1409. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1410. file_put_contents($file, $result);
  1411. return $behind . $fileName;
  1412. }
  1413. //小程序码生成,数量不限 shish 2020.4.17
  1414. public static function generateUnlimitedMiniCode($merchant, $page, $scene = '', $isOpen = 0)
  1415. {
  1416. $id = isset($merchant['id']) ? $merchant['id'] : 0;
  1417. $fileName = stringUtil::shortUniqueId($id) . '.jpg';
  1418. $behind = '/retail/mimiCode/' . date("Ym") . '/' . date("d") . '/' . $id . '/';
  1419. $filePath = dirUtil::getImgDir() . $behind;
  1420. if (!file_exists($filePath)) {
  1421. mkdir($filePath, 0777, true);
  1422. }
  1423. $file = $filePath . $fileName;
  1424. if (file_exists($file)) {
  1425. return $behind . $fileName;
  1426. }
  1427. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1428. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
  1429. $curl = new curl\Curl();
  1430. $data = [];
  1431. if (!empty($page)) {
  1432. $data["page"] = $page;
  1433. }
  1434. $data["scene"] = $scene;
  1435. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1436. Yii::info('获取小程序图片资源:' . json_encode($result));
  1437. file_put_contents($file, $result);
  1438. return $behind . $fileName;
  1439. }
  1440. //微信开放平台获取component_access_token
  1441. public static function getComponentAccessToken($open)
  1442. {
  1443. $id = $open['id'];
  1444. $componentAccessToken = $open['componentAccessToken'];
  1445. $componentAccessTokenTime = $open['componentAccessTokenTime'];
  1446. $now = time();
  1447. if ($now > strtotime($componentAccessTokenTime)) {
  1448. $appId = $open['appId'];
  1449. $appSecret = $open['appSecret'];
  1450. $verifyTicket = $open['verifyTicket'];
  1451. $data = ['component_appid' => $appId, 'component_appsecret' => $appSecret, 'component_verify_ticket' => $verifyTicket];
  1452. $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
  1453. $curl = new curl\Curl();
  1454. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1455. if (empty($result)) {
  1456. util::stop('没有取到ComponentAccessToken');
  1457. }
  1458. $result = Json::decode($result);
  1459. if (isset($result['component_access_token']) == false || empty($result['component_access_token'])) {
  1460. echo '<pre>';
  1461. print_r($result);
  1462. Yii::info('get component access token error:' . json_encode($result));
  1463. Yii::$app->end();
  1464. }
  1465. $component_access_token = $result['component_access_token'];
  1466. $expiresIn = $result['expires_in'];
  1467. $oData = [];
  1468. $oData['componentAccessToken'] = $component_access_token;
  1469. $oData['componentAccessTokenTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短
  1470. xhWxOpenService::updateById($id, $oData);
  1471. return $component_access_token;
  1472. } else {
  1473. return $componentAccessToken;
  1474. }
  1475. }
  1476. /**
  1477. * 微信开放平台获取预授权码pre_auth_code
  1478. */
  1479. public static function getPreAuthCode($open)
  1480. {
  1481. $id = $open['id'];
  1482. $preAuthCode = $open['preAuthCode'];
  1483. $preAuthCodeTime = $open['preAuthCodeTime'];
  1484. $now = time();
  1485. if ($now > strtotime($preAuthCodeTime)) {
  1486. $token = self::getComponentAccessToken($open);
  1487. $appId = $open['appId'];
  1488. $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token={$token}";
  1489. $curl = new curl\Curl();
  1490. $data = ['component_appid' => $appId];
  1491. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1492. $result = Json::decode($result);
  1493. Yii::info($result);
  1494. if (isset($result['pre_auth_code']) == false || empty($result['pre_auth_code'])) {
  1495. echo "没有取到pre_auth_code,原因:" . json_encode($result) . " appid:" . $appId . " token:" . $token;
  1496. Yii::$app->end();
  1497. }
  1498. $preAuthCode = $result['pre_auth_code'];
  1499. $expiresIn = $result['expires_in'];
  1500. $oData = [];
  1501. $oData['preAuthCode'] = $preAuthCode;
  1502. $oData['preAuthCodeTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短100秒
  1503. xhWxOpenService::updateById($id, $oData);
  1504. return $preAuthCode;
  1505. } else {
  1506. return $preAuthCode;
  1507. }
  1508. }
  1509. /**
  1510. * 微信开放平台使用授权码换取公众号的接口调用凭据和授权信息
  1511. * authorizer_access_token authorizer_refresh_token
  1512. */
  1513. public static function getAuthorizer($code)
  1514. {
  1515. $id = configDict::getConfig('openId');
  1516. $open = xhWxOpenService::getById($id);
  1517. $appId = $open['appId'];
  1518. $accessToken = self::getComponentAccessToken($open);
  1519. $url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={$accessToken}";
  1520. $curl = new curl\Curl();
  1521. $data = ['authorization_code' => $code, 'component_appid' => $appId];
  1522. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1523. $result = Json::decode($result);
  1524. return $result;
  1525. }
  1526. //微信开放平台获取授权方的公众号帐号基本信息 shish 2020.3.11
  1527. public static function getAuthorizerInfo($authorizerAppId)
  1528. {
  1529. $open = WxOpenClass::getOpen();
  1530. $appId = $open['appId'];
  1531. $accessToken = self::getComponentAccessToken($open);
  1532. $url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token={$accessToken}";
  1533. $curl = new curl\Curl();
  1534. $data = ['component_appid' => $appId, 'authorizer_appid' => $authorizerAppId];
  1535. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1536. $result = Json::decode($result);
  1537. return $result;
  1538. }
  1539. //开通即时配送权限
  1540. public static function openLocalExpress($merchant, $isOpen = 0)
  1541. {
  1542. $accessToken = self::getMiniProgramAccessToken($merchant, $isOpen);
  1543. $url = "https://api.weixin.qq.com/cgi-bin/express/local/business/open?access_token=" . $accessToken;
  1544. $curl = new curl\Curl();
  1545. $result = $curl->get($url);
  1546. $respond = Json::decode($result);
  1547. echo "<pre>";
  1548. print_r($respond);
  1549. }
  1550. }