Member.php 101 KB

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