UserController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\matchmaker\controller;
  12. use app\common\Constant;
  13. use app\common\Fun;
  14. use app\love\model\UserModel;
  15. use app\matchmaker\model\MatchmakerUserFollowModel;
  16. use app\matchmaker\model\MatchmakerUserMatingModel;
  17. use app\matchmaker\model\MatchmakerUserModel;
  18. class UserController extends MatchmakerBaseController
  19. {
  20. public function infoAdd()
  21. {
  22. $data = [
  23. 'sex' => Constant::SEX_MATCHMAKER,
  24. 'marry' => Constant::MARRY,
  25. 'high' => Constant::HIGH,
  26. 'weight' => Constant::WEIGHT,
  27. 'education' => Constant::EDUCATION,
  28. 'income' => Constant::INCOME,
  29. 'nation' => Constant::NATION,
  30. 'smoke' => Constant::SMOKE,
  31. 'drink' => Constant::DRINK,
  32. 'family' => Constant::FAMILY,
  33. 'tinyint' => Constant::TINYINT,
  34. 'animal' => Constant::ANIMAL,
  35. ];
  36. foreach ($data as &$v) {
  37. $v = json_encode($v);
  38. unset($v);
  39. }
  40. return $this->fetch('', $data);
  41. }
  42. public function infoAddPost()
  43. {
  44. $value = $this->request->post();
  45. $value['birthday'] = strtotime($value['birthday']);
  46. $value['matchmaker_id'] = $this->matchmaker['id'];
  47. $user = MatchmakerUserModel::create($value);
  48. MatchmakerUserMatingModel::create(['user_id' => $user['id'], 'animal' => []]);
  49. $this->success('', '', ['id' => $user['id']]);
  50. }
  51. public function matingAdd()
  52. {
  53. $id = $this->request->param('id');
  54. if (empty($id)) {
  55. $this->error('参数错误');
  56. }
  57. $mating = MatchmakerUserMatingModel::get(['user_id' => $id]);
  58. if (empty($mating['animal'])) {
  59. $mating['animal'] = [];
  60. }
  61. $this->assign('mating', $mating);
  62. $education = Constant::EDUCATION;
  63. $education[] = '无要求';
  64. $data = [
  65. 'min_age' => Constant::MIN_AGE,
  66. 'max_age' => Constant::MAX_AGE,
  67. 'min_high' => Constant::MIN_HIGH,
  68. 'max_high' => Constant::MAX_HIGH,
  69. 'min_weight' => Constant::MIN_WEIGHT,
  70. 'max_weight' => Constant::MAX_WEIGHT,
  71. 'native' => Constant::NATIVE,
  72. 'education' => $education,
  73. 'income' => Constant::COND_INCOME,
  74. 'smoke' => Constant::MATTING_SMOKING,
  75. 'drink' => Constant::MATTING_DRINK,
  76. 'tinyint' => Constant::COND_TINYINT,
  77. 'animal' => Constant::ANIMAL,
  78. ];
  79. foreach ($data as &$v) {
  80. $v = json_encode($v);
  81. unset($v);
  82. }
  83. return $this->fetch('', $data);
  84. }
  85. public function matingAddPost()
  86. {
  87. $param = $this->request->post();
  88. if (empty($param['animal'])) {
  89. $param['animal'] = [];
  90. }
  91. MatchmakerUserMatingModel::update($param);
  92. $this->success('操作成功');
  93. }
  94. public function infoEdit()
  95. {
  96. $id = $this->request->param('id');
  97. if (empty($id)) {
  98. $this->error('参数错误');
  99. }
  100. $user = MatchmakerUserModel::get($id);
  101. if (empty($user)) {
  102. $this->error('该用户不存在或已删除');
  103. }
  104. $user['birthday'] = date('Y-m-d', $user['birthday']);
  105. $this->assign('user', json_encode($user));
  106. $data = [
  107. 'sex' => Constant::SEX_MATCHMAKER,
  108. 'marry' => Constant::MARRY,
  109. 'high' => Constant::HIGH,
  110. 'weight' => Constant::WEIGHT,
  111. 'education' => Constant::EDUCATION,
  112. 'income' => Constant::INCOME,
  113. 'nation' => Constant::NATION,
  114. 'smoke' => Constant::SMOKE,
  115. 'drink' => Constant::DRINK,
  116. 'family' => Constant::FAMILY,
  117. 'tinyint' => Constant::TINYINT,
  118. 'animal' => Constant::ANIMAL,
  119. ];
  120. foreach ($data as &$v) {
  121. $v = json_encode($v);
  122. unset($v);
  123. }
  124. return $this->fetch('', $data);
  125. }
  126. public function infoEditPost()
  127. {
  128. $value = $this->request->post();
  129. $value['birthday'] = strtotime($value['birthday']);
  130. MatchmakerUserModel::update($value);
  131. $this->success();
  132. }
  133. public function matingEdit()
  134. {
  135. $id = $this->request->param('id');
  136. if (empty($id)) {
  137. $this->error('参数错误');
  138. }
  139. $mating = MatchmakerUserMatingModel::get(['user_id' => $id]);
  140. if (empty($mating['animal'])) {
  141. $mating['animal'] = [];
  142. }
  143. $this->assign('mating', $mating);
  144. $education = Constant::EDUCATION;
  145. $education[] = '无要求';
  146. $data = [
  147. 'min_age' => Constant::MIN_AGE,
  148. 'max_age' => Constant::MAX_AGE,
  149. 'min_high' => Constant::MIN_HIGH,
  150. 'max_high' => Constant::MAX_HIGH,
  151. 'min_weight' => Constant::MIN_WEIGHT,
  152. 'max_weight' => Constant::MAX_WEIGHT,
  153. 'native' => Constant::NATIVE,
  154. 'education' => $education,
  155. 'income' => Constant::COND_INCOME,
  156. 'smoke' => Constant::MATTING_SMOKING,
  157. 'drink' => Constant::MATTING_DRINK,
  158. 'tinyint' => Constant::COND_TINYINT,
  159. 'animal' => Constant::ANIMAL,
  160. ];
  161. foreach ($data as &$v) {
  162. $v = json_encode($v);
  163. unset($v);
  164. }
  165. return $this->fetch('', $data);
  166. }
  167. public function matingEditPost()
  168. {
  169. $param = $this->request->post();
  170. if (empty($param['animal'])) {
  171. $param['animal'] = [];
  172. }
  173. MatchmakerUserMatingModel::update($param);
  174. $this->success('操作成功');
  175. }
  176. /**
  177. * 图片上传
  178. */
  179. public function imageUpload()
  180. {
  181. $file = $this->request->post('file');
  182. $ext = pathinfo($this->request->post('name'))['extension'];
  183. if (!in_array($ext, ['jpg', 'jpeg', 'png'])) {
  184. $this->error('文件后缀必须为jpg,jpeg,png');
  185. }
  186. if ($file) {
  187. //创建目录
  188. $upload_dir = WEB_ROOT . 'upload/image';
  189. if (!is_dir($upload_dir)) {
  190. @mkdir($upload_dir);
  191. }
  192. $upload_dir = $upload_dir . '/' . date('Ymd');
  193. if (!is_dir($upload_dir)) {
  194. @mkdir($upload_dir);
  195. }
  196. //保存文件
  197. $file_name = $this->_file_name($ext);
  198. $file_path = $upload_dir . '/' . $file_name;
  199. $is_upload = $this->_base64_image_content($file, $file_path);
  200. if (!$is_upload) {
  201. $this->error('上传失败,请重新上传');
  202. }
  203. $avatar = cmf_get_user_avatar_url('image' . '/' . date('Ymd') . '/' . $file_name);
  204. $this->result([
  205. 'avatar' => $avatar,
  206. ], 1);
  207. } else {
  208. $this->error('请上传文件');
  209. }
  210. }
  211. /**
  212. * 跟进
  213. */
  214. public function follow()
  215. {
  216. $id = $this->request->get('id');
  217. $list = MatchmakerUserFollowModel::where('user_id', $id)->append(['create_time_text'])->order('create_time desc')->select();
  218. $info = MatchmakerUserModel::get($id);
  219. $info['age'] = Fun::getAgeByBirth($info['birthday']);
  220. $info['sex_text'] = Constant::SEX[$info['sex']];
  221. return $this->fetch('', ['list' => $list, 'id' => $id, 'info' => $info]);
  222. }
  223. /**
  224. * 跟进提交
  225. */
  226. public function followPost()
  227. {
  228. $data = $this->request->param();
  229. if (empty($data['user_id'])) {
  230. $this->error('数据异常,请刷新重试');
  231. }
  232. if (empty($data['content'])) {
  233. $this->error('请输入内容');
  234. }
  235. $data['create_time'] = time();
  236. MatchmakerUserFollowModel::create($data);
  237. $this->success('操作成功');
  238. }
  239. public function deleteUser()
  240. {
  241. $id = $this->request->post('id', 0);
  242. if (empty($id)) {
  243. $this->error('数据异常,请刷新重试');
  244. }
  245. MatchmakerUserModel::destroy($id);
  246. MatchmakerUserMatingModel::destroy(['user_id' => $id]);
  247. MatchmakerUserFollowModel::destroy(['user_id' => $id]);
  248. $this->success('操作成功');
  249. }
  250. public function statusUser()
  251. {
  252. $id = $this->request->post('id', 0);
  253. $status = $this->request->post('status', 0);
  254. if (empty($id) || empty($status)) {
  255. $this->error('数据异常,请刷新重试');
  256. }
  257. MatchmakerUserModel::update(['status' => $status], ['id' => $id]);
  258. $this->success('操作成功');
  259. }
  260. /**
  261. * 匹配
  262. */
  263. public function match()
  264. {
  265. $id = $this->request->param('id');
  266. $type = $this->request->param('type', 0);
  267. $match_field = $this->request->param('match_field', '');
  268. $mating = MatchmakerUserMatingModel::where('user_id', $id)->find();
  269. $where = $this->_matchWhere($match_field, $mating);
  270. if (empty($id)) {
  271. $this->error('该用户不存在');
  272. }
  273. $user = MatchmakerUserModel::get($id);
  274. if (empty($user)) {
  275. $this->error('该用户不存在');
  276. }
  277. if ($type == 0) {
  278. //自有库
  279. $list = MatchmakerUserModel::where('matchmaker_id', $this->matchmaker->id)
  280. ->where($where)
  281. ->where('id', '<>', $id)
  282. ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
  283. ->where('status',2)
  284. ->append(['sex_text'])
  285. ->select();
  286. } elseif ($type == 1) {
  287. //其他红娘库
  288. $list = MatchmakerUserModel::with('matchmaker')
  289. ->where($where)
  290. ->where('matchmaker_id', '<>', $this->matchmaker->id)
  291. ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
  292. ->where('status',2)
  293. ->append(['sex_text'])
  294. ->select();
  295. } else {
  296. //系统库
  297. $list = UserModel::where('user_type', 2)
  298. ->where($where)
  299. ->where('user_status', 1)
  300. ->where('want_status',2)
  301. ->where('is_complete', 1)
  302. ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
  303. ->append(['sex_text'])
  304. ->select();
  305. }
  306. $res = $this->_orderMatchUser($list->toArray(), $mating);
  307. return $this->fetch('', [
  308. 'list' => json_encode($res),
  309. 'type' => $type,
  310. 'user' => $user,
  311. 'id' => $id,
  312. 'mating' => $mating,
  313. 'match_field' => $match_field,
  314. 'tinyint' => json_encode(Constant::TINYINT),
  315. 'cond_tinyint' => json_encode(Constant::COND_TINYINT),
  316. ]);
  317. }
  318. private function _file_name($ext)
  319. {
  320. //生成随机文件名
  321. //定义一个包含大小写字母数字的字符串
  322. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  323. //把字符串分割成数组
  324. $newchars = str_split($chars);
  325. //打乱数组
  326. shuffle($newchars);
  327. //从数组中随机取出15个字符
  328. $chars_key = array_rand($newchars, 15);
  329. //把取出的字符重新组成字符串
  330. $fnstr = '';
  331. for ($i = 0; $i < 15; $i++) {
  332. $fnstr .= $newchars[$chars_key[$i]];
  333. }
  334. //输出文件名并做MD5加密
  335. return md5($fnstr . microtime(true) * 1000) . '.' . $ext;
  336. }
  337. private function _base64_image_content($base64_image_content, $file_path)
  338. {
  339. //匹配出图片的格式
  340. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  341. if (file_put_contents($file_path, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  342. return true;
  343. } else {
  344. return false;
  345. }
  346. } else {
  347. return false;
  348. }
  349. }
  350. private function _matchWhere($field, $mating)
  351. {
  352. $where = [];
  353. $match_field = [];
  354. if (!empty($field)) {
  355. $match_field = explode(',', $field);
  356. }
  357. //年龄
  358. if (in_array('age', $match_field)) {
  359. $year = date('Y');
  360. if (!empty($mating['min_age'])) {
  361. $min_year = $year - $mating['min_age'];
  362. $min_year_time = strtotime(date($min_year . '-m-d H:i:s'));
  363. $where[] = ['birthday', '<', $min_year_time];
  364. }
  365. if (!empty($mating['max_age'])) {
  366. $max_year = $year - $mating['max_age'];
  367. $max_year_time = strtotime(date($max_year . '-m-d H:i:s'));
  368. $where[] = ['birthday', '>', $max_year_time];
  369. }
  370. }
  371. //身高
  372. if (in_array('high', $match_field)) {
  373. if (!empty($mating['min_high'])) {
  374. $where[] = ['high', '>', $mating['min_high']];
  375. }
  376. if (!empty($mating['max_high'])) {
  377. $where[] = ['high', '<', $mating['max_high']];
  378. }
  379. }
  380. //体重
  381. if (in_array('weight', $match_field)) {
  382. if (!empty($mating['min_weight'])) {
  383. $where[] = ['weight', '>', $mating['min_weight']];
  384. }
  385. if (!empty($mating['max_weight'])) {
  386. $where[] = ['weight', '<', $mating['max_weight']];
  387. }
  388. }
  389. //生肖
  390. if (in_array('animal', $match_field)) {
  391. if (!empty($mating['animal'])) {
  392. foreach ($mating['animal'] as $animal) {
  393. $where[] = ['animal', '<>', $animal];
  394. }
  395. }
  396. }
  397. //籍贯
  398. if (in_array('native', $match_field)) {
  399. if ($mating['native'] == '晋江籍') {
  400. $where[] = ['native', 'like', "%晋江%"];
  401. }
  402. if ($mating['native'] == '非晋江籍') {
  403. $where[] = ['native', 'not like', "%晋江%"];
  404. }
  405. }
  406. //学历
  407. if (in_array('education', $match_field)) {
  408. if ($mating['education'] == '本科') {
  409. $where[] = ['education', 'in', ['本科', '硕士', '博士']];
  410. }
  411. if ($mating['education'] == '硕士') {
  412. $where[] = ['education', 'in', ['硕士', '博士']];
  413. }
  414. if ($mating['education'] == '博士') {
  415. $where[] = ['education', '=', '博士'];
  416. }
  417. }
  418. //年收入
  419. if (in_array('income', $match_field)) {
  420. if ($mating['income'] == '5-10万') {
  421. $where[] = ['income', 'in', ['5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上']];
  422. }
  423. if ($mating['income'] == '10-20万') {
  424. $where[] = ['income', 'in', ['10-20万', '20-30万', '30-50万', '50-100万', '100万以上']];
  425. }
  426. if ($mating['income'] == '20-30万') {
  427. $where[] = ['income', 'in', ['20-30万', '30-50万', '50-100万', '100万以上']];
  428. }
  429. if ($mating['income'] == '30-50万') {
  430. $where[] = ['income', 'in', ['30-50万', '50-100万', '100万以上']];
  431. }
  432. if ($mating['income'] == '50-100万') {
  433. $where[] = ['income', 'in', ['50-100万', '100万以上']];
  434. }
  435. if ($mating['income'] == '100万以上') {
  436. $where[] = ['income', '=', '100万以上'];
  437. }
  438. }
  439. //是否有房
  440. if (in_array('have_house', $match_field)) {
  441. if ($mating['have_house'] == 1) {
  442. $where[] = ['have_house', '=', 1];
  443. }
  444. }
  445. //是否有车
  446. if (in_array('have_car', $match_field)) {
  447. if ($mating['have_car'] == 1) {
  448. $where[] = ['have_car', '=', 1];
  449. }
  450. }
  451. //婚后是否愿意与父母同住
  452. if (in_array('with_parent_live', $match_field)) {
  453. if ($mating['with_parent_live'] == 1) {
  454. $where[] = ['with_parent_live', '=', 1];
  455. }
  456. }
  457. //是否吸烟
  458. if (in_array('smoke', $match_field)) {
  459. if ($mating['smoke'] == '接受偶尔') {
  460. $where[] = ['smoke', 'in', ['社交时偶尔吸烟', '不吸烟,但不反感', '不吸,很反感']];
  461. }
  462. if ($mating['smoke'] == '反感') {
  463. $where[] = ['smoke', 'in', ['不吸,很反感', '不吸烟,但不反感']];
  464. }
  465. }
  466. //是否喝酒
  467. if (in_array('drink', $match_field)) {
  468. if ($mating['drink'] == '接受偶尔') {
  469. $where[] = ['drink', 'in', ['不喝酒', '社交需要喝', '兴致时小酌']];
  470. }
  471. if ($mating['drink'] == '反感') {
  472. $where[] = ['drink', 'in', ['不喝酒']];
  473. }
  474. }
  475. //婚后是否愿意与父母同住
  476. if (in_array('want_birth', $match_field)) {
  477. if ($mating['want_birth'] == 1) {
  478. $where[] = ['want_birth', '=', 1];
  479. }
  480. }
  481. return $where;
  482. }
  483. /**
  484. * 排序
  485. */
  486. private function _orderMatchUser($list, $mating)
  487. {
  488. //匹配
  489. foreach ($list as $k => $v) {
  490. $match_arr = [];
  491. $match_count = 0;
  492. //年龄匹配
  493. $age = Fun::getAgeByBirth($v['birthday']);
  494. if (empty($mating['min_age']) && empty($mating['max_age'])) {
  495. $match_arr[] = 'age';
  496. $match_count++;
  497. } elseif (empty($mating['min_age'])) {
  498. if ($age <= $mating['max_age']) {
  499. $match_arr[] = 'age';
  500. $match_count++;
  501. }
  502. } elseif (empty($mating['max_age'])) {
  503. if ($age >= $mating['min_age']) {
  504. $match_arr[] = 'age';
  505. $match_count++;
  506. }
  507. } elseif ($age >= $mating['min_age'] && $age <= $mating['max_age']) {
  508. $match_arr[] = 'age';
  509. $match_count++;
  510. }
  511. //身高
  512. if (empty($mating['min_high']) && empty($mating['max_high'])) {
  513. $match_arr[] = 'high';
  514. $match_count++;
  515. } elseif (empty($mating['min_high'])) {
  516. if ($v['high'] <= $mating['max_high']) {
  517. $match_arr[] = 'high';
  518. $match_count++;
  519. }
  520. } elseif (empty($mating['max_high'])) {
  521. if ($v['high'] >= $mating['min_high']) {
  522. $match_arr[] = 'high';
  523. $match_count++;
  524. }
  525. } elseif ($v['high'] >= $mating['min_high'] && $v['high'] <= $mating['max_high']) {
  526. $match_arr[] = 'high';
  527. $match_count++;
  528. }
  529. //体重
  530. if (empty($mating['min_weight']) && empty($mating['max_weight'])) {
  531. $match_arr[] = 'weight';
  532. $match_count++;
  533. } elseif (empty($mating['min_weight'])) {
  534. if ($v['weight'] <= $mating['max_weight']) {
  535. $match_arr[] = 'weight';
  536. $match_count++;
  537. }
  538. } elseif (empty($mating['max_weight'])) {
  539. if ($v['weight'] >= $mating['min_weight']) {
  540. $match_arr[] = 'weight';
  541. $match_count++;
  542. }
  543. } elseif ($v['weight'] >= $mating['min_weight'] && $v['weight'] <= $mating['max_weight']) {
  544. $match_arr[] = 'weight';
  545. $match_count++;
  546. }
  547. //生肖
  548. if (empty($mating['animal'])) {
  549. $match_arr[] = 'animal';
  550. $match_count++;
  551. } else {
  552. if (!in_array($v['animal'], $mating['animal'])) {
  553. $match_arr[] = 'animal';
  554. $match_count++;
  555. }
  556. }
  557. //籍贯 ['晋江籍','非晋江籍','无要求']
  558. if ($mating['native'] == '无要求') {
  559. $match_arr[] = 'native';
  560. $match_count++;
  561. } elseif ($mating['native'] == '晋江籍') {
  562. if (strpos($v['native'], '晋江') !== false) {
  563. $match_arr[] = 'native';
  564. $match_count++;
  565. }
  566. } else {
  567. if (strpos($v['native'], '晋江') === false) {
  568. $match_arr[] = 'native';
  569. $match_count++;
  570. }
  571. }
  572. //学历 ['本科以下','本科','硕士','博士','无要求']
  573. if ($mating['education'] == '无要求' || $mating['education'] == '本科以下') {
  574. $match_arr[] = 'education';
  575. $match_count++;
  576. } elseif ($mating['education'] == '本科') {
  577. if (in_array($v['education'], ['本科', '硕士', '博士'])) {
  578. $match_arr[] = 'education';
  579. $match_count++;
  580. }
  581. } elseif ($mating['education'] == '硕士') {
  582. if (in_array($v['education'], ['硕士', '博士'])) {
  583. $match_arr[] = 'education';
  584. $match_count++;
  585. }
  586. } elseif ($mating['education'] == '博士') {
  587. if ($v['education'] == '博士') {
  588. $match_arr[] = 'education';
  589. $match_count++;
  590. }
  591. }
  592. //年收入 ['不限', '5万以下', '5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上']
  593. if ($mating['income'] == '不限' || $mating['income'] == '5万以下') {
  594. $match_arr[] = 'income';
  595. $match_count++;
  596. } elseif ($mating['income'] == '5-10万') {
  597. if (in_array($v['income'], ['5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上'])) {
  598. $match_arr[] = 'income';
  599. $match_count++;
  600. }
  601. } elseif ($mating['income'] == '10-20万') {
  602. if (in_array($v['income'], ['10-20万', '20-30万', '30-50万', '50-100万', '100万以上'])) {
  603. $match_arr[] = 'income';
  604. $match_count++;
  605. }
  606. } elseif ($mating['income'] == '20-30万') {
  607. if (in_array($v['income'], ['20-30万', '30-50万', '50-100万', '100万以上'])) {
  608. $match_arr[] = 'income';
  609. $match_count++;
  610. }
  611. } elseif ($mating['income'] == '30-50万') {
  612. if (in_array($v['income'], ['30-50万', '50-100万', '100万以上'])) {
  613. $match_arr[] = 'income';
  614. $match_count++;
  615. }
  616. } elseif ($mating['income'] == '50-100万') {
  617. if (in_array($v['income'], ['50-100万', '100万以上'])) {
  618. $match_arr[] = 'income';
  619. $match_count++;
  620. }
  621. } elseif ($mating['income'] == '100万以上') {
  622. if (in_array($v['income'], ['100万以上'])) {
  623. $match_arr[] = 'income';
  624. $match_count++;
  625. }
  626. }
  627. //是否有房 ['不限', '是', '否']
  628. if ($mating['have_house'] == 1) {
  629. if ($v['have_house'] == 1) {
  630. $match_arr[] = 'have_house';
  631. $match_count++;
  632. }
  633. } else {
  634. $match_arr[] = 'have_house';
  635. $match_count++;
  636. }
  637. //是否有车 ['不限', '是', '否']
  638. if ($mating['have_car'] == 1) {
  639. if ($v['have_car'] == 1) {
  640. $match_arr[] = 'have_car';
  641. $match_count++;
  642. }
  643. } else {
  644. $match_arr[] = 'have_car';
  645. $match_count++;
  646. }
  647. //是否婚后愿意与父母同住 ['不限', '是', '否']
  648. if ($mating['with_parent_live'] == 2) {
  649. if ($v['with_parent_live'] == 2) {
  650. $match_arr[] = 'with_parent_live';
  651. $match_count++;
  652. }
  653. } else {
  654. $match_arr[] = 'with_parent_live';
  655. $match_count++;
  656. }
  657. //是否吸烟 ['不反感', '接受偶尔', '反感']
  658. if ($mating['smoke'] == '不反感' || $mating['smoke'] == '') {
  659. $match_arr[] = 'smoke';
  660. $match_count++;
  661. } elseif ($mating['smoke'] == '接受偶尔') {
  662. if (in_array($v['smoke'], ['社交时偶尔吸烟', '不吸烟,但不反感', '不吸,很反感'])) {
  663. $match_arr[] = 'smoke';
  664. $match_count++;
  665. }
  666. } elseif ($mating['smoke'] == '反感') {
  667. if (in_array($v['smoke'], ['不吸,很反感', '不吸烟,但不反感'])) {
  668. $match_arr[] = 'smoke';
  669. $match_count++;
  670. }
  671. }
  672. //是否喝酒 ['不反感', '接受偶尔', '反感']
  673. if ($mating['drink'] == '不反感' || $mating['drink'] == '') {
  674. $match_arr[] = 'drink';
  675. $match_count++;
  676. } elseif ($mating['drink'] == '接受偶尔') {
  677. if (in_array($v['drink'], ['不喝酒', '社交需要喝', '兴致时小酌'])) {
  678. $match_arr[] = 'drink';
  679. $match_count++;
  680. }
  681. } elseif ($mating['drink'] == '反感') {
  682. if ($v['drink'] == '不喝酒') {
  683. $match_arr[] = 'drink';
  684. $match_count++;
  685. }
  686. }
  687. //是否生育
  688. if ($mating['want_birth'] != 1) {
  689. $match_arr[] = 'want_birth';
  690. $match_count++;
  691. } else {
  692. if ($v['want_birth'] == 1) {
  693. $match_arr[] = 'want_birth';
  694. $match_count++;
  695. }
  696. }
  697. $list[$k]['age'] = $age;
  698. $list[$k]['match_arr'] = $match_arr;
  699. $list[$k]['match_count'] = $match_count;
  700. //姓名
  701. if (mb_strlen($list[$k]['realname'], 'utf-8') > 1) {
  702. $list[$k]['realname'] = $this->_replaceSecondChineseCharWithAsterisk($list[$k]['realname']); // 将第二个字符替换为星号
  703. }
  704. }
  705. //排序
  706. usort($list, function ($a, $b) {
  707. if ($a['match_count'] < $b['match_count']) {
  708. return 1;
  709. } elseif ($a['match_count'] > $b['match_count']) {
  710. return -1;
  711. } else {
  712. return 0;
  713. }
  714. });
  715. //获取前10
  716. if (count($list) > 10) {
  717. return array_slice($list, 0, 10);
  718. } else {
  719. return $list;
  720. }
  721. }
  722. private function _replaceSecondChineseCharWithAsterisk($string)
  723. {
  724. $length = mb_strlen($string, 'utf-8');
  725. $arr = [];
  726. for ($i = 0; $i < $length; $i++) {
  727. $arr[] = mb_substr($string, $i, 1, 'utf-8');
  728. }
  729. $arr[1] = '*';
  730. return implode('', $arr);
  731. }
  732. }