Member.php 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631
  1. <?php
  2. namespace app\weixin\model;
  3. use app\weixin\service\PRedis;
  4. use think\Db;
  5. use think\Model;
  6. class Member extends Model
  7. {
  8. protected $table = 'sg_user';
  9. /**
  10. * 注册用户
  11. * @param $params 注册参数
  12. * @return int|string
  13. */
  14. public static function regMember($params)
  15. {
  16. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  17. $userPass = isset($params['password']) ? trim($params['password']) : '';
  18. $payPass = isset($params['pay_password']) ? trim($params['pay_password']) : '';
  19. if ($mobile && Member::where(['user_login' => $mobile])->value('id')) {
  20. return 2001;
  21. }
  22. $userName = $mobile ? $mobile : makeUserName();
  23. $userPass = $userPass ? cmf_password($userPass) : cmf_password('123456');
  24. $payPass = $payPass ? cmf_password($payPass) : $userPass;
  25. $wxInfo = isset($params['wxInfo']) ? $params['wxInfo'] : [];
  26. $openid = isset($wxInfo['openid']) ? trim($wxInfo['openid']) : '';
  27. $headimgurl = isset($wxInfo['headimgurl']) ? trim($wxInfo['headimgurl']) : '';
  28. $nickname = isset($wxInfo['nickname']) ? trim($wxInfo['nickname']) : $userName;
  29. if (empty($openid)) {
  30. return false;
  31. }
  32. $data = [
  33. 'user_type' => 2,
  34. 'sex' => isset($params['sex']) ? intval($params['sex']) : 0,
  35. 'openid' => $openid,
  36. 'parent_id' => isset($params['parent_id']) ? intval($params['parent_id']) : 0,
  37. 'user_nickname' => isset($params['user_nickname']) ? trim($params['user_nickname']) : $nickname,
  38. 'avatar' => isset($params['avatar']) ? trim($params['avatar']) : $headimgurl,
  39. 'user_login' => $userName,
  40. 'user_pass' => $userPass,
  41. 'pay_password' => $payPass,
  42. 'is_follow' => isset($wxInfo['subscribe']) ? intval($wxInfo['subscribe']) : 0,
  43. 'last_login_ip' => get_client_ip(),
  44. 'last_login_time' => time(),
  45. 'create_time' => time(),
  46. ];
  47. if ($mobile) {
  48. $data['mobile'] = $mobile;
  49. }
  50. $userId = Member::where(['openid' => $openid])
  51. ->where('user_status', '>=', 0)
  52. ->value('id');
  53. PRedis::set('test:' . $openid, $data, 600);
  54. if (!$userId) {
  55. $userId = Member::insertGetId($data);
  56. return ['userId' => $userId, 'type' => 2];
  57. } else {
  58. $data = [
  59. 'last_login_ip' => get_client_ip(),
  60. 'updated_at' => date('Y-m-d H:i:s'),
  61. ];
  62. if(empty($data['user_nickname'])){
  63. unset($data['user_nickname']);
  64. }
  65. if(empty($data['avatar'])){
  66. unset($data['avatar']);
  67. }
  68. Member::where(['openid' => $openid])
  69. ->where('user_status', '>=', 0)
  70. ->update($data);
  71. return false;
  72. }
  73. }
  74. /**
  75. * 用户登录
  76. * @param $params
  77. * @return array|int|null|\PDOStatement|string|Model
  78. * @throws \think\Exception
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. * @throws \think\exception\DbException
  82. * @throws \think\exception\PDOException
  83. */
  84. public static function login($params)
  85. {
  86. $userName = isset($params['mobile']) ? trim($params['mobile']) : '';
  87. $password = isset($params['password']) ? trim($params['password']) : '';
  88. $field = 'id,user_login,sex,user_nickname,avatar,openid,user_pass,user_type,score,balance,user_status';
  89. $userInfo = Member::where(['user_login' => $userName])->field($field)->find();
  90. $userPass = isset($userInfo['user_pass']) ? $userInfo['user_pass'] : '';
  91. $userStatus = isset($userInfo['user_status']) ? intval($userInfo['user_status']) : 0;
  92. if (empty($userInfo)) {
  93. return 2100;
  94. }
  95. $userInfo = $userInfo->toArray();
  96. if (!cmf_compare_password($password, $userPass)) {
  97. return 2101;
  98. }
  99. if ($userStatus != 1) {
  100. return 2102;
  101. }
  102. // 更新登录数据
  103. $updateData = [
  104. 'updated_at' => date('Y-m-d H:i:s'),
  105. 'last_login_ip' => get_client_ip(),
  106. 'last_login_time' => time(),
  107. ];
  108. $curOpenId = session('openid');
  109. $openid = isset($userInfo['openid']) ? trim($userInfo['openid']) : '';
  110. if ($curOpenId) {
  111. $checkId = Member::where(['openid' => $curOpenId])->value('id');
  112. if ((empty($checkId) && empty($openid)) || ($checkId && $checkId == $userInfo['id'])) {
  113. $wxInfo = session('wxInfo');
  114. $headimgurl = isset($wxInfo['headimgurl']) ? trim($wxInfo['headimgurl']) : '';
  115. $nickname = isset($wxInfo['nickname']) ? trim($wxInfo['nickname']) : $userName;
  116. $sex = isset($wxInfo['sex']) ? intval($wxInfo['sex']) : 0;
  117. if (empty($openid)) {
  118. $updateData['openid'] = $curOpenId;
  119. }
  120. if ($headimgurl && $userInfo['avatar'] != $headimgurl) {
  121. $updateData['avatar'] = $headimgurl;
  122. }
  123. if ($nickname && $userInfo['user_nickname'] != $nickname) {
  124. $updateData['user_nickname'] = $nickname;
  125. }
  126. if ($sex && $userInfo['sex'] != $sex) {
  127. $updateData['sex'] = $sex;
  128. }
  129. }
  130. }
  131. Member::where(['user_login' => $userName])->update($updateData);
  132. unset($userInfo['user_pass']);
  133. $userInfo = array_merge($userInfo, $updateData);
  134. session('userInfo', $userInfo);
  135. return $userInfo;
  136. }
  137. /**
  138. * 获取会员信息
  139. * @param $where 条件
  140. * @param string $field 字段
  141. * @return array|false|\PDOStatement|string|Model
  142. */
  143. public static function getInfo($where, $field = "")
  144. {
  145. $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';
  146. $info = self::where($where)->field($field)->order('user_status desc,id desc')->find();
  147. return $info ? $info->toArray() : [];
  148. }
  149. /**
  150. * 获取用户主页信息
  151. * @param $userId 用户ID
  152. * @param string $field 字段
  153. * @return array
  154. */
  155. public static function getHomeInfo($userId, $field = '', $type = 0)
  156. {
  157. $where = ['m.id' => $userId, 'm.user_status' => 1, 'm.user_type' => 2];
  158. $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';
  159. if ($type == 3 || $type == 4) {
  160. $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';
  161. } else if ($type == 2) {
  162. $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';
  163. }
  164. if ($type == 6) {
  165. unset($where['m.user_status']);
  166. }
  167. $field = $field ? $field : $defaultField;
  168. $info = Member::alias('m')
  169. ->join('user_profile up', 'up.userid=m.id', 'left')
  170. ->field($field)
  171. ->where($where)
  172. ->find();
  173. if ($info) {
  174. if (isset($info['avatar'])) {
  175. $info['avatar'] = cmf_get_image_preview_url($info['avatar']);
  176. }
  177. if (isset($info['brief'])) {
  178. $info['brief'] = htmlspecialchars_decode($info['brief']);
  179. }
  180. if (isset($info['sex'])) {
  181. $sexs = ['', '男', '女'];
  182. $sex = isset($info['sex']) ? $info['sex'] : 0;
  183. $info['sex_txt'] = isset($sexs[$sex]) ? $sexs[$sex] : '';
  184. }
  185. $info['albums'] = [];
  186. $photolist = isset($info['photolist']) ? $info['photolist'] : '';
  187. $photolist = $photolist ? explode(',', $photolist) : [];
  188. if ($photolist) {
  189. $albums = [];
  190. foreach ($photolist as $k => $val) {
  191. $albums[] = cmf_get_image_preview_url($val);
  192. }
  193. $info['albums'] = $albums;
  194. }
  195. if (isset($info['birthday'])) {
  196. $birthday = isset($info['birthday']) ? $info['birthday'] : 0;
  197. $info['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
  198. $info['birthday_code'] = $birthday ? date('Y-m-d', $birthday) : '';
  199. $info['birthday_day'] = $birthday ? date('y年', $birthday) : '';
  200. $year = $birthday ? date('Y', $birthday) : 0;
  201. if ($year) {
  202. $info['age'] = date('Y') - $year;
  203. $info['year'] = $year;
  204. }
  205. $month = date('m-d', $birthday);
  206. $info['start'] = getStart($month);
  207. }
  208. $info['height_txt'] = '';
  209. if (isset($info['height'])) {
  210. $height = isset($info['height']) ? intval($info['height']) : 0;
  211. $info['height'] = intval($height);
  212. $info['height_txt'] = $height ? intval($info['height']) . 'CM' : '无';
  213. }
  214. $info['weight_txt'] = '';
  215. if (isset($info['weight'])) {
  216. $weight = isset($info['weight']) ? intval($info['weight']) : 0;
  217. $info['weight'] = intval($weight);
  218. $info['weight_txt'] = $weight ? intval($info['weight']) . 'KG' : '无';
  219. }
  220. // 学历
  221. $info['education_txt'] = '';
  222. if (isset($info['education'])) {
  223. $educations = config('weixin.educations');
  224. $edu = isset($info['education']) ? $info['education'] : 0;
  225. $info['education_txt'] = isset($educations[$edu]) ? $educations[$edu] : '无';
  226. }
  227. // 资产
  228. $info['property_txt'] = '暂无';
  229. $property = isset($info['property']) ? $info['property'] : 0;
  230. if (isset($info['property'])) {
  231. $propertys = config('weixin.propertys');
  232. $info['property'] = $property;
  233. $info['property_txt'] = $property && isset($propertys[$property]) ? $propertys[$property] : '暂无';
  234. }
  235. // 地址
  236. $item['now_address'] = '';
  237. if (isset($info['province'])) {
  238. $province = isset($info['province']) ? trim($info['province']) : '';
  239. $city = isset($info['city']) ? trim($info['city']) : '';
  240. $info['now_address'] = trim($province . ' ' . $city);
  241. if ($type == 2) {
  242. $district = isset($info['district']) ? trim($info['district']) : '';
  243. $info['now_address'] = $info['now_address'] . ' ' . $district;
  244. }
  245. }
  246. $item['home_address'] = '';
  247. if (isset($info['home_province'])) {
  248. $homeProvince = isset($info['home_province']) ? trim($info['home_province']) : '';
  249. $homeCity = isset($info['home_city']) ? trim($info['home_city']) : '';
  250. $info['home_address'] = trim($homeProvince . ' ' . $homeCity);
  251. if ($type == 2) {
  252. $homeDistrict = isset($info['home_district']) ? trim($info['home_district']) : '';
  253. $info['home_address'] = $info['home_address'] . ' ' . $homeDistrict;
  254. }
  255. }
  256. // 格式化婚姻状况
  257. $item['married_txt'] = '';
  258. if (isset($info['married'])) {
  259. $marrieds = config('weixin.marrieds');
  260. $married = $info['married'] ? intval($info['married']) : 0;
  261. $info['married_txt'] = $married > 0 && isset($marrieds[$married - 1]) ? $marrieds[$married - 1] : '无';
  262. }
  263. // 隐藏手机号
  264. if ($type > 0 && isset($info['mobile'])) {
  265. /*if($type==2){
  266. $info['mobile_txt'] = $info['mobile'];
  267. }*/
  268. $info['mobile'] = $info['mobile'] ? formatStr($info['mobile']) : '';
  269. }
  270. if ($type == 4) {
  271. $info['introduce'] = isset($info['introduce']) ? str_replace("\n", "<br/>", $info['introduce']) : '';
  272. $info['family'] = isset($info['family']) ? str_replace(["\n", "\s"], ["<br/>", "&nbsp;"], $info['family']) : '';
  273. $info['hobby'] = isset($info['hobby']) ? str_replace(["\n", "\s"], ["<br/>", "&nbsp;"], $info['hobby']) : '';
  274. $info['purpose'] = isset($info['purpose']) ? str_replace(["\n", "\s"], ["<br/>", "&nbsp;"], $info['purpose']) : '';
  275. $info['cause'] = isset($info['cause']) ? str_replace(["\n", "\s"], ["<br/>", "&nbsp;"], $info['cause']) : '';
  276. $info['expect'] = isset($info['expect']) ? str_replace(["\n", "\s"], ["<br/>", "&nbsp;"], $info['expect']) : '';
  277. // VIP
  278. $vipAuth = isset($info['vip_auth']) ? intval($info['vip_auth']) : 0;
  279. $vipExpire = isset($info['vip_expire']) ? intval($info['vip_expire']) : 0;
  280. if ($vipAuth && $vipExpire >= time()) {
  281. $info['vip_auth'] = 1;
  282. $info['vip_expire'] = date('Y-m-d', $info['vip_expire']);
  283. } else {
  284. $info['vip_auth'] = 0;
  285. $info['vip_expire'] = '';
  286. }
  287. }
  288. if ($type == 1) {
  289. $accountConfig = $siteInfo = cmf_get_option('account_config');
  290. $logoutPay = isset($accountConfig['logout_pay']) ? floatval($accountConfig['logout_pay']) : 0;
  291. $info['logout_pay'] = $logoutPay > 0 ? $logoutPay : 1;
  292. }
  293. // 个性标签
  294. if (isset($info['tags'])) {
  295. $info['tags'] = $info['tags'] ? explode(',', $info['tags']) : [];
  296. }
  297. if ($type == 3 || $type == 4) {
  298. if (isset($info['introduce_img'])) {
  299. $info['introduce_img_preview'] = $info['introduce_img'] ? cmf_get_image_preview_url($info['introduce_img']) : '';
  300. }
  301. if (isset($info['family_img'])) {
  302. $info['family_img_preview'] = $info['family_img'] ? cmf_get_image_preview_url($info['family_img']) : '';
  303. }
  304. if (isset($info['hobby_img'])) {
  305. $info['hobby_img_preview'] = $info['hobby_img'] ? cmf_get_image_preview_url($info['hobby_img']) : '';
  306. }
  307. if (isset($info['purpose_img'])) {
  308. $info['purpose_img_preview'] = $info['purpose_img'] ? cmf_get_image_preview_url($info['purpose_img']) : '';
  309. }
  310. if (isset($info['cause_img'])) {
  311. $info['cause_img_preview'] = $info['cause_img'] ? cmf_get_image_preview_url($info['cause_img']) : '';
  312. }
  313. if (isset($info['expect_img'])) {
  314. $info['expect_img_preview'] = $info['expect_img'] ? cmf_get_image_preview_url($info['expect_img']) : '';
  315. }
  316. }
  317. }
  318. return $info ? $info->toArray() : [];
  319. }
  320. /**
  321. * 记录访问记录
  322. * @param $userId
  323. * @param $accessUid
  324. * @return false|int|string
  325. * @throws \think\Exception
  326. * @throws \think\db\exception\DataNotFoundException
  327. * @throws \think\db\exception\ModelNotFoundException
  328. * @throws \think\exception\DbException
  329. * @throws \think\exception\PDOException
  330. */
  331. public static function makeUserAccess($userId, $accessUid)
  332. {
  333. if (($userId == $accessUid) || empty($userId) || empty($accessUid)) {
  334. return false;
  335. }
  336. $lockCacheKey = "caches:access:{$userId}_{$accessUid}";
  337. if (PRedis::get($lockCacheKey)) {
  338. return false;
  339. }
  340. $checkData = UserAccess::where(['user_id' => $userId, 'access_uid' => $accessUid, 'status' => 1])
  341. ->field('id,created_at,status')
  342. ->find();
  343. $checkData = $checkData ? $checkData->toArray() : [];
  344. $timeAt = isset($checkData['created_at']) ? $checkData['created_at'] : '';
  345. if ($checkData && $timeAt && $timeAt >= date('Y-m-d H:i:s', time() - rand(10, 20))) {
  346. PRedis::set($lockCacheKey, $checkData, rand(10, 20));
  347. return false;
  348. }
  349. $data = ['user_id' => $userId, 'access_uid' => $accessUid, 'created_at' => date('Y-m-d H:i:s')];
  350. if (empty($checkData)) {
  351. return UserAccess::insertGetId($data);
  352. } else {
  353. return UserAccess::where(['user_id' => $userId, 'access_uid' => $accessUid])->update(['created_at' => date('Y-m-d H:i:s')]);
  354. }
  355. }
  356. /**
  357. * 获取用户访问记录
  358. * @param $userId 被访问用户
  359. * @param $pageSize
  360. * @return array
  361. */
  362. public static function getUserAccess($userId, $pageSize)
  363. {
  364. $dataList = UserAccess::alias('ua')
  365. ->join('user u', 'u.id=ua.user_id', 'left')
  366. ->join('user au', 'au.id=ua.access_uid', 'left')
  367. ->join('user_profile up', 'up.userid=ua.access_uid', 'left')
  368. ->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')
  369. ->where(['ua.user_id' => $userId, 'ua.status' => 1, 'au.user_status' => 1])
  370. ->order('ua.created_at desc,ua.id desc')
  371. ->paginate($pageSize)
  372. ->each(function ($item, $key) {
  373. $birthday = $item['birthday'];
  374. $myBirthday = $item['my_birthday'];
  375. $month = date('m-d', $birthday);
  376. $myMonth = date('m-d', $myBirthday);
  377. $start = getStart($month);
  378. $mstart = getStart($myMonth);
  379. $item['birtyday'] = $month;
  380. $item['my_birtyday'] = $myMonth;
  381. $item['start'] = $start == $mstart ? '同是' . $start : $start;
  382. $item['mstart'] = $mstart;
  383. $year = date('y年', $birthday);
  384. $item['year'] = $year;
  385. $item['city'] = str_replace('市', '', $item['city']);
  386. $item['created_at_text'] = format_time(strtotime($item['created_at']));
  387. $nickname = $item['user_nickname'] ? $item['user_nickname'] : $item['user_login'];
  388. $item['user_nickname'] = $item['is_lock'] ? formatName($nickname) : $nickname;
  389. if (isset($item['avatar'])) {
  390. $item['avatar'] = cmf_get_image_preview_url($item['avatar']);
  391. }
  392. });
  393. $dataList = $dataList ? $dataList->toArray() : ['data' => []];
  394. // 获取今日访问
  395. $dataList['todayCount'] = UserAccess::where(['user_id' => $userId, 'status' => 1])
  396. ->where('created_at', 'like', "%" . date('Y-m-d') . "%")
  397. ->count('id');
  398. return $dataList;
  399. }
  400. /**
  401. * 获取访问用户数
  402. * @param $userId
  403. * @return float|int|string
  404. */
  405. public static function getAccessCount($userId)
  406. {
  407. return UserAccess::where(['user_id' => $userId, 'status' => 1])
  408. ->count('id');
  409. }
  410. /**
  411. * 获取推荐会员数量
  412. * @param $userId
  413. * @return float|string
  414. */
  415. public static function getInviteCount($userId)
  416. {
  417. return Member::where(['parent_id' => $userId, 'user_type' => 2, 'user_status' => 1])->count('id');
  418. }
  419. /**
  420. * 验证获取验证用户信息
  421. * @param $account
  422. * @return array|bool
  423. * @throws \think\db\exception\DataNotFoundException
  424. * @throws \think\db\exception\ModelNotFoundException
  425. * @throws \think\exception\DbException
  426. */
  427. public static function checkUserInfo($account)
  428. {
  429. $cacheKey = "cache:users:check_{$account}";
  430. $info = PRedis::get($cacheKey);
  431. if ($info) {
  432. return $info;
  433. }
  434. $field = 'id,openid,user_type,agent_type,agent_status,user_status,freezing_choose,is_reg_profile';
  435. $info = self::where(['openid|id' => $account, 'user_type' => 2])->field($field)->order('user_status desc,id desc')->find();
  436. $info = $info ? $info->toArray() : [];
  437. if ($info) {
  438. PRedis::set($cacheKey, $info, 5);
  439. }
  440. return $info;
  441. }
  442. /**
  443. * 获取邀请的分销用户信息
  444. * @param $userId 当前用户ID
  445. * @param string $field 返回字段,连表u-当前用户,uu连表用户
  446. * @return array
  447. * @throws \think\db\exception\DataNotFoundException
  448. * @throws \think\db\exception\ModelNotFoundException
  449. * @throws \think\exception\DbException
  450. */
  451. public static function getInviteInfo($userId, $field = '')
  452. {
  453. $field = $field ? $field : 'u.id,uu.id as invite_id,uu.openid,uu.parent_id';
  454. $info = Member::alias('u')
  455. ->leftJoin('user uu', 'uu.id=u.parent_id')
  456. ->where(['u.id' => $userId, 'uu.user_type' => 2, 'uu.agent_type' => 1, 'uu.agent_status' => 1])
  457. ->field($field)
  458. ->find();
  459. return $info ? $info->toArray() : [];
  460. }
  461. /**
  462. * 充值记录
  463. * @param $params
  464. * @param int $pageSize
  465. * @return mixed
  466. */
  467. public static function getRechargeLog($params, $pageSize = 15)
  468. {
  469. $where = [];
  470. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  471. if ($userId) {
  472. $where['user_id'] = $userId;
  473. }
  474. $cwhere = $where;
  475. $cwhere['is_read'] = 2;
  476. $count = db('user_recharge_log')
  477. ->where($cwhere)
  478. ->where('status', 'gt', 0)
  479. ->count('id');
  480. if ($count) {
  481. db('user_recharge_log')
  482. ->where($cwhere)
  483. ->where('status', 'gt', 0)
  484. ->update(['is_read' => 1, 'updated_at' => date('Y-m-d H:i:s')]);
  485. }
  486. $dataList = db('user_recharge_log')
  487. ->where($where)
  488. ->field('id,type,order_sn,pay_type,money,give_num,pay_money,balance,remark,created_at,status')
  489. ->where('status', 'gt', 0)
  490. ->order("created_at desc")
  491. ->paginate($pageSize)
  492. ->each(function ($item, $k) {
  493. $item['format_time'] = date('Y年m月d日 H点i分', strtotime($item['created_at']));
  494. $item['money'] = in_array($item['type'], [1, 4, 5]) ? intval($item['money']) : $item['money'];
  495. return $item;
  496. });
  497. return $dataList ? $dataList->toArray() : [];
  498. }
  499. /**
  500. * 充值记录数
  501. * @param $userId
  502. * @param int $type
  503. * @return mixed
  504. */
  505. public static function getRechargeCount($userId, $type = 0)
  506. {
  507. $where = ['user_id' => $userId, 'is_read' => 2];
  508. if ($type) {
  509. $where['type'] = $type;
  510. }
  511. return db('user_recharge_log')
  512. ->where($where)
  513. ->where('status', 'gt', 0)
  514. ->count('id');
  515. }
  516. /**
  517. * 推荐用户
  518. * @param $params 参数
  519. * @param $page 分页
  520. * @param $pageSize 分页大小
  521. * @param string $field 字段
  522. * @return array|null|\PDOStatement|string|Model
  523. */
  524. public static function getRecommendList($params, $pageSize, $field = '')
  525. {
  526. $page = input('page', 1);
  527. $cacheKey = "cache:recommends:index_" . $page . '_:' . md5(json_encode($params) . $pageSize . $field);
  528. $dataList = PRedis::get($cacheKey);
  529. if ($dataList) {
  530. return $dataList;
  531. }
  532. $where = ['m.is_tuijian' => 1, 'is_heart' => 1, 'm.is_reg_profile' => 1, 'm.user_status' => 1, 'm.user_type' => 2];
  533. // 匹配当前用户信息:性别
  534. $sex = 0;
  535. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  536. if ($userId) {
  537. $sex = Member::where(['id' => $userId])->value('sex');
  538. if ($sex > 0) {
  539. $sex = $sex == 1 ? 2 : 1;
  540. }
  541. }
  542. $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';
  543. // 距离
  544. $info = Member::where(['id' => $userId])->field('lat,lng')->find();
  545. $lat = isset($params['lat']) ? $params['lat'] : 0.00;
  546. $lng = isset($params['lng']) ? $params['lng'] : 0.00;
  547. $lat = $lat ? $lat : (isset($info['lat']) ? $info['lat'] : 0);
  548. $lng = $lng ? $lng : (isset($info['lng']) ? $info['lng'] : 0);
  549. if ($lat && $lng) {
  550. $field .= ",ROUND(
  551. 6378.138 * 2 * ASIN(
  552. SQRT(
  553. POW(
  554. SIN(
  555. (
  556. {$lat} * PI() / 180 - m.`lat` * PI() / 180
  557. ) / 2
  558. ),
  559. 2
  560. ) + COS({$lat} * PI() / 180) * COS(m.`lat` * PI() / 180) * POW(
  561. SIN(
  562. (
  563. {$lng} * PI() / 180 - m.`lng` * PI() / 180
  564. ) / 2
  565. ),
  566. 2
  567. )
  568. )
  569. ) * 1000
  570. ) AS distance";
  571. }
  572. $dataList = Member::alias('m')
  573. ->join('user_profile up', 'up.userid=m.id', 'left')
  574. ->field($field)
  575. ->where($where)
  576. ->where(function ($query) use ($sex, $params) {
  577. // 性别
  578. if ($sex > 0) {
  579. $query->where('m.sex', 'in', [0, $sex]);
  580. }
  581. // 年龄
  582. $age = isset($params['age']) ? trim($params['age']) : '';
  583. if ($age) {
  584. $ages = explode('~', $age);
  585. $age1 = isset($ages[0]) ? intval($ages[0]) : 0;
  586. $age2 = isset($ages[1]) ? intval($ages[1]) : 0;
  587. if ($age2) {
  588. $query->where('up.age', '>=', $age1)->where('up.age', '<=', $age2);
  589. } else if ($age1) {
  590. $query->where('up.age', '>=', $age1);
  591. }
  592. }
  593. // 身高
  594. $height = isset($params['height']) ? trim($params['height']) : '';
  595. if ($height) {
  596. $heights = explode('~', $height);
  597. $height1 = isset($heights[0]) ? intval($heights[0]) : 0;
  598. $height2 = isset($heights[1]) ? intval($heights[1]) : 0;
  599. if ($height2) {
  600. $query->where('up.height', '>=', $height1)->where('up.height', '<=', $height2);
  601. } else if ($height1) {
  602. $query->where('up.height', '>=', $height1);
  603. }
  604. }
  605. // 学历
  606. $education = isset($params['education']) ? intval($params['education']) : 0;
  607. if ($education > 0) {
  608. $op = $education == 3 ? '>=' : '=';
  609. $query->where('up.education', $op, $education);
  610. }
  611. // 婚姻状况
  612. $married = isset($params['married']) ? intval($params['married']) : 0;
  613. if ($married > 0) {
  614. $query->where('up.married', '=', $married);
  615. }
  616. return $query;
  617. })
  618. ->where('m.id', '>', 0)
  619. ->where('m.id', 'not in', $userId)
  620. ->order('m.tuijian_time desc,m.id')
  621. ->paginate($pageSize)
  622. ->each(function ($item, $k) {
  623. $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
  624. $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
  625. $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
  626. $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
  627. $height = isset($item['height']) ? intval($item['height']) : 0;
  628. $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '';
  629. $weight = isset($item['weight']) ? intval($item['weight']) : 0;
  630. $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '';
  631. // VIP
  632. $vipAuth = isset($item['vip_auth']) ? intval($item['vip_auth']) : 0;
  633. $vipExpire = isset($item['vip_expire']) ? intval($item['vip_expire']) : 0;
  634. if ($vipAuth && $vipExpire >= time()) {
  635. $item['vip_auth'] = 1;
  636. $item['vip_expire'] = date('Y-m-d', $item['vip_expire']);
  637. } else {
  638. $item['vip_auth'] = 0;
  639. $item['vip_expire'] = '';
  640. }
  641. // 学历
  642. $educations = config('weixin.educations');
  643. $edu = isset($item['education']) ? $item['education'] : 0;
  644. $item['education_txt'] = $edu && isset($educations[$edu]) ? $educations[$edu] : '';
  645. // 地址
  646. $province = isset($item['province']) ? trim($item['province']) : '';
  647. $city = isset($item['city']) ? trim($item['city']) : '';
  648. $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
  649. $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
  650. $item['now_address'] = trim($province . ' ' . $city);
  651. $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
  652. $albums = [];
  653. $photolist = isset($item['photolist']) ? $item['photolist'] : '';
  654. $photolist = $photolist ? explode(',', $photolist) : [];
  655. if ($photolist) {
  656. foreach ($photolist as $k => $val) {
  657. $albums[] = cmf_get_image_preview_url($val);
  658. }
  659. }
  660. $item['pic_count'] = count($albums);
  661. // 坐标距离
  662. $item['distance'] = isset($item['distance']) ? $item['distance'] : '';
  663. if ($item['lat'] <= 0 || $item['lng'] <= 0) {
  664. $item['distance'] = '';
  665. } else {
  666. $item['distance'] = $item['distance'] >= 1000 ? round($item['distance'] / 1000, 2) . 'km' : ($item['distance'] ? $item['distance'] . 'm' : '');
  667. }
  668. // 地址
  669. $address = isset($item['address']) ? $item['address'] : '';
  670. $addressData = $address ? explode(',', $address) : [];
  671. $item['district'] = isset($addressData[2]) ? $addressData[2] : '';
  672. return $item;
  673. });
  674. $dataList = $dataList ? $dataList->toArray() : [];
  675. //PRedis::set('recommends:query:' . $userId, Db::name('user')->getLastSql(), 600);
  676. PRedis::set($cacheKey, $dataList, 10);
  677. return $dataList;
  678. }
  679. /**
  680. * 获取首页推荐列表
  681. * @param $params
  682. * @param $pageSize
  683. * @param string $field
  684. * @return array|bool
  685. * @throws \think\exception\DbException
  686. */
  687. public static function getTopList($params, $pageSize, $field = '')
  688. {
  689. $page = input('page', 1);
  690. $cacheKey = "cache:topList:index_" . $page . '_:' . md5(json_encode($params) . $pageSize . $field);
  691. $dataList = PRedis::get($cacheKey);
  692. if ($dataList) {
  693. return $dataList;
  694. }
  695. $sex = 0;
  696. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  697. if ($userId) {
  698. $sex = Member::where(['id' => $userId])->value('sex');
  699. if ($sex > 0) {
  700. $sex = $sex == 1 ? 2 : 1;
  701. }
  702. }
  703. $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';
  704. $where = ['m.is_top' => 1, 'm.is_heart' => 1, 'm.is_reg_profile' => 1, 'm.user_status' => 1, 'm.user_type' => 2];
  705. $dataList = Member::alias('m')
  706. ->join('user_profile up', 'up.userid=m.id', 'left')
  707. ->field($field)
  708. ->where($where)
  709. // ->where('m.sex', 'in', [0, $sex])
  710. ->where('m.top_expire', '>=', date('Y-m-d H:i:s'))
  711. ->where('m.id', '>', 0)
  712. // ->where('m.id', 'not in', $userId)
  713. ->order('m.top_expire desc,m.id')
  714. ->paginate($pageSize)
  715. ->each(function ($item, $k) {
  716. $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
  717. $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
  718. $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
  719. $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
  720. return $item;
  721. });
  722. $dataList = $dataList ? $dataList->toArray() : [];
  723. PRedis::set($cacheKey, $dataList, rand(3, 5));
  724. return $dataList;
  725. }
  726. /**
  727. * 怦然心动推荐用户
  728. * @param $userId 当前用户
  729. * @param string $field 字段
  730. * @return array|null|\PDOStatement|string|Model
  731. */
  732. public static function getHeartList($userId, $field = '', $refresh = false)
  733. {
  734. // 获取已经推荐的有效数据
  735. $updated = false;
  736. $dataList = [];
  737. $cacheKey = "hearts:recommend:" . $userId;
  738. $recommendIds = PRedis::get($cacheKey);
  739. $where = ['m.is_heart' => 1, 'm.is_reg_profile' => 1, 'm.user_status' => 1, 'm.user_type' => 2];
  740. $heartAt = Member::where(['id' => $userId])->value('heart_recommend_at');
  741. $heartAt = $heartAt ? date('Y-m-d', strtotime($heartAt)) : '';
  742. $ids = [];
  743. if (!PRedis::exists($cacheKey) || $refresh) {
  744. // 验证今天是否推荐过
  745. /*$heartAt = Member::where(['id' => $userId])->value('heart_recommend_at');
  746. $heartAt = $heartAt ? date('Y-m-d', strtotime($heartAt)) : '';
  747. if ($heartAt == date('Y-m-d')) {
  748. return true;
  749. }*/
  750. // 获取推荐的人数设置
  751. $updated = true;
  752. $siteInfo = $siteInfo = cmf_get_site_info();
  753. $ageGap = isset($siteInfo['age_gap']) ? intval($siteInfo['age_gap']) : 0;
  754. $ageGap = $ageGap ? $ageGap : 0;
  755. $recommendNum = isset($siteInfo['recommend_num']) ? intval($siteInfo['recommend_num']) : 0;
  756. $recommendNum = $recommendNum ? $recommendNum : 1;
  757. // 用户性别
  758. $memberInfo = Member::getHomeInfo($userId, 'm.sex,up.age,up.city,m.vip_auth,m.vip_expire,up.home_province');
  759. $sex = isset($memberInfo['sex']) ? intval($memberInfo['sex']) : 0;
  760. if ($sex > 0) {
  761. $sex = $sex == 1 ? 2 : 1;
  762. }
  763. // 调整VIP推荐人数
  764. $isVip = false;
  765. $vipRecommendNum = isset($siteInfo['vip_recommend_num']) ? intval($siteInfo['vip_recommend_num']) : 0;
  766. $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0;
  767. $vipExpire = isset($memberInfo['vip_expire']) ? intval($memberInfo['vip_expire']) : 0;
  768. if ($vipAuth && $vipExpire >= time() && $vipRecommendNum > 0) {
  769. $isVip = true;
  770. $recommendNum = $vipRecommendNum;
  771. }
  772. $conditions = Member::getMemberConditions($userId);
  773. //var_dump($conditions);
  774. $hasMatchData = Predis::get("hearts:hasMeatch:{$userId}");
  775. $ids = isset($hasMatchData['ids']) ? $hasMatchData['ids'] : '';
  776. $ids = $ids ? explode(',', $ids) : [];
  777. $hasExpire = isset($hasMatchData['expire']) ? $hasMatchData['expire'] : 0;
  778. /*if (time() > $hasExpire || !$isVip) {
  779. $ids = [];
  780. }*/
  781. // vip会员只推荐身份认证的
  782. if ($isVip) {
  783. $where['up.idcard_check'] = 2;
  784. }
  785. $recommendIds = Member::alias('m')
  786. ->join('user_profile up', 'up.userid=m.id', 'left')
  787. ->where($where)
  788. ->where(function ($query) use ($sex) {
  789. return $query->where('m.sex', 'in', [0, $sex]);
  790. })
  791. ->where(function ($query) use ($ids) {
  792. // 过滤一星期内已经推荐过的
  793. if ($ids) {
  794. return $query->where('m.id', 'not in', $ids);
  795. } else {
  796. return $query->where('m.id', '>', 0);
  797. }
  798. })
  799. ->where(function ($query) use ($conditions, $memberInfo, $ageGap) {
  800. $querys = $query;
  801. // 年龄
  802. $year = isset($conditions['year']) ? trim($conditions['year']) : '';
  803. if ($year) {
  804. $year = explode('~', $year);
  805. $data1 = isset($year[0]) ? intval($year[0]) : 0;
  806. $data2 = isset($year[1]) ? intval($year[1]) : 0;
  807. if ($data2) {
  808. $data1 = $data1 ? strtotime($data1 . '-01-01') : 0;
  809. $data2 = $data2 ? strtotime(($data2 + 1) . '-01-01') : 0;
  810. $querys = $querys->where('m.birthday', '>=', $data1 . '')->where('m.birthday', '<=', $data2);
  811. } else if ($data1) {
  812. $data1 = $data1 ? strtotime($data1 . '-01-01') : 0;
  813. $querys = $querys->where('m.birthday', '>=', $data1);
  814. }
  815. }
  816. // 身高
  817. $height = isset($conditions['height']) ? trim($conditions['height']) : '';
  818. if ($height) {
  819. $height = explode('~', $height);
  820. $data1 = isset($height[0]) ? intval($height[0]) : 0;
  821. $data2 = isset($height[1]) ? intval($height[1]) : 0;
  822. if ($data2) {
  823. $querys = $querys->where('up.height', '>=', $data1)->where('up.height', '<=', $data2);
  824. } else if ($data1) {
  825. $querys = $querys->where('up.height', '>=', $data1);
  826. }
  827. }
  828. // 体重
  829. $weight = isset($conditions['weight']) ? intval($conditions['weight']) : 0;
  830. if ($weight) {
  831. $dataArr = config('weixin.weights');
  832. $datas = isset($dataArr[$weight]) ? $dataArr[$weight] : [];
  833. $data1 = isset($datas['value1']) ? $datas['value1'] : 0;
  834. $data2 = isset($datas['value2']) ? $datas['value2'] : 0;
  835. if ($data2) {
  836. $querys = $querys->where('up.weight', '>=', $data1)->where('up.weight', '<=', $data2);
  837. } else if ($data1) {
  838. $querys = $querys->where('up.weight', '>=', $data1);
  839. }
  840. }
  841. // 收入
  842. $salary = isset($conditions['salary']) ? intval($conditions['salary']) : 0;
  843. if ($salary) {
  844. $dataArr = config('weixin.salarys');
  845. $datas = isset($dataArr[$salary]) ? $dataArr[$salary] : [];
  846. $data1 = isset($datas['value']) ? $datas['value'] : 0;
  847. $data2 = isset($datas['value2']) ? $datas['value2'] : 0;
  848. if ($data2) {
  849. $querys = $querys->where('up.salary', '>=', $data1)->where('up.salary', '<=', $data2);
  850. } else if ($data1) {
  851. $querys = $querys->where('up.salary', '=', $data1);
  852. }
  853. }
  854. // 所在城市
  855. $city = isset($memberInfo['city']) ? trim($memberInfo['city']) : '';
  856. $cityValue = isset($conditions['city']) ? trim($conditions['city']) : '';
  857. if ($cityValue > 0 && $city) {
  858. $querys = $querys->where('up.city', '=', $city);
  859. }
  860. // 家乡省市:是否同省
  861. $homeProvince = isset($memberInfo['home_province']) ? trim($memberInfo['home_province']) : '';
  862. $homeProvinceValue = isset($conditions['home_province']) ? trim($conditions['home_province']) : '';
  863. if ($homeProvinceValue > 0 && $homeProvince) {
  864. $querys = $querys->where('up.home_province', '=', $homeProvince);
  865. }
  866. // 学历
  867. $education = isset($conditions['education']) ? intval($conditions['education']) : 0;
  868. if ($education > 0) {
  869. // 硕士及以上或按学历匹配
  870. $op = $education == 3 ? '>=' : '=';
  871. $querys = $querys->where('up.education', $op, $education);
  872. }
  873. // 婚姻状态
  874. $married = isset($conditions['married']) ? intval($conditions['married']) : 0;
  875. if ($married > 0) {
  876. $querys = $querys->where('up.married', '>=', $married);
  877. }
  878. return $querys;
  879. })
  880. ->orderRaw('rand()')
  881. ->limit($recommendNum)
  882. ->column('m.id');
  883. PRedis::set('hearts:query:' . $userId, ['where' => $where, 'ids' => $ids, 'query' => Member::getLastSql()], 24 * 3600);
  884. if (empty($recommendIds)) {
  885. $recommendIds = Member::alias('m')
  886. ->join('user_profile up', 'up.userid=m.id', 'left')
  887. ->where($where)
  888. ->where(function ($query) use ($sex) {
  889. return $query->where('m.sex', 'in', [0, $sex]);
  890. })
  891. ->where(function ($query) use ($ids) {
  892. // 过滤一星期内已经推荐过的
  893. if ($ids) {
  894. return $query->where('m.id', 'not in', $ids);
  895. } else {
  896. return $query->where('m.id', '>', 0);
  897. }
  898. })
  899. ->where(function ($query) use ($conditions, $memberInfo, $ageGap) {
  900. $querys = $query;
  901. // 年龄
  902. $year = isset($conditions['year']) ? trim($conditions['year']) : '';
  903. if ($year) {
  904. $year = explode('~', $year);
  905. $data1 = isset($year[0]) ? intval($year[0]) : 0;
  906. $data2 = isset($year[1]) ? intval($year[1]) : 0;
  907. if ($data2) {
  908. $data1 = $data1 ? strtotime($data1 . '-01-01') : 0;
  909. $data2 = $data2 ? strtotime(($data2 + 1) . '-01-01') : 0;
  910. $querys = $querys->where('m.birthday', '>=', $data1 . '')->where('m.birthday', '<=', $data2);
  911. } else if ($data1) {
  912. $data1 = $data1 ? strtotime($data1 . '-01-01') : 0;
  913. $querys = $querys->where('m.birthday', '>=', $data1);
  914. }
  915. }
  916. // 身高
  917. $height = isset($conditions['height']) ? trim($conditions['height']) : '';
  918. if ($height) {
  919. $height = explode('~', $height);
  920. $data1 = isset($height[0]) ? intval($height[0]) : 0;
  921. $data2 = isset($height[1]) ? intval($height[1]) : 0;
  922. if ($data2) {
  923. $querys = $querys->where('up.height', '>=', $data1)->where('up.height', '<=', $data2);
  924. } else if ($data1) {
  925. $querys = $querys->where('up.height', '>=', $data1);
  926. }
  927. }
  928. // 收入
  929. $salary = isset($conditions['salary']) ? intval($conditions['salary']) : 0;
  930. if ($height <= 0 && $salary) {
  931. $dataArr = config('weixin.salarys');
  932. $datas = isset($dataArr[$salary]) ? $dataArr[$salary] : [];
  933. $data1 = isset($datas['value']) ? $datas['value'] : 0;
  934. $data2 = isset($datas['value2']) ? $datas['value2'] : 0;
  935. if ($data2) {
  936. $querys = $querys->where('up.salary', '>=', $data1)->where('up.salary', '<=', $data2);
  937. } else if ($data1) {
  938. $querys = $querys->where('up.salary', '=', $data1);
  939. }
  940. }
  941. // 所在城市
  942. $city = isset($memberInfo['city']) ? trim($memberInfo['city']) : '';
  943. $cityValue = isset($conditions['city']) ? trim($conditions['city']) : '';
  944. if ($cityValue > 0 && $city) {
  945. $querys = $querys->where('up.city', '=', $city);
  946. }
  947. // 婚姻状态
  948. $married = isset($conditions['married']) ? intval($conditions['married']) : 0;
  949. if ($married > 0) {
  950. $querys = $querys->where('up.married', '>=', $married);
  951. }
  952. // 学历
  953. $education = isset($conditions['education']) ? intval($conditions['education']) : 0;
  954. if ($education > 0) {
  955. if ($married > 0 && $education == 3) {
  956. $querys = $querys->where('up.education', '>=', 1);
  957. } else if ($married <= 0) {
  958. // 硕士及以上或按学历匹配
  959. $op = $education == 3 ? '>=' : '=';
  960. $querys = $querys->where('up.education', $op, $education);
  961. }
  962. }
  963. return $querys;
  964. })
  965. ->orderRaw('rand()')
  966. ->limit($recommendNum)
  967. ->column('m.id');
  968. }
  969. if ($recommendIds) {
  970. // 更新推荐日期
  971. Member::where(['id' => $userId])->update(['heart_recommend_at' => date('Y-m-d H:i:s')]);
  972. }
  973. $expire = isset($siteInfo['recommend_expire']) ? intval($siteInfo['recommend_expire']) : 0;
  974. $expire = $expire ? $expire : 24;
  975. if ($recommendIds) {
  976. PRedis::set($cacheKey, $recommendIds, $expire * 3600);
  977. }
  978. }
  979. // 获取数据
  980. if ($recommendIds) {
  981. $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';
  982. // 距离
  983. $info = Member::where(['id' => $userId])->field('lat,lng')->find();
  984. $lat = input('lat', 0.00);
  985. $lng = input('lng', 0.00);
  986. $lat = $lat ? $lat : (isset($info['lat']) ? $info['lat'] : 0);
  987. $lng = $lng ? $lng : (isset($info['lng']) ? $info['lng'] : 0);
  988. if ($lat && $lng) {
  989. $field .= ",ROUND(
  990. 6378.138 * 2 * ASIN(
  991. SQRT(
  992. POW(
  993. SIN(
  994. (
  995. {$lat} * PI() / 180 - m.`lat` * PI() / 180
  996. ) / 2
  997. ),
  998. 2
  999. ) + COS({$lat} * PI() / 180) * COS(m.`lat` * PI() / 180) * POW(
  1000. SIN(
  1001. (
  1002. {$lng} * PI() / 180 - m.`lng` * PI() / 180
  1003. ) / 2
  1004. ),
  1005. 2
  1006. )
  1007. )
  1008. ) * 1000
  1009. ) AS distance";
  1010. }
  1011. $dataList = Member::alias('m')
  1012. ->join('user_profile up', 'up.userid=m.id', 'left')
  1013. ->where('m.id', 'in', implode(',', $recommendIds))
  1014. ->where(['m.is_heart' => 1, 'm.user_type' => 2, 'm.user_status' => 1])
  1015. ->field($field)
  1016. ->orderField('m.id', $recommendIds)
  1017. ->paginate(count($recommendIds))
  1018. ->each(function ($item, $k) {
  1019. $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
  1020. $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
  1021. $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
  1022. $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
  1023. $height = isset($item['height']) ? intval($item['height']) : 0;
  1024. $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '';
  1025. $weight = isset($item['weight']) ? intval($item['weight']) : 0;
  1026. $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '';
  1027. // VIP
  1028. $vipAuth = isset($item['vip_auth']) ? intval($item['vip_auth']) : 0;
  1029. $vipExpire = isset($item['vip_expire']) ? intval($item['vip_expire']) : 0;
  1030. if ($vipAuth && $vipExpire >= time()) {
  1031. $item['vip_auth'] = 1;
  1032. $item['vip_expire'] = date('Y-m-d', $item['vip_expire']);
  1033. } else {
  1034. $item['vip_auth'] = 0;
  1035. $item['vip_expire'] = '';
  1036. }
  1037. // 学历
  1038. $educations = config('weixin.educations');
  1039. $edu = isset($item['education']) ? $item['education'] : 0;
  1040. $item['education_txt'] = $edu && isset($educations[$edu]) ? $educations[$edu] : '';
  1041. // 地址
  1042. $province = isset($item['province']) ? trim($item['province']) : '';
  1043. $city = isset($item['city']) ? trim($item['city']) : '';
  1044. $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
  1045. $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
  1046. $item['now_address'] = trim($province . ' ' . $city);
  1047. $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
  1048. $albums = [];
  1049. $photolist = isset($item['photolist']) ? $item['photolist'] : '';
  1050. $photolist = $photolist ? explode(',', $photolist) : [];
  1051. if ($photolist) {
  1052. foreach ($photolist as $k => $val) {
  1053. $albums[] = cmf_get_image_preview_url($val);
  1054. }
  1055. }
  1056. $item['pic_count'] = count($albums);
  1057. // 坐标距离
  1058. $item['distance'] = isset($item['distance']) ? $item['distance'] : '';
  1059. if ($item['lat'] <= 0 || $item['lng'] <= 0) {
  1060. $item['distance'] = '';
  1061. } else {
  1062. $item['distance'] = $item['distance'] >= 1000 ? round($item['distance'] / 1000, 2) . 'km' : ($item['distance'] ? $item['distance'] . 'm' : '');
  1063. }
  1064. // 地址
  1065. $address = isset($item['address']) ? $item['address'] : '';
  1066. $addressData = $address ? explode(',', $address) : [];
  1067. $item['district'] = isset($addressData[2]) ? $addressData[2] : '';
  1068. return $item;
  1069. });
  1070. // 过滤重复
  1071. if ($updated && $recommendIds) {
  1072. if ($ids) {
  1073. $newIds = array_merge($ids, $recommendIds);
  1074. Predis::set("hearts:hasMeatch:{$userId}", ['ids' => implode(',', $newIds), 'expire' => $hasExpire], 30 * 24 * 3600);
  1075. } else {
  1076. Predis::set("hearts:hasMeatch:{$userId}", ['ids' => implode(',', $recommendIds), 'expire' => time() + 3 * 24 * 3600], 30 * 24 * 3600);
  1077. }
  1078. }
  1079. PRedis::set('hearts:results:' . $userId . '_' . date('Ymd'), ['datalist' => $dataList, 'sql' => Member::getLastSql()], 3 * 3600 * 24);
  1080. $dataList = $dataList ? $dataList->toArray() : [];
  1081. }
  1082. return $dataList;
  1083. }
  1084. /**
  1085. * 获取用户匹配条件
  1086. * @param $userId 当前用户
  1087. * @return array|mixed
  1088. */
  1089. public static function getMemberConditions($userId)
  1090. {
  1091. $conditions = db('user_conditions')->where(['user_id' => $userId])->value('conditions');
  1092. $conditions = $conditions ? json_decode($conditions, true) : [];
  1093. return $conditions;
  1094. }
  1095. /**
  1096. * 设置用户推荐条件,怦然心动条件
  1097. * @param $userId 当前用户ID
  1098. * @param $params 条件参数:age、height、weight、city、home_province、education、salary
  1099. * @return int|string
  1100. */
  1101. public static function setMemberConditions($userId, $params)
  1102. {
  1103. // 条件数据
  1104. $condition = [
  1105. 'age' => isset($params['age']) ? intval($params['age']) : 0,
  1106. 'height' => isset($params['height']) ? trim($params['height']) : '',
  1107. 'weight' => isset($params['weight']) ? intval($params['weight']) : 0,
  1108. 'city' => isset($params['city']) ? intval($params['city']) : 0,
  1109. // 'home_province' => isset($params['home_province']) ? intval($params['home_province']) : 0,
  1110. 'education' => isset($params['education']) ? intval($params['education']) : 0,
  1111. 'salary' => isset($params['salary']) ? intval($params['salary']) : 0,
  1112. 'married' => isset($params['married']) ? intval($params['married']) : 0,
  1113. 'year' => isset($params['year']) ? trim($params['year']) : '',
  1114. ];
  1115. $data = [
  1116. 'user_id' => $userId,
  1117. 'conditions' => json_encode($condition, 256),
  1118. 'updated_at' => date('Y-m-d H:i:s'),
  1119. 'status' => 1,
  1120. ];
  1121. // 验证并添加或保存
  1122. $check = db('user_conditions')->where(['user_id' => $userId])->value('id');
  1123. if ($check) {
  1124. $res = db('user_conditions')->where(['user_id' => $userId])->update($data);
  1125. } else {
  1126. $res = db('user_conditions')->insertGetId($data);
  1127. }
  1128. return $res;
  1129. }
  1130. /**
  1131. * 用户关注
  1132. * @param $userId 当前用户
  1133. * @param $collectUid 关注的用户
  1134. * @param int $opType
  1135. * @return int
  1136. */
  1137. public static function collect($userId, $collectUid, $opType = 1)
  1138. {
  1139. if ($userId == $collectUid) {
  1140. return 2114;
  1141. }
  1142. if (!Member::where(['id' => $collectUid])->value('id')) {
  1143. return 2104;
  1144. }
  1145. // 验证性别
  1146. $contactSex = Member::where(['id' => $collectUid])->value('sex');
  1147. $sex = Member::where(['id' => $userId])->value('sex');
  1148. if (!$sex || ($sex == $contactSex)) {
  1149. return 2135;
  1150. }
  1151. // 验证数据
  1152. $collectData = UserCollect::where(['user_id' => $userId, 'source_id' => $collectUid, 'type' => 1])
  1153. ->field('id,status')
  1154. ->find();
  1155. // 关注
  1156. $collectId = isset($collectData['id']) ? $collectData['id'] : 0;
  1157. $status = isset($collectData['status']) ? $collectData['status'] : 0;
  1158. if ($opType == 1) {
  1159. if ($collectId && $status == 1) {
  1160. return 2105;
  1161. }
  1162. // 若收藏时间已失效更新时间
  1163. $collectExpire = Member::where(['id' => $userId])->value('collect_expire');
  1164. if ($collectExpire - time() <= 0) {
  1165. $siteInfo = $siteInfo = cmf_get_site_info();
  1166. $collectTime = isset($siteInfo['collect_time']) ? intval($siteInfo['collect_time']) : 0;
  1167. $collectTime = $collectTime ? $collectTime : 6;
  1168. Member::saveData(['id' => $userId], ['collect_expire' => time() + $collectTime * 3600]);
  1169. $memberInfo = Member::where(['id' => $userId])->field('collect_expire,vip_auth,vip_expire')->find();
  1170. $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0;
  1171. $vipExpire = isset($memberInfo['vip_expire']) ? $memberInfo['vip_expire'] : 0;
  1172. if (!$vipAuth || $vipExpire < time()) {
  1173. UserCollect::where(['user_id' => $userId])
  1174. ->update(['status' => 2, 'updated_at' => date('Y-m-d H:i:s')]);
  1175. }
  1176. }
  1177. $data = [
  1178. 'user_id' => $userId,
  1179. 'source_id' => $collectUid,
  1180. 'type' => 1,
  1181. 'created_at' => date('Y-m-d H:i:s'),
  1182. 'status' => 1,
  1183. ];
  1184. if ($collectId) {
  1185. if (!UserCollect::where(['user_id' => $userId, 'id' => $collectId])
  1186. ->update(['status' => 1, 'updated_at' => date('Y-m-d H:i:s')])) {
  1187. return 2107;
  1188. }
  1189. } else {
  1190. if (!UserCollect::insertGetId($data)) {
  1191. return 2107;
  1192. }
  1193. }
  1194. // 操作日志
  1195. UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => "收藏用户:{$collectUid}"]);
  1196. // 通知
  1197. $openid = Member::where(['id' => $collectUid])->value('openid');
  1198. $memberInfo = Member::where(['id' => $userId])->field('openid,sex,user_nickname')->find();
  1199. $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : '未公开';
  1200. $sex = isset($memberInfo['sex']) ? $memberInfo['sex'] : 0;
  1201. $sex = $sex == 1 ? '帅哥' : '美女';
  1202. $dateTime = date('Y.m.d H:i');
  1203. if ($openid) {
  1204. $params = [
  1205. 'title' => "有个{$sex}查看了你的名片并且关注了你\n\n关注时间:\t{$dateTime}",
  1206. // 'title' => "有一位{$sex}申请加您的微信并给您留言了!\n\n申请时间:\t{$dateTime}\n\n收到留言:\t{$remark}",
  1207. 'remark' => "点击查看(如果不想再收到该类型消息,可本公众号回复“隐身设置”进入链接设置为隐身)",
  1208. 'type' => 'contact',
  1209. 'keywords' => [
  1210. 'keyword1' => [
  1211. 'value' => $nickname . '(昵称)',
  1212. 'color' => '#173177',
  1213. ],
  1214. 'keyword2' => [
  1215. 'value' => '隐私信息不显示',
  1216. 'color' => '#173177',
  1217. ],
  1218. 'keyword3' => [
  1219. 'value' => '消息提醒',
  1220. 'color' => '#173177',
  1221. ],
  1222. ],
  1223. 'url' => url("/weixin/member/collect?type=2", [], '', true),
  1224. ];
  1225. PRedis::set('messages:collect:' . $userId . '_' . $collectUid . '_' . $openid, ['data' => $data, 'params' => $params], 600);
  1226. Wechat::sendTplMsg($openid, $params, false);
  1227. }
  1228. return true;
  1229. } // 取消关注
  1230. else if ($opType == 2) {
  1231. if (!$collectId) {
  1232. return 1003;
  1233. }
  1234. if ($status != 1) {
  1235. return 2115;
  1236. }
  1237. if (!UserCollect::where(['user_id' => $userId, 'source_id' => $collectUid, 'type' => 1])
  1238. ->update(['status' => 2, 'updated_at' => date('Y-m-d H:i:s')])) {
  1239. return 2109;
  1240. }
  1241. // 操作日志
  1242. UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => "取消收藏用户:{$collectUid}"]);
  1243. return true;
  1244. }
  1245. return 1009;
  1246. }
  1247. /**
  1248. * 保存信息
  1249. * @param $where
  1250. * @param $data
  1251. * @return int|string
  1252. */
  1253. public static function saveData($where, $data)
  1254. {
  1255. $data['updated_at'] = date('Y-m-d H:i:s');
  1256. return Member::where($where)->update($data);
  1257. }
  1258. /**
  1259. * 想认识处理
  1260. * @param $userId 用户ID
  1261. * @param $contactUid 认识用户ID
  1262. * @return array|int
  1263. */
  1264. public static function contactUser($userId, $contactUid, $remark = '')
  1265. {
  1266. if ($userId == $contactUid) {
  1267. return 2133;
  1268. }
  1269. $contactInfo = Member::where(['id' => $contactUid, 'user_type' => 2, 'user_status' => 1])
  1270. ->field('id,openid,user_nickname,sex,real_name,is_reg_profile')
  1271. ->find();
  1272. $memberInfo = Member::where(['id' => $userId, 'user_type' => 2, 'user_status' => 1])
  1273. ->field('id,openid,user_nickname,real_name,sex,is_reg_profile,redheart,vip_auth,vip_expire,vip_num')
  1274. ->find();
  1275. if (empty($contactInfo) || empty($memberInfo)) {
  1276. return false;
  1277. }
  1278. // 性别验证
  1279. $contactSex = isset($contactInfo['sex']) ? intval($contactInfo['sex']) : 0;
  1280. $sex = isset($memberInfo['sex']) ? intval($memberInfo['sex']) : 0;
  1281. if (!$sex || ($sex == $contactSex)) {
  1282. return 2134;
  1283. }
  1284. // 完善资料
  1285. $isRegProfile = isset($memberInfo['is_reg_profile']) ? $memberInfo['is_reg_profile'] : 0;
  1286. if ($isRegProfile != 1) {
  1287. return 2103;
  1288. }
  1289. // 验证是否已认识
  1290. $contactData = UserContactLog::where(['user_id' => $userId, 'contact_uid' => $contactUid])
  1291. ->where('status', 'in', [1, 2, 3])
  1292. ->field('id,status')
  1293. ->find();
  1294. $checkId = isset($contactData['id']) ? $contactData['id'] : 0;
  1295. $status = isset($contactData['status']) ? $contactData['status'] : 0;
  1296. if ($checkId && $status != 4) {
  1297. return $status == 1 ? 2117 : 2118;
  1298. }
  1299. //
  1300. Db::startTrans();
  1301. // 扣除爱心账户
  1302. $accountConfig = cmf_get_option('account_config');
  1303. $contactPay = isset($accountConfig['contact_pay']) ? intval($accountConfig['contact_pay']) : 0;
  1304. $contactPay = $contactPay ? $contactPay : 1;
  1305. // 会员不需要收录费
  1306. $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0;
  1307. $vipExpire = isset($memberInfo['vip_expire']) ? intval($memberInfo['vip_expire']) : 0;
  1308. $vipNum = isset($memberInfo['vip_num']) ? intval($memberInfo['vip_num']) : 0;
  1309. $contactPay = $vipAuth && $vipExpire >= time() ? 0 : $contactPay;
  1310. if ($contactPay > 0) {
  1311. $redheart = isset($memberInfo['redheart']) ? intval($memberInfo['redheart']) : 0;
  1312. if ($redheart < $contactPay) {
  1313. Db::rollback();
  1314. return 2121;
  1315. }
  1316. $newRedheart = max(0, ($redheart - $contactPay));
  1317. $memberData = ['redheart' => $newRedheart, 'updated_at' => date('Y-m-d H:i:s')];
  1318. if (!Member::where(['id' => $userId])->update($memberData)) {
  1319. Db::rollback();
  1320. return false;
  1321. }
  1322. // 账户明细
  1323. $cNickname = isset($contactInfo['user_nickname']) ? $contactInfo['user_nickname'] : '';
  1324. $accountData = [
  1325. 'user_id' => $userId,
  1326. 'type' => 2,
  1327. 'account_type' => 1,
  1328. 'change_type' => 2,
  1329. 'money' => $contactPay,
  1330. 'balance' => $redheart,
  1331. 'remark' => "认识【{$cNickname}】请求成功,".($contactPay>0?"扣除{$contactPay}个爱心":'VIP会员使用1次机会'),
  1332. 'created_at' => date('Y-m-d H:i:s'),
  1333. 'status' => 2,
  1334. ];
  1335. PRedis::set('accounts:contact:' . $contactUid . '_' . $userId, $accountData, 600);
  1336. if (!AccountLog::insertGetId($accountData)) {
  1337. Db::rollback();
  1338. return false;
  1339. }
  1340. }
  1341. // 扣除会员微信申请次数
  1342. else{
  1343. if ($vipNum < 1 || $vipExpire < time()) {
  1344. Db::rollback();
  1345. return 2148;
  1346. }
  1347. $memberData = ['vip_num' => ($vipNum - 1), 'updated_at' => date('Y-m-d H:i:s')];
  1348. if (!Member::where(['id' => $userId])->update($memberData)) {
  1349. Db::rollback();
  1350. return false;
  1351. }
  1352. }
  1353. $data = [
  1354. 'user_id' => $userId,
  1355. 'contact_uid' => $contactUid,
  1356. 'cost_redheart' => $contactPay,
  1357. 'remark' => $remark,
  1358. 'created_at' => date('Y-m-d H:i:s'),
  1359. 'status' => 1,
  1360. ];
  1361. $cid = UserContactLog::insertGetId($data);
  1362. if (!$cid) {
  1363. Db::rollback();
  1364. return false;
  1365. }
  1366. Db::commit();
  1367. if ($cid) {
  1368. // 模板消息
  1369. $openid = isset($contactInfo['openid']) ? $contactInfo['openid'] : '';
  1370. $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : '';
  1371. $cNickname = isset($contactInfo['user_nickname']) ? $contactInfo['user_nickname'] : '';
  1372. $sex = isset($memberInfo['sex']) ? $memberInfo['sex'] : 1;
  1373. $sex = $sex == 1 ? '男生' : '女生';
  1374. $dateTime = date('Y.m.d H:i');
  1375. if ($openid) {
  1376. $remark = $remark ? $remark : '无';
  1377. $params = [
  1378. 'title' => "对方留言:{$remark}",
  1379. // 'title' => "有一位{$sex}申请加您的微信并给您留言了!\n\n申请时间:\t{$dateTime}\n\n收到留言:\t{$remark}",
  1380. 'remark' => "点击查看(如果不想再收到该类型消息,可本公众号回复“隐身设置”进入链接设置为隐身)",
  1381. 'type' => 'contact',
  1382. 'keywords' => [
  1383. 'keyword1' => [
  1384. 'value' => $nickname . '(昵称)',
  1385. 'color' => '#173177',
  1386. ],
  1387. 'keyword2' => [
  1388. 'value' => '隐私信息不显示',
  1389. 'color' => '#173177',
  1390. ],
  1391. 'keyword3' => [
  1392. 'value' => '待处理',
  1393. 'color' => '#173177',
  1394. ],
  1395. ],
  1396. 'url' => url("/weixin/member/home?id={$userId}&cid={$cid}", [], '', true),
  1397. ];
  1398. PRedis::set('messages:contact:' . $openid, ['data' => $data, 'params' => $params], 600);
  1399. Wechat::sendTplMsg($openid, $params, false);
  1400. }
  1401. // 操作日志
  1402. UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => ($contactPay>0?'花费'.$contactPay.'爱心':'使用一次机会').",申请用户{$cNickname}微信"]);
  1403. return ['id' => $cid];
  1404. } else {
  1405. return 2119;
  1406. }
  1407. }
  1408. /**
  1409. * @param $userId
  1410. * @param $cid
  1411. * @return bool|int
  1412. */
  1413. public static function contactConfirm($userId, $cid, $checkStatus)
  1414. {
  1415. // 验证是否
  1416. $contactData = UserContactLog::where(['id' => $cid, 'contact_uid' => $userId])
  1417. ->field('id,user_id,cost_redheart,is_read,status')
  1418. ->find();
  1419. $checkId = isset($contactData['id']) ? $contactData['id'] : 0;
  1420. $status = isset($contactData['status']) ? $contactData['status'] : 0;
  1421. $contactUid = isset($contactData['user_id']) ? $contactData['user_id'] : 0;
  1422. $isRead = isset($contactData['is_read']) ? $contactData['is_read'] : 0; // 是否已读
  1423. if ($checkId && $status != 1 && $checkStatus != 5) {
  1424. return 2120;
  1425. }
  1426. if (empty($contactUid)) {
  1427. return 2111;
  1428. }
  1429. // 撤回操作
  1430. if ($checkStatus == 5) {
  1431. UserContactLog::where(['id' => $cid])->update(['status' => 1, 'updated_at' => date('Y-m-d H:i:s')]);
  1432. return 2139;
  1433. }
  1434. // 更新记录数据
  1435. Db::startTrans();
  1436. $updateData = ['status' => $checkStatus, 'updated_at' => date('Y-m-d H:i:s')];
  1437. if (!UserContactLog::where(['id' => $cid])->update($updateData)) {
  1438. Db::rollback();
  1439. return 2032;
  1440. }
  1441. // 当前用户信息
  1442. $field = 'm.user_nickname,m.real_name,m.openid,up.wechat_code,up.qq';
  1443. $memberInfo = Member::getHomeInfo($userId, $field);
  1444. $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : '';
  1445. if (empty($memberInfo)) {
  1446. Db::rollback();
  1447. return 1011;
  1448. }
  1449. $field = 'm.redheart,m.user_nickname,m.real_name,m.openid,up.wechat_code,m.vip_num,up.qq';
  1450. $contactUserInfo = Member::getHomeInfo($contactUid, $field, 6);
  1451. $cOpenid = isset($contactUserInfo['openid']) ? $contactUserInfo['openid'] : '';
  1452. $cNickname = isset($contactUserInfo['user_nickname']) ? $contactUserInfo['user_nickname'] : '';
  1453. $redheart = isset($contactUserInfo['redheart']) ? intval($contactUserInfo['redheart']) : 0;
  1454. $contactPay = isset($contactData['cost_redheart']) ? intval($contactData['cost_redheart']) : 0;
  1455. $vipNum = isset($contactUserInfo['vip_num']) ? intval($contactUserInfo['vip_num']) : 0;
  1456. if (empty($contactUserInfo)) {
  1457. Db::rollback();
  1458. return 1017;
  1459. }
  1460. // 已读或拒绝退款扣除的爱心
  1461. $accountConfig = cmf_get_option('account_config');
  1462. $applyRefundPay = isset($accountConfig['apply_refund_pay']) ? intval($accountConfig['apply_refund_pay']) : 0;
  1463. if ($isRead || $checkStatus == 3) {
  1464. $applyRefundPay = $applyRefundPay ? $applyRefundPay : 0;
  1465. $refundHeart = $contactPay > $applyRefundPay ? intval($contactPay - $applyRefundPay) : 0;
  1466. } else {
  1467. $refundHeart = $contactPay;
  1468. $applyRefundPay = 0;
  1469. }
  1470. // 审核失败退还扣除的爱心
  1471. if ($contactPay > 0 && ($checkStatus == 3 || $checkStatus == 4)) {
  1472. $newRedheart = max(0, ($redheart + $refundHeart));
  1473. $memberData = ['redheart' => $newRedheart, 'updated_at' => date('Y-m-d H:i:s')];
  1474. if (!Member::where(['id' => $contactUid])->update($memberData)) {
  1475. Db::rollback();
  1476. return 2031;
  1477. }
  1478. // 账户明细
  1479. $accountData = [
  1480. 'user_id' => $contactUid,
  1481. 'type' => 3,
  1482. 'account_type' => 1,
  1483. 'change_type' => 1,
  1484. 'money' => $refundHeart,
  1485. 'balance' => $redheart,
  1486. 'remark' => "认识【{$nickname}-ID:{$userId}】" . ($checkStatus == 3 ? '审核失败' : '审核超时') . ",退还{$refundHeart}个爱心,扣除{$applyRefundPay}个爱心",
  1487. 'created_at' => date('Y-m-d H:i:s'),
  1488. 'status' => 2,
  1489. ];
  1490. PRedis::set('accounts:contact:' . $contactUid . '_' . $userId, $accountData, 600);
  1491. if (!AccountLog::insertGetId($accountData)) {
  1492. Db::rollback();
  1493. return false;
  1494. }
  1495. }
  1496. // 退还申请次数
  1497. else if($checkStatus == 3 || $checkStatus == 4){
  1498. $memberData = ['vip_num' => $vipNum+1, 'updated_at' => date('Y-m-d H:i:s')];
  1499. if (!Member::where(['id' => $contactUid])->update($memberData)) {
  1500. Db::rollback();
  1501. return 2031;
  1502. }
  1503. }
  1504. Db::commit();
  1505. // 发送模板信息
  1506. /*if($openid){
  1507. $cWechatCode = isset($contactUserInfo['wechat_code'])? trim($contactUserInfo['wechat_code']) : '';
  1508. $cqq = isset($contactUserInfo['qq'])? trim($contactUserInfo['qq']) : '';
  1509. $cWechatCode = $cWechatCode? $cWechatCode : '无';
  1510. $cqq = $cqq? $cqq : '无';
  1511. $params = [
  1512. 'title' => "【{$cRealname}】想认识您的请求已确认\n\n对方微信号:\t{$cWechatCode}\n\n对方QQ:\t{$cqq}",
  1513. 'remark' => "感谢您的使用,点击详情查看对方信息",
  1514. 'type' => 'contact',
  1515. 'keywords' => [
  1516. 'keyword1' => [
  1517. 'value' => '已确认',
  1518. 'color' => '#173177',
  1519. ],
  1520. 'keyword2' => [
  1521. 'value' => date('Y年m月d日 H点i分s秒'),
  1522. 'color' => '#173177',
  1523. ],
  1524. 'keyword3' => [
  1525. 'value' => "来自【{$cNickname}】,姓名:【{$cRealname}】的认识申请已经确认通过,请及时添加对方联系方式",
  1526. 'color' => '#173177',
  1527. ],
  1528. ],
  1529. 'url' => url("/weixin/member/home?id={$contactUid}&cid=$cid", '', '', true),
  1530. ];
  1531. PRedis::set('messages:contactConfirm_' . $userId.'_'.$contactUid, ['data' => $contactData, 'params' => $params], 600);
  1532. Wechat::sendTplMsg($openid, $params);
  1533. }*/
  1534. // 通知对方模板信息
  1535. if ($cOpenid) {
  1536. $wechatCode = isset($memberInfo['wechat_code']) ? trim($memberInfo['wechat_code']) : '';
  1537. $qq = isset($memberInfo['qq']) ? trim($memberInfo['qq']) : '';
  1538. $wechatCode = $wechatCode ? $wechatCode : '无';
  1539. $qq = $qq ? $qq : '无';
  1540. $read = $isRead ? '已查看' : ' 未查看';
  1541. $dateTime = date('Y.m.d H:i');
  1542. if ($checkStatus == 2) {
  1543. $params = [
  1544. 'title' => "你喜欢的人也喜欢你啦\n\n对方姓名:\t{$nickname}(昵称)\n\n对方微信号: \t{$wechatCode}\n\n申请时间:\t{$dateTime}",
  1545. 'remark' => "脱单需主动,交友需谨慎!",
  1546. 'type' => 'contact_confirm',
  1547. 'keywords' => [
  1548. 'keyword1' => [
  1549. 'value' => '状态通知',
  1550. 'color' => '#173177',
  1551. ],
  1552. 'keyword2' => [
  1553. 'value' => '已接受',
  1554. 'color' => '#173177',
  1555. ],
  1556. ],
  1557. 'url' => url("/weixin/member/home?id={$userId}&cid={$cid}", '', '', true),
  1558. ];
  1559. } else if ($checkStatus == 3) {
  1560. // 是否扣除爱心
  1561. $remark = $applyRefundPay ? "扣除{$applyRefundPay}颗后已经原路退回" : "已经全部原路退回";
  1562. $remark = $contactPay<=0? "您的微信申请次数已返还": "您支付的爱心{$remark}";
  1563. $params = [
  1564. 'title' => "Soory!亲亲!{$nickname}拒绝了您的加微信申请,{$remark}。别灰心哦,您一定会遇到更好的另一半~\n\n申请时间:\t{$dateTime}\n查看状态:\t{$read}",
  1565. 'remark' => "天涯何处无芳草,去看看其他的异性吧!",
  1566. 'type' => 'contact_confirm',
  1567. 'keywords' => [
  1568. 'keyword1' => [
  1569. 'value' => '状态通知',
  1570. 'color' => '#173177',
  1571. ],
  1572. 'keyword2' => [
  1573. 'value' => '已拒绝',
  1574. 'color' => '#173177',
  1575. ],
  1576. ],
  1577. 'url' => url("/weixin/match/index", '', '', true),
  1578. ];
  1579. } else {
  1580. // 是否已读
  1581. $remark = $isRead && $applyRefundPay ? "扣除{$applyRefundPay}颗后已经原路退回" : "已经全部原路退回";
  1582. $remark = $contactPay<=0? "您的微信申请次数已返还": "您被扣除的爱心{$remark}";
  1583. $params = [
  1584. 'title' => "您好亲亲,非常抱歉{$nickname}未及时反馈而导致申请超时,{$remark}。您还可以请客服帮您牵线增加成功率哦~\n\n申请时间:\t{$dateTime}\n查看状态:\t{$read}",
  1585. 'remark' => "当你有勇气主动时,其实已经打败了那些看似优秀的人,遇见不易让客服牵线试试吧!",
  1586. 'type' => 'contact_confirm',
  1587. 'keywords' => [
  1588. 'keyword1' => [
  1589. 'value' => '状态通知',
  1590. 'color' => '#173177',
  1591. ],
  1592. 'keyword2' => [
  1593. 'value' => '过期失效',
  1594. 'color' => '#173177',
  1595. ],
  1596. ],
  1597. 'url' => url("/weixin/match/index", '', '', true),
  1598. ];
  1599. }
  1600. // 操作日志
  1601. if ($checkStatus == 2 || $checkStatus == 3) {
  1602. $content = $checkStatus == 2 ? "通过用户{$cNickname}微信申请" : "拒绝用户{$cNickname}微信申请";
  1603. UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => $content]);
  1604. }
  1605. PRedis::set('messages:contactConfirm:' . $contactUid . '_' . $userId, ['openid' => $cOpenid, 'data' => $contactData, 'params' => $params], 600);
  1606. Wechat::sendTplMsg($cOpenid, $params);
  1607. }
  1608. return ['id' => $cid];
  1609. }
  1610. /**
  1611. * 用户页面访问统计
  1612. * @param $userId 访问用户
  1613. * @param string $type 访问类型
  1614. * @param int $targetUid 目标用户
  1615. */
  1616. public static function visitCount($userId, $type = 'login', $targetUid = 0, $expire = 1)
  1617. {
  1618. $cacheKey = 'counts:users:' . date('Ymd') . '_' . $type;
  1619. $cacheTempKey = 'counts:usersTemp:' . $userId . '_' . $type . '_' . date('Ymd');
  1620. $checkUser = Member::where(['user_type' => 2, 'id' => $userId])
  1621. ->value('id');
  1622. if ($checkUser && $userId && !PRedis::get($cacheTempKey)) {
  1623. PRedis::set($cacheTempKey, ['userId' => $userId, 'type' => $type, 'target' => $targetUid], $expire * 3600);
  1624. $types = ['login' => '登录', 'match' => '访问单身推荐', 'heart' => '访问怦然心动', 'center' => '访问用户中心', 'home' => "访问用户[{$targetUid}]信息", 'activity' => '浏览活动列表', 'book' => '想要报名'];
  1625. $title = isset($types[$type]) ? $types[$type] : '访问平台';
  1626. if ($type != 'profile') {
  1627. UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => "【{$userId}】用户{$title}"]);
  1628. }
  1629. if (!PRedis::get($cacheKey)) {
  1630. PRedis::set($cacheKey, 1, 2 * 24 * 3600);
  1631. } else {
  1632. PRedis::inc($cacheKey, 1);
  1633. }
  1634. }
  1635. }
  1636. /**
  1637. * 获取匹配用户数据列表
  1638. * @param $matchUids
  1639. * @return array|bool
  1640. * @throws \think\db\exception\DataNotFoundException
  1641. * @throws \think\db\exception\ModelNotFoundException
  1642. * @throws \think\exception\DbException
  1643. */
  1644. public static function getMatchList($matchUids, $activityId = 0)
  1645. {
  1646. if (empty($matchUids)) {
  1647. return false;
  1648. }
  1649. $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';
  1650. $dataList = Member::alias('m')
  1651. ->leftJoin('user_profile up', 'up.userid=m.id')
  1652. ->where('m.id', 'in', $matchUids)
  1653. ->field($field)
  1654. ->select()
  1655. ->each(function ($item) use ($activityId) {
  1656. if ($activityId) {
  1657. $item['book_no'] = Books::where(['uid' => $item['id'], 'aid' => $activityId, 'status' => 3])->value('book_num');
  1658. }
  1659. Member::formatUser($item);
  1660. });
  1661. return $dataList ? $dataList->toArray() : [];
  1662. }
  1663. /**
  1664. * 格式化用户信息
  1665. * @param $item 数据
  1666. * @param $type
  1667. */
  1668. private static function formatUser(&$item, $type = 0)
  1669. {
  1670. if (isset($item['avatar'])) {
  1671. $item['avatar'] = cmf_get_image_preview_url($item['avatar']);
  1672. }
  1673. if (isset($item['brief'])) {
  1674. $item['brief'] = htmlspecialchars_decode($item['brief']);
  1675. }
  1676. if (isset($item['sex'])) {
  1677. $sexs = ['', '男', '女'];
  1678. $sex = isset($item['sex']) ? $item['sex'] : 0;
  1679. $item['sex_txt'] = isset($sexs[$sex]) ? $sexs[$sex] : '';
  1680. }
  1681. $item['albums'] = [];
  1682. $photolist = isset($item['photolist']) ? $item['photolist'] : '';
  1683. $photolist = $photolist ? explode(',', $photolist) : [];
  1684. if ($photolist) {
  1685. $albums = [];
  1686. foreach ($photolist as $k => $val) {
  1687. $albums[] = cmf_get_image_preview_url($val);
  1688. }
  1689. $item['albums'] = $albums;
  1690. }
  1691. if (isset($item['birthday'])) {
  1692. $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
  1693. $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '';
  1694. $item['birthday_code'] = $birthday ? date('Y-m-d', $birthday) : '';
  1695. $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
  1696. }
  1697. $item['height_txt'] = '';
  1698. if (isset($item['height'])) {
  1699. $height = isset($item['height']) ? intval($item['height']) : 0;
  1700. $item['height'] = intval($height);
  1701. $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '无';
  1702. }
  1703. $item['weight_txt'] = '';
  1704. if (isset($item['weight'])) {
  1705. $weight = isset($item['weight']) ? intval($item['weight']) : 0;
  1706. $item['weight'] = intval($weight);
  1707. $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '无';
  1708. }
  1709. // 学历
  1710. $item['education_txt'] = '';
  1711. if (isset($item['education'])) {
  1712. $educations = config('weixin.educations');
  1713. $edu = isset($item['education']) ? $item['education'] : 0;
  1714. $item['education_txt'] = isset($educations[$edu]) ? $educations[$edu] : '无';
  1715. }
  1716. // 资产
  1717. $item['property_txt'] = '暂无';
  1718. $property = isset($item['property']) ? $item['property'] : 0;
  1719. if (isset($item['property'])) {
  1720. $propertys = config('weixin.propertys');
  1721. $item['property'] = $property;
  1722. $item['property_txt'] = $property && isset($propertys[$property]) ? $propertys[$property] : '暂无';
  1723. }
  1724. // 地址
  1725. $item['now_address'] = '';
  1726. if (isset($item['province'])) {
  1727. $province = isset($item['province']) ? trim($item['province']) : '';
  1728. $city = isset($item['city']) ? trim($item['city']) : '';
  1729. $item['now_address'] = trim($province . ' ' . $city);
  1730. }
  1731. $item['home_address'] = '';
  1732. if (isset($item['home_province'])) {
  1733. $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
  1734. $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
  1735. $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
  1736. }
  1737. // 格式化婚姻状况
  1738. $item['married_txt'] = '';
  1739. if (isset($item['married'])) {
  1740. $marrieds = config('weixin.marrieds');
  1741. $married = $item['married'] ? intval($item['married']) : 0;
  1742. $item['married_txt'] = $married > 0 && isset($marrieds[$married - 1]) ? $marrieds[$married - 1] : '无';
  1743. }
  1744. }
  1745. /**
  1746. * 获取收到或发出消息数量
  1747. * @param $userId
  1748. * @param array $params
  1749. * @return float|int|string
  1750. */
  1751. public static function getMessageTotal($userId, $params = [])
  1752. {
  1753. $type = isset($params['type']) ? intval($params['type']) : 1;
  1754. // 我收到的
  1755. if ($type == 1) {
  1756. $where = ['uc.contact_uid' => $userId];
  1757. $joinWhere = 'u.id=uc.user_id';
  1758. } else {
  1759. $where = ['uc.user_id' => $userId];
  1760. $joinWhere = 'u.id=uc.contact_uid';
  1761. }
  1762. $status = isset($params['status']) ? intval($params['status']) : 1;
  1763. return UserContactLog::alias('uc')
  1764. ->join('user u', $joinWhere, 'left')
  1765. ->where($where)
  1766. ->where('u.id', '>', 0)
  1767. ->where(function ($query) use ($status) {
  1768. if ($status == 1) {
  1769. $query->where('uc.status', '>', 1);
  1770. } else {
  1771. $query->where('uc.status', 1);
  1772. }
  1773. })
  1774. ->count('uc.id');
  1775. }
  1776. /**
  1777. * 获取用户申请消息列表
  1778. * @param $params
  1779. * @param $pageSize
  1780. * @param string $field
  1781. * @return $this
  1782. */
  1783. public static function getMessageList($userId, $params = [], $pageSize = 20, $field = '')
  1784. {
  1785. // 验证有效时间,会员不限制
  1786. $type = isset($params['type']) ? intval($params['type']) : 1;
  1787. // 我收到的
  1788. if ($type == 1) {
  1789. $where = ['uc.contact_uid' => $userId];
  1790. $joinWhere = 'u.id=uc.user_id';
  1791. } else {
  1792. $where = ['uc.user_id' => $userId];
  1793. $joinWhere = 'u.id=uc.contact_uid';
  1794. }
  1795. $status = isset($params['status']) ? intval($params['status']) : 1;
  1796. $siteInfo = cmf_get_site_info();
  1797. $expire = isset($siteInfo['contact_time']) ? intval($siteInfo['contact_time']) : 0;
  1798. $expire = $expire ? $expire : 1;
  1799. $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';
  1800. $dataList = UserContactLog::alias('uc')
  1801. ->join('user u', $joinWhere, 'left')
  1802. ->join('user_profile mp', 'mp.userid=u.id', 'left')
  1803. ->where($where)
  1804. ->where('u.id', '>', 0)
  1805. ->where(function ($query) use ($status) {
  1806. if ($status == 1) {
  1807. $query->where('uc.status', 1);
  1808. } else {
  1809. $query->where('uc.status', '>', 1);
  1810. }
  1811. })
  1812. ->field($field)
  1813. ->order('uc.status asc,uc.created_at desc')
  1814. ->paginate($pageSize)
  1815. ->each(function ($item, $k) use ($expire) {
  1816. $expire = $expire ? $expire * 86400 : 0;
  1817. $status = isset($item['status']) ? intval($item['status']) : 0;
  1818. $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
  1819. $rtime = $item['created_at'] ? strtotime($item['created_at']) : 0;
  1820. $rtime = $rtime > time() - $expire ? $rtime - (time() - $expire) : 0;
  1821. $item['rtime'] = $rtime;
  1822. $rtime = $rtime > 23 * 3600 ? ceil($rtime / 86400) . '天' : ($rtime > 3600 ? ceil($rtime / 3600) . '小时' : ($rtime > 60 ? ceil($rtime / 60) . '分钟' : ''));
  1823. if ($status == 1) {
  1824. if ($rtime) {
  1825. $item['time'] = '<em class="red">' . $rtime . '后</em> 自动拒绝';
  1826. } else {
  1827. $item['time'] = '待处理';
  1828. }
  1829. } else {
  1830. $item['time'] = $status == 2 ? '已通过' : ($status == 3 ? '已拒绝' : '待处理');
  1831. if ($status == 4) {
  1832. $info['time'] = '过期自动处理';
  1833. }
  1834. }
  1835. Member::formatUser($item);
  1836. return $item;
  1837. });
  1838. $dataList = $dataList ? $dataList->toArray() : ['total' => 0];
  1839. $dataList['total1'] = Member::getMessageTotal($userId, $params);
  1840. return $dataList;
  1841. }
  1842. /**
  1843. * 获取用户申请消息列表
  1844. * @param $params
  1845. * @param $pageSize
  1846. * @param string $field
  1847. * @return $this
  1848. */
  1849. public static function getMessageInfo($id, $userId = 0, $field = '')
  1850. {
  1851. $siteInfo = cmf_get_site_info();
  1852. $expire = isset($siteInfo['contact_time']) ? intval($siteInfo['contact_time']) : 0;
  1853. $expire = $expire ? $expire : 1;
  1854. $type = 1;
  1855. $joinWhere = 'u.id=uc.user_id';
  1856. $data = UserContactLog::where(['id' => $id])->field('user_id,contact_uid')->find();
  1857. if ($userId && isset($data['user_id']) && $data['user_id'] == $userId) {
  1858. $joinWhere = 'u.id=uc.contact_uid';
  1859. $type = 2;
  1860. }
  1861. $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';
  1862. $info = UserContactLog::alias('uc')
  1863. ->join('user u', $joinWhere, 'left')
  1864. ->join('user_profile mp', 'mp.userid=u.id', 'left')
  1865. ->where(['uc.id' => $id])
  1866. ->where('u.id', '>', 0)
  1867. ->field($field)
  1868. ->find();
  1869. $info = $info ? $info->toArray() : [];
  1870. if ($info) {
  1871. $info['type'] = $type;
  1872. $expire = $expire ? $expire * 86400 : 0;
  1873. $status = isset($info['status']) ? intval($info['status']) : 0;
  1874. $info['avatar'] = isset($info['avatar']) ? cmf_get_image_preview_url($info['avatar']) : '';
  1875. $rtime = $info['created_at'] ? strtotime($info['created_at']) : 0;
  1876. $rtime = $rtime > time() - $expire ? $rtime - (time() - $expire) : 0;
  1877. $info['rtime'] = $rtime;
  1878. $rtime = $rtime > 23 * 3600 ? ceil($rtime / 86400) . '天' : ($rtime > 3600 ? ceil($rtime / 3600) . '小时' : ($rtime > 60 ? ceil($rtime / 60) . '分钟' : ''));
  1879. if ($status == 1) {
  1880. if ($rtime) {
  1881. $info['time'] = '<em class="red">' . $rtime . '后</em> 自动拒绝';
  1882. } else {
  1883. $info['time'] = '待处理';
  1884. }
  1885. } else {
  1886. $info['time'] = $status == 2 ? '已通过' : ($status == 3 ? '已拒绝' : '待处理');
  1887. if ($status == 4) {
  1888. $info['time'] = '过期自动处理';
  1889. }
  1890. }
  1891. $info['show_wechat'] = true;
  1892. $rtime = $info['created_at'] ? strtotime($info['created_at']) : 0;
  1893. $info['rtime'] = $rtime;
  1894. if ($rtime <= time() - 7 * 24 * 3600) {
  1895. $info['show_wechat'] = false;
  1896. }
  1897. Member::formatUser($info);
  1898. }
  1899. return $info;
  1900. }
  1901. /**
  1902. * 余额明细
  1903. * @param $params
  1904. * @param int $pageSize
  1905. * @return mixed
  1906. */
  1907. public static function getBalanceLog($params, $pageSize = 15)
  1908. {
  1909. $where = [];
  1910. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  1911. if ($userId) {
  1912. $where['b.user_id'] = $userId;
  1913. }
  1914. $changeType = isset($params['change_type']) ? $params['change_type'] : 0;
  1915. if ($changeType) {
  1916. $where['b.change_type'] = $changeType;
  1917. }
  1918. $type = isset($params['type']) ? $params['type'] : 0;
  1919. $model = db('user_balance_log')->alias('b')
  1920. ->leftJoin('user u', 'u.id=b.source_uid')
  1921. ->where($where)
  1922. ->where(function ($query) use ($type) {
  1923. if ($type == 'income') {
  1924. $query->where('b.type', '>', 1);
  1925. } else if ($type) {
  1926. $type = is_array($type) ? $type : [$type];
  1927. $query->whereIn('b.type', $type);
  1928. }
  1929. });
  1930. $types = config('weixin.incomeTypes');
  1931. $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')
  1932. ->where('b.status', 'lt', 4)
  1933. ->order("b.create_time desc")
  1934. ->paginate($pageSize)
  1935. ->each(function ($item, $k) use ($types) {
  1936. $item['format_time'] = $item['create_time'] ? date('Y年m月d日 H点i分', $item['create_time']) : '';
  1937. $item['money'] = floatval($item['money']);
  1938. $type = isset($item['type']) ? $item['type'] : 0;
  1939. $item['typeName'] = isset($types[$type]) ? $types[$type] : '';
  1940. return $item;
  1941. });
  1942. $dataList = $dataList ? $dataList->toArray() : [];
  1943. if ($dataList) {
  1944. $total = $model->where('b.status', '=', 2)->sum('b.change');
  1945. $dataList['total_money'] = moneyFormat($total);
  1946. $total = db('user_balance_log')->alias('b')
  1947. ->leftJoin('user u', 'u.id=b.source_uid')
  1948. ->where($where)
  1949. ->where(function ($query) use ($type) {
  1950. if ($type == 'income') {
  1951. $query->where('b.type', '>', 1);
  1952. } else if ($type) {
  1953. $type = is_array($type) ? $type : [$type];
  1954. $query->whereIn('b.type', $type);
  1955. }
  1956. })
  1957. ->where('b.status', '=', 1)
  1958. ->order("b.create_time desc")
  1959. ->sum('b.change');
  1960. $dataList['nopay_money'] = moneyFormat($total);
  1961. }
  1962. return $dataList;
  1963. }
  1964. /**
  1965. * 邀请扽团队成员或会员列表
  1966. * @param $params 参数
  1967. * @param $type 类型:1-团队成员,2-邀请的会员
  1968. * @param int $pageSize
  1969. * @return mixed
  1970. */
  1971. public static function getInviteList($inviteId, $type = 1, $pageSize = 15)
  1972. {
  1973. $where = ['user_type' => 2, 'user_status' => 1];
  1974. if ($inviteId) {
  1975. $where['parent_id'] = $inviteId;
  1976. }
  1977. if ($type == 1) {
  1978. $where['agent_type'] = 1;
  1979. $orderBy = "agent_create_time desc";
  1980. } else {
  1981. $where['agent_type'] = 0;
  1982. $orderBy = "create_time desc";
  1983. }
  1984. $model = Member::where($where)
  1985. ->where(function ($query) use ($type) {
  1986. if ($type == 1) {
  1987. $query->where('agent_status', '<', 4);
  1988. } else {
  1989. $query->whereIn('user_status', [0, 1, 2]);
  1990. }
  1991. });
  1992. $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';
  1993. $dataList = $model->field($field)
  1994. ->order($orderBy)
  1995. ->paginate($pageSize)
  1996. ->each(function ($item, $k) use ($type, $inviteId) {
  1997. $item['avatar'] = $item['avatar'] ? cmf_get_image_preview_url($item['avatar']) : '';
  1998. if ($type == 1) {
  1999. $item['format_time'] = $item['agent_create_time'] ? date('Y-m-d H:i', $item['agent_create_time']) : '';
  2000. $item['invite_num'] = Member::getInviteCounts(['parent_id' => $item['id']], 2);
  2001. $item['invite_market_num'] = Member::getInviteCounts(['parent_id' => $item['id']], 1);
  2002. $item['invite_sum'] = Member::getBalanceCount($inviteId, $item['id']);
  2003. } else {
  2004. // VIP
  2005. $vipAuth = isset($item['vip_auth']) ? intval($item['vip_auth']) : 0;
  2006. $vipExpire = isset($item['vip_expire']) ? intval($item['vip_expire']) : 0;
  2007. if ($vipAuth && $vipExpire >= time()) {
  2008. $item['vip_auth'] = 1;
  2009. $item['vip_expire'] = date('Y-m-d', $item['vip_expire']);
  2010. } else {
  2011. $item['vip_auth'] = 0;
  2012. $item['vip_expire'] = '';
  2013. }
  2014. $item['format_time'] = $item['create_time'] ? date('Y-m-d H:i', $item['create_time']) : '';
  2015. $item['invite_sum'] = Member::getBalanceCount($inviteId, $item['id'], 2);
  2016. }
  2017. $item['avatar'] = $item['avatar'] ? cmf_get_image_preview_url($item['avatar']) : '';
  2018. return $item;
  2019. });
  2020. $dataList = $dataList ? $dataList->toArray() : [];
  2021. if ($dataList) {
  2022. $counts = [];
  2023. if ($type == 1) {
  2024. $counts['invite_num'] = Member::getInviteCounts(['parent_id' => $inviteId], 1);
  2025. $total = Member::getBalanceCount($inviteId);
  2026. $counts['invite_sum'] = moneyFormat($total);
  2027. } else {
  2028. $counts['invite_sex_1'] = Member::getInviteCounts(['parent_id' => $inviteId, 'sex' => 1], 2);
  2029. $counts['invite_sex_2'] = Member::getInviteCounts(['parent_id' => $inviteId, 'sex' => 2], 2);
  2030. $counts['invite_vip'] = Member::getInviteCounts(['parent_id' => $inviteId, 'vip_auth' => 1], 2);
  2031. $total = Member::getBalanceCount($inviteId, 0, 2);
  2032. $counts['invite_sum'] = moneyFormat($total);
  2033. }
  2034. $dataList['counts'] = $counts;
  2035. }
  2036. return $dataList;
  2037. }
  2038. /**
  2039. * 统计邀请的会员或团队成员
  2040. * @param $params
  2041. * @param int $type
  2042. * @return float|int|string
  2043. */
  2044. public static function getInviteCounts($params, $type = 1)
  2045. {
  2046. $where = ['user_type' => 2, 'user_status' => 1];
  2047. $inviteId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  2048. if ($inviteId) {
  2049. $where['parent_id'] = $inviteId;
  2050. }
  2051. if ($type == 1) {
  2052. $where['agent_type'] = 1;
  2053. } else {
  2054. $where['agent_type'] = 0;
  2055. }
  2056. $sex = isset($params['sex']) ? $params['sex'] : 0;
  2057. if ($sex) {
  2058. $where['sex'] = $sex;
  2059. }
  2060. $sex = isset($params['sex']) ? $params['sex'] : 0;
  2061. if ($sex) {
  2062. $where['sex'] = $sex;
  2063. }
  2064. $count = Member::where($where)
  2065. ->where(function ($query) use ($type) {
  2066. if ($type == 1) {
  2067. $query->where('agent_status', '<', 4);
  2068. } else {
  2069. $query->whereIn('user_status', [0, 1, 2]);
  2070. }
  2071. })
  2072. ->where(function ($query) use ($params) {
  2073. $sex = isset($params['sex']) ? $params['sex'] : 0;
  2074. if ($sex) {
  2075. $query->where('sex', '=', $sex);
  2076. }
  2077. $vipAuth = isset($params['vip_auth']) ? $params['vip_auth'] : 0;
  2078. if ($vipAuth) {
  2079. $query->where('vip_auth', '=', $vipAuth)->where('vip_expire', '>=', time());
  2080. }
  2081. })
  2082. ->count('id');
  2083. return intval($count);
  2084. }
  2085. /**
  2086. * 统计收益金额
  2087. * @param $userId 所属用户
  2088. * @param int $sourceUid 来源用户
  2089. * @param int $type 类型:0-收益分成,
  2090. * @return float
  2091. */
  2092. public static function getBalanceCount($userId, $sourceUid = 0, $userType = 1, $profitType = 0)
  2093. {
  2094. $where = ['b.user_id' => $userId, 'b.status' => 2, 'u.user_type' => 2];
  2095. if ($sourceUid) {
  2096. $where['b.source_uid'] = $sourceUid;
  2097. }
  2098. if ($userType == 1) {
  2099. $where['u.agent_type'] = 1;
  2100. } else {
  2101. $where['u.agent_type'] = 0;
  2102. }
  2103. if ($profitType) {
  2104. $where['b.type'] = $profitType;
  2105. }
  2106. //var_dump($where);
  2107. $sum = UserBalanceLog::alias('b')
  2108. ->leftJoin('user u', 'u.id=b.source_uid')
  2109. ->where($where)
  2110. ->where('b.type', '>', 1)
  2111. ->where('b.type', '<', 30)
  2112. ->sum('b.pay_money');
  2113. return moneyFormat($sum);
  2114. }
  2115. /**
  2116. * 投诉拉黑用户列表
  2117. * @param int $type 类型
  2118. * @param int $pageSize 分页大小
  2119. * @return array|bool|\think\Paginator
  2120. * @throws \think\exception\DbException
  2121. */
  2122. public static function getComplainList($type, $pageSize = 30)
  2123. {
  2124. $page = input('page', 1);
  2125. $cacheKey = "cache:complainList:p_{$type}_{$page}_{$pageSize}";
  2126. $dataList = PRedis::get($cacheKey);
  2127. if ($dataList) {
  2128. return $dataList;
  2129. }
  2130. $complainTypes = config('weixin.complainTypes');
  2131. $dataList = Complain::alias('c')
  2132. ->leftJoin('user u', 'u.id=c.c_uid')
  2133. ->field('c.id,u.id as uid,u.real_name,u.user_nickname,u.avatar,u.mobile,c.type,c.remark,c.status')
  2134. ->where(['c.status' => 2])
  2135. ->where('u.id', '>', 0)
  2136. ->where(function ($query) use ($type) {
  2137. if ($type == 2) {
  2138. $query->where('c.created_time', '>=', time() - 3 * 24 * 3600);
  2139. }
  2140. })
  2141. ->order('c.created_time desc')
  2142. ->paginate($pageSize)
  2143. ->each(function ($item, $k) use ($complainTypes) {
  2144. $item['wechat_code'] = UserProfile::where(['userid' => $item['uid']])->value('wechat_code');
  2145. $item['wechat_code'] = $item['wechat_code'] ? formatName($item['wechat_code']) : '';
  2146. $item['avatar'] = $item['avatar'] ? cmf_get_image_preview_url($item['avatar']) : '';
  2147. $type = isset($item['type']) ? $item['type'] : 8;
  2148. $item['type_name'] = isset($complainTypes[$type]) ? $complainTypes[$type] : '其他';
  2149. $item['remark_count'] = $item['remark'] ? mb_strlen($item['remark'], 'utf-8') : 0;
  2150. $item['remark_sub'] = $item['remark_count'] > 30 ? substr($item['remark'], 0, 30) : $item['remark'];
  2151. });
  2152. if ($dataList) {
  2153. PRedis::set($cacheKey, $dataList, 10);
  2154. }
  2155. return $dataList;
  2156. }
  2157. /**
  2158. * 解锁用户访问信息
  2159. * @param $uid 当前用户
  2160. * @param $cuid 被解锁用户
  2161. * @return array|int
  2162. * @throws \think\Exception
  2163. * @throws \think\db\exception\DataNotFoundException
  2164. * @throws \think\db\exception\ModelNotFoundException
  2165. * @throws \think\exception\DbException
  2166. * @throws \think\exception\PDOException
  2167. */
  2168. public static function catchUnlock($uid, $cuid)
  2169. {
  2170. // 验证用户信息
  2171. $memberInfo = Member::where(['id' => $uid, 'user_status' => 1])->field('id,user_nickname,redheart,vip_auth,vip_expire,openid')->find();
  2172. $memberInfo = $memberInfo ? $memberInfo->toArray() : [];
  2173. if (!$memberInfo) {
  2174. return 2102;
  2175. }
  2176. $accessMemberInfo = Member::where(['id' => $cuid, 'user_status' => 1])->field('id,user_nickname,redheart,openid')->find();
  2177. if (!$accessMemberInfo) {
  2178. return 8305;
  2179. }
  2180. // 验证访问信息
  2181. $accessInfo = UserAccess::where(['user_id' => $uid, 'access_uid' => $cuid, 'status' => 1])->find();
  2182. if (!$accessInfo) {
  2183. return 1003;
  2184. }
  2185. if ($accessInfo->is_lock == 1) {
  2186. return 8304;
  2187. }
  2188. // 验证账户
  2189. $accountConfig = cmf_get_option('account_config');
  2190. $lockAccessPay = isset($accountConfig['lock_access_pay']) ? intval($accountConfig['lock_access_pay']) : 0;
  2191. // 会员
  2192. $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0;
  2193. $vipExpire = isset($memberInfo['vip_expire']) ? intval($memberInfo['vip_expire']) : 0;
  2194. $lockAccessPay = $vipAuth && $vipExpire >= time() ? 0 : $lockAccessPay;
  2195. if ($lockAccessPay <= 0) {
  2196. UserAccess::where(['user_id' => $uid, 'access_uid' => $cuid, 'status' => 1])->update(['is_lock' => 1]);
  2197. return ['id' => $accessInfo->id];
  2198. }
  2199. // 付费解锁
  2200. $redheart = isset($memberInfo['redheart']) ? $memberInfo['redheart'] : 0;
  2201. if ($redheart < $lockAccessPay) {
  2202. return 2137;
  2203. }
  2204. // 更新
  2205. Db::startTrans();
  2206. if (!UserAccess::where(['user_id' => $uid, 'access_uid' => $cuid, 'status' => 1])->update(['is_lock' => 1])) {
  2207. Db::rollback();
  2208. return 2032;
  2209. }
  2210. // 扣除账户
  2211. if (!Member::where(['id' => $uid, 'user_status' => 1])->setDec('redheart', $lockAccessPay)) {
  2212. Db::rollback();
  2213. return 2031;
  2214. }
  2215. // 账户明细
  2216. $data = [
  2217. 'user_id' => $uid,
  2218. 'type' => 2,
  2219. 'account_type' => 1,
  2220. 'change_type' => 2,
  2221. 'money' => $lockAccessPay,
  2222. 'balance' => $redheart,
  2223. 'remark' => '解锁用户【' . $accessMemberInfo->user_nickname . '】访问信息支付' . $lockAccessPay . '颗爱心',
  2224. 'created_at' => date('Y-m-d H:i:s'),
  2225. 'status' => 2
  2226. ];
  2227. if (!AccountLog::insertGetId($data)) {
  2228. Db::rollback();
  2229. return 8306;
  2230. }
  2231. Db::commit();
  2232. return ['id' => $accessInfo->id];
  2233. }
  2234. /**
  2235. * 更新身份认证信息
  2236. * @param $userId
  2237. * @param $realname
  2238. * @param $idcard
  2239. * @return bool
  2240. */
  2241. public static function updateAuth($userId, $realname, $idcard)
  2242. {
  2243. Db::startTrans();
  2244. if (!Member::saveData(['id' => $userId], ['real_name' => $realname])) {
  2245. Db::rollback();
  2246. return false;
  2247. }
  2248. // 在线验证是否通过
  2249. $profileData = [
  2250. 'idcard' => $idcard,
  2251. 'idcard_online_check' => 1,
  2252. 'idcard_type' => 1,
  2253. 'idcard_check' => 1,
  2254. ];
  2255. if (UserProfile::checkProfile($userId)) {
  2256. $profileData['updated_at'] = date('Y-m-d H:i:s');
  2257. $res = UserProfile::saveData(['userid' => $userId], $profileData);
  2258. } else {
  2259. $res = UserProfile::insertGetId($profileData);
  2260. }
  2261. if (!$res) {
  2262. Db::rollback();
  2263. return false;
  2264. }
  2265. // 操作日志
  2266. UserLog::saveLog(['user_id' => $userId, 'type' => 1, 'content' => '提交身份证认证']);
  2267. Db::commit();
  2268. return true;
  2269. }
  2270. /**
  2271. * 统计清除爱心
  2272. * @param $userId
  2273. * @return array
  2274. * @throws \think\db\exception\DataNotFoundException
  2275. * @throws \think\db\exception\ModelNotFoundException
  2276. * @throws \think\exception\DbException
  2277. */
  2278. public static function getSignReadheart($userId){
  2279. $siteInfo = cmf_get_option('site_info');
  2280. $signClearDay = isset($siteInfo['sign_clear_day'])? intval($siteInfo['sign_clear_day']) : 0;
  2281. $signClearDay = $signClearDay? min($signClearDay, 28) : 1;
  2282. $signClearDay = $signClearDay<10? '0'.$signClearDay : $signClearDay;
  2283. $datas = [
  2284. 'clear_day'=> date('m月'.$signClearDay.'号'),
  2285. 'clear_month'=>date('m月', strtotime(date('Y-m-01')) - 86400),
  2286. 'sign_total'=> 0,
  2287. 'sign_clear'=> 0,
  2288. 'show_clear'=> 1,
  2289. ];
  2290. // 最早
  2291. $month = date('Y-m-01', strtotime(date('Y-m-01')) - 86400);
  2292. $firstSign = AccountLog::where(['user_id'=> $userId,'type'=> 12,'status'=> 2])
  2293. ->where('created_at','>=', $month)
  2294. ->order('created_at','asc')
  2295. ->find();
  2296. $time = isset($firstSign['created_at'])? $firstSign['created_at'] : '';
  2297. $check = AccountLog::where(['user_id' => $userId, 'type'=> 13, 'status' => 2])
  2298. ->where('created_at', '>=', $time)
  2299. ->find();
  2300. if(empty($check) && date('Y-m-d') > date('Y-m-'.$signClearDay)){
  2301. $check = true;
  2302. }
  2303. if($firstSign && $time) {
  2304. if($check){
  2305. $datas['clear_day'] = date('m月'.$signClearDay.'号', strtotime('next month'));
  2306. $datas['clear_month'] = date('m月');
  2307. // 签到后清除月份总消费
  2308. $totalCost = AccountLog::where(['user_id' => $userId, 'status' => 2, 'account_type' => 1, 'change_type' => 2])
  2309. ->where('created_at', '>', date('Y-m-01 01:00:00'))
  2310. ->sum('money');
  2311. $totalClearSign = AccountLog::where(['user_id' => $userId, 'type' => 12, 'status' => 2])
  2312. ->where('created_at', '>', date('Y-m-01 01:00:00'))
  2313. ->sum('money');
  2314. }else{
  2315. // 签到后清除月份总消费
  2316. $totalCost = AccountLog::where(['user_id' => $userId, 'status' => 2, 'account_type' => 1, 'change_type' => 2])
  2317. ->where('created_at', '>=', $time)
  2318. ->where('created_at', '<=', date('Y-m-01 01:00:00'))
  2319. ->sum('money');
  2320. $totalClearSign = AccountLog::where(['user_id' => $userId, 'type' => 12, 'status' => 2])
  2321. ->where('created_at', '>=', $time)
  2322. ->where('created_at', '<=', date('Y-m-01 01:00:00'))
  2323. ->sum('money');
  2324. }
  2325. $clearRedheart = ($totalClearSign - $totalCost);
  2326. $datas['sign_clear'] = $clearRedheart;
  2327. }
  2328. if($check){
  2329. $totalSign = AccountLog::where(['user_id' => $userId, 'type' => 12, 'status' => 2])
  2330. ->where('created_at', '>', date('Y-m-01 01:00:00'))
  2331. ->sum('money');
  2332. $cost = AccountLog::where(['user_id' => $userId, 'account_type' => 1, 'change_type' => 2, 'status' => 2])
  2333. ->where('created_at', '>', date('Y-m-01 01:00:00'))
  2334. ->sum('money');
  2335. }else{
  2336. $totalSign = AccountLog::where(['user_id' => $userId, 'type' => 12, 'status' => 2])
  2337. ->where('created_at', '>=', $time)
  2338. ->sum('money');
  2339. $cost = AccountLog::where(['user_id' => $userId, 'account_type' => 1, 'change_type' => 2, 'status' => 2])
  2340. ->where('created_at', '>=', $time)
  2341. ->sum('money');
  2342. }
  2343. $datas['sign_total'] = $totalSign>$cost? $totalSign-$cost : 0;
  2344. if($datas['sign_total'] <= 0){
  2345. $datas['sign_clear'] = 0;
  2346. }
  2347. return $datas;
  2348. }
  2349. }