Member.php 109 KB

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