functions.php 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099
  1. <?php
  2. namespace App;
  3. /* Redis链接 */
  4. function connectionRedis(){
  5. $REDIS_HOST= \PhalApi\DI()->config->get('app.REDIS_HOST');
  6. $REDIS_AUTH= \PhalApi\DI()->config->get('app.REDIS_AUTH');
  7. $REDIS_PORT= \PhalApi\DI()->config->get('app.REDIS_PORT');
  8. $redis = new \Redis();
  9. $redis -> pconnect($REDIS_HOST,$REDIS_PORT);
  10. $redis -> auth($REDIS_AUTH);
  11. \PhalApi\DI()->redis=$redis;
  12. }
  13. /* 设置缓存 */
  14. function setcache($key,$info){
  15. $config=getConfigPri();
  16. if($config['cache_switch']!=1){
  17. return 1;
  18. }
  19. \PhalApi\DI()->redis->set($key,json_encode($info));
  20. \PhalApi\DI()->redis->expire($key, $config['cache_time']);
  21. return 1;
  22. }
  23. /* 设置缓存 可自定义时间*/
  24. function setcaches($key,$info,$time=0){
  25. \PhalApi\DI()->redis->set($key,json_encode($info));
  26. if($time>0){
  27. \PhalApi\DI()->redis->expire($key, $time);
  28. }
  29. return 1;
  30. }
  31. /* 获取缓存 */
  32. function getcache($key){
  33. $config=getConfigPri();
  34. if($config['cache_switch']!=1){
  35. $isexist=false;
  36. }else{
  37. $isexist=\PhalApi\DI()->redis->Get($key);
  38. }
  39. return json_decode($isexist,true);
  40. }
  41. /* 获取缓存 不判断后台设置 */
  42. function getcaches($key){
  43. $isexist=\PhalApi\DI()->redis->Get($key);
  44. return json_decode($isexist,true);
  45. }
  46. /* 删除缓存 */
  47. function delcache($key){
  48. $isexist=\PhalApi\DI()->redis->del($key);
  49. return 1;
  50. }
  51. /* 去除NULL 判断空处理 主要针对字符串类型*/
  52. function checkNull($checkstr){
  53. $checkstr=urldecode($checkstr);
  54. $checkstr=htmlspecialchars($checkstr);
  55. $checkstr=trim($checkstr);
  56. //$checkstr=filterEmoji($checkstr);
  57. if( strstr($checkstr,'null') || (!$checkstr && $checkstr!=0 ) ){
  58. $str='';
  59. }else{
  60. $str=$checkstr;
  61. }
  62. return $str;
  63. }
  64. /* 去除emoji表情 */
  65. function filterEmoji($str){
  66. $str = preg_replace_callback(
  67. '/./u',
  68. function (array $match) {
  69. return strlen($match[0]) >= 4 ? '' : $match[0];
  70. },
  71. $str);
  72. return $str;
  73. }
  74. /* 检验手机号 */
  75. function checkMobile($mobile){
  76. $ismobile = preg_match("/^1[3|4|5|6|7|8|9]\d{9}$/",$mobile);
  77. if($ismobile){
  78. return 1;
  79. }else{
  80. return 0;
  81. }
  82. }
  83. /* 随机数 */
  84. function random($length = 6 , $numeric = 0) {
  85. PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  86. if($numeric) {
  87. $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
  88. } else {
  89. $hash = '';
  90. $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
  91. $max = strlen($chars) - 1;
  92. for($i = 0; $i < $length; $i++) {
  93. $hash .= $chars[mt_rand(0, $max)];
  94. }
  95. }
  96. return $hash;
  97. }
  98. /* 发送验证码 - 互译无线*/
  99. function sendCode_huyi($mobile,$code){
  100. $rs=array();
  101. $config = getConfigPri();
  102. if(!$config['sendcode_switch']){
  103. $rs['code']=667;
  104. $rs['msg']='123456';
  105. return $rs;
  106. }
  107. /* 互亿无线 */
  108. $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
  109. $post_data = "account=".$config['ihuyi_account']."&password=".$config['ihuyi_ps']."&mobile=".$mobile."&content=".rawurlencode("您的验证码是:".$code."。请不要把验证码泄露给其他人。");
  110. //密码可以使用明文密码或使用32位MD5加密
  111. $gets = xml_to_array(Post($post_data, $target));
  112. if($gets['SubmitResult']['code']==2){
  113. $rs['code']=0;
  114. }else{
  115. $rs['code']=1002;
  116. //$rs['msg']=$gets['SubmitResult']['msg'];
  117. $rs['msg']=\PhalApi\T('获取失败');
  118. }
  119. return $rs;
  120. }
  121. function Post($curlPost,$url){
  122. $curl = curl_init();
  123. curl_setopt($curl, CURLOPT_URL, $url);
  124. curl_setopt($curl, CURLOPT_HEADER, false);
  125. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  126. curl_setopt($curl, CURLOPT_NOBODY, true);
  127. curl_setopt($curl, CURLOPT_POST, true);
  128. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  129. $return_str = curl_exec($curl);
  130. curl_close($curl);
  131. return $return_str;
  132. }
  133. function xml_to_array($xml){
  134. $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
  135. if(preg_match_all($reg, $xml, $matches)){
  136. $count = count($matches[0]);
  137. for($i = 0; $i < $count; $i++){
  138. $subxml= $matches[2][$i];
  139. $key = $matches[1][$i];
  140. if(preg_match( $reg, $subxml )){
  141. $arr[$key] = xml_to_array( $subxml );
  142. }else{
  143. $arr[$key] = $subxml;
  144. }
  145. }
  146. }
  147. return $arr;
  148. }
  149. /* 发送验证码 -- 阿里云 */
  150. function sendCode($country_code,$mobile,$code){
  151. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  152. $config = getConfigPri();
  153. if(!$config['sendcode_switch']){
  154. $rs['code']=667;
  155. $rs['msg']='123456';
  156. return $rs;
  157. }
  158. $code_switch=$config['code_switch'];
  159. if($code_switch=='1'){//阿里云
  160. $res=sendCodeByAli($country_code,$mobile,$code);
  161. }else if($code_switch=='2'){
  162. $res=sendCodeByRonglian($mobile,$code);//容联云
  163. }else if($code_switch=='3'){ //腾讯云
  164. $res=sendCodeByTencentSms($country_code,$mobile,$code);//腾讯云
  165. }
  166. return $res;
  167. }
  168. function sendCodeByRonglian($mobile,$code){
  169. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  170. $config = getConfigPri();
  171. require_once API_ROOT.'/../sdk/ronglianyun/CCPRestSDK.php';
  172. //主帐号
  173. $accountSid= $config['ccp_sid'];
  174. //主帐号Token
  175. $accountToken= $config['ccp_token'];
  176. //应用Id
  177. $appId=$config['ccp_appid'];
  178. //请求地址,格式如下,不需要写https://
  179. $serverIP='app.cloopen.com';
  180. //请求端口
  181. $serverPort='8883';
  182. //REST版本号
  183. $softVersion='2013-12-26';
  184. $tempId=$config['ccp_tempid'];
  185. //file_put_contents(API_ROOT.'/../data/sendCode_rly_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 post_data: accountSid:'.$accountSid.";accountToken:{$accountToken};appId:{$appId};tempId:{$tempId}\r\n",FILE_APPEND);
  186. $rest = new \REST($serverIP,$serverPort,$softVersion);
  187. $rest->setAccount($accountSid,$accountToken);
  188. $rest->setAppId($appId);
  189. $datas=[];
  190. $datas[]=$code;
  191. $result = $rest->sendTemplateSMS($mobile,$datas,$tempId);
  192. //file_put_contents(API_ROOT.'/../data/sendCode_rly_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 result:'.json_encode($result)."\r\n",FILE_APPEND);
  193. if($result == NULL ) {
  194. $rs['code']=1002;
  195. $rs['msg']=\PhalApi\T('获取失败');
  196. return $rs;
  197. }
  198. if($result->statusCode!='000000') {
  199. $rs['code']=1002;
  200. $rs['msg']=\PhalApi\T('获取失败');
  201. return $rs;
  202. }
  203. return $rs;
  204. }
  205. //阿里云短信
  206. function sendCodeByAli($country_code,$mobile,$code){
  207. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  208. $configpri = getConfigPri();
  209. //判断是否是国外
  210. $aly_sendcode_type=$configpri['aly_sendcode_type'];
  211. if($aly_sendcode_type==1 && $country_code!=86){ //国内
  212. $rs['code']=1002;
  213. $rs['msg']=\PhalApi\T('平台短信仅支持中国大陆地区');
  214. return $rs;
  215. }
  216. if($aly_sendcode_type==2 && $country_code==86){
  217. $rs['code']=1002;
  218. $rs['msg']=\PhalApi\T('平台短信仅支持国际/港澳台地区');
  219. return $rs;
  220. }
  221. require_once API_ROOT.'/../sdk/aliyunsms/AliSmsApi.php';
  222. $config_dl = array(
  223. 'accessKeyId' => $configpri['aly_keyid'],
  224. 'accessKeySecret' => $configpri['aly_secret'],
  225. 'PhoneNumbers' => $mobile,
  226. 'SignName' => $configpri['aly_signName'], //国内短信签名
  227. 'TemplateCode' => $configpri['aly_templateCode'], //国内短信模板ID
  228. 'TemplateParam' => array("code"=>$code)
  229. );
  230. $config_hw = array(
  231. 'accessKeyId' => $configpri['aly_keyid'],
  232. 'accessKeySecret' => $configpri['aly_secret'],
  233. 'PhoneNumbers' => $country_code.$mobile,
  234. 'SignName' => $configpri['aly_hw_signName'], //港澳台/国外短信签名
  235. 'TemplateCode' => $configpri['aly_hw_templateCode'], //港澳台/国外短信模板ID
  236. 'TemplateParam' => array("code"=>$code)
  237. );
  238. if($aly_sendcode_type==1){ //国内
  239. $config=$config_dl;
  240. }else if($aly_sendcode_type==2){ //国际/港澳台地区
  241. $config=$config_hw;
  242. }else{
  243. if($country_code==86){
  244. $config=$config_dl;
  245. }else{
  246. $config=$config_hw;
  247. }
  248. }
  249. $go = new \AliSmsApi($config);
  250. $result = $go->send_sms();
  251. file_put_contents(API_ROOT.'/../data/log/sendCode_aly_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 result:'.json_encode($result)."\r\n",FILE_APPEND);
  252. if($result == NULL ) {
  253. $rs['code']=1002;
  254. $rs['msg']=\PhalApi\T('发送失败');
  255. return $rs;
  256. }
  257. if($result['Code']!='OK') {
  258. //TODO 添加错误处理逻辑
  259. $rs['code']=1002;
  260. $rs['msg']=\PhalApi\T('获取失败');
  261. return $rs;
  262. }
  263. return $rs;
  264. }
  265. //腾讯云短信
  266. function sendCodeByTencentSms($nationCode,$mobile,$code){
  267. require_once API_ROOT."/../sdk/tencentSms/index.php";
  268. $rs=array();
  269. $configpri = getConfigPri();
  270. $appid=$configpri['tencent_sms_appid'];
  271. $appkey=$configpri['tencent_sms_appkey'];
  272. $smsSign_dl = $configpri['tencent_sms_signName'];
  273. $smsSign_hw = $configpri['tencent_sms_hw_signName'];
  274. $templateId_dl=$configpri['tencent_sms_templateCode'];
  275. $templateId_hw=$configpri['tencent_sms_hw_templateCode'];
  276. $tencent_sendcode_type=$configpri['tencent_sendcode_type'];
  277. if($tencent_sendcode_type==1){ //中国大陆
  278. $smsSign = $smsSign_dl;
  279. $templateId = $templateId_dl;
  280. }else if($tencent_sendcode_type==2){//港澳台/国际
  281. $smsSign=$smsSign_hw;
  282. $templateId = $templateId_hw;
  283. }else{ //全球
  284. if($nationCode==86){
  285. $smsSign = $smsSign_dl;
  286. $templateId = $templateId_dl;
  287. }else{
  288. $smsSign=$smsSign_hw;
  289. $templateId = $templateId_hw;
  290. }
  291. }
  292. $sender = new \Qcloud\Sms\SmsSingleSender($appid,$appkey);
  293. $params = [$code]; //参数列表与腾讯云后台创建模板时加的参数列表保持一致
  294. $result = $sender->sendWithParam($nationCode, $mobile, $templateId, $params, $smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
  295. file_put_contents(API_ROOT.'/../data/log/sendCode_tencent_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 result:'.json_encode($result)."\r\n",FILE_APPEND);
  296. $arr=json_decode($result,TRUE);
  297. if($arr['result']==0 && $arr['errmsg']=='OK'){
  298. //setSendcode(array('type'=>'1','account'=>$mobile,'content'=>"验证码:".$code."---国家区号:".$nationCode));
  299. $rs['code']=0;
  300. }else{
  301. $rs['code']=1002;
  302. $rs['msg']=$arr['errmsg'];
  303. // $rs['msg']='验证码发送失败';
  304. }
  305. return $rs;
  306. }
  307. /* 检测文件后缀 */
  308. function checkExt($filename){
  309. $config=array("jpg","png","jpeg");
  310. $ext = pathinfo(strip_tags($filename), PATHINFO_EXTENSION);
  311. return empty($config) ? true : in_array(strtolower($ext), $config);
  312. }
  313. /* 密码检查 */
  314. function passcheck($user_pass) {
  315. $num = preg_match("/^[a-zA-Z]+$/",$user_pass);
  316. $word = preg_match("/^[0-9]+$/",$user_pass);
  317. $check = preg_match("/^[a-zA-Z0-9]{6,12}$/",$user_pass);
  318. if($num || $word ){
  319. return 2;
  320. }else if(!$check){
  321. return 0;
  322. }
  323. return 1;
  324. }
  325. /* 密码加密 */
  326. function setPass($pass){
  327. $authcode='rCt52pF2cnnKNB3Hkp';
  328. $pass="###".md5(md5($authcode.$pass));
  329. return $pass;
  330. }
  331. /* 公共配置 */
  332. function getConfigPub() {
  333. $key='getConfigPub';
  334. $config=getcaches($key);
  335. if(!$config){
  336. $config= \PhalApi\DI()->notorm->option
  337. ->select('option_value')
  338. ->where("option_name='site_info'")
  339. ->fetchOne();
  340. $config=json_decode($config['option_value'],true);
  341. setcaches($key,$config);
  342. }
  343. if(is_array($config['login_type'])){
  344. }else if($config['login_type']){
  345. $config['login_type']=preg_split('/,|,/',$config['login_type']);
  346. }else{
  347. $config['login_type']=array();
  348. }
  349. if(is_array($config['share_type'])){
  350. }else if($config['share_type']){
  351. $config['share_type']=preg_split('/,|,/',$config['share_type']);
  352. }else{
  353. $config['share_type']=array();
  354. }
  355. $config['watermark']=get_upload_path($config['watermark']);
  356. $lang=\PhalApi\DI()->lang;
  357. if($lang=='en'){
  358. $config['name_coin']=$config['name_coin_en'];
  359. $config['name_votes']=$config['name_votes_en'];
  360. $config['recommend_classname']=$config['recommend_classname_en'];
  361. $config['apk_des']=$config['apk_des_en'];
  362. $config['ipa_des']=$config['ipa_des_en'];
  363. $config['video_share_title']=$config['video_share_title_en'];
  364. $config['video_share_des']=$config['video_share_des_en'];
  365. $config['agent_share_title']=$config['agent_share_title_en'];
  366. $config['agent_share_des']=$config['agent_share_des_en'];
  367. $config['share_title']=$config['share_title_en'];
  368. $config['share_des']=$config['share_des_en'];
  369. $config['login_alert_title']=$config['login_alert_title_en'];
  370. $config['login_alert_content']=$config['login_alert_content_en'];
  371. $config['login_clause_title']=$config['login_clause_title_en'];
  372. $config['login_private_title']=$config['login_private_title_en'];
  373. $config['login_service_title']=$config['login_service_title_en'];
  374. }
  375. return $config;
  376. }
  377. /* 私密配置 */
  378. function getConfigPri() {
  379. $key='getConfigPri';
  380. $config=getcaches($key);
  381. if(!$config){
  382. $config= \PhalApi\DI()->notorm->option
  383. ->select('option_value')
  384. ->where("option_name='configpri'")
  385. ->fetchOne();
  386. $config=json_decode($config['option_value'],true);
  387. setcaches($key,$config);
  388. }
  389. $lang=\PhalApi\DI()->lang;
  390. if($lang=='en'){
  391. $config['popular_tips']=$config['popular_tips_en'];
  392. $config['shop_system_name']=$config['shop_system_name_en'];
  393. $config['shop_certificate_desc']=$config['shop_certificate_desc_en'];
  394. }
  395. return $config;
  396. }
  397. /**
  398. * 返回带协议的域名
  399. */
  400. function get_host(){
  401. $config=getConfigPub();
  402. return $config['site'];
  403. }
  404. /**
  405. * 转化数据库保存的文件路径,为可以访问的url cloudtype:云存储方式 0 本地 1 七牛云 2 腾讯云
  406. */
  407. function get_upload_path($file){
  408. if($file==''){
  409. return $file;
  410. }
  411. $configpri=getConfigPri();
  412. if(strpos($file,"http")===0){
  413. if(strpos($file,'%@%cloudtype=') !== false){
  414. //将字符串分隔
  415. $file_arr=explode('%@%cloudtype=',$file);
  416. $file_url=$file_arr[0];
  417. $cloudtype=$file_arr[1];
  418. if(!isset($cloudtype)){
  419. return html_entity_decode($file);
  420. }
  421. if($cloudtype==1){ //存储方式为七牛
  422. return html_entity_decode($file_url);
  423. }else if($cloudtype==2 && $configpri['tx_private_signature']){
  424. return setTxUrl(html_entity_decode($file_url)); //腾讯云存储为私有读写时需要调用该方法获取签名验证
  425. }else if($cloudtype==3){
  426. return html_entity_decode($file_url);
  427. }else{
  428. return html_entity_decode($file_url);
  429. }
  430. }else{
  431. return html_entity_decode($file);
  432. }
  433. }else if(strpos($file,"/")===0){
  434. $filepath= $filepath= get_host().$file;
  435. return $filepath;
  436. }else{
  437. if(strpos($file,'%@%cloudtype=') !== false){
  438. //将字符串分隔
  439. $file_arr=explode('%@%cloudtype=',$file);
  440. $file_url=$file_arr[0];
  441. $cloudtype=$file_arr[1];
  442. if($cloudtype==1){ //七牛存储
  443. $space_host=$configpri['qiniu_protocol']."://".$configpri['qiniu_domain']."/";
  444. }else if($cloudtype==2){ //腾讯云存储
  445. $space_host=$configpri['tx_domain_url'];
  446. }else if($cloudtype==3){ //亚马逊存储
  447. $space_host=$configpri['aws_hosturl'];
  448. }else{
  449. $space_host="http://";
  450. }
  451. $filepath=$space_host.$file_url;
  452. if(!isset($cloudtype)){
  453. return html_entity_decode($filepath);
  454. }
  455. if($cloudtype==2 && $configpri['tx_private_signature']){ //腾讯云存储 且 需要签名验证
  456. return setTxUrl(html_entity_decode($filepath)); //腾讯云存储为私有读写时需要调用该方法获取签名验证
  457. }else{
  458. return html_entity_decode($filepath);
  459. }
  460. }else{
  461. return html_entity_decode($file);
  462. }
  463. }
  464. }
  465. /* 判断是否关注 */
  466. function isAttention($uid,$touid) {
  467. if($touid==0){ //系统管理员直接返回1,不让用户关注系统管理员
  468. return "1";
  469. }
  470. if($uid<0||$touid<0){
  471. return "0";
  472. }
  473. $isexist=\PhalApi\DI()->notorm->user_attention
  474. ->select("*")
  475. ->where('uid=? and touid=?',$uid,$touid)
  476. ->fetchOne();
  477. if($isexist){
  478. return '1';
  479. }else{
  480. return '0';
  481. }
  482. }
  483. /* 是否黑名单 */
  484. function isBlack($uid,$touid) {
  485. $isexist=\PhalApi\DI()->notorm->user_black
  486. ->select("*")
  487. ->where('uid=? and touid=?',$uid,$touid)
  488. ->fetchOne();
  489. if($isexist){
  490. return '1';
  491. }else{
  492. return '0';
  493. }
  494. }
  495. /* 判断token */
  496. function checkToken($uid,$token) {
  497. //判断用户是否存在
  498. $is_exist=checkUserIsExist($uid);
  499. if(!$is_exist){
  500. return 700;
  501. }
  502. $userinfo=getcaches("token_".$uid);
  503. if(!$userinfo){
  504. $userinfo=\PhalApi\DI()->notorm->user_token
  505. ->select('token,expire_time')
  506. ->where('user_id = ? ', $uid)
  507. ->fetchOne();
  508. setcaches("token_".$uid,$userinfo);
  509. }
  510. if($userinfo['token']!=$token || $userinfo['expire_time']<time()){
  511. return 700;
  512. }
  513. $isBlackUser=isBlackUser($uid);
  514. if($isBlackUser==0){
  515. return 10020;//账号被禁用
  516. }
  517. return 0;
  518. }
  519. /* 用户基本信息 */
  520. function getUserInfo($uid,$tree='') {
  521. if(!is_numeric($uid)){
  522. if($uid==='dsp_admin_1'){
  523. $config=getConfigPub();
  524. $info['user_nickname']=$config['app_name'].\PhalApi\T("官方");
  525. $info['avatar']=get_upload_path('/officeMsg.png');
  526. $info['avatar_thumb']=get_upload_path('/officeMsg.png');
  527. $info['id']="dsp_admin_1";
  528. }else if($uid==='dsp_admin_2'){
  529. $info['user_nickname']=\PhalApi\T("系统通知");
  530. $info['avatar']=get_upload_path('/systemMsg.png');
  531. $info['avatar_thumb']=get_upload_path('/systemMsg.png');
  532. $info['id']="dsp_admin_2";
  533. }else if($uid==='goodsorder_admin'){
  534. $info['user_nickname']=\PhalApi\T("订单消息");
  535. $info['avatar']=get_upload_path('/goodsorderMsg.png');
  536. $info['avatar_thumb']=get_upload_path('/goodsorderMsg.png');
  537. $info['id']="goodsorder_admin";
  538. }else{
  539. $info['user_nickname']=\PhalApi\T("系统管理员");
  540. $info['avatar']=get_upload_path('/default.png');
  541. $info['avatar_thumb']=get_upload_path('/default_thumb.png');
  542. $info['id']="0";
  543. }
  544. $info['coin']="0";
  545. $info['sex']="1";
  546. $info['signature']='';
  547. $info['province']='';
  548. $info['city']=\PhalApi\T('城市未填写');
  549. $info['birthday']='';
  550. $info['praise']='0';
  551. $info['fans']='0';
  552. $info['follows']='0';
  553. $info['workVideos']='0'; //作品数
  554. $info['likeVideos']='0'; //喜欢别人的视频数
  555. $info['age']=\PhalApi\T("年龄未填写");
  556. $info['vipinfo']=array('isvip'=>'0','vip_endtime'=>'');
  557. $info['issuper']="0";
  558. $info['votestotal']="0";
  559. $info['consumption']="0";
  560. $info['hometown']="";
  561. $info['user_status']="1";
  562. $info['isrecommend']="0";
  563. $info['recommend_time']="0";
  564. $info['is_firstlogin']="0";
  565. $info['bg_img']=$info['avatar'];
  566. }else{
  567. $info=getCache("userinfo_".$uid);
  568. $info=false;
  569. if(!$info){
  570. $info=\PhalApi\DI()->notorm->user
  571. ->select('id,user_nickname,avatar,avatar_thumb,sex,signature,province,city,area,birthday,age,issuper,coin,votestotal,consumption,user_status,isrecommend,recommend_time,is_firstlogin,bg_img')
  572. ->where('id=? and user_type="2"',$uid)
  573. ->fetchOne();
  574. if($info){
  575. if($info['age']<0){
  576. $info['age']=\PhalApi\T("年龄未填写");
  577. }else{
  578. $info['age'].=\PhalApi\T("岁");
  579. }
  580. if($info['city']==""){
  581. $info['city']=\PhalApi\T('城市未填写');
  582. }
  583. if($info['user_status']==3){ //账号已注销
  584. $info['praise']='0';
  585. $info['fans']='0';
  586. $info['follows']='0';
  587. $info['workVideos']='0';
  588. $info['likeVideos']='0';
  589. $info['vipinfo']=array('isvip'=>'0','vip_endtime'=>'');
  590. }else{
  591. $info['praise']=getPraises($uid);
  592. $info['fans']=getFans($uid);
  593. $info['follows']=getFollows($uid);
  594. $info['workVideos']=getWorks($uid);
  595. $info['likeVideos']=getLikes($uid);
  596. $info['collectionVideos']=getCollections($uid);
  597. $info['vipinfo']=getUserVipInfo($uid); //获取用户的vip信息
  598. }
  599. $info['avatar']=get_upload_path($info['avatar']);
  600. $info['avatar_thumb']=get_upload_path($info['avatar_thumb']);
  601. $info['hometown']=$info['province'].$info['city'].$info['area'];
  602. $info['bg_img']=get_upload_path($info['bg_img']);
  603. }else{
  604. $info['user_nickname']=\PhalApi\T('用户');
  605. $info['avatar']=get_upload_path('/default.png');
  606. $info['avatar_thumb']=get_upload_path('/default_thumb.png');
  607. $info['sex']='0';
  608. $info['signature']=\PhalApi\T('这家伙很懒,什么都没留下');
  609. $info['province']=\PhalApi\T('省份未填写');
  610. $info['city']=\PhalApi\T('城市未填写');
  611. $info['birthday']='';
  612. $info['age']=\PhalApi\T("年龄未填写");
  613. $info['praise']='0';
  614. $info['fans']='0';
  615. $info['follows']='0';
  616. $info['workVideos']='0';
  617. $info['likeVideos']='0';
  618. $info['vipinfo']=array('isvip'=>'0','vip_endtime'=>'');
  619. $info['issuper']="0";
  620. $info['coin']="0";
  621. $info['votestotal']="0";
  622. $info['consumption']="0"; //app端做格式化处理 如1.6万
  623. $info['hometown']="";
  624. $info['hometown']="1";
  625. $info['isrecommend']="0";
  626. $info['recommend_time']="0";
  627. $info['is_firstlogin']="0";
  628. $info['bg_img']=$info['avatar'];
  629. }
  630. }
  631. if($tree){
  632. $info['user_nickname']=eachReplaceSensitiveWords($tree,$info['user_nickname']); //用户昵称过滤敏感词
  633. $info['signature']=eachReplaceSensitiveWords($tree,$info['signature']); //个性签名过滤敏感词
  634. }else{
  635. $info['user_nickname']=ReplaceSensitiveWords($info['user_nickname']); //用户昵称过滤敏感词
  636. $info['signature']=ReplaceSensitiveWords($info['signature']); //个性签名过滤敏感词
  637. }
  638. }
  639. return $info;
  640. }
  641. /* 统计 关注 */
  642. function getFollows($uid) {
  643. $count=\PhalApi\DI()->notorm->user_attention
  644. ->where('uid=? and touid>0 ',$uid) //关注系统管理员不显示
  645. ->count();
  646. return $count;
  647. }
  648. /* 统计 个人作品数 */
  649. function getWorks($uid) {
  650. $count=\PhalApi\DI()->notorm->user_video
  651. ->where('uid=? and isdel=0 and is_userad=0 and status=1 and is_ad=0',$uid)
  652. ->count();
  653. return $count;
  654. }
  655. /* 统计 个人喜欢其他人的作品数 */
  656. function getLikes($uid) {
  657. $count=\PhalApi\DI()->notorm->user_video_like
  658. ->where('uid=? and status=1',$uid) //status=1表示视频状态正常,未被二次拒绝或被下架
  659. ->count();
  660. return $count;
  661. }
  662. /* 统计 个人收藏其他人的作品数 */
  663. function getCollections($uid) {
  664. $count=\PhalApi\DI()->notorm->user_video_collection
  665. ->where('uid=? and status=1',$uid) //status=1表示视频状态正常,未被二次拒绝或被下架
  666. ->count();
  667. return $count;
  668. }
  669. /* 统计 粉丝 */
  670. function getFans($uid) {
  671. $count=\PhalApi\DI()->notorm->user_attention
  672. ->where('touid=? ',$uid)
  673. ->count();
  674. return $count;
  675. }
  676. /**
  677. * @desc 根据两点间的经纬度计算距离
  678. * @param float $lat 纬度值
  679. * @param float $lng 经度值
  680. */
  681. function getDistance($lat1, $lng1, $lat2, $lng2){
  682. $earthRadius = 6371000; //近似地球半径 单位 米
  683. /*
  684. Convert these degrees to radians
  685. to work with the formula
  686. */
  687. $lat1 = ($lat1 * pi() ) / 180;
  688. $lng1 = ($lng1 * pi() ) / 180;
  689. $lat2 = ($lat2 * pi() ) / 180;
  690. $lng2 = ($lng2 * pi() ) / 180;
  691. $calcLongitude = $lng2 - $lng1;
  692. $calcLatitude = $lat2 - $lat1;
  693. $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2); $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  694. $calculatedDistance = $earthRadius * $stepTwo;
  695. $distance=$calculatedDistance/1000;
  696. if($distance<10){
  697. $rs=round($distance,2);
  698. }else if($distance > 1000){
  699. $rs='>1000';
  700. }else{
  701. $rs=round($distance);
  702. }
  703. return $rs.'km';
  704. }
  705. /* 判断账号是否禁用 */
  706. function isBan($uid){
  707. $status=\PhalApi\DI()->notorm->user
  708. ->select("user_status")
  709. ->where('id=?',$uid)
  710. ->fetchOne();
  711. if(!$status || $status['user_status']==0){
  712. return 0;
  713. }
  714. return 1;
  715. }
  716. /* 是否认证 */
  717. function isAuth($uid){
  718. $status=\PhalApi\DI()->notorm->user_auth
  719. ->select("status")
  720. ->where('uid=?',$uid)
  721. ->fetchOne();
  722. if($status && $status['status']==1){
  723. return 1;
  724. }
  725. return 0;
  726. }
  727. /* 过滤字符 */
  728. function filterField($field){
  729. $configpri=getConfigPri();
  730. $sensitive_field=$configpri['sensitive_field'];
  731. $sensitive=explode(",",$sensitive_field);
  732. $replace=array();
  733. $preg=array();
  734. foreach($sensitive as $k=>$v){
  735. if($v){
  736. $re='';
  737. $num=mb_strlen($v);
  738. for($i=0;$i<$num;$i++){
  739. $re.='*';
  740. }
  741. $replace[$k]=$re;
  742. $preg[$k]='/'.$v.'/';
  743. }else{
  744. unset($sensitive[$k]);
  745. }
  746. }
  747. return preg_replace($preg,$replace,$field);
  748. }
  749. /* 时间差计算 */
  750. function datetime($time){
  751. $cha=time()-$time;
  752. $iz=floor($cha/60);
  753. $hz=floor($iz/60);
  754. $dz=floor($hz/24);
  755. /* 秒 */
  756. $s=$cha%60;
  757. /* 分 */
  758. $i=floor($iz%60);
  759. /* 时 */
  760. $h=floor($hz/24);
  761. /* 天 */
  762. if($cha<60){
  763. return $cha.\PhalApi\T('秒前');
  764. }else if($iz<60){
  765. return $iz.\PhalApi\T('分钟前');
  766. }else if($hz<24){
  767. return $hz.\PhalApi\T('小时前');
  768. }else if($dz<30){
  769. return $dz.\PhalApi\T('天前');
  770. }else{
  771. return date("Y-m-d",$time);
  772. }
  773. }
  774. /* 时长格式化 */
  775. function getSeconds($time,$type=0){
  776. if(!$time){
  777. return (string)$time;
  778. }
  779. $value = array(
  780. "years" => 0,
  781. "days" => 0,
  782. "hours" => 0,
  783. "minutes" => 0,
  784. "seconds" => 0
  785. );
  786. if($time >= 31556926){
  787. $value["years"] = floor($time/31556926);
  788. $time = ($time%31556926);
  789. }
  790. if($time >= 86400){
  791. $value["days"] = floor($time/86400);
  792. $time = ($time%86400);
  793. }
  794. if($time >= 3600){
  795. $value["hours"] = floor($time/3600);
  796. $time = ($time%3600);
  797. }
  798. if($time >= 60){
  799. $value["minutes"] = floor($time/60);
  800. $time = ($time%60);
  801. }
  802. $value["seconds"] = floor($time);
  803. if($value['years']){
  804. if($type==1&&$value['years']<10){
  805. $value['years']='0'.$value['years'];
  806. }
  807. }
  808. if($value['days']){
  809. if($type==1&&$value['days']<10){
  810. $value['days']='0'.$value['days'];
  811. }
  812. }
  813. if($value['hours']){
  814. if($type==1&&$value['hours']<10){
  815. $value['hours']='0'.$value['hours'];
  816. }
  817. }
  818. if($value['minutes']){
  819. if($type==1&&$value['minutes']<10){
  820. $value['minutes']='0'.$value['minutes'];
  821. }
  822. }
  823. if($value['seconds']){
  824. if($type==1&&$value['seconds']<10){
  825. $value['seconds']='0'.$value['seconds'];
  826. }
  827. }
  828. if($value['years']){
  829. $t=$value["years"] .\PhalApi\T('年').$value["days"] .\PhalApi\T('天'). $value["hours"] .\PhalApi\T('小时'). $value["minutes"] .\PhalApi\T('分').$value["seconds"].\PhalApi\T('秒');
  830. }else if($value['days']){
  831. $t=$value["days"] .\PhalApi\T('天'). $value["hours"] .\PhalApi\T('小时'). $value["minutes"] .\PhalApi\T('分').$value["seconds"].\PhalApi\T('秒');
  832. }else if($value['hours']){
  833. $t=$value["hours"] .\PhalApi\T('小时'). $value["minutes"] .\PhalApi\T('分').$value["seconds"].\PhalApi\T('秒');
  834. }else if($value['minutes']){
  835. $t=$value["minutes"] .\PhalApi\T('分').$value["seconds"].\PhalApi\T('秒');
  836. }else if($value['seconds']){
  837. $t=$value["seconds"].\PhalApi\T('秒');
  838. }
  839. return $t;
  840. }
  841. /* 数字格式化 */
  842. function NumberFormat($num){
  843. if($num<1000){
  844. }else if($num<10000){
  845. $num=round($num/1000,2).'k';
  846. }else if($num<1000000){
  847. $num=round($num/10000,2).'w';
  848. }else if($num<100000000){
  849. $num=round($num/10000,1).'w';
  850. }else if($num<10000000000){
  851. $num=round($num/100000000,2).'y';
  852. }else{
  853. $num=round($num/100000000,1).'y';
  854. }
  855. return $num;
  856. }
  857. /* ip限定 */
  858. function ip_limit(){
  859. $configpri=getConfigPri();
  860. if($configpri['iplimit_switch']==0){
  861. return 0;
  862. }
  863. $date = date("Ymd");
  864. $ip= ip2long($_SERVER["REMOTE_ADDR"]) ;
  865. $isexist=\PhalApi\DI()->notorm->getcode_limit_ip
  866. ->select('ip,date,times')
  867. ->where(' ip=? ',$ip)
  868. ->fetchOne();
  869. if(!$isexist){
  870. $data=array(
  871. "ip" => $ip,
  872. "date" => $date,
  873. "times" => 1,
  874. );
  875. $isexist=\PhalApi\DI()->notorm->getcode_limit_ip->insert($data);
  876. return 0;
  877. }elseif($date == $isexist['date'] && $isexist['times'] >= $configpri['iplimit_times'] ){
  878. return 1;
  879. }else{
  880. if($date == $isexist['date']){
  881. $isexist=\PhalApi\DI()->notorm->getcode_limit_ip
  882. ->where(' ip=? ',$ip)
  883. ->update(array('times'=> new \NotORM_Literal("times + 1 ")));
  884. return 0;
  885. }else{
  886. $isexist=\PhalApi\DI()->notorm->getcode_limit_ip
  887. ->where(' ip=? ',$ip)
  888. ->update(array('date'=> $date ,'times'=>1));
  889. return 0;
  890. }
  891. }
  892. }
  893. /**极光推送*/
  894. function jgsend($uid,$videoid,$type){
  895. /* 极光推送 */
  896. $configpri=getConfigPri();
  897. $app_key = $configpri['jpush_key'];
  898. $master_secret = $configpri['jpush_secret'];
  899. $userinfo=getUserInfo($uid);
  900. if($app_key && $master_secret){
  901. require './JPush/autoload.php';
  902. // 初始化
  903. $client = new \JPush\Client($app_key, $master_secret,null);
  904. $anthorinfo=array(
  905. "uid"=>$userinfo['uid'],
  906. "avatar"=>$userinfo['avatar'],
  907. "avatar_thumb"=>$userinfo['avatar_thumb'],
  908. "user_nickname"=>$userinfo['user_nickname'],
  909. "title"=>$userinfo['title'],
  910. "city"=>$userinfo['city'],
  911. "stream"=>'',
  912. "pull"=>'',
  913. "thumb"=>$userinfo['thumb'],
  914. );
  915. $fansids = getFansIds($uid,$videoid,$type);
  916. $uids=array_column($fansids,'uid');
  917. $nums=count($uids);
  918. $apns_production=false;
  919. if($configpri['jpush_sandbox']){
  920. $apns_production=true;
  921. }
  922. for($i=0;$i<$nums;){
  923. $alias=array();
  924. for($n=0;$n<1000;$n++,$i++){
  925. if($uids[$i]){
  926. $alias[]=$uids[$i].'PUSH';
  927. }else{
  928. break;
  929. }
  930. }
  931. $anthorinfo['user_nickname']=ReplaceSensitiveWords($anthorinfo['user_nickname']); //用户昵称过滤敏感词
  932. try{
  933. $result = $client->push()
  934. ->setPlatform('all')
  935. ->addAlias($alias)
  936. ->setNotificationAlert('"'.$anthorinfo['user_nickname'].'"'.\PhalApi\T('发布了新的视频,快来看看吧'))
  937. ->iosNotification('"'.$anthorinfo['user_nickname'].'"'.\PhalApi\T('发布了新的视频,快来看看吧'), array(
  938. 'sound' => 'sound.caf',
  939. 'category' => 'jiguang',
  940. 'extras' => array(
  941. 'userinfo' => $anthorinfo
  942. ),
  943. ))
  944. ->androidNotification('"'.$anthorinfo['user_nickname'].'"'.\PhalApi\T('发布了新的视频,快来看看吧'), array(
  945. 'extras' => array(
  946. 'userinfo' => $anthorinfo
  947. ),
  948. ))
  949. ->options(array(
  950. 'sendno' => 100,
  951. 'time_to_live' => 0,
  952. 'apns_production' => $apns_production,
  953. ))
  954. ->send();
  955. } catch (Exception $e) {
  956. //file_put_contents('./jpush.txt',date('y-m-d h:i:s').'提交参数信息 设备名:'.json_encode($alias)."\r\n",FILE_APPEND);
  957. //file_put_contents('./jpush.txt',date('y-m-d h:i:s').'提交参数信息:'.$e."\r\n",FILE_APPEND);
  958. }
  959. }
  960. }
  961. /* 极光推送 */
  962. }
  963. //账号是否禁用
  964. function isBlackUser($uid){
  965. $userinfo=\PhalApi\DI()->notorm->user->where("id=".$uid." and user_status=0")->fetchOne();
  966. if($userinfo){
  967. return 0;//禁用
  968. }
  969. return 1;//启用
  970. }
  971. /*检测手机号是否存在*/
  972. function checkMoblieIsExist($mobile){
  973. $res=\PhalApi\DI()->notorm->user->select("id,user_nickname,user_type")->where("mobile='{$mobile}'")->fetchOne();
  974. if($res){
  975. //判断账号是否被禁用
  976. if($res['user_status']==0){
  977. return 0;
  978. }else{
  979. return 1;
  980. }
  981. }else{
  982. return 0;
  983. }
  984. }
  985. /*检测手机号是否可以发送验证码*/
  986. function checkMoblieCanCode($mobile){
  987. $res=\PhalApi\DI()->notorm->user->select("id,user_nickname,user_type,user_status")->where("mobile='{$mobile}'")->fetchOne();
  988. if($res){
  989. //判断账号是否被禁用
  990. if($res['user_status']==0){
  991. return 0;
  992. }else{
  993. return 1;
  994. }
  995. }else{
  996. return 1;
  997. }
  998. }
  999. /*获取用户的视频点赞总数*/
  1000. function getPraises($uid){
  1001. $res=\PhalApi\DI()->notorm->user_video->where("uid=?",$uid)->sum("likes");
  1002. if(!$res){
  1003. $res="0";
  1004. }
  1005. return $res;
  1006. }
  1007. /*获取音乐信息*/
  1008. function getMusicInfo($user_nickname,$musicid){
  1009. $res=\PhalApi\DI()->notorm->user_music->select("id,title,title_en,author,img_url,length,file_url,use_nums")->where("id=?",$musicid)->fetchOne();
  1010. if(!$res){
  1011. $res=array();
  1012. $res['id']='0';
  1013. $res['title']='';
  1014. $res['author']='';
  1015. $res['img_url']='';
  1016. $res['length']='00:00';
  1017. $res['file_url']='';
  1018. $res['use_nums']='0';
  1019. $res['music_format']='@'.$user_nickname.\PhalApi\T('创作的原声');
  1020. }else{
  1021. $lang=\PhalApi\DI()->lang;
  1022. if($lang=='en'){
  1023. $res['title']=$res['title_en'];
  1024. }
  1025. $res['music_format']=$res['title'].'--'.$res['anchor'];
  1026. $res['img_url']=get_upload_path($res['img_url']);
  1027. $res['file_url']=get_upload_path($res['file_url']);
  1028. }
  1029. return $res;
  1030. }
  1031. /*距离格式化*/
  1032. function distanceFormat($distance){
  1033. if($distance<1000){
  1034. return $distance.\PhalApi\T('米');
  1035. }else{
  1036. if(floor($distance/10)<10){
  1037. return number_format($distance/10,1); //保留一位小数,会四舍五入
  1038. }else{
  1039. return \PhalApi\T('>10千米');
  1040. }
  1041. }
  1042. }
  1043. /* 视频是否点赞 */
  1044. function ifLike($uid,$videoid){
  1045. $like=\PhalApi\DI()->notorm->user_video_like
  1046. ->select("id")
  1047. ->where("uid='{$uid}' and videoid='{$videoid}'")
  1048. ->fetchOne();
  1049. if($like){
  1050. return 1;
  1051. }else{
  1052. return 0;
  1053. }
  1054. }
  1055. /* 视频是否收藏 */
  1056. function isCollection($uid,$videoid){
  1057. $like=\PhalApi\DI()->notorm->user_video_collection
  1058. ->select("id")
  1059. ->where("uid='{$uid}' and videoid='{$videoid}'")
  1060. ->fetchOne();
  1061. if($like){
  1062. return 1;
  1063. }else{
  1064. return 0;
  1065. }
  1066. }
  1067. /* 腾讯COS处理 */
  1068. /*function setTxUrl($url){
  1069. if(!strstr($url,'myqcloud')){
  1070. return $url;
  1071. }
  1072. $url_a=parse_url($url);
  1073. $file=$url_a['path'];
  1074. $signkey='Shanghai0912'; //腾讯云后台设置(控制台->存储桶->域名管理->CDN鉴权配置->鉴权Key)
  1075. $now_time = time();
  1076. $sign=md5($signkey.$file.$now_time);
  1077. return $url.'?sign='.$sign.'&t='.$now_time;
  1078. }*/
  1079. /*极光IM用户名前缀(与APP端统一)*/
  1080. function userSendBefore(){
  1081. $before='';
  1082. return $before;
  1083. }
  1084. /* 标签信息 */
  1085. function getLabelInfo($labelid){
  1086. $key='LabelInfo_'.$labelid;
  1087. $info=getcaches($key);
  1088. if(!$info){
  1089. $info=\PhalApi\DI()->notorm->label
  1090. ->select("id,name,name_en,des,thumb")
  1091. ->where('id=?',$labelid)
  1092. ->fetchOne();
  1093. if($info){
  1094. setcaches($key,$info);
  1095. }
  1096. }
  1097. if($info){
  1098. $info['thumb']=get_upload_path($info['thumb']);
  1099. $lang=\PhalApi\DI()->lang;
  1100. if($lang=='en'){
  1101. $info['name']=$info['name_en'];
  1102. }
  1103. }
  1104. return $info;
  1105. }
  1106. /* 校验签名 */
  1107. function checkSign($data,$sign){
  1108. //return 1;
  1109. if($sign==''){
  1110. return 0;
  1111. }
  1112. $key=\PhalApi\DI()->config->get('app.sign_key');
  1113. $str='';
  1114. ksort($data);
  1115. foreach($data as $k=>$v){
  1116. $str.=$k.'='.$v.'&';
  1117. }
  1118. $str.=$key;
  1119. $newsign=md5($str);
  1120. if($sign==$newsign){
  1121. return 1;
  1122. }
  1123. return 0;
  1124. }
  1125. /* 视频数据处理 */
  1126. function handleVideo($uid,$v,$tree){
  1127. $userinfo=getUserInfo($v['uid'],$tree);
  1128. if(!$userinfo){
  1129. $userinfo['user_nickname']=\PhalApi\T("已删除");
  1130. }
  1131. if($v['title']){
  1132. $v['title']=eachReplaceSensitiveWords($tree,$v['title']);
  1133. }
  1134. //防止uid为0时因为找不到用户信息而出现头像昵称为null的问题
  1135. $v['user_nickname']=$userinfo['user_nickname'];
  1136. $v['avatar']=$userinfo['avatar'];
  1137. $v['userinfo']=$userinfo;
  1138. $v['datetime']=datetime($v['addtime']);
  1139. $v['addtime_format']=$v['addtime'];
  1140. $v['addtime']=date('Y-m-d H:i:s',$v['addtime']);
  1141. $v['comments']= (string)NumberFormat($v['comments']);
  1142. $v['likes'] = (string)NumberFormat($v['likes']);
  1143. $v['steps']= (string)NumberFormat($v['steps']);
  1144. $v['shares']= (string)NumberFormat($v['shares']);
  1145. $v['collections']= (string)NumberFormat($v['collections']);
  1146. $v['views'] = (string)$v['views'];
  1147. $v['islike']='0';
  1148. $v['isattent']='0';
  1149. $v['teenagers_switch']='0';
  1150. if($uid>0){
  1151. $v['islike']=(string)ifLike($uid,$v['id']);
  1152. $v['iscollection']=(string)isCollection($uid,$v['id']);
  1153. $v['teenagers_switch']=(string)checkTeenagers($uid);
  1154. }
  1155. if($uid>0 && $uid!=$v['uid']){
  1156. $v['isattent']=(string)isAttention($uid,$v['uid']);
  1157. }
  1158. $v['musicinfo']=getMusicInfo($userinfo['user_nickname'],$v['music_id']);
  1159. $v['thumb']=get_upload_path($v['thumb']);
  1160. $v['thumb_s']=get_upload_path($v['thumb_s']);
  1161. $v['href']=encryption(get_upload_path($v['href']));
  1162. $v['href_w']=encryption(get_upload_path($v['href_w']));
  1163. if($v['ad_url']){
  1164. $v['ad_url']=get_upload_path($v['ad_url']);
  1165. }
  1166. if($v['ad_endtime']<time()){
  1167. $v['ad_url']='';
  1168. }
  1169. /* 商品 */
  1170. $goodsinfo=(object)[];
  1171. $v['goodsinfo']=$goodsinfo;
  1172. $goods_type='0';
  1173. $goodsid=$v['goodsid'];
  1174. if($goodsid){
  1175. $goods_type=\PhalApi\DI()->notorm->shop_goods->where("id=?",$goodsid)->fetchOne('type');
  1176. }
  1177. $v['goods_type']=(String)$goods_type;
  1178. /* 标签 */
  1179. $label_name='';
  1180. if($v['labelid']>0){
  1181. $labelinfo=getLabelInfo($v['labelid']);
  1182. if($labelinfo){
  1183. $label_name=$labelinfo['name'];
  1184. }else{
  1185. $v['labelid']='0';
  1186. }
  1187. }
  1188. $v['label_name']=$label_name;
  1189. //获取对应用户的直播信息
  1190. $liveinfo=getLiveInfo($v['uid']);
  1191. $v['liveinfo']=$liveinfo;
  1192. if($v['coin']){
  1193. $configpri=getConfigPri();
  1194. $watch_video_type=$configpri['watch_video_type'];
  1195. if(!$watch_video_type){ //次数限制模式,将视频需消费钻石数改为0
  1196. $v['coin']='0';
  1197. }
  1198. }
  1199. if(isset($v['activeid'])){
  1200. if($v['activeid'] == 0){
  1201. $v['activity_subject'] = '';
  1202. }else{
  1203. $subject = \PhalApi\DI()->notorm->author_center_activity
  1204. ->where(['id'=>$v['activeid']])
  1205. ->fetchOne('subject');
  1206. if($subject){
  1207. $subject_arr = json_decode($subject,true);
  1208. $v['activity_subject'] = '#'.implode('#',$subject_arr);
  1209. }else{
  1210. $v['activity_subject'] = '';
  1211. }
  1212. }
  1213. }
  1214. unset($v['ad_endtime']);
  1215. unset($v['orderno']);
  1216. unset($v['isdel']);
  1217. unset($v['show_val']);
  1218. return $v;
  1219. }
  1220. function encryption($code){
  1221. $str = 'HmTPvkJ3otK5gp.COdrAi:q09Z62ash-QGn8V;FNIlbfM/D74Wj&S_E=UzYuw?1ecxXyLRB';
  1222. $strl=strlen($str);
  1223. $len = strlen($code);
  1224. $newCode = '';
  1225. for($i=0;$i<$len;$i++){
  1226. for($j=0;$j<$strl;$j++){
  1227. if($str[$j]==$code[$i]){
  1228. if(($j+1)==$strl){
  1229. $newCode.=$str[0];
  1230. }else{
  1231. $newCode.=$str[$j+1];
  1232. }
  1233. }
  1234. }
  1235. }
  1236. return $newCode;
  1237. }
  1238. function decrypt($code){
  1239. $str = 'HmTPvkJ3otK5gp.COdrAi:q09Z62ash-QGn8V;FNIlbfM/D74Wj&S_E=UzYuw?1ecxXyLRB';
  1240. $strl=strlen($str);
  1241. $len = strlen($code);
  1242. $newCode = '';
  1243. for($i=0;$i<$len;$i++){
  1244. for($j=0;$j<$strl;$j++){
  1245. if($str[$j]==$code[$i]){
  1246. if($j-1<0){
  1247. $newCode.=$str[$strl-1];
  1248. }else{
  1249. $newCode.=$str[$j-1];
  1250. }
  1251. }
  1252. }
  1253. }
  1254. return $newCode;
  1255. }
  1256. /* 腾讯云存储
  1257. * 单文件云存储
  1258. * files 单个文件上传信息(包含键值) 格式 $files['file']=$_FILES["file"]
  1259. * type 文件类型 img图片 video视频 music音乐
  1260. */
  1261. function qcloud($files='',$type='video'){
  1262. $rs=array('code'=>1000,'data'=>[],'msg'=>\PhalApi\T('上传失败'));
  1263. $configpri=getConfigPri();
  1264. /* 腾讯云 */
  1265. require_once(API_ROOT.'/../sdk/qcloud/autoload.php');
  1266. $folder = '/'.$configpri['txvideofolder'];
  1267. if($type=='img'){
  1268. $folder = '/'.$configpri['tximgfolder'];
  1269. }
  1270. $file_name = $_FILES["file"]["name"];
  1271. $src = $_FILES["file"]["tmp_name"];
  1272. if($files){
  1273. $file_name = $files["file"]["name"];
  1274. $src = $files["file"]["tmp_name"];
  1275. }
  1276. $fnarray=explode('.', $file_name);
  1277. $file_suffix = strtolower(end($fnarray)); //后缀名
  1278. $dst = $folder.'/'.date('YmdHis').rand(1,999).'.'.$file_suffix;
  1279. $cosClient = new \Qcloud\Cos\Client(array(
  1280. 'region' => $configpri['txcloud_region'], #地域,如ap-guangzhou,ap-beijing-1
  1281. 'credentials' => array(
  1282. 'secretId' => $configpri['txcloud_secret_id'],
  1283. 'secretKey' => $configpri['txcloud_secret_key'],
  1284. ),
  1285. ));
  1286. // 若初始化 Client 时未填写 appId,则 bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
  1287. $bucket = $configpri['txcloud_bucket'].'-'.$configpri['txcloud_appid'];
  1288. try {
  1289. $result = $cosClient->upload(
  1290. $bucket = $bucket,
  1291. $key = $dst,
  1292. $body = fopen($src, 'rb')
  1293. );
  1294. $url = $result['Location'];
  1295. } catch (\Exception $e) {
  1296. $rs['msg']=$e->getMessage();
  1297. return $rs;
  1298. }
  1299. $rs['code']=0;
  1300. $rs['data']['url']=$url;
  1301. return $rs;
  1302. }
  1303. //获取用户的vip信息
  1304. function getUserVipInfo($uid){
  1305. $result=array();
  1306. $configpri=getConfigPri();
  1307. $result['vip_switch']=(string)$configpri['vip_switch'];
  1308. $now=time();
  1309. $vipInfo=\PhalApi\DI()->notorm->user->where("id=?",$uid)->select("vip_endtime")->fetchOne();
  1310. if(!$vipInfo){
  1311. $result['isvip']='0';
  1312. $result['vip_endtime']='';
  1313. return $result;
  1314. }
  1315. if($vipInfo['vip_endtime']<=$now){
  1316. $result['isvip']='0';
  1317. $result['vip_endtime']='';
  1318. return $result;
  1319. }
  1320. $result['isvip']='1';
  1321. $result['vip_endtime']=date("Y.m.d",$vipInfo['vip_endtime']);
  1322. return $result;
  1323. }
  1324. //检测注册时vip限制
  1325. function checkRegIpLimit($mobileid,$ip){
  1326. $configpri=getConfigPri();
  1327. $same_device_ip_regnums=$configpri['same_device_ip_regnums'];
  1328. if(!$same_device_ip_regnums){
  1329. return 0;
  1330. }
  1331. //获取同一设备 同一ip下的总注册量
  1332. $count=\PhalApi\DI()->notorm->user->where("mobileid='{$mobileid}' and ip=?",$ip)->count();
  1333. if($count<$same_device_ip_regnums){
  1334. return 0;
  1335. }
  1336. return 1;
  1337. }
  1338. //检测视频分类
  1339. function checkVideoClass($where){
  1340. $isexist=\PhalApi\DI()->notorm->user_video_class
  1341. ->where($where)
  1342. ->where("status=1")
  1343. ->fetchOne();
  1344. if(!$isexist){
  1345. return 0;
  1346. }
  1347. return 1;
  1348. }
  1349. //过滤关键词基础方法
  1350. function trieTreeBasic(){
  1351. require_once API_ROOT.'/public/TrieTree/TrieTree.php';
  1352. //创建树
  1353. $tree = new \AbelZhou\Tree\TrieTree();
  1354. $configpri=getConfigPri();
  1355. $newKeywords=$configpri['sensitive_words'];
  1356. $newKeywords=explode(",",$newKeywords);
  1357. //向树上挂载敏感词
  1358. foreach ($newKeywords as $keyword){
  1359. $tree->append($keyword);
  1360. }
  1361. //获取整棵树
  1362. //$treeObj = $tree->getTree();
  1363. /*print_r($treeObj);
  1364. die;*/
  1365. return $tree;
  1366. }
  1367. //检测是否存在敏感词
  1368. function checkSensitiveWords($str){
  1369. if(!$str){
  1370. return 0;
  1371. }
  1372. $configpri=getConfigPri();
  1373. $newKeywords=$configpri['sensitive_words'];
  1374. if(!$newKeywords){
  1375. return 0;
  1376. }
  1377. $tree=trieTreeBasic();
  1378. $res = $tree->search($str);
  1379. if(!$res){
  1380. return 0;
  1381. }
  1382. return 1;
  1383. }
  1384. //单条信息敏感词替换
  1385. function ReplaceSensitiveWords($str){
  1386. if(!$str){
  1387. return "";
  1388. }
  1389. $configpri=getConfigPri();
  1390. $newKeywords=$configpri['sensitive_words'];
  1391. if(!$newKeywords){
  1392. return $str;
  1393. }
  1394. $tree=trieTreeBasic();
  1395. $res = $tree->search($str);
  1396. if(!$res){
  1397. return $str;
  1398. }
  1399. foreach ($res as $k => $v) {
  1400. $len=mb_strlen($v['word']);
  1401. $replace="";
  1402. for ($i=0; $i <$len ; $i++) {
  1403. $replace.="*";
  1404. }
  1405. $str=str_replace($v['word'],$replace,$str);
  1406. }
  1407. return $str;
  1408. }
  1409. //多条信息敏感词过滤【调用处需结合trieTreeBasic函数一起使用】
  1410. function eachReplaceSensitiveWords($tree,$str){
  1411. $res = $tree->search($str);
  1412. if(!$res){
  1413. return $str;
  1414. }
  1415. foreach ($res as $k => $v) {
  1416. $len=mb_strlen($v['word']);
  1417. $replace=str_repeat("*",$len);
  1418. $str=str_replace($v['word'],$replace,$str);
  1419. }
  1420. return $str;
  1421. }
  1422. //获取用户观看视频的最新时间
  1423. function getUserWatchTime($uid){
  1424. $info=\PhalApi\DI()->notorm->user_video_watchtime->where("uid=?",$uid)->fetchOne("video_watchtime");
  1425. return $info;
  1426. }
  1427. //更新用户观看视频的最新时间
  1428. function setUserWatchTime($uid){
  1429. $info=\PhalApi\DI()->notorm->user_video_watchtime->where("uid=?",$uid)->fetchOne();
  1430. $now=time();
  1431. if($info){
  1432. \PhalApi\DI()->notorm->user_video_watchtime->where("uid=?",$uid)->update(array("video_watchtime"=>$now));
  1433. }else{
  1434. \PhalApi\DI()->notorm->user_video_watchtime->insert(array("uid"=>$uid,"video_watchtime"=>$now));
  1435. }
  1436. return 1;
  1437. }
  1438. //更新用户观看视频记录(这里是记录的最新一天的观看记录,要区别用户观看历史记录功能)
  1439. function setUserWatchLists($uid,$videoid,$isfrist){
  1440. $info=\PhalApi\DI()->notorm->user_video_watchlists->where("uid=?",$uid)->fetchOne();
  1441. if($info){
  1442. if($isfrist){ //重新记录
  1443. \PhalApi\DI()->notorm->user_video_watchlists->where("uid=?",$uid)->update(array("video_ids"=>$videoid));
  1444. }else{
  1445. $str=$info['video_ids'].",".$videoid;
  1446. \PhalApi\DI()->notorm->user_video_watchlists->where("uid=?",$uid)->update(array("video_ids"=>$str));
  1447. }
  1448. }else{
  1449. \PhalApi\DI()->notorm->user_video_watchlists->insert(array("uid"=>$uid,"video_ids"=>$videoid));
  1450. }
  1451. return 1;
  1452. }
  1453. //获取用户观看视频记录
  1454. function getUserWatchLists($uid){
  1455. $info=\PhalApi\DI()->notorm->user_video_watchlists->where("uid=?",$uid)->fetchOne("video_ids");
  1456. if($info){
  1457. $info=explode(",", $info);
  1458. }else{
  1459. $info=[];
  1460. }
  1461. return $info;
  1462. }
  1463. //获取游客的最新观看时间
  1464. function getTouristWatchTime($mobileid){
  1465. $info=\PhalApi\DI()->notorm->tourist_video_watchtime->where("mobileid=?",$mobileid)->fetchOne("video_watchtime");
  1466. return $info;
  1467. }
  1468. //更新游客的观看视频最新时间
  1469. function setTouristWatchTime($mobileid){
  1470. $info=\PhalApi\DI()->notorm->tourist_video_watchtime->where("mobileid=?",$mobileid)->fetchOne();
  1471. $now=time();
  1472. if($info){
  1473. \PhalApi\DI()->notorm->tourist_video_watchtime->where("mobileid=?",$mobileid)->update(array("video_watchtime"=>$now));
  1474. }else{
  1475. \PhalApi\DI()->notorm->tourist_video_watchtime->insert(array("mobileid"=>$mobileid,"video_watchtime"=>$now));
  1476. }
  1477. return 1;
  1478. }
  1479. //更新游客观看视频记录
  1480. function setTouristWatchLists($mobileid,$videoid,$isfrist){
  1481. $info=\PhalApi\DI()->notorm->tourist_video_watchlists->where("mobileid=?",$mobileid)->fetchOne();
  1482. if($info){
  1483. if($isfrist){ //重新记录
  1484. \PhalApi\DI()->notorm->tourist_video_watchlists->where("mobileid=?",$mobileid)->update(array("video_ids"=>$videoid));
  1485. }else{
  1486. $str=$info['video_ids'].",".$videoid;
  1487. \PhalApi\DI()->notorm->tourist_video_watchlists->where("mobileid=?",$mobileid)->update(array("video_ids"=>$str));
  1488. }
  1489. }else{
  1490. \PhalApi\DI()->notorm->tourist_video_watchlists->insert(array("mobileid"=>$mobileid,"video_ids"=>$videoid));
  1491. }
  1492. return 1;
  1493. }
  1494. //获取游客观看视频记录
  1495. function getTouristWatchLists($mobileid){
  1496. $info=\PhalApi\DI()->notorm->tourist_video_watchlists->where("mobileid=?",$mobileid)->fetchOne("video_ids");
  1497. if($info){
  1498. $info=explode(",", $info);
  1499. }else{
  1500. $info=[];
  1501. }
  1502. return $info;
  1503. }
  1504. //获取用户的付费视频记录
  1505. function getVideoPayLists($uid){
  1506. $info=\PhalApi\DI()->notorm->user_video_paylists->where("uid=?",$uid)->fetchOne("video_ids");
  1507. if($info){
  1508. $info=explode(",", $info);
  1509. }else{
  1510. $info=[];
  1511. }
  1512. return $info;
  1513. }
  1514. //更新用户的付费视频记录
  1515. function setVideoPayLists($uid,$videoid){
  1516. $info=\PhalApi\DI()->notorm->user_video_paylists->where("uid=?",$uid)->select("video_ids")->fetchOne();
  1517. if($info){
  1518. $videoids=$info['video_ids'].",".$videoid;
  1519. \PhalApi\DI()->notorm->user_video_paylists->where("uid=?",$uid)->update(array("video_ids"=>$videoids));
  1520. }else{
  1521. \PhalApi\DI()->notorm->user_video_paylists->where("uid=?",$uid)->insert(array("uid"=>$uid,"video_ids"=>$videoid));
  1522. }
  1523. }
  1524. //获取用户的钻石
  1525. function getUserCoin($uid){
  1526. $coin=\PhalApi\DI()->notorm->user->where("id=?",$uid)->fetchOne("coin");
  1527. if(!$coin){
  1528. $coin=0;
  1529. }
  1530. return $coin;
  1531. }
  1532. //更新用户的钻石数
  1533. function changeUserCoin($uid,$coin,$type=0){ //type 为0 扣费 type 为1 增加
  1534. if(!$type){
  1535. $res=\PhalApi\DI()->notorm->user->where("id=? and coin>=?",$uid,$coin)->update(array("coin"=>new \NotORM_Literal("coin-{$coin}")));
  1536. }else{
  1537. $res=\PhalApi\DI()->notorm->user->where("id=?",$uid)->update(array("coin"=>new \NotORM_Literal("coin + {$coin}")));
  1538. }
  1539. if(!$res){
  1540. return 0;
  1541. }
  1542. if(!$type){
  1543. \PhalApi\DI()->notorm->user->where("id=?",$uid)->update(array("consumption"=>new \NotORM_Literal("consumption + {$coin}")));
  1544. }
  1545. return 1;
  1546. }
  1547. //写入钻石消费记录
  1548. function setCoinRecord($data){
  1549. \PhalApi\DI()->notorm->user_coinrecord->insert($data);
  1550. }
  1551. //写入映票收入记录
  1552. function setVoteRecord($data){
  1553. \PhalApi\DI()->notorm->votes_record->insert($data);
  1554. }
  1555. //更新用户的普通映票
  1556. function changeUserVotes($uid,$votes,$type=0){ //type 为0 扣费 type 为1 增加
  1557. if(!$type){
  1558. $res=\PhalApi\DI()->notorm->user->where("id=? and votes >=?",$uid,$votes)
  1559. ->update(
  1560. array('votes'=>new \NotORM_Literal("votes-{$votes}"))
  1561. );
  1562. }else{
  1563. $res=\PhalApi\DI()->notorm->user->where("id=?",$uid)
  1564. ->update(
  1565. array(
  1566. 'votes'=>new \NotORM_Literal("votes+{$votes}"),
  1567. 'votestotal'=>new \NotORM_Literal("votestotal+{$votes}")
  1568. )
  1569. );
  1570. }
  1571. if(!$res){
  1572. return 0;
  1573. }
  1574. return 1;
  1575. }
  1576. //更新用户的红包映票
  1577. function changeUserRedVotes($uid,$votes,$type=0){ //type 为0 扣费 type 为1 增加
  1578. if(!$type){
  1579. $res=\PhalApi\DI()->notorm->user->where("id=? and red_votes >=?",$uid,$votes)
  1580. ->update(
  1581. array('red_votes'=>new \NotORM_Literal("red_votes - {$votes}"))
  1582. );
  1583. }else{
  1584. $res=\PhalApi\DI()->notorm->user->where("id=?",$uid)
  1585. ->update(
  1586. array(
  1587. 'red_votes'=>new \NotORM_Literal("red_votes+{$votes}"),
  1588. 'red_votes_total'=>new \NotORM_Literal("red_votes_total+{$votes}")
  1589. )
  1590. );
  1591. }
  1592. if(!$res){
  1593. return 0;
  1594. }
  1595. return 1;
  1596. }
  1597. //获取粉丝的极光推送id
  1598. function getFansIds($touid) {
  1599. $list=\PhalApi\DI()->notorm->user_attention
  1600. ->select("uid")
  1601. ->where('touid=?',$touid)
  1602. ->fetchAll();
  1603. return $list;
  1604. }
  1605. //检测视频是否存在
  1606. function checkVideoIsExist($videoid){
  1607. $video=\PhalApi\DI()->notorm->user_video
  1608. ->select("uid")
  1609. ->where("id=? and isdel=0 and status=1",$videoid)
  1610. ->fetchOne();
  1611. if(!$video){
  1612. return 0;
  1613. }
  1614. return 1;
  1615. }
  1616. //根据视频id获取视频详情
  1617. function getVideoInfoById($videoid,$column='*'){
  1618. $info=\PhalApi\DI()->notorm->user_video
  1619. ->select($column)
  1620. ->where("id=? and isdel=0 and status=1",$videoid)
  1621. ->fetchOne();
  1622. if(!$info){
  1623. $info=[];
  1624. }
  1625. return $info;
  1626. }
  1627. /**
  1628. * @desc 获取推拉流地址
  1629. * @param string $host 协议,如:http、rtmp
  1630. * @param string $stream 流名,如有则包含 .flv、.m3u8
  1631. * @param int $type 类型,0表示播流,1表示推流
  1632. */
  1633. function PrivateKeyA($host,$stream,$type){
  1634. $configpri=getConfigPri();
  1635. $cdn_switch=$configpri['cdn_switch'];
  1636. //$cdn_switch=2;
  1637. switch($cdn_switch){
  1638. case '1':
  1639. $url=PrivateKey_tx($host,$stream,$type);
  1640. break;
  1641. }
  1642. return $url;
  1643. }
  1644. /**
  1645. * @desc 腾讯云推拉流地址
  1646. * @param string $host 协议,如:http、rtmp
  1647. * @param string $stream 流名,如有则包含 .flv、.m3u8
  1648. * @param int $type 类型,0表示播流,1表示推流
  1649. */
  1650. function PrivateKey_txBF($host,$stream,$type){
  1651. $configpri=getConfigPri();
  1652. $bizid=$configpri['tx_bizid'];
  1653. $push_url_key=$configpri['tx_push_key'];
  1654. $push=$configpri['tx_push'];
  1655. $pull=$configpri['tx_pull'];
  1656. $stream_a=explode('.',$stream);
  1657. $streamKey = $stream_a[0];
  1658. $ext = $stream_a[1];
  1659. //$live_code = $bizid . "_" .$streamKey;
  1660. $live_code = $streamKey;
  1661. $now_time = time() + 3*60*60;
  1662. $txTime = dechex($now_time); //转为16进制
  1663. $txSecret = md5($push_url_key . $live_code . $txTime);
  1664. $safe_url = "?txSecret=" .$txSecret."&txTime=" .$txTime;
  1665. if($type==1){
  1666. //$push_url = "rtmp://" . $bizid . ".livepush2.myqcloud.com/live/" . $live_code . "?bizid=" . $bizid . "&record=flv" .$safe_url; 可录像
  1667. $url = "rtmp://{$push}/live/" . $live_code . $safe_url;
  1668. }else{
  1669. $url = "http://{$pull}/live/" . $live_code . ".flv";
  1670. }
  1671. return $url;
  1672. }
  1673. /**
  1674. * @desc 腾讯云推拉流地址
  1675. * @param string $host 协议,如:http、rtmp、trtc
  1676. * @param string $stream 流名,如有则包含 .flv、.m3u8
  1677. * @param int $type 类型,0表示播流,1表示推流
  1678. */
  1679. function PrivateKey_tx($host,$stream,$type){
  1680. $configpri=getConfigPri();
  1681. $stream_arr=explode('.',$stream);
  1682. $streamKey = isset($stream_arr[0])? $stream_arr[0] : '';
  1683. $ext = isset($stream_arr[1])? $stream_arr[1] : '';
  1684. $streamkey_arr=explode('_',$streamKey);
  1685. $uid = $streamkey_arr[0];
  1686. $showid = $streamkey_arr[1];
  1687. $now=time();
  1688. if($type==1){
  1689. //TRTC推流
  1690. $url = getTxTrtcUrl($uid,$streamKey,1);
  1691. //rtmp推流
  1692. /*$push=$configpri['tx_push'];
  1693. $push_url_key=$configpri['tx_push_key'];
  1694. $live_code=$streamKey;
  1695. $now_time = $now + 3*60*60;
  1696. $txTime = dechex($now_time);
  1697. $txSecret = md5($push_url_key . $live_code . $txTime);
  1698. $safe_url = "?txSecret=" .$txSecret."&txTime=" .$txTime;
  1699. $url = "rtmp://{$push}/live/" . $live_code . $safe_url;*/
  1700. }else{
  1701. //rtmp播流
  1702. $pull=$configpri['tx_pull'];
  1703. $play_url_key=$configpri['tx_play_key'];
  1704. $play_safe_url='';
  1705. $live_code=$streamKey;
  1706. //后台开启了播流鉴权
  1707. if($configpri['tx_play_key_switch']){
  1708. //播流鉴权时间
  1709. $play_auth_time=$now+(int)$configpri['tx_play_time'];
  1710. $txPlayTime = dechex($play_auth_time);
  1711. $txPlaySecret = md5($play_url_key . $live_code . $txPlayTime);
  1712. $play_safe_url = "?txSecret=" .$txPlaySecret."&txTime=" .$txPlayTime;
  1713. }
  1714. $url = "http://{$pull}/live/" . $live_code . ".flv".$play_safe_url;
  1715. if($ext){
  1716. $url = "http://{$pull}/live/" . $live_code . ".".$ext.$play_safe_url;
  1717. }
  1718. $configpub=getConfigPub();
  1719. if(strstr($configpub['site'],'https')){
  1720. $url=str_replace('http:','https:',$url);
  1721. }
  1722. //TRTC播流
  1723. //$url=getTxTrtcUrl($uid,$streamKey,0);
  1724. }
  1725. return $url;
  1726. }
  1727. //获取用户的映票总数
  1728. function getUserVotesTotal($uid){
  1729. $votestotal=\PhalApi\DI()->notorm->user->where("id=?",$uid)->fetchOne("votestotal");
  1730. return $votestotal;
  1731. }
  1732. // 判断直播用户权限
  1733. function isAdmin($uid,$liveuid) {
  1734. if($uid==$liveuid){
  1735. return 50; //主播
  1736. }
  1737. $isuper=isSuper($uid);
  1738. if($isuper){
  1739. return 60; //超级管理员
  1740. }
  1741. $isexist=\PhalApi\DI()->notorm->user_livemanager
  1742. ->select("*")
  1743. ->where('uid=? and liveuid=?',$uid,$liveuid)
  1744. ->fetchOne();
  1745. if($isexist){
  1746. return 40; //房间管理员
  1747. }
  1748. return 30; //普通用户
  1749. }
  1750. // 判断账号是否超管
  1751. function isSuper($uid){
  1752. $isexist=\PhalApi\DI()->notorm->user_super
  1753. ->select("*")
  1754. ->where('uid=?',$uid)
  1755. ->fetchOne();
  1756. if($isexist){
  1757. return 1;
  1758. }
  1759. return 0;
  1760. }
  1761. //判断用户是否在直播
  1762. function getLiveInfo($uid){
  1763. $info=\PhalApi\DI()->notorm->user_live->select("uid,islive,stream,pull,showid")->where("uid=?",$uid)->fetchOne();
  1764. if(!$info){
  1765. $info=array(
  1766. "islive"=>"0",
  1767. "stream"=>"",
  1768. "pull"=>"",
  1769. "showid"=>"0",
  1770. "uid"=>$uid
  1771. );
  1772. }
  1773. return $info;
  1774. }
  1775. //判断用户是否存在
  1776. function checkUserIsExist($uid){
  1777. $info=\PhalApi\DI()->notorm->user->select("user_login")->where("id=? and user_type=2",$uid)->fetchOne();
  1778. if(!$info){
  1779. return 0;
  1780. }
  1781. return 1;
  1782. }
  1783. /*腾讯云存储私有读写的签名验证start*/
  1784. function setTxUrl($url){
  1785. /*if(!strstr($url,'myqcloud')){
  1786. return $url;
  1787. }*/
  1788. $url_a=parse_url($url);
  1789. // 获取前端过来的参数
  1790. $method = isset($_GET['method']) ? $_GET['method'] : 'get';
  1791. $pathname = isset($url_a['path']) ? $url_a['path'] : '/';
  1792. if($pathname=="/"){
  1793. return $url;
  1794. }
  1795. $signinfo=getcaches($pathname);
  1796. if($signinfo){
  1797. $now=time();
  1798. if($signinfo['endtime']>$now){
  1799. return $url."?".$signinfo['sign'];
  1800. }
  1801. }
  1802. // 获取临时密钥,计算签名
  1803. $tempKeys = getTempKeys();
  1804. if ($tempKeys && isset($tempKeys['credentials'])) {
  1805. $data = array(
  1806. 'Authorization' => getAuthorization($tempKeys, $method, $pathname),
  1807. //'Authorization' => aaa($tempKeys),
  1808. 'XCosSecurityToken' => $tempKeys['credentials']['sessionToken'],
  1809. );
  1810. } else {
  1811. //$data = array('error'=> $tempKeys);
  1812. return $url;
  1813. }
  1814. $sign=$data['Authorization']."&x-cos-security-token=".$data['XCosSecurityToken'];
  1815. $signArr=array(
  1816. "endtime"=>time()-10+600,
  1817. "sign"=>$sign
  1818. );
  1819. \PhalApi\DI()->redis->set($pathname,json_encode($signArr));
  1820. $str=$url."?".$sign;
  1821. return $str;
  1822. }
  1823. // 获取临时密钥
  1824. function getTempKeys() {
  1825. $config=getCosConfig();
  1826. // 判断是否修改了 AllowPrefix
  1827. if ($config['AllowPrefix'] === '_ALLOW_DIR_/*') {
  1828. return array('error'=> '请修改 AllowPrefix 配置项,指定允许上传的路径前缀');
  1829. }
  1830. $ShortBucketName = substr($config['Bucket'],0, strripos($config['Bucket'], '-'));
  1831. $AppId = substr($config['Bucket'], 1 + strripos($config['Bucket'], '-'));
  1832. $policy = array(
  1833. 'version'=> '2.0',
  1834. 'statement'=> array(
  1835. array(
  1836. 'action'=> array(
  1837. // 简单文件操作
  1838. 'name/cos:PutObject',
  1839. 'name/cos:PostObject',
  1840. 'name/cos:AppendObject',
  1841. 'name/cos:GetObject',
  1842. 'name/cos:HeadObject',
  1843. 'name/cos:OptionsObject',
  1844. 'name/cos:PutObjectCopy',
  1845. 'name/cos:PostObjectRestore',
  1846. // 分片上传操作
  1847. 'name/cos:InitiateMultipartUpload',
  1848. 'name/cos:ListMultipartUploads',
  1849. 'name/cos:ListParts',
  1850. 'name/cos:UploadPart',
  1851. 'name/cos:CompleteMultipartUpload',
  1852. 'name/cos:AbortMultipartUpload',
  1853. ),
  1854. 'effect'=> 'allow',
  1855. 'principal'=> array('qcs'=> array('*')),
  1856. 'resource'=> array(
  1857. 'qcs::cos:' . $config['Region'] . ':uid/' . $AppId . ':prefix//' . $AppId . '/' . $ShortBucketName . '/',
  1858. 'qcs::cos:' . $config['Region'] . ':uid/' . $AppId . ':prefix//' . $AppId . '/' . $ShortBucketName . '/' . resourceUrlEncode($config['AllowPrefix'])
  1859. )
  1860. )
  1861. )
  1862. );
  1863. $policyStr = str_replace('\\/', '/', json_encode($policy));
  1864. $Action = 'GetFederationToken';
  1865. $Nonce = rand(10000, 20000);
  1866. $Timestamp = time() - 1;
  1867. $Method = 'GET';
  1868. $params = array(
  1869. 'Action'=> $Action,
  1870. 'Nonce'=> $Nonce,
  1871. 'Region'=> '',
  1872. 'SecretId'=> $config['SecretId'],
  1873. 'Timestamp'=> $Timestamp,
  1874. 'durationSeconds'=> 7200,
  1875. 'name'=> 'cos',
  1876. 'policy'=> urlencode($policyStr)
  1877. );
  1878. $params['Signature'] = urlencode(getSignature($params, $config['SecretKey'], $Method));
  1879. $url = $config['Url'] . '?' . json2str($params);
  1880. $ch = curl_init($url);
  1881. $config['Proxy'] && curl_setopt($ch, CURLOPT_PROXY, $config['Proxy']);
  1882. curl_setopt($ch, CURLOPT_HEADER, 0);
  1883. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
  1884. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
  1885. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1886. $result = curl_exec($ch);
  1887. if(curl_errno($ch)) $result = curl_error($ch);
  1888. curl_close($ch);
  1889. $result = json_decode($result, 1);
  1890. if (isset($result['data'])) $result = $result['data'];
  1891. return $result;
  1892. }
  1893. function getCosConfig(){
  1894. $configpri=getConfigPri();
  1895. // 配置参数
  1896. $config = array(
  1897. 'Url' => 'https://sts.api.qcloud.com/v2/index.php',
  1898. 'Domain' => 'sts.api.qcloud.com',
  1899. 'Proxy' => '',
  1900. 'SecretId' => $configpri['txcloud_secret_id'], // 固定密钥
  1901. 'SecretKey' => $configpri['txcloud_secret_key'], // 固定密钥
  1902. 'Bucket' => $configpri['txcloud_bucket'].'-'.$configpri['txcloud_appid'],//dsp-1257569725,
  1903. 'Region' => $configpri['txcloud_region'], //存储桶地域
  1904. 'AllowPrefix' => '*', // 这里改成允许的路径前缀,这里可以根据自己网站的用户登录态判断允许上传的目录,例子:* 或者 a/* 或者 a.jpg
  1905. );
  1906. return $config;
  1907. }
  1908. // 计算临时密钥用的签名
  1909. function resourceUrlEncode($str) {
  1910. $str = rawurlencode($str);
  1911. //特殊处理字符 !()~
  1912. $str = str_replace('%2F', '/', $str);
  1913. $str = str_replace('%2A', '*', $str);
  1914. $str = str_replace('%21', '!', $str);
  1915. $str = str_replace('%28', '(', $str);
  1916. $str = str_replace('%29', ')', $str);
  1917. $str = str_replace('%7E', '~', $str);
  1918. return $str;
  1919. }
  1920. // 计算 COS API 请求用的签名
  1921. function getAuthorization($keys, $method, $pathname){
  1922. // 获取个人 API 密钥 https://console.qcloud.com/capi
  1923. $SecretId = $keys['credentials']['tmpSecretId'];
  1924. $SecretKey = $keys['credentials']['tmpSecretKey'];
  1925. // 整理参数
  1926. $query = array();
  1927. $headers = array();
  1928. $method = strtolower($method ? $method : 'get');
  1929. $pathname = $pathname ? $pathname : '/';
  1930. substr($pathname, 0, 1) != '/' && ($pathname = '/' . $pathname);
  1931. // 签名有效起止时间
  1932. $now = time() - 1;
  1933. $expired = $now + 600; // 签名过期时刻,600 秒后
  1934. // 要用到的 Authorization 参数列表
  1935. $qSignAlgorithm = 'sha1';
  1936. $qAk = $SecretId;
  1937. $qSignTime = $now . ';' . $expired;
  1938. //$qSignTime = "1554284206;1554287806";
  1939. //$qKeyTime = $now . ';' . $expired;
  1940. $qKeyTime = $qSignTime;
  1941. $qHeaderList = strtolower(implode(';', getObjectKeys($headers)));
  1942. $qUrlParamList = strtolower(implode(';', getObjectKeys($query)));
  1943. // 签名算法说明文档:https://www.qcloud.com/document/product/436/7778
  1944. // 步骤一:计算 SignKey
  1945. $signKey = hash_hmac("sha1", $qKeyTime, $SecretKey);
  1946. // 步骤二:构成 FormatString
  1947. $formatString = implode("\n", array(strtolower($method), $pathname, obj2str($query), obj2str($headers), ''));
  1948. //header('x-test-method', $method);
  1949. // header('x-test-pathname', $pathname);
  1950. // 步骤三:计算 StringToSign
  1951. $stringToSign = implode("\n", array('sha1', $qSignTime, sha1($formatString), ''));
  1952. // 步骤四:计算 Signature
  1953. $qSignature = hash_hmac('sha1', $stringToSign, $signKey);
  1954. // 步骤五:构造 Authorization
  1955. $authorization = implode('&', array(
  1956. 'q-sign-algorithm=' . $qSignAlgorithm,
  1957. 'q-ak=' . $qAk,
  1958. 'q-sign-time=' . $qSignTime,
  1959. 'q-key-time=' . $qKeyTime,
  1960. 'q-header-list=' . $qHeaderList,
  1961. 'q-url-param-list=' . $qUrlParamList,
  1962. 'q-signature=' . $qSignature
  1963. ));
  1964. return $authorization;
  1965. }
  1966. // 工具方法
  1967. function getObjectKeys($obj){
  1968. $list = array_keys($obj);
  1969. sort($list);
  1970. return $list;
  1971. }
  1972. function obj2str($obj){
  1973. $list = array();
  1974. $keyList = getObjectKeys($obj);
  1975. $len = count($keyList);
  1976. for ($i = 0; $i < $len; $i++) {
  1977. $key = $keyList[$i];
  1978. $val = isset($obj[$key]) ? $obj[$key] : '';
  1979. $key = strtolower($key);
  1980. $list[] = rawurlencode($key) . '=' . rawurlencode($val);
  1981. }
  1982. return implode('&', $list);
  1983. }
  1984. // 计算临时密钥用的签名
  1985. function getSignature($opt, $key, $method) {
  1986. $config=getCosConfig();
  1987. $formatString = $method . $config['Domain'] . '/v2/index.php?' . json2str($opt);
  1988. $formatString = urldecode($formatString);
  1989. $sign = hash_hmac('sha1', $formatString, $key);
  1990. $sign = base64_encode(hex2bin($sign));
  1991. return $sign;
  1992. }
  1993. // obj 转 query string
  1994. function json2str($obj) {
  1995. ksort($obj);
  1996. $arr = array();
  1997. foreach ($obj as $key => $val) {
  1998. array_push($arr, $key . '=' . $val);
  1999. }
  2000. return join('&', $arr);
  2001. }
  2002. /*私有读写的签名验证end*/
  2003. //为文件拼接存储方式,方便get_upload_path做签名处理
  2004. function setCloudType($url){
  2005. if(!$url){
  2006. return $url;
  2007. }
  2008. $configpri=getConfigPri();
  2009. $cloudtype=$configpri['cloudtype'];
  2010. $url=$url."%@%cloudtype=".$cloudtype;
  2011. return $url;
  2012. }
  2013. //检测用户是否填写过邀请码
  2014. function checkAgentIsExist($uid){
  2015. $isexist=\PhalApi\DI()->notorm->agent
  2016. ->select('*')
  2017. ->where('uid=?',$uid)
  2018. ->fetchOne();
  2019. if(!$isexist){
  2020. return 0;
  2021. }
  2022. return 1;
  2023. }
  2024. //判断视频是否为广告视频
  2025. function checkVideoIsAd($videoid){
  2026. $info=\PhalApi\DI()->notorm->user_video->where("id=? and (is_ad=1 or is_userad=1)",$videoid)->fetchOne();
  2027. if(!$info){
  2028. return 0;
  2029. }
  2030. return 1;
  2031. }
  2032. //判断用户是否注销
  2033. function checkIsDestroyByLogin($user_login){
  2034. $user_status=\PhalApi\DI()->notorm->user->where("user_login=?",$user_login)->fetchOne('user_status');
  2035. if($user_status==3){
  2036. return 1;
  2037. }
  2038. return 0;
  2039. }
  2040. //判断用户是否注销
  2041. function checkIsDestroyByUid($uid){
  2042. $user_status=\PhalApi\DI()->notorm->user->where("id=?",$uid)->fetchOne('user_status');
  2043. if($user_status==3){
  2044. return 1;
  2045. }
  2046. return 0;
  2047. }
  2048. //每日任务处理
  2049. function dailyTasks($uid,$data){
  2050. $configpri=getConfigPri();
  2051. $type=$data['type']; //type 任务类型
  2052. // 当天时间
  2053. $time=strtotime(date("Y-m-d 00:00:00",time()));
  2054. $where="uid={$uid} and type={$type}";
  2055. //每日任务
  2056. $info=\PhalApi\DI()->notorm->user_daily_tasks
  2057. ->where($where)
  2058. ->select("*")
  2059. ->fetchOne();
  2060. if($info){
  2061. if($info['addtime']!=$time){
  2062. \PhalApi\DI()->notorm->user_daily_tasks
  2063. ->where($where)
  2064. ->delete();
  2065. $info=[];
  2066. }else{
  2067. if($info['state']==1 ||$info['state']==2){
  2068. return 1;
  2069. }
  2070. }
  2071. }
  2072. $save=[
  2073. 'uid'=>$uid,
  2074. 'type'=>$type,
  2075. 'addtime'=>$time,
  2076. 'uptime'=>time(),
  2077. ];
  2078. $state='0';
  2079. if($type==1){ //1观看直播
  2080. $target=$configpri['watch_live_term'];
  2081. $reward=$configpri['watch_live_coin'];
  2082. }else if($type==2){ //2观看视频
  2083. $target=$configpri['watch_video_term'];
  2084. $reward=$configpri['watch_video_coin'];
  2085. }else if($type==3){ //3直播奖励
  2086. $target=$configpri['open_live_term']*60;
  2087. $reward=$configpri['open_live_coin'];
  2088. }else if($type==4){ //4打赏奖励
  2089. $target=$configpri['award_live_term'];
  2090. $reward=$configpri['award_live_coin'];
  2091. $schedule=ceil($data['total']);
  2092. }else if($type==5){ //5分享奖励
  2093. $target=$configpri['share_live_term'];
  2094. $reward=$configpri['share_live_coin'];
  2095. $schedule=ceil($data['nums']);
  2096. }
  2097. if($target==''){
  2098. $target=0;
  2099. }
  2100. if($reward==''){
  2101. $reward=0;
  2102. }
  2103. //关于时间奖励的处理
  2104. if(in_array($type,['1','2','3'])){
  2105. $day=date("d",$data['starttime']);
  2106. $day2=date("d",$data['endtime']);
  2107. if($day!=$day2){ //判断结束时间是否超过当天, 超过则按照今天凌晨来算
  2108. $data['starttime']=$time;
  2109. }
  2110. $schedulet=0;
  2111. $time_diff=$data['endtime']-$data['starttime'];
  2112. if($time_diff>=60){ //超过一分钟才算有效时间
  2113. $schedulet=$time_diff/60; //观看时长
  2114. }
  2115. $schedule=floor($schedulet); //向下取整
  2116. }
  2117. if(!$info || $info['addtime']!=$time){ //当数据中查不到当天的数据时
  2118. $save['target']=$target;
  2119. $save['reward']=$reward;
  2120. if($schedule>=$target){
  2121. $schedule=$target;
  2122. $state='1';
  2123. }
  2124. }else{ //当有今天的数据时
  2125. $schedule=$info['schedule']+$schedule;
  2126. if($schedule>=$info['target']){
  2127. $schedule=$info['target'];
  2128. $state='1';
  2129. }
  2130. }
  2131. $save['schedule']=(int)$schedule; //进度
  2132. $save['state']=$state; //状态
  2133. if(!$info){
  2134. \PhalApi\DI()->notorm->user_daily_tasks->insert($save);
  2135. }else{
  2136. \PhalApi\DI()->notorm->user_daily_tasks->where('id=?',$info['id'])->update($save);
  2137. }
  2138. //删除用户每日任务数据
  2139. $key="seeDailyTasks_".$uid;
  2140. delcache($key);
  2141. }
  2142. //二维数bai组内进行模du糊搜索
  2143. function array_searchs($field,$data) {
  2144. $arr=$result=array();
  2145. foreach($data as $k => $v){
  2146. $lists=$v['lists'];
  2147. foreach ($lists as $key => $value) {
  2148. if(strstr($value, $field) !== false){
  2149. array_push($arr, $key);
  2150. }
  2151. }
  2152. foreach ($arr as $key => $value) {
  2153. if(array_key_exists($value,$lists)){
  2154. array_push($result, $lists[$value]);
  2155. }
  2156. }
  2157. }
  2158. return $result;
  2159. }
  2160. /* 判断商品是否收藏 */
  2161. function isGoodsCollect($uid,$goodsid) {
  2162. if($uid<0||$goodsid<0){
  2163. return "0";
  2164. }
  2165. $isexist=\PhalApi\DI()->notorm->user_goods_collect
  2166. ->select("*")
  2167. ->where('uid=? and goodsid=?',$uid,$goodsid)
  2168. ->fetchOne();
  2169. if($isexist){
  2170. return '1';
  2171. }else{
  2172. return '0';
  2173. }
  2174. }
  2175. //处理热门视频 type 处理类型 1要更新视频的热门信息 0不处理
  2176. function changeVideoPopular($videoid,$pnums,$type){
  2177. //如果该视频正在上热门,将上热门剩余的钻石退回
  2178. $popinfo=\PhalApi\DI()->notorm->popular_orders->where("videoid=? and refund_status=0 and status=1",$videoid)->select("id,uid,touid,money,nums")->fetchOne();
  2179. if($popinfo['nums']){
  2180. $coin=$pnums/$popinfo['nums']*$popinfo['money'];
  2181. $coin=floor($coin);
  2182. if($coin>=1){
  2183. $isok=changeUserCoin($popinfo['uid'],$coin,1);
  2184. if($isok){
  2185. $data=array(
  2186. 'type'=>'income',
  2187. 'action'=>'pop_refund',
  2188. 'uid'=>$popinfo['touid'], //视频发布者
  2189. 'touid'=>$popinfo['uid'], //花钱帮助上热门用户的id
  2190. 'totalcoin'=>$coin,
  2191. 'videoid'=>$videoid,
  2192. 'addtime'=>time()
  2193. );
  2194. //写入钻石消费记录
  2195. setCoinRecord($data);
  2196. if($type){
  2197. //更新视频的热门信息
  2198. $data1=array(
  2199. 'p_expire'=>0,
  2200. 'p_nums'=>0,
  2201. 'p_add'=>0
  2202. );
  2203. \PhalApi\DI()->notorm->user_video->where("id=?",$videoid)->update($data1);
  2204. }
  2205. }
  2206. }
  2207. //将上热门记录的退款状态修改一下
  2208. \PhalApi\DI()->notorm->popular_orders->where("id=?",$popinfo['id'])->update(["refund_status"=>1]);
  2209. }
  2210. }
  2211. /////////////////新增/////////////////////////
  2212. // 根据不同条件获取订单总数
  2213. function getOrderNums($where){
  2214. $count=\PhalApi\DI()->notorm->shop_order->where($where)->count();
  2215. return $count;
  2216. }
  2217. /**
  2218. * 判断是否为合法的身份证号码
  2219. * @param $mobile
  2220. * @return int
  2221. */
  2222. function isCreditNo($vStr){
  2223. return true;
  2224. $vCity = array(
  2225. '11','12','13','14','15','21','22',
  2226. '23','31','32','33','34','35','36',
  2227. '37','41','42','43','44','45','46',
  2228. '50','51','52','53','54','61','62',
  2229. '63','64','65','71','81','82','91'
  2230. );
  2231. if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)){
  2232. return false;
  2233. }
  2234. if (!in_array(substr($vStr, 0, 2), $vCity)){
  2235. return false;
  2236. }
  2237. $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
  2238. $vLength = strlen($vStr);
  2239. if($vLength == 18){
  2240. $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
  2241. }else{
  2242. $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
  2243. }
  2244. if(date('Y-m-d', strtotime($vBirthday)) != $vBirthday){
  2245. return false;
  2246. }
  2247. if ($vLength == 18) {
  2248. $vSum = 0;
  2249. for ($i = 17 ; $i >= 0 ; $i--) {
  2250. $vSubStr = substr($vStr, 17 - $i, 1);
  2251. $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
  2252. }
  2253. if($vSum % 11 != 1){
  2254. return false;
  2255. }
  2256. }
  2257. return true;
  2258. }
  2259. //单个商品信息格式化处理
  2260. function handleGoods($goodsinfo){
  2261. //获取商品的分类名称
  2262. $one_classinfo=getGoodsClassInfo($goodsinfo['one_classid']);
  2263. $two_classinfo=getGoodsClassInfo($goodsinfo['two_classid']);
  2264. $three_classinfo=getGoodsClassInfo($goodsinfo['three_classid']);
  2265. $goodsinfo['one_class_name']=isset($one_classinfo['gc_name'])?$one_classinfo['gc_name']:\PhalApi\T('分类不存在');
  2266. $goodsinfo['two_class_name']=isset($two_classinfo['gc_name'])?$two_classinfo['gc_name']:\PhalApi\T('分类不存在');
  2267. $goodsinfo['three_class_name']=isset($three_classinfo['gc_name'])?$three_classinfo['gc_name']:\PhalApi\T('分类不存在');
  2268. $goodsinfo['hits']=isset($goodsinfo['hits'])?NumberFormat($goodsinfo['hits']):'0';
  2269. $goodsinfo['sale_nums']=isset($goodsinfo['sale_nums'])?NumberFormat($goodsinfo['sale_nums']):'0';
  2270. $goodsinfo['video_url_format']=isset($goodsinfo['video_url'])?get_upload_path($goodsinfo['video_url']):'';
  2271. $goodsinfo['video_thumb_format']=isset($goodsinfo['video_thumb'])?get_upload_path($goodsinfo['video_thumb']):'';
  2272. if($goodsinfo['thumbs']){
  2273. $thumb_arr=explode(',',$goodsinfo['thumbs']);
  2274. foreach ($thumb_arr as $k => $v) {
  2275. $thumb_arr[$k]=get_upload_path($v);
  2276. }
  2277. }else{
  2278. $thumb_arr=[];
  2279. }
  2280. $goodsinfo['thumbs_format']=$thumb_arr;
  2281. if($goodsinfo['type']==1){ //外链商品
  2282. $goodsinfo['specs_format']=[];
  2283. }else{
  2284. $spec_arr=(array)json_decode($goodsinfo['specs'],true);
  2285. foreach ($spec_arr as $k => $v) {
  2286. $spec_arr[$k]['thumb']=get_upload_path($v['thumb']);
  2287. $spec_arr[$k]['price']=(string)$v['price'];
  2288. }
  2289. $goodsinfo['specs_format']=$spec_arr;
  2290. }
  2291. if($goodsinfo['pictures']){
  2292. $picture_arr=explode(',', $goodsinfo['pictures']);
  2293. foreach ($picture_arr as $k => $v) {
  2294. $picture_arr[$k]=get_upload_path($v);
  2295. }
  2296. }else{
  2297. $picture_arr=[];
  2298. }
  2299. $goodsinfo['pictures_format']=$picture_arr;
  2300. if($goodsinfo['postage']==0){
  2301. $goodsinfo['postage']='0.0';
  2302. }
  2303. if($goodsinfo['share_income']==0){
  2304. $goodsinfo['share_income']='0.0';
  2305. }
  2306. $goodsinfo['original_price']=(string)$goodsinfo['original_price'];
  2307. $goodsinfo['present_price']=(string)$goodsinfo['present_price'];
  2308. $goodsinfo['low_price']=(string)$goodsinfo['low_price'];
  2309. $goodsinfo['postage']=(string)$goodsinfo['postage'];
  2310. $goodsinfo['commission']=(string)$goodsinfo['commission'];
  2311. $goodsinfo['share_income']=(string)$goodsinfo['share_income'];
  2312. unset($goodsinfo['addtime']);
  2313. unset($goodsinfo['uptime']);
  2314. return $goodsinfo;
  2315. }
  2316. //获取商品分类信息
  2317. function getGoodsClassInfo($classid){
  2318. $info=\PhalApi\DI()->notorm->shop_goods_class->where("gc_id=?",$classid)->fetchOne();
  2319. if(!$info){
  2320. return '';
  2321. }
  2322. $lang=\PhalApi\DI()->lang;
  2323. if($lang=='en'){
  2324. $info['gc_name']=$info['gc_name_en'];
  2325. }
  2326. return $info;
  2327. }
  2328. /*判断店铺是否审核通过*/
  2329. function checkShopIsPass($uid){
  2330. $info=\PhalApi\DI()->notorm->shop_apply->select("status")->where("uid=?",$uid)->fetchOne();
  2331. if(!$info){
  2332. return '0';
  2333. }
  2334. $status=$info['status'];
  2335. if($status!=1){
  2336. return '0';
  2337. }
  2338. return '1';
  2339. }
  2340. //更改商品库存
  2341. function changeShopGoodsSpecNum($goodsid,$spec_id,$nums,$type){
  2342. $goods_info=\PhalApi\DI()->notorm->shop_goods
  2343. ->where("id=?",$goodsid)
  2344. ->fetchOne();
  2345. if(!$goods_info){
  2346. return 0;
  2347. }
  2348. $spec_arr=json_decode($goods_info['specs'],true);
  2349. $specid_arr=array_column($spec_arr, 'spec_id');
  2350. if(!in_array($spec_id, $specid_arr)){
  2351. return 0;
  2352. }
  2353. //file_put_contents("222.txt", "goodsid:".$goodsid.";spec_id:".$spec_id.";nums:".$nums.";type:".$type);
  2354. foreach ($spec_arr as $k => $v) {
  2355. if($v['spec_id']==$spec_id){
  2356. if($type==1){
  2357. $spec_num=$v['spec_num']+$nums;
  2358. }else{
  2359. $spec_num=$v['spec_num']-$nums;
  2360. }
  2361. if($spec_num<0){
  2362. $spec_num=0;
  2363. }
  2364. $spec_arr[$k]['spec_num']=(string)$spec_num;
  2365. }
  2366. }
  2367. $spec_str=json_encode($spec_arr);
  2368. //file_put_contents("333.txt", $spec_str);
  2369. \PhalApi\DI()->notorm->shop_goods->where("id=?",$goodsid)->update(array('specs'=>$spec_str));
  2370. return 1;
  2371. }
  2372. // 获取用户店铺余额
  2373. function getUserShopBalance($uid){
  2374. $info=\PhalApi\DI()->notorm->user
  2375. ->select("balance,balance_total")
  2376. ->where("id=?",$uid)
  2377. ->fetchOne();
  2378. return $info;
  2379. }
  2380. // 获取店铺商品订单详情
  2381. function getShopOrderInfo($where,$files='*'){
  2382. $info=\PhalApi\DI()->notorm->shop_order
  2383. ->select($files)
  2384. ->where($where)
  2385. ->fetchOne();
  2386. return $info;
  2387. }
  2388. //订单自动处理【用于买家/卖家获取订单列表时自动处理】
  2389. function goodsOrderAutoProcess($uid,$where){
  2390. $list=\PhalApi\DI()->notorm->shop_order
  2391. ->select("*")
  2392. ->where($where)
  2393. ->where("status !=-1") //待付款、待发货、待收货、待评价、已评价、退款
  2394. ->order("addtime desc")
  2395. ->fetchAll();
  2396. $now=time();
  2397. $effective_time=getShopEffectiveTime();
  2398. foreach ($list as $k => $v) {
  2399. if($v['status']==0){ //待付款要判断是否付款超时
  2400. $pay_end=$v['addtime']+$effective_time['shop_payment_time']*60;
  2401. if($pay_end<=$now){
  2402. $data=array(
  2403. 'status'=>-1,
  2404. 'cancel_time'=>$now
  2405. );
  2406. changeShopOrderStatus($v['uid'],$v['id'],$data); //将订单关闭
  2407. //商品规格库存回增
  2408. changeShopGoodsSpecNum($v['goodsid'],$v['spec_id'],$v['nums'],1);
  2409. //给买家发消息
  2410. $title=\PhalApi\T('你购买的“{goods_name}”订单由于超时未付款,已自动关闭',['goods_name'=>$v['goods_name']]);
  2411. $data1=array(
  2412. 'uid'=>$v['uid'],
  2413. 'orderid'=>$v['id'],
  2414. 'title'=>$title,
  2415. 'addtime'=>$now,
  2416. 'type'=>'0'
  2417. );
  2418. addShopGoodsOrderMessage($data1);
  2419. //发送腾讯IM
  2420. txMessageIM($title,$v['uid'],'goodsorder_admin');
  2421. }
  2422. }
  2423. if($v['status']==1){ //买家已付款 判断卖家发货是否超时
  2424. //如果买家没有申请退款
  2425. if($v['refund_status']==0){
  2426. $shipment_end=$v['paytime']+$effective_time['shop_shipment_time']*60*60*24;
  2427. }else{ //买家申请了退款,判断时间超时,要根据退款最终的处理时间
  2428. $shipment_end=$v['refund_endtime']+$effective_time['shop_shipment_time']*60*60*24;
  2429. }
  2430. if($shipment_end<=$now){
  2431. $data=array(
  2432. 'status'=>-1,
  2433. 'cancel_time'=>$now
  2434. );
  2435. changeShopOrderStatus($v['uid'],$v['id'],$data); //将订单关闭
  2436. //退还买家货款
  2437. setUserBalance($v['uid'],1,$v['total']);
  2438. //添加余额操作记录
  2439. $data1=array(
  2440. 'uid'=>$v['uid'],
  2441. 'touid'=>$v['shop_uid'],
  2442. 'balance'=>$v['total'],
  2443. 'type'=>1,
  2444. 'action'=>3, //卖家超时未发货,退款给买家
  2445. 'orderid'=>$v['id'],
  2446. 'addtime'=>$now
  2447. );
  2448. addBalanceRecord($data1);
  2449. //店铺逾期发货记录+1
  2450. \PhalApi\DI()->notorm->shop_apply
  2451. ->where("uid=?",$v['shop_uid'])
  2452. ->update(
  2453. array('shipment_overdue_num' => new \NotORM_Literal("shipment_overdue_num + 1"))
  2454. );
  2455. //减去商品销量
  2456. changeShopGoodsSaleNums($v['goodsid'],0,$v['nums']);
  2457. //减去店铺销量
  2458. changeShopSaleNums($v['shop_uid'],0,$v['nums']);
  2459. //给买家发消息
  2460. $title=\PhalApi\T('你购买的“{goods_name}”订单由于卖家超时未发货已自动关闭,货款已退还到余额账户中',['goods_name'=>$v['goods_name']]);
  2461. $data2=array(
  2462. 'uid'=>$v['uid'],
  2463. 'orderid'=>$v['id'],
  2464. 'title'=>$title,
  2465. 'addtime'=>$now,
  2466. 'type'=>'0'
  2467. );
  2468. addShopGoodsOrderMessage($data2);
  2469. //发送腾讯IM
  2470. txMessageIM($title,$v['uid'],'goodsorder_admin');
  2471. }
  2472. }
  2473. if($v['status']==2){ //待收货 判断自动确认收货时间是否已满足
  2474. //如果买家没有申请退款
  2475. if($v['refund_status']==0){
  2476. $receive_end=$v['shipment_time']+$effective_time['shop_receive_time']*60*60*24;
  2477. }else{
  2478. $receive_end=$v['refund_endtime']+$effective_time['shop_receive_time']*60*60*24;
  2479. }
  2480. if($receive_end<=$now){
  2481. $data=array(
  2482. 'status'=>3,
  2483. 'receive_time'=>$now
  2484. );
  2485. changeShopOrderStatus($v['uid'],$v['id'],$data); //将订单改为待评价
  2486. //给买家发消息
  2487. $title=\PhalApi\T('你购买的“{goods_name}”订单已自动确认收货',['goods_name'=>$v['goods_name']]);
  2488. $data1=array(
  2489. 'uid'=>$v['uid'],
  2490. 'orderid'=>$v['id'],
  2491. 'title'=>$title,
  2492. 'addtime'=>$now,
  2493. 'type'=>'0'
  2494. );
  2495. addShopGoodsOrderMessage($data1);
  2496. //发送腾讯IM
  2497. txMessageIM($title,$v['uid'],'goodsorder_admin');
  2498. }
  2499. }
  2500. if(($v['status']==3||$v['status']==4)&&$v['settlement_time']==0){ //待评价或已评价 且未结算
  2501. //判断是否有过退货处理 判断确认收货后是否达到后台设置的给卖家打款的时间
  2502. if($v['refund_status']==0){
  2503. $settlement_end=$v['receive_time']+$effective_time['shop_settlement_time']*60*60*24;
  2504. }else{
  2505. $settlement_end=$v['refund_endtime']+$effective_time['shop_settlement_time']*60*60*24;
  2506. }
  2507. if($settlement_end<=$now){
  2508. //判断自动结算记录是否存在
  2509. $balance_record=\PhalApi\DI()->notorm->user_balance_record->where("uid=? and touid=? and type=1 and action=2 and orderid=?",$v['shop_uid'],$v['uid'],$v['id'])->fetchOne();
  2510. if(!$balance_record){
  2511. //计算主播代售平台商品佣金
  2512. if($v['commission']>0 && $v['liveuid']){
  2513. //给主播增加余额
  2514. setUserBalance($v['liveuid'],1,$v['commission']);
  2515. //写入余额操作记录
  2516. $data3=array(
  2517. 'uid'=>$v['liveuid'], //主播ID
  2518. 'touid'=>$v['uid'], //买家用户ID
  2519. 'balance'=>$v['commission'],
  2520. 'type'=>1,
  2521. 'action'=>9, //代售平台商品佣金
  2522. 'orderid'=>$v['id'],
  2523. 'addtime'=>$now
  2524. );
  2525. addBalanceRecord($data3);
  2526. //给主播发消息
  2527. $title1=\PhalApi\T('买家购买的“{goods_name}”订单佣金{commission}已自动结算到你的账户',['goods_name'=>$v['goods_name'],'commission'=>$v['commission']]);
  2528. $data4=array(
  2529. 'uid'=>$v['liveuid'],
  2530. 'orderid'=>$v['id'],
  2531. 'title'=>$title1,
  2532. 'addtime'=>$now,
  2533. 'type'=>'1',
  2534. 'is_commission'=>'1'
  2535. );
  2536. addShopGoodsOrderMessage($data4);
  2537. //发送腾讯IM
  2538. txMessageIM($title,$v['liveuid'],'goodsorder_admin');
  2539. }
  2540. //计算分享用户的分享佣金
  2541. if($v['shareuid']>0 && $v['share_income']){
  2542. //给用户增加余额
  2543. setUserBalance($v['shareuid'],1,$v['share_income']);
  2544. //写入余额操作记录
  2545. $data5=array(
  2546. 'uid'=>$v['shareuid'], //分享用户ID
  2547. 'touid'=>$v['uid'], //买家用户ID
  2548. 'balance'=>$v['share_income'],
  2549. 'type'=>1,
  2550. 'action'=>10, //分享商品给其他用户购买后获得佣金
  2551. 'orderid'=>$v['id'],
  2552. 'addtime'=>$now
  2553. );
  2554. addBalanceRecord($data5);
  2555. }
  2556. //给卖家增加余额
  2557. $balance=$v['total']-$v['share_income'];
  2558. if($v['order_percent']>0){
  2559. $balance=$balance*(100-$v['order_percent'])/100;
  2560. $balance=round($balance,2);
  2561. }
  2562. $res1=setUserBalance($v['shop_uid'],1,$balance);
  2563. //更改订单信息
  2564. $data=array(
  2565. 'settlement_time'=>$now
  2566. );
  2567. changeShopOrderStatus($v['uid'],$v['id'],$data);
  2568. //添加余额操作记录
  2569. $data1=array(
  2570. 'uid'=>$v['shop_uid'],
  2571. 'touid'=>$v['uid'],
  2572. 'balance'=>$balance,
  2573. 'type'=>1,
  2574. 'action'=>2, //系统自动结算货款给卖家
  2575. 'orderid'=>$v['id'],
  2576. 'addtime'=>$now
  2577. );
  2578. addBalanceRecord($data1);
  2579. //主播才发送消息,平台自营不发消息
  2580. if($v['shop_uid']>1){
  2581. //给卖家发消息
  2582. $title=\PhalApi\T('买家购买的“{goods_name}”订单已自动结算到你的账户',['goods_name'=>$v['goods_name']]);
  2583. $data2=array(
  2584. 'uid'=>$v['shop_uid'],
  2585. 'orderid'=>$v['id'],
  2586. 'title'=>$title,
  2587. 'addtime'=>$now,
  2588. 'type'=>'1'
  2589. );
  2590. addShopGoodsOrderMessage($data2);
  2591. //发送腾讯IM
  2592. txMessageIM($title,$v['shop_uid'],'goodsorder_admin');
  2593. }
  2594. }
  2595. }
  2596. }
  2597. if($v['status']==5&&$v['refund_status']==0){ //退款 判断等待卖家处理的时间是否超出后台设定的时间,如果超出,自动退款
  2598. //获取退款申请信息
  2599. $where=array(
  2600. 'orderid'=>$v['id']
  2601. );
  2602. $refund_info=getShopOrderRefundInfo($where);
  2603. if($refund_info['is_platform_interpose']==0&&$refund_info['shop_result']==0){ //平台未介入且店家未处理
  2604. $refund_end=$refund_info['addtime']+$effective_time['shop_refund_time']*60*60*24;
  2605. if($refund_end<=$now){
  2606. //更改订单退款状态
  2607. $data=array(
  2608. 'refund_status'=>1,
  2609. 'refund_endtime'=>$now
  2610. );
  2611. changeShopOrderStatus($v['uid'],$v['id'],$data);
  2612. //更改订单退款记录信息
  2613. $data1=array(
  2614. 'system_process_time'=>$now,
  2615. 'status'=>1,
  2616. );
  2617. changeGoodsOrderRefund($where,$data1);
  2618. //退还买家货款
  2619. setUserBalance($v['uid'],1,$v['total']);
  2620. //添加余额操作记录
  2621. $data1=array(
  2622. 'uid'=>$v['uid'],
  2623. 'touid'=>$v['shop_uid'],
  2624. 'balance'=>$v['total'],
  2625. 'type'=>1,
  2626. 'action'=>4, //买家发起退款,卖家超时未处理,系统自动退款
  2627. 'orderid'=>$v['id'],
  2628. 'addtime'=>$now
  2629. );
  2630. addBalanceRecord($data1);
  2631. //减去商品销量
  2632. changeShopGoodsSaleNums($v['goodsid'],0,$v['nums']);
  2633. //减去店铺销量
  2634. changeShopSaleNums($v['shop_uid'],0,$v['nums']);
  2635. //商品规格库存回增
  2636. changeShopGoodsSpecNum($v['goodsid'],$v['spec_id'],$v['nums'],1);
  2637. //给买家发消息
  2638. $title=\PhalApi\T('你申请的“{goods_name}”订单退款卖家超时未处理,已自动退款到你的余额账户中',['goods_name'=>$v['goods_name']]);
  2639. $data2=array(
  2640. 'uid'=>$v['uid'],
  2641. 'orderid'=>$v['id'],
  2642. 'title'=>$title,
  2643. 'addtime'=>$now,
  2644. 'type'=>'0'
  2645. );
  2646. addShopGoodsOrderMessage($data2);
  2647. //发送腾讯IM
  2648. txMessageIM($title,$v['uid'],'goodsorder_admin');
  2649. }
  2650. }
  2651. if($refund_info['is_platform_interpose']==0&&$refund_info['shop_result']==-1){ //未申请平台介入且店家已拒绝
  2652. //超时,退款自动完成,订单自动进入退款前状态
  2653. $finish_endtime=$refund_info['shop_process_time']+$effective_time['shop_refund_finish_time']*60*60*24;
  2654. if($finish_endtime<=$now){
  2655. //更改退款订单状态
  2656. $data=array(
  2657. 'status'=>1,
  2658. 'system_process_time'=>$now
  2659. );
  2660. changeGoodsOrderRefund($where,$data);
  2661. //更改订单状态
  2662. $data1=array(
  2663. 'refund_endtime'=>$now,
  2664. 'refund_status'=>-1
  2665. );
  2666. if($v['receive_time']>0){
  2667. $data1['status']=3; //待评价
  2668. }else{
  2669. if($v['shipment_time']>0){
  2670. $data1['status']=2; //待收货
  2671. }else{
  2672. $data1['status']=1; //待发货
  2673. }
  2674. }
  2675. changeShopOrderStatus($v['uid'],$v['id'],$data1);
  2676. //给买家发消息
  2677. $title=\PhalApi\T('你购买的“{goods_name}”订单退款申请被卖家拒绝后,{shop_refund_finish_time}天内你没有进一步操作,系统自动处理结束',['goods_name'=>$v['goods_name'],'shop_refund_finish_time'=>$effective_time['shop_refund_finish_time']]);
  2678. $data2=array(
  2679. 'uid'=>$v['uid'],
  2680. 'orderid'=>$v['id'],
  2681. 'title'=>$title,
  2682. 'addtime'=>$now,
  2683. 'type'=>'0'
  2684. );
  2685. addShopGoodsOrderMessage($data2);
  2686. //发送腾讯IM
  2687. txMessageIM($title,$v['uid'],'goodsorder_admin');
  2688. }
  2689. }
  2690. }
  2691. }
  2692. }
  2693. // 修改店铺商品订单状态【 -1 已关闭 0 待付款 1 待发货 2 待收货 3 待评价 4 已评价 5 退款】
  2694. function changeShopOrderStatus($uid,$orderid,$data){
  2695. $res=\PhalApi\DI()->notorm->shop_order
  2696. ->where("id=?",$orderid)
  2697. ->update($data);
  2698. return $res;
  2699. }
  2700. //写入订单操作记录
  2701. function addShopGoodsOrderMessage($data){
  2702. $res=\PhalApi\DI()->notorm->shop_order_message->insert($data);
  2703. return $res;
  2704. }
  2705. //获取商城订单退款详情
  2706. function getShopOrderRefundInfo($where){
  2707. $info=\PhalApi\DI()->notorm->shop_order_refund
  2708. ->where($where)
  2709. ->fetchOne();
  2710. return $info;
  2711. }
  2712. //修改用户的余额 type:0 扣除余额 1 增加余额
  2713. function setUserBalance($uid,$type,$balance){
  2714. $res=0;
  2715. if($type==0){ //扣除用户余额,增加用户余额消费总额
  2716. $res=\PhalApi\DI()->notorm->user
  2717. ->where("id=? and balance>=?",$uid,$balance)
  2718. ->update(array('balance' => new \NotORM_Literal("balance - {$balance}"),'balance_consumption'=>new \NotORM_Literal("balance_consumption + {$balance}")) );
  2719. }else if($type==1){ //增加用户余额
  2720. $res=\PhalApi\DI()->notorm->user
  2721. ->where("id=?",$uid)
  2722. ->update(array('balance' => new \NotORM_Literal("balance + {$balance}"),'balance_total'=>new \NotORM_Literal("balance_total + {$balance}")) );
  2723. }
  2724. return $res;
  2725. }
  2726. //添加余额操作记录
  2727. function addBalanceRecord($data){
  2728. $res=\PhalApi\DI()->notorm->user_balance_record->insert($data);
  2729. return $res;
  2730. }
  2731. //获取店铺设置的有效时间
  2732. function getShopEffectiveTime(){
  2733. $configpri=getConfigPri();
  2734. $shop_payment_time=$configpri['shop_payment_time']; //付款有效时间(单位:分钟)
  2735. $shop_shipment_time=$configpri['shop_shipment_time']; //发货有效时间(单位:天)
  2736. $shop_receive_time=$configpri['shop_receive_time']; //自动确认收货时间(单位:天)
  2737. $shop_refund_time=$configpri['shop_refund_time']; //买家发起退款,卖家不做处理自动退款时间(单位:天)
  2738. $shop_refund_finish_time=$configpri['shop_refund_finish_time']; //卖家拒绝买家退款后,买家不做任何操作,退款自动完成时间(单位:天)
  2739. $shop_receive_refund_time=$configpri['shop_receive_refund_time']; //订单确认收货后,指定天内可以发起退货退款(单位:天)
  2740. $shop_settlement_time=$configpri['shop_settlement_time']; //订单确认收货后,货款自动打到卖家的时间(单位:天)
  2741. $data['shop_payment_time']=$shop_payment_time;
  2742. $data['shop_shipment_time']=$shop_shipment_time;
  2743. $data['shop_receive_time']=$shop_receive_time;
  2744. $data['shop_refund_time']=$shop_refund_time;
  2745. $data['shop_refund_finish_time']=$shop_refund_finish_time;
  2746. $data['shop_receive_refund_time']=$shop_receive_refund_time;
  2747. $data['shop_settlement_time']=$shop_settlement_time;
  2748. return $data;
  2749. }
  2750. //商品订单详情处理
  2751. function handleGoodsOrder($orderinfo){
  2752. $orderinfo['address_format']=$orderinfo['province'].' '.$orderinfo['city'].' '.$orderinfo['area'].' '.$orderinfo['address'];
  2753. $orderinfo['spec_thumb_format']=get_upload_path($orderinfo['spec_thumb']); //商品规格封面
  2754. $effective_time=getShopEffectiveTime();
  2755. $now=time();
  2756. switch ($orderinfo['type']) {
  2757. case '1':
  2758. $orderinfo['type_name']=\PhalApi\T('支付宝');
  2759. break;
  2760. case '2':
  2761. $orderinfo['type_name']=\PhalApi\T('微信');
  2762. break;
  2763. case '3':
  2764. $orderinfo['type_name']=\PhalApi\T('余额');
  2765. break;
  2766. case '4':
  2767. $orderinfo['type_name']=\PhalApi\T('微信小程序');
  2768. break;
  2769. case '5':
  2770. $orderinfo['type_name']='Paypal';
  2771. break;
  2772. case '6':
  2773. $orderinfo['type_name']='BraintreePaypal';
  2774. break;
  2775. }
  2776. $orderinfo['status_name']='';
  2777. $orderinfo['status_desc']='';
  2778. $orderinfo['is_refund']='0';
  2779. switch ($orderinfo['status']) {
  2780. case '-1': //已关闭
  2781. $orderinfo['status_name']=\PhalApi\T('交易关闭');
  2782. $orderinfo['status_desc']=\PhalApi\T('因支付超时,交易关闭');
  2783. break;
  2784. case '0': //待付款
  2785. $orderinfo['status_name']=\PhalApi\T('等待买家付款');
  2786. $end=$orderinfo['addtime']+$effective_time['shop_payment_time']*60;
  2787. $cha=$end-$now;
  2788. $orderinfo['status_desc']=\PhalApi\T('剩余时间 ').getSeconds($cha,1);
  2789. break;
  2790. case '1': //待发货
  2791. $orderinfo['status_name']=\PhalApi\T('支付成功,等待卖家发货');
  2792. if($orderinfo['refund_status']==0){ //只要退款未处理过
  2793. $orderinfo['is_refund']='1'; //是否可退款 0 否 1 是
  2794. }
  2795. break;
  2796. case '2': //已发货 待收货
  2797. $orderinfo['status_name']=\PhalApi\T('卖家已发货');
  2798. $end=$orderinfo['shipment_time']+$effective_time['shop_receive_time']*24*60*60;
  2799. $cha=$end-$now;
  2800. $orderinfo['status_desc']=\PhalApi\T('自动确认收货还剩').getSeconds($cha);
  2801. if($orderinfo['refund_status']==0){ //只要退款未处理过
  2802. $orderinfo['is_refund']='1'; //是否可退款 0 否 1 是
  2803. }
  2804. break;
  2805. case '3': //已收货待评价
  2806. $orderinfo['status_name']=\PhalApi\T('已签收');
  2807. $orderinfo['status_desc']=\PhalApi\T('交易成功,快去评价一下吧');
  2808. $end=$orderinfo['receive_time']+$effective_time['shop_receive_refund_time']*24*60*60;
  2809. if(($orderinfo['refund_status']==0)&&($now<$end)){ //只要退款未处理过 且在后台设定的退货时间范围内就可以发起退款
  2810. $orderinfo['is_refund']='1'; //是否可退款 0 否 1 是
  2811. }
  2812. break;
  2813. case '4': //已评价
  2814. $orderinfo['status_name']=\PhalApi\T('订单已评价');
  2815. break;
  2816. case '5': //请求退款详情单独接口
  2817. if($orderinfo['refund_status']==1){ //退款成功
  2818. $orderinfo['status_name']=\PhalApi\T('退款成功');
  2819. }else if($orderinfo['refund_status']==0){ //退款中状态
  2820. //获取退款详情
  2821. $refund_where=array(
  2822. 'orderid'=>$orderinfo['id']
  2823. );
  2824. $refund_info=getShopOrderRefundInfo($refund_where);
  2825. if($refund_info['is_platform_interpose']==0){
  2826. if($refund_info['shop_result']==0){
  2827. $orderinfo['status_name']=\PhalApi\T('等待卖家处理');
  2828. }else if($refund_info['shop_result']==-1){
  2829. $orderinfo['status_name']=\PhalApi\T('卖家已拒绝');
  2830. }
  2831. }else{
  2832. $orderinfo['status_name']=\PhalApi\T('等待平台处理');
  2833. }
  2834. }
  2835. break;
  2836. }
  2837. $orderinfo['addtime']=date("Y-m-d H:i:s",$orderinfo['addtime']); //添加时间
  2838. $orderinfo['cancel_time']=$orderinfo['cancel_time']>0?date("Y-m-d H:i:s",$orderinfo['cancel_time']):''; //取消时间
  2839. $orderinfo['paytime']=$orderinfo['paytime']>0?date("Y-m-d H:i:s",$orderinfo['paytime']):''; //支付时间
  2840. $orderinfo['shipment_time']=$orderinfo['shipment_time']>0?date("Y-m-d H:i:s",$orderinfo['shipment_time']):''; //发货时间
  2841. $orderinfo['receive_time']=$orderinfo['receive_time']>0?date("Y-m-d H:i:s",$orderinfo['receive_time']):''; //收货时间
  2842. $orderinfo['evaluate_time']=$orderinfo['evaluate_time']>0?date("Y-m-d H:i:s",$orderinfo['evaluate_time']):''; //评价时间
  2843. $orderinfo['settlement_time']=$orderinfo['settlement_time']>0?date("Y-m-d H:i:s",$orderinfo['settlement_time']):''; //结算时间
  2844. $orderinfo['refund_starttime']=$orderinfo['refund_starttime']>0?date("Y-m-d H:i:s",$orderinfo['refund_starttime']):''; //退款申请时间
  2845. $orderinfo['refund_endtime']=$orderinfo['refund_endtime']>0?date("Y-m-d H:i:s",$orderinfo['refund_endtime']):''; //退款处理结束时间
  2846. return $orderinfo;
  2847. }
  2848. //获取物流信息
  2849. function getExpressInfoByKDN($express_code,$express_number,$phone){
  2850. $configpri=getConfigPri();
  2851. $EBusinessID=isset($configpri['express_id'])?$configpri['express_id']:'';
  2852. $AppKey=isset($configpri['express_appkey'])?$configpri['express_appkey']:'';
  2853. $ReqURL='https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx'; //在途监控即时查询
  2854. $requestData=array(
  2855. 'ShipperCode'=>$express_code,
  2856. 'LogisticCode'=>$express_number
  2857. );
  2858. if($express_code=='SF' || $express_code=='KYSY'){ //顺丰要带上发件人/收件人手机号的后四位
  2859. $requestData['CustomerName']=substr($phone, -4);
  2860. }
  2861. $requestData= json_encode($requestData);
  2862. $datas = array(
  2863. 'EBusinessID' => $EBusinessID,
  2864. 'RequestType' => '8001',
  2865. 'RequestData' => urlencode($requestData) ,
  2866. 'DataType' => '2',
  2867. );
  2868. $datas['DataSign'] = encrypt_kdn($requestData, $AppKey);
  2869. $result=sendPost_KDN($ReqURL, $datas);
  2870. return json_decode($result,true);
  2871. }
  2872. /**
  2873. * 快递鸟电商Sign签名生成
  2874. * @param data 内容
  2875. * @param appkey Appkey
  2876. * @return DataSign签名
  2877. */
  2878. function encrypt_kdn($data, $appkey) {
  2879. return urlencode(base64_encode(md5($data.$appkey)));
  2880. }
  2881. /**
  2882. * post提交数据
  2883. * @param string $url 请求Url
  2884. * @param array $datas 提交的数据
  2885. * @return url响应返回的html
  2886. */
  2887. function sendPost_KDN($url, $datas) {
  2888. $temps = array();
  2889. foreach ($datas as $key => $value) {
  2890. $temps[] = sprintf('%s=%s', $key, $value);
  2891. }
  2892. $post_data = implode('&', $temps);
  2893. $url_info = parse_url($url);
  2894. if(empty($url_info['port']))
  2895. {
  2896. $url_info['port']=80;
  2897. }
  2898. $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
  2899. $httpheader.= "Host:" . $url_info['host'] . "\r\n";
  2900. $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
  2901. $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
  2902. $httpheader.= "Connection:close\r\n\r\n";
  2903. $httpheader.= $post_data;
  2904. $fd = fsockopen($url_info['host'], $url_info['port']);
  2905. fwrite($fd, $httpheader);
  2906. $gets = "";
  2907. $headerFlag = true;
  2908. while (!feof($fd)) {
  2909. if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
  2910. break;
  2911. }
  2912. }
  2913. while (!feof($fd)) {
  2914. $gets.= fread($fd, 128);
  2915. }
  2916. fclose($fd);
  2917. return $gets;
  2918. }
  2919. function is_true($val, $return_null=false){
  2920. $boolval = ( is_string($val) ? filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) : (bool) $val );
  2921. return ( $boolval===null && !$return_null ? false : $boolval );
  2922. }
  2923. //获取物流状态【即时查询版】
  2924. function getExpressStateInfo($express_code,$express_number,$express_name,$username,$phone){
  2925. $express_info=[];
  2926. $express_info_kdn=getExpressInfoByKDN($express_code,$express_number,$phone);
  2927. $express_state=$express_info_kdn['State']; //物流状态 0-暂无轨迹信息 1-已揽收 2-在途中 3-已签收4-问题件
  2928. if(!$express_state){
  2929. $express_info['state_name']=\PhalApi\T('包裹正在等待揽收');
  2930. $express_info['desc']=$express_name.' '.$express_number;
  2931. }elseif($express_state==1){
  2932. $express_info['state_name']=\PhalApi\T('包裹已揽收');
  2933. $express_info['desc']=$express_name.' '.$express_number;
  2934. }elseif($express_state==2){
  2935. $express_info['state_name']=\PhalApi\T('包裹运输中');
  2936. $express_info['desc']=$express_name.' '.$express_number;
  2937. }elseif($express_state==3){
  2938. $express_info['state_name']=\PhalApi\T('包裹已签收');
  2939. $express_info['desc']=\PhalApi\T('签收人:').$username;
  2940. }
  2941. return $express_info;
  2942. }
  2943. //更改退款详情信息
  2944. function changeGoodsOrderRefund($where,$data){
  2945. $res=\PhalApi\DI()->notorm->shop_order_refund
  2946. ->where($where)
  2947. ->update($data);
  2948. return $res;
  2949. }
  2950. //添加退款操作记录
  2951. function setGoodsOrderRefundList($data){
  2952. $res=\PhalApi\DI()->notorm->shop_order_refund_list->insert($data);
  2953. return $res;
  2954. }
  2955. //更新商品的销量 type=0 减 type=1 增
  2956. function changeShopGoodsSaleNums($goodsid,$type,$nums){
  2957. if($type==0){
  2958. $res=\PhalApi\DI()->notorm->shop_goods
  2959. ->where("id=? and sale_nums>= ?",$goodsid,$nums)
  2960. ->update(
  2961. array('sale_nums' => new \NotORM_Literal("sale_nums - {$nums}"))
  2962. );
  2963. }else{
  2964. $res=\PhalApi\DI()->notorm->shop_goods
  2965. ->where("id=?",$goodsid)
  2966. ->update(
  2967. array('sale_nums' => new \NotORM_Literal("sale_nums + {$nums}"))
  2968. );
  2969. }
  2970. return $res;
  2971. }
  2972. //更新商品的销量 type=0 减 type=1 增
  2973. function changeShopSaleNums($uid,$type,$nums){
  2974. if($type==0){
  2975. $res=\PhalApi\DI()->notorm->shop_apply
  2976. ->where("uid=? and sale_nums>= ?",$uid,$nums)
  2977. ->update(
  2978. array('sale_nums' => new \NotORM_Literal("sale_nums - {$nums}"))
  2979. );
  2980. }else{
  2981. $res=\PhalApi\DI()->notorm->shop_apply
  2982. ->where("uid=?",$uid)
  2983. ->update(
  2984. array('sale_nums' => new \NotORM_Literal("sale_nums + {$nums}"))
  2985. );
  2986. }
  2987. return $res;
  2988. }
  2989. //获取商品评价的追评信息
  2990. function getGoodsAppendComment($uid,$orderid){
  2991. $info=\PhalApi\DI()->notorm->shop_order_comments
  2992. ->where("uid=? and orderid=? and is_append=1",$uid,$orderid)
  2993. ->fetchOne();
  2994. return $info;
  2995. }
  2996. //商品评价信息处理
  2997. function handleGoodsComments($comments_info){
  2998. $comments_info['time_format']=secondsFormat($comments_info['addtime']);
  2999. $comments_info['video_thumb']=get_upload_path($comments_info['video_thumb']);
  3000. $comments_info['video_url']=get_upload_path($comments_info['video_url']);
  3001. if($comments_info['thumbs']!=''){
  3002. $thumb_arr=explode(',',$comments_info['thumbs']);
  3003. foreach ($thumb_arr as $k => $v) {
  3004. $thumb_arr[$k]=get_upload_path($v);
  3005. }
  3006. }else{
  3007. $thumb_arr=array();
  3008. }
  3009. $comments_info['thumb_format']=$thumb_arr;
  3010. $order_info=getShopOrderInfo(array('id'=>$comments_info['orderid']),'spec_name');
  3011. $comments_info['spec_name']=$order_info['spec_name']; //商品规格名称
  3012. //获取用户信息
  3013. $user_info=\PhalApi\DI()->notorm->user
  3014. ->where("id=?",$comments_info['uid'])
  3015. ->select("avatar,user_nickname")
  3016. ->fetchOne();
  3017. $comments_info['user_nickname']=$user_info['user_nickname'];
  3018. $comments_info['avatar']=get_upload_path($user_info['avatar']);
  3019. if($comments_info['is_anonym']){
  3020. $comments_info['user_nickname']=\PhalApi\T('匿名用户');
  3021. $comments_info['avatar']=get_upload_path("/anonym.png");
  3022. }
  3023. unset($comments_info['service_points']);
  3024. unset($comments_info['express_points']);
  3025. unset($comments_info['thumbs']);
  3026. unset($comments_info['is_anonym']);
  3027. return $comments_info;
  3028. }
  3029. /* 时长格式化 */
  3030. function secondsFormat($time){
  3031. $now=time();
  3032. $cha=$now-$time;
  3033. if($cha<60){
  3034. return \PhalApi\T('刚刚');
  3035. }
  3036. if($cha>=4*24*60*60){ //超过4天
  3037. $now_year=date('Y',$now);
  3038. $time_year=date('Y',$time);
  3039. if($now_year==$time_year){
  3040. return date("m-d",$time);
  3041. }else{
  3042. return date("Y-m-d",$time);
  3043. }
  3044. }else{
  3045. $iz=floor($cha/60);
  3046. $hz=floor($iz/60);
  3047. $dz=floor($hz/24);
  3048. if($dz>3){
  3049. return \PhalApi\T('3天前');
  3050. }else if($dz>2){
  3051. return \PhalApi\T('2天前');
  3052. }else if($dz>1){
  3053. return \PhalApi\T('1天前');
  3054. }
  3055. if($hz>1){
  3056. return $hz.\PhalApi\T('小时前');
  3057. }
  3058. return $iz.\PhalApi\T('分钟前');
  3059. }
  3060. }
  3061. //商城分类-一级
  3062. function getShopOneClass(){
  3063. $key="oneGoodsClass";
  3064. // $list=getcaches($key);
  3065. $list=[];
  3066. if(!$list){
  3067. $list=\PhalApi\DI()->notorm->shop_goods_class
  3068. ->select("gc_id,gc_name,gc_name_en,gc_icon")
  3069. ->where('gc_grade=1')
  3070. ->order("gc_sort")
  3071. ->fetchAll();
  3072. if($list){
  3073. setcaches($key,$list);
  3074. }
  3075. }
  3076. $lang=\PhalApi\DI()->lang;
  3077. foreach($list as $k=>$v){
  3078. $v['gc_icon']=get_upload_path($v['gc_icon']);
  3079. if($lang=='en'){
  3080. $v['gc_name']=$v['gc_name_en'];
  3081. }
  3082. $list[$k]=$v;
  3083. }
  3084. return $list;
  3085. }
  3086. // 商城分类-二级
  3087. function getShopTwoClass(){
  3088. $key="twoGoodsClass";
  3089. // $list=getcaches($key);
  3090. $list = [];
  3091. if(!$list){
  3092. $list=\PhalApi\DI()->notorm->shop_goods_class
  3093. ->select("gc_id,gc_name,gc_name_en,gc_icon")
  3094. ->where('gc_isshow=1 and gc_grade=2')
  3095. ->order("gc_sort")
  3096. ->fetchAll();
  3097. if($list){
  3098. setcaches($key,$list);
  3099. }
  3100. }
  3101. $lang=\PhalApi\DI()->lang;
  3102. foreach($list as $k=>$v){
  3103. $v['gc_icon']=get_upload_path($v['gc_icon']);
  3104. if($lang=='en'){
  3105. $v['gc_name']=$v['gc_name_en'];
  3106. }
  3107. $list[$k]=$v;
  3108. }
  3109. return $list;
  3110. }
  3111. //获取指定二级分类
  3112. function getApppointShopTwoClass($id){
  3113. $key="twoShopGoodsClass".'_'.$id;
  3114. // $list=getcaches($key);
  3115. $list=[];
  3116. if(!$list){
  3117. $list=\PhalApi\DI()->notorm->shop_goods_class
  3118. ->select("gc_id,gc_name,gc_name_en,gc_icon")
  3119. ->where('gc_isshow=1 and gc_grade=2')
  3120. ->where(['gc_parentid'=>$id])
  3121. ->order("gc_sort")
  3122. ->fetchAll();
  3123. if($list){
  3124. setcaches($key,$list);
  3125. }
  3126. }
  3127. $lang=\PhalApi\DI()->lang;
  3128. foreach($list as $k=>$v){
  3129. $v['gc_icon']=get_upload_path($v['gc_icon']);
  3130. if($lang=='en'){
  3131. $v['gc_name']=$v['gc_name_en'];
  3132. }
  3133. $list[$k]=$v;
  3134. }
  3135. return $list;
  3136. }
  3137. // 商城分类-三级
  3138. function getShopThreeClass($classid){
  3139. $key="threeGoodsClass_".$classid;
  3140. $list=[];
  3141. // $list=getcaches($key);
  3142. if(!$list){
  3143. $list=\PhalApi\DI()->notorm->shop_goods_class
  3144. ->select("gc_id,gc_name,gc_name_en")
  3145. ->where("gc_isshow=1 and gc_grade=3 and gc_parentid={$classid}")
  3146. ->order("gc_sort")
  3147. ->fetchAll();
  3148. if($list){
  3149. setcaches($key,$list);
  3150. }else{
  3151. $list=[];
  3152. }
  3153. }
  3154. $lang=\PhalApi\DI()->lang;
  3155. foreach($list as $k=>$v){
  3156. if($lang=='en'){
  3157. $v['gc_name']=$v['gc_name_en'];
  3158. }
  3159. $list[$k]=$v;
  3160. }
  3161. return $list;
  3162. }
  3163. //检测姓名
  3164. function checkUsername($username){
  3165. $preg='/^(?=.*\d.*\b)/';
  3166. $isok = preg_match($preg,$username);
  3167. if($isok){
  3168. return 1;
  3169. }else{
  3170. return 0;
  3171. }
  3172. }
  3173. //身份证检测
  3174. /*function checkCardNo($cardno){
  3175. $preg='/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/';
  3176. $isok=preg_match($preg, $cardno);
  3177. if($isok){
  3178. return 1;
  3179. }else{
  3180. return 0;
  3181. }
  3182. }*/
  3183. //获取店铺协商历史
  3184. function getShopOrderRefundList($where){
  3185. $list=\PhalApi\DI()->notorm->shop_order_refund_list
  3186. ->where($where)
  3187. ->order("addtime desc")
  3188. ->fetchAll();
  3189. return $list;
  3190. }
  3191. //代售平台商品列表格式化处理
  3192. function handlePlatformGoods($list,$platform_list,$type){
  3193. foreach ($list as $k => $v) {
  3194. $thumb_arr=explode(',',$v['thumbs']);
  3195. $list[$k]['thumb']=get_upload_path($thumb_arr[0]);
  3196. if($v['type']==1){ //外链商品
  3197. $list[$k]['price']=(string)$v['present_price'];
  3198. $list[$k]['specs']=[];
  3199. }else{
  3200. $spec_arr=json_decode($v['specs'],true);
  3201. $list[$k]['price']=(string)$spec_arr[0]['price'];
  3202. $list[$k]['specs']=$spec_arr;
  3203. }
  3204. if($platform_list){
  3205. foreach ($platform_list as $k1 => $v1) {
  3206. if($v1['goodsid']==$v['id']){
  3207. $list[$k]['issale']=$v1['issale'];
  3208. $list[$k]['live_isshow']=$v1['live_isshow'];
  3209. break;
  3210. }
  3211. }
  3212. }
  3213. unset($list[$k]['thumbs']);
  3214. unset($list[$k]['present_price']);
  3215. unset($list[$k]['specs']);
  3216. }
  3217. return $list;
  3218. }
  3219. //检测用户代售商品
  3220. function checkUserSalePlatformGoods($where){
  3221. $info=\PhalApi\DI()->notorm->seller_platform_goods
  3222. ->where($where)
  3223. ->fetchOne();
  3224. if(!$info){
  3225. return 0;
  3226. }
  3227. return 1;
  3228. }
  3229. //获取代售平台商品记录
  3230. function getOnsalePlatformInfo($where){
  3231. $info=\PhalApi\DI()->notorm->seller_platform_goods
  3232. ->where($where)
  3233. ->fetchOne();
  3234. return $info;
  3235. }
  3236. //修改代售平台商品记录的信息
  3237. function setOnsalePlatformInfo($where,$data){
  3238. $res=\PhalApi\DI()->notorm->seller_platform_goods
  3239. ->where($where)
  3240. ->update($data);
  3241. return $res;
  3242. }
  3243. //获取低延迟推流和播流地址【废弃,腾讯rtmp低延迟流改为了trtc格式,参考getTxTrtcUrl】
  3244. function getLowLatencyStreamBF($stream){
  3245. $configpri=getConfigPri();
  3246. $nowtime=time();
  3247. $cdn_switch=$configpri['cdn_switch']; //cdn_switch 1 腾讯云
  3248. if($cdn_switch==1){
  3249. $bizid = $configpri['tx_bizid'];
  3250. $push_url_key = $configpri['tx_push_key'];
  3251. $tx_acc_key = $configpri['tx_acc_key'];
  3252. $push = $configpri['tx_push'];
  3253. $pull = $configpri['tx_pull'];
  3254. $now_time2 = $nowtime + 3*60*60;
  3255. $txTime = dechex($now_time2);
  3256. $live_code = $stream ;
  3257. $txSecret = md5($push_url_key . $live_code . $txTime);
  3258. $safe_url = "?txSecret=" . $txSecret."&txTime=" .$txTime;
  3259. $push_url = "rtmp://" . $push . "/live/" . $live_code .$safe_url. "&bizid=" . $bizid ;
  3260. $txSecret2 = md5($tx_acc_key . $live_code . $txTime);
  3261. $safe_url2 = "?txSecret=" . $txSecret2."&txTime=" .$txTime;
  3262. $play_url = "rtmp://" . $pull . "/live/" .$live_code .$safe_url2. "&bizid=" . $bizid;
  3263. }else{
  3264. $push_url=PrivateKeyA('rtmp',$stream,1);
  3265. $play_url=PrivateKeyA('rtmp',$stream,0);
  3266. }
  3267. $info=array(
  3268. "pushurl" => $push_url,
  3269. "timestamp" => $nowtime,
  3270. "playurl" => $play_url
  3271. );
  3272. return $info;
  3273. }
  3274. /*获取店铺申请状态*/
  3275. function getShopApplyStatus($uid){
  3276. $info=\PhalApi\DI()->notorm->shop_apply
  3277. ->select("status")
  3278. ->where("uid=?",$uid)
  3279. ->fetchOne();
  3280. if(!$info){
  3281. return '-1';
  3282. }
  3283. return $info['status'];
  3284. }
  3285. // 获取用户的余额
  3286. function getUserBalance($uid){
  3287. $res=array(
  3288. 'balance'=>'0.00',
  3289. 'balance_total'=>'0.00'
  3290. );
  3291. $info=\PhalApi\DI()->notorm->user->where("id=?",$uid)->select("balance,balance_total")->fetchOne();
  3292. if($info){
  3293. $res['balance']=$info['balance'];
  3294. $res['balance_total']=$info['balance_total'];
  3295. }
  3296. return $res;
  3297. }
  3298. //商品列表格式化处理
  3299. function handleGoodsList($where,$p,$order="id desc",$shopclassid='',$type=4,$isrecom=0){
  3300. if($p<1){
  3301. $p=1;
  3302. }
  3303. if(!empty($shopclassid)){
  3304. $where['one_classid'] = $shopclassid;
  3305. }
  3306. if($isrecom == 0) {
  3307. $nums=50;
  3308. switch ($type) {
  3309. case 1: //普通商品
  3310. $where['is_assisting_farmers'] = 0;
  3311. $where['is_seckill'] = 0;
  3312. break;
  3313. case 2: //助农商品
  3314. $where['is_assisting_farmers'] = 1;
  3315. break;
  3316. case 3: //特价秒杀
  3317. $where['is_seckill'] = 1;
  3318. break;
  3319. default:
  3320. break;
  3321. }
  3322. }else{
  3323. $nums=3;
  3324. $where['isrecom'] = 1;
  3325. }
  3326. $start=($p-1)*$nums;
  3327. $list=\PhalApi\DI()->notorm->shop_goods
  3328. ->select("id,name,thumbs,sale_nums,specs,hits,issale,type,original_price,present_price,status,live_isshow,commission")
  3329. ->where($where)
  3330. ->order($order)
  3331. ->limit($start,$nums)
  3332. ->fetchAll();
  3333. if(!$list){
  3334. return [];
  3335. }
  3336. foreach ($list as $k => $v) {
  3337. $thumb_arr=explode(',',$v['thumbs']);
  3338. $list[$k]['thumb']=get_upload_path($thumb_arr[0]);
  3339. if($v['type']==1){ //外链商品
  3340. $list[$k]['price']=(string)$v['present_price'];
  3341. $list[$k]['specs']=[];
  3342. }else{
  3343. $spec_arr=json_decode($v['specs'],true);
  3344. $list[$k]['price']=(string)$spec_arr[0]['price'];
  3345. $list[$k]['specs']=$spec_arr;
  3346. }
  3347. unset($list[$k]['thumbs']);
  3348. unset($list[$k]['present_price']);
  3349. }
  3350. return $list;
  3351. }
  3352. //获取新闻格式化处理
  3353. function getNewsList($where,$p){
  3354. $nums = 20;
  3355. if($p<1){
  3356. $p=1;
  3357. }
  3358. $start=($p-1)*$nums;
  3359. $rs=\PhalApi\DI()->notorm->news
  3360. ->select("id,cate_id,keywords,title,image,content,content,create_time")
  3361. ->where($where)
  3362. ->order("istop DESC,id DESC")
  3363. ->limit($start,$nums)
  3364. ->fetchAll();
  3365. foreach($rs as &$v){
  3366. $cate_name=\PhalApi\DI()->notorm->news_cate
  3367. ->where(['id'=>$v['cate_id']])
  3368. ->fetchRow('name');
  3369. $v['cate_name']=$cate_name;
  3370. $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
  3371. $v['image'] = get_upload_path($v['image']);
  3372. }
  3373. return $rs;
  3374. }
  3375. function getNewsDetail($where)
  3376. {
  3377. $info = \PhalApi\DI()->notorm->news
  3378. ->select("id,cate_id,keywords,title,image,content,content,create_time")
  3379. ->where($where)
  3380. ->fetch();
  3381. $cate_name = \PhalApi\DI()->notorm->news_cate
  3382. ->where(['id' => $info['cate_id']])
  3383. ->fetchRow('name');
  3384. $info['cate_name'] = $cate_name;
  3385. $info['create_time'] = date('Y-m-d H:i:s', $info['create_time']);
  3386. $info['image'] = get_upload_path($info['image']);
  3387. return $info;
  3388. }
  3389. //获分类店铺格式化处理
  3390. function getClassidShopLists($where,$province,$name,$p){
  3391. $nums = 20;
  3392. if($p<1){
  3393. $p=1;
  3394. }
  3395. $start=($p-1)*$nums;
  3396. $rs=\PhalApi\DI()->notorm->seller_goods_class
  3397. ->select("uid,goods_classid,status")
  3398. ->where($where)
  3399. ->limit($start,$nums)
  3400. ->fetchAll();
  3401. $shop_list = [];
  3402. foreach($rs as &$v){
  3403. $shopwhere['uid'] = $v['uid'];
  3404. if(!empty($province)){
  3405. $shopwhere['province']=$province;
  3406. }
  3407. $shop_info=\PhalApi\DI()->notorm->shop_apply
  3408. ->select('uid,name,thumb,username,sale_nums,service_points,province')
  3409. ->where($shopwhere)
  3410. ->fetchOne();
  3411. // $v['cate_name']=$cate_name;
  3412. //
  3413. // $v['addtime'] = date('Y-m-d H:i:s',$shop_info['addtime']);
  3414. //
  3415. if($shop_info){
  3416. $shop_info['thumb'] = get_upload_path( $shop_info['thumb']);
  3417. $shop_info['delivery_time'] = '35分钟' ;
  3418. $shop_list[]=$shop_info;
  3419. }
  3420. }
  3421. return $shop_list;
  3422. }
  3423. // 根据不同条件获取物流列表信息
  3424. function getExpressInfo($where){
  3425. $info=\PhalApi\DI()->notorm->shop_express
  3426. ->where($where)
  3427. ->fetchOne();
  3428. return $info;
  3429. }
  3430. /* 生成二维码 */
  3431. function scerweima($url=''){
  3432. $key=md5($url);
  3433. //生成二维码图片
  3434. $filename2 = '/upload/qr/'.$key.'.png';
  3435. $filename = API_ROOT.'/../public/upload/qr/'.$key.'.png';
  3436. if(!file_exists($filename)){
  3437. require_once API_ROOT.'/../sdk/phpqrcode/phpqrcode.php';
  3438. $value = $url; //二维码内容
  3439. $errorCorrectionLevel = 'H'; //容错级别
  3440. $matrixPointSize = 6.2068965517241379310344827586207; //生成图片大小
  3441. //生成二维码图片
  3442. \QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
  3443. }
  3444. return $filename2;
  3445. }
  3446. /* 直播分类 */
  3447. function getLiveClass(){
  3448. $key="getLiveClass";
  3449. $list=getcaches($key);
  3450. if(!$list){
  3451. $list=\PhalApi\DI()->notorm->live_class
  3452. ->select("*")
  3453. ->order("list_order asc,id desc")
  3454. ->fetchAll();
  3455. if($list){
  3456. setcaches($key,$list);
  3457. }
  3458. }
  3459. $lang=\PhalApi\DI()->lang;
  3460. foreach ($list as $k => $v) {
  3461. if($lang=='en'){
  3462. $list[$k]['name']=$v['name_en'];
  3463. $list[$k]['des']=$v['des_en'];
  3464. }
  3465. $list[$k]['id']=(string)$v['id'];
  3466. $list[$k]['list_order']=(string)$v['list_order'];
  3467. }
  3468. return $list;
  3469. }
  3470. /* 检测用户是否存在 */
  3471. function checkUser($where){
  3472. if($where==''){
  3473. return 0;
  3474. }
  3475. $isexist=\PhalApi\DI()->notorm->user->where($where)->fetchOne();
  3476. if($isexist){
  3477. return 1;
  3478. }
  3479. return 0;
  3480. }
  3481. //验证数字是否整数/两位小数
  3482. function checkNumber($num){
  3483. if(floor($num) ==$num){
  3484. return 1;
  3485. }
  3486. if (preg_match('/^[0-9]+(.[0-9]{1,2})$/', $num)) {
  3487. return 1;
  3488. }
  3489. return 0;
  3490. }
  3491. //亚马逊存储
  3492. function awscloud($files,$type){
  3493. $rs=array('code'=>1000,'data'=>[],'msg'=>'上传失败');
  3494. $name=$files["file"]['name'];
  3495. $name_arr=explode(".", $name);
  3496. $suffix=$name_arr[count($name_arr)-1];
  3497. $rand=rand(0,100000);
  3498. $name=time().$rand.'.'.$suffix;
  3499. require_once(API_ROOT.'/../sdk/aws/aws-autoloader.php');
  3500. $configpri=getConfigPri();
  3501. $sharedConfig = [
  3502. 'profile' => 'default',
  3503. 'region' => $configpri['aws_region'], //区域
  3504. 'version' => 'latest',
  3505. 'Content-Type' => $files["file"]['type'],
  3506. //'debug' => true
  3507. ];
  3508. $sdk = new \Aws\Sdk($sharedConfig);
  3509. $s3Client = $sdk->createS3();
  3510. $result = $s3Client->putObject([
  3511. 'Bucket' => $configpri['aws_bucket'],
  3512. 'Key' => $name,
  3513. 'ACL' => 'public-read',
  3514. 'Content-Type' => $files["file"]['type'],
  3515. 'Body' => fopen($files["file"]['tmp_name'], 'r')
  3516. ]);
  3517. $a = (array)$result;
  3518. $n = 0;
  3519. $aws_res=1;
  3520. foreach($a as $k =>$t){
  3521. if($n==0){
  3522. $n++;
  3523. $info = $t['ObjectURL'];
  3524. if($info){
  3525. //return $info;
  3526. }else{
  3527. $aws_res=0;
  3528. }
  3529. }
  3530. }
  3531. if(!$aws_res){
  3532. return $rs;
  3533. }
  3534. $rs['code']=0;
  3535. $rs['data']['url']=$name;
  3536. return $rs;
  3537. }
  3538. //检测用户是否被禁播
  3539. function checkLiveBan($uid){
  3540. $arr=[
  3541. 'score'=>'0',
  3542. 'msg'=>'',
  3543. ];
  3544. $isexist=\PhalApi\DI()->notorm->user_live_ban
  3545. ->where('uid=?',$uid)
  3546. ->fetchOne();
  3547. if($isexist){
  3548. $time=time();
  3549. if($isexist['end_time']==0){
  3550. $arr['score']='1';
  3551. $arr['msg']=\PhalApi\T('您直播间已被永久封禁');
  3552. return $arr;
  3553. }else if($isexist['end_time']>$time){
  3554. $arr['score']='1';
  3555. $arr['msg']=\PhalApi\T('您直播间已被封禁,截止日期:').date('Y-m-d h:i',$isexist['end_time']);
  3556. return $arr;
  3557. }
  3558. //删除禁播记录
  3559. \PhalApi\DI()->notorm->user_live_ban
  3560. ->where('uid=? ',$uid)
  3561. ->delete();
  3562. }
  3563. return $arr;
  3564. }
  3565. /* 检测用户青少年模式是否开启 */
  3566. function checkTeenagers($uid){
  3567. if($uid==''){
  3568. return 0;
  3569. }
  3570. $result=\PhalApi\DI()->notorm->user_adolescent
  3571. ->where(['uid'=>$uid,'state'=>1])
  3572. ->fetchOne();
  3573. if($result){
  3574. return 1;
  3575. }
  3576. return 0;
  3577. }
  3578. //腾讯云IMUserSign
  3579. function txImUserSign($id){
  3580. $sig='';
  3581. $configpri=getConfigPri();
  3582. $appid=$configpri['tencentIM_appid'];
  3583. $appkey=$configpri['tencentIM_appkey'];
  3584. require_once API_ROOT.'/../sdk/tencentIM/TLSSigAPIv2.php';
  3585. $api = new \Tencent\TLSSigAPIv2($appid,$appkey);
  3586. $sign = $api->genUserSig($id);
  3587. return $sign;
  3588. }
  3589. /**
  3590. * 发送腾讯IM
  3591. * @param string $test 文本消息内容
  3592. * @param int $uid 被通知用户id
  3593. * @param string $adminName 发送消息者
  3594. */
  3595. function txMessageIM($test,$uid,$adminName){
  3596. $identifier='administrator'; //跟腾讯云控制台即时通讯IM默认管理员保持一致
  3597. $method_name='openim/sendmsg';
  3598. $data=array(
  3599. "SyncOtherMachine"=>2,
  3600. "From_Account"=>(string)$adminName,
  3601. "To_Account"=>(string)$uid,
  3602. "MsgRandom"=>(int)get_str(8),
  3603. "MsgBody"=>array(
  3604. 0=>array(
  3605. "MsgType"=>"TIMTextElem",
  3606. "MsgContent"=>array(
  3607. "Text"=>$test
  3608. )
  3609. ))
  3610. );
  3611. $data=json_encode($data);
  3612. $response=txImPostParam($identifier,$method_name,$data);
  3613. $result=json_decode($response,true);
  3614. /*var_dump($result);
  3615. die;*/
  3616. //ErrorCode:10001 自定义参数,代表请求失败
  3617. if($result && $result['ActionStatus']=='OK' && $result['ErrorCode']==0){
  3618. }
  3619. }
  3620. //腾讯云IM数据请求 identifier:标识体,method_name:方法名,data:请求消息体【json字符串】
  3621. function txImPostParam($identifier,$method_name,$data){
  3622. $configpri=getConfigPri();
  3623. $appid=$configpri['tencentIM_appid'];
  3624. $user_sign=txImUserSign($identifier);
  3625. $random=get_str(8);
  3626. $area_url='';
  3627. $tencentIM_area = $configpri['tencentIM_area'];
  3628. switch($tencentIM_area){
  3629. case 'china':
  3630. $area_url='console.tim.qq.com';
  3631. break;
  3632. case 'singapore':
  3633. $area_url='adminapisgp.im.qcloud.com';
  3634. break;
  3635. case 'seoul':
  3636. $area_url='adminapikr.im.qcloud.com';
  3637. break;
  3638. case 'frankfurt':
  3639. $area_url='adminapiger.im.qcloud.com';
  3640. break;
  3641. case 'bombay':
  3642. $area_url='adminapiind.im.qcloud.com';
  3643. break;
  3644. case 'silicon-valley':
  3645. $area_url='adminapiusa.im.qcloud.com';
  3646. break;
  3647. case 'jakarta':
  3648. $area_url='adminapiidn.im.qcloud.com';
  3649. break;
  3650. default:
  3651. $area_url='console.tim.qq.com';
  3652. }
  3653. $url='https://'.$area_url.'/v4/'.$method_name;
  3654. $url.='?sdkappid='.$appid.'&identifier='.$identifier.'&usersig='.$user_sign.'&random='.$random.'&contenttype=json';
  3655. $curl = curl_init();
  3656. curl_setopt($curl, CURLOPT_URL, $url);
  3657. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  3658. curl_setopt($curl, CURLOPT_NOBODY, true);
  3659. curl_setopt($curl, CURLOPT_POST, true);
  3660. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  3661. $return_str = curl_exec($curl);
  3662. if (curl_error($curl)) {
  3663. curl_close($curl);
  3664. return json_encode(['ActionStatus'=>'FAIL','ErrorCode'=>'10001']);
  3665. }
  3666. curl_close($curl);
  3667. return $return_str;
  3668. }
  3669. //随机生成存数字字符串
  3670. function get_str($length){
  3671. $str = '0123456789';
  3672. $len = strlen($str)-1;
  3673. $randstr = '';
  3674. for ($i=0;$i<$length;$i++) {
  3675. $num=mt_rand(0,$len);
  3676. $randstr .= $str[$num];
  3677. }
  3678. return $randstr;
  3679. }
  3680. /**
  3681. * 腾讯云TPNS移动推送
  3682. * @param string $title 推送标题
  3683. * @param string $msg 推送消息内容
  3684. * @param string $type 推送类型 all 全员推送 single 单账号推送 account_list 账号列表推送
  3685. * @param integer $uid 单账号用户id
  3686. * @url https://cloud.tencent.com/document/product/548/39064
  3687. */
  3688. function txMessageTpns($title,$msg,$type,$uid=0,$account_list=[],$json_str=''){
  3689. require_once API_ROOT.'/../sdk/tencentTpns/tpns.php';
  3690. $configpri=getConfigPri();
  3691. $area=$configpri['tencentTpns_area'];
  3692. $accessid_android=$configpri['tencentTpns_accessid_android'];
  3693. $secretkey_android=$configpri['tencentTpns_secretkey_android'];
  3694. $accessid_ios=$configpri['tencentTpns_accessid_ios'];
  3695. $secretkey_ios=$configpri['tencentTpns_secretkey_ios'];
  3696. $ios_environment=$configpri['tencentTpns_ios_environment'];
  3697. if(
  3698. !in_array($area,['guangzhou','shanghai','hongkong','singapore']) ||
  3699. !$accessid_android ||
  3700. !$secretkey_android ||
  3701. !$accessid_ios ||
  3702. !$secretkey_ios
  3703. ){
  3704. return;
  3705. }
  3706. if($area=='guangzhou'){
  3707. $stub_android = new \tpns\Stub($accessid_android, $secretkey_android, \tpns\GUANGZHOU);
  3708. $stub_ios = new \tpns\Stub($accessid_ios, $secretkey_ios, \tpns\GUANGZHOU);
  3709. }else if($area=='shanghai'){
  3710. $stub_android = new \tpns\Stub($accessid_android, $secretkey_android, \tpns\SHANGHAI);
  3711. $stub_ios = new \tpns\Stub($accessid_ios, $secretkey_ios, \tpns\SHANGHAI);
  3712. }else if($area=='hongkong'){
  3713. $stub_android = new \tpns\Stub($accessid_android, $secretkey_android, \tpns\HONGKONG);
  3714. $stub_ios = new \tpns\Stub($accessid_ios, $secretkey_ios, \tpns\HONGKONG);
  3715. }else if($area=='singapore'){
  3716. $stub_android = new \tpns\Stub($accessid_android, $secretkey_android, \tpns\SINGAPORE);
  3717. $stub_ios = new \tpns\Stub($accessid_ios, $secretkey_ios, \tpns\SINGAPORE);
  3718. }else{
  3719. return;
  3720. }
  3721. if($type=='account_list' && count($account_list)==1){
  3722. $type='single';
  3723. $uid=$account_list[0];
  3724. }
  3725. if($type=='all'){
  3726. //Android推送
  3727. $android = new \tpns\AndroidMessage;
  3728. if($json_str){
  3729. $android->custom_content = $json_str;
  3730. }
  3731. //控制通知点击时乱转到指定页面
  3732. $action=[
  3733. "action_type"=> 1,// 动作类型,1,打开activity或app本身;2,打开浏览器;3,打开Intent
  3734. "activity"=> "com.yunbao.im.activity.ImMsgNotifyActivity"
  3735. ];
  3736. $android->action=(object)$action;
  3737. $req_android = \tpns\NewRequest(
  3738. \tpns\WithAudienceType(\tpns\AUDIENCE_ALL),
  3739. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3740. \tpns\WithTitle($title),
  3741. \tpns\WithContent($msg),
  3742. \tpns\WithAndroidMessage($android),
  3743. \tpns\WithEnvironment(\tpns\ENVIRONMENT_PROD)
  3744. );
  3745. $result_android = $stub_android->Push($req_android);
  3746. //var_dump($result_android);
  3747. //iOS推送
  3748. $ios = new \tpns\iOSMessage;
  3749. if($json_str){
  3750. $ios->custom = $json_str;
  3751. }
  3752. $req_ios = \tpns\NewRequest(
  3753. \tpns\WithAudienceType(\tpns\AUDIENCE_ALL),
  3754. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3755. \tpns\WithTitle($title),
  3756. \tpns\WithContent($msg),
  3757. \tpns\WithIOSMessage($ios),
  3758. \tpns\WithEnvironment(\tpns\ENVIRONMENT_DEV)
  3759. );
  3760. if($ios_environment==0){ //开发
  3761. $req_ios = \tpns\NewRequest(
  3762. \tpns\WithAudienceType(\tpns\AUDIENCE_ALL),
  3763. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3764. \tpns\WithTitle($title),
  3765. \tpns\WithContent($msg),
  3766. \tpns\WithIOSMessage($ios),
  3767. \tpns\WithEnvironment(\tpns\ENVIRONMENT_DEV)
  3768. );
  3769. }else{
  3770. $req_ios = \tpns\NewRequest(
  3771. \tpns\WithAudienceType(\tpns\AUDIENCE_ALL),
  3772. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3773. \tpns\WithTitle($title),
  3774. \tpns\WithContent($msg),
  3775. \tpns\WithIOSMessage($ios),
  3776. \tpns\WithEnvironment(\tpns\ENVIRONMENT_PROD)
  3777. );
  3778. }
  3779. $result_ios = $stub_ios->Push($req_ios);
  3780. //var_dump($result_ios);
  3781. }else if($type=='single'){
  3782. if(!$uid){
  3783. return;
  3784. }
  3785. $uid=(string)$uid;
  3786. //Android推送
  3787. $android = new \tpns\AndroidMessage;
  3788. if($json_str){
  3789. $android->custom_content = $json_str;
  3790. }
  3791. $action=[
  3792. "action_type"=> 1,// 动作类型,1,打开activity或app本身;2,打开浏览器;3,打开Intent
  3793. "activity"=> "com.yunbao.im.activity.ImMsgNotifyActivity"
  3794. ];
  3795. $android->action=(object)$action;
  3796. $req_android = \tpns\NewRequest(
  3797. \tpns\WithAudienceType(\tpns\AUDIENCE_ACCOUNT),
  3798. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3799. \tpns\WithTitle($title),
  3800. \tpns\WithContent($msg),
  3801. \tpns\WithAndroidMessage($android),
  3802. \tpns\WithAccountList(array($uid)),
  3803. \tpns\WithEnvironment(\tpns\ENVIRONMENT_PROD)
  3804. );
  3805. $result_android = $stub_android->Push($req_android);
  3806. //var_dump($result_android);
  3807. //iOS推送
  3808. $ios = new \tpns\iOSMessage;
  3809. if($json_str){
  3810. $ios->custom = $json_str;
  3811. }
  3812. if($ios_environment==0){ //开发
  3813. $req_ios = \tpns\NewRequest(
  3814. \tpns\WithAudienceType(\tpns\AUDIENCE_ACCOUNT),
  3815. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3816. \tpns\WithTitle($title),
  3817. \tpns\WithContent($msg),
  3818. \tpns\WithIOSMessage($ios),
  3819. \tpns\WithAccountList(array($uid)),
  3820. \tpns\WithEnvironment(\tpns\ENVIRONMENT_DEV)
  3821. );
  3822. }else{
  3823. $req_ios = \tpns\NewRequest(
  3824. \tpns\WithAudienceType(\tpns\AUDIENCE_ACCOUNT),
  3825. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3826. \tpns\WithTitle($title),
  3827. \tpns\WithContent($msg),
  3828. \tpns\WithIOSMessage($ios),
  3829. \tpns\WithAccountList(array($uid)),
  3830. \tpns\WithEnvironment(\tpns\ENVIRONMENT_PROD)
  3831. );
  3832. }
  3833. $result_ios = $stub_ios->Push($req_ios);
  3834. //var_dump($result_ios);
  3835. }else if($type=='account_list'){
  3836. if(empty($account_list)){
  3837. return;
  3838. }
  3839. //Android推送
  3840. $android = new \tpns\AndroidMessage;
  3841. if($json_str){
  3842. $android->custom_content = $json_str;
  3843. }
  3844. $action=[
  3845. "action_type"=> 1,// 动作类型,1,打开activity或app本身;2,打开浏览器;3,打开Intent
  3846. "activity"=> "com.yunbao.im.activity.ImMsgNotifyActivity"
  3847. ];
  3848. $android->action=(object)$action;
  3849. $req_android = \tpns\NewRequest(
  3850. \tpns\WithAudienceType(\tpns\AUDIENCE_ACCOUNT_LIST),
  3851. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3852. \tpns\WithTitle($title),
  3853. \tpns\WithContent($msg),
  3854. \tpns\WithAndroidMessage($android),
  3855. //\tpns\WithIOSMessage($ios),
  3856. \tpns\WithAccountList($account_list),
  3857. \tpns\WithEnvironment(\tpns\ENVIRONMENT_PROD)
  3858. );
  3859. $result_android = $stub_android->Push($req_android);
  3860. //var_dump($result_android);
  3861. //iOS推送
  3862. $ios = new \tpns\iOSMessage;
  3863. if($json_str){
  3864. $ios->custom = $json_str;
  3865. }
  3866. if($ios_environment==0){ //开发
  3867. $req_ios = \tpns\NewRequest(
  3868. \tpns\WithAudienceType(\tpns\AUDIENCE_ACCOUNT_LIST),
  3869. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3870. \tpns\WithTitle($title),
  3871. \tpns\WithContent($msg),
  3872. \tpns\WithIOSMessage($ios),
  3873. \tpns\WithAccountList($account_list),
  3874. \tpns\WithEnvironment(\tpns\ENVIRONMENT_DEV)
  3875. );
  3876. }else{
  3877. $req_ios = \tpns\NewRequest(
  3878. \tpns\WithAudienceType(\tpns\AUDIENCE_ACCOUNT_LIST),
  3879. \tpns\WithMessageType(\tpns\MESSAGE_NOTIFY),
  3880. \tpns\WithTitle($title),
  3881. \tpns\WithContent($msg),
  3882. \tpns\WithIOSMessage($ios),
  3883. \tpns\WithAccountList($account_list),
  3884. \tpns\WithEnvironment(\tpns\ENVIRONMENT_PROD)
  3885. );
  3886. }
  3887. $result_ios = $stub_ios->Push($req_ios);
  3888. //var_dump($result_ios);
  3889. }
  3890. }
  3891. /**
  3892. * @desc 获取腾讯云trtc推流/播流地址
  3893. * @param int $uid 观看用户id
  3894. * @param string $stream 流名 如:31258_1675238014
  3895. * @param int $type 流类型 0 播流 1 推流
  3896. */
  3897. function getTxTrtcUrl($uid,$stream,$type=0){
  3898. $configpri=getConfigPri();
  3899. $txim_appid=$configpri['tencentIM_appid'];
  3900. $user_sign=txImUserSign($uid);
  3901. if($type==0){
  3902. $stream_type='play';
  3903. }else{
  3904. $stream_type='push';
  3905. }
  3906. $url = 'trtc://cloud.tencent.com/'.$stream_type.'/'.$stream.'?sdkappid='.$txim_appid.'&userId='.$uid.'&usersig='.$user_sign.'&appscene=live';
  3907. return $url;
  3908. }
  3909. //获取直播上热门的曝光值和预估直播间人数
  3910. function getHotLiveExposure($num){
  3911. $exposure=$num*10;
  3912. $user_min_num=round($num*1.5);
  3913. $user_max_num=$user_min_num+112;
  3914. return [
  3915. 'exposure'=>(string)$exposure,
  3916. 'user_min_num'=>(string)$user_min_num,
  3917. 'user_max_num'=>(string)$user_max_num
  3918. ];
  3919. }