UserUtils.php 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111
  1. <?php
  2. namespace App\Wen\Utils;
  3. use App\Http\Controllers\Api\NotifyController;
  4. use App\Http\Controllers\Api\Repositories\WxContRepositories;
  5. use App\Http\Controllers\Api\Repositories\WxNoticeRepositories;
  6. use App\Http\Controllers\Api\Repositories\WxUserRepositores;
  7. use App\Jobs\SubscribeMessageJob;
  8. use App\Jobs\User\UpdateUserAnonymousInfoJob;
  9. use App\Jobs\UserBgColorUpdateJob;
  10. use App\Lib\Uploads\UploadHandler;
  11. use App\Models\Pets\WxPetsAdoption;
  12. use App\Models\Posts\WxComment;
  13. use App\Models\Posts\WxPost;
  14. use App\Models\Circle\WxCircle;
  15. use App\Models\Shop\WxShop;
  16. use App\Models\Shop\WxShopAddress;
  17. use App\Models\Shop\WxShopGoods;
  18. use App\Models\Shop\WxShopGoodsProduct;
  19. use App\Models\Shop\WxShopGuaranteeRecord;
  20. use App\Models\Shop\WxShopOrder;
  21. use App\Models\Shop\WxShopOrderGoods;
  22. use App\Models\User\WxUserFinancialFreeze;
  23. use App\Models\User\WxUserSetting;
  24. use App\Models\Voter\WxVoter;
  25. use App\Models\Voter\WxVoterPlayer;
  26. use App\Models\Voter\WxVoterPlayerBallot;
  27. use App\Models\WxAuthentication;
  28. use App\Models\WxChat;
  29. use App\Models\WxTask;
  30. use App\Models\User\WxUser;
  31. use App\Models\User\WxUserAvatarFrame;
  32. use App\Models\User\WxUserAvatarFramePurchase;
  33. use App\Models\User\WxUserCoinRecord;
  34. use App\Models\User\WxUserExperience;
  35. use App\Models\User\WxUserFinancial;
  36. use App\Models\User\WxUserFinancialRecord;
  37. use App\Models\User\WxUserFollow;
  38. use App\Models\User\WxUserInviteBenefitRecord;
  39. use App\Models\User\WxUserOnline;
  40. use App\Models\User\WxUserPlate;
  41. use App\Models\User\WxUserSignRecord;
  42. use App\Models\User\WxUserVisit;
  43. use Carbon\Carbon;
  44. use Illuminate\Support\Facades\Cache;
  45. use Illuminate\Support\Facades\DB;
  46. use Illuminate\Support\Facades\Redis;
  47. use Intervention\Image\Facades\Image;
  48. use Illuminate\Http\UploadedFile;
  49. use App\Models\Conditions;
  50. class UserUtils{
  51. public static function distance($uid, $longitude, $latitude){
  52. $user = self::get_cached_user($uid);
  53. if($user){
  54. if($user['longitude'] && $user['latitude']){
  55. $lng1=$user['longitude']; //经度1
  56. $lat1=$user['latitude']; //纬度1
  57. $lng2=$longitude; //经度2
  58. $lat2=$latitude; //纬度2
  59. $EARTH_RADIUS = 6378137; //地球半径
  60. $RAD = pi() / 180.0;
  61. $radLat1 = $lat1 * $RAD;
  62. $radLat2 = $lat2 * $RAD;
  63. $a = $radLat1 - $radLat2; // 两点纬度差
  64. $b = ($lng1 - $lng2) * $RAD; // 两点经度差
  65. $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  66. $s = $s * $EARTH_RADIUS;
  67. $s = round($s * 10000) / 10000;
  68. return $s;
  69. }
  70. }
  71. Cache::forget('get:cached:user:'.$uid);
  72. return null;
  73. }
  74. public static function get_simple_cached_user($user_id, $columns){
  75. $user = self::get_cached_user($user_id);
  76. $res = [];
  77. if($user){
  78. foreach ($columns as $column){
  79. $res[$column] = $user[$column];
  80. }
  81. }
  82. return $res;
  83. }
  84. public static function get_anonymous_info(WxUser &$user, $change = 0){
  85. if($user){
  86. if($user->anonymous_name && $change != 1){
  87. return [
  88. 'anonymous_name'=>$user->anonymous_name,
  89. 'anonymous_avatar'=>$user->anonymous_avatar
  90. ];
  91. }else{
  92. $anonymous_name_avatar = Settings::get('anonymous_name_avatar', [], true);
  93. if(_empty_($anonymous_name_avatar) || !is_array($anonymous_name_avatar)){
  94. return null;
  95. }
  96. $name_avatar = $anonymous_name_avatar[mini_rand(0, count($anonymous_name_avatar) - 1)];
  97. if($name_avatar && is_array($name_avatar)){
  98. WxUser::withoutSyncingToSearch(function () use (&$user, &$name_avatar){
  99. WxUser::where('id', $user->id)->update([
  100. 'anonymous_name' => $name_avatar['name'],
  101. 'anonymous_avatar' => $name_avatar['avatar']
  102. ]);
  103. Cache::forget('get:cached:user:'.$user->id);
  104. UpdateUserAnonymousInfoJob::dispatch($user->id * -1);
  105. });
  106. return [
  107. 'anonymous_name' => _array_key($name_avatar, 'name', ''),
  108. 'anonymous_avatar' => _array_key($name_avatar, 'avatar', '')
  109. ];
  110. }else{
  111. return null;
  112. }
  113. }
  114. }else{
  115. return null;
  116. }
  117. }
  118. public static function get_cached_user($user_id, $online_info = false, $message_reply_rate = false, $anonymous = false){
  119. if(_empty_($user_id)){
  120. return null;
  121. }
  122. $user = Cache::remember('get:cached:user:'.$user_id, 3600, function () use ($user_id){
  123. try {
  124. $user = WxUser::where('id', $user_id)->first(FieldUtils::userInfoColums());
  125. }catch (\Exception $e){
  126. if(strpos($e->getMessage(), 'Column not found') !== false){
  127. SiteUtils::forget_config_data();
  128. }else{
  129. _logger_(__file__, __line__, $e->getMessage());
  130. return null;
  131. }
  132. }
  133. if($user){
  134. self::user_expend($user);
  135. return $user->toArray();
  136. }
  137. return null;
  138. });
  139. if($user){
  140. if($anonymous){
  141. $user['user_name'] = $user['anonymous_name'];
  142. $user['user_avatar'] = $user['anonymous_avatar'];
  143. }
  144. if(_array_key($user, 'is_robot', 0) > 0){
  145. global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
  146. if($__MINI_GLOBAL_IS_ADMIN_SUPER__){
  147. $user['user_name'] = $user['user_name'].' 🤖';
  148. }
  149. }
  150. if($online_info){
  151. $online_item = WxUserOnline::where('user_id', $user['id'])->first(['online', 'device']);
  152. if($online_item){
  153. $user['online_time'] = format_datetime($online_item->online);
  154. $user['device'] = $online_item->device;
  155. }else{
  156. $user['online_time'] = '';
  157. $user['device'] = '';
  158. }
  159. }
  160. if($message_reply_rate){
  161. $user['message_replay_rate'] = (self::message_response_rate($user['id']) * 100) . '%' ;
  162. }
  163. if(_empty_(_array_key($user, 'setting', null))){
  164. Cache::forget('get:cached:user:'.$user['id']);
  165. $user['setting'] = WxUserSetting::find($user['id']);
  166. if(_empty_($user['setting'])){
  167. $user_setting = new WxUserSetting();
  168. $user_setting->user_id = $user['id'];
  169. $user_setting->save();
  170. $user->setting = WxUserSetting::find($user['id']);
  171. }
  172. }
  173. if(_empty_(_array_key($user, 'user_background_color', ''))){
  174. Cache::forget('get:cached:user:'.$user['id']);
  175. UserBgColorUpdateJob::dispatch($user['id'], false);
  176. $user['user_background_color'] = '63,47,45';
  177. }
  178. return $user;
  179. }
  180. return null;
  181. }
  182. public static function user_expend(&$user, $left_panel = false, $publish = false, $shop = false, $used = false, $scene = 0){
  183. Redis::sadd('robot:timed:user:active', $user->id);
  184. if(_empty_($user)){
  185. return;
  186. }
  187. $user->paycode = get_user_meta($user->id, 'paycode', 's') ? '******' : '';
  188. $user->coins = UserUtils::user_coins($user->id);
  189. $user->lv = UserUtils::lv($user->id);
  190. $user->honor = get_user_meta($user->id, 'honor');
  191. $user->user_labels = get_user_meta($user->id, 'user_labels', 'j');
  192. $user->avatar_frame_url = get_user_meta($user->id, 'cur_avatar_frame');
  193. $user->age = StrUtils::age($user->user_birthday);
  194. $user->constellation = StrUtils::constellation($user->user_birthday);
  195. $user->setting = WxUserSetting::find($user->id);
  196. if($shop){
  197. if($user->shop_id && $user->shop_id > 0){
  198. $user->shop = WxShop::where('id', $user->shop_id)->where('status', 1)->first(FieldUtils::shopInfoColums());
  199. }else{
  200. $user->shop = null;
  201. }
  202. }else{
  203. // 占位
  204. $user->shop = null;
  205. }
  206. $user->user_background_color = get_user_meta($user->id, 'user_background_color');
  207. if(_empty_($user->user_background_color)){
  208. UserBgColorUpdateJob::dispatch($user->id, false);
  209. $user->user_background_color = '63,47,45';
  210. }
  211. if(_empty_($user->setting)){
  212. $user_setting = new WxUserSetting();
  213. $user_setting->user_id = $user->id;
  214. $user_setting->save();
  215. $user->setting = WxUserSetting::find($user->id);
  216. }
  217. if($user->is_authentication){
  218. if($user->authentication_id && $user->authentication_id > 0){
  219. $user->authentication = WxAuthentication::find($user->authentication_id);
  220. }else{
  221. $user->authentication = WxAuthentication::where([
  222. ['user_id', '=', $user->id],
  223. ['authentication_state', '=', 1]
  224. ])->orderBy('id', 'desc')->first();
  225. }
  226. }
  227. if($left_panel){
  228. global $__MINI_GLOBAL_PLATFORM__,$__MINI_GLOBAL_DEVICE__;
  229. $is_exam = is_mini_examine_mode(11);
  230. $left_pannel_arr = [];
  231. $left_set_panels = Settings::get('app_user_left_panel', ['my_wallet', 'payment_password', 'consumption_records', 'my_order','my_sales', 'my_shopping_bag', 'address', 'my_certification', 'my_members', 'my_circle', 'my_team']);
  232. $left_set_panels[] = 'line';
  233. $last_is_line = false;
  234. foreach (['my_wallet', 'payment_password', 'consumption_records', 'line', 'line', 'my_order', 'my_sales', 'my_shopping_bag', 'address', 'line', 'my_certification', 'my_members', 'my_circle', 'my_team'] as $name){
  235. if(!in_array($name, $left_set_panels)){
  236. continue;
  237. }
  238. if($name == 'my_wallet'){
  239. if($is_exam && $__MINI_GLOBAL_PLATFORM__ == 'ios' && $__MINI_GLOBAL_DEVICE__ == 'app'){
  240. continue;
  241. }
  242. $left_pannel_arr[] = [
  243. 'type' => 1,
  244. 'text' => '我的钱包',
  245. 'icon' => 'mini-wodeqianbao mini-icon2',
  246. 'target_type' => 6,
  247. 'target_id' => '/pagesA/mine/earnings/earnings',
  248. ];
  249. }else if($name == 'payment_password'){
  250. $left_pannel_arr[] = [
  251. 'type' => 1,
  252. 'text' => '支付密码',
  253. 'icon' => 'mini-zhifumima mini-icon2',
  254. 'target_type' => 6,
  255. 'target_id' => '/pagesA/mine/paycode/paycode?direct=0',
  256. ];
  257. }else if($name == 'consumption_records'){
  258. $left_pannel_arr[] =[
  259. 'type' => 1,
  260. 'text' => '消费记录',
  261. 'icon' => 'mini-xiaofeijilu mini-icon2',
  262. 'target_type' => 6,
  263. 'target_id' => '/pagesA/mine/order/order',
  264. ];
  265. }else if($name == 'my_order'){
  266. $left_pannel_arr[] = [
  267. 'type' => 1,
  268. 'text' => '我的买入',
  269. 'icon' => 'mini-wodedingdan mini-icon2',
  270. 'target_type' => 6,
  271. 'target_id' => '/pagesA/shop/order/order',
  272. ];
  273. }else if($name == 'my_sales'){
  274. if(!$is_exam) {
  275. $left_pannel_arr[] = [
  276. 'type' => 1,
  277. 'text' => '我的卖出',
  278. 'icon' => 'mini-womaichude mini-icon2',
  279. 'target_type' => 6,
  280. 'target_id' => '/pagesS/sellers/sales/sales',
  281. ];
  282. if($user->shop_id > 0){
  283. $left_pannel_arr[] = [
  284. 'type' => 1,
  285. 'text' => '店铺管理',
  286. 'icon' => 'mini-dianpuguanli mini-icon2',
  287. 'target_type' => 6,
  288. 'target_id' => '/pagesA/shop/apply/apply',
  289. ];
  290. }
  291. }
  292. }else if($name == 'my_shopping_bag'){
  293. $left_pannel_arr[] = [
  294. 'type' => 1,
  295. 'text' => '我的购物袋',
  296. 'icon' => 'mini-wodegouwudai mini-icon2',
  297. 'target_type' => 6,
  298. 'target_id' => '/pagesA/shop/cart/cart',
  299. ];
  300. }else if($name == 'address'){
  301. $left_pannel_arr[] = [
  302. 'type' => 1,
  303. 'text' => '收货地址',
  304. 'icon' => 'mini-shouhuodizhi mini-icon2',
  305. 'target_type' => 6,
  306. 'target_id' => '/pagesA/mine/address/address?id=0',
  307. ];
  308. }else if($name == 'my_certification'){
  309. $left_pannel_arr[] = [
  310. 'type' => 1,
  311. 'text' => '我的认证',
  312. 'icon' => 'mini-woderenzheng mini-icon2',
  313. 'target_type' => 6,
  314. 'target_id' => '/pagesA/mine/certification/certification',
  315. ];
  316. }else if($name == 'my_members'){
  317. if(!$is_exam){
  318. $left_pannel_arr[] = [
  319. 'type' => 1,
  320. 'text' => '我的会员',
  321. 'icon' => 'mini-wodehuiyuan mini-icon2',
  322. 'target_type' => 6,
  323. 'target_id' => '/pagesA/mine/members/members',
  324. ];
  325. }
  326. }else if($name == 'my_circle'){
  327. $left_pannel_arr[] = [
  328. 'type' => 1,
  329. 'text' => '我的'.env('circle_call', '圈子'),
  330. 'icon' => 'mini-wodequanzi mini-icon2',
  331. 'target_type' => 6,
  332. 'target_id' => '/pagesA/mine/circleaudit/circleaudit',
  333. ];
  334. }else if($name == 'my_team'){
  335. if(!$is_exam) {
  336. if (Settings::get('user_invite_benefit_status', 1) == 1) {
  337. if ($user->lv >= Settings::get('app_user_lv_invite_benefit', 1)) {
  338. $left_pannel_arr[] = [
  339. 'type' => 1,
  340. 'text' => '我的团队',
  341. 'icon' => 'mini-wodetuandui mini-icon2',
  342. 'target_type' => 6,
  343. 'target_id' => '/pagesA/mine/invite/invite',
  344. ];
  345. }
  346. }
  347. }
  348. }
  349. if($name == 'line'){
  350. if(!$last_is_line){
  351. $last_is_line = true;
  352. $left_pannel_arr[] = [
  353. 'type' => 0,
  354. ];
  355. }
  356. }else{
  357. $last_is_line = false;
  358. }
  359. }
  360. $user->leftPanel = $left_pannel_arr;
  361. }
  362. if($publish){
  363. $publish_arr = [];
  364. $publish_video_lv = Settings::get('app_user_lv_publish_video', 2);
  365. global $__MINI_GLOBAL_PLATFORM__,$__MINI_GLOBAL_DEVICE__;
  366. $is_exam = is_mini_examine_mode(11);
  367. $simple_fixed = Settings::get('app_publish_simple_fixed', '101,102,103,104,105,106', true);
  368. if($simple_fixed){
  369. foreach (explode(',', $simple_fixed) as $code) {
  370. if($code == 102 || $code == 303){
  371. if($user->lv < $publish_video_lv || $is_exam){
  372. continue;
  373. }
  374. }
  375. if($__MINI_GLOBAL_DEVICE__ == 'app' && $is_exam && $__MINI_GLOBAL_PLATFORM__ == 'ios' && ($code == 104)){
  376. continue;
  377. }
  378. $icon = self::add_publish_icon($code, 1);
  379. if($icon){
  380. $publish_arr[] = $icon;
  381. }
  382. }
  383. }
  384. $simple_popup = Settings::get('app_publish_simple_popup', '201,202,203,204', true);
  385. if($simple_popup){
  386. foreach (explode(',', $simple_popup) as $code) {
  387. if($code == 102 || $code == 303){
  388. if($user->lv < $publish_video_lv || $is_exam){
  389. continue;
  390. }
  391. }
  392. if($__MINI_GLOBAL_DEVICE__ == 'app' && $is_exam && $__MINI_GLOBAL_PLATFORM__ == 'ios' && ($code == 104)){
  393. continue;
  394. }
  395. $icon = self::add_publish_icon($code, 2);
  396. if($icon){
  397. $publish_arr[] = $icon;
  398. }
  399. }
  400. }
  401. $imagetext_fixed = Settings::get('app_publish_imagetext_fixed', '301,302,303,305,306,307,308', true);
  402. if($imagetext_fixed){
  403. foreach (explode(',', $imagetext_fixed) as $code) {
  404. if($code == 102 || $code == 303){
  405. if($user->lv < $publish_video_lv || $is_exam){
  406. continue;
  407. }
  408. }
  409. if($__MINI_GLOBAL_DEVICE__ == 'app' && $is_exam && $__MINI_GLOBAL_PLATFORM__ == 'ios' && ($code == 104)){
  410. continue;
  411. }
  412. $icon = self::add_publish_icon($code, 3);
  413. if($icon){
  414. $publish_arr[] = $icon;
  415. }
  416. }
  417. }
  418. $imagetext_popup = Settings::get('app_publish_imagetext_popup', '201,202,203,204', true);
  419. if($imagetext_popup){
  420. foreach (explode(',', $imagetext_popup) as $code) {
  421. if($code == 102 || $code == 303){
  422. if($user->lv < $publish_video_lv || $is_exam){
  423. continue;
  424. }
  425. }
  426. if($__MINI_GLOBAL_DEVICE__ == 'app' && $is_exam && $__MINI_GLOBAL_PLATFORM__ == 'ios' && ($code == 104)){
  427. continue;
  428. }
  429. $icon = self::add_publish_icon($code, 4);
  430. if($icon){
  431. $publish_arr[] = $icon;
  432. }
  433. }
  434. }
  435. $user->publish = $publish_arr;
  436. }
  437. if($scene == 5 || $scene == 8){
  438. if($scene == 5){
  439. $grid_display_force = Settings::get('app_user_grid_display_force', []);
  440. }else{
  441. $grid_display_force = Settings::get('app_user_grid_three_display_force', []);
  442. }
  443. if(_empty_($grid_display_force)){
  444. $grid_display_force = [];
  445. }
  446. $grid = [];
  447. $card_count = ShopUtils::user_cart_count($user->id);
  448. $order_count = ShopUtils::get_order_count($user->id);
  449. $tip = '买过的商品';
  450. if($order_count){
  451. if( _array_key($order_count, 'payCount', 0) > 0 ){
  452. $tip = $order_count['payCount'].'件待付款';
  453. }else if( _array_key($order_count, 'deliverCount', 0) > 0 ){
  454. $tip = $order_count['deliverCount'].'件待发货';
  455. }else if( _array_key($order_count, 'closedCount', 0) > 0 ){
  456. $tip = $order_count['closedCount'].'件待晒单';
  457. }
  458. }
  459. if($scene == 8){
  460. if($user->setting && _array_key($user->setting, 'social_sph_id', '')){
  461. $grid[] = [
  462. 'icon' => 'mini-shipinhao',
  463. 'title' => '视频号',
  464. 'desc' => '点击跳转',
  465. 'target_type' => 14,
  466. 'path' => $user->setting['social_sph_id']
  467. ];
  468. }
  469. }
  470. foreach ($grid_display_force as $index){
  471. if($index == 'my_wallet'){
  472. $grid[] = [
  473. 'icon' => 'mini-icon2 mini-wodeqianbao',
  474. 'title' => '我的钱包',
  475. 'desc' => '查看余额和'.Settings::get('app_coin_name', '硬币'),
  476. 'target_type' => 6,
  477. 'path' => '/pagesA/mine/earnings/earnings'
  478. ];
  479. }
  480. else if($index == 'my_vip'){
  481. if(!$is_exam) {
  482. $grid[] = [
  483. 'icon' => 'mini-icon2 mini-wodehuiyuan',
  484. 'title' => '我的会员',
  485. 'desc' => $user->is_member ? '尊享会员' : '未开通',
  486. 'target_type' => 6,
  487. 'path' => '/pagesA/mine/members/members'
  488. ];
  489. }
  490. }else if($index == 'my_shop_order'){
  491. $grid[] = [
  492. 'icon' => 'mini-icon2 mini-wodedingdan',
  493. 'title' => '我的买入',
  494. 'desc' => $tip,
  495. 'target_type' => 6,
  496. 'path' => '/pagesA/shop/order/order'
  497. ];
  498. }else if($index == 'my_shop_bag'){
  499. $grid[] = [
  500. 'icon' => 'mini-icon2 mini-wodegouwudai',
  501. 'title' => '我的购物袋',
  502. 'desc' => $card_count > 0 ? $card_count.'件商品' : '空空如也',
  503. 'target_type' => 6,
  504. 'path' => '/pagesA/shop/cart/cart'
  505. ];
  506. }else if($index == 'my_pets_adoption'){
  507. if($scene == 5){
  508. $grid[] = [
  509. 'icon' => 'mini-songyang',
  510. 'title' => '我的领养',
  511. 'desc' => '以及送养,收藏',
  512. 'target_type' => 6,
  513. 'path' => '/pagesP/pet-adoption/pet-common-list/index?type=1'
  514. ];
  515. }else if($scene == 8){
  516. $desc = '一只也没有';
  517. $adopt_status_0_1 = WxPetsAdoption::where('user_id', $user->id)->whereIn('adopt_status', [0, 1])->get();
  518. if($adopt_status_0_1){
  519. $adopt_status_0 = 0;
  520. $adopt_status_1 = 0;
  521. $adopt_status_0_1->map(function ($v) use (&$adopt_status_0, &$adopt_status_1){
  522. if($v->adopt_status == 1){
  523. $adopt_status_1 += 1;
  524. }else{
  525. $adopt_status_0 += 1;
  526. }
  527. });
  528. if($adopt_status_0 > 0){
  529. $desc = $adopt_status_0.'只待送养';
  530. }else{
  531. if($adopt_status_1 > 0){
  532. $desc = '空空如也';
  533. }else{
  534. $desc = $adopt_status_1.'只已送养';
  535. }
  536. }
  537. }
  538. $grid[] = [
  539. 'icon' => 'mini-shipinhao',
  540. 'title' => 'TA的送养',
  541. 'desc' => $desc,
  542. 'target_type' => 14,
  543. // 'path' => '/pagesP/pet-xiangqin/pet-xiangqin-list/index?user_id='.$user->id
  544. 'path' => '/pagesP/pet-adoption/pet-common-list/index?user_id='.$user->id.'&type=5'
  545. ];
  546. }
  547. }else if($index == 'my_pets_xiangqin') {
  548. $xiangqin_info = Conditions::where('user_id', $user->id)->first();
  549. if($scene == 5){ $ta = '我';}else{ $ta = 'TA';}
  550. if (!isset($xiangqin_info) || $xiangqin_info->step == 0) {
  551. $grid[] = [
  552. 'icon' => 'mini-songyanga',
  553. 'title' => $ta.'的相亲',
  554. 'desc' => $ta.' 未发布相亲',
  555. 'target_type' => 6,
  556. 'path' => '/pagesP/pet-xiangqin/pet-xiangqin-list/index?user_id='.$user->id
  557. // 'path' => '/'
  558. ];
  559. } else {
  560. $grid[] = [
  561. 'icon' => 'mini-songyanga',
  562. 'title' => $ta.'的相亲',
  563. 'desc' => $ta.'的相亲',
  564. 'target_type' => 6,
  565. // 'path' => '/pagesP/pet-xiangqin/pet-xiangqin-list/index?user_id='.$user->id
  566. 'path' => '/pagesMeet/info/info?id='.$xiangqin_info->id
  567. ];
  568. }
  569. }else if($index == 'my_used'){
  570. $desc = '空空如也';
  571. $counts = DB::table('wx_used_good')
  572. ->select(
  573. DB::raw('SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS pushingCount'),
  574. DB::raw('SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) AS completedCount'),
  575. DB::raw('COUNT(*) AS totalCount')
  576. )
  577. ->where('user_id', $user->id)->whereNotIn('status', [0,2,4])
  578. ->first();
  579. if($counts->pushingCount > 0){
  580. $desc = $counts->pushingCount.'件闲置出售中';
  581. }else if($counts->completedCount > 0){
  582. $desc = '已卖出'.$counts->completedCount.'件闲置';
  583. }else{
  584. if($counts->totalCount > 0){
  585. $desc = '共发布了'.$counts->totalCount.'件闲置';
  586. }else{
  587. $desc = '一件闲置也没有';
  588. }
  589. }
  590. $grid[] = [
  591. 'icon' => 'mini-xianzhi1',
  592. 'title' => $scene == 5 ? '我的闲置' : 'TA的闲置',
  593. 'desc' => $desc,
  594. 'target_type' => 6,
  595. 'path' => '/pages/user/used/used?id=' . $user->id . '&name=' . $user->user_name
  596. ];
  597. }else if($index == 'my_voter'){
  598. $voter_path = '/pagesV/voter/list/list?uid='.$user->id;
  599. $tip2 = Cache::remember('user:gird_voter:tip:'.$user->id, 3600, function () use (&$user){
  600. $creat_count = WxVoter::where('user_id', $user->id)->where('status', 1)->count();
  601. $tip_ = '';
  602. if($creat_count > 0){
  603. $tip_ = '创建了'.$creat_count.'个选票';
  604. }else{
  605. $player_count = WxVoterPlayer::where('user_id', $user->id)->count();
  606. if($player_count > 0){
  607. $tip_ = '报名了'.$player_count.'个选票';
  608. }else{
  609. $ballot_count = WxVoterPlayerBallot::where('user_id', $user->id)->count();
  610. if($ballot_count > 0){
  611. $tip_ = $ballot_count.'次帮别人投票';
  612. }
  613. }
  614. }
  615. return $tip_;
  616. });
  617. if(_empty_($tip2)){
  618. $tip2 = '一个投票也没有';
  619. }else{
  620. if(StrUtils::startsWith($tip2, '报名了')){
  621. $voter_path = '/pagesV/voter/list/list?uid='.$user->id.'&tab=1';
  622. }else if(StrUtils::endsWith($tip2, '帮别人投票')){
  623. $voter_path = '/pagesV/voter/list/list?uid='.$user->id.'&tab=2';
  624. }
  625. }
  626. $grid[] = [
  627. 'icon' => 'mini-icon2 mini-xuanpiao',
  628. 'title' => $scene == 5 ? '我的选票' : 'Ta的选票',
  629. 'desc' => $tip2,
  630. 'target_type' => 6,
  631. 'path' => $voter_path
  632. ];
  633. }
  634. }
  635. if($scene == 5){
  636. if($tip != '买过的商品'){
  637. if(!in_array('my_shop_order', $grid_display_force)){
  638. $grid[] = [
  639. 'icon' => 'mini-icon2 mini-wodedingdan',
  640. 'title' => '我的买入',
  641. 'desc' => $tip,
  642. 'target_type' => 6,
  643. 'path' => '/pagesA/shop/order/order'
  644. ];
  645. }
  646. }
  647. if($card_count > 0){
  648. if(!in_array('my_shop_bag', $grid_display_force)){
  649. $grid[] = [
  650. 'icon' => 'mini-icon2 mini-wodegouwudai',
  651. 'title' => '我的购物袋',
  652. 'desc' => $card_count > 0 ? $card_count.'件商品' : '空空如也',
  653. 'target_type' => 6,
  654. 'path' => '/pagesA/shop/cart/cart'
  655. ];
  656. }
  657. }
  658. }
  659. if($scene == 5 || $scene == 8){
  660. if($user->user_state === 0){
  661. $user->state_tip = '正常用户';
  662. }else if($user->user_state === 1){
  663. $user->state_tip = '该用户已注销账号';
  664. }else if($user->user_state === 2){
  665. $ban_until = get_user_meta($user->id, 'ban_until', 's');
  666. if(time() > $ban_until){
  667. // 解除封号
  668. if(UserUtils::unlock_user($user, 2)){
  669. $user->user_state = 0;
  670. $user->state_tip = '正常用户';
  671. }
  672. }
  673. if($ban_until && $user->user_state === 2){
  674. $user->state_tip = '该用户被封号至'.Carbon::createFromTimestamp($ban_until)->toDateTimeString();
  675. }
  676. }else if($user->user_state === 3){
  677. $mute_until = get_user_meta($user->id, 'mute_until', 's');
  678. if(time() > $mute_until){
  679. // 解除禁言
  680. if(UserUtils::unlock_user($user, 3)){
  681. $user->user_state = 0;
  682. $user->state_tip = '正常用户';
  683. }
  684. }
  685. if($mute_until && $user->user_state === 3){
  686. $user->state_tip = '该用户被禁言至'.Carbon::createFromTimestamp($mute_until)->toDateTimeString();
  687. }
  688. }
  689. }
  690. if($scene == 8) {
  691. if (!in_array('my_used', $grid_display_force)) {
  692. $desc = '空空如也';
  693. $counts = DB::table('wx_used_good')
  694. ->select(
  695. DB::raw('SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS pushingCount'),
  696. DB::raw('SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) AS completedCount'),
  697. DB::raw('COUNT(*) AS totalCount')
  698. )
  699. ->where('user_id', $user->id)->whereNotIn('status', [0,2,4])
  700. ->first();
  701. if ($counts->pushingCount > 0) {
  702. $desc = $counts->pushingCount . '件闲置出售中';
  703. } else if ($counts->completedCount > 0) {
  704. $desc = '已卖出' . $counts->completedCount . '件闲置';
  705. } else {
  706. if ($counts->totalCount > 0) {
  707. $desc = '共发布了' . $counts->totalCount . '件闲置';
  708. } else {
  709. $desc = '一件闲置也没有';
  710. }
  711. }
  712. if ($counts->totalCount > 0) {
  713. $grid[] = [
  714. 'icon' => 'mini-xianzhi1',
  715. 'title' => 'TA的闲置',
  716. 'desc' => $desc,
  717. 'target_type' => 6,
  718. 'path' => '/pages/user/used/used?id=' . $user->id . '&name=' . $user->user_name
  719. ];
  720. }
  721. }
  722. }
  723. if (!in_array('my_voter', $grid_display_force)) {
  724. $tip = Cache::remember('user:gird_voter:tip:'.$user->id, 3600, function () use (&$user, $scene){
  725. $tip_ = '';
  726. if($scene == 5){
  727. $creat_count = WxVoter::where('user_id', $user->id)->count();
  728. }else{
  729. $creat_count = WxVoter::where('user_id', $user->id)->where('status', 1)->count();
  730. }
  731. if($creat_count > 0){
  732. $tip_ = '创建了'.$creat_count.'个选票';
  733. }else{
  734. $player_count = WxVoterPlayer::where('user_id', $user->id)->count();
  735. if($player_count > 0){
  736. $tip_ = '报名了'.$player_count.'个选票';
  737. }else{
  738. $ballot_count = WxVoterPlayerBallot::where('user_id', $user->id)->count();
  739. if($ballot_count > 0){
  740. $tip_ = $ballot_count.'次帮别人投票';
  741. }
  742. }
  743. }
  744. return $tip_;
  745. });
  746. if($tip){
  747. $voter_path = '/pagesV/voter/list/list?uid='.$user->id;
  748. if(StrUtils::startsWith($tip, '报名了')){
  749. $voter_path = '/pagesV/voter/list/list?uid='.$user->id.'&tab=1';
  750. }else if(StrUtils::endsWith($tip, '帮别人投票')){
  751. $voter_path = '/pagesV/voter/list/list?uid='.$user->id.'&tab=2';
  752. }
  753. $grid[] = [
  754. 'icon' => 'mini-icon2 mini-xuanpiao',
  755. 'title' => $scene == 5 ? '我的选票' : 'Ta的选票',
  756. 'desc' => $tip,
  757. 'target_type' => 6,
  758. 'path' => $voter_path
  759. ];
  760. }
  761. }
  762. if($scene == 8){
  763. if(!in_array('my_pets_adoption', $grid_display_force)) {
  764. $desc = '一只也没有';
  765. $adopt_status_0_1 = WxPetsAdoption::where('user_id', $user->id)->whereIn('adopt_status', [0, 1])->get();
  766. if ($adopt_status_0_1->isNotEmpty()) {
  767. $adopt_status_0 = 0;
  768. $adopt_status_1 = 0;
  769. $adopt_status_0_1->map(function ($v) use (&$adopt_status_0, &$adopt_status_1) {
  770. if ($v->adopt_status == 1) {
  771. $adopt_status_1 += 1;
  772. } else {
  773. $adopt_status_0 += 1;
  774. }
  775. });
  776. if ($adopt_status_0 > 0) {
  777. $desc = $adopt_status_0 . '只待送养';
  778. } else if($adopt_status_1 > 0) {
  779. $desc = $adopt_status_1 . '只已送养';
  780. }else{
  781. $desc = '空空如也';
  782. }
  783. $grid[] = [
  784. 'icon' => 'mini-songyang',
  785. 'title' => 'TA的送养',
  786. 'desc' => $desc,
  787. 'target_type' => 6,
  788. 'path' => '/pagesP/pet-adoption/pet-common-list/index?user_id=' . $user->id . '&type=5'
  789. ];
  790. }
  791. }
  792. }
  793. $user->grid = $grid;
  794. }
  795. if($scene == 8){
  796. $longtaps = [];
  797. global $__MINI_GLOBAL_CURRENT_USER_ID__;
  798. if($__MINI_GLOBAL_CURRENT_USER_ID__ > 0){
  799. if(is_object_user_special($__MINI_GLOBAL_CURRENT_USER_ID__, $user->tenant_id, true)){
  800. if($user->user_state == 3){
  801. $longtaps[] = [
  802. 'text' => '解除禁言',
  803. 'emoji' => '🤐',
  804. 'list' => [
  805. [
  806. 'action' => 'unmute',
  807. 'name' => '解除',
  808. 'user_id' => $user->id
  809. ]
  810. ]
  811. ];
  812. }else{
  813. $longtaps[] = [
  814. 'text' => '禁言',
  815. 'emoji' => '🤐',
  816. 'list' => [
  817. [
  818. 'action' => 'mute',
  819. 'name' => '三天',
  820. 'days' => 3,
  821. 'user_id' => $user->id
  822. ],
  823. [
  824. 'action' => 'mute',
  825. 'name' => '一周',
  826. 'days' => 7,
  827. 'user_id' => $user->id
  828. ],
  829. [
  830. 'action' => 'mute',
  831. 'name' => '一个月',
  832. 'days' => 30,
  833. 'user_id' => $user->id
  834. ],
  835. [
  836. 'action' => 'mute',
  837. 'name' => '三个月',
  838. 'days' => 90,
  839. 'user_id' => $user->id
  840. ]
  841. ]
  842. ];
  843. }
  844. if($user->user_state == 2){
  845. $longtaps[] = [
  846. 'text' => '解除封号',
  847. 'emoji' => '❌',
  848. 'list' => [
  849. [
  850. 'action' => 'unban',
  851. 'name' => '解除',
  852. 'user_id' => $user->id
  853. ],
  854. ]
  855. ];
  856. }else{
  857. $longtaps[] = [
  858. 'text' => '封号',
  859. 'emoji' => '❌',
  860. 'list' => [
  861. [
  862. 'action' => 'ban',
  863. 'name' => '一周',
  864. 'days' => 7,
  865. 'user_id' => $user->id
  866. ],
  867. [
  868. 'action' => 'ban',
  869. 'name' => '一个月',
  870. 'days' => 30,
  871. 'user_id' => $user->id
  872. ],
  873. [
  874. 'action' => 'ban',
  875. 'name' => '三个月',
  876. 'days' => 90,
  877. 'user_id' => $user->id
  878. ],
  879. [
  880. 'action' => 'ban',
  881. 'name' => '10年',
  882. 'days' => 3650,
  883. 'user_id' => $user->id
  884. ]
  885. ]
  886. ];
  887. }
  888. $longtaps[] = [
  889. 'text' => '用户管理',
  890. 'emoji' => '🥸',
  891. 'list' => [
  892. [
  893. 'name' => 'Ta的钱包',
  894. 'action' => 'icon',
  895. 'target_type' => 6,
  896. 'target_id' => '/pagesA/mine/earnings/earnings?uid=' . $user->id,
  897. 'user_id' => $user->id,
  898. ],
  899. [
  900. 'name' => 'Ta的资料',
  901. 'action' => 'icon',
  902. 'target_type' => 6,
  903. 'target_id' => '/pagesA/mine/editmine/editmine?uid=' . $user->id,
  904. 'user_id' => $user->id,
  905. ],
  906. [
  907. 'name' => '裂变层级'.$user->invite_benefit_layer,
  908. 'action' => 'icon',
  909. 'target_type' => 6,
  910. 'target_id' => '/pagesA/mine/invite/layer?uid=' . $user->id,
  911. 'user_id' => $user->id,
  912. ],
  913. ]
  914. ];
  915. }
  916. $longtaps[] = [
  917. 'text' => '其他',
  918. 'emoji' => '😣',
  919. 'list' => [
  920. [
  921. 'name' => '举报',
  922. 'action' => 'report',
  923. 'user_id' => $user->id,
  924. ]
  925. ]
  926. ];
  927. }
  928. $user->longtaps = $longtaps;
  929. }
  930. }
  931. /**
  932. * @param $uid
  933. * @param false $force_update_default 是否强制掉更新默认的背景色
  934. * @return array|mixed|string|null
  935. */
  936. public static function user_bg_color_update($uid, $force_update_default = false){
  937. if(!$force_update_default){
  938. if(_empty_($uid)){
  939. return '';
  940. }
  941. }
  942. $color_arr = [
  943. [81, 76, 74],
  944. [63, 47, 45],
  945. [52, 55, 39],
  946. [78, 90, 97],
  947. [75, 71, 65],
  948. [89, 77, 86],
  949. [75, 80, 85],
  950. [113, 93, 94],
  951. [132, 84, 0],
  952. [18, 14, 17],
  953. [30, 38, 72],
  954. [62, 56, 95],
  955. [26, 39, 74],
  956. [74, 69, 74],
  957. [49, 36, 75],
  958. [35, 67, 86],
  959. [114, 112, 90]
  960. ];
  961. $is_update_site = false;
  962. if(!$force_update_default){
  963. $bg = WxUser::where('id', $uid)->value('user_background_maps');
  964. }else{
  965. $uid = 0;
  966. $bg = '';
  967. }
  968. if(_empty_($bg)){
  969. if(!$force_update_default){
  970. $default_background_color = get_site_meta('user_background_color', 's');
  971. if(!_empty_($default_background_color)){
  972. if($uid > 0){
  973. update_user_meta($uid, 'user_background_color', $default_background_color);
  974. Cache::forget('get:cached:user:'.$uid);
  975. }
  976. return $default_background_color;
  977. }
  978. }
  979. $bg = Settings::get('user_background_maps', '');
  980. if(_empty_($bg)){
  981. if($uid > 0) {
  982. update_user_meta($uid, 'user_background_color', implode(',', [63, 47, 45]));
  983. Cache::forget('get:cached:user:'.$uid);
  984. }
  985. return implode(',', [63, 47, 45]);
  986. }
  987. $is_update_site = true;
  988. }
  989. try {
  990. if(strpos($bg, 'img.mini.chongyeapp.com') !== false){
  991. $bg = str_replace('img.mini.chongyeapp.com', 'img.mini.minisns.cn', $bg);
  992. }
  993. // 设置图片路径
  994. $imagePath = file_get_contents($bg);
  995. $img = Image::make($imagePath);
  996. $mainColor = $img->limitColors(1)->pickColor(0, 0, 'array');
  997. $res_color = ImageUtils::find_closed_color($color_arr, $mainColor);
  998. if($res_color){
  999. $res_color = implode(',', $res_color);
  1000. if($uid > 0) {
  1001. update_user_meta($uid, 'user_background_color', $res_color);
  1002. Cache::forget('get:cached:user:'.$uid);
  1003. }
  1004. if($is_update_site){
  1005. update_site_meta('user_background_color', $res_color);
  1006. }
  1007. return $res_color;
  1008. }
  1009. }catch (\Exception $e){
  1010. _logger_(__file__, __line__, $e->getMessage());
  1011. return implode(',', [63, 47, 45]);
  1012. }
  1013. }
  1014. private static function add_publish_icon($code, $pos){
  1015. global $__MINI_GLOBAL_CURRENT_PLAYER_ID__;
  1016. if($code == 101){
  1017. return [
  1018. 'id' => 101,
  1019. 'name' => '图片',
  1020. 'icon' => 'mini-icon mini-tupian21',
  1021. 'pos' => $pos
  1022. ];
  1023. }else if($code == 102){
  1024. return [
  1025. 'id' => 102,
  1026. 'name' => '视频',
  1027. 'icon' => 'mini-icon mini-shipin2',
  1028. 'pos' => $pos
  1029. ];
  1030. }else if($code == 103){
  1031. return [
  1032. 'id' => 103,
  1033. 'name' => '话题',
  1034. 'icon' => 'mini-icon mini-huati2',
  1035. 'pos' => $pos
  1036. ];
  1037. }else if($code == 104){
  1038. return [
  1039. 'id' => 104,
  1040. 'name' => 'gif',
  1041. 'icon' => 'mini-icon mini-gif',
  1042. 'pos' => $pos
  1043. ];
  1044. }else if($code == 105){
  1045. return [
  1046. 'id' => 105,
  1047. 'name' => '表情',
  1048. 'icon' => 'mini-icon mini-biaoqing1',
  1049. 'pos' => $pos
  1050. ];
  1051. }else if($code == 106){
  1052. return [
  1053. 'id' => 106,
  1054. 'name' => '更多',
  1055. 'icon' => 'mini-icon mini-tianjia1',
  1056. 'pos' => $pos
  1057. ];
  1058. }else if($code == 107){
  1059. return [
  1060. 'id' => 107,
  1061. 'name' => '艾特',
  1062. 'icon' => 'mini-icon mini-a-',
  1063. 'pos' => $pos
  1064. ];
  1065. }else if($code == 108){
  1066. return [
  1067. 'id' => 108,
  1068. 'name' => '电话',
  1069. 'icon' => 'mini-icon mini-dianhua',
  1070. 'pos' => $pos
  1071. ];
  1072. }else if($code == 201){
  1073. return [
  1074. 'id' => 201,
  1075. 'name' => '音频',
  1076. 'icon' => 'mini-icon mini-yinpin',
  1077. 'pos' => $pos
  1078. ];
  1079. }else if($code == 202){
  1080. return [
  1081. 'id' => 202,
  1082. 'name' => '投票',
  1083. 'icon' => 'mini-icon mini-toupiao-m',
  1084. 'pos' => $pos
  1085. ];
  1086. }else if($code == 203){
  1087. return [
  1088. 'id' => 203,
  1089. 'name' => '商品',
  1090. 'icon' => 'mini-icon mini-shangpin11',
  1091. 'pos' => $pos
  1092. ];
  1093. }else if($code == 204){
  1094. return [
  1095. 'id' => 204,
  1096. 'name' => '附件',
  1097. 'icon' => 'mini-icon mini-fujian',
  1098. 'pos' => $pos
  1099. ];
  1100. }else if($code == 205){
  1101. return [
  1102. 'id' => 205,
  1103. 'name' => '链接',
  1104. 'icon' => 'mini-icon mini-lianjie',
  1105. 'pos' => $pos
  1106. ];
  1107. }else if($code == 206){
  1108. return [
  1109. 'id' => 206,
  1110. 'name' => '推广',
  1111. 'icon' => 'mini-icon mini-tuiguang',
  1112. 'pos' => $pos
  1113. ];
  1114. }else if($code == 207){
  1115. global $__MINI_GLOBAL_CURRENT_USER_ID__;
  1116. if(UserUtils::is_mini_admin($__MINI_GLOBAL_CURRENT_USER_ID__) || $__MINI_GLOBAL_CURRENT_PLAYER_ID__ > 0 || strpos(env(_multi_key('APP_PUBLISH_POST_SPH_USER'), ''), $__MINI_GLOBAL_CURRENT_USER_ID__) !== false){
  1117. return [
  1118. 'id' => 207,
  1119. 'name' => '视频号',
  1120. 'icon' => 'mini-icon mini-shipinhao',
  1121. 'pos' => $pos
  1122. ];
  1123. }
  1124. return null;
  1125. }else if($code == 208){
  1126. return [
  1127. 'id' => 208,
  1128. 'name' => '闲置',
  1129. 'icon' => 'mini-icon mini-xianzhi',
  1130. 'pos' => $pos
  1131. ];
  1132. }else if($code == 209){
  1133. return [
  1134. 'id' => 209,
  1135. 'name' => '组局',
  1136. 'icon' => 'mini-icon2 mini-zuju1',
  1137. 'pos' => $pos
  1138. ];
  1139. }else if($code == 301){
  1140. return [
  1141. 'id' => 301,
  1142. 'name' => '段落',
  1143. 'icon' => 'mini-icon mini-wenzi1',
  1144. 'pos' => $pos
  1145. ];
  1146. }else if($code == 302){
  1147. return [
  1148. 'id' => 302,
  1149. 'name' => '图片',
  1150. 'icon' => 'mini-icon mini-tupian21',
  1151. 'pos' => $pos
  1152. ];
  1153. }else if($code == 303){
  1154. return [
  1155. 'id' => 303,
  1156. 'name' => '视频',
  1157. 'icon' => 'mini-icon mini-shipin2',
  1158. 'pos' => $pos
  1159. ];
  1160. }else if($code == 304){
  1161. return [
  1162. 'id' => 304,
  1163. 'name' => '链接',
  1164. 'icon' => 'mini-icon mini-lianjie',
  1165. 'pos' => $pos
  1166. ];
  1167. }else if($code == 305){
  1168. return [
  1169. 'id' => 305,
  1170. 'name' => '音频',
  1171. 'icon' => 'mini-icon mini-yinpin',
  1172. 'pos' => $pos
  1173. ];
  1174. }else if($code == 306){
  1175. return [
  1176. 'id' => 306,
  1177. 'name' => '表格',
  1178. 'icon' => 'mini-icon mini-a-12biaoge',
  1179. 'pos' => $pos
  1180. ];
  1181. }else if($code == 307){
  1182. return [
  1183. 'id' => 307,
  1184. 'name' => '撤回',
  1185. 'icon' => 'mini-icon mini-fanhui',
  1186. 'pos' => $pos
  1187. ];
  1188. }else if($code == 308){
  1189. return [
  1190. 'id' => 308,
  1191. 'name' => '更多',
  1192. 'icon' => 'mini-icon mini-tianjia1',
  1193. 'pos' => $pos
  1194. ];
  1195. }
  1196. }
  1197. public static function is_user_blacked_to($uid, $user_id){
  1198. if(_empty_($uid) || _empty_($user_id)){
  1199. return false;
  1200. }
  1201. $user_set = get_user_meta($uid, 'user:blacked:set', 'j');
  1202. if(in_array($user_id, $user_set)){
  1203. return true;
  1204. }else{
  1205. return false;
  1206. }
  1207. }
  1208. /**
  1209. * @param $user_id
  1210. * @param $type
  1211. * @param $amount
  1212. * @param $reason
  1213. * @param $para [] 数组类型
  1214. * @return mixed|null
  1215. */
  1216. public static function user_financial_freeze($user_id, $type, $amount, $reason, array $para, $shop_order_id = null, $order_goods_id = null){
  1217. if(_empty_($user_id) || _empty_($type, true) || _empty_($amount) || $amount < 0.01 || _empty_($reason) || _empty_($para)){
  1218. return null;
  1219. }
  1220. if(_empty_(_array_key($para, 'type', null)) || _empty_(_array_key($para, 'tip', null))){
  1221. return null;
  1222. }
  1223. if(!in_array($type, [0, 1])){
  1224. return null;
  1225. }
  1226. if($type == 0){
  1227. // 商品
  1228. if(_empty_($shop_order_id) || _empty_($order_goods_id)){
  1229. return null;
  1230. }
  1231. }
  1232. $model = new WxUserFinancialFreeze();
  1233. $model->user_id = $user_id;
  1234. $model->type = $type;
  1235. $model->freeze_amout = $amount;
  1236. $model->freeze_reason = $reason;
  1237. $model->freeze_status = 0;
  1238. $model->para = json_encode($para);
  1239. if($shop_order_id){
  1240. $model->shop_order_id = $shop_order_id;
  1241. }
  1242. if($order_goods_id){
  1243. $model->order_goods_id = $shop_order_id;
  1244. }
  1245. $r = $model->save();
  1246. if($r){
  1247. return $model->id;
  1248. }else{
  1249. return null;
  1250. }
  1251. }
  1252. public static function get_cached_user_name($user_id, $remove_robot_flag = false){
  1253. if(_empty_($user_id)){
  1254. return null;
  1255. }
  1256. $user = self::get_cached_user($user_id);
  1257. if($user){
  1258. if($remove_robot_flag){
  1259. return str_replace([' 🤖', '🤖',' 💙🤖', ' 💗🤖', '💙🤖', '💗🤖'], '', _array_key($user, 'user_name', ''));
  1260. }else{
  1261. return $user['user_name'];
  1262. }
  1263. }
  1264. return null;
  1265. }
  1266. public static function lv($user_id){
  1267. if(_empty_($user_id)){
  1268. return 0;
  1269. }
  1270. return (int)Cache::remember('user:lv:'.$user_id, 3600, function () use ($user_id){
  1271. $experience = get_user_meta($user_id, 'experience', 'n');
  1272. if(_empty_($experience)){
  1273. return 1;
  1274. }
  1275. if($experience >= 38800){
  1276. return 6;
  1277. }else if($experience >= 10800){
  1278. return 5;
  1279. }else if($experience >= 3500){
  1280. return 4;
  1281. }else if($experience >= 800){
  1282. return 3;
  1283. }else if($experience >= 60){
  1284. return 2;
  1285. }
  1286. return 1;
  1287. });
  1288. }
  1289. public static function add_user_experience($user_id, $type, $num = 0){
  1290. if(_empty_($user_id) || _empty_($type, true)){
  1291. return false;
  1292. }
  1293. $experience = 0;
  1294. $today_num = WxUserExperience::where([
  1295. ['user_id', '=', $user_id],
  1296. ['type', '=', $type],
  1297. ['created_at', '>=', Carbon::today()]
  1298. ])->sum('num');
  1299. if(_empty_($today_num)){
  1300. $today_num = 0;
  1301. }
  1302. if($type == 0){
  1303. $experience = 1;
  1304. if($today_num >= 60){
  1305. return false;
  1306. }
  1307. }else if($type == 1){
  1308. $experience = 10;
  1309. if($today_num >= 30){
  1310. return false;
  1311. }
  1312. }else if($type == 2){
  1313. $experience = 3;
  1314. if($today_num >= 30){
  1315. return false;
  1316. }
  1317. }else if($type == 3){
  1318. $experience = 1;
  1319. if($today_num >= 30){
  1320. return false;
  1321. }
  1322. }else if($type == 4){
  1323. $experience = 2;
  1324. if($today_num >= 16){
  1325. return false;
  1326. }
  1327. }else if($type == 5){
  1328. $experience = 1;
  1329. if($today_num >= 30){
  1330. return false;
  1331. }
  1332. }else if($type == 6){
  1333. $experience = 4;
  1334. if($today_num >= 12){
  1335. return false;
  1336. }
  1337. }else if($type == 7){
  1338. $experience = 2;
  1339. if($today_num >= 10){
  1340. return false;
  1341. }
  1342. }else if($type == 8){
  1343. $experience = 3;
  1344. if($today_num >= 15){
  1345. return false;
  1346. }
  1347. }else if($type == 9){
  1348. $experience = 10;
  1349. if($today_num >= 10){
  1350. return false;
  1351. }
  1352. }else if($type == 10){
  1353. $experience = 3;
  1354. if($today_num >= 15){
  1355. return false;
  1356. }
  1357. }else if($type == 11){
  1358. $experience = 2;
  1359. if($today_num >= 10){
  1360. return false;
  1361. }
  1362. }else if($type == 12){
  1363. $experience = $num;
  1364. if($today_num >= 10){
  1365. return false;
  1366. }
  1367. }else if($type == 13){
  1368. $experience = $num;
  1369. if($today_num >= 100){
  1370. return false;
  1371. }
  1372. }else if($type == 14){
  1373. $experience = $num;
  1374. if($today_num >= 100){
  1375. return false;
  1376. }
  1377. }else if($type == 15){
  1378. $experience = $num;
  1379. if($today_num >= 100){
  1380. return false;
  1381. }
  1382. }
  1383. if($experience > 0){
  1384. $user_exper = new WxUserExperience();
  1385. $user_exper->user_id = $user_id;
  1386. $user_exper->type = $type;
  1387. $user_exper->num = $experience;
  1388. $r = $user_exper->save();
  1389. if($r){
  1390. $user_experience_ = get_user_meta($user_id, 'experience', 'n');
  1391. $new_experience = $user_experience_ + $experience;
  1392. if($new_experience >= 38800 && $user_experience_ < 38800){
  1393. Cache::forget('user:lv:'.$user_id);
  1394. Cache::forget('get:cached:user:'.$user_id);
  1395. UserUtils::assistant_notice($user_id, '恭喜,您的等级提升到lv6啦!');
  1396. GatewayUtils::success(GatewayUtils::uid2client_id($user_id), 12);
  1397. }else if($new_experience >= 10800 && $user_experience_ < 10800){
  1398. Cache::forget('user:lv:'.$user_id);
  1399. Cache::forget('get:cached:user:'.$user_id);
  1400. UserUtils::assistant_notice($user_id, '恭喜,您的等级提升到lv5啦!');
  1401. GatewayUtils::success(GatewayUtils::uid2client_id($user_id), 12);
  1402. }else if($new_experience >= 3500 && $user_experience_ < 3500){
  1403. Cache::forget('user:lv:'.$user_id);
  1404. Cache::forget('get:cached:user:'.$user_id);
  1405. UserUtils::assistant_notice($user_id, '恭喜,您的等级提升到lv4啦!');
  1406. GatewayUtils::success(GatewayUtils::uid2client_id($user_id), 12);
  1407. }else if($new_experience >= 800 && $user_experience_ < 800){
  1408. Cache::forget('user:lv:'.$user_id);
  1409. Cache::forget('get:cached:user:'.$user_id);
  1410. UserUtils::assistant_notice($user_id, '恭喜,您的等级提升到lv3啦!');
  1411. GatewayUtils::success(GatewayUtils::uid2client_id($user_id), 12);
  1412. }else if($new_experience >= 60 && $user_experience_ < 60){
  1413. Cache::forget('user:lv:'.$user_id);
  1414. Cache::forget('get:cached:user:'.$user_id);
  1415. UserUtils::assistant_notice($user_id, '恭喜,您的等级提升到lv2啦!');
  1416. GatewayUtils::success(GatewayUtils::uid2client_id($user_id), 12);
  1417. }
  1418. update_user_meta($user_id, 'experience', $new_experience, 'n');
  1419. return true;
  1420. }
  1421. }
  1422. return false;
  1423. }
  1424. public static function assistant_notice_review($expand_type, $expand_id, $to_user_id = 0){
  1425. $assistant_user = (int)Settings::get('app_notice_review_user', 0);
  1426. if(_empty_($assistant_user) || $assistant_user < 0){
  1427. return;
  1428. }
  1429. if($to_user_id > 0){
  1430. $users_id = [$to_user_id];
  1431. }else{
  1432. $users_id = explode(',', Settings::get('app_admin_super_admin', ''));
  1433. }
  1434. if($users_id){
  1435. foreach (array_unique($users_id) as $to_id){
  1436. if($to_id && $to_id > 0){
  1437. $chat_content_ = '';
  1438. if($expand_type == 101){
  1439. $chat_content_ = '笔记审核推送';
  1440. }else if($expand_type == 102){
  1441. $chat_content_ = '评论审核推送';
  1442. }else if($expand_type == 103){
  1443. $chat_content_ = '组局审核推送';
  1444. }else if($expand_type == 104){
  1445. $chat_content_ = '选票审核推送';
  1446. }else if($expand_type == 105){
  1447. $chat_content_ = '选票选手审核推送';
  1448. }else if($expand_type == 106){
  1449. $chat_content_ = '闲置审核推送';
  1450. }else if($expand_type == 107){
  1451. $chat_content_ = '圈子审核推送';
  1452. }else if($expand_type == 108){
  1453. $chat_content_ = '学生认证审核推送';
  1454. }else if($expand_type == 109){
  1455. $chat_content_ = '身份认证审核推送';
  1456. }else if($expand_type == 110){
  1457. $chat_content_ = '俱乐部审核推送';
  1458. }
  1459. WxChat::where('expand_type', $expand_type)->where('expand_id', $expand_id)->forceDelete();
  1460. $chatModel = new WxChat();
  1461. $chatModel->user_id = $assistant_user;
  1462. $chatModel->object_id = $to_id;
  1463. $chatModel->chat_content = $chat_content_;
  1464. $chatModel->chat_image = '';
  1465. $chatModel->chat_audio_url = '';
  1466. $chatModel->chat_audio_length = 0;
  1467. $chatModel->is_read = 0;
  1468. $chatModel->chat_state = 0;
  1469. $chatModel->expand_type = $expand_type;
  1470. $chatModel->expand_id = $expand_id;
  1471. $chatModel->save();
  1472. GatewayUtils::success(GatewayUtils::uid2client_id($to_id), 5, ['chat_content'=>$chat_content_, 'chat_image'=>'',
  1473. 'from_user'=>WxUser::where('id', $assistant_user)->first(FieldUtils::userInfoColums()) ]);
  1474. }
  1475. }
  1476. }
  1477. }
  1478. public static function assistant_notice($to_user_id, $content){
  1479. if(_empty_($to_user_id) || _empty_($content)){
  1480. return;
  1481. }
  1482. $assistant_user = (int)Settings::get('app_notice_assistant_user', 0);
  1483. if(_empty_($assistant_user) || $assistant_user < 0){
  1484. return;
  1485. }
  1486. if($to_user_id == 'admin'){
  1487. $users_id = explode(',', Settings::get('app_admin_super_admin', ''));
  1488. if($users_id){
  1489. foreach ($users_id as $to_id){
  1490. if($to_id && $to_id > 0){
  1491. WxContRepositories::add($assistant_user, $to_id, $content, '', '', 0, 0);//普通聊天
  1492. $websocket_id = GatewayUtils::uid2client_id($to_id);
  1493. if($websocket_id){
  1494. GatewayUtils::success($websocket_id, 5, ['chat_content'=>strip_tags($content), 'chat_image'=>'',
  1495. 'from_user'=>WxUser::where('id', $assistant_user)->first(FieldUtils::userInfoColums()) ]);
  1496. }
  1497. }
  1498. }
  1499. }
  1500. }else{
  1501. WxContRepositories::add($assistant_user, $to_user_id, $content, '', '', 0, 0);//普通聊天
  1502. $websocket_id = GatewayUtils::uid2client_id($to_user_id);
  1503. if($websocket_id){
  1504. GatewayUtils::success($websocket_id, 5, ['chat_content'=>strip_tags($content), 'chat_image'=>'',
  1505. 'from_user'=>WxUser::where('id', $assistant_user)->first(FieldUtils::userInfoColums()) ]);
  1506. }
  1507. }
  1508. }
  1509. public static function reward_user_coin($user_id, $action){
  1510. if(_empty_($user_id) || _empty_($action)){
  1511. return null;
  1512. }
  1513. if($action == 'publish'){
  1514. $today_times = WxUserCoinRecord::where([
  1515. ['user_id', '=', $user_id],
  1516. ['type', '=', 11],
  1517. ['created_at', '>=', Carbon::today()]
  1518. ])->count();
  1519. $max_times = Settings::get('app_coin_publish_reward_times', 1);
  1520. if($today_times >= $max_times){
  1521. return '';
  1522. }
  1523. $reward_every = Settings::get('app_coin_publish_reward_every', 10);
  1524. if($reward_every > 0){
  1525. self::update_user_coins($user_id, 11, $reward_every, '发帖奖励:'.Settings::get('app_coin_name', '硬币').'+'.$reward_every);
  1526. return Settings::get('app_coin_name', '硬币').'+'.$reward_every;
  1527. }
  1528. return '';
  1529. }else if($action == 'comment'){
  1530. $today_times = WxUserCoinRecord::where([
  1531. ['user_id', '=', $user_id],
  1532. ['type', '=', 12],
  1533. ['created_at', '>=', Carbon::today()]
  1534. ])->count();
  1535. $max_times = Settings::get('app_coin_comment_reward_times', 3);
  1536. if($today_times >= $max_times){
  1537. return '';
  1538. }
  1539. $reward_every = Settings::get('app_coin_comment_reward_every', 5);
  1540. if($reward_every > 0){
  1541. self::update_user_coins($user_id, 12, $reward_every, '评论奖励:'.Settings::get('app_coin_name', '硬币').'+'.$reward_every);
  1542. return Settings::get('app_coin_name', '硬币').'+'.$reward_every;
  1543. }
  1544. return '';
  1545. }else if($action == 'used-good'){
  1546. return '';
  1547. }else if($action == 'used-comment'){
  1548. return '';
  1549. }
  1550. }
  1551. public static function generate_text_avatar($text, $size = 200, $backgroound_color = '#8d8f9b')
  1552. {
  1553. return '';
  1554. if(Cache::has('generate_text_avatar:'.$text)){
  1555. return Cache::get('generate_text_avatar:'.$text);
  1556. }
  1557. // 创建一个空白图像
  1558. $image = Image::canvas($size, $size, $backgroound_color);
  1559. // 设置文字字体、颜色和大小
  1560. $fontPath = public_path('storage/font/DingTalk_JinBuTi_Regular.ttf');
  1561. $textColor = '#ffffff';
  1562. $fontSize = $size / 2;
  1563. // 将文字居中绘制在图像上
  1564. $image->text($text, $size / 2, $size / 2, function ($font) use ($fontPath, $textColor, $fontSize) {
  1565. $font->file($fontPath);
  1566. $font->size($fontSize);
  1567. $font->color($textColor);
  1568. $font->align('center');
  1569. $font->valign('middle');
  1570. });
  1571. // 将图像保存到指定路径
  1572. $file_name = 'images/'.md5(uniqid()) . '.png';
  1573. $filePath = public_path('storage/' . $file_name );
  1574. $image->save($filePath);
  1575. $cos_res = UploadHandler::handle(new UploadedFile(public_path('storage/'.$file_name), $file_name));
  1576. if(_array_key($cos_res, 'url', null)){
  1577. @unlink(public_path('storage/'.$file_name));
  1578. Cache::put('generate_text_avatar:'.$text, $cos_res['url']);
  1579. return $cos_res['url'];
  1580. }
  1581. // 返回生成的头像图像路径
  1582. @unlink(public_path('storage/'.$file_name));
  1583. return null;
  1584. }
  1585. public static function user_coins($user_id){
  1586. return get_user_meta($user_id, 'coins', 'n') ?: 0;
  1587. }
  1588. public static function update_user_coins($user_id, $type, $coins, $tip){
  1589. if(_empty_($user_id) || _empty_($coins) || _empty_($tip)){
  1590. return false;
  1591. }
  1592. $has_coins = get_user_meta($user_id, 'coins', 'n');
  1593. if(_empty_($has_coins)){
  1594. $has_coins = 0;
  1595. }
  1596. if($coins < 0 && $has_coins < _abs($coins)){
  1597. return false;
  1598. }
  1599. $has_coins += $coins;
  1600. $r = update_user_meta($user_id, 'coins', $has_coins, 'n');
  1601. if($r){
  1602. $WxUserCoinRecord = new WxUserCoinRecord();
  1603. $WxUserCoinRecord->user_id = $user_id;
  1604. $WxUserCoinRecord->type = $type;
  1605. $WxUserCoinRecord->incre = $coins;
  1606. $WxUserCoinRecord->tip = $tip;
  1607. $r_ = $WxUserCoinRecord->save();
  1608. return true;
  1609. }
  1610. return false;
  1611. }
  1612. public static function is_player_admin(){
  1613. global $__MINI_GLOBAL_CURRENT_PLAYER_ID__;
  1614. return self::is_mini_admin($__MINI_GLOBAL_CURRENT_PLAYER_ID__);
  1615. }
  1616. public static function is_mini_admin($uid, $not_cheeck_super = false){
  1617. if(!$not_cheeck_super){
  1618. if(self::is_mini_supder_admin($uid)){
  1619. return true;
  1620. }
  1621. }
  1622. global $__MINI_GLOBAL_TENANT_ID__;
  1623. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  1624. if(get_user_meta($uid, 'tenant_id_'.$__MINI_GLOBAL_TENANT_ID__, 'n') == 1){
  1625. return true;
  1626. }else{
  1627. return false;
  1628. }
  1629. }else{
  1630. if(in_array($uid, explode(',', env('APP_ADMIN_USER_ID', '')))){
  1631. return true;
  1632. }
  1633. }
  1634. return false;
  1635. }
  1636. public static function get_user_state($uid, $no_cache = false){
  1637. if($no_cache){
  1638. $user_state = WxUser::where('id', $uid)->value('user_state');
  1639. if(!_empty_($user_state, true)){
  1640. Cache::put('user:state:'.$uid, $user_state, 600);
  1641. return $user_state;
  1642. }
  1643. return -1;
  1644. }
  1645. if(Cache::has('user:state:'.$uid)){
  1646. return Cache::get('user:state:'.$uid);
  1647. }else{
  1648. $user_state = WxUser::where('id', $uid)->value('user_state');
  1649. if(!_empty_($user_state, true)){
  1650. Cache::put('user:state:'.$uid, $user_state, 600);
  1651. return $user_state;
  1652. }
  1653. }
  1654. return -1;
  1655. }
  1656. public static function is_user_can_upload($uid){
  1657. $state = self::get_user_state($uid);
  1658. if($state == 0){
  1659. return true;
  1660. }
  1661. return false;
  1662. }
  1663. public static function is_user_can_speak($uid){
  1664. if(_empty_($uid)){
  1665. return false;
  1666. }
  1667. $state = UserUtils::get_user_state($uid, false);
  1668. if($state == 3){
  1669. $mute_until = get_user_meta($uid, 'mute_until', 's');
  1670. $the_user = WxUser::find($uid);
  1671. if(time() > $mute_until && UserUtils::unlock_user($the_user, 3)){
  1672. return true;
  1673. }else{
  1674. return false;
  1675. }
  1676. }
  1677. return true;
  1678. }
  1679. public static function is_user_active($uid){
  1680. $state = self::get_user_state($uid, true);
  1681. if($state == 0){
  1682. return true;
  1683. }
  1684. return false;
  1685. }
  1686. public static function lock_user_incre(&$user, $type, $incre_seconds, $black_reason = ''){
  1687. $ban_until = max(get_user_meta($user->id, 'ban_until', 's'), time());
  1688. return self::lock_user($user, $type, $ban_until + $incre_seconds, $black_reason);
  1689. }
  1690. /**
  1691. * @param $user
  1692. * @param $type 3:禁言 2:封号
  1693. * @param $timestamp
  1694. * @param string $black_reason
  1695. * @return bool
  1696. */
  1697. public static function lock_user(&$user, $type, $timestamp, $black_reason = ''){
  1698. if($user){
  1699. if($type == 2){
  1700. DB::beginTransaction();
  1701. try {
  1702. // todo:
  1703. WxUser::where('id', $user['id'])->update( [ 'user_state' => 2, 'black_reason' => $black_reason ] );
  1704. update_user_meta($user['id'], 'ban_until', $timestamp, 's');
  1705. Cache::forget('get:cached:user:'.$user['id']);
  1706. Cache::forget('user:state:'.$user['id']);
  1707. DB::commit();
  1708. return true;
  1709. } catch (\Exception $e) {
  1710. DB::rollBack();
  1711. _logger_(__file__, __line__, $e->getMessage());
  1712. return false;
  1713. }
  1714. }else if($type == 3){
  1715. DB::beginTransaction();
  1716. try {
  1717. // todo:
  1718. WxUser::where('id', $user['id'])->update( [ 'user_state' => 3, 'black_reason' => $black_reason ] );
  1719. update_user_meta($user['id'], 'mute_until', $timestamp, 's');
  1720. Cache::forget('get:cached:user:'.$user['id']);
  1721. Cache::forget('user:state:'.$user['id']);
  1722. DB::commit();
  1723. return true;
  1724. } catch (\Exception $e) {
  1725. DB::rollBack();
  1726. _logger_(__file__, __line__, $e->getMessage());
  1727. return false;
  1728. }
  1729. }
  1730. }
  1731. return false;
  1732. }
  1733. public static function unlock_user(&$user, $type){
  1734. global $__MINI_GLOBAL_CURRENT_USER_ID__;
  1735. if($user){
  1736. if($type == 2){
  1737. if($user['user_state'] === 2){
  1738. $ban_until = get_user_meta($user['id'], 'ban_until', 's');
  1739. // 解除封号
  1740. DB::beginTransaction();
  1741. try {
  1742. // todo:
  1743. WxUser::where('id', $user['id'])->update( [ 'user_state' => 0 ] );
  1744. update_user_meta($user['id'], 'ban_until', null, 's');
  1745. Cache::forget('get:cached:user:'.$user['id']);
  1746. DB::commit();
  1747. GatewayUtils::success(GatewayUtils::uid2client_id($user['id']), 12);
  1748. return true;
  1749. } catch (\Exception $e) {
  1750. DB::rollBack();
  1751. _logger_(__file__, __line__, $e->getMessage());
  1752. return false;
  1753. }
  1754. }
  1755. }else if($type == 3){
  1756. if($user['user_state'] === 3){
  1757. $mute_until = get_user_meta($user['id'], 'mute_until', 's');
  1758. // 解除禁言
  1759. DB::beginTransaction();
  1760. try {
  1761. // todo:
  1762. WxUser::where('id', $user['id'])->update( [ 'user_state' => 0 ] );
  1763. update_user_meta($user['id'], 'mute_until', null, 's');
  1764. Cache::forget('get:cached:user:'.$user['id']);
  1765. update_user_meta($user['id'], 'sql_injection_times', 0, 'n');
  1766. DB::commit();
  1767. GatewayUtils::success(GatewayUtils::uid2client_id($user['id']), 12);
  1768. return true;
  1769. } catch (\Exception $e) {
  1770. DB::rollBack();
  1771. _logger_(__file__, __line__, $e->getMessage());
  1772. return false;
  1773. }
  1774. }
  1775. }
  1776. return false;
  1777. }else{
  1778. return false;
  1779. }
  1780. }
  1781. public static function is_user_not_active($uid){
  1782. return !self::is_user_active($uid);
  1783. }
  1784. public static function is_user_can_chat($uid){
  1785. $state = self::get_user_state($uid, true);
  1786. if($state == 0){
  1787. return true;
  1788. }
  1789. return false;
  1790. }
  1791. public static function user_permissions_check_by_config($uid, $key){
  1792. if(_empty_($uid) || _empty_($key)){
  1793. return false;
  1794. }
  1795. $permissions = Settings::get($key, []);
  1796. if(self::user_permissions_check($uid, $permissions)){
  1797. return true;
  1798. }
  1799. if($permissions && in_array(10, $permissions)){
  1800. if(in_array($uid, Settings::get($key.'_users', []))){
  1801. return true;
  1802. }
  1803. }
  1804. return false;
  1805. }
  1806. // 满足任一权限返回true
  1807. public static function user_permissions_check($uid, $permissions){
  1808. if(_empty_($uid)){
  1809. return false;
  1810. }
  1811. if(UserUtils::is_mini_supder_admin($uid)){
  1812. return true;
  1813. }
  1814. if(_empty_($permissions) || !is_array($permissions)){
  1815. return false;
  1816. }
  1817. foreach ($permissions as $permissions_code){
  1818. if($permissions_code == 1){
  1819. if(WxUser::where('id', $uid)->value('is_authentication') == 1){
  1820. return true;
  1821. }
  1822. }else if($permissions_code == 2){
  1823. if(WxUser::where('id', $uid)->value('is_member')){
  1824. return true;
  1825. }
  1826. }else if($permissions_code == 3){
  1827. if(UserUtils::is_mini_admin($uid)){
  1828. return true;
  1829. }
  1830. }else if(in_array($permissions_code, [4,5,6,7,8,9])){
  1831. if(UserUtils::lv($uid) >= $permissions_code - 3){
  1832. return true;
  1833. }
  1834. }
  1835. }
  1836. return false;
  1837. }
  1838. public static function user_action_permissions_check($uid, $action = 'publish_post'){
  1839. $arr = ['code'=>0, 'msg'=>''];
  1840. if(_empty_($uid)){
  1841. return $arr;
  1842. }
  1843. $state = self::get_user_state($uid, true);
  1844. if($state != 0){
  1845. $arr['msg'] = '用户非正常状态';
  1846. return $arr;
  1847. }
  1848. $permissions_code = 0;
  1849. if($action == 'publish_post'){
  1850. $permissions_code = Settings::get('app_publish_permissions', 0, true);
  1851. }else if($action == 'create_circle'){
  1852. $permissions_code = Settings::get('app_create_circle_permissions', 0, true);
  1853. }else if($action == 'user_authenticate'){
  1854. $permissions_code = Settings::get('app_user_authenticate_permissions', 0);
  1855. }
  1856. if($permissions_code == 0 || self::is_mini_admin($uid)){
  1857. $arr['code'] = 1;
  1858. return $arr;
  1859. }
  1860. // 认证用户
  1861. if($permissions_code == 1){
  1862. if(WxUser::where('id', $uid)->value('is_authentication') != 1){
  1863. $arr['msg'] = '您没有[认证用户]权限';
  1864. GatewayUtils::warn_message(GatewayUtils::uid2client_id($uid), '仅认证用户可以发布内容');
  1865. return $arr;
  1866. }
  1867. }else if($permissions_code == 2){
  1868. if(!WxUser::where('id', $uid)->value('is_member')){
  1869. $arr['msg'] = '您没有[会员]权限';
  1870. GatewayUtils::warn_message(GatewayUtils::uid2client_id($uid), '仅会员可以发布内容');
  1871. return $arr;
  1872. }
  1873. }else if($permissions_code == 3){
  1874. GatewayUtils::warn_message(GatewayUtils::uid2client_id($uid), '仅管理员可以发布内容');
  1875. $arr['msg'] = '您没有[管理员]权限';
  1876. return $arr;
  1877. }else if(in_array($permissions_code, [4,5,6,7,8,9])){
  1878. if(UserUtils::lv($uid) < $permissions_code - 3){
  1879. $arr['msg'] = '您没有[lv'.($permissions_code - 3).']权限';
  1880. GatewayUtils::warn_message(GatewayUtils::uid2client_id($uid), '仅会员可以发布内容');
  1881. return $arr;
  1882. }
  1883. }
  1884. $arr['code'] = 1;
  1885. return $arr;
  1886. }
  1887. public static function is_user_online($uid, $minutes = 5){
  1888. if(_empty_($uid)){
  1889. return false;
  1890. }
  1891. if(GatewayUtils::uid2client_id($uid)){
  1892. return true;
  1893. }else{
  1894. return WxUserOnline::where('user_id', $uid)->where('online', '>=', Carbon::now()->subMinutes($minutes))->exists();
  1895. }
  1896. }
  1897. public static function get_user_online_time($uid, $is_format = false){
  1898. if(_empty_($uid)){
  1899. return false;
  1900. }
  1901. $online = WxUserOnline::where('user_id', $uid)->value('online');
  1902. if($online){
  1903. if($is_format){
  1904. return format_datetime($online, 1);
  1905. }
  1906. }
  1907. return null;
  1908. }
  1909. public static function user_onlines($return_uids = 0, $sub_minute = 5, $page = 0){
  1910. $offset = ($page - 1) * 15;
  1911. if($return_uids == 1){
  1912. if($page > 0){
  1913. return WxUserOnline::where('online', '>', now()->subMinute($sub_minute))->orderBy('online', 'desc')->offset($offset)->limit(15)->pluck('user_id')->toArray();
  1914. }else{
  1915. return WxUserOnline::where('online', '>', now()->subMinute($sub_minute))->orderBy('online', 'desc')->pluck('user_id')->toArray();
  1916. }
  1917. }if($return_uids == 2){
  1918. if($page > 0){
  1919. return DB::table('wx_user')->select(['wx_user.id', 'wx_user.user_name','wx_user.user_avatar','wx_user_online.online'])->leftJoin('wx_user_online', 'wx_user.id', '=', 'wx_user_online.user_id')
  1920. ->where('wx_user_online.online', '>', now()->subMinute($sub_minute))->orderBy('wx_user_online.online', 'desc')
  1921. ->simplePaginate(15);
  1922. }else{
  1923. return WxUserOnline::where('online', '>', now()->subMinute($sub_minute))->orderBy('online', 'desc')->get();
  1924. }
  1925. }else{
  1926. return WxUserOnline::where('online', '>', now()->subMinute($sub_minute))->count();
  1927. }
  1928. }
  1929. public static function is_mini_supder_admin($uid){
  1930. if(_empty_($uid)){
  1931. return false;
  1932. }
  1933. global $__MINI_GLOBAL_IS_ADMIN_SUPER__, $__MINI_GLOBAL_CURRENT_USER_ID__;
  1934. if($uid > 0 && in_array($uid, explode(',', env('APP_SUPER_ADMIN_USER_ID', '')))){
  1935. if($__MINI_GLOBAL_CURRENT_USER_ID__ == $uid){
  1936. $__MINI_GLOBAL_IS_ADMIN_SUPER__ = true;
  1937. }
  1938. return true;
  1939. }
  1940. try{
  1941. if($uid > 0 && in_array( $uid, explode(',', Settings::get('app_admin_super_admin', '')) )){
  1942. // 主站管理员
  1943. if($__MINI_GLOBAL_CURRENT_USER_ID__ == $uid){
  1944. $__MINI_GLOBAL_IS_ADMIN_SUPER__ = true;
  1945. _update_env([
  1946. 'APP_SUPER_ADMIN_USER_ID' => Settings::get('app_admin_super_admin', ''),
  1947. ], [], 1);
  1948. }
  1949. return true;
  1950. }
  1951. global $__MINI_GLOBAL_TENANT_ID__;
  1952. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  1953. if(in_array( $uid, explode(',', Settings::get('app_admin_super_admin', '', true)) )){
  1954. return true;
  1955. }
  1956. }
  1957. }catch (\Exception $e){
  1958. return false;
  1959. }
  1960. return false;
  1961. }
  1962. public static function is_circle_admin($circle_id, $uid){
  1963. return WxCircle::where([
  1964. ['id', '=', $circle_id],
  1965. ['user_id', '=', $uid]
  1966. ])->exists();
  1967. }
  1968. public static function is_post_circle_admin($post_id, $uid){
  1969. $circle_id = WxPost::where('id', $post_id)->value('circle_id');
  1970. if($circle_id){
  1971. return self::is_circle_admin($circle_id, $uid);
  1972. }
  1973. return null;
  1974. }
  1975. public static function field_user_privacy($user){
  1976. // ['phone', 'user_birthday', 'user_mobile', 'weixin_name']
  1977. $user['phone'] = md5($user['phone']);
  1978. $user['user_birthday'] = md5($user['user_birthday']);
  1979. $user['user_mobile'] = md5($user['user_mobile']);
  1980. $user['weixin_name'] = md5($user['weixin_name']);
  1981. return $user;
  1982. }
  1983. public static function set_online($user_id, $device = '', $device_model = '', $system = ''){
  1984. if(_empty_($user_id)){
  1985. return false;
  1986. }
  1987. $updates = ['online'=>current_time()];
  1988. if($device){
  1989. $updates['device'] = $device;
  1990. }
  1991. if($device_model){
  1992. $updates['device_model'] = $device_model;
  1993. }
  1994. if($system){
  1995. $updates['system'] = $system;
  1996. }
  1997. $r = WxUserOnline::updateOrCreate(['user_id' => $user_id], $updates);
  1998. if($r){
  1999. return true;
  2000. }
  2001. return false;
  2002. }
  2003. public static function user_balance($uid){
  2004. return WxUserFinancial::where('user_id', $uid)->value('balance') ?? 0;
  2005. }
  2006. public static function follow_user($uid, $user_follow_id){
  2007. // 不能关注自己
  2008. $res = ['code'=>0, 'message'=>''];
  2009. if($user_follow_id == $uid){
  2010. $res['message'] = 403023;
  2011. return $res;
  2012. }
  2013. $isExists = WxUserFollow::where('user_follow_id', $user_follow_id)
  2014. ->where('user_id', $uid)
  2015. ->exists();
  2016. if (!$isExists) {
  2017. // 关注
  2018. $wxLike = new WxUserFollow();
  2019. $wxLike->user_follow_id = $user_follow_id;
  2020. $wxLike->user_id = $uid;
  2021. $wxLike->save();
  2022. Redis::sadd('realtime:others:set', json_encode([$user_follow_id, 4, 1]));
  2023. UserUtils::add_user_experience($uid, 8);
  2024. $res['code'] = 1;
  2025. $res['message'] = 403021;
  2026. } else {
  2027. // 取消关注
  2028. (new WxUserFollow())->where('user_follow_id', $user_follow_id)
  2029. ->where('user_id', $uid)
  2030. ->delete();
  2031. $res['code'] = 1;
  2032. $res['message'] = 403022;
  2033. }
  2034. Cache::forget('user:fans:count:'.$user_follow_id);
  2035. Cache::forget('user:follow:count:'.$uid);
  2036. Cache::forget($uid.':follow:user:'.$user_follow_id);
  2037. return $res;
  2038. }
  2039. /**
  2040. * @param $user_id
  2041. * @param $type getUserFinancialType
  2042. * @param $nums
  2043. * @param $tip 给用户发送的通知
  2044. * @param int $postsId
  2045. * @param string $bank_name
  2046. * @param string $bank_card
  2047. * @return bool
  2048. */
  2049. public static function update_user_financial($user_id, $type, $nums, $tip, $postsId = 0, $bank_name = '', $bank_card = ''){
  2050. // 「 」
  2051. if(_empty_($user_id) || _empty_($nums) || _empty_($tip)){
  2052. return false;
  2053. }
  2054. if($nums < 0){
  2055. _logger_(__file__, __line__, '出现了修改余额,nums参数为负数的情况');
  2056. return false;
  2057. }
  2058. // 充电
  2059. if($type == 0){
  2060. if(_empty_($postsId)){
  2061. return false;
  2062. }
  2063. }
  2064. if(in_array($type, [0, 1, 3, 4, 5, 6, 12,15, 16, 17, 18, 20, 23, 24, 25, 26, 28, 29, 31, 33, 103, 104, 10002])){
  2065. // 增加余额
  2066. $financial = WxUserFinancial::where('user_id', $user_id)->exists();
  2067. if ($financial) {
  2068. $uf = WxUserFinancial::where('user_id', $user_id)->first();
  2069. $balance = $uf['balance'] + $nums;
  2070. $sumPrice = $uf['sum_price'] + $nums;
  2071. WxUserFinancial::where('user_id', $user_id)
  2072. ->update(['balance' => $balance, 'sum_price' => $sumPrice,]);
  2073. } else {
  2074. $userFinancialModel = new WxUserFinancial();
  2075. $userFinancialModel->user_id = $user_id;
  2076. $userFinancialModel->balance = $nums;
  2077. $userFinancialModel->sum_price = $nums;
  2078. $userFinancialModel->save();
  2079. }
  2080. }
  2081. // 提现
  2082. if($type == 2){
  2083. $wd = WxUserFinancial::where('user_id', $user_id)->first();
  2084. if(_empty_($wd)){
  2085. return false;
  2086. }
  2087. if($wd['balance'] - $nums < 0){
  2088. return false;
  2089. }
  2090. $balance = $wd['balance'] - $nums;
  2091. $withdrawalPrice = ($wd['withdrawal_price'] ?? 0) + $nums;
  2092. WxUserFinancial::where('user_id', $user_id)
  2093. ->update(['bank_name' => $bank_name, 'bank_card' => $bank_card, 'balance' => $balance, 'withdrawal_price' => $withdrawalPrice]);
  2094. }
  2095. if(in_array($type, [7, 8, 9, 10, 11, 13, 19, 21, 22, 27, 30, 32, 101, 102, 10001])){
  2096. // 购买付费
  2097. $wd = WxUserFinancial::where('user_id', $user_id)->first();
  2098. if(_empty_($wd)){
  2099. return false;
  2100. }
  2101. if($wd['balance'] - $nums < 0){
  2102. return false;
  2103. }
  2104. $balance = $wd['balance'] - $nums;
  2105. WxUserFinancial::where('user_id', $user_id)->update(['balance'=>$balance]);
  2106. }
  2107. $record = new WxUserFinancialRecord();
  2108. $record->user_id = $user_id;
  2109. if(in_array($type, [2, 7, 8, 9, 10, 11, 13, 19, 21, 22, 27, 30, 32, 101, 102, 10001])){
  2110. $record->num = -$nums;
  2111. }else{
  2112. $record->num = $nums;
  2113. }
  2114. $record->type = $type;
  2115. $record->object = $postsId;
  2116. $record->tip = $tip;
  2117. $record->save();
  2118. // 通知用户
  2119. UserUtils::add_user_notice(7005, $user_id, '余额变动提醒:'.FieldUtils::getUserFinancialType()[(int)$type], $tip, 100, $postsId);
  2120. return true;
  2121. }
  2122. private function balance_type_data(){
  2123. //减少
  2124. $arr1 = [
  2125. 2 => '提现',
  2126. 101 => '付费咨询',
  2127. 102 => '付费旁听',
  2128. ];
  2129. //增加
  2130. $arr2 = [
  2131. 103 => '答主收益',
  2132. 104 => '咨询者旁听收益',
  2133. ];
  2134. }
  2135. public static function get_user_shop_address($uid){
  2136. if(_empty_($uid)){
  2137. return null;
  2138. }
  2139. return WxShopAddress::where('user_id', $uid)->orderBy('is_check', 'desc')->first();
  2140. }
  2141. public static function user_gift($uid, $financialType, $type, $num, $tip, $coin_record = 8, $product_id = 0){
  2142. if($type == 0){
  2143. self::update_user_coins($uid, $coin_record, $num, $tip);
  2144. return Settings::get('app_coin_name', '硬币').'*'.$num;
  2145. }else if($type == 1){
  2146. self::update_user_financial($uid, $financialType, $num, $tip);
  2147. return '余额*'.$num;
  2148. }else if($type == 2){
  2149. WxUserRepositores::vip($uid, $num);
  2150. return 'Vip*'.$num.'天';
  2151. }else if($type == 3){
  2152. $product_id = trim(trim($product_id, ','));
  2153. if(_empty_($product_id)){
  2154. return $product_id.'规格id不存在';
  2155. }
  2156. if($product_id > 0){
  2157. $product = WxShopGoodsProduct::where('id', $product_id)->first();
  2158. if($product){
  2159. if($product->stock <= 0){
  2160. return '商品规格'.$product_id.'库存不足';
  2161. }
  2162. DB::beginTransaction();
  2163. try {
  2164. $NotifyController = new NotifyController();
  2165. $orderSn = Utils::getSn(1);//生成订单号
  2166. $is_vip = false;
  2167. $goods_amount = $product->price * $num;
  2168. $addsinfo = self::get_user_shop_address($uid);
  2169. if(_empty_($addsinfo)){
  2170. $addsinfo = [
  2171. 'name' => '未填写',
  2172. 'mobile' => '16600000000',
  2173. 'province' => '北京市',
  2174. 'city' => '北京市',
  2175. 'county' => '东城区',
  2176. 'adds' => '长安街'
  2177. ];
  2178. UserUtils::add_user_notice(6002, $uid, '订单缺陷', '您抽奖获取的奖品订单缺少收货地址,请您及时更新,点击进入<a href="/pagesA/shop/order/order">我的买入</a>', 100);
  2179. }
  2180. $WxShopGoods = WxShopGoods::where('id', $product->goods_id)->first();
  2181. // todo:
  2182. //生成SHOP订单
  2183. $order_model = new WxShopOrder();
  2184. $order_model->user_id = $uid;
  2185. $order_model->seller_user_id = $WxShopGoods->user_id;
  2186. $order_model->order_id = $orderSn;//订单号
  2187. $order_model->goods_amount = $goods_amount;//商品总价
  2188. $order_model->discounts_amount = 0;//优惠金额
  2189. $order_model->order_amount = 0;//实付
  2190. $order_model->adds_id = _array_key($addsinfo, 'id', null);//收货地址id
  2191. $order_model->adds_name = $addsinfo['name'];//收货人姓名
  2192. $order_model->adds_mobile = $addsinfo['mobile'];//收货人电话
  2193. $order_model->address = $addsinfo['province'] . $addsinfo['city'] . $addsinfo['county'] . $addsinfo['adds'];//收货详细地址
  2194. $order_model->user_remark = '抽奖获得';//用户备注
  2195. $order_model->pay_status = 1;//支付状态 1=未付款 2=已付款 3=已退款
  2196. $order_model->status = 0;//发货状态 0=正常 1=未发货 2=已发货 3=确认收货 4=已退货 5=取消
  2197. $order_model->order_goods_id = [$WxShopGoods->id];
  2198. if($WxShopGoods->contact_id){
  2199. $order_model->contact_id = $WxShopGoods->contact_id;
  2200. }
  2201. $order_model->save();
  2202. $orderId = $order_model->id;//获取订单id
  2203. //
  2204. $_goods_amount = 0;
  2205. $_order_amount = 0;
  2206. //批量添加订单商品
  2207. $orderGoods = [];
  2208. $good_servers = $WxShopGoods->service_id;
  2209. if(_empty_($good_servers)){
  2210. $good_servers = '[]';
  2211. }
  2212. $created_at = date('Y-m-d H:i:s', time());
  2213. $good_servers = json_decode($good_servers, true);
  2214. $refund_deadline = Carbon::now()->addDays(7);
  2215. if(in_array(1, $good_servers)){
  2216. $refund_deadline = Carbon::now();
  2217. $created_at = $refund_deadline;
  2218. }
  2219. $recharge = $is_vip ? $product->vip_price * $num : $product->price * $num;
  2220. $_order_amount += $recharge;
  2221. $_goods_amount += $product->price * $num;
  2222. $orderGoods[0]['pic'] = $product->pic;
  2223. $orderGoods[0]['name'] = $WxShopGoods->name;
  2224. $orderGoods[0]['product'] = $product->param_value;
  2225. $orderGoods[0]['vip_price'] = $product->vip_price;
  2226. $orderGoods[0]['price'] = $product->price;
  2227. $orderGoods[0]['recharge'] = 0;
  2228. $orderGoods[0]['quantity'] = $num;
  2229. $orderGoods[0]['order_id'] = $orderId;
  2230. $orderGoods[0]['seller_user_id'] = $order_model->seller_user_id ?: 0;
  2231. $orderGoods[0]['buyer_user_id'] = $uid;
  2232. $orderGoods[0]['goods_id'] = $product->goods_id;
  2233. $orderGoods[0]['goods_type'] = $WxShopGoods->type;
  2234. $orderGoods[0]['product_id'] = $product->id;
  2235. $orderGoods[0]['created_at'] = $created_at;
  2236. $orderGoods[0]['refund_deadline'] = $refund_deadline;
  2237. $ogModel = new WxShopOrderGoods();
  2238. $ogModel->addAll($orderGoods);
  2239. WxShopOrder::where('id', $orderId)
  2240. ->update(['pay_status' => 2, 'status' => 1, 'coins_num'=>0, 'coins_pay'=>0, 'pay_way'=>ShopUtils::generate_payway_text(0,0, 0,0,0, 0, 0, 0, true)]);
  2241. WxShopGoodsProduct::where('id', $product_id)->update(['stock' => $product->stock - 1]);//减库存
  2242. // 理论上闲置商品不可以当做礼物
  2243. $NotifyController->usedGoodPaied($orderId);
  2244. ShopUtils::paied_content_process($orderId);
  2245. ShopUtils::split_shop_order_by_seller($orderId);
  2246. ShopUtils::split_shop_order_by_type($orderId);
  2247. ShopUtils::order_buys_and_stock($orderId);
  2248. DB::commit();
  2249. return $WxShopGoods->goods_name.'[规格:'.$product->param_value.']*'.$num;
  2250. } catch (\Exception $e) {
  2251. DB::rollBack();
  2252. _logger_(__file__, __line__, $e->getMessage());
  2253. return '出现未知错误,请联系管理员查明';
  2254. }
  2255. }
  2256. }
  2257. }
  2258. }
  2259. /**
  2260. * @param $uid
  2261. * @param string $date 'Y-m-d H:i:s'格式
  2262. * @return false
  2263. */
  2264. public static function is_signed($uid,string $date = ''){
  2265. if(_empty_($uid)){
  2266. return false;
  2267. }
  2268. if(_empty_($date)){
  2269. $date = current_time('day');
  2270. }else{
  2271. $date = date('Y-m-d', strtotime($date));
  2272. }
  2273. $count = WxUserSignRecord::where([['user_id', '=', $uid],['created_at', 'like', '%' . $date . '%']])->count();
  2274. return $count > 0;
  2275. }
  2276. /**
  2277. * @param $uid
  2278. * @param string $date
  2279. * @return bool
  2280. */
  2281. public static function sign_reward($uid,string $date){
  2282. if(_empty_($uid)){
  2283. return false;
  2284. }
  2285. if(_empty_($date)){
  2286. $date = current_time();
  2287. }
  2288. $week = date('w', strtotime($date));
  2289. if($week == 0){
  2290. $week = 6;
  2291. }else{
  2292. $week -= 1;
  2293. }
  2294. $app_user_sign = Settings::get('app_user_sign', []);
  2295. if(isset($app_user_sign[$week])){
  2296. $reward = $app_user_sign[$week];
  2297. UserUtils::user_gift($uid, 4, $reward['type'], $reward['num'],
  2298. '签到奖励:'.FieldUtils::getGiftTypes()[(int)$reward['type']].'*'.$reward['num'], 7);
  2299. return true;
  2300. }else{
  2301. return false;
  2302. }
  2303. }
  2304. public static function task_reward($uid, $task_id){
  2305. if(_empty_($uid) || _empty_($task_id)){
  2306. return false;
  2307. }
  2308. $task = WxTask::find($task_id);
  2309. if(_empty_($task)){
  2310. return false;
  2311. }
  2312. return self::user_gift($uid, 5, $task->reward_type, $task->reward_num, '任务奖励:'.$task->name.' '.FieldUtils::getGiftTypes()[(int)$task->reward_type].'*'.$task->reward_num);
  2313. }
  2314. public static function add_user_notice($notice_code, $uid, $title, $content, $type, $posts_id = '', $order_id = '', $url = ''){
  2315. if($uid > 0){
  2316. $r = WxNoticeRepositories::addNotice($notice_code, $uid,$title,$content,$type,$posts_id, $order_id, $url);
  2317. if($r){
  2318. if(strpos(env('TEMPLATE_MESSAGE_NOTICE_BLACK_LIST', ''), (string)($notice_code)) === false){
  2319. Utils::templateMessage($uid, FieldUtils::getNoticeTypes()[(int)$type], $content, $title, '', '点击可查看详情');
  2320. }
  2321. if(!UserUtils::is_user_online($uid)){
  2322. Utils::app_push($title, $content, 999, ['target_type'=>6, 'target_id'=>'/pages/tabbar/notice/notice'], 'single', [$uid], 0);
  2323. }else{
  2324. GatewayUtils::info_message(GatewayUtils::uid2client_id($uid), $title);
  2325. }
  2326. }
  2327. return $r;
  2328. }
  2329. return 0;
  2330. }
  2331. public static function get_a_random_robot_uid($robot_flag = [1,2]){
  2332. $user_id = WxUser::whereIn('is_robot', $robot_flag)->inRandomOrder()->value('id');
  2333. if($user_id){
  2334. return $user_id;
  2335. }
  2336. throw new \Exception('没有可用机器人');
  2337. }
  2338. public static function insert_null_user($user_name, $avatar, $sex = 0, $tenant_id = 0){
  2339. $user_name = trim(_empty_default_($user_name, ''));
  2340. if(_empty_($user_name) || _empty_($avatar)){
  2341. return 0;
  2342. }
  2343. if (WxUser::where('user_name',$user_name)->exists()){
  2344. return 0;
  2345. }
  2346. // $maxId = WxUser::max('id');
  2347. $user = new WxUser();
  2348. $user->weixin_openid=null;
  2349. $user->weixin_app_openid=null;
  2350. $user->weixin_mp_openid=null;
  2351. $user->weixin_web_openid=null;
  2352. $user->weixin_shop_openid=null;
  2353. $user->weixin_unionid = null;
  2354. $user->user_name=$user_name;
  2355. $user->weixin_name=$user_name;
  2356. $user->user_background_maps = Settings::get('user_background_maps');
  2357. $user->user_avatar=$avatar ?? Settings::get('img_default');
  2358. $user->country='';
  2359. $user->province='';
  2360. $user->city='';
  2361. $user->is_robot = 1;
  2362. // [0 => '未知',1 => '男',2 => '女']
  2363. $user->gender=$sex;
  2364. $user->tenant_id = $tenant_id;
  2365. $r = $user->save();
  2366. if($r){
  2367. // UserUtils::set_online($user->id, ['app', 'mp'][(int)mini_rand(0,1)]);
  2368. return $user->id;
  2369. }
  2370. return 0;
  2371. }
  2372. public static function give_coins($user_id, $coins, $post_id, $comment_id, $reply_user_id){
  2373. $res_arr = [
  2374. 'code' => 0,
  2375. 'msg' => ''
  2376. ];
  2377. if(_empty_($user_id) || _empty_($post_id) || _empty_($coins)){
  2378. return $res_arr;
  2379. }
  2380. $my_coins = self::user_coins($user_id);
  2381. if($my_coins < $coins){
  2382. $res_arr = [
  2383. 'code' => 200011,
  2384. 'msg' => '金币不足'
  2385. ];
  2386. return $res_arr;
  2387. }
  2388. $give_options = Settings::get('app_coin_give_options', []);
  2389. if(_empty_($give_options)){
  2390. $res_arr = [
  2391. 'code' => 200004,
  2392. 'msg' => '管理员没有配置投币'
  2393. ];
  2394. return $res_arr;
  2395. }
  2396. $flag = false;
  2397. $exp = 0;
  2398. foreach ($give_options as $option){
  2399. if($option['num'] == $coins){
  2400. $flag = true;
  2401. $exp = $option['exp'];
  2402. break;
  2403. }
  2404. }
  2405. if(!$flag){
  2406. $res_arr = [
  2407. 'code' => 200004,
  2408. 'msg' => '管理员没有配置'.$coins.'的投币选项'
  2409. ];
  2410. return $res_arr;
  2411. }
  2412. $platform_percent = _between_(Settings::get('app_givecoin_platform_percent', 20), 0, 100);
  2413. if(_empty_($platform_percent)){
  2414. $platform_percent = 0;
  2415. }
  2416. DB::beginTransaction();
  2417. try{
  2418. $user_name = self::get_cached_user_name($user_id);
  2419. $author_id = WxPost::where('id', $post_id)->value('user_id');
  2420. if(!_empty_($reply_user_id)){
  2421. if($user_id == $reply_user_id){
  2422. DB::rollBack();
  2423. $res_arr = [
  2424. 'code' => 200004,
  2425. 'msg' => '不能给自己投币'
  2426. ];
  2427. return $res_arr;
  2428. }
  2429. }else{
  2430. if($comment_id > 0){
  2431. $comment_author_id = WxComment::where('id', $comment_id)->value('user_id');
  2432. if($user_id == $comment_author_id){
  2433. DB::rollBack();
  2434. $res_arr = [
  2435. 'code' => 200004,
  2436. 'msg' => '不能给自己投币'
  2437. ];
  2438. return $res_arr;
  2439. }
  2440. }else{
  2441. $comment_author_id = 0;
  2442. if($user_id == $author_id){
  2443. DB::rollBack();
  2444. $res_arr = [
  2445. 'code' => 200004,
  2446. 'msg' => '不能给自己投币'
  2447. ];
  2448. return $res_arr;
  2449. }
  2450. }
  2451. }
  2452. if($reply_user_id > 0){
  2453. $r = self::update_user_coins($user_id, 18, -$coins, '投币用户(id:'.$reply_user_id.'):'.Settings::get('app_coin_name', '硬币').'-'.$coins);
  2454. } else if($comment_author_id > 0){
  2455. $r = self::update_user_coins($user_id, 18, -$coins, '投币评论(id:'.$comment_id.'):'.Settings::get('app_coin_name', '硬币').'-'.$coins);
  2456. }else{
  2457. $r = self::update_user_coins($user_id, 18, -$coins, '投币笔记(id:'.$post_id.'):'.Settings::get('app_coin_name', '硬币').'-'.$coins);
  2458. }
  2459. if($r){
  2460. self::add_user_experience($user_id, 12, $exp);
  2461. $rest_coins = $coins - (int)($coins * $platform_percent / 100);
  2462. if($platform_percent > 0 && $rest_coins >= 1){
  2463. if($reply_user_id > 0){
  2464. self::update_user_coins($reply_user_id, 19, $rest_coins, $user_name.'(id: '.$user_id.') 给您投币(系统分成'.$platform_percent.'%):'.Settings::get('app_coin_name', '硬币').'+'.$rest_coins);
  2465. }else if($comment_author_id > 0){
  2466. self::update_user_coins($comment_author_id, 19, $rest_coins, $user_name.'(id: '.$user_id.') 给您的评论(id:'.$comment_id.')投币(系统分成'.$platform_percent.'%):'.Settings::get('app_coin_name', '硬币').'+'.$rest_coins);
  2467. }else{
  2468. self::update_user_coins($author_id, 19, $rest_coins, $user_name.'(id: '.$user_id.') 给您的笔记(id:'.$post_id.')投币(系统分成'.$platform_percent.'%):'.Settings::get('app_coin_name', '硬币').'+'.$rest_coins);
  2469. }
  2470. }else{
  2471. if($reply_user_id > 0){
  2472. self::update_user_coins($reply_user_id, 19, $coins, $user_name.'(id: '.$user_id.') 给您投币:'.Settings::get('app_coin_name', '硬币').'+'.$coins);
  2473. }else if($comment_author_id > 0){
  2474. self::update_user_coins($comment_author_id, 19, $coins, $user_name.'(id: '.$user_id.') 给您的评论(id:'.$comment_id.')投币:'.Settings::get('app_coin_name', '硬币').'+'.$coins);
  2475. }else{
  2476. self::update_user_coins($author_id, 19, $coins, $user_name.'(id: '.$user_id.') 给您的笔记(id:'.$post_id.')投币:'.Settings::get('app_coin_name', '硬币').'+'.$coins);
  2477. }
  2478. }
  2479. DB::commit();
  2480. $res_arr = [
  2481. 'code' => 200,
  2482. 'msg' => '投币成功,获得经验+'.$exp,
  2483. 'to_user' => $reply_user_id > 0 ? $reply_user_id : ($comment_author_id > 0 ? $comment_author_id : $author_id)
  2484. ];
  2485. return $res_arr;
  2486. }else{
  2487. DB::rollBack();
  2488. $res_arr = [
  2489. 'code' => 0,
  2490. 'msg' => '未知'
  2491. ];
  2492. return $res_arr;
  2493. }
  2494. }catch (\Exception $e){
  2495. DB::rollBack();
  2496. _logger_(__file__, __line__, $e->getMessage());
  2497. return $res_arr;
  2498. }
  2499. }
  2500. public static function user_grafting($user_id, $object_id){
  2501. if(_empty_($user_id) || _empty_($object_id)){
  2502. return false;
  2503. }
  2504. if(!WxUser::where('id', $user_id)->exists() || !WxUser::where('id', $object_id)->exists()){
  2505. return false;
  2506. }
  2507. // WxUser::where('weixin_openid', '')->update(['weixin_openid'=>null]);
  2508. // WxUser::where('weixin_unionid', '')->update(['weixin_unionid'=>null]);
  2509. // WxUser::where('weixin_app_openid', '')->update(['weixin_app_openid'=>null]);
  2510. // WxUser::where('weixin_mp_openid', '')->update(['weixin_mp_openid'=>null]);
  2511. // WxUser::where('weixin_web_openid', '')->update(['weixin_web_openid'=>null]);
  2512. // WxUser::where('weixin_shop_openid', '')->update(['weixin_shop_openid'=>null]);
  2513. // WxPost::where('user_id', $user_id)->update(['user_id'=>$object_id]);
  2514. // WxComment::where('user_id', $user_id)->update(['user_id'=>$object_id]);
  2515. // WxComment::where('comment_agent_id', $user_id)->update(['comment_agent_id'=>$object_id]);
  2516. // WxUserFollow::where('user_id', $user_id)->update(['user_id'=>$object_id]);
  2517. // WxUserFollow::where('user_follow_id', $user_id)->update(['user_follow_id'=>$object_id]);
  2518. }
  2519. public static function add_user_plate($uid, $plateId){
  2520. if(_empty_($uid) || _empty_($plateId)){
  2521. return false;
  2522. }
  2523. if (WxUserPlate::where('user_id', $uid)->where('plate_id', $plateId)->exists()) {
  2524. WxUserPlate::where('user_id', $uid)->where('plate_id', $plateId)->update(['status'=>1]);
  2525. return true;
  2526. }
  2527. // $sort = $request->input('sort',100);
  2528. $sort = WxUserPlate::where('user_id', $uid)->where('plate_id', $plateId)->orderBy('sort', 'desc')->value('sort');
  2529. // 排序
  2530. if (_empty_($sort)) {
  2531. $sort = 0;
  2532. } else {
  2533. $sort += $sort;
  2534. }
  2535. $UserPlate = new WxUserPlate();
  2536. $UserPlate->plate_id = $plateId;
  2537. $UserPlate->user_id = $uid;
  2538. $UserPlate->sort = $sort;
  2539. $UserPlate->save();
  2540. UserUtils::add_user_experience($uid, 11);
  2541. return true;
  2542. }
  2543. public static function is_user_readed_circle($uid, $circle_id){
  2544. if(_empty_($uid) || _empty_($circle_id)){
  2545. return true;
  2546. }
  2547. $updated_at = WxUserVisit::where([
  2548. ['user_id', '=', $uid],
  2549. ['type', '=', 2],
  2550. ['object_id', '=', $circle_id]
  2551. ])->value('updated_at');
  2552. if($updated_at){
  2553. $created_at = WxPost::where([
  2554. ['circle_id', '=', $circle_id],
  2555. ['is_examine', '=', 1],
  2556. ['posts_state', '=', 0],
  2557. ['created_at', '>', $updated_at]
  2558. ])->orderBy('created_at', 'desc')->value('created_at');
  2559. if($created_at){
  2560. return false;
  2561. }else{
  2562. return true;
  2563. }
  2564. }else{
  2565. $created_at = WxPost::where([
  2566. ['circle_id', '=', $circle_id],
  2567. ['is_examine', '=', 1],
  2568. ['posts_state', '=', 0],
  2569. ])->orderBy('created_at', 'desc')->value('created_at');
  2570. if($created_at){
  2571. return false;
  2572. }else{
  2573. return true;
  2574. }
  2575. }
  2576. }
  2577. public static function get_user_invite_benifit($user_id){
  2578. if(_empty_($user_id)){
  2579. return null;
  2580. }
  2581. $res = [
  2582. 'invitelv' => '',
  2583. 'total' => 0,
  2584. 'today' => 0,
  2585. 'lv1_total' => 0,
  2586. 'lv2_total' => 0,
  2587. 'lv3_total' => 0,
  2588. 'financial' => null
  2589. ];
  2590. $lv1_benifit = WxUserInviteBenefitRecord::where('lv1_user', $user_id)->sum('lv1_benefit');
  2591. $lv2_benifit = WxUserInviteBenefitRecord::where('lv2_user', $user_id)->sum('lv2_benefit');
  2592. $lv3_benifit = WxUserInviteBenefitRecord::where('lv3_user', $user_id)->sum('lv3_benefit');
  2593. $res['total'] = round($lv1_benifit + $lv2_benifit + $lv3_benifit, 2);
  2594. $res['lv1_total'] = $lv1_benifit;
  2595. $res['lv2_total'] = $lv2_benifit;
  2596. $res['lv3_total'] = $lv3_benifit;
  2597. if($res['total'] > 0){
  2598. $res['invitelv'] = '一级代理人';
  2599. }
  2600. $today_lv1_benifit = WxUserInviteBenefitRecord::where([
  2601. ['lv1_user','=', $user_id],
  2602. ['created_at', '>=', Carbon::today()]
  2603. ])->sum('lv1_benefit');
  2604. $today_lv2_benifit = WxUserInviteBenefitRecord::where([
  2605. ['lv2_user','=', $user_id],
  2606. ['created_at', '>=', Carbon::today()]
  2607. ])->sum('lv2_benefit');
  2608. $today_lv3_benifit = WxUserInviteBenefitRecord::where([
  2609. ['lv3_user','=', $user_id],
  2610. ['created_at', '>=', Carbon::today()]
  2611. ])->sum('lv3_benefit');
  2612. $res['today'] = $today_lv1_benifit + $today_lv2_benifit + $today_lv3_benifit;
  2613. $financial = WxUserFinancial::where('user_id', $user_id)->first();
  2614. if($financial){
  2615. $res['financial'] = $financial;
  2616. }else{
  2617. $res['financial'] = [
  2618. 'balance' => 0,
  2619. 'sum_price' => 0,
  2620. 'withdrawal_price' => 0
  2621. ];
  2622. }
  2623. return $res;
  2624. }
  2625. public static function purchase_avatar_frame($user_id, $avatar_frame_id){
  2626. $res = [
  2627. 'code' => 0,
  2628. 'msg' => ''
  2629. ];
  2630. if(_empty_($user_id) || _empty_($avatar_frame_id)){
  2631. $res['code'] = 200001;
  2632. $res['code'] = 200001;
  2633. return $res;
  2634. }
  2635. $avatar_frame = WxUserAvatarFrame::find($avatar_frame_id);
  2636. if(_empty_($avatar_frame)){
  2637. $res['code'] = 200003;
  2638. $res['tip'] = '没有'.$avatar_frame_id.'对应的头像框';
  2639. return $res;
  2640. }
  2641. DB::beginTransaction();
  2642. try{
  2643. $record = WxUserAvatarFramePurchase::where([
  2644. ['user_id', '=', $user_id], ['avatar_frame_id', '=', $avatar_frame->id]
  2645. ])->first();
  2646. if($record){
  2647. update_user_meta($user_id, 'cur_avatar_frame', $avatar_frame->image_url, 's');
  2648. update_user_meta($user_id, 'cur_avatar_frame_id', $avatar_frame->id, 'n');
  2649. Cache::forget('get:cached:user:'.$user_id);
  2650. DB::commit();
  2651. return [
  2652. 'code' => 200,
  2653. 'msg' => '已经购买过'
  2654. ];
  2655. }else {
  2656. $type = $avatar_frame->type;
  2657. $flag = false;
  2658. $tip = '';
  2659. $credit_type = 0;
  2660. $price = 0;
  2661. $activity_id = 0;
  2662. $purchase_way = 0;
  2663. // [0=>'普通', 1=>'会员', 2=>'金币', 3=>'余额']
  2664. if ($type == 0) {
  2665. $flag = true;
  2666. $tip = '该头像框为普通类型,直接佩戴';
  2667. $purchase_way = 0;
  2668. $res['code'] = 200;
  2669. $res['msg'] = '该头像框为普通类型,直接佩戴';
  2670. }else if($type == 1){
  2671. $is_member = WxUser::where('id', $user_id)->value('is_member');
  2672. if($is_member){
  2673. $flag = true;
  2674. $tip = '会员权限解锁';
  2675. $purchase_way = 1;
  2676. $res['code'] = 200;
  2677. $res['msg'] = '会员权限解锁';
  2678. }else{
  2679. DB::rollBack();
  2680. $res['code'] = 200000;
  2681. $res['msg'] = '该头像框为会员专属';
  2682. return $res;
  2683. }
  2684. }else if($type == 2){
  2685. $price = (int)($avatar_frame->price);
  2686. $coins = self::user_coins($user_id);
  2687. if($coins < $price){
  2688. DB::rollBack();
  2689. $res['code'] = 200011;
  2690. $res['msg'] = '金币不足';
  2691. return $res;
  2692. }
  2693. $r = self::update_user_coins($user_id, 16, -$price, '购买了头像框(id:'.$avatar_frame->id.'),金币-'.$price);
  2694. if($r){
  2695. $flag = true;
  2696. $purchase_way = 2;
  2697. $res['code'] = 200;
  2698. $res['msg'] = '金币购买成功';
  2699. $tip = '金币购买成功';
  2700. }else{
  2701. DB::rollBack();
  2702. $res['code'] = 200002;
  2703. $res['msg'] = '数据库错误';
  2704. return $res;
  2705. }
  2706. }else{
  2707. DB::rollBack();
  2708. $res['code'] = 200004;
  2709. $res['msg'] = '该接口不支持购买余额类型的头像框';
  2710. return $res;
  2711. }
  2712. if($flag){
  2713. $model = new WxUserAvatarFramePurchase();
  2714. $model->user_id = $user_id;
  2715. $model->avatar_frame_id = $avatar_frame->id;
  2716. //0:普通解锁 1:会员解锁 2:金币解锁 3:余额解锁 4:活动解锁
  2717. $model->purchase_way = $purchase_way;
  2718. $model->credit_type = $credit_type;
  2719. $model->price = $price;
  2720. $model->activity_id = $activity_id;
  2721. $model->description = $tip;
  2722. $model->save();
  2723. update_user_meta($user_id, 'cur_avatar_frame', $avatar_frame->image_url, 's');
  2724. update_user_meta($user_id, 'cur_avatar_frame_id', $avatar_frame->id, 'n');
  2725. Cache::forget('get:cached:user:'.$user_id);
  2726. }
  2727. }
  2728. DB::commit();
  2729. return $res;
  2730. }catch (\Exception $e){
  2731. DB::rollBack();
  2732. _logger_(__file__, __line__, $e->getMessage());
  2733. $res['code'] = 200006;
  2734. return $res;
  2735. }
  2736. }
  2737. public static function get_user_member_time($uid, $timestamp = false){
  2738. $end_time = _get_origin_attribute('wx_user', [['id', '=', $uid]], 'is_member');
  2739. if($end_time){
  2740. $end_time = (int)$end_time;
  2741. if($end_time <= time()){
  2742. $end_time = 0;
  2743. }
  2744. }else{
  2745. $end_time = 0;
  2746. }
  2747. if($timestamp){
  2748. return $end_time;
  2749. }else{
  2750. return $end_time > 0 ? timetostr($end_time) : '';
  2751. }
  2752. }
  2753. public static function message_response_rate( $uid ){
  2754. // 统计给定人收到的私信消息数(给我发私信的人数)
  2755. $recive_message_users = WxChat::where('object_id', $uid)->distinct('user_id')->pluck('user_id');
  2756. if($recive_message_users){
  2757. $recive_message_users_count = count($recive_message_users);
  2758. if($recive_message_users_count > 0){
  2759. $replay_count = WxChat::where('user_id', $uid)->whereIn('object_id', $recive_message_users)->distinct('object_id')->count('object_id');
  2760. return round($replay_count/$recive_message_users_count, 2);
  2761. }
  2762. }
  2763. return 0;
  2764. }
  2765. public static function update_user_shop_gurantee($uid, $num, $title){
  2766. if(_empty_($uid) || _empty_($title) || _empty_($num)){
  2767. return false;
  2768. }
  2769. DB::beginTransaction();
  2770. try {
  2771. // todo:
  2772. $shop = WxShop::where('user_id', $uid)->first();
  2773. $margin_rest = $shop->margin_rest;
  2774. WxShop::where('user_id', $uid)->update(['margin_rest'=> $margin_rest + $num]);
  2775. $model = new WxShopGuaranteeRecord();
  2776. $model->shop_id = $shop->id;
  2777. $model->title = $title;
  2778. $model->num = $num;
  2779. $r = $model->save();
  2780. UserUtils::add_user_notice(6005, $shop->user_id, '店铺保证金变动:'.$title, '保证金'.$num, 100, '', '');
  2781. if($r){
  2782. DB::commit();
  2783. return true;
  2784. }else{
  2785. DB::rollBack();
  2786. return false;
  2787. }
  2788. } catch (\Exception $e) {
  2789. DB::rollBack();
  2790. _logger_(__file__, __line__, $e->getMessage());
  2791. return false;
  2792. }
  2793. }
  2794. public static function authenticate_pass(WxAuthentication &$authentication, $flag = 1, $overrule_content = ''){
  2795. $is_need_notice = false;
  2796. if($authentication->authentication_state === 0){
  2797. $is_need_notice = true;
  2798. }
  2799. if($flag == 1){
  2800. WxAuthentication::where('id',$authentication->id)->update([
  2801. 'authentication_state'=>1
  2802. ]);
  2803. WxUser::where('id',$authentication->user_id)->update([
  2804. 'is_authentication'=>1,
  2805. 'authentication_id' => $authentication->id
  2806. ]);
  2807. if($is_need_notice){
  2808. UserUtils::add_user_notice(4005, $authentication->user_id, '认证处理结果', '认证成功,您提交的认证已经审核通过', 100);
  2809. SubscribeMessageJob::dispatch('authentication', $authentication->id);
  2810. }
  2811. }else if($flag == 2){
  2812. WxAuthentication::where('id',$authentication->id)->update([
  2813. 'authentication_state'=>2,
  2814. 'overrule_content'=>$overrule_content
  2815. ]);
  2816. UserUtils::add_user_notice(4005, $authentication->user_id, '认证处理结果', '认证失败'.($overrule_content ? ','.$overrule_content : ''), 100);
  2817. SubscribeMessageJob::dispatch('authentication', $authentication->id);
  2818. }
  2819. Cache::forget('total:todo:count');
  2820. Cache::forget('total:tenant:todo:count');
  2821. }
  2822. /**
  2823. * user_followed_circle_unread_post_count
  2824. * @param $userId
  2825. * @return int
  2826. */
  2827. public static function user_circle_unread_count($userId){
  2828. if(_empty_($userId)){
  2829. return 0;
  2830. }
  2831. //这里有10分钟缓存,所以可能对不上
  2832. return (int)(Cache::remember('user_circle_unread_count:'.$userId, 600, function () use (&$userId){
  2833. // 提前获取用户最近一次访问每个圈子的时间
  2834. $userVisits = DB::table('wx_user_visit')
  2835. ->select('object_id', 'updated_at')
  2836. ->where('user_id', $userId)
  2837. ->where('type', 2)
  2838. ->get()
  2839. ->keyBy('object_id');
  2840. // 获取符合条件的圈子ID
  2841. $circleIds = DB::table('wx_user_circle as uc')
  2842. ->join('wx_circle as c', 'uc.circle_id', '=', 'c.id')
  2843. ->where('uc.user_id', $userId)
  2844. ->where('uc.user_circle_state', 0)
  2845. ->where('c.circle_state', 1)
  2846. ->pluck('uc.circle_id');
  2847. // 如果没有符合条件的圈子,直接返回0
  2848. if ($circleIds->isEmpty()) {
  2849. return 0;
  2850. } else {
  2851. // 计算符合条件的未读帖子总数
  2852. $totalUnreadPosts = DB::table('wx_posts as p')
  2853. ->whereIn('p.circle_id', $circleIds)
  2854. ->where('p.posts_state', 0)
  2855. ->where('p.is_examine', 1)
  2856. ->where(function ($query) use (&$userVisits, &$circleIds) {
  2857. foreach ($userVisits as $circleId => $visit) {
  2858. $query->orWhere(function ($q) use ($circleId, $visit) {
  2859. $q->where('p.circle_id', $circleId)
  2860. ->where('p.created_at', '>', $visit->updated_at);
  2861. });
  2862. }
  2863. $query->orWhere(function ($q) use ($userVisits, $circleIds) {
  2864. $q->whereIn('p.circle_id', $circleIds->diff($userVisits->keys()));
  2865. });
  2866. })
  2867. ->count();
  2868. return $totalUnreadPosts;
  2869. }
  2870. }));
  2871. }
  2872. }