| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631 |
- <?php
- namespace app\weixin\model;
- use app\weixin\service\PRedis;
- use think\Db;
- use think\Model;
- class Member extends Model
- {
- protected $table = 'sg_user';
- /**
- * 注册用户
- * @param $params 注册参数
- * @return int|string
- */
- public static function regMember($params)
- {
- $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
- $userPass = isset($params['password']) ? trim($params['password']) : '';
- $payPass = isset($params['pay_password']) ? trim($params['pay_password']) : '';
- if ($mobile && Member::where(['user_login' => $mobile])->value('id')) {
- return 2001;
- }
- $userName = $mobile ? $mobile : makeUserName();
- $userPass = $userPass ? cmf_password($userPass) : cmf_password('123456');
- $payPass = $payPass ? cmf_password($payPass) : $userPass;
- $wxInfo = isset($params['wxInfo']) ? $params['wxInfo'] : [];
- $openid = isset($wxInfo['openid']) ? trim($wxInfo['openid']) : '';
- $headimgurl = isset($wxInfo['headimgurl']) ? trim($wxInfo['headimgurl']) : '';
- $nickname = isset($wxInfo['nickname']) ? trim($wxInfo['nickname']) : $userName;
- if (empty($openid)) {
- return false;
- }
- $data = [
- 'user_type' => 2,
- 'sex' => isset($params['sex']) ? intval($params['sex']) : 0,
- 'openid' => $openid,
- 'parent_id' => isset($params['parent_id']) ? intval($params['parent_id']) : 0,
- 'user_nickname' => isset($params['user_nickname']) ? trim($params['user_nickname']) : $nickname,
- 'avatar' => isset($params['avatar']) ? trim($params['avatar']) : $headimgurl,
- 'user_login' => $userName,
- 'user_pass' => $userPass,
- 'pay_password' => $payPass,
- 'is_follow' => isset($wxInfo['subscribe']) ? intval($wxInfo['subscribe']) : 0,
- 'last_login_ip' => get_client_ip(),
- 'last_login_time' => time(),
- 'create_time' => time(),
- ];
- if ($mobile) {
- $data['mobile'] = $mobile;
- }
- $userId = Member::where(['openid' => $openid])
- ->where('user_status', '>=', 0)
- ->value('id');
- PRedis::set('test:' . $openid, $data, 600);
- if (!$userId) {
- $userId = Member::insertGetId($data);
- return ['userId' => $userId, 'type' => 2];
- } else {
- $data = [
- 'last_login_ip' => get_client_ip(),
- 'updated_at' => date('Y-m-d H:i:s'),
- ];
- if(empty($data['user_nickname'])){
- unset($data['user_nickname']);
- }
- if(empty($data['avatar'])){
- unset($data['avatar']);
- }
- Member::where(['openid' => $openid])
- ->where('user_status', '>=', 0)
- ->update($data);
- return false;
- }
- }
- /**
- * 用户登录
- * @param $params
- * @return array|int|null|\PDOStatement|string|Model
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public static function login($params)
- {
- $userName = isset($params['mobile']) ? trim($params['mobile']) : '';
- $password = isset($params['password']) ? trim($params['password']) : '';
- $field = 'id,user_login,sex,user_nickname,avatar,openid,user_pass,user_type,score,balance,user_status';
- $userInfo = Member::where(['user_login' => $userName])->field($field)->find();
- $userPass = isset($userInfo['user_pass']) ? $userInfo['user_pass'] : '';
- $userStatus = isset($userInfo['user_status']) ? intval($userInfo['user_status']) : 0;
- if (empty($userInfo)) {
- return 2100;
- }
- $userInfo = $userInfo->toArray();
- if (!cmf_compare_password($password, $userPass)) {
- return 2101;
- }
- if ($userStatus != 1) {
- return 2102;
- }
- // 更新登录数据
- $updateData = [
- 'updated_at' => date('Y-m-d H:i:s'),
- 'last_login_ip' => get_client_ip(),
- 'last_login_time' => time(),
- ];
- $curOpenId = session('openid');
- $openid = isset($userInfo['openid']) ? trim($userInfo['openid']) : '';
- if ($curOpenId) {
- $checkId = Member::where(['openid' => $curOpenId])->value('id');
- if ((empty($checkId) && empty($openid)) || ($checkId && $checkId == $userInfo['id'])) {
- $wxInfo = session('wxInfo');
- $headimgurl = isset($wxInfo['headimgurl']) ? trim($wxInfo['headimgurl']) : '';
- $nickname = isset($wxInfo['nickname']) ? trim($wxInfo['nickname']) : $userName;
- $sex = isset($wxInfo['sex']) ? intval($wxInfo['sex']) : 0;
- if (empty($openid)) {
- $updateData['openid'] = $curOpenId;
- }
- if ($headimgurl && $userInfo['avatar'] != $headimgurl) {
- $updateData['avatar'] = $headimgurl;
- }
- if ($nickname && $userInfo['user_nickname'] != $nickname) {
- $updateData['user_nickname'] = $nickname;
- }
- if ($sex && $userInfo['sex'] != $sex) {
- $updateData['sex'] = $sex;
- }
- }
- }
- Member::where(['user_login' => $userName])->update($updateData);
- unset($userInfo['user_pass']);
- $userInfo = array_merge($userInfo, $updateData);
- session('userInfo', $userInfo);
- return $userInfo;
- }
- /**
- * 获取会员信息
- * @param $where 条件
- * @param string $field 字段
- * @return array|false|\PDOStatement|string|Model
- */
- public static function getInfo($where, $field = "")
- {
- $field = $field ? $field : 'id,openid,user_nickname,avatar_confirm_status,user_type,agent_type,shop_type,sign_day,sign_at,agent_status,avatar,user_login,collect_expire,lat,lng,address,real_name,redheart,is_reg_profile,mobile,balance,user_status,wechat_account as wechat_code,is_heart,vip_auth,vip_expire,vip_num,is_follow,freezing_choose';
- $info = self::where($where)->field($field)->order('user_status desc,id desc')->find();
- return $info ? $info->toArray() : [];
- }
- /**
- * 获取用户主页信息
- * @param $userId 用户ID
- * @param string $field 字段
- * @return array
- */
- public static function getHomeInfo($userId, $field = '', $type = 0)
- {
- $where = ['m.id' => $userId, 'm.user_status' => 1, 'm.user_type' => 2];
- $defaultField = 'm.id,m.user_nickname,m.avatar,m.avatar_confirm_status,m.mobile,m.sex,m.birthday,m.is_heart,m.real_name,m.agent_type,m.agent_status,up.age,up.height,up.weight,up.company,up.occupation,up.property,up.graduate,up.education,up.province,up.salary,up.city,up.district,up.home_province,up.home_city,up.home_district,up.introduce,up.family,up.hobby,up.purpose,up.cause,up.expect,up.show_company,up.show_graduate,up.married,up.tags,up.isinfo,up.photolist';
- if ($type == 3 || $type == 4) {
- $defaultField = 'm.id,m.user_nickname,m.avatar,m.avatar_confirm_status,m.is_reg_profile,m.user_status,m.mobile,m.sex,m.is_heart,m.birthday,m.real_name,up.age,up.height,up.weight,up.company,up.occupation,up.property,up.graduate,up.education,up.photolist,up.salary,up.province,up.city,up.district,up.home_province,up.home_city,up.home_district,up.introduce,up.brief,up.family,up.hobby,up.purpose,up.cause,up.expect,up.show_company,up.show_graduate,up.married,up.isinfo,m.vip_auth,m.vip_expire,up.education_check,up.front_idcard,up.education_type,up.position_type,up.position_check,up.idcard_check,up.idcard_type,up.introduce_img,up.family_img,up.hobby_img,up.purpose_img,up.tags,up.cause_img,up.expect_img';
- } else if ($type == 2) {
- $defaultField = 'm.id,m.user_nickname,m.wechat_account,m.mobile,m.sex,m.birthday,m.real_name,up.age,up.height,up.weight,up.company,up.occupation,up.property,up.graduate,m.is_heart,up.education,up.province,up.salary,up.city,up.district,up.home_province,up.home_city,up.home_district,up.married,up.wechat_code,up.qq,up.education_check,up.position_check,up.idcard_check';
- }
- if ($type == 6) {
- unset($where['m.user_status']);
- }
- $field = $field ? $field : $defaultField;
- $info = Member::alias('m')
- ->join('user_profile up', 'up.userid=m.id', 'left')
- ->field($field)
- ->where($where)
- ->find();
- if ($info) {
- if (isset($info['avatar'])) {
- $info['avatar'] = cmf_get_image_preview_url($info['avatar']);
- }
- if (isset($info['brief'])) {
- $info['brief'] = htmlspecialchars_decode($info['brief']);
- }
- if (isset($info['sex'])) {
- $sexs = ['', '男', '女'];
- $sex = isset($info['sex']) ? $info['sex'] : 0;
- $info['sex_txt'] = isset($sexs[$sex]) ? $sexs[$sex] : '';
- }
- $info['albums'] = [];
- $photolist = isset($info['photolist']) ? $info['photolist'] : '';
- $photolist = $photolist ? explode(',', $photolist) : [];
- if ($photolist) {
- $albums = [];
- foreach ($photolist as $k => $val) {
- $albums[] = cmf_get_image_preview_url($val);
- }
- $info['albums'] = $albums;
- }
- if (isset($info['birthday'])) {
- $birthday = isset($info['birthday']) ? $info['birthday'] : 0;
- $info['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
- $info['birthday_code'] = $birthday ? date('Y-m-d', $birthday) : '';
- $info['birthday_day'] = $birthday ? date('y年', $birthday) : '';
- $year = $birthday ? date('Y', $birthday) : 0;
- if ($year) {
- $info['age'] = date('Y') - $year;
- $info['year'] = $year;
- }
- $month = date('m-d', $birthday);
- $info['start'] = getStart($month);
- }
- $info['height_txt'] = '';
- if (isset($info['height'])) {
- $height = isset($info['height']) ? intval($info['height']) : 0;
- $info['height'] = intval($height);
- $info['height_txt'] = $height ? intval($info['height']) . 'CM' : '无';
- }
- $info['weight_txt'] = '';
- if (isset($info['weight'])) {
- $weight = isset($info['weight']) ? intval($info['weight']) : 0;
- $info['weight'] = intval($weight);
- $info['weight_txt'] = $weight ? intval($info['weight']) . 'KG' : '无';
- }
- // 学历
- $info['education_txt'] = '';
- if (isset($info['education'])) {
- $educations = config('weixin.educations');
- $edu = isset($info['education']) ? $info['education'] : 0;
- $info['education_txt'] = isset($educations[$edu]) ? $educations[$edu] : '无';
- }
- // 资产
- $info['property_txt'] = '暂无';
- $property = isset($info['property']) ? $info['property'] : 0;
- if (isset($info['property'])) {
- $propertys = config('weixin.propertys');
- $info['property'] = $property;
- $info['property_txt'] = $property && isset($propertys[$property]) ? $propertys[$property] : '暂无';
- }
- // 地址
- $item['now_address'] = '';
- if (isset($info['province'])) {
- $province = isset($info['province']) ? trim($info['province']) : '';
- $city = isset($info['city']) ? trim($info['city']) : '';
- $info['now_address'] = trim($province . ' ' . $city);
- if ($type == 2) {
- $district = isset($info['district']) ? trim($info['district']) : '';
- $info['now_address'] = $info['now_address'] . ' ' . $district;
- }
- }
- $item['home_address'] = '';
- if (isset($info['home_province'])) {
- $homeProvince = isset($info['home_province']) ? trim($info['home_province']) : '';
- $homeCity = isset($info['home_city']) ? trim($info['home_city']) : '';
- $info['home_address'] = trim($homeProvince . ' ' . $homeCity);
- if ($type == 2) {
- $homeDistrict = isset($info['home_district']) ? trim($info['home_district']) : '';
- $info['home_address'] = $info['home_address'] . ' ' . $homeDistrict;
- }
- }
- // 格式化婚姻状况
- $item['married_txt'] = '';
- if (isset($info['married'])) {
- $marrieds = config('weixin.marrieds');
- $married = $info['married'] ? intval($info['married']) : 0;
- $info['married_txt'] = $married > 0 && isset($marrieds[$married - 1]) ? $marrieds[$married - 1] : '无';
- }
- // 隐藏手机号
- if ($type > 0 && isset($info['mobile'])) {
- /*if($type==2){
- $info['mobile_txt'] = $info['mobile'];
- }*/
- $info['mobile'] = $info['mobile'] ? formatStr($info['mobile']) : '';
- }
- if ($type == 4) {
- $info['introduce'] = isset($info['introduce']) ? str_replace("\n", "<br/>", $info['introduce']) : '';
- $info['family'] = isset($info['family']) ? str_replace(["\n", "\s"], ["<br/>", " "], $info['family']) : '';
- $info['hobby'] = isset($info['hobby']) ? str_replace(["\n", "\s"], ["<br/>", " "], $info['hobby']) : '';
- $info['purpose'] = isset($info['purpose']) ? str_replace(["\n", "\s"], ["<br/>", " "], $info['purpose']) : '';
- $info['cause'] = isset($info['cause']) ? str_replace(["\n", "\s"], ["<br/>", " "], $info['cause']) : '';
- $info['expect'] = isset($info['expect']) ? str_replace(["\n", "\s"], ["<br/>", " "], $info['expect']) : '';
- // VIP
- $vipAuth = isset($info['vip_auth']) ? intval($info['vip_auth']) : 0;
- $vipExpire = isset($info['vip_expire']) ? intval($info['vip_expire']) : 0;
- if ($vipAuth && $vipExpire >= time()) {
- $info['vip_auth'] = 1;
- $info['vip_expire'] = date('Y-m-d', $info['vip_expire']);
- } else {
- $info['vip_auth'] = 0;
- $info['vip_expire'] = '';
- }
- }
- if ($type == 1) {
- $accountConfig = $siteInfo = cmf_get_option('account_config');
- $logoutPay = isset($accountConfig['logout_pay']) ? floatval($accountConfig['logout_pay']) : 0;
- $info['logout_pay'] = $logoutPay > 0 ? $logoutPay : 1;
- }
- // 个性标签
- if (isset($info['tags'])) {
- $info['tags'] = $info['tags'] ? explode(',', $info['tags']) : [];
- }
- if ($type == 3 || $type == 4) {
- if (isset($info['introduce_img'])) {
- $info['introduce_img_preview'] = $info['introduce_img'] ? cmf_get_image_preview_url($info['introduce_img']) : '';
- }
- if (isset($info['family_img'])) {
- $info['family_img_preview'] = $info['family_img'] ? cmf_get_image_preview_url($info['family_img']) : '';
- }
- if (isset($info['hobby_img'])) {
- $info['hobby_img_preview'] = $info['hobby_img'] ? cmf_get_image_preview_url($info['hobby_img']) : '';
- }
- if (isset($info['purpose_img'])) {
- $info['purpose_img_preview'] = $info['purpose_img'] ? cmf_get_image_preview_url($info['purpose_img']) : '';
- }
- if (isset($info['cause_img'])) {
- $info['cause_img_preview'] = $info['cause_img'] ? cmf_get_image_preview_url($info['cause_img']) : '';
- }
- if (isset($info['expect_img'])) {
- $info['expect_img_preview'] = $info['expect_img'] ? cmf_get_image_preview_url($info['expect_img']) : '';
- }
- }
- }
- return $info ? $info->toArray() : [];
- }
- /**
- * 记录访问记录
- * @param $userId
- * @param $accessUid
- * @return false|int|string
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public static function makeUserAccess($userId, $accessUid)
- {
- if (($userId == $accessUid) || empty($userId) || empty($accessUid)) {
- return false;
- }
- $lockCacheKey = "caches:access:{$userId}_{$accessUid}";
- if (PRedis::get($lockCacheKey)) {
- return false;
- }
- $checkData = UserAccess::where(['user_id' => $userId, 'access_uid' => $accessUid, 'status' => 1])
- ->field('id,created_at,status')
- ->find();
- $checkData = $checkData ? $checkData->toArray() : [];
- $timeAt = isset($checkData['created_at']) ? $checkData['created_at'] : '';
- if ($checkData && $timeAt && $timeAt >= date('Y-m-d H:i:s', time() - rand(10, 20))) {
- PRedis::set($lockCacheKey, $checkData, rand(10, 20));
- return false;
- }
- $data = ['user_id' => $userId, 'access_uid' => $accessUid, 'created_at' => date('Y-m-d H:i:s')];
- if (empty($checkData)) {
- return UserAccess::insertGetId($data);
- } else {
- return UserAccess::where(['user_id' => $userId, 'access_uid' => $accessUid])->update(['created_at' => date('Y-m-d H:i:s')]);
- }
- }
- /**
- * 获取用户访问记录
- * @param $userId 被访问用户
- * @param $pageSize
- * @return array
- */
- public static function getUserAccess($userId, $pageSize)
- {
- $dataList = UserAccess::alias('ua')
- ->join('user u', 'u.id=ua.user_id', 'left')
- ->join('user au', 'au.id=ua.access_uid', 'left')
- ->join('user_profile up', 'up.userid=ua.access_uid', 'left')
- ->field('ua.id,ua.user_id,ua.access_uid,ua.created_at,ua.is_lock,ua.status,au.avatar,au.user_nickname,au.user_login,au.birthday,u.birthday as my_birthday,au.sex,up.city')
- ->where(['ua.user_id' => $userId, 'ua.status' => 1, 'au.user_status' => 1])
- ->order('ua.created_at desc,ua.id desc')
- ->paginate($pageSize)
- ->each(function ($item, $key) {
- $birthday = $item['birthday'];
- $myBirthday = $item['my_birthday'];
- $month = date('m-d', $birthday);
- $myMonth = date('m-d', $myBirthday);
- $start = getStart($month);
- $mstart = getStart($myMonth);
- $item['birtyday'] = $month;
- $item['my_birtyday'] = $myMonth;
- $item['start'] = $start == $mstart ? '同是' . $start : $start;
- $item['mstart'] = $mstart;
- $year = date('y年', $birthday);
- $item['year'] = $year;
- $item['city'] = str_replace('市', '', $item['city']);
- $item['created_at_text'] = format_time(strtotime($item['created_at']));
- $nickname = $item['user_nickname'] ? $item['user_nickname'] : $item['user_login'];
- $item['user_nickname'] = $item['is_lock'] ? formatName($nickname) : $nickname;
- if (isset($item['avatar'])) {
- $item['avatar'] = cmf_get_image_preview_url($item['avatar']);
- }
- });
- $dataList = $dataList ? $dataList->toArray() : ['data' => []];
- // 获取今日访问
- $dataList['todayCount'] = UserAccess::where(['user_id' => $userId, 'status' => 1])
- ->where('created_at', 'like', "%" . date('Y-m-d') . "%")
- ->count('id');
- return $dataList;
- }
- /**
- * 获取访问用户数
- * @param $userId
- * @return float|int|string
- */
- public static function getAccessCount($userId)
- {
- return UserAccess::where(['user_id' => $userId, 'status' => 1])
- ->count('id');
- }
- /**
- * 获取推荐会员数量
- * @param $userId
- * @return float|string
- */
- public static function getInviteCount($userId)
- {
- return Member::where(['parent_id' => $userId, 'user_type' => 2, 'user_status' => 1])->count('id');
- }
- /**
- * 验证获取验证用户信息
- * @param $account
- * @return array|bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function checkUserInfo($account)
- {
- $cacheKey = "cache:users:check_{$account}";
- $info = PRedis::get($cacheKey);
- if ($info) {
- return $info;
- }
- $field = 'id,openid,user_type,agent_type,agent_status,user_status,freezing_choose,is_reg_profile';
- $info = self::where(['openid|id' => $account, 'user_type' => 2])->field($field)->order('user_status desc,id desc')->find();
- $info = $info ? $info->toArray() : [];
- if ($info) {
- PRedis::set($cacheKey, $info, 5);
- }
- return $info;
- }
- /**
- * 获取邀请的分销用户信息
- * @param $userId 当前用户ID
- * @param string $field 返回字段,连表u-当前用户,uu连表用户
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getInviteInfo($userId, $field = '')
- {
- $field = $field ? $field : 'u.id,uu.id as invite_id,uu.openid,uu.parent_id';
- $info = Member::alias('u')
- ->leftJoin('user uu', 'uu.id=u.parent_id')
- ->where(['u.id' => $userId, 'uu.user_type' => 2, 'uu.agent_type' => 1, 'uu.agent_status' => 1])
- ->field($field)
- ->find();
- return $info ? $info->toArray() : [];
- }
- /**
- * 充值记录
- * @param $params
- * @param int $pageSize
- * @return mixed
- */
- public static function getRechargeLog($params, $pageSize = 15)
- {
- $where = [];
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId) {
- $where['user_id'] = $userId;
- }
- $cwhere = $where;
- $cwhere['is_read'] = 2;
- $count = db('user_recharge_log')
- ->where($cwhere)
- ->where('status', 'gt', 0)
- ->count('id');
- if ($count) {
- db('user_recharge_log')
- ->where($cwhere)
- ->where('status', 'gt', 0)
- ->update(['is_read' => 1, 'updated_at' => date('Y-m-d H:i:s')]);
- }
- $dataList = db('user_recharge_log')
- ->where($where)
- ->field('id,type,order_sn,pay_type,money,give_num,pay_money,balance,remark,created_at,status')
- ->where('status', 'gt', 0)
- ->order("created_at desc")
- ->paginate($pageSize)
- ->each(function ($item, $k) {
- $item['format_time'] = date('Y年m月d日 H点i分', strtotime($item['created_at']));
- $item['money'] = in_array($item['type'], [1, 4, 5]) ? intval($item['money']) : $item['money'];
- return $item;
- });
- return $dataList ? $dataList->toArray() : [];
- }
- /**
- * 充值记录数
- * @param $userId
- * @param int $type
- * @return mixed
- */
- public static function getRechargeCount($userId, $type = 0)
- {
- $where = ['user_id' => $userId, 'is_read' => 2];
- if ($type) {
- $where['type'] = $type;
- }
- return db('user_recharge_log')
- ->where($where)
- ->where('status', 'gt', 0)
- ->count('id');
- }
- /**
- * 推荐用户
- * @param $params 参数
- * @param $page 分页
- * @param $pageSize 分页大小
- * @param string $field 字段
- * @return array|null|\PDOStatement|string|Model
- */
- public static function getRecommendList($params, $pageSize, $field = '')
- {
- $page = input('page', 1);
- $cacheKey = "cache:recommends:index_" . $page . '_:' . md5(json_encode($params) . $pageSize . $field);
- $dataList = PRedis::get($cacheKey);
- if ($dataList) {
- return $dataList;
- }
- $where = ['m.is_tuijian' => 1, 'is_heart' => 1, 'm.is_reg_profile' => 1, 'm.user_status' => 1, 'm.user_type' => 2];
- // 匹配当前用户信息:性别
- $sex = 0;
- $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
- if ($userId) {
- $sex = Member::where(['id' => $userId])->value('sex');
- if ($sex > 0) {
- $sex = $sex == 1 ? 2 : 1;
- }
- }
- $field = $field ? $field : 'm.id,m.user_nickname,m.real_name,m.lat,m.lng,m.address,m.birthday,m.avatar,m.create_time,m.sex,up.graduate,up.education,up.height,up.weight,up.company,up.occupation,up.province,up.city,up.show_graduate,m.vip_auth,m.is_heart,m.vip_expire,up.show_company,up.idcard_check,up.education_check,up.position_check,up.province,up.home_city,up.photolist,up.introduce';
- // 距离
- $info = Member::where(['id' => $userId])->field('lat,lng')->find();
- $lat = isset($params['lat']) ? $params['lat'] : 0.00;
- $lng = isset($params['lng']) ? $params['lng'] : 0.00;
- $lat = $lat ? $lat : (isset($info['lat']) ? $info['lat'] : 0);
- $lng = $lng ? $lng : (isset($info['lng']) ? $info['lng'] : 0);
- if ($lat && $lng) {
- $field .= ",ROUND(
- 6378.138 * 2 * ASIN(
- SQRT(
- POW(
- SIN(
- (
- {$lat} * PI() / 180 - m.`lat` * PI() / 180
- ) / 2
- ),
- 2
- ) + COS({$lat} * PI() / 180) * COS(m.`lat` * PI() / 180) * POW(
- SIN(
- (
- {$lng} * PI() / 180 - m.`lng` * PI() / 180
- ) / 2
- ),
- 2
- )
- )
- ) * 1000
- ) AS distance";
- }
- $dataList = Member::alias('m')
- ->join('user_profile up', 'up.userid=m.id', 'left')
- ->field($field)
- ->where($where)
- ->where(function ($query) use ($sex, $params) {
- // 性别
- if ($sex > 0) {
- $query->where('m.sex', 'in', [0, $sex]);
- }
- // 年龄
- $age = isset($params['age']) ? trim($params['age']) : '';
- if ($age) {
- $ages = explode('~', $age);
- $age1 = isset($ages[0]) ? intval($ages[0]) : 0;
- $age2 = isset($ages[1]) ? intval($ages[1]) : 0;
- if ($age2) {
- $query->where('up.age', '>=', $age1)->where('up.age', '<=', $age2);
- } else if ($age1) {
- $query->where('up.age', '>=', $age1);
- }
- }
- // 身高
- $height = isset($params['height']) ? trim($params['height']) : '';
- if ($height) {
- $heights = explode('~', $height);
- $height1 = isset($heights[0]) ? intval($heights[0]) : 0;
- $height2 = isset($heights[1]) ? intval($heights[1]) : 0;
- if ($height2) {
- $query->where('up.height', '>=', $height1)->where('up.height', '<=', $height2);
- } else if ($height1) {
- $query->where('up.height', '>=', $height1);
- }
- }
- // 学历
- $education = isset($params['education']) ? intval($params['education']) : 0;
- if ($education > 0) {
- $op = $education == 3 ? '>=' : '=';
- $query->where('up.education', $op, $education);
- }
- // 婚姻状况
- $married = isset($params['married']) ? intval($params['married']) : 0;
- if ($married > 0) {
- $query->where('up.married', '=', $married);
- }
- return $query;
- })
- ->where('m.id', '>', 0)
- ->where('m.id', 'not in', $userId)
- ->order('m.tuijian_time desc,m.id')
- ->paginate($pageSize)
- ->each(function ($item, $k) {
- $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
- $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
- $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
- $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
- $height = isset($item['height']) ? intval($item['height']) : 0;
- $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '';
- $weight = isset($item['weight']) ? intval($item['weight']) : 0;
- $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '';
- // VIP
- $vipAuth = isset($item['vip_auth']) ? intval($item['vip_auth']) : 0;
- $vipExpire = isset($item['vip_expire']) ? intval($item['vip_expire']) : 0;
- if ($vipAuth && $vipExpire >= time()) {
- $item['vip_auth'] = 1;
- $item['vip_expire'] = date('Y-m-d', $item['vip_expire']);
- } else {
- $item['vip_auth'] = 0;
- $item['vip_expire'] = '';
- }
- // 学历
- $educations = config('weixin.educations');
- $edu = isset($item['education']) ? $item['education'] : 0;
- $item['education_txt'] = $edu && isset($educations[$edu]) ? $educations[$edu] : '';
- // 地址
- $province = isset($item['province']) ? trim($item['province']) : '';
- $city = isset($item['city']) ? trim($item['city']) : '';
- $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
- $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
- $item['now_address'] = trim($province . ' ' . $city);
- $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
- $albums = [];
- $photolist = isset($item['photolist']) ? $item['photolist'] : '';
- $photolist = $photolist ? explode(',', $photolist) : [];
- if ($photolist) {
- foreach ($photolist as $k => $val) {
- $albums[] = cmf_get_image_preview_url($val);
- }
- }
- $item['pic_count'] = count($albums);
- // 坐标距离
- $item['distance'] = isset($item['distance']) ? $item['distance'] : '';
- if ($item['lat'] <= 0 || $item['lng'] <= 0) {
- $item['distance'] = '';
- } else {
- $item['distance'] = $item['distance'] >= 1000 ? round($item['distance'] / 1000, 2) . 'km' : ($item['distance'] ? $item['distance'] . 'm' : '');
- }
- // 地址
- $address = isset($item['address']) ? $item['address'] : '';
- $addressData = $address ? explode(',', $address) : [];
- $item['district'] = isset($addressData[2]) ? $addressData[2] : '';
- return $item;
- });
- $dataList = $dataList ? $dataList->toArray() : [];
- //PRedis::set('recommends:query:' . $userId, Db::name('user')->getLastSql(), 600);
- PRedis::set($cacheKey, $dataList, 10);
- return $dataList;
- }
- /**
- * 获取首页推荐列表
- * @param $params
- * @param $pageSize
- * @param string $field
- * @return array|bool
- * @throws \think\exception\DbException
- */
- public static function getTopList($params, $pageSize, $field = '')
- {
- $page = input('page', 1);
- $cacheKey = "cache:topList:index_" . $page . '_:' . md5(json_encode($params) . $pageSize . $field);
- $dataList = PRedis::get($cacheKey);
- if ($dataList) {
- return $dataList;
- }
- $sex = 0;
- $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
- if ($userId) {
- $sex = Member::where(['id' => $userId])->value('sex');
- if ($sex > 0) {
- $sex = $sex == 1 ? 2 : 1;
- }
- }
- $field = $field ? $field : 'm.id,m.user_nickname,m.real_name,m.address,m.birthday,m.avatar,m.sex,up.graduate,up.education,up.height,up.weight,up.company,up.occupation,up.province,up.city,up.show_graduate,m.vip_auth,m.is_heart,m.vip_expire,up.show_company,up.idcard_check,up.education_check,up.position_check,up.province,up.home_city,up.photolist,up.introduce,m.is_top,m.top_expire';
- $where = ['m.is_top' => 1, 'm.is_heart' => 1, 'm.is_reg_profile' => 1, 'm.user_status' => 1, 'm.user_type' => 2];
- $dataList = Member::alias('m')
- ->join('user_profile up', 'up.userid=m.id', 'left')
- ->field($field)
- ->where($where)
- // ->where('m.sex', 'in', [0, $sex])
- ->where('m.top_expire', '>=', date('Y-m-d H:i:s'))
- ->where('m.id', '>', 0)
- // ->where('m.id', 'not in', $userId)
- ->order('m.top_expire desc,m.id')
- ->paginate($pageSize)
- ->each(function ($item, $k) {
- $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
- $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
- $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
- $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
- return $item;
- });
- $dataList = $dataList ? $dataList->toArray() : [];
- PRedis::set($cacheKey, $dataList, rand(3, 5));
- return $dataList;
- }
- /**
- * 怦然心动推荐用户
- * @param $userId 当前用户
- * @param string $field 字段
- * @return array|null|\PDOStatement|string|Model
- */
- public static function getHeartList($userId, $field = '', $refresh = false)
- {
- // 获取已经推荐的有效数据
- $updated = false;
- $dataList = [];
- $cacheKey = "hearts:recommend:" . $userId;
- $recommendIds = PRedis::get($cacheKey);
- $where = ['m.is_heart' => 1, 'm.is_reg_profile' => 1, 'm.user_status' => 1, 'm.user_type' => 2];
- $heartAt = Member::where(['id' => $userId])->value('heart_recommend_at');
- $heartAt = $heartAt ? date('Y-m-d', strtotime($heartAt)) : '';
- $ids = [];
- if (!PRedis::exists($cacheKey) || $refresh) {
- // 验证今天是否推荐过
- /*$heartAt = Member::where(['id' => $userId])->value('heart_recommend_at');
- $heartAt = $heartAt ? date('Y-m-d', strtotime($heartAt)) : '';
- if ($heartAt == date('Y-m-d')) {
- return true;
- }*/
- // 获取推荐的人数设置
- $updated = true;
- $siteInfo = $siteInfo = cmf_get_site_info();
- $ageGap = isset($siteInfo['age_gap']) ? intval($siteInfo['age_gap']) : 0;
- $ageGap = $ageGap ? $ageGap : 0;
- $recommendNum = isset($siteInfo['recommend_num']) ? intval($siteInfo['recommend_num']) : 0;
- $recommendNum = $recommendNum ? $recommendNum : 1;
- // 用户性别
- $memberInfo = Member::getHomeInfo($userId, 'm.sex,up.age,up.city,m.vip_auth,m.vip_expire,up.home_province');
- $sex = isset($memberInfo['sex']) ? intval($memberInfo['sex']) : 0;
- if ($sex > 0) {
- $sex = $sex == 1 ? 2 : 1;
- }
- // 调整VIP推荐人数
- $isVip = false;
- $vipRecommendNum = isset($siteInfo['vip_recommend_num']) ? intval($siteInfo['vip_recommend_num']) : 0;
- $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0;
- $vipExpire = isset($memberInfo['vip_expire']) ? intval($memberInfo['vip_expire']) : 0;
- if ($vipAuth && $vipExpire >= time() && $vipRecommendNum > 0) {
- $isVip = true;
- $recommendNum = $vipRecommendNum;
- }
- $conditions = Member::getMemberConditions($userId);
- //var_dump($conditions);
- $hasMatchData = Predis::get("hearts:hasMeatch:{$userId}");
- $ids = isset($hasMatchData['ids']) ? $hasMatchData['ids'] : '';
- $ids = $ids ? explode(',', $ids) : [];
- $hasExpire = isset($hasMatchData['expire']) ? $hasMatchData['expire'] : 0;
- /*if (time() > $hasExpire || !$isVip) {
- $ids = [];
- }*/
- // vip会员只推荐身份认证的
- if ($isVip) {
- $where['up.idcard_check'] = 2;
- }
- $recommendIds = Member::alias('m')
- ->join('user_profile up', 'up.userid=m.id', 'left')
- ->where($where)
- ->where(function ($query) use ($sex) {
- return $query->where('m.sex', 'in', [0, $sex]);
- })
- ->where(function ($query) use ($ids) {
- // 过滤一星期内已经推荐过的
- if ($ids) {
- return $query->where('m.id', 'not in', $ids);
- } else {
- return $query->where('m.id', '>', 0);
- }
- })
- ->where(function ($query) use ($conditions, $memberInfo, $ageGap) {
- $querys = $query;
- // 年龄
- $year = isset($conditions['year']) ? trim($conditions['year']) : '';
- if ($year) {
- $year = explode('~', $year);
- $data1 = isset($year[0]) ? intval($year[0]) : 0;
- $data2 = isset($year[1]) ? intval($year[1]) : 0;
- if ($data2) {
- $data1 = $data1 ? strtotime($data1 . '-01-01') : 0;
- $data2 = $data2 ? strtotime(($data2 + 1) . '-01-01') : 0;
- $querys = $querys->where('m.birthday', '>=', $data1 . '')->where('m.birthday', '<=', $data2);
- } else if ($data1) {
- $data1 = $data1 ? strtotime($data1 . '-01-01') : 0;
- $querys = $querys->where('m.birthday', '>=', $data1);
- }
- }
- // 身高
- $height = isset($conditions['height']) ? trim($conditions['height']) : '';
- if ($height) {
- $height = explode('~', $height);
- $data1 = isset($height[0]) ? intval($height[0]) : 0;
- $data2 = isset($height[1]) ? intval($height[1]) : 0;
- if ($data2) {
- $querys = $querys->where('up.height', '>=', $data1)->where('up.height', '<=', $data2);
- } else if ($data1) {
- $querys = $querys->where('up.height', '>=', $data1);
- }
- }
- // 体重
- $weight = isset($conditions['weight']) ? intval($conditions['weight']) : 0;
- if ($weight) {
- $dataArr = config('weixin.weights');
- $datas = isset($dataArr[$weight]) ? $dataArr[$weight] : [];
- $data1 = isset($datas['value1']) ? $datas['value1'] : 0;
- $data2 = isset($datas['value2']) ? $datas['value2'] : 0;
- if ($data2) {
- $querys = $querys->where('up.weight', '>=', $data1)->where('up.weight', '<=', $data2);
- } else if ($data1) {
- $querys = $querys->where('up.weight', '>=', $data1);
- }
- }
- // 收入
- $salary = isset($conditions['salary']) ? intval($conditions['salary']) : 0;
- if ($salary) {
- $dataArr = config('weixin.salarys');
- $datas = isset($dataArr[$salary]) ? $dataArr[$salary] : [];
- $data1 = isset($datas['value']) ? $datas['value'] : 0;
- $data2 = isset($datas['value2']) ? $datas['value2'] : 0;
- if ($data2) {
- $querys = $querys->where('up.salary', '>=', $data1)->where('up.salary', '<=', $data2);
- } else if ($data1) {
- $querys = $querys->where('up.salary', '=', $data1);
- }
- }
- // 所在城市
- $city = isset($memberInfo['city']) ? trim($memberInfo['city']) : '';
- $cityValue = isset($conditions['city']) ? trim($conditions['city']) : '';
- if ($cityValue > 0 && $city) {
- $querys = $querys->where('up.city', '=', $city);
- }
- // 家乡省市:是否同省
- $homeProvince = isset($memberInfo['home_province']) ? trim($memberInfo['home_province']) : '';
- $homeProvinceValue = isset($conditions['home_province']) ? trim($conditions['home_province']) : '';
- if ($homeProvinceValue > 0 && $homeProvince) {
- $querys = $querys->where('up.home_province', '=', $homeProvince);
- }
- // 学历
- $education = isset($conditions['education']) ? intval($conditions['education']) : 0;
- if ($education > 0) {
- // 硕士及以上或按学历匹配
- $op = $education == 3 ? '>=' : '=';
- $querys = $querys->where('up.education', $op, $education);
- }
- // 婚姻状态
- $married = isset($conditions['married']) ? intval($conditions['married']) : 0;
- if ($married > 0) {
- $querys = $querys->where('up.married', '>=', $married);
- }
- return $querys;
- })
- ->orderRaw('rand()')
- ->limit($recommendNum)
- ->column('m.id');
- PRedis::set('hearts:query:' . $userId, ['where' => $where, 'ids' => $ids, 'query' => Member::getLastSql()], 24 * 3600);
- if (empty($recommendIds)) {
- $recommendIds = Member::alias('m')
- ->join('user_profile up', 'up.userid=m.id', 'left')
- ->where($where)
- ->where(function ($query) use ($sex) {
- return $query->where('m.sex', 'in', [0, $sex]);
- })
- ->where(function ($query) use ($ids) {
- // 过滤一星期内已经推荐过的
- if ($ids) {
- return $query->where('m.id', 'not in', $ids);
- } else {
- return $query->where('m.id', '>', 0);
- }
- })
- ->where(function ($query) use ($conditions, $memberInfo, $ageGap) {
- $querys = $query;
- // 年龄
- $year = isset($conditions['year']) ? trim($conditions['year']) : '';
- if ($year) {
- $year = explode('~', $year);
- $data1 = isset($year[0]) ? intval($year[0]) : 0;
- $data2 = isset($year[1]) ? intval($year[1]) : 0;
- if ($data2) {
- $data1 = $data1 ? strtotime($data1 . '-01-01') : 0;
- $data2 = $data2 ? strtotime(($data2 + 1) . '-01-01') : 0;
- $querys = $querys->where('m.birthday', '>=', $data1 . '')->where('m.birthday', '<=', $data2);
- } else if ($data1) {
- $data1 = $data1 ? strtotime($data1 . '-01-01') : 0;
- $querys = $querys->where('m.birthday', '>=', $data1);
- }
- }
- // 身高
- $height = isset($conditions['height']) ? trim($conditions['height']) : '';
- if ($height) {
- $height = explode('~', $height);
- $data1 = isset($height[0]) ? intval($height[0]) : 0;
- $data2 = isset($height[1]) ? intval($height[1]) : 0;
- if ($data2) {
- $querys = $querys->where('up.height', '>=', $data1)->where('up.height', '<=', $data2);
- } else if ($data1) {
- $querys = $querys->where('up.height', '>=', $data1);
- }
- }
- // 收入
- $salary = isset($conditions['salary']) ? intval($conditions['salary']) : 0;
- if ($height <= 0 && $salary) {
- $dataArr = config('weixin.salarys');
- $datas = isset($dataArr[$salary]) ? $dataArr[$salary] : [];
- $data1 = isset($datas['value']) ? $datas['value'] : 0;
- $data2 = isset($datas['value2']) ? $datas['value2'] : 0;
- if ($data2) {
- $querys = $querys->where('up.salary', '>=', $data1)->where('up.salary', '<=', $data2);
- } else if ($data1) {
- $querys = $querys->where('up.salary', '=', $data1);
- }
- }
- // 所在城市
- $city = isset($memberInfo['city']) ? trim($memberInfo['city']) : '';
- $cityValue = isset($conditions['city']) ? trim($conditions['city']) : '';
- if ($cityValue > 0 && $city) {
- $querys = $querys->where('up.city', '=', $city);
- }
- // 婚姻状态
- $married = isset($conditions['married']) ? intval($conditions['married']) : 0;
- if ($married > 0) {
- $querys = $querys->where('up.married', '>=', $married);
- }
- // 学历
- $education = isset($conditions['education']) ? intval($conditions['education']) : 0;
- if ($education > 0) {
- if ($married > 0 && $education == 3) {
- $querys = $querys->where('up.education', '>=', 1);
- } else if ($married <= 0) {
- // 硕士及以上或按学历匹配
- $op = $education == 3 ? '>=' : '=';
- $querys = $querys->where('up.education', $op, $education);
- }
- }
- return $querys;
- })
- ->orderRaw('rand()')
- ->limit($recommendNum)
- ->column('m.id');
- }
- if ($recommendIds) {
- // 更新推荐日期
- Member::where(['id' => $userId])->update(['heart_recommend_at' => date('Y-m-d H:i:s')]);
- }
- $expire = isset($siteInfo['recommend_expire']) ? intval($siteInfo['recommend_expire']) : 0;
- $expire = $expire ? $expire : 24;
- if ($recommendIds) {
- PRedis::set($cacheKey, $recommendIds, $expire * 3600);
- }
- }
- // 获取数据
- if ($recommendIds) {
- $field = $field ? $field : 'm.id,m.user_nickname,m.real_name,m.birthday,m.lat,m.lng,m.address,m.avatar,m.create_time,m.sex,m.vip_auth,m.vip_expire,up.graduate,up.education,up.height,up.weight,up.company,up.occupation,up.province,up.city,up.show_graduate,up.show_company,up.home_province,up.home_city,up.photolist,up.introduce,up.idcard_check,up.education_check,up.position_check,up.introduce';
- // 距离
- $info = Member::where(['id' => $userId])->field('lat,lng')->find();
- $lat = input('lat', 0.00);
- $lng = input('lng', 0.00);
- $lat = $lat ? $lat : (isset($info['lat']) ? $info['lat'] : 0);
- $lng = $lng ? $lng : (isset($info['lng']) ? $info['lng'] : 0);
- if ($lat && $lng) {
- $field .= ",ROUND(
- 6378.138 * 2 * ASIN(
- SQRT(
- POW(
- SIN(
- (
- {$lat} * PI() / 180 - m.`lat` * PI() / 180
- ) / 2
- ),
- 2
- ) + COS({$lat} * PI() / 180) * COS(m.`lat` * PI() / 180) * POW(
- SIN(
- (
- {$lng} * PI() / 180 - m.`lng` * PI() / 180
- ) / 2
- ),
- 2
- )
- )
- ) * 1000
- ) AS distance";
- }
- $dataList = Member::alias('m')
- ->join('user_profile up', 'up.userid=m.id', 'left')
- ->where('m.id', 'in', implode(',', $recommendIds))
- ->where(['m.is_heart' => 1, 'm.user_type' => 2, 'm.user_status' => 1])
- ->field($field)
- ->orderField('m.id', $recommendIds)
- ->paginate(count($recommendIds))
- ->each(function ($item, $k) {
- $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
- $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
- $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
- $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
- $height = isset($item['height']) ? intval($item['height']) : 0;
- $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '';
- $weight = isset($item['weight']) ? intval($item['weight']) : 0;
- $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '';
- // VIP
- $vipAuth = isset($item['vip_auth']) ? intval($item['vip_auth']) : 0;
- $vipExpire = isset($item['vip_expire']) ? intval($item['vip_expire']) : 0;
- if ($vipAuth && $vipExpire >= time()) {
- $item['vip_auth'] = 1;
- $item['vip_expire'] = date('Y-m-d', $item['vip_expire']);
- } else {
- $item['vip_auth'] = 0;
- $item['vip_expire'] = '';
- }
- // 学历
- $educations = config('weixin.educations');
- $edu = isset($item['education']) ? $item['education'] : 0;
- $item['education_txt'] = $edu && isset($educations[$edu]) ? $educations[$edu] : '';
- // 地址
- $province = isset($item['province']) ? trim($item['province']) : '';
- $city = isset($item['city']) ? trim($item['city']) : '';
- $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
- $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
- $item['now_address'] = trim($province . ' ' . $city);
- $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
- $albums = [];
- $photolist = isset($item['photolist']) ? $item['photolist'] : '';
- $photolist = $photolist ? explode(',', $photolist) : [];
- if ($photolist) {
- foreach ($photolist as $k => $val) {
- $albums[] = cmf_get_image_preview_url($val);
- }
- }
- $item['pic_count'] = count($albums);
- // 坐标距离
- $item['distance'] = isset($item['distance']) ? $item['distance'] : '';
- if ($item['lat'] <= 0 || $item['lng'] <= 0) {
- $item['distance'] = '';
- } else {
- $item['distance'] = $item['distance'] >= 1000 ? round($item['distance'] / 1000, 2) . 'km' : ($item['distance'] ? $item['distance'] . 'm' : '');
- }
- // 地址
- $address = isset($item['address']) ? $item['address'] : '';
- $addressData = $address ? explode(',', $address) : [];
- $item['district'] = isset($addressData[2]) ? $addressData[2] : '';
- return $item;
- });
- // 过滤重复
- if ($updated && $recommendIds) {
- if ($ids) {
- $newIds = array_merge($ids, $recommendIds);
- Predis::set("hearts:hasMeatch:{$userId}", ['ids' => implode(',', $newIds), 'expire' => $hasExpire], 30 * 24 * 3600);
- } else {
- Predis::set("hearts:hasMeatch:{$userId}", ['ids' => implode(',', $recommendIds), 'expire' => time() + 3 * 24 * 3600], 30 * 24 * 3600);
- }
- }
- PRedis::set('hearts:results:' . $userId . '_' . date('Ymd'), ['datalist' => $dataList, 'sql' => Member::getLastSql()], 3 * 3600 * 24);
- $dataList = $dataList ? $dataList->toArray() : [];
- }
- return $dataList;
- }
- /**
- * 获取用户匹配条件
- * @param $userId 当前用户
- * @return array|mixed
- */
- public static function getMemberConditions($userId)
- {
- $conditions = db('user_conditions')->where(['user_id' => $userId])->value('conditions');
- $conditions = $conditions ? json_decode($conditions, true) : [];
- return $conditions;
- }
- /**
- * 设置用户推荐条件,怦然心动条件
- * @param $userId 当前用户ID
- * @param $params 条件参数:age、height、weight、city、home_province、education、salary
- * @return int|string
- */
- public static function setMemberConditions($userId, $params)
- {
- // 条件数据
- $condition = [
- 'age' => isset($params['age']) ? intval($params['age']) : 0,
- 'height' => isset($params['height']) ? trim($params['height']) : '',
- 'weight' => isset($params['weight']) ? intval($params['weight']) : 0,
- 'city' => isset($params['city']) ? intval($params['city']) : 0,
- // 'home_province' => isset($params['home_province']) ? intval($params['home_province']) : 0,
- 'education' => isset($params['education']) ? intval($params['education']) : 0,
- 'salary' => isset($params['salary']) ? intval($params['salary']) : 0,
- 'married' => isset($params['married']) ? intval($params['married']) : 0,
- 'year' => isset($params['year']) ? trim($params['year']) : '',
- ];
- $data = [
- 'user_id' => $userId,
- 'conditions' => json_encode($condition, 256),
- 'updated_at' => date('Y-m-d H:i:s'),
- 'status' => 1,
- ];
- // 验证并添加或保存
- $check = db('user_conditions')->where(['user_id' => $userId])->value('id');
- if ($check) {
- $res = db('user_conditions')->where(['user_id' => $userId])->update($data);
- } else {
- $res = db('user_conditions')->insertGetId($data);
- }
- return $res;
- }
- /**
- * 用户关注
- * @param $userId 当前用户
- * @param $collectUid 关注的用户
- * @param int $opType
- * @return int
- */
- public static function collect($userId, $collectUid, $opType = 1)
- {
- if ($userId == $collectUid) {
- return 2114;
- }
- if (!Member::where(['id' => $collectUid])->value('id')) {
- return 2104;
- }
- // 验证性别
- $contactSex = Member::where(['id' => $collectUid])->value('sex');
- $sex = Member::where(['id' => $userId])->value('sex');
- if (!$sex || ($sex == $contactSex)) {
- return 2135;
- }
- // 验证数据
- $collectData = UserCollect::where(['user_id' => $userId, 'source_id' => $collectUid, 'type' => 1])
- ->field('id,status')
- ->find();
- // 关注
- $collectId = isset($collectData['id']) ? $collectData['id'] : 0;
- $status = isset($collectData['status']) ? $collectData['status'] : 0;
- if ($opType == 1) {
- if ($collectId && $status == 1) {
- return 2105;
- }
- // 若收藏时间已失效更新时间
- $collectExpire = Member::where(['id' => $userId])->value('collect_expire');
- if ($collectExpire - time() <= 0) {
- $siteInfo = $siteInfo = cmf_get_site_info();
- $collectTime = isset($siteInfo['collect_time']) ? intval($siteInfo['collect_time']) : 0;
- $collectTime = $collectTime ? $collectTime : 6;
- Member::saveData(['id' => $userId], ['collect_expire' => time() + $collectTime * 3600]);
- $memberInfo = Member::where(['id' => $userId])->field('collect_expire,vip_auth,vip_expire')->find();
- $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0;
- $vipExpire = isset($memberInfo['vip_expire']) ? $memberInfo['vip_expire'] : 0;
- if (!$vipAuth || $vipExpire < time()) {
- UserCollect::where(['user_id' => $userId])
- ->update(['status' => 2, 'updated_at' => date('Y-m-d H:i:s')]);
- }
- }
- $data = [
- 'user_id' => $userId,
- 'source_id' => $collectUid,
- 'type' => 1,
- 'created_at' => date('Y-m-d H:i:s'),
- 'status' => 1,
- ];
- if ($collectId) {
- if (!UserCollect::where(['user_id' => $userId, 'id' => $collectId])
- ->update(['status' => 1, 'updated_at' => date('Y-m-d H:i:s')])) {
- return 2107;
- }
- } else {
- if (!UserCollect::insertGetId($data)) {
- return 2107;
- }
- }
- // 操作日志
- UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => "收藏用户:{$collectUid}"]);
- // 通知
- $openid = Member::where(['id' => $collectUid])->value('openid');
- $memberInfo = Member::where(['id' => $userId])->field('openid,sex,user_nickname')->find();
- $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : '未公开';
- $sex = isset($memberInfo['sex']) ? $memberInfo['sex'] : 0;
- $sex = $sex == 1 ? '帅哥' : '美女';
- $dateTime = date('Y.m.d H:i');
- if ($openid) {
- $params = [
- 'title' => "有个{$sex}查看了你的名片并且关注了你\n\n关注时间:\t{$dateTime}",
- // 'title' => "有一位{$sex}申请加您的微信并给您留言了!\n\n申请时间:\t{$dateTime}\n\n收到留言:\t{$remark}",
- 'remark' => "点击查看(如果不想再收到该类型消息,可本公众号回复“隐身设置”进入链接设置为隐身)",
- 'type' => 'contact',
- 'keywords' => [
- 'keyword1' => [
- 'value' => $nickname . '(昵称)',
- 'color' => '#173177',
- ],
- 'keyword2' => [
- 'value' => '隐私信息不显示',
- 'color' => '#173177',
- ],
- 'keyword3' => [
- 'value' => '消息提醒',
- 'color' => '#173177',
- ],
- ],
- 'url' => url("/weixin/member/collect?type=2", [], '', true),
- ];
- PRedis::set('messages:collect:' . $userId . '_' . $collectUid . '_' . $openid, ['data' => $data, 'params' => $params], 600);
- Wechat::sendTplMsg($openid, $params, false);
- }
- return true;
- } // 取消关注
- else if ($opType == 2) {
- if (!$collectId) {
- return 1003;
- }
- if ($status != 1) {
- return 2115;
- }
- if (!UserCollect::where(['user_id' => $userId, 'source_id' => $collectUid, 'type' => 1])
- ->update(['status' => 2, 'updated_at' => date('Y-m-d H:i:s')])) {
- return 2109;
- }
- // 操作日志
- UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => "取消收藏用户:{$collectUid}"]);
- return true;
- }
- return 1009;
- }
- /**
- * 保存信息
- * @param $where
- * @param $data
- * @return int|string
- */
- public static function saveData($where, $data)
- {
- $data['updated_at'] = date('Y-m-d H:i:s');
- return Member::where($where)->update($data);
- }
- /**
- * 想认识处理
- * @param $userId 用户ID
- * @param $contactUid 认识用户ID
- * @return array|int
- */
- public static function contactUser($userId, $contactUid, $remark = '')
- {
- if ($userId == $contactUid) {
- return 2133;
- }
- $contactInfo = Member::where(['id' => $contactUid, 'user_type' => 2, 'user_status' => 1])
- ->field('id,openid,user_nickname,sex,real_name,is_reg_profile')
- ->find();
- $memberInfo = Member::where(['id' => $userId, 'user_type' => 2, 'user_status' => 1])
- ->field('id,openid,user_nickname,real_name,sex,is_reg_profile,redheart,vip_auth,vip_expire,vip_num')
- ->find();
- if (empty($contactInfo) || empty($memberInfo)) {
- return false;
- }
- // 性别验证
- $contactSex = isset($contactInfo['sex']) ? intval($contactInfo['sex']) : 0;
- $sex = isset($memberInfo['sex']) ? intval($memberInfo['sex']) : 0;
- if (!$sex || ($sex == $contactSex)) {
- return 2134;
- }
- // 完善资料
- $isRegProfile = isset($memberInfo['is_reg_profile']) ? $memberInfo['is_reg_profile'] : 0;
- if ($isRegProfile != 1) {
- return 2103;
- }
- // 验证是否已认识
- $contactData = UserContactLog::where(['user_id' => $userId, 'contact_uid' => $contactUid])
- ->where('status', 'in', [1, 2, 3])
- ->field('id,status')
- ->find();
- $checkId = isset($contactData['id']) ? $contactData['id'] : 0;
- $status = isset($contactData['status']) ? $contactData['status'] : 0;
- if ($checkId && $status != 4) {
- return $status == 1 ? 2117 : 2118;
- }
- //
- Db::startTrans();
- // 扣除爱心账户
- $accountConfig = cmf_get_option('account_config');
- $contactPay = isset($accountConfig['contact_pay']) ? intval($accountConfig['contact_pay']) : 0;
- $contactPay = $contactPay ? $contactPay : 1;
- // 会员不需要收录费
- $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0;
- $vipExpire = isset($memberInfo['vip_expire']) ? intval($memberInfo['vip_expire']) : 0;
- $vipNum = isset($memberInfo['vip_num']) ? intval($memberInfo['vip_num']) : 0;
- $contactPay = $vipAuth && $vipExpire >= time() ? 0 : $contactPay;
- if ($contactPay > 0) {
- $redheart = isset($memberInfo['redheart']) ? intval($memberInfo['redheart']) : 0;
- if ($redheart < $contactPay) {
- Db::rollback();
- return 2121;
- }
- $newRedheart = max(0, ($redheart - $contactPay));
- $memberData = ['redheart' => $newRedheart, 'updated_at' => date('Y-m-d H:i:s')];
- if (!Member::where(['id' => $userId])->update($memberData)) {
- Db::rollback();
- return false;
- }
- // 账户明细
- $cNickname = isset($contactInfo['user_nickname']) ? $contactInfo['user_nickname'] : '';
- $accountData = [
- 'user_id' => $userId,
- 'type' => 2,
- 'account_type' => 1,
- 'change_type' => 2,
- 'money' => $contactPay,
- 'balance' => $redheart,
- 'remark' => "认识【{$cNickname}】请求成功,".($contactPay>0?"扣除{$contactPay}个爱心":'VIP会员使用1次机会'),
- 'created_at' => date('Y-m-d H:i:s'),
- 'status' => 2,
- ];
- PRedis::set('accounts:contact:' . $contactUid . '_' . $userId, $accountData, 600);
- if (!AccountLog::insertGetId($accountData)) {
- Db::rollback();
- return false;
- }
- }
- // 扣除会员微信申请次数
- else{
- if ($vipNum < 1 || $vipExpire < time()) {
- Db::rollback();
- return 2148;
- }
- $memberData = ['vip_num' => ($vipNum - 1), 'updated_at' => date('Y-m-d H:i:s')];
- if (!Member::where(['id' => $userId])->update($memberData)) {
- Db::rollback();
- return false;
- }
- }
- $data = [
- 'user_id' => $userId,
- 'contact_uid' => $contactUid,
- 'cost_redheart' => $contactPay,
- 'remark' => $remark,
- 'created_at' => date('Y-m-d H:i:s'),
- 'status' => 1,
- ];
- $cid = UserContactLog::insertGetId($data);
- if (!$cid) {
- Db::rollback();
- return false;
- }
- Db::commit();
- if ($cid) {
- // 模板消息
- $openid = isset($contactInfo['openid']) ? $contactInfo['openid'] : '';
- $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : '';
- $cNickname = isset($contactInfo['user_nickname']) ? $contactInfo['user_nickname'] : '';
- $sex = isset($memberInfo['sex']) ? $memberInfo['sex'] : 1;
- $sex = $sex == 1 ? '男生' : '女生';
- $dateTime = date('Y.m.d H:i');
- if ($openid) {
- $remark = $remark ? $remark : '无';
- $params = [
- 'title' => "对方留言:{$remark}",
- // 'title' => "有一位{$sex}申请加您的微信并给您留言了!\n\n申请时间:\t{$dateTime}\n\n收到留言:\t{$remark}",
- 'remark' => "点击查看(如果不想再收到该类型消息,可本公众号回复“隐身设置”进入链接设置为隐身)",
- 'type' => 'contact',
- 'keywords' => [
- 'keyword1' => [
- 'value' => $nickname . '(昵称)',
- 'color' => '#173177',
- ],
- 'keyword2' => [
- 'value' => '隐私信息不显示',
- 'color' => '#173177',
- ],
- 'keyword3' => [
- 'value' => '待处理',
- 'color' => '#173177',
- ],
- ],
- 'url' => url("/weixin/member/home?id={$userId}&cid={$cid}", [], '', true),
- ];
- PRedis::set('messages:contact:' . $openid, ['data' => $data, 'params' => $params], 600);
- Wechat::sendTplMsg($openid, $params, false);
- }
- // 操作日志
- UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => ($contactPay>0?'花费'.$contactPay.'爱心':'使用一次机会').",申请用户{$cNickname}微信"]);
- return ['id' => $cid];
- } else {
- return 2119;
- }
- }
- /**
- * @param $userId
- * @param $cid
- * @return bool|int
- */
- public static function contactConfirm($userId, $cid, $checkStatus)
- {
- // 验证是否
- $contactData = UserContactLog::where(['id' => $cid, 'contact_uid' => $userId])
- ->field('id,user_id,cost_redheart,is_read,status')
- ->find();
- $checkId = isset($contactData['id']) ? $contactData['id'] : 0;
- $status = isset($contactData['status']) ? $contactData['status'] : 0;
- $contactUid = isset($contactData['user_id']) ? $contactData['user_id'] : 0;
- $isRead = isset($contactData['is_read']) ? $contactData['is_read'] : 0; // 是否已读
- if ($checkId && $status != 1 && $checkStatus != 5) {
- return 2120;
- }
- if (empty($contactUid)) {
- return 2111;
- }
- // 撤回操作
- if ($checkStatus == 5) {
- UserContactLog::where(['id' => $cid])->update(['status' => 1, 'updated_at' => date('Y-m-d H:i:s')]);
- return 2139;
- }
- // 更新记录数据
- Db::startTrans();
- $updateData = ['status' => $checkStatus, 'updated_at' => date('Y-m-d H:i:s')];
- if (!UserContactLog::where(['id' => $cid])->update($updateData)) {
- Db::rollback();
- return 2032;
- }
- // 当前用户信息
- $field = 'm.user_nickname,m.real_name,m.openid,up.wechat_code,up.qq';
- $memberInfo = Member::getHomeInfo($userId, $field);
- $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : '';
- if (empty($memberInfo)) {
- Db::rollback();
- return 1011;
- }
- $field = 'm.redheart,m.user_nickname,m.real_name,m.openid,up.wechat_code,m.vip_num,up.qq';
- $contactUserInfo = Member::getHomeInfo($contactUid, $field, 6);
- $cOpenid = isset($contactUserInfo['openid']) ? $contactUserInfo['openid'] : '';
- $cNickname = isset($contactUserInfo['user_nickname']) ? $contactUserInfo['user_nickname'] : '';
- $redheart = isset($contactUserInfo['redheart']) ? intval($contactUserInfo['redheart']) : 0;
- $contactPay = isset($contactData['cost_redheart']) ? intval($contactData['cost_redheart']) : 0;
- $vipNum = isset($contactUserInfo['vip_num']) ? intval($contactUserInfo['vip_num']) : 0;
- if (empty($contactUserInfo)) {
- Db::rollback();
- return 1017;
- }
- // 已读或拒绝退款扣除的爱心
- $accountConfig = cmf_get_option('account_config');
- $applyRefundPay = isset($accountConfig['apply_refund_pay']) ? intval($accountConfig['apply_refund_pay']) : 0;
- if ($isRead || $checkStatus == 3) {
- $applyRefundPay = $applyRefundPay ? $applyRefundPay : 0;
- $refundHeart = $contactPay > $applyRefundPay ? intval($contactPay - $applyRefundPay) : 0;
- } else {
- $refundHeart = $contactPay;
- $applyRefundPay = 0;
- }
- // 审核失败退还扣除的爱心
- if ($contactPay > 0 && ($checkStatus == 3 || $checkStatus == 4)) {
- $newRedheart = max(0, ($redheart + $refundHeart));
- $memberData = ['redheart' => $newRedheart, 'updated_at' => date('Y-m-d H:i:s')];
- if (!Member::where(['id' => $contactUid])->update($memberData)) {
- Db::rollback();
- return 2031;
- }
- // 账户明细
- $accountData = [
- 'user_id' => $contactUid,
- 'type' => 3,
- 'account_type' => 1,
- 'change_type' => 1,
- 'money' => $refundHeart,
- 'balance' => $redheart,
- 'remark' => "认识【{$nickname}-ID:{$userId}】" . ($checkStatus == 3 ? '审核失败' : '审核超时') . ",退还{$refundHeart}个爱心,扣除{$applyRefundPay}个爱心",
- 'created_at' => date('Y-m-d H:i:s'),
- 'status' => 2,
- ];
- PRedis::set('accounts:contact:' . $contactUid . '_' . $userId, $accountData, 600);
- if (!AccountLog::insertGetId($accountData)) {
- Db::rollback();
- return false;
- }
- }
- // 退还申请次数
- else if($checkStatus == 3 || $checkStatus == 4){
- $memberData = ['vip_num' => $vipNum+1, 'updated_at' => date('Y-m-d H:i:s')];
- if (!Member::where(['id' => $contactUid])->update($memberData)) {
- Db::rollback();
- return 2031;
- }
- }
- Db::commit();
- // 发送模板信息
- /*if($openid){
- $cWechatCode = isset($contactUserInfo['wechat_code'])? trim($contactUserInfo['wechat_code']) : '';
- $cqq = isset($contactUserInfo['qq'])? trim($contactUserInfo['qq']) : '';
- $cWechatCode = $cWechatCode? $cWechatCode : '无';
- $cqq = $cqq? $cqq : '无';
- $params = [
- 'title' => "【{$cRealname}】想认识您的请求已确认\n\n对方微信号:\t{$cWechatCode}\n\n对方QQ:\t{$cqq}",
- 'remark' => "感谢您的使用,点击详情查看对方信息",
- 'type' => 'contact',
- 'keywords' => [
- 'keyword1' => [
- 'value' => '已确认',
- 'color' => '#173177',
- ],
- 'keyword2' => [
- 'value' => date('Y年m月d日 H点i分s秒'),
- 'color' => '#173177',
- ],
- 'keyword3' => [
- 'value' => "来自【{$cNickname}】,姓名:【{$cRealname}】的认识申请已经确认通过,请及时添加对方联系方式",
- 'color' => '#173177',
- ],
- ],
- 'url' => url("/weixin/member/home?id={$contactUid}&cid=$cid", '', '', true),
- ];
- PRedis::set('messages:contactConfirm_' . $userId.'_'.$contactUid, ['data' => $contactData, 'params' => $params], 600);
- Wechat::sendTplMsg($openid, $params);
- }*/
- // 通知对方模板信息
- if ($cOpenid) {
- $wechatCode = isset($memberInfo['wechat_code']) ? trim($memberInfo['wechat_code']) : '';
- $qq = isset($memberInfo['qq']) ? trim($memberInfo['qq']) : '';
- $wechatCode = $wechatCode ? $wechatCode : '无';
- $qq = $qq ? $qq : '无';
- $read = $isRead ? '已查看' : ' 未查看';
- $dateTime = date('Y.m.d H:i');
- if ($checkStatus == 2) {
- $params = [
- 'title' => "你喜欢的人也喜欢你啦\n\n对方姓名:\t{$nickname}(昵称)\n\n对方微信号: \t{$wechatCode}\n\n申请时间:\t{$dateTime}",
- 'remark' => "脱单需主动,交友需谨慎!",
- 'type' => 'contact_confirm',
- 'keywords' => [
- 'keyword1' => [
- 'value' => '状态通知',
- 'color' => '#173177',
- ],
- 'keyword2' => [
- 'value' => '已接受',
- 'color' => '#173177',
- ],
- ],
- 'url' => url("/weixin/member/home?id={$userId}&cid={$cid}", '', '', true),
- ];
- } else if ($checkStatus == 3) {
- // 是否扣除爱心
- $remark = $applyRefundPay ? "扣除{$applyRefundPay}颗后已经原路退回" : "已经全部原路退回";
- $remark = $contactPay<=0? "您的微信申请次数已返还": "您支付的爱心{$remark}";
- $params = [
- 'title' => "Soory!亲亲!{$nickname}拒绝了您的加微信申请,{$remark}。别灰心哦,您一定会遇到更好的另一半~\n\n申请时间:\t{$dateTime}\n查看状态:\t{$read}",
- 'remark' => "天涯何处无芳草,去看看其他的异性吧!",
- 'type' => 'contact_confirm',
- 'keywords' => [
- 'keyword1' => [
- 'value' => '状态通知',
- 'color' => '#173177',
- ],
- 'keyword2' => [
- 'value' => '已拒绝',
- 'color' => '#173177',
- ],
- ],
- 'url' => url("/weixin/match/index", '', '', true),
- ];
- } else {
- // 是否已读
- $remark = $isRead && $applyRefundPay ? "扣除{$applyRefundPay}颗后已经原路退回" : "已经全部原路退回";
- $remark = $contactPay<=0? "您的微信申请次数已返还": "您被扣除的爱心{$remark}";
- $params = [
- 'title' => "您好亲亲,非常抱歉{$nickname}未及时反馈而导致申请超时,{$remark}。您还可以请客服帮您牵线增加成功率哦~\n\n申请时间:\t{$dateTime}\n查看状态:\t{$read}",
- 'remark' => "当你有勇气主动时,其实已经打败了那些看似优秀的人,遇见不易让客服牵线试试吧!",
- 'type' => 'contact_confirm',
- 'keywords' => [
- 'keyword1' => [
- 'value' => '状态通知',
- 'color' => '#173177',
- ],
- 'keyword2' => [
- 'value' => '过期失效',
- 'color' => '#173177',
- ],
- ],
- 'url' => url("/weixin/match/index", '', '', true),
- ];
- }
- // 操作日志
- if ($checkStatus == 2 || $checkStatus == 3) {
- $content = $checkStatus == 2 ? "通过用户{$cNickname}微信申请" : "拒绝用户{$cNickname}微信申请";
- UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => $content]);
- }
- PRedis::set('messages:contactConfirm:' . $contactUid . '_' . $userId, ['openid' => $cOpenid, 'data' => $contactData, 'params' => $params], 600);
- Wechat::sendTplMsg($cOpenid, $params);
- }
- return ['id' => $cid];
- }
- /**
- * 用户页面访问统计
- * @param $userId 访问用户
- * @param string $type 访问类型
- * @param int $targetUid 目标用户
- */
- public static function visitCount($userId, $type = 'login', $targetUid = 0, $expire = 1)
- {
- $cacheKey = 'counts:users:' . date('Ymd') . '_' . $type;
- $cacheTempKey = 'counts:usersTemp:' . $userId . '_' . $type . '_' . date('Ymd');
- $checkUser = Member::where(['user_type' => 2, 'id' => $userId])
- ->value('id');
- if ($checkUser && $userId && !PRedis::get($cacheTempKey)) {
- PRedis::set($cacheTempKey, ['userId' => $userId, 'type' => $type, 'target' => $targetUid], $expire * 3600);
- $types = ['login' => '登录', 'match' => '访问单身推荐', 'heart' => '访问怦然心动', 'center' => '访问用户中心', 'home' => "访问用户[{$targetUid}]信息", 'activity' => '浏览活动列表', 'book' => '想要报名'];
- $title = isset($types[$type]) ? $types[$type] : '访问平台';
- if ($type != 'profile') {
- UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => "【{$userId}】用户{$title}"]);
- }
- if (!PRedis::get($cacheKey)) {
- PRedis::set($cacheKey, 1, 2 * 24 * 3600);
- } else {
- PRedis::inc($cacheKey, 1);
- }
- }
- }
- /**
- * 获取匹配用户数据列表
- * @param $matchUids
- * @return array|bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getMatchList($matchUids, $activityId = 0)
- {
- if (empty($matchUids)) {
- return false;
- }
- $field = 'm.id,m.user_nickname,m.avatar,m.mobile,m.sex,m.birthday,m.is_heart,m.real_name,up.age,up.height,up.weight,up.company,up.occupation,up.property,up.graduate,up.education,up.province,up.salary,up.city,up.district,up.home_province,up.home_city,up.home_district,up.introduce,up.family,up.hobby,up.purpose,up.cause,up.expect,up.show_company,up.show_graduate,up.married,up.tags,up.isinfo,m.vip_auth,m.vip_expire,up.education_check,up.position_check,up.idcard_check';
- $dataList = Member::alias('m')
- ->leftJoin('user_profile up', 'up.userid=m.id')
- ->where('m.id', 'in', $matchUids)
- ->field($field)
- ->select()
- ->each(function ($item) use ($activityId) {
- if ($activityId) {
- $item['book_no'] = Books::where(['uid' => $item['id'], 'aid' => $activityId, 'status' => 3])->value('book_num');
- }
- Member::formatUser($item);
- });
- return $dataList ? $dataList->toArray() : [];
- }
- /**
- * 格式化用户信息
- * @param $item 数据
- * @param $type
- */
- private static function formatUser(&$item, $type = 0)
- {
- if (isset($item['avatar'])) {
- $item['avatar'] = cmf_get_image_preview_url($item['avatar']);
- }
- if (isset($item['brief'])) {
- $item['brief'] = htmlspecialchars_decode($item['brief']);
- }
- if (isset($item['sex'])) {
- $sexs = ['', '男', '女'];
- $sex = isset($item['sex']) ? $item['sex'] : 0;
- $item['sex_txt'] = isset($sexs[$sex]) ? $sexs[$sex] : '';
- }
- $item['albums'] = [];
- $photolist = isset($item['photolist']) ? $item['photolist'] : '';
- $photolist = $photolist ? explode(',', $photolist) : [];
- if ($photolist) {
- $albums = [];
- foreach ($photolist as $k => $val) {
- $albums[] = cmf_get_image_preview_url($val);
- }
- $item['albums'] = $albums;
- }
- if (isset($item['birthday'])) {
- $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
- $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
- $item['birthday_code'] = $birthday ? date('Y-m-d', $birthday) : '';
- $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
- }
- $item['height_txt'] = '';
- if (isset($item['height'])) {
- $height = isset($item['height']) ? intval($item['height']) : 0;
- $item['height'] = intval($height);
- $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '无';
- }
- $item['weight_txt'] = '';
- if (isset($item['weight'])) {
- $weight = isset($item['weight']) ? intval($item['weight']) : 0;
- $item['weight'] = intval($weight);
- $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '无';
- }
- // 学历
- $item['education_txt'] = '';
- if (isset($item['education'])) {
- $educations = config('weixin.educations');
- $edu = isset($item['education']) ? $item['education'] : 0;
- $item['education_txt'] = isset($educations[$edu]) ? $educations[$edu] : '无';
- }
- // 资产
- $item['property_txt'] = '暂无';
- $property = isset($item['property']) ? $item['property'] : 0;
- if (isset($item['property'])) {
- $propertys = config('weixin.propertys');
- $item['property'] = $property;
- $item['property_txt'] = $property && isset($propertys[$property]) ? $propertys[$property] : '暂无';
- }
- // 地址
- $item['now_address'] = '';
- if (isset($item['province'])) {
- $province = isset($item['province']) ? trim($item['province']) : '';
- $city = isset($item['city']) ? trim($item['city']) : '';
- $item['now_address'] = trim($province . ' ' . $city);
- }
- $item['home_address'] = '';
- if (isset($item['home_province'])) {
- $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
- $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
- $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
- }
- // 格式化婚姻状况
- $item['married_txt'] = '';
- if (isset($item['married'])) {
- $marrieds = config('weixin.marrieds');
- $married = $item['married'] ? intval($item['married']) : 0;
- $item['married_txt'] = $married > 0 && isset($marrieds[$married - 1]) ? $marrieds[$married - 1] : '无';
- }
- }
- /**
- * 获取收到或发出消息数量
- * @param $userId
- * @param array $params
- * @return float|int|string
- */
- public static function getMessageTotal($userId, $params = [])
- {
- $type = isset($params['type']) ? intval($params['type']) : 1;
- // 我收到的
- if ($type == 1) {
- $where = ['uc.contact_uid' => $userId];
- $joinWhere = 'u.id=uc.user_id';
- } else {
- $where = ['uc.user_id' => $userId];
- $joinWhere = 'u.id=uc.contact_uid';
- }
- $status = isset($params['status']) ? intval($params['status']) : 1;
- return UserContactLog::alias('uc')
- ->join('user u', $joinWhere, 'left')
- ->where($where)
- ->where('u.id', '>', 0)
- ->where(function ($query) use ($status) {
- if ($status == 1) {
- $query->where('uc.status', '>', 1);
- } else {
- $query->where('uc.status', 1);
- }
- })
- ->count('uc.id');
- }
- /**
- * 获取用户申请消息列表
- * @param $params
- * @param $pageSize
- * @param string $field
- * @return $this
- */
- public static function getMessageList($userId, $params = [], $pageSize = 20, $field = '')
- {
- // 验证有效时间,会员不限制
- $type = isset($params['type']) ? intval($params['type']) : 1;
- // 我收到的
- if ($type == 1) {
- $where = ['uc.contact_uid' => $userId];
- $joinWhere = 'u.id=uc.user_id';
- } else {
- $where = ['uc.user_id' => $userId];
- $joinWhere = 'u.id=uc.contact_uid';
- }
- $status = isset($params['status']) ? intval($params['status']) : 1;
- $siteInfo = cmf_get_site_info();
- $expire = isset($siteInfo['contact_time']) ? intval($siteInfo['contact_time']) : 0;
- $expire = $expire ? $expire : 1;
- $field = $field ? $field : 'uc.id,uc.user_id,uc.contact_uid,uc.created_at,uc.remark,uc.updated_at,uc.is_read,uc.status,u.id as uid,u.user_login,u.real_name,u.user_nickname,u.avatar,u.sex,u.birthday,mp.graduate,mp.education,mp.height,mp.weight,mp.show_graduate,mp.show_company,mp.company,mp.occupation,mp.province,mp.city,mp.home_province,mp.home_city';
- $dataList = UserContactLog::alias('uc')
- ->join('user u', $joinWhere, 'left')
- ->join('user_profile mp', 'mp.userid=u.id', 'left')
- ->where($where)
- ->where('u.id', '>', 0)
- ->where(function ($query) use ($status) {
- if ($status == 1) {
- $query->where('uc.status', 1);
- } else {
- $query->where('uc.status', '>', 1);
- }
- })
- ->field($field)
- ->order('uc.status asc,uc.created_at desc')
- ->paginate($pageSize)
- ->each(function ($item, $k) use ($expire) {
- $expire = $expire ? $expire * 86400 : 0;
- $status = isset($item['status']) ? intval($item['status']) : 0;
- $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
- $rtime = $item['created_at'] ? strtotime($item['created_at']) : 0;
- $rtime = $rtime > time() - $expire ? $rtime - (time() - $expire) : 0;
- $item['rtime'] = $rtime;
- $rtime = $rtime > 23 * 3600 ? ceil($rtime / 86400) . '天' : ($rtime > 3600 ? ceil($rtime / 3600) . '小时' : ($rtime > 60 ? ceil($rtime / 60) . '分钟' : ''));
- if ($status == 1) {
- if ($rtime) {
- $item['time'] = '<em class="red">' . $rtime . '后</em> 自动拒绝';
- } else {
- $item['time'] = '待处理';
- }
- } else {
- $item['time'] = $status == 2 ? '已通过' : ($status == 3 ? '已拒绝' : '待处理');
- if ($status == 4) {
- $info['time'] = '过期自动处理';
- }
- }
- Member::formatUser($item);
- return $item;
- });
- $dataList = $dataList ? $dataList->toArray() : ['total' => 0];
- $dataList['total1'] = Member::getMessageTotal($userId, $params);
- return $dataList;
- }
- /**
- * 获取用户申请消息列表
- * @param $params
- * @param $pageSize
- * @param string $field
- * @return $this
- */
- public static function getMessageInfo($id, $userId = 0, $field = '')
- {
- $siteInfo = cmf_get_site_info();
- $expire = isset($siteInfo['contact_time']) ? intval($siteInfo['contact_time']) : 0;
- $expire = $expire ? $expire : 1;
- $type = 1;
- $joinWhere = 'u.id=uc.user_id';
- $data = UserContactLog::where(['id' => $id])->field('user_id,contact_uid')->find();
- if ($userId && isset($data['user_id']) && $data['user_id'] == $userId) {
- $joinWhere = 'u.id=uc.contact_uid';
- $type = 2;
- }
- $field = $field ? $field : 'uc.id,uc.user_id,uc.contact_uid,uc.created_at,uc.remark,uc.updated_at,uc.is_read,uc.status,u.id as uid,u.user_login,u.real_name,u.user_nickname,u.avatar,u.sex,u.birthday,mp.wechat_code,mp.graduate,mp.education,mp.height,mp.weight,mp.show_graduate,mp.show_company,mp.company,mp.occupation,mp.province,mp.city,mp.home_province,mp.home_city,mp.education_check,mp.position_check,mp.idcard_check';
- $info = UserContactLog::alias('uc')
- ->join('user u', $joinWhere, 'left')
- ->join('user_profile mp', 'mp.userid=u.id', 'left')
- ->where(['uc.id' => $id])
- ->where('u.id', '>', 0)
- ->field($field)
- ->find();
- $info = $info ? $info->toArray() : [];
- if ($info) {
- $info['type'] = $type;
- $expire = $expire ? $expire * 86400 : 0;
- $status = isset($info['status']) ? intval($info['status']) : 0;
- $info['avatar'] = isset($info['avatar']) ? cmf_get_image_preview_url($info['avatar']) : '';
- $rtime = $info['created_at'] ? strtotime($info['created_at']) : 0;
- $rtime = $rtime > time() - $expire ? $rtime - (time() - $expire) : 0;
- $info['rtime'] = $rtime;
- $rtime = $rtime > 23 * 3600 ? ceil($rtime / 86400) . '天' : ($rtime > 3600 ? ceil($rtime / 3600) . '小时' : ($rtime > 60 ? ceil($rtime / 60) . '分钟' : ''));
- if ($status == 1) {
- if ($rtime) {
- $info['time'] = '<em class="red">' . $rtime . '后</em> 自动拒绝';
- } else {
- $info['time'] = '待处理';
- }
- } else {
- $info['time'] = $status == 2 ? '已通过' : ($status == 3 ? '已拒绝' : '待处理');
- if ($status == 4) {
- $info['time'] = '过期自动处理';
- }
- }
- $info['show_wechat'] = true;
- $rtime = $info['created_at'] ? strtotime($info['created_at']) : 0;
- $info['rtime'] = $rtime;
- if ($rtime <= time() - 7 * 24 * 3600) {
- $info['show_wechat'] = false;
- }
- Member::formatUser($info);
- }
- return $info;
- }
- /**
- * 余额明细
- * @param $params
- * @param int $pageSize
- * @return mixed
- */
- public static function getBalanceLog($params, $pageSize = 15)
- {
- $where = [];
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId) {
- $where['b.user_id'] = $userId;
- }
- $changeType = isset($params['change_type']) ? $params['change_type'] : 0;
- if ($changeType) {
- $where['b.change_type'] = $changeType;
- }
- $type = isset($params['type']) ? $params['type'] : 0;
- $model = db('user_balance_log')->alias('b')
- ->leftJoin('user u', 'u.id=b.source_uid')
- ->where($where)
- ->where(function ($query) use ($type) {
- if ($type == 'income') {
- $query->where('b.type', '>', 1);
- } else if ($type) {
- $type = is_array($type) ? $type : [$type];
- $query->whereIn('b.type', $type);
- }
- });
- $types = config('weixin.incomeTypes');
- $dataList = $model->field('b.id,b.type,b.order_sn,b.change as money,b.change_type,b.balance,b.remark,b.description,b.create_time,status,u.user_nickname')
- ->where('b.status', 'lt', 4)
- ->order("b.create_time desc")
- ->paginate($pageSize)
- ->each(function ($item, $k) use ($types) {
- $item['format_time'] = $item['create_time'] ? date('Y年m月d日 H点i分', $item['create_time']) : '';
- $item['money'] = floatval($item['money']);
- $type = isset($item['type']) ? $item['type'] : 0;
- $item['typeName'] = isset($types[$type]) ? $types[$type] : '';
- return $item;
- });
- $dataList = $dataList ? $dataList->toArray() : [];
- if ($dataList) {
- $total = $model->where('b.status', '=', 2)->sum('b.change');
- $dataList['total_money'] = moneyFormat($total);
- $total = db('user_balance_log')->alias('b')
- ->leftJoin('user u', 'u.id=b.source_uid')
- ->where($where)
- ->where(function ($query) use ($type) {
- if ($type == 'income') {
- $query->where('b.type', '>', 1);
- } else if ($type) {
- $type = is_array($type) ? $type : [$type];
- $query->whereIn('b.type', $type);
- }
- })
- ->where('b.status', '=', 1)
- ->order("b.create_time desc")
- ->sum('b.change');
- $dataList['nopay_money'] = moneyFormat($total);
- }
- return $dataList;
- }
- /**
- * 邀请扽团队成员或会员列表
- * @param $params 参数
- * @param $type 类型:1-团队成员,2-邀请的会员
- * @param int $pageSize
- * @return mixed
- */
- public static function getInviteList($inviteId, $type = 1, $pageSize = 15)
- {
- $where = ['user_type' => 2, 'user_status' => 1];
- if ($inviteId) {
- $where['parent_id'] = $inviteId;
- }
- if ($type == 1) {
- $where['agent_type'] = 1;
- $orderBy = "agent_create_time desc";
- } else {
- $where['agent_type'] = 0;
- $orderBy = "create_time desc";
- }
- $model = Member::where($where)
- ->where(function ($query) use ($type) {
- if ($type == 1) {
- $query->where('agent_status', '<', 4);
- } else {
- $query->whereIn('user_status', [0, 1, 2]);
- }
- });
- $field = 'id,openid,user_nickname,sex,user_type,agent_type,agent_status,agent_create_time,create_time,avatar,is_reg_profile,user_status,is_heart,vip_auth,vip_expire,is_follow';
- $dataList = $model->field($field)
- ->order($orderBy)
- ->paginate($pageSize)
- ->each(function ($item, $k) use ($type, $inviteId) {
- $item['avatar'] = $item['avatar'] ? cmf_get_image_preview_url($item['avatar']) : '';
- if ($type == 1) {
- $item['format_time'] = $item['agent_create_time'] ? date('Y-m-d H:i', $item['agent_create_time']) : '';
- $item['invite_num'] = Member::getInviteCounts(['parent_id' => $item['id']], 2);
- $item['invite_market_num'] = Member::getInviteCounts(['parent_id' => $item['id']], 1);
- $item['invite_sum'] = Member::getBalanceCount($inviteId, $item['id']);
- } else {
- // VIP
- $vipAuth = isset($item['vip_auth']) ? intval($item['vip_auth']) : 0;
- $vipExpire = isset($item['vip_expire']) ? intval($item['vip_expire']) : 0;
- if ($vipAuth && $vipExpire >= time()) {
- $item['vip_auth'] = 1;
- $item['vip_expire'] = date('Y-m-d', $item['vip_expire']);
- } else {
- $item['vip_auth'] = 0;
- $item['vip_expire'] = '';
- }
- $item['format_time'] = $item['create_time'] ? date('Y-m-d H:i', $item['create_time']) : '';
- $item['invite_sum'] = Member::getBalanceCount($inviteId, $item['id'], 2);
- }
- $item['avatar'] = $item['avatar'] ? cmf_get_image_preview_url($item['avatar']) : '';
- return $item;
- });
- $dataList = $dataList ? $dataList->toArray() : [];
- if ($dataList) {
- $counts = [];
- if ($type == 1) {
- $counts['invite_num'] = Member::getInviteCounts(['parent_id' => $inviteId], 1);
- $total = Member::getBalanceCount($inviteId);
- $counts['invite_sum'] = moneyFormat($total);
- } else {
- $counts['invite_sex_1'] = Member::getInviteCounts(['parent_id' => $inviteId, 'sex' => 1], 2);
- $counts['invite_sex_2'] = Member::getInviteCounts(['parent_id' => $inviteId, 'sex' => 2], 2);
- $counts['invite_vip'] = Member::getInviteCounts(['parent_id' => $inviteId, 'vip_auth' => 1], 2);
- $total = Member::getBalanceCount($inviteId, 0, 2);
- $counts['invite_sum'] = moneyFormat($total);
- }
- $dataList['counts'] = $counts;
- }
- return $dataList;
- }
- /**
- * 统计邀请的会员或团队成员
- * @param $params
- * @param int $type
- * @return float|int|string
- */
- public static function getInviteCounts($params, $type = 1)
- {
- $where = ['user_type' => 2, 'user_status' => 1];
- $inviteId = isset($params['parent_id']) ? $params['parent_id'] : 0;
- if ($inviteId) {
- $where['parent_id'] = $inviteId;
- }
- if ($type == 1) {
- $where['agent_type'] = 1;
- } else {
- $where['agent_type'] = 0;
- }
- $sex = isset($params['sex']) ? $params['sex'] : 0;
- if ($sex) {
- $where['sex'] = $sex;
- }
- $sex = isset($params['sex']) ? $params['sex'] : 0;
- if ($sex) {
- $where['sex'] = $sex;
- }
- $count = Member::where($where)
- ->where(function ($query) use ($type) {
- if ($type == 1) {
- $query->where('agent_status', '<', 4);
- } else {
- $query->whereIn('user_status', [0, 1, 2]);
- }
- })
- ->where(function ($query) use ($params) {
- $sex = isset($params['sex']) ? $params['sex'] : 0;
- if ($sex) {
- $query->where('sex', '=', $sex);
- }
- $vipAuth = isset($params['vip_auth']) ? $params['vip_auth'] : 0;
- if ($vipAuth) {
- $query->where('vip_auth', '=', $vipAuth)->where('vip_expire', '>=', time());
- }
- })
- ->count('id');
- return intval($count);
- }
- /**
- * 统计收益金额
- * @param $userId 所属用户
- * @param int $sourceUid 来源用户
- * @param int $type 类型:0-收益分成,
- * @return float
- */
- public static function getBalanceCount($userId, $sourceUid = 0, $userType = 1, $profitType = 0)
- {
- $where = ['b.user_id' => $userId, 'b.status' => 2, 'u.user_type' => 2];
- if ($sourceUid) {
- $where['b.source_uid'] = $sourceUid;
- }
- if ($userType == 1) {
- $where['u.agent_type'] = 1;
- } else {
- $where['u.agent_type'] = 0;
- }
- if ($profitType) {
- $where['b.type'] = $profitType;
- }
- //var_dump($where);
- $sum = UserBalanceLog::alias('b')
- ->leftJoin('user u', 'u.id=b.source_uid')
- ->where($where)
- ->where('b.type', '>', 1)
- ->where('b.type', '<', 30)
- ->sum('b.pay_money');
- return moneyFormat($sum);
- }
- /**
- * 投诉拉黑用户列表
- * @param int $type 类型
- * @param int $pageSize 分页大小
- * @return array|bool|\think\Paginator
- * @throws \think\exception\DbException
- */
- public static function getComplainList($type, $pageSize = 30)
- {
- $page = input('page', 1);
- $cacheKey = "cache:complainList:p_{$type}_{$page}_{$pageSize}";
- $dataList = PRedis::get($cacheKey);
- if ($dataList) {
- return $dataList;
- }
- $complainTypes = config('weixin.complainTypes');
- $dataList = Complain::alias('c')
- ->leftJoin('user u', 'u.id=c.c_uid')
- ->field('c.id,u.id as uid,u.real_name,u.user_nickname,u.avatar,u.mobile,c.type,c.remark,c.status')
- ->where(['c.status' => 2])
- ->where('u.id', '>', 0)
- ->where(function ($query) use ($type) {
- if ($type == 2) {
- $query->where('c.created_time', '>=', time() - 3 * 24 * 3600);
- }
- })
- ->order('c.created_time desc')
- ->paginate($pageSize)
- ->each(function ($item, $k) use ($complainTypes) {
- $item['wechat_code'] = UserProfile::where(['userid' => $item['uid']])->value('wechat_code');
- $item['wechat_code'] = $item['wechat_code'] ? formatName($item['wechat_code']) : '';
- $item['avatar'] = $item['avatar'] ? cmf_get_image_preview_url($item['avatar']) : '';
- $type = isset($item['type']) ? $item['type'] : 8;
- $item['type_name'] = isset($complainTypes[$type]) ? $complainTypes[$type] : '其他';
- $item['remark_count'] = $item['remark'] ? mb_strlen($item['remark'], 'utf-8') : 0;
- $item['remark_sub'] = $item['remark_count'] > 30 ? substr($item['remark'], 0, 30) : $item['remark'];
- });
- if ($dataList) {
- PRedis::set($cacheKey, $dataList, 10);
- }
- return $dataList;
- }
- /**
- * 解锁用户访问信息
- * @param $uid 当前用户
- * @param $cuid 被解锁用户
- * @return array|int
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public static function catchUnlock($uid, $cuid)
- {
- // 验证用户信息
- $memberInfo = Member::where(['id' => $uid, 'user_status' => 1])->field('id,user_nickname,redheart,vip_auth,vip_expire,openid')->find();
- $memberInfo = $memberInfo ? $memberInfo->toArray() : [];
- if (!$memberInfo) {
- return 2102;
- }
- $accessMemberInfo = Member::where(['id' => $cuid, 'user_status' => 1])->field('id,user_nickname,redheart,openid')->find();
- if (!$accessMemberInfo) {
- return 8305;
- }
- // 验证访问信息
- $accessInfo = UserAccess::where(['user_id' => $uid, 'access_uid' => $cuid, 'status' => 1])->find();
- if (!$accessInfo) {
- return 1003;
- }
- if ($accessInfo->is_lock == 1) {
- return 8304;
- }
- // 验证账户
- $accountConfig = cmf_get_option('account_config');
- $lockAccessPay = isset($accountConfig['lock_access_pay']) ? intval($accountConfig['lock_access_pay']) : 0;
- // 会员
- $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0;
- $vipExpire = isset($memberInfo['vip_expire']) ? intval($memberInfo['vip_expire']) : 0;
- $lockAccessPay = $vipAuth && $vipExpire >= time() ? 0 : $lockAccessPay;
- if ($lockAccessPay <= 0) {
- UserAccess::where(['user_id' => $uid, 'access_uid' => $cuid, 'status' => 1])->update(['is_lock' => 1]);
- return ['id' => $accessInfo->id];
- }
- // 付费解锁
- $redheart = isset($memberInfo['redheart']) ? $memberInfo['redheart'] : 0;
- if ($redheart < $lockAccessPay) {
- return 2137;
- }
- // 更新
- Db::startTrans();
- if (!UserAccess::where(['user_id' => $uid, 'access_uid' => $cuid, 'status' => 1])->update(['is_lock' => 1])) {
- Db::rollback();
- return 2032;
- }
- // 扣除账户
- if (!Member::where(['id' => $uid, 'user_status' => 1])->setDec('redheart', $lockAccessPay)) {
- Db::rollback();
- return 2031;
- }
- // 账户明细
- $data = [
- 'user_id' => $uid,
- 'type' => 2,
- 'account_type' => 1,
- 'change_type' => 2,
- 'money' => $lockAccessPay,
- 'balance' => $redheart,
- 'remark' => '解锁用户【' . $accessMemberInfo->user_nickname . '】访问信息支付' . $lockAccessPay . '颗爱心',
- 'created_at' => date('Y-m-d H:i:s'),
- 'status' => 2
- ];
- if (!AccountLog::insertGetId($data)) {
- Db::rollback();
- return 8306;
- }
- Db::commit();
- return ['id' => $accessInfo->id];
- }
- /**
- * 更新身份认证信息
- * @param $userId
- * @param $realname
- * @param $idcard
- * @return bool
- */
- public static function updateAuth($userId, $realname, $idcard)
- {
- Db::startTrans();
- if (!Member::saveData(['id' => $userId], ['real_name' => $realname])) {
- Db::rollback();
- return false;
- }
- // 在线验证是否通过
- $profileData = [
- 'idcard' => $idcard,
- 'idcard_online_check' => 1,
- 'idcard_type' => 1,
- 'idcard_check' => 1,
- ];
- if (UserProfile::checkProfile($userId)) {
- $profileData['updated_at'] = date('Y-m-d H:i:s');
- $res = UserProfile::saveData(['userid' => $userId], $profileData);
- } else {
- $res = UserProfile::insertGetId($profileData);
- }
- if (!$res) {
- Db::rollback();
- return false;
- }
- // 操作日志
- UserLog::saveLog(['user_id' => $userId, 'type' => 1, 'content' => '提交身份证认证']);
- Db::commit();
- return true;
- }
- /**
- * 统计清除爱心
- * @param $userId
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getSignReadheart($userId){
- $siteInfo = cmf_get_option('site_info');
- $signClearDay = isset($siteInfo['sign_clear_day'])? intval($siteInfo['sign_clear_day']) : 0;
- $signClearDay = $signClearDay? min($signClearDay, 28) : 1;
- $signClearDay = $signClearDay<10? '0'.$signClearDay : $signClearDay;
- $datas = [
- 'clear_day'=> date('m月'.$signClearDay.'号'),
- 'clear_month'=>date('m月', strtotime(date('Y-m-01')) - 86400),
- 'sign_total'=> 0,
- 'sign_clear'=> 0,
- 'show_clear'=> 1,
- ];
- // 最早
- $month = date('Y-m-01', strtotime(date('Y-m-01')) - 86400);
- $firstSign = AccountLog::where(['user_id'=> $userId,'type'=> 12,'status'=> 2])
- ->where('created_at','>=', $month)
- ->order('created_at','asc')
- ->find();
- $time = isset($firstSign['created_at'])? $firstSign['created_at'] : '';
- $check = AccountLog::where(['user_id' => $userId, 'type'=> 13, 'status' => 2])
- ->where('created_at', '>=', $time)
- ->find();
- if(empty($check) && date('Y-m-d') > date('Y-m-'.$signClearDay)){
- $check = true;
- }
- if($firstSign && $time) {
- if($check){
- $datas['clear_day'] = date('m月'.$signClearDay.'号', strtotime('next month'));
- $datas['clear_month'] = date('m月');
- // 签到后清除月份总消费
- $totalCost = AccountLog::where(['user_id' => $userId, 'status' => 2, 'account_type' => 1, 'change_type' => 2])
- ->where('created_at', '>', date('Y-m-01 01:00:00'))
- ->sum('money');
- $totalClearSign = AccountLog::where(['user_id' => $userId, 'type' => 12, 'status' => 2])
- ->where('created_at', '>', date('Y-m-01 01:00:00'))
- ->sum('money');
- }else{
- // 签到后清除月份总消费
- $totalCost = AccountLog::where(['user_id' => $userId, 'status' => 2, 'account_type' => 1, 'change_type' => 2])
- ->where('created_at', '>=', $time)
- ->where('created_at', '<=', date('Y-m-01 01:00:00'))
- ->sum('money');
- $totalClearSign = AccountLog::where(['user_id' => $userId, 'type' => 12, 'status' => 2])
- ->where('created_at', '>=', $time)
- ->where('created_at', '<=', date('Y-m-01 01:00:00'))
- ->sum('money');
- }
- $clearRedheart = ($totalClearSign - $totalCost);
- $datas['sign_clear'] = $clearRedheart;
- }
- if($check){
- $totalSign = AccountLog::where(['user_id' => $userId, 'type' => 12, 'status' => 2])
- ->where('created_at', '>', date('Y-m-01 01:00:00'))
- ->sum('money');
- $cost = AccountLog::where(['user_id' => $userId, 'account_type' => 1, 'change_type' => 2, 'status' => 2])
- ->where('created_at', '>', date('Y-m-01 01:00:00'))
- ->sum('money');
- }else{
- $totalSign = AccountLog::where(['user_id' => $userId, 'type' => 12, 'status' => 2])
- ->where('created_at', '>=', $time)
- ->sum('money');
- $cost = AccountLog::where(['user_id' => $userId, 'account_type' => 1, 'change_type' => 2, 'status' => 2])
- ->where('created_at', '>=', $time)
- ->sum('money');
- }
- $datas['sign_total'] = $totalSign>$cost? $totalSign-$cost : 0;
- if($datas['sign_total'] <= 0){
- $datas['sign_clear'] = 0;
- }
- return $datas;
- }
- }
|