weixinUtil.php 54 KB

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