Member.php 95 KB

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