Wechat.php 183 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977
  1. <?php
  2. /**
  3. * 微信公众平台PHP-SDK, 官方API部分
  4. * @author dodge <dodgepudding@gmail.com>
  5. * @link https://github.com/dodgepudding/wechat-php-sdk
  6. * @version 1.2
  7. * usage:
  8. * $options = array(
  9. * 'token'=>'tokenaccesskey', //填写你设定的key
  10. * 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
  11. * 'appid'=>'wxdk1234567890', //填写高级调用功能的app id
  12. * 'appsecret'=>'xxxxxxxxxxxxxxxxxxx' //填写高级调用功能的密钥
  13. * );
  14. * $weObj = new Wechat($options);
  15. * $weObj->valid();
  16. * $type = $weObj->getRev()->getRevType();
  17. * switch($type) {
  18. * case Wechat::MSGTYPE_TEXT:
  19. * $weObj->text("hello, I'm wechat")->reply();
  20. * exit;
  21. * break;
  22. * case Wechat::MSGTYPE_EVENT:
  23. * ....
  24. * break;
  25. * case Wechat::MSGTYPE_IMAGE:
  26. * ...
  27. * break;
  28. * default:
  29. * $weObj->text("help info")->reply();
  30. * }
  31. *
  32. * //获取菜单操作:
  33. * $menu = $weObj->getMenu();
  34. * //设置菜单
  35. * $newmenu = array(
  36. * "button"=>
  37. * array(
  38. * array('type'=>'click','name'=>'最新消息','key'=>'MENU_KEY_NEWS'),
  39. * array('type'=>'view','name'=>'我要搜索','url'=>'http://www.baidu.com'),
  40. * )
  41. * );
  42. * $result = $weObj->createMenu($newmenu);
  43. */
  44. namespace app\common;
  45. use think\facade\Cache;
  46. use think\Db;
  47. use think\facade\Log;
  48. class Wechat
  49. {
  50. const MSGTYPE_TEXT = 'text';
  51. const MSGTYPE_IMAGE = 'image';
  52. const MSGTYPE_LOCATION = 'location';
  53. const MSGTYPE_LINK = 'link';
  54. const MSGTYPE_EVENT = 'event';
  55. const MSGTYPE_MUSIC = 'music';
  56. const MSGTYPE_NEWS = 'news';
  57. const MSGTYPE_VOICE = 'voice';
  58. const MSGTYPE_VIDEO = 'video';
  59. const MSGTYPE_SHORTVIDEO = 'shortvideo';
  60. const EVENT_SUBSCRIBE = 'subscribe'; //订阅
  61. const EVENT_UNSUBSCRIBE = 'unsubscribe'; //取消订阅
  62. const EVENT_SCAN = 'SCAN'; //扫描带参数二维码
  63. const EVENT_LOCATION = 'LOCATION'; //上报地理位置
  64. const EVENT_MENU_VIEW = 'VIEW'; //菜单 - 点击菜单跳转链接
  65. const EVENT_MENU_CLICK = 'CLICK'; //菜单 - 点击菜单拉取消息
  66. const EVENT_MENU_SCAN_PUSH = 'scancode_push'; //菜单 - 扫码推事件(客户端跳URL)
  67. const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg'; //菜单 - 扫码推事件(客户端不跳URL)
  68. const EVENT_MENU_PIC_SYS = 'pic_sysphoto'; //菜单 - 弹出系统拍照发图
  69. const EVENT_MENU_PIC_PHOTO = 'pic_photo_or_album'; //菜单 - 弹出拍照或者相册发图
  70. const EVENT_MENU_PIC_WEIXIN = 'pic_weixin'; //菜单 - 弹出微信相册发图器
  71. const EVENT_MENU_LOCATION = 'location_select'; //菜单 - 弹出地理位置选择器
  72. const EVENT_SEND_MASS = 'MASSSENDJOBFINISH'; //发送结果 - 高级群发完成
  73. const EVENT_SEND_TEMPLATE = 'TEMPLATESENDJOBFINISH';//发送结果 - 模板消息发送结果
  74. const EVENT_KF_SEESION_CREATE = 'kfcreatesession'; //多客服 - 接入会话
  75. const EVENT_KF_SEESION_CLOSE = 'kfclosesession'; //多客服 - 关闭会话
  76. const EVENT_KF_SEESION_SWITCH = 'kfswitchsession'; //多客服 - 转接会话
  77. const EVENT_CARD_PASS = 'card_pass_check'; //卡券 - 审核通过
  78. const EVENT_CARD_NOTPASS = 'card_not_pass_check'; //卡券 - 审核未通过
  79. const EVENT_CARD_USER_GET = 'user_get_card'; //卡券 - 用户领取卡券
  80. const EVENT_CARD_USER_DEL = 'user_del_card'; //卡券 - 用户删除卡券
  81. const EVENT_MERCHANT_ORDER = 'merchant_order'; //微信小店 - 订单付款通知
  82. const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
  83. const AUTH_URL = '/token?grant_type=client_credential&';
  84. const MENU_CREATE_URL = '/menu/create?';
  85. const MENU_GET_URL = '/menu/get?';
  86. const MENU_DELETE_URL = '/menu/delete?';
  87. const MENU_ADDCONDITIONAL_URL = '/menu/addconditional?';
  88. const MENU_DELCONDITIONAL_URL = '/menu/delconditional?';
  89. const MENU_TRYMATCH_URL = '/menu/trymatch?';
  90. const GET_TICKET_URL = '/ticket/getticket?';
  91. const CALLBACKSERVER_GET_URL = '/getcallbackip?';
  92. const QRCODE_CREATE_URL = '/qrcode/create?';
  93. const QR_SCENE = 0;
  94. const QR_LIMIT_SCENE = 1;
  95. const QRCODE_IMG_URL = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=';
  96. const SHORT_URL = '/shorturl?';
  97. const USER_GET_URL = '/user/get?';
  98. const USER_INFO_URL = '/user/info?';
  99. const USERS_INFO_URL = '/user/info/batchget?';
  100. const USER_UPDATEREMARK_URL = '/user/info/updateremark?';
  101. const GROUP_GET_URL = '/groups/get?';
  102. const USER_GROUP_URL = '/groups/getid?';
  103. const GROUP_CREATE_URL = '/groups/create?';
  104. const GROUP_UPDATE_URL = '/groups/update?';
  105. const GROUP_MEMBER_UPDATE_URL = '/groups/members/update?';
  106. const GROUP_MEMBER_BATCHUPDATE_URL = '/groups/members/batchupdate?';
  107. const CUSTOM_SEND_URL = '/message/custom/send?';
  108. const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
  109. const MASS_SEND_URL = '/message/mass/send?';
  110. const TEMPLATE_SET_INDUSTRY_URL = '/template/api_set_industry?';
  111. const TEMPLATE_ADD_TPL_URL = '/template/api_add_template?';
  112. const TEMPLATE_SEND_URL = '/message/template/send?';
  113. const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
  114. const MASS_DELETE_URL = '/message/mass/delete?';
  115. const MASS_PREVIEW_URL = '/message/mass/preview?';
  116. const MASS_QUERY_URL = '/message/mass/get?';
  117. const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin';
  118. const MEDIA_UPLOAD_URL = '/media/upload?';
  119. const MEDIA_UPLOADIMG_URL = '/media/uploadimg?';//图片上传接口
  120. const MEDIA_GET_URL = '/media/get?';
  121. const MEDIA_VIDEO_UPLOAD = '/media/uploadvideo?';
  122. const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
  123. const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
  124. const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
  125. const MEDIA_FOREVER_GET_URL = '/material/get_material?';
  126. const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
  127. const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
  128. const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';
  129. const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2';
  130. const OAUTH_AUTHORIZE_URL = '/authorize?';
  131. ///多客服相关地址
  132. const CUSTOM_SERVICE_GET_RECORD = '/customservice/getrecord?';
  133. const CUSTOM_SERVICE_GET_KFLIST = '/customservice/getkflist?';
  134. const CUSTOM_SERVICE_GET_ONLINEKFLIST = '/customservice/getonlinekflist?';
  135. const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com'; //以下API接口URL需要使用此前缀
  136. const OAUTH_TOKEN_URL = '/sns/oauth2/access_token?';
  137. const OAUTH_REFRESH_URL = '/sns/oauth2/refresh_token?';
  138. const OAUTH_USERINFO_URL = '/sns/userinfo?';
  139. const OAUTH_AUTH_URL = '/sns/auth?';
  140. ///多客服相关地址
  141. const CUSTOM_SESSION_CREATE = '/customservice/kfsession/create?';
  142. const CUSTOM_SESSION_CLOSE = '/customservice/kfsession/close?';
  143. const CUSTOM_SESSION_SWITCH = '/customservice/kfsession/switch?';
  144. const CUSTOM_SESSION_GET = '/customservice/kfsession/getsession?';
  145. const CUSTOM_SESSION_GET_LIST = '/customservice/kfsession/getsessionlist?';
  146. const CUSTOM_SESSION_GET_WAIT = '/customservice/kfsession/getwaitcase?';
  147. const CS_KF_ACCOUNT_ADD_URL = '/customservice/kfaccount/add?';
  148. const CS_KF_ACCOUNT_UPDATE_URL = '/customservice/kfaccount/update?';
  149. const CS_KF_ACCOUNT_DEL_URL = '/customservice/kfaccount/del?';
  150. const CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL = '/customservice/kfaccount/uploadheadimg?';
  151. ///卡券相关地址
  152. const CARD_CREATE = '/card/create?';
  153. const CARD_DELETE = '/card/delete?';
  154. const CARD_UPDATE = '/card/update?';
  155. const CARD_GET = '/card/get?';
  156. const CARD_USER_GETCARDLIST = '/card/user/getcardlist?';
  157. const CARD_BATCHGET = '/card/batchget?';
  158. const CARD_MODIFY_STOCK = '/card/modifystock?';
  159. const CARD_LOCATION_BATCHADD = '/card/location/batchadd?';
  160. const CARD_LOCATION_BATCHGET = '/card/location/batchget?';
  161. const CARD_GETCOLORS = '/card/getcolors?';
  162. const CARD_QRCODE_CREATE = '/card/qrcode/create?';
  163. const CARD_CODE_CONSUME = '/card/code/consume?';
  164. const CARD_CODE_DECRYPT = '/card/code/decrypt?';
  165. const CARD_CODE_GET = '/card/code/get?';
  166. const CARD_CODE_UPDATE = '/card/code/update?';
  167. const CARD_CODE_UNAVAILABLE = '/card/code/unavailable?';
  168. const CARD_TESTWHILELIST_SET = '/card/testwhitelist/set?';
  169. const CARD_MEETINGCARD_UPDATEUSER = '/card/meetingticket/updateuser?'; //更新会议门票
  170. const CARD_MEMBERCARD_ACTIVATE = '/card/membercard/activate?'; //激活会员卡
  171. const CARD_MEMBERCARD_UPDATEUSER = '/card/membercard/updateuser?'; //更新会员卡
  172. const CARD_MOVIETICKET_UPDATEUSER = '/card/movieticket/updateuser?'; //更新电影票(未加方法)
  173. const CARD_BOARDINGPASS_CHECKIN = '/card/boardingpass/checkin?'; //飞机票-在线选座(未加方法)
  174. const CARD_LUCKYMONEY_UPDATE = '/card/luckymoney/updateuserbalance?'; //更新红包金额
  175. const SEMANTIC_API_URL = '/semantic/semproxy/search?'; //语义理解
  176. ///数据分析接口
  177. static $DATACUBE_URL_ARR
  178. = [ //用户分析
  179. 'user' => [
  180. 'summary' => '/datacube/getusersummary?', //获取用户增减数据(getusersummary)
  181. 'cumulate' => '/datacube/getusercumulate?', //获取累计用户数据(getusercumulate)
  182. ],
  183. 'article' => [ //图文分析
  184. 'summary' => '/datacube/getarticlesummary?', //获取图文群发每日数据(getarticlesummary)
  185. 'total' => '/datacube/getarticletotal?', //获取图文群发总数据(getarticletotal)
  186. 'read' => '/datacube/getuserread?', //获取图文统计数据(getuserread)
  187. 'readhour' => '/datacube/getuserreadhour?', //获取图文统计分时数据(getuserreadhour)
  188. 'share' => '/datacube/getusershare?', //获取图文分享转发数据(getusershare)
  189. 'sharehour' => '/datacube/getusersharehour?', //获取图文分享转发分时数据(getusersharehour)
  190. ],
  191. 'upstreammsg' => [ //消息分析
  192. 'summary' => '/datacube/getupstreammsg?', //获取消息发送概况数据(getupstreammsg)
  193. 'hour' => '/datacube/getupstreammsghour?', //获取消息分送分时数据(getupstreammsghour)
  194. 'week' => '/datacube/getupstreammsgweek?', //获取消息发送周数据(getupstreammsgweek)
  195. 'month' => '/datacube/getupstreammsgmonth?', //获取消息发送月数据(getupstreammsgmonth)
  196. 'dist' => '/datacube/getupstreammsgdist?', //获取消息发送分布数据(getupstreammsgdist)
  197. 'distweek' => '/datacube/getupstreammsgdistweek?', //获取消息发送分布周数据(getupstreammsgdistweek)
  198. 'distmonth' => '/datacube/getupstreammsgdistmonth?', //获取消息发送分布月数据(getupstreammsgdistmonth)
  199. ],
  200. 'interface' => [ //接口分析
  201. 'summary' => '/datacube/getinterfacesummary?', //获取接口分析数据(getinterfacesummary)
  202. 'summaryhour' => '/datacube/getinterfacesummaryhour?', //获取接口分析分时数据(getinterfacesummaryhour)
  203. ],
  204. ];
  205. ///微信摇一摇周边
  206. const SHAKEAROUND_DEVICE_APPLYID = '/shakearound/device/applyid?';//申请设备ID
  207. const SHAKEAROUND_DEVICE_UPDATE = '/shakearound/device/update?';//编辑设备信息
  208. const SHAKEAROUND_DEVICE_SEARCH = '/shakearound/device/search?';//查询设备列表
  209. const SHAKEAROUND_DEVICE_BINDLOCATION = '/shakearound/device/bindlocation?';//配置设备与门店ID的关系
  210. const SHAKEAROUND_DEVICE_BINDPAGE = '/shakearound/device/bindpage?';//配置设备与页面的绑定关系
  211. const SHAKEAROUND_MATERIAL_ADD = '/shakearound/material/add?';//上传摇一摇图片素材
  212. const SHAKEAROUND_PAGE_ADD = '/shakearound/page/add?';//增加页面
  213. const SHAKEAROUND_PAGE_UPDATE = '/shakearound/page/update?';//编辑页面
  214. const SHAKEAROUND_PAGE_SEARCH = '/shakearound/page/search?';//查询页面列表
  215. const SHAKEAROUND_PAGE_DELETE = '/shakearound/page/delete?';//删除页面
  216. const SHAKEAROUND_USER_GETSHAKEINFO = '/shakearound/user/getshakeinfo?';//获取摇周边的设备及用户信息
  217. const SHAKEAROUND_STATISTICS_DEVICE = '/shakearound/statistics/device?';//以设备为维度的数据统计接口
  218. const SHAKEAROUND_STATISTICS_PAGE = '/shakearound/statistics/page?';//以页面为维度的数据统计接口
  219. ///微信小店相关接口
  220. const MERCHANT_ORDER_GETBYID = '/merchant/order/getbyid?';//根据订单ID获取订单详情
  221. const MERCHANT_ORDER_GETBYFILTER = '/merchant/order/getbyfilter?';//根据订单状态/创建时间获取订单详情
  222. const MERCHANT_ORDER_SETDELIVERY = '/merchant/order/setdelivery?';//设置订单发货信息
  223. const MERCHANT_ORDER_CLOSE = '/merchant/order/close?';//关闭订单
  224. private $token;
  225. private $encodingAesKey;
  226. private $encrypt_type;
  227. private $appid;
  228. private $appsecret;
  229. private $access_token;
  230. private $jsapi_ticket;
  231. private $api_ticket;
  232. private $user_token;
  233. private $partnerid;
  234. private $partnerkey;
  235. private $paysignkey;
  236. private $postxml;
  237. private $_msg;
  238. private $_funcflag = false;
  239. private $_receive;
  240. private $_text_filter = true;
  241. public $debug = false;
  242. public $errCode = 40001;
  243. public $errMsg = "no access";
  244. public $logcallback;
  245. public $is_dingyue = false; //是否订阅号
  246. public function __construct($options)
  247. {
  248. $this->token = isset($options['token']) ? $options['token'] : '';
  249. $this->encodingAesKey = isset($options['encodingaeskey']) ? $options['encodingaeskey'] : '';
  250. $this->appid = isset($options['appid']) ? $options['appid'] : '';
  251. $this->appsecret = isset($options['appsecret']) ? $options['appsecret'] : '';
  252. $this->debug = isset($options['debug']) ? $options['debug'] : false;
  253. $this->logcallback = isset($options['logcallback']) ? $options['logcallback'] : false;
  254. $this->is_dingyue = isset($options['is_dingyue']) ? (intval($options['is_dingyue']) == 1) : false;
  255. }
  256. private static $_instance = null;
  257. /**
  258. * 单例模式的实例
  259. * @return bool|Wechat|null
  260. */
  261. public static function instance()
  262. {
  263. if (self::$_instance == null) {
  264. $setting = [
  265. 'token' => 'QrxDIl1Iq2JOjZh2JtxTOcjHq2c3rgLB',
  266. 'app_id' => 'wxa3dec10df68cd87d',
  267. 'app_secret' => '17bff4aa5bf1ffa2c15aeb5eea0e2b32',
  268. 'aec_key' => 'Ja4CqKlYMS92vc99S5eSyC99C2235s93S94939oAsAA',
  269. 'is_dingyue' => 0,
  270. 'is_bind' => 0,
  271. ];
  272. if (empty($setting)) {
  273. return false;
  274. }
  275. self::$_instance = new self(
  276. [
  277. 'token' => $setting['token'],
  278. 'appid' => $setting['app_id'],
  279. 'appsecret' => $setting['app_secret'],
  280. 'encodingaeskey' => $setting['aec_key'],
  281. 'is_dingyue' => $setting['is_dingyue'],
  282. ]);
  283. }
  284. return self::$_instance;
  285. }
  286. /**
  287. * For weixin server validation
  288. */
  289. private function checkSignature($str = '')
  290. {
  291. $signature = isset($_GET["signature"]) ? $_GET["signature"] : '';
  292. $signature = isset($_GET["msg_signature"]) ? $_GET["msg_signature"] : $signature; //如果存在加密验证则用加密验证段
  293. $timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : '';
  294. $nonce = isset($_GET["nonce"]) ? $_GET["nonce"] : '';
  295. $token = $this->token;
  296. $tmpArr = [$token, $timestamp, $nonce, $str];
  297. sort($tmpArr, SORT_STRING);
  298. $tmpStr = implode($tmpArr);
  299. $tmpStr = sha1($tmpStr);
  300. if ($tmpStr == $signature) {
  301. return true;
  302. } else {
  303. return false;
  304. }
  305. }
  306. /**
  307. * For weixin server validation
  308. * @param bool $return 是否返回
  309. */
  310. public function valid($return = false)
  311. {
  312. $encryptStr = "";
  313. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  314. $postStr = file_get_contents("php://input");
  315. $array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  316. $this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"] : '';
  317. if ($this->encrypt_type == 'aes') { //aes加密
  318. $this->log($postStr);
  319. $encryptStr = $array['Encrypt'];
  320. $pc = new Prpcrypt($this->encodingAesKey);
  321. $array = $pc->decrypt($encryptStr, $this->appid);
  322. if (!isset($array[0]) || ($array[0] != 0)) {
  323. if (!$return) {
  324. die('decrypt error!');
  325. } else {
  326. return false;
  327. }
  328. }
  329. $this->postxml = $array[1];
  330. if (!$this->appid)
  331. $this->appid = $array[2];//为了没有appid的订阅号。
  332. } else {
  333. $this->postxml = $postStr;
  334. }
  335. } elseif (isset($_GET["echostr"])) {
  336. $echoStr = $_GET["echostr"];
  337. if ($return) {
  338. if ($this->checkSignature())
  339. return $echoStr;
  340. else
  341. return false;
  342. } else {
  343. if ($this->checkSignature()) {
  344. die($echoStr);
  345. } else {
  346. die('no access');
  347. }
  348. }
  349. }
  350. if (!$this->checkSignature($encryptStr)) {
  351. if ($return)
  352. return false;
  353. else
  354. die('no access');
  355. }
  356. return true;
  357. }
  358. /**
  359. * 设置发送消息
  360. * @param array $msg 消息数组
  361. * @param bool $append 是否在原消息数组追加
  362. */
  363. public function Message($msg = '', $append = false)
  364. {
  365. if (is_null($msg)) {
  366. $this->_msg = [];
  367. } elseif (is_array($msg)) {
  368. if ($append)
  369. $this->_msg = array_merge($this->_msg, $msg);
  370. else
  371. $this->_msg = $msg;
  372. return $this->_msg;
  373. } else {
  374. return $this->_msg;
  375. }
  376. }
  377. /**
  378. * 设置消息的星标标志,官方已取消对此功能的支持
  379. */
  380. public function setFuncFlag($flag)
  381. {
  382. $this->_funcflag = $flag;
  383. return $this;
  384. }
  385. /**
  386. * 日志记录,可被重载。
  387. * @param mixed $log 输入日志
  388. * @return mixed
  389. */
  390. protected function log($log)
  391. {
  392. if ($this->debug && function_exists($this->logcallback)) {
  393. if (is_array($log)) $log = print_r($log, true);
  394. return call_user_func($this->logcallback, $log);
  395. }
  396. }
  397. /**
  398. * 获取微信服务器发来的信息
  399. */
  400. public function getRev()
  401. {
  402. if ($this->_receive) return $this;
  403. $postStr = !empty($this->postxml) ? $this->postxml : file_get_contents("php://input");
  404. //兼顾使用明文又不想调用valid()方法的情况
  405. $this->log($postStr);
  406. if (!empty($postStr)) {
  407. $this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  408. }
  409. return $this;
  410. }
  411. /**
  412. * 获取微信服务器发来的信息
  413. */
  414. public function getRevData()
  415. {
  416. return $this->_receive;
  417. }
  418. /**
  419. * 获取消息发送者
  420. */
  421. public function getRevFrom()
  422. {
  423. if (isset($this->_receive['FromUserName']))
  424. return $this->_receive['FromUserName'];
  425. else
  426. return false;
  427. }
  428. /**
  429. * 获取消息接受者
  430. */
  431. public function getRevTo()
  432. {
  433. if (isset($this->_receive['ToUserName']))
  434. return $this->_receive['ToUserName'];
  435. else
  436. return false;
  437. }
  438. /**
  439. * 获取接收消息的类型
  440. */
  441. public function getRevType()
  442. {
  443. if (isset($this->_receive['MsgType']))
  444. return $this->_receive['MsgType'];
  445. else
  446. return false;
  447. }
  448. /**
  449. * 获取消息ID
  450. */
  451. public function getRevID()
  452. {
  453. if (isset($this->_receive['MsgId']))
  454. return $this->_receive['MsgId'];
  455. else
  456. return false;
  457. }
  458. /**
  459. * 获取消息发送时间
  460. */
  461. public function getRevCtime()
  462. {
  463. if (isset($this->_receive['CreateTime']))
  464. return $this->_receive['CreateTime'];
  465. else
  466. return false;
  467. }
  468. /**
  469. * 获取接收消息内容正文
  470. */
  471. public function getRevContent()
  472. {
  473. if (isset($this->_receive['Content']))
  474. return $this->_receive['Content'];
  475. else if (isset($this->_receive['Recognition'])) //获取语音识别文字内容,需申请开通
  476. return $this->_receive['Recognition'];
  477. else
  478. return false;
  479. }
  480. /**
  481. * 获取接收消息图片
  482. */
  483. public function getRevPic()
  484. {
  485. if (isset($this->_receive['PicUrl']))
  486. return [
  487. 'mediaid' => $this->_receive['MediaId'],
  488. 'picurl' => (string)$this->_receive['PicUrl'], //防止picurl为空导致解析出错
  489. ];
  490. else
  491. return false;
  492. }
  493. /**
  494. * 获取接收消息链接
  495. */
  496. public function getRevLink()
  497. {
  498. if (isset($this->_receive['Url'])) {
  499. return [
  500. 'url' => $this->_receive['Url'],
  501. 'title' => $this->_receive['Title'],
  502. 'description' => $this->_receive['Description'],
  503. ];
  504. } else
  505. return false;
  506. }
  507. /**
  508. * 获取接收地理位置
  509. */
  510. public function getRevGeo()
  511. {
  512. if (isset($this->_receive['Location_X'])) {
  513. return [
  514. 'x' => $this->_receive['Location_X'],
  515. 'y' => $this->_receive['Location_Y'],
  516. 'scale' => $this->_receive['Scale'],
  517. 'label' => $this->_receive['Label'],
  518. ];
  519. } else
  520. return false;
  521. }
  522. /**
  523. * 获取上报地理位置事件
  524. */
  525. public function getRevEventGeo()
  526. {
  527. if (isset($this->_receive['Latitude'])) {
  528. return [
  529. 'x' => $this->_receive['Latitude'],
  530. 'y' => $this->_receive['Longitude'],
  531. 'precision' => $this->_receive['Precision'],
  532. ];
  533. } else
  534. return false;
  535. }
  536. /**
  537. * 获取接收事件推送
  538. */
  539. public function getRevEvent()
  540. {
  541. if (isset($this->_receive['Event'])) {
  542. $array['event'] = $this->_receive['Event'];
  543. }
  544. if (isset($this->_receive['EventKey'])) {
  545. $array['key'] = $this->_receive['EventKey'];
  546. }
  547. if (isset($array) && count($array) > 0) {
  548. return $array;
  549. } else {
  550. return false;
  551. }
  552. }
  553. /**
  554. * 获取自定义菜单的扫码推事件信息
  555. *
  556. * 事件类型为以下两种时则调用此方法有效
  557. * Event 事件类型,scancode_push
  558. * Event 事件类型,scancode_waitmsg
  559. *
  560. * @return: array | false
  561. * array (
  562. * 'ScanType'=>'qrcode',
  563. * 'ScanResult'=>'123123'
  564. * )
  565. */
  566. public function getRevScanInfo()
  567. {
  568. if (isset($this->_receive['ScanCodeInfo'])) {
  569. if (!is_array($this->_receive['ScanCodeInfo'])) {
  570. $array = (array)$this->_receive['ScanCodeInfo'];
  571. $this->_receive['ScanCodeInfo'] = $array;
  572. } else {
  573. $array = $this->_receive['ScanCodeInfo'];
  574. }
  575. }
  576. if (isset($array) && count($array) > 0) {
  577. return $array;
  578. } else {
  579. return false;
  580. }
  581. }
  582. /**
  583. * 获取自定义菜单的图片发送事件信息
  584. *
  585. * 事件类型为以下三种时则调用此方法有效
  586. * Event 事件类型,pic_sysphoto 弹出系统拍照发图的事件推送
  587. * Event 事件类型,pic_photo_or_album 弹出拍照或者相册发图的事件推送
  588. * Event 事件类型,pic_weixin 弹出微信相册发图器的事件推送
  589. *
  590. * @return: array | false
  591. * array (
  592. * 'Count' => '2',
  593. * 'PicList' =>array (
  594. * 'item' =>array (
  595. * 0 =>array ('PicMd5Sum' => 'aaae42617cf2a14342d96005af53624c'),
  596. * 1 =>array ('PicMd5Sum' => '149bd39e296860a2adc2f1bb81616ff8'),
  597. * ),
  598. * ),
  599. * )
  600. *
  601. */
  602. public function getRevSendPicsInfo()
  603. {
  604. if (isset($this->_receive['SendPicsInfo'])) {
  605. if (!is_array($this->_receive['SendPicsInfo'])) {
  606. $array = (array)$this->_receive['SendPicsInfo'];
  607. if (isset($array['PicList'])) {
  608. $array['PicList'] = (array)$array['PicList'];
  609. $item = $array['PicList']['item'];
  610. $array['PicList']['item'] = [];
  611. foreach ($item as $key => $value) {
  612. $array['PicList']['item'][$key] = (array)$value;
  613. }
  614. }
  615. $this->_receive['SendPicsInfo'] = $array;
  616. } else {
  617. $array = $this->_receive['SendPicsInfo'];
  618. }
  619. }
  620. if (isset($array) && count($array) > 0) {
  621. return $array;
  622. } else {
  623. return false;
  624. }
  625. }
  626. /**
  627. * 获取自定义菜单的地理位置选择器事件推送
  628. *
  629. * 事件类型为以下时则可以调用此方法有效
  630. * Event 事件类型,location_select 弹出地理位置选择器的事件推送
  631. *
  632. * @return: array | false
  633. * array (
  634. * 'Location_X' => '33.731655000061',
  635. * 'Location_Y' => '113.29955200008047',
  636. * 'Scale' => '16',
  637. * 'Label' => '某某市某某区某某路',
  638. * 'Poiname' => '',
  639. * )
  640. *
  641. */
  642. public function getRevSendGeoInfo()
  643. {
  644. if (isset($this->_receive['SendLocationInfo'])) {
  645. if (!is_array($this->_receive['SendLocationInfo'])) {
  646. $array = (array)$this->_receive['SendLocationInfo'];
  647. if (empty($array['Poiname'])) {
  648. $array['Poiname'] = "";
  649. }
  650. if (empty($array['Label'])) {
  651. $array['Label'] = "";
  652. }
  653. $this->_receive['SendLocationInfo'] = $array;
  654. } else {
  655. $array = $this->_receive['SendLocationInfo'];
  656. }
  657. }
  658. if (isset($array) && count($array) > 0) {
  659. return $array;
  660. } else {
  661. return false;
  662. }
  663. }
  664. /**
  665. * 获取接收语音推送
  666. */
  667. public function getRevVoice()
  668. {
  669. if (isset($this->_receive['MediaId'])) {
  670. return [
  671. 'mediaid' => $this->_receive['MediaId'],
  672. 'format' => $this->_receive['Format'],
  673. ];
  674. } else
  675. return false;
  676. }
  677. /**
  678. * 获取接收视频推送
  679. */
  680. public function getRevVideo()
  681. {
  682. if (isset($this->_receive['MediaId'])) {
  683. return [
  684. 'mediaid' => $this->_receive['MediaId'],
  685. 'thumbmediaid' => $this->_receive['ThumbMediaId'],
  686. ];
  687. } else
  688. return false;
  689. }
  690. /**
  691. * 获取接收TICKET
  692. */
  693. public function getRevTicket()
  694. {
  695. if (isset($this->_receive['Ticket'])) {
  696. return $this->_receive['Ticket'];
  697. } else
  698. return false;
  699. }
  700. /**
  701. * 获取二维码的场景值
  702. */
  703. public function getRevSceneId()
  704. {
  705. if (isset($this->_receive['EventKey'])) {
  706. return str_replace('qrscene_', '', $this->_receive['EventKey']);
  707. } else {
  708. return false;
  709. }
  710. }
  711. /**
  712. * 获取主动推送的消息ID
  713. * 经过验证,这个和普通的消息MsgId不一样
  714. * 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH
  715. */
  716. public function getRevTplMsgID()
  717. {
  718. if (isset($this->_receive['MsgID'])) {
  719. return $this->_receive['MsgID'];
  720. } else
  721. return false;
  722. }
  723. /**
  724. * 获取模板消息发送状态
  725. */
  726. public function getRevStatus()
  727. {
  728. if (isset($this->_receive['Status'])) {
  729. return $this->_receive['Status'];
  730. } else
  731. return false;
  732. }
  733. /**
  734. * 获取群发或模板消息发送结果
  735. * 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH,即高级群发/模板消息
  736. */
  737. public function getRevResult()
  738. {
  739. if (isset($this->_receive['Status'])) //发送是否成功,具体的返回值请参考 高级群发/模板消息 的事件推送说明
  740. $array['Status'] = $this->_receive['Status'];
  741. if (isset($this->_receive['MsgID'])) //发送的消息id
  742. $array['MsgID'] = $this->_receive['MsgID'];
  743. //以下仅当群发消息时才会有的事件内容
  744. if (isset($this->_receive['TotalCount'])) //分组或openid列表内粉丝数量
  745. $array['TotalCount'] = $this->_receive['TotalCount'];
  746. if (isset($this->_receive['FilterCount'])) //过滤(过滤是指特定地区、性别的过滤、用户设置拒收的过滤,用户接收已超4条的过滤)后,准备发送的粉丝数
  747. $array['FilterCount'] = $this->_receive['FilterCount'];
  748. if (isset($this->_receive['SentCount'])) //发送成功的粉丝数
  749. $array['SentCount'] = $this->_receive['SentCount'];
  750. if (isset($this->_receive['ErrorCount'])) //发送失败的粉丝数
  751. $array['ErrorCount'] = $this->_receive['ErrorCount'];
  752. if (isset($array) && count($array) > 0) {
  753. return $array;
  754. } else {
  755. return false;
  756. }
  757. }
  758. /**
  759. * 获取多客服会话状态推送事件 - 接入会话
  760. * 当Event为 kfcreatesession 即接入会话
  761. * @return string | boolean 返回分配到的客服
  762. */
  763. public function getRevKFCreate()
  764. {
  765. if (isset($this->_receive['KfAccount'])) {
  766. return $this->_receive['KfAccount'];
  767. } else
  768. return false;
  769. }
  770. /**
  771. * 获取多客服会话状态推送事件 - 关闭会话
  772. * 当Event为 kfclosesession 即关闭会话
  773. * @return string | boolean 返回分配到的客服
  774. */
  775. public function getRevKFClose()
  776. {
  777. if (isset($this->_receive['KfAccount'])) {
  778. return $this->_receive['KfAccount'];
  779. } else
  780. return false;
  781. }
  782. /**
  783. * 获取多客服会话状态推送事件 - 转接会话
  784. * 当Event为 kfswitchsession 即转接会话
  785. * @return array | boolean 返回分配到的客服
  786. * {
  787. * 'FromKfAccount' => '', //原接入客服
  788. * 'ToKfAccount' => '' //转接到客服
  789. * }
  790. */
  791. public function getRevKFSwitch()
  792. {
  793. if (isset($this->_receive['FromKfAccount'])) //原接入客服
  794. $array['FromKfAccount'] = $this->_receive['FromKfAccount'];
  795. if (isset($this->_receive['ToKfAccount'])) //转接到客服
  796. $array['ToKfAccount'] = $this->_receive['ToKfAccount'];
  797. if (isset($array) && count($array) > 0) {
  798. return $array;
  799. } else {
  800. return false;
  801. }
  802. }
  803. /**
  804. * 获取卡券事件推送 - 卡卷审核是否通过
  805. * 当Event为 card_pass_check(审核通过) 或 card_not_pass_check(未通过)
  806. * @return string|boolean 返回卡券ID
  807. */
  808. public function getRevCardPass()
  809. {
  810. if (isset($this->_receive['CardId']))
  811. return $this->_receive['CardId'];
  812. else
  813. return false;
  814. }
  815. /**
  816. * 获取卡券事件推送 - 领取卡券
  817. * 当Event为 user_get_card(用户领取卡券)
  818. * @return array|boolean
  819. */
  820. public function getRevCardGet()
  821. {
  822. if (isset($this->_receive['CardId'])) //卡券 ID
  823. $array['CardId'] = $this->_receive['CardId'];
  824. if (isset($this->_receive['IsGiveByFriend'])) //是否为转赠,1 代表是,0 代表否。
  825. $array['IsGiveByFriend'] = $this->_receive['IsGiveByFriend'];
  826. $array['OldUserCardCode'] = $this->_receive['OldUserCardCode'];
  827. if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) //code 序列号。自定义 code 及非自定义 code的卡券被领取后都支持事件推送。
  828. $array['UserCardCode'] = $this->_receive['UserCardCode'];
  829. if (isset($array) && count($array) > 0) {
  830. return $array;
  831. } else {
  832. return false;
  833. }
  834. }
  835. /**
  836. * 获取卡券事件推送 - 删除卡券
  837. * 当Event为 user_del_card(用户删除卡券)
  838. * @return array|boolean
  839. */
  840. public function getRevCardDel()
  841. {
  842. if (isset($this->_receive['CardId'])) //卡券 ID
  843. $array['CardId'] = $this->_receive['CardId'];
  844. if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) //code 序列号。自定义 code 及非自定义 code的卡券被领取后都支持事件推送。
  845. $array['UserCardCode'] = $this->_receive['UserCardCode'];
  846. if (isset($array) && count($array) > 0) {
  847. return $array;
  848. } else {
  849. return false;
  850. }
  851. }
  852. /**
  853. * 获取订单ID - 订单付款通知
  854. * 当Event为 merchant_order(订单付款通知)
  855. * @return orderId|boolean
  856. */
  857. public function getRevOrderId()
  858. {
  859. if (isset($this->_receive['OrderId'])) //订单 ID
  860. return $this->_receive['OrderId'];
  861. else
  862. return false;
  863. }
  864. public static function xmlSafeStr($str)
  865. {
  866. return '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $str) . ']]>';
  867. }
  868. /**
  869. * 数据XML编码
  870. * @param mixed $data 数据
  871. * @return string
  872. */
  873. public static function data_to_xml($data)
  874. {
  875. $xml = '';
  876. foreach ($data as $key => $val) {
  877. is_numeric($key) && $key = "item id=\"$key\"";
  878. $xml .= "<$key>";
  879. $xml .= (is_array($val) || is_object($val)) ? self::data_to_xml($val) : self::xmlSafeStr($val);
  880. list($key,) = explode(' ', $key);
  881. $xml .= "</$key>";
  882. }
  883. return $xml;
  884. }
  885. /**
  886. * XML编码
  887. * @param mixed $data 数据
  888. * @param string $root 根节点名
  889. * @param string $item 数字索引的子节点名
  890. * @param string $attr 根节点属性
  891. * @param string $id 数字索引子节点key转换的属性名
  892. * @param string $encoding 数据编码
  893. * @return string
  894. */
  895. public function xml_encode($data, $root = 'xml', $item = 'item', $attr = '', $id = 'id', $encoding = 'utf-8')
  896. {
  897. if (is_array($attr)) {
  898. $_attr = [];
  899. foreach ($attr as $key => $value) {
  900. $_attr[] = "{$key}=\"{$value}\"";
  901. }
  902. $attr = implode(' ', $_attr);
  903. }
  904. $attr = trim($attr);
  905. $attr = empty($attr) ? '' : " {$attr}";
  906. $xml = "<{$root}{$attr}>";
  907. $xml .= self::data_to_xml($data, $item, $id);
  908. $xml .= "</{$root}>";
  909. return $xml;
  910. }
  911. /**
  912. * 过滤文字回复\r\n换行符
  913. * @param string $text
  914. * @return string|mixed
  915. */
  916. private function _auto_text_filter($text)
  917. {
  918. if (!$this->_text_filter) return $text;
  919. return str_replace("\r\n", "\n", $text);
  920. }
  921. /**
  922. * 设置回复消息
  923. * Example: $obj->text('hello')->reply();
  924. * @param string $text
  925. */
  926. public function text($text = '')
  927. {
  928. $FuncFlag = $this->_funcflag ? 1 : 0;
  929. $msg = [
  930. 'ToUserName' => $this->getRevFrom(),
  931. 'FromUserName' => $this->getRevTo(),
  932. 'MsgType' => self::MSGTYPE_TEXT,
  933. 'Content' => $this->_auto_text_filter($text),
  934. 'CreateTime' => time(),
  935. 'FuncFlag' => $FuncFlag,
  936. ];
  937. $this->Message($msg);
  938. return $this;
  939. }
  940. /**
  941. * 设置回复消息
  942. * Example: $obj->image('media_id')->reply();
  943. * @param string $mediaid
  944. */
  945. public function image($mediaid = '')
  946. {
  947. $FuncFlag = $this->_funcflag ? 1 : 0;
  948. $msg = [
  949. 'ToUserName' => $this->getRevFrom(),
  950. 'FromUserName' => $this->getRevTo(),
  951. 'MsgType' => self::MSGTYPE_IMAGE,
  952. 'Image' => ['MediaId' => $mediaid],
  953. 'CreateTime' => time(),
  954. 'FuncFlag' => $FuncFlag,
  955. ];
  956. $this->Message($msg);
  957. return $this;
  958. }
  959. /**
  960. * 设置回复消息
  961. * Example: $obj->voice('media_id')->reply();
  962. * @param string $mediaid
  963. */
  964. public function voice($mediaid = '')
  965. {
  966. $FuncFlag = $this->_funcflag ? 1 : 0;
  967. $msg = [
  968. 'ToUserName' => $this->getRevFrom(),
  969. 'FromUserName' => $this->getRevTo(),
  970. 'MsgType' => self::MSGTYPE_VOICE,
  971. 'Voice' => ['MediaId' => $mediaid],
  972. 'CreateTime' => time(),
  973. 'FuncFlag' => $FuncFlag,
  974. ];
  975. $this->Message($msg);
  976. return $this;
  977. }
  978. /**
  979. * 设置回复消息
  980. * Example: $obj->video('media_id','title','description')->reply();
  981. * @param string $mediaid
  982. */
  983. public function video($mediaid = '', $title = '', $description = '')
  984. {
  985. $FuncFlag = $this->_funcflag ? 1 : 0;
  986. $msg = [
  987. 'ToUserName' => $this->getRevFrom(),
  988. 'FromUserName' => $this->getRevTo(),
  989. 'MsgType' => self::MSGTYPE_VIDEO,
  990. 'Video' => [
  991. 'MediaId' => $mediaid,
  992. 'Title' => $title,
  993. 'Description' => $description,
  994. ],
  995. 'CreateTime' => time(),
  996. 'FuncFlag' => $FuncFlag,
  997. ];
  998. $this->Message($msg);
  999. return $this;
  1000. }
  1001. /**
  1002. * 设置回复音乐
  1003. * @param string $title
  1004. * @param string $desc
  1005. * @param string $musicurl
  1006. * @param string $hgmusicurl
  1007. * @param string $thumbmediaid 音乐图片缩略图的媒体id,非必须
  1008. */
  1009. public function music($title, $desc, $musicurl, $hgmusicurl = '', $thumbmediaid = '')
  1010. {
  1011. $FuncFlag = $this->_funcflag ? 1 : 0;
  1012. $msg = [
  1013. 'ToUserName' => $this->getRevFrom(),
  1014. 'FromUserName' => $this->getRevTo(),
  1015. 'CreateTime' => time(),
  1016. 'MsgType' => self::MSGTYPE_MUSIC,
  1017. 'Music' => [
  1018. 'Title' => $title,
  1019. 'Description' => $desc,
  1020. 'MusicUrl' => $musicurl,
  1021. 'HQMusicUrl' => $hgmusicurl,
  1022. ],
  1023. 'FuncFlag' => $FuncFlag,
  1024. ];
  1025. if ($thumbmediaid) {
  1026. $msg['Music']['ThumbMediaId'] = $thumbmediaid;
  1027. }
  1028. $this->Message($msg);
  1029. return $this;
  1030. }
  1031. /**
  1032. * 设置回复图文
  1033. * @param array $newsData
  1034. * 数组结构:
  1035. * array(
  1036. * "0"=>array(
  1037. * 'Title'=>'msg title',
  1038. * 'Description'=>'summary text',
  1039. * 'PicUrl'=>'http://www.domain.com/1.jpg',
  1040. * 'Url'=>'http://www.domain.com/1.html'
  1041. * ),
  1042. * "1"=>....
  1043. * )
  1044. */
  1045. public function news($newsData = [])
  1046. {
  1047. $FuncFlag = $this->_funcflag ? 1 : 0;
  1048. $count = count($newsData);
  1049. $msg = [
  1050. 'ToUserName' => $this->getRevFrom(),
  1051. 'FromUserName' => $this->getRevTo(),
  1052. 'MsgType' => self::MSGTYPE_NEWS,
  1053. 'CreateTime' => time(),
  1054. 'ArticleCount' => $count,
  1055. 'Articles' => $newsData,
  1056. 'FuncFlag' => $FuncFlag,
  1057. ];
  1058. $this->Message($msg);
  1059. return $this;
  1060. }
  1061. /**
  1062. *
  1063. * 回复微信服务器, 此函数支持链式操作
  1064. * Example: $this->text('msg tips')->reply();
  1065. * @param string $msg 要发送的信息, 默认取$this->_msg
  1066. * @param bool $return 是否返回信息而不抛出到浏览器 默认:否
  1067. */
  1068. public function reply($msg = [], $return = false)
  1069. {
  1070. if (empty($msg)) {
  1071. if (empty($this->_msg)) //防止不先设置回复内容,直接调用reply方法导致异常
  1072. return false;
  1073. $msg = $this->_msg;
  1074. }
  1075. $xmldata = $this->xml_encode($msg);
  1076. $this->log($xmldata);
  1077. if ($this->encrypt_type == 'aes') { //如果来源消息为加密方式
  1078. $pc = new Prpcrypt($this->encodingAesKey);
  1079. $array = $pc->encrypt($xmldata, $this->appid);
  1080. $ret = $array[0];
  1081. if ($ret != 0) {
  1082. $this->log('encrypt err!');
  1083. return false;
  1084. }
  1085. $timestamp = time();
  1086. $nonce = rand(77, 999) * rand(605, 888) * rand(11, 99);
  1087. $encrypt = $array[1];
  1088. $tmpArr = [$this->token, $timestamp, $nonce, $encrypt];//比普通公众平台多了一个加密的密文
  1089. sort($tmpArr, SORT_STRING);
  1090. $signature = implode($tmpArr);
  1091. $signature = sha1($signature);
  1092. $xmldata = $this->generate($encrypt, $signature, $timestamp, $nonce);
  1093. $this->log($xmldata);
  1094. }
  1095. if ($return)
  1096. return $xmldata;
  1097. else
  1098. echo $xmldata;
  1099. }
  1100. /**
  1101. * xml格式加密,仅请求为加密方式时再用
  1102. */
  1103. private function generate($encrypt, $signature, $timestamp, $nonce)
  1104. {
  1105. //格式化加密信息
  1106. $format
  1107. = "<xml>
  1108. <Encrypt><![CDATA[%s]]></Encrypt>
  1109. <MsgSignature><![CDATA[%s]]></MsgSignature>
  1110. <TimeStamp>%s</TimeStamp>
  1111. <Nonce><![CDATA[%s]]></Nonce>
  1112. </xml>";
  1113. return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
  1114. }
  1115. /**
  1116. * GET 请求
  1117. * @param string $url
  1118. */
  1119. private function http_get($url)
  1120. {
  1121. $oCurl = curl_init();
  1122. if (stripos($url, "https://") !== FALSE) {
  1123. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  1124. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  1125. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  1126. }
  1127. curl_setopt($oCurl, CURLOPT_URL, $url);
  1128. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  1129. $sContent = curl_exec($oCurl);
  1130. $aStatus = curl_getinfo($oCurl);
  1131. curl_close($oCurl);
  1132. if (intval($aStatus["http_code"]) == 200) {
  1133. return $sContent;
  1134. } else {
  1135. return false;
  1136. }
  1137. }
  1138. /**
  1139. * POST 请求
  1140. * @param string $url
  1141. * @param array $param
  1142. * @param boolean $post_file 是否文件上传
  1143. * @return string content
  1144. */
  1145. private function http_post($url, $param, $post_file = false)
  1146. {
  1147. $oCurl = curl_init();
  1148. if (stripos($url, "https://") !== FALSE) {
  1149. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  1150. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  1151. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  1152. }
  1153. if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
  1154. $is_curlFile = true;
  1155. } else {
  1156. $is_curlFile = false;
  1157. if (defined('CURLOPT_SAFE_UPLOAD')) {
  1158. curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
  1159. }
  1160. }
  1161. if (is_string($param)) {
  1162. $strPOST = $param;
  1163. } elseif ($post_file) {
  1164. if ($is_curlFile) {
  1165. foreach ($param as $key => $val) {
  1166. if (substr($val, 0, 1) == '@') {
  1167. $param[$key] = new \CURLFile(realpath(substr($val, 1)));
  1168. }
  1169. }
  1170. }
  1171. $strPOST = $param;
  1172. } else {
  1173. $aPOST = [];
  1174. foreach ($param as $key => $val) {
  1175. $aPOST[] = $key . "=" . urlencode($val);
  1176. }
  1177. $strPOST = join("&", $aPOST);
  1178. }
  1179. curl_setopt($oCurl, CURLOPT_URL, $url);
  1180. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  1181. curl_setopt($oCurl, CURLOPT_POST, true);
  1182. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
  1183. $sContent = curl_exec($oCurl);
  1184. $aStatus = curl_getinfo($oCurl);
  1185. curl_close($oCurl);
  1186. if (intval($aStatus["http_code"]) == 200) {
  1187. return $sContent;
  1188. } else {
  1189. return false;
  1190. }
  1191. }
  1192. /**
  1193. * 设置缓存,按需重载
  1194. * @param string $cachename
  1195. * @param mixed $value
  1196. * @param int $expired
  1197. * @return boolean
  1198. */
  1199. protected function setCache($cachename, $value, $expired)
  1200. {
  1201. return Cache::set($cachename, $value, $expired);
  1202. }
  1203. /**
  1204. * 获取缓存,按需重载
  1205. * @param string $cachename
  1206. * @return mixed
  1207. */
  1208. protected function getCache($cachename)
  1209. {
  1210. return Cache::get($cachename);
  1211. }
  1212. /**
  1213. * 清除缓存,按需重载
  1214. * @param string $cachename
  1215. * @return boolean
  1216. */
  1217. protected function removeCache($cachename)
  1218. {
  1219. //TODO: remove cache implementation
  1220. return false;
  1221. }
  1222. /**
  1223. * 获取access_token
  1224. * @param string $appid 如在类初始化时已提供,则可为空
  1225. * @param string $appsecret 如在类初始化时已提供,则可为空
  1226. * @param string $token 手动指定access_token,非必要情况不建议用
  1227. */
  1228. public function checkAuth($appid = '', $appsecret = '', $token = '')
  1229. {
  1230. if (!$appid || !$appsecret) {
  1231. $appid = $this->appid;
  1232. $appsecret = $this->appsecret;
  1233. }
  1234. if ($token) { //手动指定token,优先使用
  1235. $this->access_token = $token;
  1236. return $this->access_token;
  1237. }
  1238. $authname = 'wechat_access_token' . $appid;
  1239. /*if ($rs = $this->getCache($authname)) {
  1240. $this->access_token = $rs;
  1241. return $rs;
  1242. }*/
  1243. if ($rs = Db::name('weixin_accesstoken')
  1244. ->where('app_id',$appid)
  1245. ->where('expired_time','>',time())
  1246. ->order('create_time desc')
  1247. ->value('access_token')
  1248. ) {
  1249. $this->access_token = $rs;
  1250. return $rs;
  1251. }
  1252. $result = $this->http_get(self::API_URL_PREFIX . self::AUTH_URL . 'appid=' . $appid . '&secret=' . $appsecret);
  1253. if ($result) {
  1254. $json = json_decode($result, true);
  1255. if (!$json || isset($json['errcode'])) {
  1256. $this->errCode = $json['errcode'];
  1257. $this->errMsg = $json['errmsg'];
  1258. return false;
  1259. }
  1260. $this->access_token = $json['access_token'];
  1261. $expire = $json['expires_in'] ? intval($json['expires_in']) - 100 : 3600;
  1262. //$this->setCache($authname, $this->access_token, $expire);
  1263. Db::name('weixin_accesstoken')->insert([
  1264. 'app_id' => $appid,
  1265. 'expired_time' => $expire + time(),
  1266. 'access_token' => $this->access_token,
  1267. 'create_time' => date('Y-m-d H:i:s'),
  1268. ]);
  1269. return $this->access_token;
  1270. }
  1271. return false;
  1272. }
  1273. /**
  1274. * 删除验证数据
  1275. * @param string $appid
  1276. */
  1277. public function resetAuth($appid = '')
  1278. {
  1279. if (!$appid) $appid = $this->appid;
  1280. $this->access_token = '';
  1281. $authname = 'wechat_access_token' . $appid;
  1282. $this->removeCache($authname);
  1283. return true;
  1284. }
  1285. /**
  1286. * 删除JSAPI授权TICKET
  1287. * @param string $appid 用于多个appid时使用
  1288. */
  1289. public function resetJsTicket($appid = '')
  1290. {
  1291. if (!$appid) $appid = $this->appid;
  1292. $this->jsapi_ticket = '';
  1293. $authname = 'wechat_jsapi_ticket' . $appid;
  1294. $this->removeCache($authname);
  1295. return true;
  1296. }
  1297. /**
  1298. * 获取JSAPI授权TICKET
  1299. * @param string $appid 用于多个appid时使用,可空
  1300. * @param string $jsapi_ticket 手动指定jsapi_ticket,非必要情况不建议用
  1301. */
  1302. public function getJsTicket($appid = '', $jsapi_ticket = '')
  1303. {
  1304. if (!$this->access_token && !$this->checkAuth()) return false;
  1305. if (!$appid) $appid = $this->appid;
  1306. if ($jsapi_ticket) { //手动指定token,优先使用
  1307. $this->jsapi_ticket = $jsapi_ticket;
  1308. return $this->jsapi_ticket;
  1309. }
  1310. $authname = 'wechat_jsapi_ticket' . $appid;
  1311. if ($rs = $this->getCache($authname)) {
  1312. $this->jsapi_ticket = $rs;
  1313. return $rs;
  1314. }
  1315. $result = $this->http_get(self::API_URL_PREFIX . self::GET_TICKET_URL . 'access_token=' . $this->access_token . '&type=jsapi');
  1316. if ($result) {
  1317. $json = json_decode($result, true);
  1318. if (!$json || !empty($json['errcode'])) {
  1319. $this->errCode = $json['errcode'];
  1320. $this->errMsg = $json['errmsg'];
  1321. return false;
  1322. }
  1323. $this->jsapi_ticket = $json['ticket'];
  1324. $expire = $json['expires_in'] ? intval($json['expires_in']) - 100 : 3600;
  1325. $this->setCache($authname, $this->jsapi_ticket, $expire);
  1326. return $this->jsapi_ticket;
  1327. }
  1328. return false;
  1329. }
  1330. /**
  1331. * 获取JsApi使用签名
  1332. * @param string $url 网页的URL,自动处理#及其后面部分
  1333. * @param string $timestamp 当前时间戳 (为空则自动生成)
  1334. * @param string $noncestr 随机串 (为空则自动生成)
  1335. * @param string $appid 用于多个appid时使用,可空
  1336. * @return array|bool 返回签名字串
  1337. */
  1338. public function getJsSign($url, $timestamp = 0, $noncestr = '', $appid = '')
  1339. {
  1340. if (!$this->jsapi_ticket && !$this->getJsTicket($appid) || !$url) return false;
  1341. if (!$timestamp)
  1342. $timestamp = time();
  1343. if (!$noncestr)
  1344. $noncestr = $this->generateNonceStr();
  1345. $ret = strpos($url, '#');
  1346. if ($ret)
  1347. $url = substr($url, 0, $ret);
  1348. $url = trim($url);
  1349. if (empty($url))
  1350. return false;
  1351. $arrdata = ["timestamp" => $timestamp, "noncestr" => $noncestr, "url" => $url, "jsapi_ticket" => $this->jsapi_ticket];
  1352. $sign = $this->getSignature($arrdata);
  1353. if (!$sign)
  1354. return false;
  1355. $signPackage = [
  1356. "appId" => $this->appid,
  1357. "nonceStr" => $noncestr,
  1358. "timestamp" => $timestamp,
  1359. "url" => $url,
  1360. "signature" => $sign,
  1361. ];
  1362. return $signPackage;
  1363. }
  1364. /**
  1365. * 获取卡券签名cardSign
  1366. * @param string $card_type 卡券的类型,不可为空,官方jssdk文档说这个值可空,但签名验证工具又必填这个值,官方文档到处是坑,
  1367. * @param string $card_id 卡券的ID,可空
  1368. * @param string $location_id 卡券的适用门店ID,可空
  1369. * @param string $timestamp 当前时间戳 (为空则自动生成)
  1370. * @param string $noncestr 随机串 (为空则自动生成)
  1371. * @param string $appid 用于多个appid时使用,可空
  1372. * @return array|bool 返回签名字串
  1373. */
  1374. public function getCardSign($card_type = '', $card_id = '', $code = '', $location_id = '', $timestamp = 0, $noncestr = '', $appid = '')
  1375. {
  1376. if (!$this->api_ticket && !$this->getJsCardTicket($appid)) return false;
  1377. if (!$timestamp)
  1378. $timestamp = time();
  1379. if (!$noncestr)
  1380. $noncestr = $this->generateNonceStr();
  1381. $arrdata = ["api_ticket" => $this->api_ticket, "app_id" => $this->appid, "card_id" => $card_id, "code" => $code, "card_type" => $card_type, "location_id" => $location_id, "timestamp" => $timestamp, "noncestr" => $noncestr];
  1382. $sign = $this->getTicketSignature($arrdata);
  1383. if (!$sign)
  1384. return false;
  1385. $signPackage = [
  1386. "cardType" => $card_type,
  1387. "cardId" => $card_id,
  1388. "shopId" => $location_id, //location_id就是shopId
  1389. "nonceStr" => $noncestr,
  1390. "timestamp" => $timestamp,
  1391. "cardSign" => $sign,
  1392. ];
  1393. return $signPackage;
  1394. }
  1395. /**
  1396. * 微信api不支持中文转义的json结构
  1397. * @param array $arr
  1398. */
  1399. static function json_encode($arr)
  1400. {
  1401. if (count($arr) == 0) return "[]";
  1402. $parts = [];
  1403. $is_list = false;
  1404. //Find out if the given array is a numerical array
  1405. $keys = array_keys($arr);
  1406. $max_length = count($arr) - 1;
  1407. if (($keys [0] === 0) && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
  1408. $is_list = true;
  1409. for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
  1410. if ($i != $keys [$i]) { //A key fails at position check.
  1411. $is_list = false; //It is an associative array.
  1412. break;
  1413. }
  1414. }
  1415. }
  1416. foreach ($arr as $key => $value) {
  1417. if (is_array($value)) { //Custom handling for arrays
  1418. if ($is_list)
  1419. $parts [] = self::json_encode($value); /* :RECURSION: */
  1420. else
  1421. $parts [] = '"' . $key . '":' . self::json_encode($value); /* :RECURSION: */
  1422. } else {
  1423. $str = '';
  1424. if (!$is_list)
  1425. $str = '"' . $key . '":';
  1426. //Custom handling for multiple data types
  1427. if (!is_string($value) && is_numeric($value) && $value < 2000000000)
  1428. $str .= $value; //Numbers
  1429. elseif ($value === false)
  1430. $str .= 'false'; //The booleans
  1431. elseif ($value === true)
  1432. $str .= 'true';
  1433. else
  1434. $str .= '"' . addslashes($value) . '"'; //All other things
  1435. // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
  1436. $parts [] = $str;
  1437. }
  1438. }
  1439. $json = implode(',', $parts);
  1440. if ($is_list)
  1441. return '[' . $json . ']'; //Return numerical JSON
  1442. return '{' . $json . '}'; //Return associative JSON
  1443. }
  1444. /**
  1445. * 获取签名
  1446. * @param array $arrdata 签名数组
  1447. * @param string $method 签名方法
  1448. * @return boolean|string 签名值
  1449. */
  1450. public function getSignature($arrdata, $method = "sha1")
  1451. {
  1452. if (!function_exists($method)) return false;
  1453. ksort($arrdata);
  1454. $paramstring = "";
  1455. foreach ($arrdata as $key => $value) {
  1456. if (strlen($paramstring) == 0)
  1457. $paramstring .= $key . "=" . $value;
  1458. else
  1459. $paramstring .= "&" . $key . "=" . $value;
  1460. }
  1461. $Sign = $method($paramstring);
  1462. return $Sign;
  1463. }
  1464. /**
  1465. * 获取微信卡券api_ticket
  1466. * @param string $appid 用于多个appid时使用,可空
  1467. * @param string $api_ticket 手动指定api_ticket,非必要情况不建议用
  1468. */
  1469. public function getJsCardTicket($appid = '', $api_ticket = '')
  1470. {
  1471. if (!$this->access_token && !$this->checkAuth()) return false;
  1472. if (!$appid) $appid = $this->appid;
  1473. if ($api_ticket) { //手动指定token,优先使用
  1474. $this->api_ticket = $api_ticket;
  1475. return $this->api_ticket;
  1476. }
  1477. $authname = 'wechat_api_ticket_wxcard' . $appid;
  1478. if ($rs = $this->getCache($authname)) {
  1479. $this->api_ticket = $rs;
  1480. return $rs;
  1481. }
  1482. $result = $this->http_get(self::API_URL_PREFIX . self::GET_TICKET_URL . 'access_token=' . $this->access_token . '&type=wx_card');
  1483. if ($result) {
  1484. $json = json_decode($result, true);
  1485. if (!$json || !empty($json['errcode'])) {
  1486. $this->errCode = $json['errcode'];
  1487. $this->errMsg = $json['errmsg'];
  1488. return false;
  1489. }
  1490. $this->api_ticket = $json['ticket'];
  1491. $expire = $json['expires_in'] ? intval($json['expires_in']) - 100 : 3600;
  1492. $this->setCache($authname, $this->api_ticket, $expire);
  1493. return $this->api_ticket;
  1494. }
  1495. return false;
  1496. }
  1497. /**
  1498. * 获取微信卡券签名
  1499. * @param array $arrdata 签名数组
  1500. * @param string $method 签名方法
  1501. * @return boolean|string 签名值
  1502. */
  1503. public function getTicketSignature($arrdata, $method = "sha1")
  1504. {
  1505. if (!function_exists($method)) return false;
  1506. $newArray = [];
  1507. foreach ($arrdata as $key => $value) {
  1508. array_push($newArray, (string)$value);
  1509. }
  1510. sort($newArray, SORT_STRING);
  1511. return $method(implode($newArray));
  1512. }
  1513. /**
  1514. * 生成随机字串
  1515. * @param number $length 长度,默认为16,最长为32字节
  1516. * @return string
  1517. */
  1518. public function generateNonceStr($length = 16)
  1519. {
  1520. // 密码字符集,可任意添加你需要的字符
  1521. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  1522. $str = "";
  1523. for ($i = 0; $i < $length; $i++) {
  1524. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  1525. }
  1526. return $str;
  1527. }
  1528. /**
  1529. * 获取微信服务器IP地址列表
  1530. * @return array('127.0.0.1','127.0.0.1')
  1531. */
  1532. public function getServerIp()
  1533. {
  1534. if (!$this->access_token && !$this->checkAuth()) return false;
  1535. $result = $this->http_get(self::API_URL_PREFIX . self::CALLBACKSERVER_GET_URL . 'access_token=' . $this->access_token);
  1536. if ($result) {
  1537. $json = json_decode($result, true);
  1538. if (!$json || isset($json['errcode'])) {
  1539. $this->errCode = $json['errcode'];
  1540. $this->errMsg = $json['errmsg'];
  1541. return false;
  1542. }
  1543. return $json['ip_list'];
  1544. }
  1545. return false;
  1546. }
  1547. /**
  1548. * 创建菜单(认证后的订阅号可用)
  1549. * @param array $data 菜单数组数据
  1550. * example:
  1551. * array (
  1552. * 'button' => array (
  1553. * 0 => array (
  1554. * 'name' => '扫码',
  1555. * 'sub_button' => array (
  1556. * 0 => array (
  1557. * 'type' => 'scancode_waitmsg',
  1558. * 'name' => '扫码带提示',
  1559. * 'key' => 'rselfmenu_0_0',
  1560. * ),
  1561. * 1 => array (
  1562. * 'type' => 'scancode_push',
  1563. * 'name' => '扫码推事件',
  1564. * 'key' => 'rselfmenu_0_1',
  1565. * ),
  1566. * ),
  1567. * ),
  1568. * 1 => array (
  1569. * 'name' => '发图',
  1570. * 'sub_button' => array (
  1571. * 0 => array (
  1572. * 'type' => 'pic_sysphoto',
  1573. * 'name' => '系统拍照发图',
  1574. * 'key' => 'rselfmenu_1_0',
  1575. * ),
  1576. * 1 => array (
  1577. * 'type' => 'pic_photo_or_album',
  1578. * 'name' => '拍照或者相册发图',
  1579. * 'key' => 'rselfmenu_1_1',
  1580. * )
  1581. * ),
  1582. * ),
  1583. * 2 => array (
  1584. * 'type' => 'location_select',
  1585. * 'name' => '发送位置',
  1586. * 'key' => 'rselfmenu_2_0'
  1587. * ),
  1588. * ),
  1589. * )
  1590. * type可以选择为以下几种,其中5-8除了收到菜单事件以外,还会单独收到对应类型的信息。
  1591. * 1、click:点击推事件
  1592. * 2、view:跳转URL
  1593. * 3、scancode_push:扫码推事件
  1594. * 4、scancode_waitmsg:扫码推事件且弹出“消息接收中”提示框
  1595. * 5、pic_sysphoto:弹出系统拍照发图
  1596. * 6、pic_photo_or_album:弹出拍照或者相册发图
  1597. * 7、pic_weixin:弹出微信相册发图器
  1598. * 8、location_select:弹出地理位置选择器
  1599. */
  1600. public function createMenu($data)
  1601. {
  1602. if (!$this->access_token && !$this->checkAuth()) return false;
  1603. $result = $this->http_post(self::API_URL_PREFIX . self::MENU_CREATE_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1604. if ($result) {
  1605. $json = json_decode($result, true);
  1606. if (!$json || !empty($json['errcode'])) {
  1607. $this->errCode = $json['errcode'];
  1608. $this->errMsg = $json['errmsg'];
  1609. return false;
  1610. }
  1611. return true;
  1612. }
  1613. return false;
  1614. }
  1615. /**
  1616. * 获取菜单(认证后的订阅号可用)
  1617. * @return array('menu'=>array(....s))
  1618. */
  1619. public function getMenu()
  1620. {
  1621. if (!$this->access_token && !$this->checkAuth()) return false;
  1622. $result = $this->http_get(self::API_URL_PREFIX . self::MENU_GET_URL . 'access_token=' . $this->access_token);
  1623. if ($result) {
  1624. $json = json_decode($result, true);
  1625. if (!$json || isset($json['errcode'])) {
  1626. $this->errCode = $json['errcode'];
  1627. $this->errMsg = $json['errmsg'];
  1628. return false;
  1629. }
  1630. return $json;
  1631. }
  1632. return false;
  1633. }
  1634. /**
  1635. * 删除菜单(认证后的订阅号可用)
  1636. * @return boolean
  1637. */
  1638. public function deleteMenu()
  1639. {
  1640. if (!$this->access_token && !$this->checkAuth()) return false;
  1641. $result = $this->http_get(self::API_URL_PREFIX . self::MENU_DELETE_URL . 'access_token=' . $this->access_token);
  1642. if ($result) {
  1643. $json = json_decode($result, true);
  1644. if (!$json || !empty($json['errcode'])) {
  1645. $this->errCode = $json['errcode'];
  1646. $this->errMsg = $json['errmsg'];
  1647. return false;
  1648. }
  1649. return true;
  1650. }
  1651. return false;
  1652. }
  1653. /**
  1654. * 创建个性化菜单(认证后的订阅号可用)
  1655. * @param array $data
  1656. * @return bool
  1657. *
  1658. */
  1659. public function addconditionalMenu($data)
  1660. {
  1661. if (!$this->access_token && !$this->checkAuth()) return false;
  1662. $result = $this->http_post(self::API_URL_PREFIX . self::MENU_ADDCONDITIONAL_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1663. if ($result) {
  1664. $json = json_decode($result, true);
  1665. if (!$json || !empty($json['errcode'])) {
  1666. $this->errCode = $json['errcode'];
  1667. $this->errMsg = $json['errmsg'];
  1668. return false;
  1669. }
  1670. return true;
  1671. }
  1672. return false;
  1673. }
  1674. /**
  1675. * 删除个性化菜单(认证后的订阅号可用)
  1676. * @param $data {"menuid":"208379533"}
  1677. *
  1678. * @return bool
  1679. */
  1680. public function delconditionalMenu($data)
  1681. {
  1682. if (!$this->access_token && !$this->checkAuth()) return false;
  1683. $result = $this->http_post(self::API_URL_PREFIX . self::MENU_DELCONDITIONAL_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1684. if ($result) {
  1685. $json = json_decode($result, true);
  1686. if (!$json || !empty($json['errcode'])) {
  1687. $this->errCode = $json['errcode'];
  1688. $this->errMsg = $json['errmsg'];
  1689. return false;
  1690. }
  1691. return true;
  1692. }
  1693. return false;
  1694. }
  1695. /**
  1696. * 测试个性化菜单匹配结果(认证后的订阅号可用)
  1697. * @param $data {"user_id":"weixin"} user_id可以是粉丝的OpenID,也可以是粉丝的微信号
  1698. *
  1699. * @return bool|array('button'=>array(....s))
  1700. */
  1701. public function trymatchMenu($data)
  1702. {
  1703. if (!$this->access_token && !$this->checkAuth()) return false;
  1704. $result = $this->http_post(self::API_URL_PREFIX . self::MENU_TRYMATCH_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1705. if ($result) {
  1706. $json = json_decode($result, true);
  1707. if (!$json || !empty($json['errcode'])) {
  1708. $this->errCode = $json['errcode'];
  1709. $this->errMsg = $json['errmsg'];
  1710. return false;
  1711. }
  1712. return $json;
  1713. }
  1714. return false;
  1715. }
  1716. /**
  1717. * 上传临时素材,有效期为3天(认证后的订阅号可用)
  1718. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  1719. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  1720. * 注意:临时素材的media_id是可复用的!
  1721. * @param array $data {"media":'@Path\filename.jpg'}
  1722. * @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
  1723. * @return boolean|array
  1724. */
  1725. public function uploadMedia($data, $type)
  1726. {
  1727. if (!$this->access_token && !$this->checkAuth()) return false;
  1728. //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
  1729. $result = $this->http_post(self::API_URL_PREFIX . self::MEDIA_UPLOAD_URL . 'access_token=' . $this->access_token . '&type=' . $type, $data, true);
  1730. if ($result) {
  1731. $json = json_decode($result, true);
  1732. if (!$json || !empty($json['errcode'])) {
  1733. $this->errCode = $json['errcode'];
  1734. $this->errMsg = $json['errmsg'];
  1735. return false;
  1736. }
  1737. return $json;
  1738. }
  1739. return false;
  1740. }
  1741. /**
  1742. * 获取临时素材(认证后的订阅号可用)
  1743. * @param string $media_id 媒体文件id
  1744. * @param boolean $is_video 是否为视频文件,默认为否
  1745. * @return raw data
  1746. */
  1747. public function getMedia($media_id, $is_video = false)
  1748. {
  1749. if (!$this->access_token && !$this->checkAuth()) return false;
  1750. //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
  1751. //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
  1752. $url_prefix = $is_video ? str_replace('https', 'http', self::API_URL_PREFIX) : self::API_URL_PREFIX;
  1753. $result = $this->http_get($url_prefix . self::MEDIA_GET_URL . 'access_token=' . $this->access_token . '&media_id=' . $media_id);
  1754. if ($result) {
  1755. if (is_string($result)) {
  1756. $json = json_decode($result, true);
  1757. if (isset($json['errcode'])) {
  1758. $this->errCode = $json['errcode'];
  1759. $this->errMsg = $json['errmsg'];
  1760. return false;
  1761. }
  1762. }
  1763. return $result;
  1764. }
  1765. return false;
  1766. }
  1767. /**
  1768. * 上传图片,本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。 (认证后的订阅号可用)
  1769. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  1770. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  1771. * @param array $data {"media":'@Path\filename.jpg'}
  1772. *
  1773. * @return boolean|array
  1774. */
  1775. public function uploadImg($data)
  1776. {
  1777. if (!$this->access_token && !$this->checkAuth()) return false;
  1778. //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
  1779. $result = $this->http_post(self::API_URL_PREFIX . self::MEDIA_UPLOADIMG_URL . 'access_token=' . $this->access_token, $data, true);
  1780. if ($result) {
  1781. $json = json_decode($result, true);
  1782. if (!$json || !empty($json['errcode'])) {
  1783. $this->errCode = $json['errcode'];
  1784. $this->errMsg = $json['errmsg'];
  1785. return false;
  1786. }
  1787. return $json;
  1788. }
  1789. return false;
  1790. }
  1791. /**
  1792. * 上传永久素材(认证后的订阅号可用)
  1793. * 新增的永久素材也可以在公众平台官网素材管理模块中看到
  1794. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  1795. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  1796. * @param array $data {"media":'@Path\filename.jpg'}
  1797. * @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
  1798. * @param boolean $is_video 是否为视频文件,默认为否
  1799. * @param array $video_info 视频信息数组,非视频素材不需要提供 array('title'=>'视频标题','introduction'=>'描述')
  1800. * @return boolean|array
  1801. */
  1802. public function uploadForeverMedia($data, $type, $is_video = false, $video_info = [])
  1803. {
  1804. if (!$this->access_token && !$this->checkAuth()) return false;
  1805. //#TODO 暂不确定此接口是否需要让视频文件走http协议
  1806. //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
  1807. //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
  1808. //当上传视频文件时,附加视频文件信息
  1809. if ($is_video) $data['description'] = self::json_encode($video_info);
  1810. $result = $this->http_post(self::API_URL_PREFIX . self::MEDIA_FOREVER_UPLOAD_URL . 'access_token=' . $this->access_token . '&type=' . $type, $data, true);
  1811. if ($result) {
  1812. $json = json_decode($result, true);
  1813. if (!$json || !empty($json['errcode'])) {
  1814. $this->errCode = $json['errcode'];
  1815. $this->errMsg = $json['errmsg'];
  1816. return false;
  1817. }
  1818. return $json;
  1819. }
  1820. return false;
  1821. }
  1822. /**
  1823. * 上传永久图文素材(认证后的订阅号可用)
  1824. * 新增的永久素材也可以在公众平台官网素材管理模块中看到
  1825. * @param array $data 消息结构{"articles":[{...}]}
  1826. * @return boolean|array
  1827. */
  1828. public function uploadForeverArticles($data)
  1829. {
  1830. if (!$this->access_token && !$this->checkAuth()) return false;
  1831. $result = $this->http_post(self::API_URL_PREFIX . self::MEDIA_FOREVER_NEWS_UPLOAD_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1832. if ($result) {
  1833. $json = json_decode($result, true);
  1834. if (!$json || !empty($json['errcode'])) {
  1835. $this->errCode = $json['errcode'];
  1836. $this->errMsg = $json['errmsg'];
  1837. return false;
  1838. }
  1839. return $json;
  1840. }
  1841. return false;
  1842. }
  1843. /**
  1844. * 修改永久图文素材(认证后的订阅号可用)
  1845. * 永久素材也可以在公众平台官网素材管理模块中看到
  1846. * @param string $media_id 图文素材id
  1847. * @param array $data 消息结构{"articles":[{...}]}
  1848. * @param int $index 更新的文章在图文素材的位置,第一篇为0,仅多图文使用
  1849. * @return boolean|array
  1850. */
  1851. public function updateForeverArticles($media_id, $data, $index = 0)
  1852. {
  1853. if (!$this->access_token && !$this->checkAuth()) return false;
  1854. if (!isset($data['media_id'])) $data['media_id'] = $media_id;
  1855. if (!isset($data['index'])) $data['index'] = $index;
  1856. $result = $this->http_post(self::API_URL_PREFIX . self::MEDIA_FOREVER_NEWS_UPDATE_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1857. if ($result) {
  1858. $json = json_decode($result, true);
  1859. if (!$json || !empty($json['errcode'])) {
  1860. $this->errCode = $json['errcode'];
  1861. $this->errMsg = $json['errmsg'];
  1862. return false;
  1863. }
  1864. return $json;
  1865. }
  1866. return false;
  1867. }
  1868. /**
  1869. * 获取永久素材(认证后的订阅号可用)
  1870. * 返回图文消息数组或二进制数据,失败返回false
  1871. * @param string $media_id 媒体文件id
  1872. * @param boolean $is_video 是否为视频文件,默认为否
  1873. * @return boolean|array|raw data
  1874. */
  1875. public function getForeverMedia($media_id, $is_video = false)
  1876. {
  1877. if (!$this->access_token && !$this->checkAuth()) return false;
  1878. $data = ['media_id' => $media_id];
  1879. //#TODO 暂不确定此接口是否需要让视频文件走http协议
  1880. //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
  1881. //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
  1882. $result = $this->http_post(self::API_URL_PREFIX . self::MEDIA_FOREVER_GET_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1883. if ($result) {
  1884. if (is_string($result)) {
  1885. $json = json_decode($result, true);
  1886. if ($json) {
  1887. if (isset($json['errcode'])) {
  1888. $this->errCode = $json['errcode'];
  1889. $this->errMsg = $json['errmsg'];
  1890. return false;
  1891. }
  1892. return $json;
  1893. } else {
  1894. return $result;
  1895. }
  1896. }
  1897. return $result;
  1898. }
  1899. return false;
  1900. }
  1901. /**
  1902. * 删除永久素材(认证后的订阅号可用)
  1903. * @param string $media_id 媒体文件id
  1904. * @return boolean
  1905. */
  1906. public function delForeverMedia($media_id)
  1907. {
  1908. if (!$this->access_token && !$this->checkAuth()) return false;
  1909. $data = ['media_id' => $media_id];
  1910. $result = $this->http_post(self::API_URL_PREFIX . self::MEDIA_FOREVER_DEL_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1911. if ($result) {
  1912. $json = json_decode($result, true);
  1913. if (!$json || !empty($json['errcode'])) {
  1914. $this->errCode = $json['errcode'];
  1915. $this->errMsg = $json['errmsg'];
  1916. return false;
  1917. }
  1918. return true;
  1919. }
  1920. return false;
  1921. }
  1922. /**
  1923. * 获取永久素材列表(认证后的订阅号可用)
  1924. * @param string $type 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
  1925. * @param int $offset 全部素材的偏移位置,0表示从第一个素材
  1926. * @param int $count 返回素材的数量,取值在1到20之间
  1927. * @return boolean|array
  1928. * 返回数组格式:
  1929. * array(
  1930. * 'total_count'=>0, //该类型的素材的总数
  1931. * 'item_count'=>0, //本次调用获取的素材的数量
  1932. * 'item'=>array() //素材列表数组,内容定义请参考官方文档
  1933. * )
  1934. */
  1935. public function getForeverList($type, $offset, $count)
  1936. {
  1937. if (!$this->access_token && !$this->checkAuth()) return false;
  1938. $data = [
  1939. 'type' => $type,
  1940. 'offset' => $offset,
  1941. 'count' => $count,
  1942. ];
  1943. $result = $this->http_post(self::API_URL_PREFIX . self::MEDIA_FOREVER_BATCHGET_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1944. if ($result) {
  1945. $json = json_decode($result, true);
  1946. if (isset($json['errcode'])) {
  1947. $this->errCode = $json['errcode'];
  1948. $this->errMsg = $json['errmsg'];
  1949. return false;
  1950. }
  1951. return $json;
  1952. }
  1953. return false;
  1954. }
  1955. /**
  1956. * 获取永久素材总数(认证后的订阅号可用)
  1957. * @return boolean|array
  1958. * 返回数组格式:
  1959. * array(
  1960. * 'voice_count'=>0, //语音总数量
  1961. * 'video_count'=>0, //视频总数量
  1962. * 'image_count'=>0, //图片总数量
  1963. * 'news_count'=>0 //图文总数量
  1964. * )
  1965. */
  1966. public function getForeverCount()
  1967. {
  1968. if (!$this->access_token && !$this->checkAuth()) return false;
  1969. $result = $this->http_get(self::API_URL_PREFIX . self::MEDIA_FOREVER_COUNT_URL . 'access_token=' . $this->access_token);
  1970. if ($result) {
  1971. $json = json_decode($result, true);
  1972. if (isset($json['errcode'])) {
  1973. $this->errCode = $json['errcode'];
  1974. $this->errMsg = $json['errmsg'];
  1975. return false;
  1976. }
  1977. return $json;
  1978. }
  1979. return false;
  1980. }
  1981. /**
  1982. * 上传图文消息素材,用于群发(认证后的订阅号可用)
  1983. * @param array $data 消息结构{"articles":[{...}]}
  1984. * @return boolean|array
  1985. */
  1986. public function uploadArticles($data)
  1987. {
  1988. if (!$this->access_token && !$this->checkAuth()) return false;
  1989. $result = $this->http_post(self::API_URL_PREFIX . self::MEDIA_UPLOADNEWS_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  1990. if ($result) {
  1991. $json = json_decode($result, true);
  1992. if (!$json || !empty($json['errcode'])) {
  1993. $this->errCode = $json['errcode'];
  1994. $this->errMsg = $json['errmsg'];
  1995. return false;
  1996. }
  1997. return $json;
  1998. }
  1999. return false;
  2000. }
  2001. /**
  2002. * 上传视频素材(认证后的订阅号可用)
  2003. * @param array $data 消息结构
  2004. * {
  2005. * "media_id"=>"", //通过上传媒体接口得到的MediaId
  2006. * "title"=>"TITLE", //视频标题
  2007. * "description"=>"Description" //视频描述
  2008. * }
  2009. * @return boolean|array
  2010. * {
  2011. * "type":"video",
  2012. * "media_id":"mediaid",
  2013. * "created_at":1398848981
  2014. * }
  2015. */
  2016. public function uploadMpVideo($data)
  2017. {
  2018. if (!$this->access_token && !$this->checkAuth()) return false;
  2019. $result = $this->http_post(self::UPLOAD_MEDIA_URL . self::MEDIA_VIDEO_UPLOAD . 'access_token=' . $this->access_token, self::json_encode($data));
  2020. if ($result) {
  2021. $json = json_decode($result, true);
  2022. if (!$json || !empty($json['errcode'])) {
  2023. $this->errCode = $json['errcode'];
  2024. $this->errMsg = $json['errmsg'];
  2025. return false;
  2026. }
  2027. return $json;
  2028. }
  2029. return false;
  2030. }
  2031. /**
  2032. * 高级群发消息, 根据OpenID列表群发图文消息(订阅号不可用)
  2033. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  2034. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  2035. * @param array $data 消息结构
  2036. * {
  2037. * "touser"=>array(
  2038. * "OPENID1",
  2039. * "OPENID2"
  2040. * ),
  2041. * "msgtype"=>"mpvideo",
  2042. * // 在下面5种类型中选择对应的参数内容
  2043. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  2044. * // text => array ( "content" => "hello")
  2045. * }
  2046. * @return boolean|array
  2047. */
  2048. public function sendMassMessage($data)
  2049. {
  2050. if (!$this->access_token && !$this->checkAuth()) return false;
  2051. $result = $this->http_post(self::API_URL_PREFIX . self::MASS_SEND_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2052. if ($result) {
  2053. $json = json_decode($result, true);
  2054. if (!$json || !empty($json['errcode'])) {
  2055. $this->errCode = $json['errcode'];
  2056. $this->errMsg = $json['errmsg'];
  2057. return false;
  2058. }
  2059. return $json;
  2060. }
  2061. return false;
  2062. }
  2063. /**
  2064. * 高级群发消息, 根据群组id群发图文消息(认证后的订阅号可用)
  2065. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  2066. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  2067. * @param array $data 消息结构
  2068. * {
  2069. * "filter"=>array(
  2070. * "is_to_all"=>False, //是否群发给所有用户.True不用分组id,False需填写分组id
  2071. * "group_id"=>"2" //群发的分组id
  2072. * ),
  2073. * "msgtype"=>"mpvideo",
  2074. * // 在下面5种类型中选择对应的参数内容
  2075. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  2076. * // text => array ( "content" => "hello")
  2077. * }
  2078. * @return boolean|array
  2079. */
  2080. public function sendGroupMassMessage($data)
  2081. {
  2082. if (!$this->access_token && !$this->checkAuth()) return false;
  2083. $result = $this->http_post(self::API_URL_PREFIX . self::MASS_SEND_GROUP_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2084. if ($result) {
  2085. $json = json_decode($result, true);
  2086. if (!$json || !empty($json['errcode'])) {
  2087. $this->errCode = $json['errcode'];
  2088. $this->errMsg = $json['errmsg'];
  2089. return false;
  2090. }
  2091. return $json;
  2092. }
  2093. return false;
  2094. }
  2095. /**
  2096. * 高级群发消息, 删除群发图文消息(认证后的订阅号可用)
  2097. * @param int $msg_id 消息id
  2098. * @return boolean|array
  2099. */
  2100. public function deleteMassMessage($msg_id)
  2101. {
  2102. if (!$this->access_token && !$this->checkAuth()) return false;
  2103. $result = $this->http_post(self::API_URL_PREFIX . self::MASS_DELETE_URL . 'access_token=' . $this->access_token, self::json_encode(['msg_id' => $msg_id]));
  2104. if ($result) {
  2105. $json = json_decode($result, true);
  2106. if (!$json || !empty($json['errcode'])) {
  2107. $this->errCode = $json['errcode'];
  2108. $this->errMsg = $json['errmsg'];
  2109. return false;
  2110. }
  2111. return true;
  2112. }
  2113. return false;
  2114. }
  2115. /**
  2116. * 高级群发消息, 预览群发消息(认证后的订阅号可用)
  2117. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  2118. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  2119. * @param array $data 消息结构
  2120. * {
  2121. * "touser"=>"OPENID",
  2122. * "msgtype"=>"mpvideo",
  2123. * // 在下面5种类型中选择对应的参数内容
  2124. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  2125. * // text => array ( "content" => "hello")
  2126. * }
  2127. * @return boolean|array
  2128. */
  2129. public function previewMassMessage($data)
  2130. {
  2131. if (!$this->access_token && !$this->checkAuth()) return false;
  2132. $result = $this->http_post(self::API_URL_PREFIX . self::MASS_PREVIEW_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2133. if ($result) {
  2134. $json = json_decode($result, true);
  2135. if (!$json || !empty($json['errcode'])) {
  2136. $this->errCode = $json['errcode'];
  2137. $this->errMsg = $json['errmsg'];
  2138. return false;
  2139. }
  2140. return $json;
  2141. }
  2142. return false;
  2143. }
  2144. /**
  2145. * 高级群发消息, 查询群发消息发送状态(认证后的订阅号可用)
  2146. * @param int $msg_id 消息id
  2147. * @return boolean|array
  2148. * {
  2149. * "msg_id":201053012, //群发消息后返回的消息id
  2150. * "msg_status":"SEND_SUCCESS" //消息发送后的状态,SENDING表示正在发送 SEND_SUCCESS表示发送成功
  2151. * }
  2152. */
  2153. public function queryMassMessage($msg_id)
  2154. {
  2155. if (!$this->access_token && !$this->checkAuth()) return false;
  2156. $result = $this->http_post(self::API_URL_PREFIX . self::MASS_QUERY_URL . 'access_token=' . $this->access_token, self::json_encode(['msg_id' => $msg_id]));
  2157. if ($result) {
  2158. $json = json_decode($result, true);
  2159. if (!$json || !empty($json['errcode'])) {
  2160. $this->errCode = $json['errcode'];
  2161. $this->errMsg = $json['errmsg'];
  2162. return false;
  2163. }
  2164. return $json;
  2165. }
  2166. return false;
  2167. }
  2168. /**
  2169. * 创建二维码ticket
  2170. * @param int|string $scene_id 自定义追踪id,临时二维码只能用数值型
  2171. * @param int $type 0:临时二维码;1:数值型永久二维码(此时expire参数无效);2:字符串型永久二维码(此时expire参数无效 3:临时二维码(字符型);)
  2172. * @param int $expire 临时二维码有效期,最大为604800秒
  2173. * @return array('ticket'=>'qrcode字串','expire_seconds'=>2592000,'url'=>'二维码图片解析后的地址')
  2174. */
  2175. public function getQRCode($scene_id, $type = 0, $expire = 2592000)
  2176. {
  2177. if (!$this->access_token && !$this->checkAuth()) {
  2178. return false;
  2179. }
  2180. if (!isset($scene_id)) {
  2181. return false;
  2182. }
  2183. switch ($type) {
  2184. case '0':
  2185. if (!is_numeric($scene_id))
  2186. return false;
  2187. $action_name = 'QR_SCENE';
  2188. $action_info = ['scene' => (['scene_id' => $scene_id])];
  2189. break;
  2190. case '3':
  2191. if (!is_string($scene_id))
  2192. return false;
  2193. $action_name = 'QR_STR_SCENE';
  2194. $action_info = ['scene' => (['scene_str' => $scene_id])];
  2195. break;
  2196. case '1':
  2197. if (!is_numeric($scene_id))
  2198. return false;
  2199. $action_name = 'QR_LIMIT_SCENE';
  2200. $action_info = ['scene' => (['scene_id' => $scene_id])];
  2201. break;
  2202. case '2':
  2203. if (!is_string($scene_id))
  2204. return false;
  2205. $action_name = 'QR_LIMIT_STR_SCENE';
  2206. $action_info = ['scene' => (['scene_str' => $scene_id])];
  2207. break;
  2208. default:
  2209. return false;
  2210. }
  2211. $data = [
  2212. 'action_name' => $action_name,
  2213. 'expire_seconds' => $expire,
  2214. 'action_info' => $action_info,
  2215. ];
  2216. if ($type == '1' || $type == '2') {
  2217. unset($data['expire_seconds']);
  2218. }
  2219. $result = $this->http_post(self::API_URL_PREFIX . self::QRCODE_CREATE_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2220. if ($result) {
  2221. $json = json_decode($result, true);
  2222. if (!$json || !empty($json['errcode'])) {
  2223. $this->errCode = $json['errcode'];
  2224. $this->errMsg = $json['errmsg'];
  2225. return false;
  2226. }
  2227. return $json;
  2228. }
  2229. return false;
  2230. }
  2231. /**
  2232. * 获取二维码图片
  2233. * @param string $ticket 传入由getQRCode方法生成的ticket参数
  2234. * @return string url 返回http地址
  2235. */
  2236. public function getQRUrl($ticket)
  2237. {
  2238. return self::QRCODE_IMG_URL . urlencode($ticket);
  2239. }
  2240. /**
  2241. * 长链接转短链接接口
  2242. * @param string $long_url 传入要转换的长url
  2243. * @return boolean|string url 成功则返回转换后的短url
  2244. */
  2245. public function getShortUrl($long_url)
  2246. {
  2247. if (!$this->access_token && !$this->checkAuth()) return false;
  2248. $data = [
  2249. 'action' => 'long2short',
  2250. 'long_url' => $long_url,
  2251. ];
  2252. $result = $this->http_post(self::API_URL_PREFIX . self::SHORT_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2253. if ($result) {
  2254. $json = json_decode($result, true);
  2255. if (!$json || !empty($json['errcode'])) {
  2256. $this->errCode = $json['errcode'];
  2257. $this->errMsg = $json['errmsg'];
  2258. return false;
  2259. }
  2260. return $json['short_url'];
  2261. }
  2262. return false;
  2263. }
  2264. /**
  2265. * 获取统计数据
  2266. * @param string $type 数据分类(user|article|upstreammsg|interface)分别为(用户分析|图文分析|消息分析|接口分析)
  2267. * @param string $subtype 数据子分类,参考 DATACUBE_URL_ARR 常量定义部分 或者README.md说明文档
  2268. * @param string $begin_date 开始时间
  2269. * @param string $end_date 结束时间
  2270. * @return boolean|array 成功返回查询结果数组,其定义请看官方文档
  2271. */
  2272. public function getDatacube($type, $subtype, $begin_date, $end_date = '')
  2273. {
  2274. if (!$this->access_token && !$this->checkAuth()) return false;
  2275. if (!isset(self::$DATACUBE_URL_ARR[$type]) || !isset(self::$DATACUBE_URL_ARR[$type][$subtype]))
  2276. return false;
  2277. $data = [
  2278. 'begin_date' => $begin_date,
  2279. 'end_date' => $end_date ? $end_date : $begin_date,
  2280. ];
  2281. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::$DATACUBE_URL_ARR[$type][$subtype] . 'access_token=' . $this->access_token, self::json_encode($data));
  2282. if ($result) {
  2283. $json = json_decode($result, true);
  2284. if (!$json || !empty($json['errcode'])) {
  2285. $this->errCode = $json['errcode'];
  2286. $this->errMsg = $json['errmsg'];
  2287. return false;
  2288. }
  2289. return isset($json['list']) ? $json['list'] : $json;
  2290. }
  2291. return false;
  2292. }
  2293. /**
  2294. * 批量获取关注用户列表
  2295. * @param unknown $next_openid
  2296. */
  2297. public function getUserList($next_openid = '')
  2298. {
  2299. if (!$this->access_token && !$this->checkAuth()) return false;
  2300. $result = $this->http_get(self::API_URL_PREFIX . self::USER_GET_URL . 'access_token=' . $this->access_token . '&next_openid=' . $next_openid);
  2301. if ($result) {
  2302. $json = json_decode($result, true);
  2303. if (isset($json['errcode'])) {
  2304. $this->errCode = $json['errcode'];
  2305. $this->errMsg = $json['errmsg'];
  2306. return false;
  2307. }
  2308. return $json;
  2309. }
  2310. return false;
  2311. }
  2312. /**
  2313. * 获取关注者详细信息
  2314. * @param string $openid
  2315. * @param string $lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
  2316. * @return array {subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}
  2317. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  2318. */
  2319. public function getUserInfo($openid, $lang = 'zh_CN')
  2320. {
  2321. if (!$this->access_token && !$this->checkAuth()) return false;
  2322. $result = $this->http_get(self::API_URL_PREFIX . self::USER_INFO_URL . 'access_token=' . $this->access_token . '&openid=' . $openid . '&lang=' . $lang);
  2323. if ($result) {
  2324. $json = json_decode($result, true);
  2325. if (isset($json['errcode'])) {
  2326. $this->errCode = $json['errcode'];
  2327. $this->errMsg = $json['errmsg'];
  2328. return false;
  2329. }
  2330. return $json;
  2331. }
  2332. return false;
  2333. }
  2334. /**
  2335. * 批量获取关注者详细信息
  2336. * @param array $openids user_list{{'openid:xxxxxx'},{},{}}
  2337. * @return array user_info_list{subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}{}{}...
  2338. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  2339. */
  2340. public function getUsersInfo($openids)
  2341. {
  2342. if (!$this->access_token && !$this->checkAuth()) return false;
  2343. $result = $this->http_post(self::API_URL_PREFIX . self::USERS_INFO_URL . 'access_token=' . $this->access_token, json_encode($openids));
  2344. if ($result) {
  2345. $json = json_decode($result, true);
  2346. if (isset($json['errcode'])) {
  2347. $this->errCode = $json['errcode'];
  2348. $this->errMsg = $json['errmsg'];
  2349. return false;
  2350. }
  2351. return $json;
  2352. }
  2353. return false;
  2354. }
  2355. /**
  2356. * 设置用户备注名
  2357. * @param string $openid
  2358. * @param string $remark 备注名
  2359. * @return boolean|array
  2360. */
  2361. public function updateUserRemark($openid, $remark)
  2362. {
  2363. if (!$this->access_token && !$this->checkAuth()) return false;
  2364. $data = [
  2365. 'openid' => $openid,
  2366. 'remark' => $remark,
  2367. ];
  2368. $result = $this->http_post(self::API_URL_PREFIX . self::USER_UPDATEREMARK_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2369. if ($result) {
  2370. $json = json_decode($result, true);
  2371. if (!$json || !empty($json['errcode'])) {
  2372. $this->errCode = $json['errcode'];
  2373. $this->errMsg = $json['errmsg'];
  2374. return false;
  2375. }
  2376. return $json;
  2377. }
  2378. return false;
  2379. }
  2380. /**
  2381. * 获取用户分组列表
  2382. * @return boolean|array
  2383. */
  2384. public function getGroup()
  2385. {
  2386. if (!$this->access_token && !$this->checkAuth()) return false;
  2387. $result = $this->http_get(self::API_URL_PREFIX . self::GROUP_GET_URL . 'access_token=' . $this->access_token);
  2388. if ($result) {
  2389. $json = json_decode($result, true);
  2390. if (isset($json['errcode'])) {
  2391. $this->errCode = $json['errcode'];
  2392. $this->errMsg = $json['errmsg'];
  2393. return false;
  2394. }
  2395. return $json;
  2396. }
  2397. return false;
  2398. }
  2399. /**
  2400. * 获取用户所在分组
  2401. * @param string $openid
  2402. * @return boolean|int 成功则返回用户分组id
  2403. */
  2404. public function getUserGroup($openid)
  2405. {
  2406. if (!$this->access_token && !$this->checkAuth()) return false;
  2407. $data = [
  2408. 'openid' => $openid,
  2409. ];
  2410. $result = $this->http_post(self::API_URL_PREFIX . self::USER_GROUP_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2411. if ($result) {
  2412. $json = json_decode($result, true);
  2413. if (!$json || !empty($json['errcode'])) {
  2414. $this->errCode = $json['errcode'];
  2415. $this->errMsg = $json['errmsg'];
  2416. return false;
  2417. } else
  2418. if (isset($json['groupid'])) return $json['groupid'];
  2419. }
  2420. return false;
  2421. }
  2422. /**
  2423. * 新增自定分组
  2424. * @param string $name 分组名称
  2425. * @return boolean|array
  2426. */
  2427. public function createGroup($name)
  2428. {
  2429. if (!$this->access_token && !$this->checkAuth()) return false;
  2430. $data = [
  2431. 'group' => ['name' => $name],
  2432. ];
  2433. $result = $this->http_post(self::API_URL_PREFIX . self::GROUP_CREATE_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2434. if ($result) {
  2435. $json = json_decode($result, true);
  2436. if (!$json || !empty($json['errcode'])) {
  2437. $this->errCode = $json['errcode'];
  2438. $this->errMsg = $json['errmsg'];
  2439. return false;
  2440. }
  2441. return $json;
  2442. }
  2443. return false;
  2444. }
  2445. /**
  2446. * 更改分组名称
  2447. * @param int $groupid 分组id
  2448. * @param string $name 分组名称
  2449. * @return boolean|array
  2450. */
  2451. public function updateGroup($groupid, $name)
  2452. {
  2453. if (!$this->access_token && !$this->checkAuth()) return false;
  2454. $data = [
  2455. 'group' => ['id' => $groupid, 'name' => $name],
  2456. ];
  2457. $result = $this->http_post(self::API_URL_PREFIX . self::GROUP_UPDATE_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2458. if ($result) {
  2459. $json = json_decode($result, true);
  2460. if (!$json || !empty($json['errcode'])) {
  2461. $this->errCode = $json['errcode'];
  2462. $this->errMsg = $json['errmsg'];
  2463. return false;
  2464. }
  2465. return $json;
  2466. }
  2467. return false;
  2468. }
  2469. /**
  2470. * 移动用户分组
  2471. * @param int $groupid 分组id
  2472. * @param string $openid 用户openid
  2473. * @return boolean|array
  2474. */
  2475. public function updateGroupMembers($groupid, $openid)
  2476. {
  2477. if (!$this->access_token && !$this->checkAuth()) return false;
  2478. $data = [
  2479. 'openid' => $openid,
  2480. 'to_groupid' => $groupid,
  2481. ];
  2482. $result = $this->http_post(self::API_URL_PREFIX . self::GROUP_MEMBER_UPDATE_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2483. if ($result) {
  2484. $json = json_decode($result, true);
  2485. if (!$json || !empty($json['errcode'])) {
  2486. $this->errCode = $json['errcode'];
  2487. $this->errMsg = $json['errmsg'];
  2488. return false;
  2489. }
  2490. return $json;
  2491. }
  2492. return false;
  2493. }
  2494. /**
  2495. * 批量移动用户分组
  2496. * @param int $groupid 分组id
  2497. * @param string $openid_list 用户openid数组,一次不能超过50个
  2498. * @return boolean|array
  2499. */
  2500. public function batchUpdateGroupMembers($groupid, $openid_list)
  2501. {
  2502. if (!$this->access_token && !$this->checkAuth()) return false;
  2503. $data = [
  2504. 'openid_list' => $openid_list,
  2505. 'to_groupid' => $groupid,
  2506. ];
  2507. $result = $this->http_post(self::API_URL_PREFIX . self::GROUP_MEMBER_BATCHUPDATE_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2508. if ($result) {
  2509. $json = json_decode($result, true);
  2510. if (!$json || !empty($json['errcode'])) {
  2511. $this->errCode = $json['errcode'];
  2512. $this->errMsg = $json['errmsg'];
  2513. return false;
  2514. }
  2515. return $json;
  2516. }
  2517. return false;
  2518. }
  2519. /**
  2520. * 发送客服消息
  2521. * @param array $data 消息结构{"touser":"OPENID","msgtype":"news","news":{...}}
  2522. * @return boolean|array
  2523. */
  2524. public function sendCustomMessage($data)
  2525. {
  2526. if (!$this->access_token && !$this->checkAuth()) return false;
  2527. //$result = $this->http_post(self::API_URL_PREFIX . self::CUSTOM_SEND_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2528. return Queue::instance()->send(Queue::KEY_WX_CUSTOMER_MESSAGE, [
  2529. 'url' => self::API_URL_PREFIX . self::CUSTOM_SEND_URL . 'access_token=' . $this->access_token,
  2530. 'message' => $data,
  2531. ]);
  2532. }
  2533. /**
  2534. * 直接发送
  2535. * @param $data
  2536. * @return bool|mixed
  2537. */
  2538. public function sendCustomMessageDirect($data)
  2539. {
  2540. if (!$this->access_token && !$this->checkAuth()) return false;
  2541. $result = $this->http_post(self::API_URL_PREFIX . self::CUSTOM_SEND_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2542. if ($result) {
  2543. $json = json_decode($result, true);
  2544. if (!$json || !empty($json['errcode'])) {
  2545. $this->errCode = $json['errcode'];
  2546. $this->errMsg = $json['errmsg'];
  2547. return false;
  2548. }
  2549. return $json;
  2550. }
  2551. return false;
  2552. }
  2553. /**
  2554. * oauth 授权跳转接口
  2555. * @param string $callback 回调URI
  2556. * @return string
  2557. */
  2558. public function getOauthRedirect($callback, $state = '', $scope = 'snsapi_userinfo')
  2559. {
  2560. return self::OAUTH_PREFIX . self::OAUTH_AUTHORIZE_URL . 'appid=' . $this->appid . '&redirect_uri=' . urlencode($callback) . '&response_type=code&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
  2561. }
  2562. /**
  2563. * 通过code获取Access Token
  2564. * @return array {access_token,expires_in,refresh_token,openid,scope}
  2565. */
  2566. public function getOauthAccessToken()
  2567. {
  2568. $code = isset($_GET['code']) ? $_GET['code'] : '';
  2569. if (!$code) return false;
  2570. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::OAUTH_TOKEN_URL . 'appid=' . $this->appid . '&secret=' . $this->appsecret . '&code=' . $code . '&grant_type=authorization_code');
  2571. if ($result) {
  2572. $json = json_decode($result, true);
  2573. if (!$json || !empty($json['errcode'])) {
  2574. $this->errCode = $json['errcode'];
  2575. $this->errMsg = $json['errmsg'];
  2576. return false;
  2577. }
  2578. $this->user_token = $json['access_token'];
  2579. return $json;
  2580. }
  2581. return false;
  2582. }
  2583. /**
  2584. * 刷新access token并续期
  2585. * @param string $refresh_token
  2586. * @return boolean|mixed
  2587. */
  2588. public function getOauthRefreshToken($refresh_token)
  2589. {
  2590. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::OAUTH_REFRESH_URL . 'appid=' . $this->appid . '&grant_type=refresh_token&refresh_token=' . $refresh_token);
  2591. if ($result) {
  2592. $json = json_decode($result, true);
  2593. if (!$json || !empty($json['errcode'])) {
  2594. $this->errCode = $json['errcode'];
  2595. $this->errMsg = $json['errmsg'];
  2596. return false;
  2597. }
  2598. $this->user_token = $json['access_token'];
  2599. return $json;
  2600. }
  2601. return false;
  2602. }
  2603. /**
  2604. * 获取授权后的用户资料
  2605. * @param string $access_token
  2606. * @param string $openid
  2607. * @return array {openid,nickname,sex,province,city,country,headimgurl,privilege,[unionid]}
  2608. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  2609. */
  2610. public function getOauthUserinfo($access_token, $openid)
  2611. {
  2612. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::OAUTH_USERINFO_URL . 'access_token=' . $access_token . '&openid=' . $openid);
  2613. if ($result) {
  2614. $json = json_decode($result, true);
  2615. if (!$json || !empty($json['errcode'])) {
  2616. $this->errCode = $json['errcode'];
  2617. $this->errMsg = $json['errmsg'];
  2618. return false;
  2619. }
  2620. return $json;
  2621. }
  2622. return false;
  2623. }
  2624. /**
  2625. * 检验授权凭证是否有效
  2626. * @param string $access_token
  2627. * @param string $openid
  2628. * @return boolean 是否有效
  2629. */
  2630. public function getOauthAuth($access_token, $openid)
  2631. {
  2632. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::OAUTH_AUTH_URL . 'access_token=' . $access_token . '&openid=' . $openid);
  2633. if ($result) {
  2634. $json = json_decode($result, true);
  2635. if (!$json || !empty($json['errcode'])) {
  2636. $this->errCode = $json['errcode'];
  2637. $this->errMsg = $json['errmsg'];
  2638. return false;
  2639. } else
  2640. if ($json['errcode'] == 0) return true;
  2641. }
  2642. return false;
  2643. }
  2644. /**
  2645. * 模板消息 设置所属行业
  2646. * @param int $id1 公众号模板消息所属行业编号,参看官方开发文档 行业代码
  2647. * @param int $id2 同$id1。但如果只有一个行业,此参数可省略
  2648. * @return boolean|array
  2649. */
  2650. public function setTMIndustry($id1, $id2 = '')
  2651. {
  2652. if ($id1) $data['industry_id1'] = $id1;
  2653. if ($id2) $data['industry_id2'] = $id2;
  2654. if (!$this->access_token && !$this->checkAuth()) return false;
  2655. $result = $this->http_post(self::API_URL_PREFIX . self::TEMPLATE_SET_INDUSTRY_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2656. if ($result) {
  2657. $json = json_decode($result, true);
  2658. if (!$json || !empty($json['errcode'])) {
  2659. $this->errCode = $json['errcode'];
  2660. $this->errMsg = $json['errmsg'];
  2661. return false;
  2662. }
  2663. return $json;
  2664. }
  2665. return false;
  2666. }
  2667. /**
  2668. * 模板消息 添加消息模板
  2669. * 成功返回消息模板的调用id
  2670. * @param string $tpl_id 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
  2671. * @return boolean|string
  2672. */
  2673. public function addTemplateMessage($tpl_id)
  2674. {
  2675. $data = ['template_id_short' => $tpl_id];
  2676. if (!$this->access_token && !$this->checkAuth()) return false;
  2677. $result = $this->http_post(self::API_URL_PREFIX . self::TEMPLATE_ADD_TPL_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2678. if ($result) {
  2679. $json = json_decode($result, true);
  2680. if (!$json || !empty($json['errcode'])) {
  2681. $this->errCode = $json['errcode'];
  2682. $this->errMsg = $json['errmsg'];
  2683. return false;
  2684. }
  2685. return $json['template_id'];
  2686. }
  2687. return false;
  2688. }
  2689. /**
  2690. * 发送模板消息
  2691. * @param array $data 消息结构
  2692. * {
  2693. * "touser":"OPENID",
  2694. * "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
  2695. * "url":"http://weixin.qq.com/download",
  2696. * "topcolor":"#FF0000",
  2697. * "data":{
  2698. * "参数名1": {
  2699. * "value":"参数",
  2700. * "color":"#173177" //参数颜色
  2701. * },
  2702. * "Date":{
  2703. * "value":"06月07日 19时24分",
  2704. * "color":"#173177"
  2705. * },
  2706. * "CardNumber":{
  2707. * "value":"0426",
  2708. * "color":"#173177"
  2709. * },
  2710. * "Type":{
  2711. * "value":"消费",
  2712. * "color":"#173177"
  2713. * }
  2714. * }
  2715. * }
  2716. * @return boolean|array
  2717. */
  2718. public function sendTemplateMessage($data)
  2719. {
  2720. if (!$this->access_token && !$this->checkAuth()) return false;
  2721. //$result = $this->http_post(self::API_URL_PREFIX . self::CUSTOM_SEND_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2722. return Queue::instance()->send(Queue::KEY_WX_CUSTOMER_MESSAGE, [
  2723. 'url' => self::API_URL_PREFIX . self::TEMPLATE_SEND_URL . 'access_token=' . $this->access_token,
  2724. 'message' => $data,
  2725. ]);
  2726. }
  2727. /**
  2728. * 发送模板消息
  2729. * @param $data
  2730. */
  2731. public function sendTemplateMessageDirect($data) {
  2732. if (!$this->access_token && !$this->checkAuth()) return false;
  2733. $result = $this->http_post(self::API_URL_PREFIX . self::TEMPLATE_SEND_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  2734. if ($result) {
  2735. $json = json_decode($result, true);
  2736. if (!$json || !empty($json['errcode'])) {
  2737. $this->errCode = $json['errcode'];
  2738. $this->errMsg = $json['errmsg'];
  2739. return false;
  2740. }
  2741. return $json;
  2742. }
  2743. return false;
  2744. }
  2745. /**
  2746. * 获取多客服会话记录
  2747. * @param array $data 数据结构{"starttime":123456789,"endtime":987654321,"openid":"OPENID","pagesize":10,"pageindex":1,}
  2748. * @return boolean|array
  2749. */
  2750. public function getCustomServiceMessage($data)
  2751. {
  2752. if (!$this->access_token && !$this->checkAuth()) return false;
  2753. $result = $this->http_post(self::API_URL_PREFIX . self::CUSTOM_SERVICE_GET_RECORD . 'access_token=' . $this->access_token, self::json_encode($data));
  2754. if ($result) {
  2755. $json = json_decode($result, true);
  2756. if (!$json || !empty($json['errcode'])) {
  2757. $this->errCode = $json['errcode'];
  2758. $this->errMsg = $json['errmsg'];
  2759. return false;
  2760. }
  2761. return $json;
  2762. }
  2763. return false;
  2764. }
  2765. /**
  2766. * 转发多客服消息
  2767. * Example: $obj->transfer_customer_service($customer_account)->reply();
  2768. * @param string $customer_account 转发到指定客服帐号:test1@test
  2769. */
  2770. public function transfer_customer_service($customer_account = '')
  2771. {
  2772. $msg = [
  2773. 'ToUserName' => $this->getRevFrom(),
  2774. 'FromUserName' => $this->getRevTo(),
  2775. 'CreateTime' => time(),
  2776. 'MsgType' => 'transfer_customer_service',
  2777. ];
  2778. if ($customer_account) {
  2779. $msg['TransInfo'] = ['KfAccount' => $customer_account];
  2780. }
  2781. $this->Message($msg);
  2782. return $this;
  2783. }
  2784. /**
  2785. * 获取多客服客服基本信息
  2786. *
  2787. * @return boolean|array
  2788. */
  2789. public function getCustomServiceKFlist()
  2790. {
  2791. if (!$this->access_token && !$this->checkAuth()) return false;
  2792. $result = $this->http_get(self::API_URL_PREFIX . self::CUSTOM_SERVICE_GET_KFLIST . 'access_token=' . $this->access_token);
  2793. if ($result) {
  2794. $json = json_decode($result, true);
  2795. if (!$json || !empty($json['errcode'])) {
  2796. $this->errCode = $json['errcode'];
  2797. $this->errMsg = $json['errmsg'];
  2798. return false;
  2799. }
  2800. return $json;
  2801. }
  2802. return false;
  2803. }
  2804. /**
  2805. * 获取多客服在线客服接待信息
  2806. *
  2807. * @return boolean|array {
  2808. * "kf_online_list": [
  2809. * {
  2810. * "kf_account": "test1@test", //客服账号@微信别名
  2811. * "status": 1, //客服在线状态 1:pc在线,2:手机在线,若pc和手机同时在线则为 1+2=3
  2812. * "kf_id": "1001", //客服工号
  2813. * "auto_accept": 0, //客服设置的最大自动接入数
  2814. * "accepted_case": 1 //客服当前正在接待的会话数
  2815. * }
  2816. * ]
  2817. * }
  2818. */
  2819. public function getCustomServiceOnlineKFlist()
  2820. {
  2821. if (!$this->access_token && !$this->checkAuth()) return false;
  2822. $result = $this->http_get(self::API_URL_PREFIX . self::CUSTOM_SERVICE_GET_ONLINEKFLIST . 'access_token=' . $this->access_token);
  2823. if ($result) {
  2824. $json = json_decode($result, true);
  2825. if (!$json || !empty($json['errcode'])) {
  2826. $this->errCode = $json['errcode'];
  2827. $this->errMsg = $json['errmsg'];
  2828. return false;
  2829. }
  2830. return $json;
  2831. }
  2832. return false;
  2833. }
  2834. /**
  2835. * 创建指定多客服会话
  2836. * @tutorial 当用户已被其他客服接待或指定客服不在线则会失败
  2837. * @param string $openid //用户openid
  2838. * @param string $kf_account //客服账号
  2839. * @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
  2840. * @return boolean | array //成功返回json数组
  2841. * {
  2842. * "errcode": 0,
  2843. * "errmsg": "ok",
  2844. * }
  2845. */
  2846. public function createKFSession($openid, $kf_account, $text = '')
  2847. {
  2848. $data = [
  2849. "openid" => $openid,
  2850. "kf_account" => $kf_account,
  2851. ];
  2852. if ($text) $data["text"] = $text;
  2853. if (!$this->access_token && !$this->checkAuth()) return false;
  2854. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
  2855. if ($result) {
  2856. $json = json_decode($result, true);
  2857. if (!$json || !empty($json['errcode'])) {
  2858. $this->errCode = $json['errcode'];
  2859. $this->errMsg = $json['errmsg'];
  2860. return false;
  2861. }
  2862. return $json;
  2863. }
  2864. return false;
  2865. }
  2866. /**
  2867. * 关闭指定多客服会话
  2868. * @tutorial 当用户被其他客服接待时则会失败
  2869. * @param string $openid //用户openid
  2870. * @param string $kf_account //客服账号
  2871. * @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
  2872. * @return boolean | array //成功返回json数组
  2873. * {
  2874. * "errcode": 0,
  2875. * "errmsg": "ok",
  2876. * }
  2877. */
  2878. public function closeKFSession($openid, $kf_account, $text = '')
  2879. {
  2880. $data = [
  2881. "openid" => $openid,
  2882. "kf_account" => $kf_account,
  2883. ];
  2884. if ($text) $data["text"] = $text;
  2885. if (!$this->access_token && !$this->checkAuth()) return false;
  2886. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_CLOSE . 'access_token=' . $this->access_token, self::json_encode($data));
  2887. if ($result) {
  2888. $json = json_decode($result, true);
  2889. if (!$json || !empty($json['errcode'])) {
  2890. $this->errCode = $json['errcode'];
  2891. $this->errMsg = $json['errmsg'];
  2892. return false;
  2893. }
  2894. return $json;
  2895. }
  2896. return false;
  2897. }
  2898. /**
  2899. * 获取用户会话状态
  2900. * @param string $openid //用户openid
  2901. * @return boolean | array //成功返回json数组
  2902. * {
  2903. * "errcode" : 0,
  2904. * "errmsg" : "ok",
  2905. * "kf_account" : "test1@test", //正在接待的客服
  2906. * "createtime": 123456789, //会话接入时间
  2907. * }
  2908. */
  2909. public function getKFSession($openid)
  2910. {
  2911. if (!$this->access_token && !$this->checkAuth()) return false;
  2912. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_GET . 'access_token=' . $this->access_token . '&openid=' . $openid);
  2913. if ($result) {
  2914. $json = json_decode($result, true);
  2915. if (!$json || !empty($json['errcode'])) {
  2916. $this->errCode = $json['errcode'];
  2917. $this->errMsg = $json['errmsg'];
  2918. return false;
  2919. }
  2920. return $json;
  2921. }
  2922. return false;
  2923. }
  2924. /**
  2925. * 获取指定客服的会话列表
  2926. * @param string $openid //用户openid
  2927. * @return boolean | array //成功返回json数组
  2928. * array(
  2929. * 'sessionlist' => array (
  2930. * array (
  2931. * 'openid'=>'OPENID', //客户 openid
  2932. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2933. * ),
  2934. * array (
  2935. * 'openid'=>'OPENID', //客户 openid
  2936. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2937. * ),
  2938. * )
  2939. * )
  2940. */
  2941. public function getKFSessionlist($kf_account)
  2942. {
  2943. if (!$this->access_token && !$this->checkAuth()) return false;
  2944. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_GET_LIST . 'access_token=' . $this->access_token . '&kf_account=' . $kf_account);
  2945. if ($result) {
  2946. $json = json_decode($result, true);
  2947. if (!$json || !empty($json['errcode'])) {
  2948. $this->errCode = $json['errcode'];
  2949. $this->errMsg = $json['errmsg'];
  2950. return false;
  2951. }
  2952. return $json;
  2953. }
  2954. return false;
  2955. }
  2956. /**
  2957. * 获取未接入会话列表
  2958. * @param string $openid //用户openid
  2959. * @return boolean | array //成功返回json数组
  2960. * array (
  2961. * 'count' => 150 , //未接入会话数量
  2962. * 'waitcaselist' => array (
  2963. * array (
  2964. * 'openid'=>'OPENID', //客户 openid
  2965. * 'kf_account ' =>'', //指定接待的客服,为空则未指定
  2966. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2967. * ),
  2968. * array (
  2969. * 'openid'=>'OPENID', //客户 openid
  2970. * 'kf_account ' =>'', //指定接待的客服,为空则未指定
  2971. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2972. * )
  2973. * )
  2974. * )
  2975. */
  2976. public function getKFSessionWait()
  2977. {
  2978. if (!$this->access_token && !$this->checkAuth()) return false;
  2979. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_GET_WAIT . 'access_token=' . $this->access_token);
  2980. if ($result) {
  2981. $json = json_decode($result, true);
  2982. if (!$json || !empty($json['errcode'])) {
  2983. $this->errCode = $json['errcode'];
  2984. $this->errMsg = $json['errmsg'];
  2985. return false;
  2986. }
  2987. return $json;
  2988. }
  2989. return false;
  2990. }
  2991. /**
  2992. * 添加客服账号
  2993. *
  2994. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2995. * @param string $nickname //客服昵称,最长6个汉字或12个英文字符
  2996. * @param string $password //客服账号明文登录密码,会自动加密
  2997. * @return boolean|array
  2998. * 成功返回结果
  2999. * {
  3000. * "errcode": 0,
  3001. * "errmsg": "ok",
  3002. * }
  3003. */
  3004. public function addKFAccount($account, $nickname, $password)
  3005. {
  3006. $data = [
  3007. "kf_account" => $account,
  3008. "nickname" => $nickname,
  3009. "password" => md5($password),
  3010. ];
  3011. if (!$this->access_token && !$this->checkAuth()) return false;
  3012. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_ADD_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  3013. if ($result) {
  3014. $json = json_decode($result, true);
  3015. if (!$json || !empty($json['errcode'])) {
  3016. $this->errCode = $json['errcode'];
  3017. $this->errMsg = $json['errmsg'];
  3018. return false;
  3019. }
  3020. return $json;
  3021. }
  3022. return false;
  3023. }
  3024. /**
  3025. * 修改客服账号信息
  3026. *
  3027. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  3028. * @param string $nickname //客服昵称,最长6个汉字或12个英文字符
  3029. * @param string $password //客服账号明文登录密码,会自动加密
  3030. * @return boolean|array
  3031. * 成功返回结果
  3032. * {
  3033. * "errcode": 0,
  3034. * "errmsg": "ok",
  3035. * }
  3036. */
  3037. public function updateKFAccount($account, $nickname, $password)
  3038. {
  3039. $data = [
  3040. "kf_account" => $account,
  3041. "nickname" => $nickname,
  3042. "password" => md5($password),
  3043. ];
  3044. if (!$this->access_token && !$this->checkAuth()) return false;
  3045. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_UPDATE_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  3046. if ($result) {
  3047. $json = json_decode($result, true);
  3048. if (!$json || !empty($json['errcode'])) {
  3049. $this->errCode = $json['errcode'];
  3050. $this->errMsg = $json['errmsg'];
  3051. return false;
  3052. }
  3053. return $json;
  3054. }
  3055. return false;
  3056. }
  3057. /**
  3058. * 删除客服账号
  3059. *
  3060. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  3061. * @return boolean|array
  3062. * 成功返回结果
  3063. * {
  3064. * "errcode": 0,
  3065. * "errmsg": "ok",
  3066. * }
  3067. */
  3068. public function deleteKFAccount($account)
  3069. {
  3070. if (!$this->access_token && !$this->checkAuth()) return false;
  3071. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_DEL_URL . 'access_token=' . $this->access_token . '&kf_account=' . $account);
  3072. if ($result) {
  3073. $json = json_decode($result, true);
  3074. if (!$json || !empty($json['errcode'])) {
  3075. $this->errCode = $json['errcode'];
  3076. $this->errMsg = $json['errmsg'];
  3077. return false;
  3078. }
  3079. return $json;
  3080. }
  3081. return false;
  3082. }
  3083. /**
  3084. * 上传客服头像
  3085. *
  3086. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  3087. * @param string $imgfile //头像文件完整路径,如:'D:\user.jpg'。头像文件必须JPG格式,像素建议640*640
  3088. * @return boolean|array
  3089. * 成功返回结果
  3090. * {
  3091. * "errcode": 0,
  3092. * "errmsg": "ok",
  3093. * }
  3094. */
  3095. public function setKFHeadImg($account, $imgfile)
  3096. {
  3097. if (!$this->access_token && !$this->checkAuth()) return false;
  3098. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL . 'access_token=' . $this->access_token . '&kf_account=' . $account, ['media' => '@' . $imgfile], true);
  3099. if ($result) {
  3100. $json = json_decode($result, true);
  3101. if (!$json || !empty($json['errcode'])) {
  3102. $this->errCode = $json['errcode'];
  3103. $this->errMsg = $json['errmsg'];
  3104. return false;
  3105. }
  3106. return $json;
  3107. }
  3108. return false;
  3109. }
  3110. /**
  3111. * 语义理解接口
  3112. * @param String $uid 用户唯一id(非开发者id),用户区分公众号下的不同用户(建议填入用户openid)
  3113. * @param String $query 输入文本串
  3114. * @param String $category 需要使用的服务类型,多个用“,”隔开,不能为空
  3115. * @param Float $latitude 纬度坐标,与经度同时传入;与城市二选一传入
  3116. * @param Float $longitude 经度坐标,与纬度同时传入;与城市二选一传入
  3117. * @param String $city 城市名称,与经纬度二选一传入
  3118. * @param String $region 区域名称,在城市存在的情况下可省略;与经纬度二选一传入
  3119. * @return boolean|array
  3120. */
  3121. public function querySemantic($uid, $query, $category, $latitude = 0, $longitude = 0, $city = "", $region = "")
  3122. {
  3123. if (!$this->access_token && !$this->checkAuth()) return false;
  3124. $data = [
  3125. 'query' => $query,
  3126. 'category' => $category,
  3127. 'appid' => $this->appid,
  3128. 'uid' => '',
  3129. ];
  3130. //地理坐标或城市名称二选一
  3131. if ($latitude) {
  3132. $data['latitude'] = $latitude;
  3133. $data['longitude'] = $longitude;
  3134. } elseif ($city) {
  3135. $data['city'] = $city;
  3136. } elseif ($region) {
  3137. $data['region'] = $region;
  3138. }
  3139. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SEMANTIC_API_URL . 'access_token=' . $this->access_token, self::json_encode($data));
  3140. if ($result) {
  3141. $json = json_decode($result, true);
  3142. if (!$json || !empty($json['errcode'])) {
  3143. $this->errCode = $json['errcode'];
  3144. $this->errMsg = $json['errmsg'];
  3145. return false;
  3146. }
  3147. return $json;
  3148. }
  3149. return false;
  3150. }
  3151. /**
  3152. * 创建卡券
  3153. * @param Array $data 卡券数据
  3154. * @return array|boolean 返回数组中card_id为卡券ID
  3155. */
  3156. public function createCard($data)
  3157. {
  3158. if (!$this->access_token && !$this->checkAuth()) return false;
  3159. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3160. if ($result) {
  3161. $json = json_decode($result, true);
  3162. if (!$json || !empty($json['errcode'])) {
  3163. $this->errCode = $json['errcode'];
  3164. $this->errMsg = $json['errmsg'];
  3165. return false;
  3166. }
  3167. return $json;
  3168. }
  3169. return false;
  3170. }
  3171. /**
  3172. * 更改卡券信息
  3173. * 调用该接口更新信息后会重新送审,卡券状态变更为待审核。已被用户领取的卡券会实时更新票面信息。
  3174. * @param string $data
  3175. * @return boolean
  3176. */
  3177. public function updateCard($data)
  3178. {
  3179. if (!$this->access_token && !$this->checkAuth()) return false;
  3180. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3181. if ($result) {
  3182. $json = json_decode($result, true);
  3183. if (!$json || !empty($json['errcode'])) {
  3184. $this->errCode = $json['errcode'];
  3185. $this->errMsg = $json['errmsg'];
  3186. return false;
  3187. }
  3188. return true;
  3189. }
  3190. return false;
  3191. }
  3192. /**
  3193. * 删除卡券
  3194. * 允许商户删除任意一类卡券。删除卡券后,该卡券对应已生成的领取用二维码、添加到卡包 JS API 均会失效。
  3195. * 注意:删除卡券不能删除已被用户领取,保存在微信客户端中的卡券,已领取的卡券依旧有效。
  3196. * @param string $card_id 卡券ID
  3197. * @return boolean
  3198. */
  3199. public function delCard($card_id)
  3200. {
  3201. $data = [
  3202. 'card_id' => $card_id,
  3203. ];
  3204. if (!$this->access_token && !$this->checkAuth()) return false;
  3205. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_DELETE . 'access_token=' . $this->access_token, self::json_encode($data));
  3206. if ($result) {
  3207. $json = json_decode($result, true);
  3208. if (!$json || !empty($json['errcode'])) {
  3209. $this->errCode = $json['errcode'];
  3210. $this->errMsg = $json['errmsg'];
  3211. return false;
  3212. }
  3213. return true;
  3214. }
  3215. return false;
  3216. }
  3217. /**
  3218. * 查询卡券详情
  3219. * @param string $card_id
  3220. * @return boolean|array 返回数组信息比较复杂,请参看卡券接口文档
  3221. */
  3222. public function getCardInfo($card_id)
  3223. {
  3224. $data = [
  3225. 'card_id' => $card_id,
  3226. ];
  3227. if (!$this->access_token && !$this->checkAuth()) return false;
  3228. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_GET . 'access_token=' . $this->access_token, self::json_encode($data));
  3229. if ($result) {
  3230. $json = json_decode($result, true);
  3231. if (!$json || !empty($json['errcode'])) {
  3232. $this->errCode = $json['errcode'];
  3233. $this->errMsg = $json['errmsg'];
  3234. return false;
  3235. }
  3236. return $json;
  3237. }
  3238. return false;
  3239. }
  3240. /**
  3241. * 获取用户已领取卡券接口
  3242. * @param string $openid
  3243. * @param string $card_id
  3244. * @return boolean|array 返回数组信息比较复杂,请参看卡券接口文档
  3245. * 成功返回结果
  3246. * {
  3247. * "errcode":0,
  3248. * "errmsg":"ok",
  3249. * "card_list": [
  3250. * {"code": "xxx1434079154", "card_id": "xxxxxxxxxx"},
  3251. * {"code": "xxx1434079155", "card_id": "xxxxxxxxxx"}
  3252. * ]
  3253. * }
  3254. */
  3255. public function getUserCardList($openid, $card_id)
  3256. {
  3257. $data = [
  3258. 'openid' => $openid,
  3259. 'card_id' => $card_id,
  3260. ];
  3261. if (!$this->access_token && !$this->checkAuth()) return false;
  3262. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_USER_GETCARDLIST . 'access_token=' . $this->access_token, self::json_encode($data));
  3263. if ($result) {
  3264. $json = json_decode($result, true);
  3265. if (!$json || !empty($json['errcode'])) {
  3266. $this->errCode = $json['errcode'];
  3267. $this->errMsg = $json['errmsg'];
  3268. return false;
  3269. }
  3270. return $json;
  3271. }
  3272. return false;
  3273. }
  3274. /**
  3275. * 获取颜色列表
  3276. * 获得卡券的最新颜色列表,用于创建卡券
  3277. * @return boolean|array 返回数组请参看 微信卡券接口文档 的json格式
  3278. */
  3279. public function getCardColors()
  3280. {
  3281. if (!$this->access_token && !$this->checkAuth()) return false;
  3282. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::CARD_GETCOLORS . 'access_token=' . $this->access_token);
  3283. if ($result) {
  3284. $json = json_decode($result, true);
  3285. if (!$json || !empty($json['errcode'])) {
  3286. $this->errCode = $json['errcode'];
  3287. $this->errMsg = $json['errmsg'];
  3288. return false;
  3289. }
  3290. return $json;
  3291. }
  3292. return false;
  3293. }
  3294. /**
  3295. * 拉取门店列表
  3296. * 获取在公众平台上申请创建的门店列表
  3297. * @param int $offset 开始拉取的偏移,默认为0从头开始
  3298. * @param int $count 拉取的数量,默认为0拉取全部
  3299. * @return boolean|array 返回数组请参看 微信卡券接口文档 的json格式
  3300. */
  3301. public function getCardLocations($offset = 0, $count = 0)
  3302. {
  3303. $data = [
  3304. 'offset' => $offset,
  3305. 'count' => $count,
  3306. ];
  3307. if (!$this->access_token && !$this->checkAuth()) return false;
  3308. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
  3309. if ($result) {
  3310. $json = json_decode($result, true);
  3311. if (!$json || !empty($json['errcode'])) {
  3312. $this->errCode = $json['errcode'];
  3313. $this->errMsg = $json['errmsg'];
  3314. return false;
  3315. }
  3316. return $json;
  3317. }
  3318. return false;
  3319. }
  3320. /**
  3321. * 批量导入门店信息
  3322. * @tutorial 返回插入的门店id列表,以逗号分隔。如果有插入失败的,则为-1,请自行核查是哪个插入失败
  3323. * @param array $data 数组形式的json数据,由于内容较多,具体内容格式请查看 微信卡券接口文档
  3324. * @return boolean|string 成功返回插入的门店id列表
  3325. */
  3326. public function addCardLocations($data)
  3327. {
  3328. if (!$this->access_token && !$this->checkAuth()) return false;
  3329. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHADD . 'access_token=' . $this->access_token, self::json_encode($data));
  3330. if ($result) {
  3331. $json = json_decode($result, true);
  3332. if (!$json || !empty($json['errcode'])) {
  3333. $this->errCode = $json['errcode'];
  3334. $this->errMsg = $json['errmsg'];
  3335. return false;
  3336. }
  3337. return $json;
  3338. }
  3339. return false;
  3340. }
  3341. /**
  3342. * 生成卡券二维码
  3343. * 成功则直接返回ticket值,可以用 getQRUrl($ticket) 换取二维码url
  3344. *
  3345. * @param string $cardid 卡券ID 必须
  3346. * @param string $code 指定卡券 code 码,只能被领一次。use_custom_code 字段为 true 的卡券必须填写,非自定义 code 不必填写。
  3347. * @param string $openid 指定领取者的 openid,只有该用户能领取。bind_openid 字段为 true 的卡券必须填写,非自定义 openid 不必填写。
  3348. * @param int $expire_seconds 指定二维码的有效时间,范围是 60 ~ 1800 秒。不填默认为永久有效。
  3349. * @param boolean $is_unique_code 指定下发二维码,生成的二维码随机分配一个 code,领取后不可再次扫描。填写 true 或 false。默认 false。
  3350. * @param string $balance 红包余额,以分为单位。红包类型必填(LUCKY_MONEY),其他卡券类型不填。
  3351. * @return boolean|string
  3352. */
  3353. public function createCardQrcode($card_id, $code = '', $openid = '', $expire_seconds = 0, $is_unique_code = false, $balance = '')
  3354. {
  3355. $card = [
  3356. 'card_id' => $card_id,
  3357. ];
  3358. $data = [
  3359. 'action_name' => "QR_CARD",
  3360. ];
  3361. if ($code)
  3362. $card['code'] = $code;
  3363. if ($openid)
  3364. $card['openid'] = $openid;
  3365. if ($is_unique_code)
  3366. $card['is_unique_code'] = $is_unique_code;
  3367. if ($balance)
  3368. $card['balance'] = $balance;
  3369. if ($expire_seconds)
  3370. $data['expire_seconds'] = $expire_seconds;
  3371. $data['action_info'] = ['card' => $card];
  3372. if (!$this->access_token && !$this->checkAuth()) return false;
  3373. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_QRCODE_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3374. if ($result) {
  3375. $json = json_decode($result, true);
  3376. if (!$json || !empty($json['errcode'])) {
  3377. $this->errCode = $json['errcode'];
  3378. $this->errMsg = $json['errmsg'];
  3379. return false;
  3380. }
  3381. return $json;
  3382. }
  3383. return false;
  3384. }
  3385. /**
  3386. * 消耗 code
  3387. * 自定义 code(use_custom_code 为 true)的优惠券,在 code 被核销时,必须调用此接口。
  3388. *
  3389. * @param string $code 要消耗的序列号
  3390. * @param string $card_id 要消耗序列号所述的 card_id,创建卡券时use_custom_code 填写 true 时必填。
  3391. * @return boolean|array
  3392. * {
  3393. * "errcode":0,
  3394. * "errmsg":"ok",
  3395. * "card":{"card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc"},
  3396. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA"
  3397. * }
  3398. */
  3399. public function consumeCardCode($code, $card_id = '')
  3400. {
  3401. $data = ['code' => $code];
  3402. if ($card_id)
  3403. $data['card_id'] = $card_id;
  3404. if (!$this->access_token && !$this->checkAuth()) return false;
  3405. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_CONSUME . 'access_token=' . $this->access_token, self::json_encode($data));
  3406. if ($result) {
  3407. $json = json_decode($result, true);
  3408. if (!$json || !empty($json['errcode'])) {
  3409. $this->errCode = $json['errcode'];
  3410. $this->errMsg = $json['errmsg'];
  3411. return false;
  3412. }
  3413. return $json;
  3414. }
  3415. return false;
  3416. }
  3417. /**
  3418. * code 解码
  3419. * @param string $encrypt_code 通过 choose_card_info 获取的加密字符串
  3420. * @return boolean|array
  3421. * {
  3422. * "errcode":0,
  3423. * "errmsg":"ok",
  3424. * "code":"751234212312"
  3425. * }
  3426. */
  3427. public function decryptCardCode($encrypt_code)
  3428. {
  3429. $data = [
  3430. 'encrypt_code' => $encrypt_code,
  3431. ];
  3432. if (!$this->access_token && !$this->checkAuth()) return false;
  3433. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_DECRYPT . 'access_token=' . $this->access_token, self::json_encode($data));
  3434. if ($result) {
  3435. $json = json_decode($result, true);
  3436. if (!$json || !empty($json['errcode'])) {
  3437. $this->errCode = $json['errcode'];
  3438. $this->errMsg = $json['errmsg'];
  3439. return false;
  3440. }
  3441. return $json;
  3442. }
  3443. return false;
  3444. }
  3445. /**
  3446. * 查询 code 的有效性(非自定义 code)
  3447. * @param string $code
  3448. * @return boolean|array
  3449. * {
  3450. * "errcode":0,
  3451. * "errmsg":"ok",
  3452. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA", //用户 openid
  3453. * "card":{
  3454. * "card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc",
  3455. * "begin_time": 1404205036, //起始使用时间
  3456. * "end_time": 1404205036, //结束时间
  3457. * }
  3458. * }
  3459. */
  3460. public function checkCardCode($code)
  3461. {
  3462. $data = [
  3463. 'code' => $code,
  3464. ];
  3465. if (!$this->access_token && !$this->checkAuth()) return false;
  3466. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_GET . 'access_token=' . $this->access_token, self::json_encode($data));
  3467. if ($result) {
  3468. $json = json_decode($result, true);
  3469. if (!$json || !empty($json['errcode'])) {
  3470. $this->errCode = $json['errcode'];
  3471. $this->errMsg = $json['errmsg'];
  3472. return false;
  3473. }
  3474. return $json;
  3475. }
  3476. return false;
  3477. }
  3478. /**
  3479. * 批量查询卡列表
  3480. * @param $offset 开始拉取的偏移,默认为0从头开始
  3481. * @param $count 需要查询的卡片的数量(数量最大50,默认50)
  3482. * @return boolean|array
  3483. * {
  3484. * "errcode":0,
  3485. * "errmsg":"ok",
  3486. * "card_id_list":["ph_gmt7cUVrlRk8swPwx7aDyF-pg"], //卡 id 列表
  3487. * "total_num":1 //该商户名下 card_id 总数
  3488. * }
  3489. */
  3490. public function getCardIdList($offset = 0, $count = 50)
  3491. {
  3492. if ($count > 50)
  3493. $count = 50;
  3494. $data = [
  3495. 'offset' => $offset,
  3496. 'count' => $count,
  3497. ];
  3498. if (!$this->access_token && !$this->checkAuth()) return false;
  3499. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
  3500. if ($result) {
  3501. $json = json_decode($result, true);
  3502. if (!$json || !empty($json['errcode'])) {
  3503. $this->errCode = $json['errcode'];
  3504. $this->errMsg = $json['errmsg'];
  3505. return false;
  3506. }
  3507. return $json;
  3508. }
  3509. return false;
  3510. }
  3511. /**
  3512. * 更改 code
  3513. * 为确保转赠后的安全性,微信允许自定义code的商户对已下发的code进行更改。
  3514. * 注:为避免用户疑惑,建议仅在发生转赠行为后(发生转赠后,微信会通过事件推送的方式告知商户被转赠的卡券code)对用户的code进行更改。
  3515. * @param string $code 卡券的 code 编码
  3516. * @param string $card_id 卡券 ID
  3517. * @param string $new_code 新的卡券 code 编码
  3518. * @return boolean
  3519. */
  3520. public function updateCardCode($code, $card_id, $new_code)
  3521. {
  3522. $data = [
  3523. 'code' => $code,
  3524. 'card_id' => $card_id,
  3525. 'new_code' => $new_code,
  3526. ];
  3527. if (!$this->access_token && !$this->checkAuth()) return false;
  3528. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3529. if ($result) {
  3530. $json = json_decode($result, true);
  3531. if (!$json || !empty($json['errcode'])) {
  3532. $this->errCode = $json['errcode'];
  3533. $this->errMsg = $json['errmsg'];
  3534. return false;
  3535. }
  3536. return true;
  3537. }
  3538. return false;
  3539. }
  3540. /**
  3541. * 设置卡券失效
  3542. * 设置卡券失效的操作不可逆
  3543. * @param string $code 需要设置为失效的 code
  3544. * @param string $card_id 自定义 code 的卡券必填。非自定义 code 的卡券不填。
  3545. * @return boolean
  3546. */
  3547. public function unavailableCardCode($code, $card_id = '')
  3548. {
  3549. $data = [
  3550. 'code' => $code,
  3551. ];
  3552. if ($card_id)
  3553. $data['card_id'] = $card_id;
  3554. if (!$this->access_token && !$this->checkAuth()) return false;
  3555. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UNAVAILABLE . 'access_token=' . $this->access_token, self::json_encode($data));
  3556. if ($result) {
  3557. $json = json_decode($result, true);
  3558. if (!$json || !empty($json['errcode'])) {
  3559. $this->errCode = $json['errcode'];
  3560. $this->errMsg = $json['errmsg'];
  3561. return false;
  3562. }
  3563. return true;
  3564. }
  3565. return false;
  3566. }
  3567. /**
  3568. * 库存修改
  3569. * @param string $data
  3570. * @return boolean
  3571. */
  3572. public function modifyCardStock($data)
  3573. {
  3574. if (!$this->access_token && !$this->checkAuth()) return false;
  3575. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MODIFY_STOCK . 'access_token=' . $this->access_token, self::json_encode($data));
  3576. if ($result) {
  3577. $json = json_decode($result, true);
  3578. if (!$json || !empty($json['errcode'])) {
  3579. $this->errCode = $json['errcode'];
  3580. $this->errMsg = $json['errmsg'];
  3581. return false;
  3582. }
  3583. return true;
  3584. }
  3585. return false;
  3586. }
  3587. /**
  3588. * 更新门票
  3589. * @param string $data
  3590. * @return boolean
  3591. */
  3592. public function updateMeetingCard($data)
  3593. {
  3594. if (!$this->access_token && !$this->checkAuth()) return false;
  3595. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEETINGCARD_UPDATEUSER . 'access_token=' . $this->access_token, self::json_encode($data));
  3596. if ($result) {
  3597. $json = json_decode($result, true);
  3598. if (!$json || !empty($json['errcode'])) {
  3599. $this->errCode = $json['errcode'];
  3600. $this->errMsg = $json['errmsg'];
  3601. return false;
  3602. }
  3603. return true;
  3604. }
  3605. return false;
  3606. }
  3607. /**
  3608. * 激活/绑定会员卡
  3609. * @param string $data 具体结构请参看卡券开发文档(6.1.1 激活/绑定会员卡)章节
  3610. * @return boolean
  3611. */
  3612. public function activateMemberCard($data)
  3613. {
  3614. if (!$this->access_token && !$this->checkAuth()) return false;
  3615. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_ACTIVATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3616. if ($result) {
  3617. $json = json_decode($result, true);
  3618. if (!$json || !empty($json['errcode'])) {
  3619. $this->errCode = $json['errcode'];
  3620. $this->errMsg = $json['errmsg'];
  3621. return false;
  3622. }
  3623. return true;
  3624. }
  3625. return false;
  3626. }
  3627. /**
  3628. * 会员卡交易
  3629. * 会员卡交易后每次积分及余额变更需通过接口通知微信,便于后续消息通知及其他扩展功能。
  3630. * @param string $data 具体结构请参看卡券开发文档(6.1.2 会员卡交易)章节
  3631. * @return boolean|array
  3632. */
  3633. public function updateMemberCard($data)
  3634. {
  3635. if (!$this->access_token && !$this->checkAuth()) return false;
  3636. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_UPDATEUSER . 'access_token=' . $this->access_token, self::json_encode($data));
  3637. if ($result) {
  3638. $json = json_decode($result, true);
  3639. if (!$json || !empty($json['errcode'])) {
  3640. $this->errCode = $json['errcode'];
  3641. $this->errMsg = $json['errmsg'];
  3642. return false;
  3643. }
  3644. return $json;
  3645. }
  3646. return false;
  3647. }
  3648. /**
  3649. * 更新红包金额
  3650. * @param string $code 红包的序列号
  3651. * @param $balance 红包余额
  3652. * @param string $card_id 自定义 code 的卡券必填。非自定义 code 可不填。
  3653. * @return boolean|array
  3654. */
  3655. public function updateLuckyMoney($code, $balance, $card_id = '')
  3656. {
  3657. $data = [
  3658. 'code' => $code,
  3659. 'balance' => $balance,
  3660. ];
  3661. if ($card_id)
  3662. $data['card_id'] = $card_id;
  3663. if (!$this->access_token && !$this->checkAuth()) return false;
  3664. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LUCKYMONEY_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3665. if ($result) {
  3666. $json = json_decode($result, true);
  3667. if (!$json || !empty($json['errcode'])) {
  3668. $this->errCode = $json['errcode'];
  3669. $this->errMsg = $json['errmsg'];
  3670. return false;
  3671. }
  3672. return true;
  3673. }
  3674. return false;
  3675. }
  3676. /**
  3677. * 设置卡券测试白名单
  3678. * @param string $openid 测试的 openid 列表
  3679. * @param string $user 测试的微信号列表
  3680. * @return boolean
  3681. */
  3682. public function setCardTestWhiteList($openid = [], $user = [])
  3683. {
  3684. $data = [];
  3685. if (count($openid) > 0)
  3686. $data['openid'] = $openid;
  3687. if (count($user) > 0)
  3688. $data['username'] = $user;
  3689. if (!$this->access_token && !$this->checkAuth()) return false;
  3690. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_TESTWHILELIST_SET . 'access_token=' . $this->access_token, self::json_encode($data));
  3691. if ($result) {
  3692. $json = json_decode($result, true);
  3693. if (!$json || !empty($json['errcode'])) {
  3694. $this->errCode = $json['errcode'];
  3695. $this->errMsg = $json['errmsg'];
  3696. return false;
  3697. }
  3698. return true;
  3699. }
  3700. return false;
  3701. }
  3702. /**
  3703. * 申请设备ID
  3704. * [applyShakeAroundDevice 申请配置设备所需的UUID、Major、Minor。
  3705. * 若激活率小于50%,不能新增设备。单次新增设备超过500 个,需走人工审核流程。
  3706. * 审核通过后,可用迒回的批次ID 用“查询设备列表”接口拉取本次申请的设备ID]
  3707. * @param array $data
  3708. * array(
  3709. * "quantity" => 3, //申请的设备ID 的数量,单次新增设备超过500 个,需走人工审核流程(必填)
  3710. * "apply_reason" => "测试",//申请理由(必填)
  3711. * "comment" => "测试专用", //备注(非必填)
  3712. * "poi_id" => 1234 //设备关联的门店ID(非必填)
  3713. * )
  3714. * @return boolean|mixed
  3715. * {
  3716. * "data": {
  3717. * "apply_id": 123,
  3718. * "device_identifiers":[
  3719. * {
  3720. * "device_id":10100,
  3721. * "uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
  3722. * "major":10001,
  3723. * "minor":10002
  3724. * }
  3725. * ]
  3726. * },
  3727. * "errcode": 0,
  3728. * "errmsg": "success."
  3729. * }
  3730. *
  3731. * apply_id:申请的批次ID,可用在“查询设备列表”接口按批次查询本次申请成功的设备ID
  3732. * device_identifiers:指定的设备ID 列表
  3733. * device_id:设备编号
  3734. * uuid、major、minor
  3735. * audit_status:审核状态。0:审核未通过、1:审核中、2:审核已通过;审核会在三个工作日内完成
  3736. * audit_comment:审核备注,包括审核不通过的原因
  3737. * @access public
  3738. * @author polo<gao.bo168@gmail.com>
  3739. * @version 2015-3-25 下午1:24:06
  3740. * @copyright Show More
  3741. */
  3742. public function applyShakeAroundDevice($data)
  3743. {
  3744. if (!$this->access_token && !$this->checkAuth()) return false;
  3745. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_APPLYID . 'access_token=' . $this->access_token, self::json_encode($data));
  3746. $this->log($result);
  3747. if ($result) {
  3748. $json = json_decode($result, true);
  3749. if (!$json || !empty($json['errcode'])) {
  3750. $this->errCode = $json['errcode'];
  3751. $this->errMsg = $json['errmsg'];
  3752. return false;
  3753. }
  3754. return $json;
  3755. }
  3756. return false;
  3757. }
  3758. /**
  3759. * 编辑设备信息
  3760. * [updateShakeAroundDevice 编辑设备的备注信息。可用设备ID或完整的UUID、Major、Minor指定设备,二者选其一。]
  3761. * @param array $data
  3762. * array(
  3763. * "device_identifier" => array(
  3764. * "device_id" => 10011, //当提供了device_id则不需要使用uuid、major、minor,反之亦然
  3765. * "uuid" => "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
  3766. * "major" => 1002,
  3767. * "minor" => 1223
  3768. * ),
  3769. * "comment" => "测试专用", //备注(非必填)
  3770. * )
  3771. * {
  3772. * "data": {
  3773. * },
  3774. * "errcode": 0,
  3775. * "errmsg": "success."
  3776. * }
  3777. * @return boolean
  3778. * @author binsee<binsee@163.com>
  3779. * @version 2015-4-20 23:45:00
  3780. */
  3781. public function updateShakeAroundDevice($data)
  3782. {
  3783. if (!$this->access_token && !$this->checkAuth()) return false;
  3784. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3785. $this->log($result);
  3786. if ($result) {
  3787. $json = json_decode($result, true);
  3788. if (!$json || !empty($json['errcode'])) {
  3789. $this->errCode = $json['errcode'];
  3790. $this->errMsg = $json['errmsg'];
  3791. return false;
  3792. }
  3793. return true;
  3794. }
  3795. return false;
  3796. }
  3797. /**
  3798. * 查询设备列表
  3799. * [searchShakeAroundDevice 查询已有的设备ID、UUID、Major、Minor、激活状态、备注信息、关联门店、关联页面等信息。
  3800. * 可指定设备ID 或完整的UUID、Major、Minor 查询,也可批量拉取设备信息列表。]
  3801. * @param array $data
  3802. * $data 三种格式:
  3803. * ①查询指定设备时:$data = array(
  3804. * "device_identifiers" => array(
  3805. * array(
  3806. * "device_id" => 10100,
  3807. * "uuid" => "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
  3808. * "major" => 10001,
  3809. * "minor" => 10002
  3810. * )
  3811. * )
  3812. * );
  3813. * device_identifiers:指定的设备
  3814. * device_id:设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先
  3815. * uuid、major、minor:三个信息需填写完整,若填了设备编号,则可不填此信息
  3816. * +-------------------------------------------------------------------------------------------------------------
  3817. * ②需要分页查询或者指定范围内的设备时: $data = array(
  3818. * "begin" => 0,
  3819. * "count" => 3
  3820. * );
  3821. * begin:设备列表的起始索引值
  3822. * count:待查询的设备个数
  3823. * +-------------------------------------------------------------------------------------------------------------
  3824. * ③当需要根据批次ID 查询时: $data = array(
  3825. * "apply_id" => 1231,
  3826. * "begin" => 0,
  3827. * "count" => 3
  3828. * );
  3829. * apply_id:批次ID
  3830. * +-------------------------------------------------------------------------------------------------------------
  3831. * @return boolean|mixed
  3832. *正确迒回JSON 数据示例:
  3833. *字段说明
  3834. * {
  3835. * "data": {
  3836. * "devices": [ //指定的设备信息列表
  3837. * {
  3838. * "comment": "", //设备的备注信息
  3839. * "device_id": 10097, //设备编号
  3840. * "major": 10001,
  3841. * "minor": 12102,
  3842. * "page_ids": "15369", //与此设备关联的页面ID 列表,用逗号隔开
  3843. * "status": 1, //激活状态,0:未激活,1:已激活(但不活跃),2:活跃
  3844. * "poi_id": 0, //门店ID
  3845. * "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
  3846. * },
  3847. * {
  3848. * "comment": "", //设备的备注信息
  3849. * "device_id": 10098, //设备编号
  3850. * "major": 10001,
  3851. * "minor": 12103,
  3852. * "page_ids": "15368", //与此设备关联的页面ID 列表,用逗号隔开
  3853. * "status": 1, //激活状态,0:未激活,1:已激活(但不活跃),2:活跃
  3854. * "poi_id": 0, //门店ID
  3855. * "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
  3856. * }
  3857. * ],
  3858. * "total_count": 151 //商户名下的设备总量
  3859. * },
  3860. * "errcode": 0,
  3861. * "errmsg": "success."
  3862. * }
  3863. * @access public
  3864. * @author polo<gao.bo168@gmail.com>
  3865. * @version 2015-3-25 下午1:45:42
  3866. * @copyright Show More
  3867. */
  3868. public function searchShakeAroundDevice($data)
  3869. {
  3870. if (!$this->access_token && !$this->checkAuth()) return false;
  3871. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_SEARCH . 'access_token=' . $this->access_token, self::json_encode($data));
  3872. $this->log($result);
  3873. if ($result) {
  3874. $json = json_decode($result, true);
  3875. if (!$json || !empty($json['errcode'])) {
  3876. $this->errCode = $json['errcode'];
  3877. $this->errMsg = $json['errmsg'];
  3878. return false;
  3879. }
  3880. return $json;
  3881. }
  3882. return false;
  3883. }
  3884. /**
  3885. * [bindLocationShakeAroundDevice 配置设备与门店的关联关系]
  3886. * @param string $device_id 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先
  3887. * @param int $poi_id 待关联的门店ID
  3888. * @param string $uuid UUID、major、minor,三个信息需填写完整,若填了设备编号,则可不填此信息
  3889. * @param int $major
  3890. * @param int $minor
  3891. * @return boolean|mixed
  3892. * 正确返回JSON 数据示例:
  3893. * {
  3894. * "data": {
  3895. * },
  3896. * "errcode": 0,
  3897. * "errmsg": "success."
  3898. * }
  3899. * @access public
  3900. * @author polo<gao.bo168@gmail.com>
  3901. * @version 2015-4-21 00:14:00
  3902. * @copyright Show More
  3903. */
  3904. public function bindLocationShakeAroundDevice($device_id, $poi_id, $uuid = '', $major = 0, $minor = 0)
  3905. {
  3906. if (!$this->access_token && !$this->checkAuth()) return false;
  3907. if (!$device_id) {
  3908. if (!$uuid || !$major || !$minor) {
  3909. return false;
  3910. }
  3911. $device_identifier = [
  3912. 'uuid' => $uuid,
  3913. 'major' => $major,
  3914. 'minor' => $minor,
  3915. ];
  3916. } else {
  3917. $device_identifier = [
  3918. 'device_id' => $device_id,
  3919. ];
  3920. }
  3921. $data = [
  3922. 'device_identifier' => $device_identifier,
  3923. 'poi_id' => $poi_id,
  3924. ];
  3925. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_BINDLOCATION . 'access_token=' . $this->access_token, self::json_encode($data));
  3926. $this->log($result);
  3927. if ($result) {
  3928. $json = json_decode($result, true);
  3929. if (!$json || !empty($json['errcode'])) {
  3930. $this->errCode = $json['errcode'];
  3931. $this->errMsg = $json['errmsg'];
  3932. return false;
  3933. }
  3934. return $json; //这个可以更改为返回true
  3935. }
  3936. return false;
  3937. }
  3938. /**
  3939. * [bindPageShakeAroundDevice 配置设备与页面的关联关系。
  3940. * 支持建立或解除关联关系,也支持新增页面或覆盖页面等操作。
  3941. * 配置完成后,在此设备的信号范围内,即可摇出关联的页面信息。
  3942. * 若设备配置多个页面,则随机出现页面信息]
  3943. * @param string $device_id 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先
  3944. * @param array $page_ids 待关联的页面列表
  3945. * @param number $bind 关联操作标志位, 0 为解除关联关系,1 为建立关联关系
  3946. * @param number $append 新增操作标志位, 0 为覆盖,1 为新增
  3947. * @param string $uuid UUID、major、minor,三个信息需填写完整,若填了设备编号,则可不填此信息
  3948. * @param int $major
  3949. * @param int $minor
  3950. * @return boolean|mixed
  3951. * 正确返回JSON 数据示例:
  3952. * {
  3953. * "data": {
  3954. * },
  3955. * "errcode": 0,
  3956. * "errmsg": "success."
  3957. * }
  3958. * @access public
  3959. * @author polo<gao.bo168@gmail.com>
  3960. * @version 2015-4-21 00:31:00
  3961. * @copyright Show More
  3962. */
  3963. public function bindPageShakeAroundDevice($device_id, $page_ids = [], $bind = 1, $append = 1, $uuid = '', $major = 0, $minor = 0)
  3964. {
  3965. if (!$this->access_token && !$this->checkAuth()) return false;
  3966. if (!$device_id) {
  3967. if (!$uuid || !$major || !$minor) {
  3968. return false;
  3969. }
  3970. $device_identifier = [
  3971. 'uuid' => $uuid,
  3972. 'major' => $major,
  3973. 'minor' => $minor,
  3974. ];
  3975. } else {
  3976. $device_identifier = [
  3977. 'device_id' => $device_id,
  3978. ];
  3979. }
  3980. $data = [
  3981. 'device_identifier' => $device_identifier,
  3982. 'page_ids' => $page_ids,
  3983. 'bind' => $bind,
  3984. 'append' => $append,
  3985. ];
  3986. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_BINDPAGE . 'access_token=' . $this->access_token, self::json_encode($data));
  3987. $this->log($result);
  3988. if ($result) {
  3989. $json = json_decode($result, true);
  3990. if (!$json || !empty($json['errcode'])) {
  3991. $this->errCode = $json['errcode'];
  3992. $this->errMsg = $json['errmsg'];
  3993. return false;
  3994. }
  3995. return $json;
  3996. }
  3997. return false;
  3998. }
  3999. /**
  4000. * 上传在摇一摇页面展示的图片素材
  4001. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  4002. * @param array $data {"media":'@Path\filename.jpg'} 格式限定为:jpg,jpeg,png,gif,图片大小建议120px*120 px,限制不超过200 px *200 px,图片需为正方形。
  4003. * @return boolean|array
  4004. * {
  4005. * "data": {
  4006. * "pic_url":"http://shp.qpic.cn/wechat_shakearound_pic/0/1428377032e9dd2797018cad79186e03e8c5aec8dc/120"
  4007. * },
  4008. * "errcode": 0,
  4009. * "errmsg": "success."
  4010. * }
  4011. * }
  4012. * @author binsee<binsee@163.com>
  4013. * @version 2015-4-21 00:51:00
  4014. */
  4015. public function uploadShakeAroundMedia($data)
  4016. {
  4017. if (!$this->access_token && !$this->checkAuth()) return false;
  4018. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_MATERIAL_ADD . 'access_token=' . $this->access_token, $data, true);
  4019. if ($result) {
  4020. $json = json_decode($result, true);
  4021. if (!$json || !empty($json['errcode'])) {
  4022. $this->errCode = $json['errcode'];
  4023. $this->errMsg = $json['errmsg'];
  4024. return false;
  4025. }
  4026. return $json;
  4027. }
  4028. return false;
  4029. }
  4030. /**
  4031. * [addShakeAroundPage 增加摇一摇出来的页面信息,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。]
  4032. * @param string $title 在摇一摇页面展示的主标题,不超过6 个字
  4033. * @param string $description 在摇一摇页面展示的副标题,不超过7 个字
  4034. * @param sting $icon_url 在摇一摇页面展示的图片, 格式限定为:jpg,jpeg,png,gif; 建议120*120 , 限制不超过200*200
  4035. * @param string $page_url 跳转链接
  4036. * @param string $comment 页面的备注信息,不超过15 个字,可不填
  4037. * @return boolean|mixed
  4038. * 正确返回JSON 数据示例:
  4039. * {
  4040. * "data": {
  4041. * "page_id": 28840 //新增页面的页面id
  4042. * }
  4043. * "errcode": 0,
  4044. * "errmsg": "success."
  4045. * }
  4046. * @access public
  4047. * @author polo<gao.bo168@gmail.com>
  4048. * @version 2015-3-25 下午2:57:09
  4049. * @copyright Show More
  4050. */
  4051. public function addShakeAroundPage($title, $description, $icon_url, $page_url, $comment = '')
  4052. {
  4053. if (!$this->access_token && !$this->checkAuth()) return false;
  4054. $data = [
  4055. "title" => $title,
  4056. "description" => $description,
  4057. "icon_url" => $icon_url,
  4058. "page_url" => $page_url,
  4059. "comment" => $comment,
  4060. ];
  4061. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_ADD . 'access_token=' . $this->access_token, self::json_encode($data));
  4062. $this->log($result);
  4063. if ($result) {
  4064. $json = json_decode($result, true);
  4065. if (!$json || !empty($json['errcode'])) {
  4066. $this->errCode = $json['errcode'];
  4067. $this->errMsg = $json['errmsg'];
  4068. return false;
  4069. }
  4070. return $json;
  4071. }
  4072. return false;
  4073. }
  4074. /**
  4075. * [updateShakeAroundPage 编辑摇一摇出来的页面信息,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。]
  4076. * @param int $page_id
  4077. * @param string $title 在摇一摇页面展示的主标题,不超过6 个字
  4078. * @param string $description 在摇一摇页面展示的副标题,不超过7 个字
  4079. * @param sting $icon_url 在摇一摇页面展示的图片, 格式限定为:jpg,jpeg,png,gif; 建议120*120 , 限制不超过200*200
  4080. * @param string $page_url 跳转链接
  4081. * @param string $comment 页面的备注信息,不超过15 个字,可不填
  4082. * @return boolean|mixed
  4083. * 正确返回JSON 数据示例:
  4084. * {
  4085. * "data": {
  4086. * "page_id": 28840 //编辑页面的页面ID
  4087. * }
  4088. * "errcode": 0,
  4089. * "errmsg": "success."
  4090. * }
  4091. * @access public
  4092. * @author polo<gao.bo168@gmail.com>
  4093. * @version 2015-3-25 下午3:02:51
  4094. * @copyright Show More
  4095. */
  4096. public function updateShakeAroundPage($page_id, $title, $description, $icon_url, $page_url, $comment = '')
  4097. {
  4098. if (!$this->access_token && !$this->checkAuth()) return false;
  4099. $data = [
  4100. "page_id" => $page_id,
  4101. "title" => $title,
  4102. "description" => $description,
  4103. "icon_url" => $icon_url,
  4104. "page_url" => $page_url,
  4105. "comment" => $comment,
  4106. ];
  4107. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  4108. $this->log($result);
  4109. if ($result) {
  4110. $json = json_decode($result, true);
  4111. if (!$json || !empty($json['errcode'])) {
  4112. $this->errCode = $json['errcode'];
  4113. $this->errMsg = $json['errmsg'];
  4114. return false;
  4115. }
  4116. return $json;
  4117. }
  4118. return false;
  4119. }
  4120. /**
  4121. * [searchShakeAroundPage 查询已有的页面,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。
  4122. * 提供两种查询方式,①可指定页面ID 查询,②也可批量拉取页面列表。]
  4123. * @param array $page_ids
  4124. * @param int $begin
  4125. * @param int $count
  4126. * ①需要查询指定页面时:
  4127. * {
  4128. * "page_ids":[12345, 23456, 34567]
  4129. * }
  4130. * +-------------------------------------------------------------------------------------------------------------
  4131. * ②需要分页查询或者指定范围内的页面时:
  4132. * {
  4133. * "begin": 0,
  4134. * "count": 3
  4135. * }
  4136. * +-------------------------------------------------------------------------------------------------------------
  4137. * @return boolean|mixed
  4138. * 正确返回JSON 数据示例:
  4139. * {
  4140. * "data": {
  4141. * "pages": [
  4142. * {
  4143. * "comment": "just for test",
  4144. * "description": "test",
  4145. * "icon_url": "https://www.baidu.com/img/bd_logo1.png",
  4146. * "page_id": 28840,
  4147. * "page_url": "http://xw.qq.com/testapi1",
  4148. * "title": "测试1"
  4149. * },
  4150. * {
  4151. * "comment": "just for test",
  4152. * "description": "test",
  4153. * "icon_url": "https://www.baidu.com/img/bd_logo1.png",
  4154. * "page_id": 28842,
  4155. * "page_url": "http://xw.qq.com/testapi2",
  4156. * "title": "测试2"
  4157. * }
  4158. * ],
  4159. * "total_count": 2
  4160. * },
  4161. * "errcode": 0,
  4162. * "errmsg": "success."
  4163. * }
  4164. *字段说明:
  4165. *total_count 商户名下的页面总数
  4166. *page_id 摇周边页面唯一ID
  4167. *title 在摇一摇页面展示的主标题
  4168. *description 在摇一摇页面展示的副标题
  4169. *icon_url 在摇一摇页面展示的图片
  4170. *page_url 跳转链接
  4171. *comment 页面的备注信息
  4172. * @access public
  4173. * @author polo<gao.bo168@gmail.com>
  4174. * @version 2015-3-25 下午3:12:17
  4175. * @copyright Show More
  4176. */
  4177. public function searchShakeAroundPage($page_ids = [], $begin = 0, $count = 1)
  4178. {
  4179. if (!$this->access_token && !$this->checkAuth()) return false;
  4180. if (!empty($page_ids)) {
  4181. $data = [
  4182. 'page_ids' => $page_ids,
  4183. ];
  4184. } else {
  4185. $data = [
  4186. 'begin' => $begin,
  4187. 'count' => $count,
  4188. ];
  4189. }
  4190. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_SEARCH . 'access_token=' . $this->access_token, self::json_encode($data));
  4191. $this->log($result);
  4192. if ($result) {
  4193. $json = json_decode($result, true);
  4194. if (!$json || !empty($json['errcode'])) {
  4195. $this->errCode = $json['errcode'];
  4196. $this->errMsg = $json['errmsg'];
  4197. return false;
  4198. }
  4199. return $json;
  4200. }
  4201. return false;
  4202. }
  4203. /**
  4204. * [deleteShakeAroundPage 删除已有的页面,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。
  4205. * 只有页面与设备没有关联关系时,才可被删除。]
  4206. * @param array $page_ids
  4207. * {
  4208. * "page_ids":[12345,23456,34567]
  4209. * }
  4210. * @return boolean|mixed
  4211. * 正确返回JSON 数据示例:
  4212. * {
  4213. * "data": {
  4214. * },
  4215. * "errcode": 0,
  4216. * "errmsg": "success."
  4217. * }
  4218. * @access public
  4219. * @author polo<gao.bo168@gmail.com>
  4220. * @version 2015-3-25 下午3:23:00
  4221. * @copyright Show More
  4222. */
  4223. public function deleteShakeAroundPage($page_ids = [])
  4224. {
  4225. if (!$this->access_token && !$this->checkAuth()) return false;
  4226. $data = [
  4227. 'page_ids' => $page_ids,
  4228. ];
  4229. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_DELETE . 'access_token=' . $this->access_token, self::json_encode($data));
  4230. $this->log($result);
  4231. if ($result) {
  4232. $json = json_decode($result, true);
  4233. if (!$json || !empty($json['errcode'])) {
  4234. $this->errCode = $json['errcode'];
  4235. $this->errMsg = $json['errmsg'];
  4236. return false;
  4237. }
  4238. return $json;
  4239. }
  4240. return false;
  4241. }
  4242. /**
  4243. * [getShakeInfoShakeAroundUser 获取设备信息,包括UUID、major、minor,以及距离、openID 等信息。]
  4244. * @param string $ticket 摇周边业务的ticket,可在摇到的URL 中得到,ticket生效时间为30 分钟
  4245. * @return boolean|mixed
  4246. * 正确返回JSON 数据示例:
  4247. * {
  4248. * "data": {
  4249. * "page_id ": 14211,
  4250. * "beacon_info": {
  4251. * "distance": 55.00620700469034,
  4252. * "major": 10001,
  4253. * "minor": 19007,
  4254. * "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
  4255. * },
  4256. * "openid": "oVDmXjp7y8aG2AlBuRpMZTb1-cmA"
  4257. * },
  4258. * "errcode": 0,
  4259. * "errmsg": "success."
  4260. * }
  4261. * 字段说明:
  4262. * beacon_info 设备信息,包括UUID、major、minor,以及距离
  4263. * UUID、major、minor UUID、major、minor
  4264. * distance Beacon 信号与手机的距离
  4265. * page_id 摇周边页面唯一ID
  4266. * openid 商户AppID 下用户的唯一标识
  4267. * poi_id 门店ID,有的话则返回,没有的话不会在JSON 格式内
  4268. * @access public
  4269. * @author polo<gao.bo168@gmail.com>
  4270. * @version 2015-3-25 下午3:28:20
  4271. * @copyright Show More
  4272. */
  4273. public function getShakeInfoShakeAroundUser($ticket)
  4274. {
  4275. if (!$this->access_token && !$this->checkAuth()) return false;
  4276. $data = ['ticket' => $ticket];
  4277. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_USER_GETSHAKEINFO . 'access_token=' . $this->access_token, self::json_encode($data));
  4278. $this->log($result);
  4279. if ($result) {
  4280. $json = json_decode($result, true);
  4281. if (!$json || !empty($json['errcode'])) {
  4282. $this->errCode = $json['errcode'];
  4283. $this->errMsg = $json['errmsg'];
  4284. return false;
  4285. }
  4286. return $json;
  4287. }
  4288. return false;
  4289. }
  4290. /**
  4291. * [deviceShakeAroundStatistics 以设备为维度的数据统计接口。
  4292. * 查询单个设备进行摇周边操作的人数、次数,点击摇周边消息的人数、次数;查询的最长时间跨度为30天。]
  4293. * @param int $device_id 设备编号,若填了UUID、major、minor,即可不填设备编号,二者选其一
  4294. * @param int $begin_date 起始日期时间戳,最长时间跨度为30 天
  4295. * @param int $end_date 结束日期时间戳,最长时间跨度为30 天
  4296. * @param string $uuid UUID、major、minor,三个信息需填写完成,若填了设备编辑,即可不填此信息,二者选其一
  4297. * @param int $major
  4298. * @param int $minor
  4299. * @return boolean|mixed
  4300. * 正确返回JSON 数据示例:
  4301. * {
  4302. * "data": [
  4303. * {
  4304. * "click_pv": 0,
  4305. * "click_uv": 0,
  4306. * "ftime": 1425052800,
  4307. * "shake_pv": 0,
  4308. * "shake_uv": 0
  4309. * },
  4310. * {
  4311. * "click_pv": 0,
  4312. * "click_uv": 0,
  4313. * "ftime": 1425139200,
  4314. * "shake_pv": 0,
  4315. * "shake_uv": 0
  4316. * }
  4317. * ],
  4318. * "errcode": 0,
  4319. * "errmsg": "success."
  4320. * }
  4321. * 字段说明:
  4322. * ftime 当天0 点对应的时间戳
  4323. * click_pv 点击摇周边消息的次数
  4324. * click_uv 点击摇周边消息的人数
  4325. * shake_pv 摇周边的次数
  4326. * shake_uv 摇周边的人数
  4327. * @access public
  4328. * @author polo<gao.bo168@gmail.com>
  4329. * @version 2015-4-21 00:39:00
  4330. * @copyright Show More
  4331. */
  4332. public function deviceShakeAroundStatistics($device_id, $begin_date, $end_date, $uuid = '', $major = 0, $minor = 0)
  4333. {
  4334. if (!$this->access_token && !$this->checkAuth()) return false;
  4335. if (!$device_id) {
  4336. if (!$uuid || !$major || !$minor) {
  4337. return false;
  4338. }
  4339. $device_identifier = [
  4340. 'uuid' => $uuid,
  4341. 'major' => $major,
  4342. 'minor' => $minor,
  4343. ];
  4344. } else {
  4345. $device_identifier = [
  4346. 'device_id' => $device_id,
  4347. ];
  4348. }
  4349. $data = [
  4350. 'device_identifier' => $device_identifier,
  4351. 'begin_date' => $begin_date,
  4352. 'end_date' => $end_date,
  4353. ];
  4354. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_STATISTICS_DEVICE . 'access_token=' . $this->access_token, self::json_encode($data));
  4355. $this->log($result);
  4356. if ($result) {
  4357. $json = json_decode($result, true);
  4358. if (!$json || !empty($json['errcode'])) {
  4359. $this->errCode = $json['errcode'];
  4360. $this->errMsg = $json['errmsg'];
  4361. return false;
  4362. }
  4363. return $json;
  4364. }
  4365. return false;
  4366. }
  4367. /**
  4368. * [pageShakeAroundStatistics 以页面为维度的数据统计接口。
  4369. * 查询单个页面通过摇周边摇出来的人数、次数,点击摇周边页面的人数、次数;查询的最长时间跨度为30天。]
  4370. * @param int $page_id 指定页面的ID
  4371. * @param int $begin_date 起始日期时间戳,最长时间跨度为30 天
  4372. * @param int $end_date 结束日期时间戳,最长时间跨度为30 天
  4373. * @return boolean|mixed
  4374. * 正确返回JSON 数据示例:
  4375. * {
  4376. * "data": [
  4377. * {
  4378. * "click_pv": 0,
  4379. * "click_uv": 0,
  4380. * "ftime": 1425052800,
  4381. * "shake_pv": 0,
  4382. * "shake_uv": 0
  4383. * },
  4384. * {
  4385. * "click_pv": 0,
  4386. * "click_uv": 0,
  4387. * "ftime": 1425139200,
  4388. * "shake_pv": 0,
  4389. * "shake_uv": 0
  4390. * }
  4391. * ],
  4392. * "errcode": 0,
  4393. * "errmsg": "success."
  4394. * }
  4395. * 字段说明:
  4396. * ftime 当天0 点对应的时间戳
  4397. * click_pv 点击摇周边消息的次数
  4398. * click_uv 点击摇周边消息的人数
  4399. * shake_pv 摇周边的次数
  4400. * shake_uv 摇周边的人数
  4401. * @author binsee<binsee@163.com>
  4402. * @version 2015-4-21 00:43:00
  4403. */
  4404. public function pageShakeAroundStatistics($page_id, $begin_date, $end_date)
  4405. {
  4406. if (!$this->access_token && !$this->checkAuth()) return false;
  4407. $data = [
  4408. 'page_id' => $page_id,
  4409. 'begin_date' => $begin_date,
  4410. 'end_date' => $end_date,
  4411. ];
  4412. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_STATISTICS_DEVICE . 'access_token=' . $this->access_token, self::json_encode($data));
  4413. $this->log($result);
  4414. if ($result) {
  4415. $json = json_decode($result, true);
  4416. if (!$json || !empty($json['errcode'])) {
  4417. $this->errCode = $json['errcode'];
  4418. $this->errMsg = $json['errmsg'];
  4419. return false;
  4420. }
  4421. return $json;
  4422. }
  4423. return false;
  4424. }
  4425. /**
  4426. * 根据订单ID获取订单详情
  4427. * @param string $order_id 订单ID
  4428. * @return order array|bool
  4429. */
  4430. public function getOrderByID($order_id)
  4431. {
  4432. if (!$this->access_token && !$this->checkAuth()) return false;
  4433. if (!$order_id) return false;
  4434. $data = [
  4435. 'order_id' => $order_id,
  4436. ];
  4437. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::MERCHANT_ORDER_GETBYID . 'access_token=' . $this->access_token, self::json_encode($data));
  4438. if ($result) {
  4439. $json = json_decode($result, true);
  4440. if (isset($json['errcode']) && $json['errcode']) {
  4441. $this->errCode = $json['errcode'];
  4442. $this->errMsg = $json['errmsg'];
  4443. return false;
  4444. }
  4445. return $json['order'];
  4446. }
  4447. return false;
  4448. }
  4449. /**
  4450. * 根据订单状态/创建时间获取订单详情
  4451. * @param int $status 订单状态(不带该字段-全部状态, 2-待发货, 3-已发货, 5-已完成, 8-维权中, )
  4452. * @param int $begintime 订单创建时间起始时间(不带该字段则不按照时间做筛选)
  4453. * @param int $endtime 订单创建时间终止时间(不带该字段则不按照时间做筛选)
  4454. * @return order list array|bool
  4455. */
  4456. public function getOrderByFilter($status = null, $begintime = null, $endtime = null)
  4457. {
  4458. if (!$this->access_token && !$this->checkAuth()) return false;
  4459. $data = [];
  4460. $valid_status = [2, 3, 5, 8];
  4461. if (is_numeric($status) && in_array($status, $valid_status)) {
  4462. $data['status'] = $status;
  4463. }
  4464. if (is_numeric($begintime) && is_numeric($endtime)) {
  4465. $data['begintime'] = $begintime;
  4466. $data['endtime'] = $endtime;
  4467. }
  4468. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::MERCHANT_ORDER_GETBYFILTER . 'access_token=' . $this->access_token, self::json_encode($data));
  4469. if ($result) {
  4470. $json = json_decode($result, true);
  4471. if (isset($json['errcode']) && $json['errcode']) {
  4472. $this->errCode = $json['errcode'];
  4473. $this->errMsg = $json['errmsg'];
  4474. return false;
  4475. }
  4476. return $json['order_list'];
  4477. }
  4478. return false;
  4479. }
  4480. /**
  4481. * 设置订单发货信息
  4482. * @param string $order_id 订单 ID
  4483. * @param int $need_delivery 商品是否需要物流(0-不需要,1-需要)
  4484. * @param string $delivery_company 物流公司 ID
  4485. * @param string $delivery_track_no 运单 ID
  4486. * @param int $is_others 是否为 6.4.5 表之外的其它物流公司(0-否,1-是)
  4487. * @return bool
  4488. */
  4489. public function setOrderDelivery($order_id, $need_delivery = 0, $delivery_company = null, $delivery_track_no = null, $is_others = 0)
  4490. {
  4491. if (!$this->access_token && !$this->checkAuth()) return false;
  4492. if (!$order_id) return false;
  4493. $data = [];
  4494. $data['order_id'] = $order_id;
  4495. if ($need_delivery) {
  4496. $data['delivery_company'] = $delivery_company;
  4497. $data['delivery_track_no'] = $delivery_track_no;
  4498. $data['is_others'] = $is_others;
  4499. } else {
  4500. $data['need_delivery'] = $need_delivery;
  4501. }
  4502. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::MERCHANT_ORDER_SETDELIVERY . 'access_token=' . $this->access_token, self::json_encode($data));
  4503. if ($result) {
  4504. $json = json_decode($result, true);
  4505. if (isset($json['errcode']) && $json['errcode']) {
  4506. $this->errCode = $json['errcode'];
  4507. $this->errMsg = $json['errmsg'];
  4508. return false;
  4509. }
  4510. return true;
  4511. }
  4512. return false;
  4513. }
  4514. /**
  4515. * 关闭订单
  4516. * @param string $order_id 订单 ID
  4517. * @return bool
  4518. */
  4519. public function closeOrder($order_id)
  4520. {
  4521. if (!$this->access_token && !$this->checkAuth()) return false;
  4522. if (!$order_id) return false;
  4523. $data = [
  4524. 'order_id' => $order_id,
  4525. ];
  4526. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::MERCHANT_ORDER_CLOSE . 'access_token=' . $this->access_token, self::json_encode($data));
  4527. if ($result) {
  4528. $json = json_decode($result, true);
  4529. if (isset($json['errcode']) && $json['errcode']) {
  4530. $this->errCode = $json['errcode'];
  4531. $this->errMsg = $json['errmsg'];
  4532. return false;
  4533. }
  4534. return true;
  4535. }
  4536. return false;
  4537. }
  4538. private function parseSkuInfo($skuInfo)
  4539. {
  4540. $skuInfo = str_replace("\$", "", $skuInfo);
  4541. $matches = explode(";", $skuInfo);
  4542. $result = [];
  4543. foreach ($matches as $matche) {
  4544. $arrs = explode(":", $matche);
  4545. $result[$arrs[0]] = $arrs[1];
  4546. }
  4547. return $result;
  4548. }
  4549. /**
  4550. * 获取订单SkuInfo - 订单付款通知
  4551. * 当Event为 merchant_order(订单付款通知)
  4552. * @return array|boolean
  4553. */
  4554. public function getRevOrderSkuInfo()
  4555. {
  4556. if (isset($this->_receive['SkuInfo'])) //订单 SkuInfo
  4557. return $this->parseSkuInfo($this->_receive['SkuInfo']);
  4558. else
  4559. return false;
  4560. }
  4561. }
  4562. /**
  4563. * PKCS7Encoder class
  4564. *
  4565. * 提供基于PKCS7算法的加解密接口.
  4566. */
  4567. class PKCS7Encoder
  4568. {
  4569. public static $block_size = 32;
  4570. /**
  4571. * 对需要加密的明文进行填充补位
  4572. * @param $text 需要进行填充补位操作的明文
  4573. * @return 补齐明文字符串
  4574. */
  4575. function encode($text)
  4576. {
  4577. $block_size = PKCS7Encoder::$block_size;
  4578. $text_length = strlen($text);
  4579. //计算需要填充的位数
  4580. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  4581. if ($amount_to_pad == 0) {
  4582. $amount_to_pad = PKCS7Encoder::block_size;
  4583. }
  4584. //获得补位所用的字符
  4585. $pad_chr = chr($amount_to_pad);
  4586. $tmp = "";
  4587. for ($index = 0; $index < $amount_to_pad; $index++) {
  4588. $tmp .= $pad_chr;
  4589. }
  4590. return $text . $tmp;
  4591. }
  4592. /**
  4593. * 对解密后的明文进行补位删除
  4594. * @param decrypted 解密后的明文
  4595. * @return 删除填充补位后的明文
  4596. */
  4597. function decode($text)
  4598. {
  4599. $pad = ord(substr($text, -1));
  4600. if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
  4601. $pad = 0;
  4602. }
  4603. return substr($text, 0, (strlen($text) - $pad));
  4604. }
  4605. }
  4606. /**
  4607. * Prpcrypt class
  4608. *
  4609. * 提供接收和推送给公众平台消息的加解密接口.
  4610. */
  4611. class Prpcrypt
  4612. {
  4613. public $key;
  4614. function __construct($k)
  4615. {
  4616. $this->key = base64_decode($k . "=");
  4617. }
  4618. /**
  4619. * 兼容老版本php构造函数,不能在 __construct() 方法前边,否则报错
  4620. */
  4621. function Prpcrypt($k)
  4622. {
  4623. $this->key = base64_decode($k . "=");
  4624. }
  4625. /**
  4626. * 对明文进行加密
  4627. * @param string $text 需要加密的明文
  4628. * @return string 加密后的密文
  4629. */
  4630. public function encrypt($text, $appid)
  4631. {
  4632. try {
  4633. //获得16位随机字符串,填充到明文之前
  4634. $random = $this->getRandomStr();//"aaaabbbbccccdddd";
  4635. $text = $random . pack("N", strlen($text)) . $text . $appid;
  4636. // 网络字节序
  4637. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  4638. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  4639. $iv = substr($this->key, 0, 16);
  4640. //使用自定义的填充方式对明文进行补位填充
  4641. $pkc_encoder = new PKCS7Encoder;
  4642. $text = $pkc_encoder->encode($text);
  4643. mcrypt_generic_init($module, $this->key, $iv);
  4644. //加密
  4645. $encrypted = mcrypt_generic($module, $text);
  4646. mcrypt_generic_deinit($module);
  4647. mcrypt_module_close($module);
  4648. // print(base64_encode($encrypted));
  4649. //使用BASE64对加密后的字符串进行编码
  4650. return [ErrorCode::$OK, base64_encode($encrypted)];
  4651. } catch (Exception $e) {
  4652. //print $e;
  4653. return [ErrorCode::$EncryptAESError, null];
  4654. }
  4655. }
  4656. /**
  4657. * 对密文进行解密
  4658. * @param string $encrypted 需要解密的密文
  4659. * @return string 解密得到的明文
  4660. */
  4661. public function decrypt($encrypted, $appid)
  4662. {
  4663. try {
  4664. //使用BASE64对需要解密的字符串进行解码
  4665. $ciphertext_dec = base64_decode($encrypted);
  4666. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  4667. $iv = substr($this->key, 0, 16);
  4668. mcrypt_generic_init($module, $this->key, $iv);
  4669. //解密
  4670. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  4671. mcrypt_generic_deinit($module);
  4672. mcrypt_module_close($module);
  4673. } catch (Exception $e) {
  4674. return [ErrorCode::$DecryptAESError, null];
  4675. }
  4676. try {
  4677. //去除补位字符
  4678. $pkc_encoder = new PKCS7Encoder;
  4679. $result = $pkc_encoder->decode($decrypted);
  4680. //去除16位随机字符串,网络字节序和AppId
  4681. if (strlen($result) < 16)
  4682. return "";
  4683. $content = substr($result, 16, strlen($result));
  4684. $len_list = unpack("N", substr($content, 0, 4));
  4685. $xml_len = $len_list[1];
  4686. $xml_content = substr($content, 4, $xml_len);
  4687. $from_appid = substr($content, $xml_len + 4);
  4688. if (!$appid)
  4689. $appid = $from_appid;
  4690. //如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
  4691. } catch (Exception $e) {
  4692. //print $e;
  4693. return [ErrorCode::$IllegalBuffer, null];
  4694. }
  4695. if ($from_appid != $appid)
  4696. return [ErrorCode::$ValidateAppidError, null];
  4697. //不注释上边两行,避免传入appid是错误的情况
  4698. return [0, $xml_content, $from_appid]; //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
  4699. }
  4700. /**
  4701. * 随机生成16位字符串
  4702. * @return string 生成的字符串
  4703. */
  4704. function getRandomStr()
  4705. {
  4706. $str = "";
  4707. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  4708. $max = strlen($str_pol) - 1;
  4709. for ($i = 0; $i < 16; $i++) {
  4710. $str .= $str_pol[mt_rand(0, $max)];
  4711. }
  4712. return $str;
  4713. }
  4714. }
  4715. /**
  4716. * error code
  4717. * 仅用作类内部使用,不用于官方API接口的errCode码
  4718. */
  4719. class ErrorCode
  4720. {
  4721. public static $OK = 0;
  4722. public static $ValidateSignatureError = 40001;
  4723. public static $ParseXmlError = 40002;
  4724. public static $ComputeSignatureError = 40003;
  4725. public static $IllegalAesKey = 40004;
  4726. public static $ValidateAppidError = 40005;
  4727. public static $EncryptAESError = 40006;
  4728. public static $DecryptAESError = 40007;
  4729. public static $IllegalBuffer = 40008;
  4730. public static $EncodeBase64Error = 40009;
  4731. public static $DecodeBase64Error = 40010;
  4732. public static $GenReturnXmlError = 40011;
  4733. public static $errCode
  4734. = [
  4735. '0' => '处理成功',
  4736. '40001' => '校验签名失败',
  4737. '40002' => '解析xml失败',
  4738. '40003' => '计算签名失败',
  4739. '40004' => '不合法的AESKey',
  4740. '40005' => '校验AppID失败',
  4741. '40006' => 'AES加密失败',
  4742. '40007' => 'AES解密失败',
  4743. '40008' => '公众平台发送的xml不合法',
  4744. '40009' => 'Base64编码失败',
  4745. '40010' => 'Base64解码失败',
  4746. '40011' => '公众帐号生成回包xml失败',
  4747. ];
  4748. public static function getErrText($err)
  4749. {
  4750. if (isset(self::$errCode[$err])) {
  4751. return self::$errCode[$err];
  4752. } else {
  4753. return false;
  4754. };
  4755. }
  4756. }