weixinUtil.php 54 KB

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