miniUtil.php 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. <?php
  2. namespace common\components;
  3. use bizHd\wx\classes\WxOpenClass;
  4. use bizHd\wx\services\WxBaseService;
  5. use bizHd\wx\services\WxMiniBaseService;
  6. use bizHd\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 = '', $ptStyle = 1)
  20. {
  21. $this->access_token = !empty($merchant) ? self::getMiniProgramAccessToken($merchant, $ptStyle) : '';
  22. }
  23. //获取用户基本信息
  24. public static function userInfo($openId, $merchant, $ptStyle = 0)
  25. {
  26. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  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, $ptStyle = 0)
  38. {
  39. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  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, $ptStyle = 0)
  49. {
  50. if ($isMiniProgram) {
  51. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  52. } else {
  53. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  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, $ptStyle = 0)
  64. {
  65. if ($isMiniProgram) {
  66. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  67. } else {
  68. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  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. //解绑公众号绑定的平台 ssh 2019.9.15
  78. public static function unbindAccount($merchant, $ptStyle = 0)
  79. {
  80. $accessToken = self::getAuthorizerAccessToken($merchant, $ptStyle);
  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. //解绑小程序绑定的平台 ssh 2019.9.15
  91. public static function unbindMiniProgram($merchant, $ptStyle = 0)
  92. {
  93. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  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 = '', $ptStyle = 1)
  104. {
  105. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  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, $ptStyle)
  112. {
  113. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  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::getMiniProgramAccessToken($merchant, $ptStyle);
  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::getMiniProgramAccessToken($merchant, $ptStyle);
  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. */
  212. public static function uploadMedia($media_id, $access_token)
  213. {
  214. $url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $access_token . '&media_id=' . $media_id;
  215. $result = CurlUtil::httpsGet($url);
  216. if (isset($result['errcode'])) {
  217. return false;
  218. } else {
  219. return $result;
  220. }
  221. }
  222. /**
  223. * 获取素材总数
  224. * 图片和图文消息素材(包括单图文和多图文)的总数上限为5000,其他素材的总数上限为1000
  225. */
  226. public function getMaterialCount($merchant, $ptStyle)
  227. {
  228. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  229. $url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token={$token}";
  230. $curl = new curl\Curl();
  231. $response = $curl->get($url);
  232. return $response;
  233. }
  234. /**
  235. * 获取素材列表 图文列表
  236. * $data = ['type' => 'image','offset' => 0,'count' => 100];
  237. * type:news image video voice
  238. */
  239. public function batchgetMaterial($merchant, $data, $ptStyle)
  240. {
  241. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  242. $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={$token}";
  243. $curl = new curl\Curl();
  244. $response = $curl->setOption(CURLOPT_POSTFIELDS, json_encode($data))->post($url);
  245. return json_decode($response, true);
  246. }
  247. /**
  248. * 解除绑定删除自定义菜单
  249. */
  250. public static function menuDelete($merchant, $ptStyle)
  251. {
  252. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  253. $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=$token";
  254. $curl = new curl\Curl();
  255. $response = $curl->get($url);
  256. return $response;
  257. }
  258. //微信api不支持中文转义的json结构
  259. static function json_encode($arr)
  260. {
  261. if (count($arr) == 0) return "[]";
  262. $parts = array();
  263. $is_list = false;
  264. //Find out if the given array is a numerical array
  265. $keys = array_keys($arr);
  266. $max_length = count($arr) - 1;
  267. if (($keys [0] === 0) && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
  268. $is_list = true;
  269. for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
  270. if ($i != $keys [$i]) { //A key fails at position check.
  271. $is_list = false; //It is an associative array.
  272. break;
  273. }
  274. }
  275. }
  276. foreach ($arr as $key => $value) {
  277. if (is_array($value)) { //Custom handling for arrays
  278. if ($is_list)
  279. $parts [] = self::json_encode($value); /* :RECURSION: */
  280. else
  281. $parts [] = '"' . $key . '":' . self::json_encode($value); /* :RECURSION: */
  282. } else {
  283. $str = '';
  284. if (!$is_list)
  285. $str = '"' . $key . '":';
  286. //Custom handling for multiple data types
  287. if (!is_string($value) && is_numeric($value) && $value < 2000000000)
  288. $str .= $value; //Numbers
  289. elseif ($value === false)
  290. $str .= 'false'; //The booleans
  291. elseif ($value === true)
  292. $str .= 'true';
  293. else
  294. $str .= '"' . addslashes($value) . '"'; //All other things
  295. $parts [] = $str;
  296. }
  297. }
  298. $json = implode(',', $parts);
  299. if ($is_list)
  300. return '[' . $json . ']'; //Return numerical JSON
  301. return '{' . $json . '}'; //Return associative JSON
  302. }
  303. /**
  304. * 创建临时二维码 ssh 2019.9.4
  305. */
  306. public static function qrcodeCreate($merchant, $sceneId, $expireSeconds = null, $ptStyle = 1)
  307. {
  308. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  309. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$accessToken";
  310. //有效期默认一天
  311. $expireSeconds = isset($expireSeconds) ? $expireSeconds : 86400;
  312. $post = array('expire_seconds' => $expireSeconds, 'action_name' => 'QR_STR_SCENE', "action_info" => array('scene' => array('scene_str' => $sceneId)));
  313. $curl = new curl\Curl();
  314. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  315. $result = Json::decode($result);
  316. return $result;
  317. }
  318. /**
  319. * 创建永久二维码
  320. */
  321. public static function qrcodePermanentCreate($merchant, $sceneId, $ptStyle = 1)
  322. {
  323. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  324. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$accessToken";
  325. $post = array('action_name' => 'QR_LIMIT_STR_SCENE', "action_info" => array('scene' => array('scene_str' => $sceneId)));
  326. $curl = new curl\Curl();
  327. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  328. $result = Json::decode($result);
  329. return $result;
  330. }
  331. /**
  332. * 微信授权获取code
  333. */
  334. public static function getCode($urlEncode, $merchant, $scope, $ptStyle)
  335. {
  336. $host = Yii::$app->request->getHostInfo();
  337. $REDIRECT_URI = $host . '/auth/get-user-info?url=' . $urlEncode . '&authScope=' . $scope;
  338. Yii::info($REDIRECT_URI);
  339. $appId = $merchant['wxAppId'];
  340. $open = [];
  341. if ($ptStyle == 1) {
  342. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  343. }
  344. if ($ptStyle == 2) {
  345. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  346. }
  347. if ($ptStyle == 3) {
  348. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  349. }
  350. if (empty($open)) {
  351. util::fail('没有找到平台');
  352. }
  353. $component_app_id = $open['appId'];
  354. $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";
  355. header("Location:" . $url);
  356. //如果这里没有exit(),在apache服务器下无法实现跳转
  357. exit();
  358. }
  359. /**
  360. * 获取openId
  361. */
  362. public static function getOpenId($code, $merchant, $ptStyle)
  363. {
  364. $appId = $merchant['wxAppId'];
  365. $open = [];
  366. if ($ptStyle == 1) {
  367. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  368. }
  369. if ($ptStyle == 2) {
  370. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  371. }
  372. if ($ptStyle == 3) {
  373. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  374. }
  375. if (empty($open)) {
  376. util::fail('没有找到平台');
  377. }
  378. $component_app_id = $open['appId'];
  379. $componentAccessToken = wxUtil::getComponentAccessToken($open);
  380. $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}";
  381. $curl = new curl\Curl();
  382. $result = $curl->get($url);
  383. $data = Json::decode($result);
  384. if (isset($data['openid']) == false) {
  385. Yii::warning('error:' . json_encode($data) . ' code:' . $code, __METHOD__);
  386. return false;
  387. }
  388. return $data;
  389. }
  390. /**
  391. * 授权获取自己的信息
  392. */
  393. public function authGetUserInfo($openid, $access_token)
  394. {
  395. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid";
  396. $curl = new curl\Curl();
  397. $result = $curl->get($url);
  398. $data = Json::decode($result);
  399. return $data;
  400. }
  401. /**
  402. * 自动获取自己的信息(公众号的粉丝才能获取)
  403. */
  404. public static function autoGetUserInfo($openid, $merchant, $ptStyle)
  405. {
  406. $access_token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  407. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
  408. $curl = new curl\Curl();
  409. $result = $curl->get($url);
  410. $data = Json::decode($result);
  411. return $data;
  412. }
  413. /**
  414. * 网址接入验证
  415. */
  416. public static function signatureValidation()
  417. {
  418. $signature = $_GET['signature'];
  419. $timestamp = $_GET['timestamp'];
  420. $nonce = $_GET['nonce'];
  421. $account = $_GET['account'];
  422. $merchant = xhMerchantService::getByAccount($account);
  423. if ($merchant) {
  424. $token = $merchant['wxToken'];
  425. // 3个数值组成数组验证
  426. $tmpArr = array(
  427. $token,
  428. $timestamp,
  429. $nonce
  430. );
  431. // 字典排序
  432. sort($tmpArr, SORT_STRING);
  433. $tmpStr = implode($tmpArr);
  434. // sha1加密
  435. $tmpStr = sha1($tmpStr);
  436. if ($signature == $tmpStr) {
  437. return true;
  438. } else {
  439. return false;
  440. }
  441. } else {
  442. return false;
  443. }
  444. }
  445. //客服接口发消息
  446. public static function sendMsg($content, $merchant, $openId, $ptStyle)
  447. {
  448. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  449. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  450. $data = ['touser' => $openId, 'msgtype' => 'text', 'text' => ['content' => $content]];
  451. $curl = new curl\Curl();
  452. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  453. $result = Json::decode($result);
  454. return $result;
  455. }
  456. public static function sendImgMsg($mediaId, $merchant, $openId, $ptStyle)
  457. {
  458. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  459. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  460. $data = ['touser' => $openId, 'msgtype' => 'image', 'image' => ['media_id' => $mediaId]];
  461. $curl = new curl\Curl();
  462. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  463. $result = Json::decode($result);
  464. return $result;
  465. }
  466. public static function sendVideoMsg($mediaId, $merchant, $openId, $ptStyle)
  467. {
  468. $token = self::getMiniProgramAccessToken($merchant, $ptStyle);
  469. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  470. $data = ['touser' => $openId, 'msgtype' => 'video', 'video' => ['media_id' => $mediaId,
  471. 'thumb_media_id' => $mediaId,
  472. 'title' => '因爱而生',
  473. 'description' => "情定原生,爱在今生;\n坐标:浙江,绍兴上虞;\n看到过一句话:“一个城市不过是几条巷道上,几间房子和几个人的组合。如果没有了这些,一个城市如同陨落,只剩下悲凉的记忆;”\n万事万物持续流变,珍爱的东西尤其不可能一直存在,如朝霞,如春花,如爱情;\n无论什么时候,不管天气好坏,都能拿出某些个让人喜欢的东西。",
  474. ]];
  475. $curl = new curl\Curl();
  476. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  477. $result = Json::decode($result);
  478. return $result;
  479. }
  480. /**
  481. * 消息模板,发送任务通知
  482. * @param unknown $data
  483. * @return mixed
  484. */
  485. public static function sendTaskInform($data, $merchant, $ptStyle)
  486. {
  487. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  488. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$accessToken}";
  489. $data = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  490. $curl = new curl\Curl();
  491. $response = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  492. $arr = json_decode($response);
  493. return $arr;
  494. }
  495. /**
  496. * 创建模板消息
  497. * @param unknown $accessToken
  498. * @param unknown $template_id_short
  499. * @return unknown
  500. */
  501. public static function tMessageCreate($merchant, $shortTemplateId, $ptStyle)
  502. {
  503. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  504. $url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token={$accessToken}";
  505. $post = array('template_id_short' => $shortTemplateId);
  506. $curl = new curl\Curl();
  507. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  508. $result = Json::decode($result);
  509. return $result;
  510. }
  511. /**
  512. * 删除模板消息
  513. */
  514. public static function delTMessage($merchant, $templateId, $ptStyle)
  515. {
  516. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  517. $url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token={$accessToken}";
  518. $post = array('template_id' => $templateId);
  519. $curl = new curl\Curl();
  520. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  521. $result = Json::decode($result);
  522. return $result;
  523. }
  524. /**
  525. * 取所有模板消息
  526. */
  527. public function getAllTMessage($merchant, $ptStyle)
  528. {
  529. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  530. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token={$accessToken}";
  531. $curl = new curl\Curl();
  532. $result = $curl->get($url);
  533. $result = Json::decode($result);
  534. return $result;
  535. }
  536. /**
  537. * 设置模板消息的行业
  538. */
  539. public function setTMIndustry($merchant, $industry_id1, $industry_id2, $ptStyle)
  540. {
  541. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  542. $url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token={$accessToken}";
  543. $post = array('industry_id1' => $industry_id1, 'industry_id2' => $industry_id2);
  544. $curl = new curl\Curl();
  545. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  546. $result = Json::decode($result);
  547. return $result;
  548. }
  549. /**
  550. * 获取公众号的accessToken 公众号调用凭据
  551. */
  552. public static function getAuthorizerAccessToken($merchant, $ptStyle = 0)
  553. {
  554. $sjId = $merchant['id'];
  555. $open = [];
  556. if ($ptStyle == 1) {
  557. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  558. }
  559. if ($ptStyle == 2) {
  560. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  561. }
  562. if ($ptStyle == 3) {
  563. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  564. }
  565. if (empty($open)) {
  566. util::fail('没有找到平台');
  567. }
  568. $component_app_id = $open['appId'];
  569. $componentAccessToken = wxUtil::getComponentAccessToken($open);
  570. $appId = $merchant['wxAppId'];
  571. $wxAccessToken = $merchant['wxAccessToken'];
  572. $wxAccessTokenTime = $merchant['wxAccessTokenTime'];
  573. $authorize_refresh_token = $merchant['wxRefreshToken'];
  574. $now = time();
  575. if ($now > strtotime($wxAccessTokenTime)) {
  576. $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
  577. $data = ['component_appid' => $component_app_id, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $authorize_refresh_token];
  578. $curl = new curl\Curl();
  579. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  580. $result = Json::decode($result);
  581. if (isset($result['authorizer_access_token'])) {
  582. $authorize_access_token = $result['authorizer_access_token'];
  583. $expires_in = $result['expires_in'];
  584. $authorize_refresh_token = $result['authorizer_refresh_token'];
  585. $uData['wxRefreshToken'] = $authorize_refresh_token;;
  586. $uData['wxAccessToken'] = $authorize_access_token;
  587. $uData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  588. if ($ptStyle == 1) {
  589. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  590. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  591. if (!empty($wxBaseId)) {
  592. WxBaseService::updateById($wxBaseId, $uData);
  593. }
  594. } elseif ($ptStyle == 2) {
  595. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  596. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  597. if (!empty($wxBaseId)) {
  598. WxBaseService::updateById($wxBaseId, $uData);
  599. }
  600. } elseif ($ptStyle == 3) {
  601. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  602. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  603. if (!empty($wxBaseId)) {
  604. WxBaseService::updateById($wxBaseId, $uData);
  605. }
  606. } else {
  607. xhMerchantService::updateById($sjId, $uData);
  608. }
  609. return $authorize_access_token;
  610. }
  611. return;
  612. } else {
  613. return $wxAccessToken;
  614. }
  615. }
  616. /**
  617. * 获取小程序的accessToken
  618. */
  619. public static function getMiniProgramAccessToken($merchant, $ptStyle = 0)
  620. {
  621. $sjId = isset($merchant['id']) ? $merchant['id'] : 0;
  622. //$id = dict::getDict('openId');
  623. $open = [];
  624. if ($ptStyle == 1) {
  625. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  626. }
  627. if ($ptStyle == 2) {
  628. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  629. }
  630. if ($ptStyle == 3) {
  631. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  632. }
  633. if (empty($open)) {
  634. util::fail('没有找到平台信息');
  635. }
  636. $component_app_id = $open['appId'];
  637. $componentAccessToken = wxUtil::getComponentAccessToken($open);
  638. $appId = $merchant['miniAppId'];
  639. if ($ptStyle == 1) {
  640. $miniAccessToken = $merchant['miniAccessToken'];
  641. $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
  642. $miniRefreshToken = $merchant['miniRefreshToken'];
  643. } elseif ($ptStyle == 2) {
  644. $miniAccessToken = $merchant['miniAccessToken'];
  645. $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
  646. $miniRefreshToken = $merchant['miniRefreshToken'];
  647. } elseif ($ptStyle == 3) {
  648. $miniAccessToken = $merchant['miniAccessToken'];
  649. $miniAccessTokenTime = $merchant['miniAccessTokenTime'];
  650. $miniRefreshToken = $merchant['miniRefreshToken'];
  651. } else {
  652. $extend = xhMerchantExtendService::getBySjId($sjId);
  653. $miniAccessToken = $extend['miniAccessToken'];
  654. $miniAccessTokenTime = $extend['miniAccessTokenTime'];
  655. $miniRefreshToken = $extend['miniRefreshToken'];
  656. }
  657. $now = time();
  658. if ($now > strtotime($miniAccessTokenTime)) {
  659. $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$componentAccessToken}";
  660. $data = ['component_appid' => $component_app_id, 'authorizer_appid' => $appId, 'authorizer_refresh_token' => $miniRefreshToken];
  661. $curl = new curl\Curl();
  662. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  663. $result = Json::decode($result);
  664. Yii::info('重新获取mini accesstoken:' . json_encode($result));
  665. if (isset($result['authorizer_access_token'])) {
  666. $expires_in = $result['expires_in'];
  667. $miniAccessToken = $result['authorizer_access_token'];
  668. $uData['miniRefreshToken'] = $result['authorizer_refresh_token'];
  669. $uData['miniAccessToken'] = $miniAccessToken;
  670. $uData['miniAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  671. if ($ptStyle == 1) {
  672. //零售
  673. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  674. if (!empty($wxMiniBaseId)) {
  675. WxMiniBaseService::updateById($wxMiniBaseId, $uData);
  676. }
  677. } elseif ($ptStyle == 2) {
  678. //供货商
  679. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  680. if (!empty($wxMiniBaseId)) {
  681. WxMiniBaseService::updateById($wxMiniBaseId, $uData);
  682. }
  683. } elseif ($ptStyle == 3) {
  684. //商城
  685. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  686. if (!empty($wxMiniBaseId)) {
  687. WxMiniBaseService::updateById($wxMiniBaseId, $uData);
  688. }
  689. } else {
  690. //其它
  691. xhMerchantExtendService::updateBySjId($sjId, $uData);
  692. }
  693. return $miniAccessToken;
  694. }
  695. Yii::info('accessToken失效:sjId:' . $sjId);
  696. }
  697. return $miniAccessToken;
  698. }
  699. public static function saveWeixinImg($userId, $url, $class = 0)
  700. {
  701. $className = [
  702. '0' => 'weixinUserAvatar',//微信头像
  703. '1' => 'weixinUserImg',//微信公众号二维码
  704. '2' => 'weixinMpQrcode',
  705. '3' => 'weixinMpAvatar',
  706. ];
  707. $folder = Yii::getAlias('@webroot') . '/../../images';
  708. $pre = '/' . $className[$class] . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  709. if (!file_exists($folder . $pre))
  710. mkdir($folder . $pre, 0777, true);
  711. $randString = stringUtil::buildOrderNo();
  712. $name = md5($userId . $randString) . '.jpg';
  713. $fullFileName = $folder . $pre . $name;
  714. $fileName = $pre . $name;
  715. $curl = new curl\Curl();
  716. $result = $curl->get($url);
  717. $fp2 = @fopen($fullFileName, 'a');//文件大小
  718. fwrite($fp2, $result);
  719. fclose($fp2);
  720. return $fileName;
  721. }
  722. /**
  723. * 下载学员作品图片
  724. */
  725. public static function downloadStudentWorksImg($merchant, $mediaId, $savePathType = 'wxUserImg', $ptStyle = 1)
  726. {
  727. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  728. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
  729. $ch = curl_init($url);
  730. curl_setopt($ch, CURLOPT_HEADER, 0);
  731. curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
  732. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  733. $body = curl_exec($ch);
  734. $httpinfo = curl_getinfo($ch);
  735. curl_close($ch);
  736. preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
  737. $fileExt = $extmatches[1];
  738. $imgSavePath = dict::getDict('imgSavePath');
  739. $comImgSavePath = $imgSavePath[$savePathType];
  740. $folder = Yii::getAlias('@webroot') . '/../../images';
  741. $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  742. if (!file_exists($folder . $pre)) {
  743. mkdir($folder . $pre, 0777, true);
  744. }
  745. $randString = stringUtil::buildOrderNo();
  746. $fullFileName = $folder . $pre . $randString . '.jpg';
  747. $fileName = $pre . $randString . '.jpg';
  748. $fp2 = @fopen($fullFileName, 'a');//文件大小
  749. fwrite($fp2, $body);
  750. fclose($fp2);
  751. $srcImg = $fullFileName;
  752. $dstImg300 = $folder . $pre . $randString . "_300.jpg";//生成300px缩略图,不保存到数据库
  753. util::img2thumb($srcImg, $dstImg300, $width = 300, 0, 0, 0);
  754. $dstImg100 = $folder . $pre . $randString . "_100.jpg";//生成50px缩略图,不保存到数据库
  755. util::img2thumb($srcImg, $dstImg100, $width = 100, 0, 0, 0);
  756. return $fileName;
  757. }
  758. /**
  759. * 获取media_id里的图片保存到服务器
  760. */
  761. public static function downloadmediaIdImg($merchant, $mediaId, $savePathType = 'wxUserImg', $ptStyle)
  762. {
  763. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  764. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id={$mediaId}";
  765. $ch = curl_init($url);
  766. curl_setopt($ch, CURLOPT_HEADER, 0);
  767. curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
  768. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  769. $body = curl_exec($ch);
  770. $httpinfo = curl_getinfo($ch);
  771. curl_close($ch);
  772. preg_match('/\w\/(\w+)/i', $httpinfo["content_type"], $extmatches);//求出文件格式
  773. $fileExt = $extmatches[1];
  774. $imgSavePath = dict::getDict('imgSavePath');
  775. $comImgSavePath = $imgSavePath[$savePathType];
  776. $folder = Yii::getAlias('@webroot') . '/../../images';
  777. $pre = '/' . $comImgSavePath . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  778. if (!file_exists($folder . $pre)) {
  779. mkdir($folder . $pre, 0777, true);
  780. }
  781. $randString = stringUtil::buildOrderNo();
  782. $fullFileName = $folder . $pre . $randString . '.jpg';
  783. $fileName = $pre . $randString . '.jpg';
  784. $fp2 = @fopen($fullFileName, 'a');//文件大小
  785. fwrite($fp2, $body);
  786. fclose($fp2);
  787. $srcImg = $fullFileName;
  788. $mediumImg = $pre . $randString . "_200.jpg";
  789. $dstImg200 = $folder . $mediumImg;//生成200px缩略图,不保存到数据库
  790. util::img2thumb($srcImg, $dstImg200, $width = 200, 0, 0, 0);
  791. $smallImg = $pre . $randString . "_50.jpg";
  792. $dstImg50 = $folder . $smallImg;//生成50px缩略图,不保存到数据库
  793. util::img2thumb($srcImg, $dstImg50, $width = 50, 0, 0, 0);
  794. return ['large' => $fileName, 'medium' => $mediumImg, 'small' => $smallImg];
  795. }
  796. public static function createTag($merchant, $name, $ptStyle)
  797. {
  798. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  799. $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token={$accessToken}";
  800. $post = ['tag' => ['name' => $name]];
  801. $curl = new curl\Curl();
  802. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  803. $result = Json::decode($result);
  804. return $result;
  805. }
  806. public static function delTag($merchant, $id, $ptStyle)
  807. {
  808. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  809. $url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={$accessToken}";
  810. $post = ['tag' => ['id' => $id]];
  811. $curl = new curl\Curl();
  812. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  813. $result = Json::decode($result);
  814. return $result;
  815. }
  816. public static function modTag($merchant, $id, $name, $ptStyle)
  817. {
  818. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  819. $url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token={$accessToken}";
  820. $post = ['tag' => ['id' => $id, 'name' => $name]];
  821. $curl = new curl\Curl();
  822. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  823. $result = Json::decode($result);
  824. return $result;
  825. }
  826. /**
  827. * 获取商家所有标签
  828. */
  829. public static function getTag($merchant, $ptStyle)
  830. {
  831. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  832. $url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token={$accessToken}";
  833. $curl = new curl\Curl();
  834. $result = $curl->get($url);
  835. $result = Json::decode($result);
  836. return $result;
  837. }
  838. public function getUserTag($tagId, $nextOpenId = '')
  839. {
  840. $url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=" . $this->access_token;
  841. $post = ['tagid' => $tagId, 'next_openid' => $nextOpenId];
  842. $curl = new curl\Curl();
  843. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  844. $result = Json::decode($result);
  845. return $result;
  846. }
  847. /**
  848. * 批量为用户打标签
  849. */
  850. public static function membersBatchTag($merchant, $openIdList, $tagId, $ptStyle)
  851. {
  852. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  853. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$accessToken}";
  854. $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
  855. $curl = new curl\Curl();
  856. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  857. $result = Json::decode($result);
  858. return $result;
  859. }
  860. /**
  861. * 批量取消用户标签
  862. */
  863. public static function membersCancelTag($merchant, $openIdList, $tagId, $ptStyle)
  864. {
  865. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  866. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token={$accessToken}";
  867. $post = ['openid_list' => $openIdList, 'tagid' => $tagId];
  868. $curl = new curl\Curl();
  869. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($post))->post($url);
  870. $result = Json::decode($result);
  871. return $result;
  872. }
  873. /**
  874. * 新增临时素材
  875. */
  876. public static function uploadTempMedia($merchant, $fullPathImg, $ptStyle, $type = 'image')
  877. {
  878. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  879. $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$accessToken}&type=" . $type;
  880. $ch = curl_init();
  881. if (class_exists('CURLFile')) {// 这里用特性检测判断php版本
  882. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
  883. $postData = array('media' => new \CURLFile(realpath($fullPathImg)));//>=5.5
  884. } else {
  885. if (defined('CURLOPT_SAFE_UPLOAD')) {
  886. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
  887. $postData = array('media' => '@' . realpath($fullPathImg));//<=5.5
  888. }
  889. }
  890. curl_setopt($ch, CURLOPT_URL, $url);
  891. curl_setopt($ch, CURLOPT_POST, 1);
  892. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  893. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  894. $response = curl_exec($ch);
  895. $result = Json::decode($response);
  896. return $result;
  897. }
  898. /**
  899. * 获取临时素材
  900. */
  901. public static function getTempMaterial($merchant, $mediaId, $ptStyle)
  902. {
  903. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  904. $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$accessToken}&media_id=" . $mediaId;
  905. $fileInfo = self::getTempMaterialInfo($url);
  906. if (empty($fileInfo) || isset($fileInfo['errcode'])) {
  907. $msg = '未错误哦~';
  908. return ['code' => 'A0002', 'msg' => $msg];
  909. }
  910. $fileName = substr(md5($mediaId), -4) . stringUtil::buildOrderNo() . '.amr';
  911. $preFolder = Yii::getAlias('@webroot') . '/../../images';
  912. $folder = '/weixinChatVoice/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  913. if (!file_exists($preFolder . $folder))
  914. mkdir($preFolder . $folder, 0777, true);
  915. $saveFile = $preFolder . $folder . $fileName;
  916. if (file_exists($saveFile) == false) {
  917. self::readTempMaterial($saveFile, $fileInfo["body"]);
  918. }
  919. return ['code' => 'A0001', 'msg' => 'success', 'data' => ['url' => $folder . $fileName]];
  920. }
  921. /**
  922. * 获取临时素材的头信息等
  923. */
  924. public static function getTempMaterialInfo($url)
  925. {
  926. $ch = curl_init($url);
  927. curl_setopt($ch, CURLOPT_HEADER, 0);
  928. curl_setopt($ch, CURLOPT_NOBODY, 0);//只取body头
  929. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  930. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  931. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  932. $package = curl_exec($ch);
  933. $httpinfo = curl_getinfo($ch);
  934. curl_close($ch);
  935. $imageAll = ['header' => $httpinfo, 'body' => $package];
  936. return $imageAll;
  937. }
  938. /**
  939. * 读取临时素材的资源
  940. */
  941. function readTempMaterial($filename, $fileContent)
  942. {
  943. $localFile = fopen($filename, 'w');
  944. if (false !== $localFile) {
  945. if (false !== fwrite($localFile, $fileContent)) {
  946. fclose($localFile);
  947. }
  948. }
  949. }
  950. /**
  951. * 创建门店
  952. */
  953. public function createStore($info)
  954. {
  955. $jsonData = urldecode(json_encode($info, JSON_UNESCAPED_UNICODE));
  956. $url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token=" . $this->access_token;
  957. $curl = new curl\Curl();
  958. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  959. }
  960. /**
  961. * 查询门店信息
  962. */
  963. public function getStoreInfo($poi_id)
  964. {
  965. $data = ['poi_id' => $poi_id];
  966. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  967. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoi?access_token=" . $this->access_token;
  968. $curl = new curl\Curl();
  969. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  970. }
  971. /**
  972. * 查询门店列表
  973. */
  974. public function getStoreList($begin = 0, $count = 20)
  975. {
  976. $data = ["begin" => $begin, "limit" => $count];
  977. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  978. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
  979. $curl = new curl\Curl();
  980. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  981. }
  982. /**
  983. * 查询门店列表 -- 重复 可删除
  984. */
  985. function getShopList($data)
  986. {
  987. $url = "https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=" . $this->access_token;
  988. $curl = new curl\Curl();
  989. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  990. $result = Json::decode($result);
  991. return $result;
  992. }
  993. /**
  994. * 修改门店服务信息
  995. */
  996. public function updateStore(array $buffer)
  997. {
  998. $jsonData = urldecode(json_encode($buffer, JSON_UNESCAPED_UNICODE));
  999. $url = "http://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token=" . $this->access_token;
  1000. $curl = new curl\Curl();
  1001. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  1002. }
  1003. /**
  1004. * 删除门店
  1005. * @param $poi_id
  1006. * @return mixed
  1007. */
  1008. public function delStore($poi_id)
  1009. {
  1010. $data = ['poi_id' => $poi_id];
  1011. $jsonData = urldecode(json_encode($data, JSON_UNESCAPED_UNICODE));
  1012. $url = "http://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token=" . $this->access_token;
  1013. $curl = new curl\Curl();
  1014. return $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
  1015. }
  1016. /**
  1017. * 门店类目表
  1018. */
  1019. public function storeTypeList()
  1020. {
  1021. $url = "http://api.weixin.qq.com/cgi-bin/poi/getwxcategory?access_token=" . $this->access_token;
  1022. $curl = new curl\Curl();
  1023. return $curl->get($url);
  1024. }
  1025. /******************************* wifi *************************************/
  1026. /**
  1027. * 获取wifi门店列表
  1028. */
  1029. function getShopWifiList($pageindex = 1, $pagesize = 10)
  1030. {
  1031. $data = ["pageindex" => $pageindex, "pagesize" => $pagesize];
  1032. $url = "https://api.weixin.qq.com/bizwifi/shop/list?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 $shopId 门店ID
  1041. * @param $ssid 无线网络设备的ssid
  1042. * @param $pwd 无线网络设备的密码
  1043. * @return mixed
  1044. */
  1045. function addPasswordDevice($shopId, $ssid, $pwd)
  1046. {
  1047. $data = ["shop_id" => $shopId, "ssid" => $ssid, "password" => $pwd];
  1048. $url = "https://api.weixin.qq.com/bizwifi/device/add?access_token=" . $this->access_token;
  1049. $curl = new curl\Curl();
  1050. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1051. $result = Json::decode($result);
  1052. return $result;
  1053. }
  1054. /**
  1055. * 修改门店Wi-Fi(修改门店网络信息)
  1056. * @param $shopId
  1057. * @param $oldSsid
  1058. * @param $ssid
  1059. * @param string $pwd
  1060. * @return mixed
  1061. */
  1062. function updateDevice($shopId, $oldSsid, $ssid, $pwd = '')
  1063. {
  1064. $data = ["shop_id" => $shopId, "old_ssid" => $oldSsid, "ssid" => $ssid];
  1065. if (!empty($pwd)) {
  1066. $data["password"] = $pwd;
  1067. }
  1068. $url = "https://api.weixin.qq.com/bizwifi/shop/update?access_token=" . $this->access_token;
  1069. $curl = new curl\Curl();
  1070. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1071. $result = Json::decode($result);
  1072. return $result;
  1073. }
  1074. /**
  1075. * 删除密码型设备
  1076. * @param $bssid 无线网络设备无线mac地址
  1077. * @return mixed
  1078. */
  1079. function delPasswordDevice($bssid)
  1080. {
  1081. $data = ["bssid" => $bssid];
  1082. $url = "https://api.weixin.qq.com/bizwifi/device/delete?access_token=" . $this->access_token;
  1083. $curl = new curl\Curl();
  1084. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1085. $result = Json::decode($result);
  1086. return $result;
  1087. }
  1088. /**
  1089. * 查询门店Wi-Fi信息
  1090. * @param $shopId 门店ID
  1091. * @return mixed
  1092. */
  1093. function getShopWifi($shopId)
  1094. {
  1095. $data = ["shop_id" => $shopId];
  1096. $url = "https://api.weixin.qq.com/bizwifi/shop/get?access_token=" . $this->access_token;
  1097. $curl = new curl\Curl();
  1098. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1099. $result = Json::decode($result);
  1100. return $result;
  1101. }
  1102. /**
  1103. * 获取物料二维码
  1104. * @param $shopId 门店ID
  1105. * @param $ssid 无线网络设备的ssid
  1106. * @param $img_id 物料样式编号(0-纯二维码,可用于自由设计宣传材料;1-二维码物料,155mm×215mm(宽×高),可直接张贴)
  1107. * @return mixed
  1108. */
  1109. function getWifiQrCode($shopId, $ssid, $img_id)
  1110. {
  1111. //$data = ["shop_id" => 429620, "ssid" => "WX567", "img_id" => $img_id];
  1112. $data = ["shop_id" => $shopId, "ssid" => $ssid, "img_id" => $img_id];
  1113. $url = "https://api.weixin.qq.com/bizwifi/qrcode/get?access_token=" . $this->access_token;
  1114. $curl = new curl\Curl();
  1115. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1116. $result = Json::decode($result);
  1117. return $result;
  1118. }
  1119. function getWifiUrl()
  1120. {
  1121. $url = "https://api.weixin.qq.com/bizwifi/account/get_connecturl?access_token=" . $this->access_token;
  1122. $curl = new curl\Curl();
  1123. $result = $curl->get($url);
  1124. $result = Json::decode($result);
  1125. return $result;
  1126. }
  1127. function generateShortUrl($longUrl)
  1128. {
  1129. $data = ["action" => "long2short", "long_url" => $longUrl,];
  1130. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $this->access_token;
  1131. $curl = new curl\Curl();
  1132. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1133. $result = Json::decode($result);
  1134. if (isset($result['errcode']) && $result['errcode'] == 0) {
  1135. return ['code' => 'A0001', 'data' => ['shortUrl' => $result['short_url']]];
  1136. }
  1137. return ['code' => 'A0002', 'data' => []];
  1138. }
  1139. //长链接转短链接 ssh 2019.9.26
  1140. public static function urlLongToShort($longUrl, $merchant)
  1141. {
  1142. $accessToken = self::getMiniProgramAccessToken($merchant);
  1143. $data = ["action" => "long2short", "long_url" => $longUrl,];
  1144. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" . $accessToken;
  1145. $curl = new curl\Curl();
  1146. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1147. $result = Json::decode($result);
  1148. $url = '';
  1149. if (isset($result['errcode']) && $result['errcode'] == 0) {
  1150. $url = $result['short_url'];
  1151. }
  1152. return ['url' => $url];
  1153. }
  1154. /**
  1155. * 拉取门店所有的设备信息
  1156. * @return array
  1157. */
  1158. public function getShopWifiListAll()
  1159. {
  1160. $startPage = 1;
  1161. $list = [];
  1162. while (true) {
  1163. $reList = $this->getShopWifiList($startPage, 20);
  1164. if ($startPage <= $reList['data']['pagecount']) {
  1165. $list = array_merge($list, $reList['data']['records']);
  1166. ++$startPage;
  1167. } else {
  1168. break;
  1169. }
  1170. }
  1171. return $list;
  1172. }
  1173. /**
  1174. * 通过 poi_id 从Wi-Fi门店列表中 获取 shopId
  1175. * @param $poiId
  1176. * @return bool
  1177. */
  1178. public function getShopId($poiId, $list = [])
  1179. {
  1180. if (empty($list)) {
  1181. $list = $this->getShopWifiListAll();
  1182. }
  1183. foreach ($list as $shop) {
  1184. if ($shop['poi_id'] == $poiId)
  1185. return (string)$shop['shop_id'];
  1186. }
  1187. return false;
  1188. }
  1189. //设置小程序服务器域名
  1190. public static function setDomain($merchant, $ptStyle = 0)
  1191. {
  1192. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1193. $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $accessToken;
  1194. $curl = new curl\Curl();
  1195. $mallHost = Yii::$app->params['mallHost'];
  1196. if ($ptStyle == 1) {
  1197. $mallHost = Yii::$app->params['hdHost'];
  1198. }
  1199. $has = strpos($mallHost, 'https');
  1200. //如果不是https则换成https
  1201. if ($has === false) {
  1202. $mallHost = str_replace('http', 'https', $mallHost);
  1203. }
  1204. $data = [
  1205. "action" => "set",
  1206. "requestdomain" => [$mallHost],
  1207. "wsrequestdomain" => [$mallHost],
  1208. "uploaddomain" => [$mallHost],
  1209. "downloaddomain" => [$mallHost],
  1210. ];
  1211. print_r($data);
  1212. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1213. $respond = Json::decode($result);
  1214. print_r($respond);
  1215. }
  1216. //设置业务域名
  1217. public static function setWebViewDomain($merchant, $ptStyle = 0)
  1218. {
  1219. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1220. $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $accessToken;
  1221. $curl = new curl\Curl();
  1222. $mallHost = Yii::$app->params['mallHost'];
  1223. if ($ptStyle == 1) {
  1224. $mallHost = Yii::$app->params['hdHost'];
  1225. }
  1226. $has = strpos($mallHost, 'https');
  1227. //如果不是https则换成https
  1228. if ($has === false) {
  1229. $mallHost = str_replace('http', 'https', $mallHost);
  1230. }
  1231. $data = [
  1232. "action" => "set",
  1233. "webviewdomain" => [$mallHost],
  1234. ];
  1235. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1236. $respond = Json::decode($result);
  1237. print_r($respond);
  1238. }
  1239. //为授权的小程序帐号上传小程序代码
  1240. public static function miniCommit($merchant, $templateId, $ptStyle = 0)
  1241. {
  1242. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1243. $url = 'https://api.weixin.qq.com/wxa/commit?access_token=' . $accessToken;
  1244. $curl = new curl\Curl();
  1245. $apiUrl = Yii::$app->params['mallHost'];
  1246. if ($ptStyle == 1) {
  1247. $apiUrl = Yii::$app->params['hdHost'];
  1248. }
  1249. $imgUrl = imgUtil::getPrefix();
  1250. //如果不是https则换成https
  1251. $has = strpos($apiUrl, 'https');
  1252. if ($has === false) {
  1253. $apiUrl = str_replace('http', 'https', $apiUrl);
  1254. }
  1255. //如果不是https则换成https
  1256. $has = strpos($imgUrl, 'https');
  1257. if ($has === false) {
  1258. $imgUrl = str_replace('http', 'https', $imgUrl);
  1259. }
  1260. $account = isset($merchant['id']) ? $merchant['id'] : 0;
  1261. $name = isset($merchant['name']) ? $merchant['name'] : '';
  1262. $open = [];
  1263. if ($ptStyle == 1) {
  1264. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  1265. }
  1266. if ($ptStyle == 2) {
  1267. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  1268. }
  1269. if ($ptStyle == 3) {
  1270. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  1271. }
  1272. if (empty($open)) {
  1273. util::fail('没有找到平台');
  1274. }
  1275. $name = isset($open['name']) ? $open['name'] : '';
  1276. $ext = [
  1277. 'extAppid' => $merchant['miniAppId'],
  1278. 'ext' => [
  1279. 'account' => $account,
  1280. 'apiHost' => $apiUrl,
  1281. 'socketApiHost' => str_replace("https://", "", $apiUrl),
  1282. 'imgHost' => $imgUrl,
  1283. 'name' => $name,
  1284. ],
  1285. ];
  1286. echo '<pre>';
  1287. print_r($ext);
  1288. Yii::info("ext:" . json_encode($ext));
  1289. $extJson = json_encode($ext);
  1290. $data = [
  1291. "template_id" => $templateId,
  1292. "ext_json" => $extJson,
  1293. "user_version" => date("mdHis"),
  1294. "user_desc" => date("mdHis")
  1295. ];
  1296. print_r($data);
  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 getExperienceQrCode($merchant, $page, $ptStyle = 0)
  1303. {
  1304. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1305. $path = urlencode($page);
  1306. $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token={$accessToken}&path=" . $path;
  1307. $curl = new curl\Curl();
  1308. $result = $curl->get($url);
  1309. $file = dirUtil::getImgDir() . 'retail/miniExperience/' . $merchant['id'] . '.jpg';
  1310. file_put_contents($file, $result);
  1311. echo "<b /><img src='" . imgUtil::getPrefix() . "/retail/miniExperience/" . $merchant['id'] . ".jpg' />";
  1312. }
  1313. //获取授权小程序帐号已设置的类目
  1314. public static function getMiniCategory($merchant, $ptStyle = 0)
  1315. {
  1316. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1317. $url = "https://api.weixin.qq.com/wxa/get_category?access_token=" . $accessToken;
  1318. $curl = new curl\Curl();
  1319. $result = $curl->get($url);
  1320. $respond = Json::decode($result);
  1321. echo "<pre>";
  1322. print_r($respond);
  1323. }
  1324. //小程序支付成功后获取唯一ID ssh 2020.5.12
  1325. public static function getPaidUnionId($merchant, $miniOpenId, $mcId, $orderId, $ptStyle = 0)
  1326. {
  1327. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1328. $url = "https://api.weixin.qq.com/wxa/getpaidunionid?access_token={$accessToken}&openid={$miniOpenId}&mch_id={$mcId}&out_trade_no=" . (string)$orderId;
  1329. $curl = new curl\Curl();
  1330. $result = $curl->get($url);
  1331. $respond = Json::decode($result);
  1332. $unionId = isset($respond['unionid']) && !empty($respond['unionid']) ? $respond['unionid'] : '';
  1333. return $unionId;
  1334. }
  1335. //小程序加急审核 ssh 2020.4.20
  1336. public static function speedupAudit($merchant, $id, $ptStyle = 0)
  1337. {
  1338. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1339. $url = "https://api.weixin.qq.com/wxa/speedupaudit?access_token=" . $accessToken;
  1340. $curl = new curl\Curl();
  1341. $data = [
  1342. "auditid" => $id
  1343. ];
  1344. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1345. $respond = Json::decode($result);
  1346. print_r($respond);
  1347. }
  1348. //获取小程序的第三方提交代码的页面配置
  1349. public static function getMiniPage($merchant, $ptStyle = 0)
  1350. {
  1351. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1352. $url = "https://api.weixin.qq.com/wxa/get_page?access_token=" . $accessToken;
  1353. $curl = new curl\Curl();
  1354. $result = $curl->get($url);
  1355. $respond = Json::decode($result);
  1356. echo "<pre>";
  1357. print_r($respond);
  1358. }
  1359. //将第三方提交的代码包提交审核
  1360. public static function miniSubmitAudit($merchant, $page, $ptStyle = 0)
  1361. {
  1362. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1363. $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $accessToken;
  1364. $curl = new curl\Curl();
  1365. if ($ptStyle == 1) {
  1366. $data = [
  1367. "item_list" => [
  1368. [
  1369. "address" => $page,
  1370. "tag" => "花卉宝 花店",
  1371. "first_class" => "IT科技",
  1372. "second_class" => "软件服务提供商",
  1373. "first_id" => 210,
  1374. "second_id" => 413,
  1375. "title" => "花卉宝",
  1376. ],
  1377. ],
  1378. "feedback_info" => "",
  1379. "feedback_stuff" => "",
  1380. ];
  1381. } else {
  1382. $data = [
  1383. "item_list" => [
  1384. [
  1385. "address" => $page,
  1386. "tag" => "鲜花 花店",
  1387. "first_class" => "商家自营",
  1388. "second_class" => "鲜花/园艺/工艺品",
  1389. "first_id" => 304,
  1390. "second_id" => 323,
  1391. "title" => "商城",
  1392. ],
  1393. ],
  1394. "feedback_info" => "",
  1395. "feedback_stuff" => "",
  1396. ];
  1397. }
  1398. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1399. $respond = Json::decode($result);
  1400. echo "<pre>";
  1401. print_r($respond);
  1402. }
  1403. //查询最新一次提交的审核状态
  1404. public static function miniGetLatestAuditStatus($merchant, $ptStyle = 0)
  1405. {
  1406. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1407. $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $accessToken;
  1408. $curl = new curl\Curl();
  1409. $result = $curl->get($url);
  1410. $respond = Json::decode($result);
  1411. echo "<pre>";
  1412. print_r($respond);
  1413. }
  1414. //发布已通过审核的小程序
  1415. public static function miniRelease($merchant, $ptStyle = 0)
  1416. {
  1417. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1418. $url = "https://api.weixin.qq.com/wxa/release?access_token=" . $accessToken;
  1419. $curl = new curl\Curl();
  1420. $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
  1421. $respond = Json::decode($result);
  1422. return $respond;
  1423. }
  1424. //撤回审核
  1425. public static function rollbackCheck($merchant, $ptStyle = 0)
  1426. {
  1427. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1428. $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=" . $accessToken;
  1429. $curl = new curl\Curl();
  1430. $result = $curl->get($url);
  1431. $respond = Json::decode($result);
  1432. echo "<pre>";
  1433. print_r($respond);
  1434. }
  1435. //申请会员的小程序码
  1436. public static function generateMemberCode($merchant, $shopId, $ptStyle = 0)
  1437. {
  1438. $fileName = $merchant['id'] . '_' . $shopId . '_apply_member.jpg';
  1439. $behind = 'retail/mimiMemberCode/';
  1440. $filePath = dirUtil::getImgDir() . $behind;
  1441. if (!file_exists($filePath)) {
  1442. mkdir($filePath, 0777, true);
  1443. }
  1444. $file = $filePath . $fileName;
  1445. if (file_exists($file)) {
  1446. return $behind . $fileName;
  1447. }
  1448. $path = 'pages/home/user?scan=1&shopId=' . $shopId;
  1449. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1450. $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
  1451. $curl = new curl\Curl();
  1452. $data = [
  1453. "path" => $path,
  1454. "width" => 1000,
  1455. ];
  1456. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1457. file_put_contents($file, $result);
  1458. return $behind . $fileName;
  1459. }
  1460. public static function generateShortLink($merchant, $ptStyle = 0, $pageUrl, $pageTitle)
  1461. {
  1462. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1463. $url = "https://api.weixin.qq.com/wxa/genwxashortlink?access_token={$accessToken}";
  1464. $curl = new curl\Curl();
  1465. $data = [
  1466. "page_url" => $pageUrl,
  1467. "page_title" => $pageTitle,
  1468. 'is_permanent' => false,
  1469. ];
  1470. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1471. $result = Json::decode($result);
  1472. echo "<pre>";
  1473. print_r($result);
  1474. die;
  1475. }
  1476. //小程序码生成,永久有效,数量暂无限制 ssh 2020.4.17
  1477. public static function generateUnlimitedMiniCode($merchant, $page, $scene = '', $ptStyle = 0)
  1478. {
  1479. $id = isset($merchant['id']) ? $merchant['id'] : 0;
  1480. $fileName = md5($page . '_' . $scene) . '.jpg';
  1481. $behind = 'retail/mimiCode/' . date("Ym") . '/' . date("d") . '/' . $id . '/';
  1482. $filePath = dirUtil::getImgDir() . $behind;
  1483. if (!file_exists($filePath)) {
  1484. mkdir($filePath, 0777, true);
  1485. }
  1486. $file = $filePath . $fileName;
  1487. if (file_exists($file)) {
  1488. return $behind . $fileName;
  1489. }
  1490. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1491. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
  1492. $curl = new curl\Curl();
  1493. $data = [];
  1494. if (!empty($page)) {
  1495. $data["page"] = $page;
  1496. }
  1497. $data["scene"] = $scene;
  1498. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1499. Yii::info('获取小程序图片资源:' . json_encode($result));
  1500. file_put_contents($file, $result);
  1501. //上传oss
  1502. $obj = $behind . $fileName;
  1503. $obj = ltrim($obj, "/");
  1504. oss::uploadImage($obj, $file);
  1505. return $behind . $fileName;
  1506. }
  1507. //生成充值码,永久有效,数量暂无限制 ssh 20250307
  1508. public static function generateUnlimitedRechargeMiniCode($merchant, $page, $scene = '', $ptStyle = 0)
  1509. {
  1510. $id = isset($merchant['id']) ? $merchant['id'] : 0;
  1511. $fileName = md5($page . '_' . $scene) . '.jpg';
  1512. $behind = 'retail/rechargeCode/' . date("Ym") . '/' . date("d") . '/' . $id . '/';
  1513. $filePath = dirUtil::getImgDir() . $behind;
  1514. if (!file_exists($filePath)) {
  1515. mkdir($filePath, 0777, true);
  1516. }
  1517. $file = $filePath . $fileName;
  1518. if (file_exists($file)) {
  1519. return $behind . $fileName;
  1520. }
  1521. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1522. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
  1523. $curl = new curl\Curl();
  1524. $data = [];
  1525. if (!empty($page)) {
  1526. $data["page"] = $page;
  1527. }
  1528. $data["scene"] = $scene;
  1529. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1530. Yii::info('获取小程序图片资源:' . json_encode($result));
  1531. file_put_contents($file, $result);
  1532. //上传oss
  1533. $obj = $behind . $fileName;
  1534. $obj = ltrim($obj, "/");
  1535. oss::uploadImage($obj, $file);
  1536. return $behind . $fileName;
  1537. }
  1538. //微信开放平台获取component_access_token
  1539. public static function getComponentAccessToken($open)
  1540. {
  1541. $id = $open['id'];
  1542. $componentAccessToken = $open['componentAccessToken'];
  1543. $componentAccessTokenTime = $open['componentAccessTokenTime'];
  1544. $now = time();
  1545. if ($now > strtotime($componentAccessTokenTime)) {
  1546. $appId = $open['appId'];
  1547. $appSecret = $open['appSecret'];
  1548. $verifyTicket = $open['verifyTicket'];
  1549. $data = ['component_appid' => $appId, 'component_appsecret' => $appSecret, 'component_verify_ticket' => $verifyTicket];
  1550. $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
  1551. $curl = new curl\Curl();
  1552. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  1553. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1554. if (empty($result)) {
  1555. util::stop('没有取到ComponentAccessToken');
  1556. }
  1557. $result = Json::decode($result);
  1558. if (isset($result['component_access_token']) == false || empty($result['component_access_token'])) {
  1559. Yii::info('get component access token error:' . json_encode($result));
  1560. Yii::$app->end();
  1561. }
  1562. $component_access_token = $result['component_access_token'];
  1563. $expiresIn = $result['expires_in'];
  1564. $oData = [];
  1565. $oData['componentAccessToken'] = $component_access_token;
  1566. $oData['componentAccessTokenTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短
  1567. xhWxOpenService::updateById($id, $oData);
  1568. return $component_access_token;
  1569. } else {
  1570. return $componentAccessToken;
  1571. }
  1572. }
  1573. /**
  1574. * 微信开放平台获取预授权码pre_auth_code
  1575. */
  1576. public static function getPreAuthCode($open)
  1577. {
  1578. $id = $open['id'];
  1579. $preAuthCode = $open['preAuthCode'];
  1580. $preAuthCodeTime = $open['preAuthCodeTime'];
  1581. $now = time();
  1582. if ($now > strtotime($preAuthCodeTime)) {
  1583. $token = self::getComponentAccessToken($open);
  1584. $appId = $open['appId'];
  1585. $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token={$token}";
  1586. $curl = new curl\Curl();
  1587. $data = ['component_appid' => $appId];
  1588. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1589. $result = Json::decode($result);
  1590. Yii::info($result);
  1591. if (isset($result['pre_auth_code']) == false || empty($result['pre_auth_code'])) {
  1592. echo "没有取到pre_auth_code,原因:" . json_encode($result) . " appid:" . $appId . " token:" . $token;
  1593. Yii::$app->end();
  1594. }
  1595. $preAuthCode = $result['pre_auth_code'];
  1596. $expiresIn = $result['expires_in'];
  1597. $oData = [];
  1598. $oData['preAuthCode'] = $preAuthCode;
  1599. $oData['preAuthCodeTime'] = date("Y-m-d H:i:s", ($now + $expiresIn - 100));//安全起见,将过期时间设置得比微信里还短100秒
  1600. xhWxOpenService::updateById($id, $oData);
  1601. return $preAuthCode;
  1602. } else {
  1603. return $preAuthCode;
  1604. }
  1605. }
  1606. /**
  1607. * 微信开放平台使用授权码换取公众号的接口调用凭据和授权信息
  1608. * authorizer_access_token authorizer_refresh_token
  1609. */
  1610. public static function getAuthorizer($code, $ptStyle)
  1611. {
  1612. $open = [];
  1613. if ($ptStyle == 1) {
  1614. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  1615. }
  1616. if ($ptStyle == 2) {
  1617. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  1618. }
  1619. if ($ptStyle == 3) {
  1620. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  1621. }
  1622. if (empty($open)) {
  1623. util::fail('没有找到平台');
  1624. }
  1625. $appId = $open['appId'];
  1626. $accessToken = self::getComponentAccessToken($open);
  1627. $url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={$accessToken}";
  1628. $curl = new curl\Curl();
  1629. $data = ['authorization_code' => $code, 'component_appid' => $appId];
  1630. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1631. $result = Json::decode($result);
  1632. return $result;
  1633. }
  1634. //微信开放平台获取授权方的公众号帐号基本信息 ssh 2020.3.11
  1635. public static function getAuthorizerInfo($authorizeAppId, $open)
  1636. {
  1637. $appId = $open['appId'];
  1638. $accessToken = self::getComponentAccessToken($open);
  1639. $url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token={$accessToken}";
  1640. $curl = new curl\Curl();
  1641. $data = ['component_appid' => $appId, 'authorizer_appid' => $authorizeAppId];
  1642. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1643. $result = Json::decode($result);
  1644. return $result;
  1645. }
  1646. /**************************即时配送相关接口 START *****************************************/
  1647. //开通即时配送权限
  1648. public static function openLocalExpress($merchant, $ptStyle = 0)
  1649. {
  1650. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1651. $url = "https://api.weixin.qq.com/cgi-bin/express/local/business/open?access_token=" . $accessToken;
  1652. $curl = new curl\Curl();
  1653. $result = $curl->get($url);
  1654. $respond = Json::decode($result);
  1655. echo "<pre>";
  1656. print_r($respond);
  1657. }
  1658. //异常件退回商家商家确认收货接口
  1659. //场景:当订单配送异常,骑手把货物退还给商家,商家收货以后调用本接口返回确认收货
  1660. /*
  1661. * $data
  1662. * {
  1663. "shopid": "123456",
  1664. "shop_order_id": "123456",
  1665. "shop_no": "shop_no_111"
  1666. "waybill_id": "123456",
  1667. "remark": "remark",
  1668. "delivery_sign": "123456"
  1669. }
  1670. */
  1671. public static function abnormalConfirm($data, $merchant, $ptStyle = 0)
  1672. {
  1673. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1674. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/confirm_return?access_token=' . $accessToken;
  1675. $curl = new curl\Curl();
  1676. $data = Json::encode($data);
  1677. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1678. $respond = Json::decode($result);
  1679. return $respond;
  1680. }
  1681. //即时配送,拉取已绑定账号 ssh 2021.2.1
  1682. public static function getBindAccount($merchant, $ptStyle = 0)
  1683. {
  1684. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1685. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/shop/get?access_token=' . $accessToken;
  1686. $curl = new curl\Curl();
  1687. $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
  1688. $respond = Json::decode($result);
  1689. return $respond;
  1690. }
  1691. //获取已支持的配送公司列表接口 lqh 2021.2.2
  1692. public static function getAllImmeDelivery($merchant, $ptStyle = 0)
  1693. {
  1694. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1695. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/delivery/getall?access_token=' . $accessToken;
  1696. $curl = new curl\Curl();
  1697. $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
  1698. $respond = Json::decode($result);
  1699. return $respond;
  1700. }
  1701. //下配送单接口 lqh 2021.2.2
  1702. //场景:
  1703. /*
  1704. * 商家可调用本接口向配送公司请求下配送单,配送公司会返回这一单的配送单号、配送费、预计骑手接单时间等信息。
  1705. 如遇下单错误,请先确认一下编码方式,python建议 json.dumps(b, ensure_ascii=False),php建议 json_encode($arr, JSON_UNESCAPED_UNICODE) 可预约时间:达达:72小时内,闪送2小时以后,48小时以内
  1706. {
  1707. "resultcode": 0,
  1708. "resultmsg": "ok",
  1709. "fee": 10, 实际运费(单位:元),运费减去优惠券费用
  1710. "deliverfee": 10, 运费(单位:元)
  1711. "couponfee": 0, 优惠券费用(单位:元)
  1712. "tips": 0, 小费(单位:元)
  1713. "insurancfee": 0, 保价费(单位:元)
  1714. "distance": 1000, 配送距离(整数单位:米)
  1715. "waybill_id": "123456789", 配送单号
  1716. "order_status": 101, 配送状态。
  1717. "finish_code": 1024, 收货码
  1718. "pickup_code": 2048, 取货码
  1719. "dispatch_duration": 300 预计骑手接单时间,单位秒,比如5分钟,就填300, 无法预计填0
  1720. }
  1721. */
  1722. public static function addOrder($data, $merchant, $ptStyle = 0)
  1723. {
  1724. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1725. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/add?access_token=' . $accessToken;
  1726. $curl = new curl\Curl();
  1727. $data = Json::encode($data);
  1728. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1729. $respond = Json::decode($result);
  1730. return $respond;
  1731. }
  1732. //可以对待接单状态的订单增加小费。需要注意:订单的小费,以最新一次加小费动作的金额为准,故下一次增加小费额必须大于上一次小费额
  1733. /*
  1734. * {
  1735. "shopid": "123456",
  1736. "shop_order_id": "123456",
  1737. "waybill_id": "123456",
  1738. "tips": 5,
  1739. "remark": "gogogo",
  1740. "delivery_sign": "123456",
  1741. "shop_no": "shop_no_111"
  1742. }
  1743. */
  1744. public static function addTip($data, $merchant, $ptStyle = 0)
  1745. {
  1746. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1747. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/addtips?access_token=' . $accessToken;
  1748. $curl = new curl\Curl();
  1749. $data = Json::encode($data);
  1750. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1751. $respond = Json::decode($result);
  1752. return $respond;
  1753. }
  1754. //取消配送单接口 lqh 2021.2.3
  1755. public static function cancelOrder($data, $merchant, $ptStyle = 0)
  1756. {
  1757. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1758. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/cancel?access_token=' . $accessToken;
  1759. $curl = new curl\Curl();
  1760. $data = Json::encode($data);
  1761. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1762. $respond = Json::decode($result);
  1763. return $respond;
  1764. }
  1765. //拉取配送单信息 lqh 2021.2.3
  1766. public static function getOrder($data, $merchant, $ptStyle = 0)
  1767. {
  1768. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1769. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/get?access_token=' . $accessToken;
  1770. $curl = new curl\Curl();
  1771. $data = Json::encode($data);
  1772. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1773. $respond = Json::decode($result);
  1774. return $respond;
  1775. }
  1776. //模拟配送公司更新配送单状态, 该接口只用于沙盒环境,即订单并没有真实流转到运力方
  1777. public static function mockUpdateOrder($data, $merchant, $ptStyle = 0)
  1778. {
  1779. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1780. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/test_update_order?access_token=' . $accessToken;
  1781. $curl = new curl\Curl();
  1782. $data = Json::encode($data);
  1783. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1784. $respond = Json::decode($result);
  1785. return $respond;
  1786. }
  1787. //预下配送单接口
  1788. //在用户提交外卖订单时,商家可调用本接口查询配送公司是否可接单、预计多久接单、运费预估等。
  1789. //预估运费可作为展示给用户的运费参考值。
  1790. public static function preAddOrder($data, $merchant, $ptStyle = 0)
  1791. {
  1792. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1793. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/pre_add?access_token=' . $accessToken;
  1794. $curl = new curl\Curl();
  1795. $data = Json::encode($data);
  1796. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1797. $respond = Json::decode($result);
  1798. return $respond;
  1799. }
  1800. //预取消配送单接口
  1801. //在正式取消配送单前,商家可调用本接口查询该订单是否可以取消,取消订单配送公司需要扣除的费用是多少
  1802. public static function preCancelOrder($data, $merchant, $ptStyle = 0)
  1803. {
  1804. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1805. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/precancel?access_token=' . $accessToken;
  1806. $curl = new curl\Curl();
  1807. $data = Json::encode($data);
  1808. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1809. $respond = Json::decode($result);
  1810. return $respond;
  1811. }
  1812. //重新下单
  1813. //在调用下配送单接口、订单被取消、过期或者投递异常的情况下,可调用此接口,重新下单,需要保持orderid一致。
  1814. //备注:美团不支持重新下单接口,如果订单被取消商家需要重新下单,请修改orderid之后,调用下配送单接口下单。
  1815. public static function reOrder($data, $merchant, $ptStyle = 0)
  1816. {
  1817. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1818. $url = 'https://api.weixin.qq.com/cgi-bin/express/local/business/order/readd?access_token=' . $accessToken;
  1819. $curl = new curl\Curl();
  1820. $data = Json::encode($data);
  1821. $result = $curl->setOption(CURLOPT_POSTFIELDS, $data)->post($url);
  1822. $respond = Json::decode($result);
  1823. return $respond;
  1824. }
  1825. /**************************即时配送相关接口 END *****************************************/
  1826. //发送订阅消息通知 ssh 2023511
  1827. public static function commonSendSubscribeMessage($merchant, $ptStyle, $msgId, $miniOpenId, $page, $msg)
  1828. {
  1829. $accessToken = self::getMiniProgramAccessToken($merchant, $ptStyle);
  1830. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $accessToken;
  1831. $curl = new curl\Curl();
  1832. $data = ['template_id' => $msgId, 'page' => $page, 'touser' => $miniOpenId, 'data' => $msg];
  1833. $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
  1834. Yii::info('订阅消息通知返回:' . $result);
  1835. $result = Json::decode($result);
  1836. return $result;
  1837. }
  1838. }