Member.php 110 KB

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