UserController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. if (empty($id)) {
  268. $this->error('该用户不存在');
  269. }
  270. $user = MatchmakerUserModel::get($id);
  271. if (empty($user)) {
  272. $this->error('该用户不存在');
  273. }
  274. if ($type == 0) {
  275. //自有库
  276. $list = MatchmakerUserModel::where('matchmaker_id', $this->matchmaker->id)
  277. ->where('id', '<>', $id)
  278. ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
  279. ->append(['sex_text'])
  280. ->select();
  281. } elseif ($type == 1) {
  282. //其他红娘库
  283. $list = MatchmakerUserModel::with('matchmaker')
  284. ->where('matchmaker_id', '<>', $this->matchmaker->id)
  285. ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
  286. ->append(['sex_text'])
  287. ->select();
  288. } else {
  289. //系统库
  290. $list = UserModel::where('user_type', 2)
  291. ->where('user_status', 1)
  292. ->where('is_complete', 1)
  293. ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
  294. ->append(['sex_text'])
  295. ->select();
  296. }
  297. $mating = MatchmakerUserMatingModel::where('user_id', $id)->find();
  298. $res = $this->_orderMatchUser($list->toArray(), $mating);
  299. return $this->fetch('', [
  300. 'list' => json_encode($res),
  301. 'type' => $type,
  302. 'user' => $user,
  303. 'id' => $id,
  304. 'mating' => $mating,
  305. 'tinyint' => json_encode(Constant::TINYINT),
  306. 'cond_tinyint' => json_encode(Constant::COND_TINYINT),
  307. ]);
  308. }
  309. private function _file_name($ext)
  310. {
  311. //生成随机文件名
  312. //定义一个包含大小写字母数字的字符串
  313. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  314. //把字符串分割成数组
  315. $newchars = str_split($chars);
  316. //打乱数组
  317. shuffle($newchars);
  318. //从数组中随机取出15个字符
  319. $chars_key = array_rand($newchars, 15);
  320. //把取出的字符重新组成字符串
  321. $fnstr = '';
  322. for ($i = 0; $i < 15; $i++) {
  323. $fnstr .= $newchars[$chars_key[$i]];
  324. }
  325. //输出文件名并做MD5加密
  326. return md5($fnstr . microtime(true) * 1000) . '.' . $ext;
  327. }
  328. private function _base64_image_content($base64_image_content, $file_path)
  329. {
  330. //匹配出图片的格式
  331. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  332. if (file_put_contents($file_path, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  333. return true;
  334. } else {
  335. return false;
  336. }
  337. } else {
  338. return false;
  339. }
  340. }
  341. /**
  342. * 排序
  343. */
  344. private function _orderMatchUser($list, $mating)
  345. {
  346. //匹配
  347. foreach ($list as $k => $v) {
  348. $match_arr = [];
  349. $match_count = 0;
  350. //年龄匹配
  351. $age = Fun::getAgeByBirth($v['birthday']);
  352. if (empty($mating['min_age']) && empty($mating['max_age'])) {
  353. $match_arr[] = 'age';
  354. $match_count++;
  355. } elseif (empty($mating['min_age'])) {
  356. if ($age <= $mating['max_age']) {
  357. $match_arr[] = 'age';
  358. $match_count++;
  359. }
  360. } elseif (empty($mating['max_age'])) {
  361. if ($age >= $mating['min_age']) {
  362. $match_arr[] = 'age';
  363. $match_count++;
  364. }
  365. } elseif ($age >= $mating['min_age'] && $age <= $mating['max_age']) {
  366. $match_arr[] = 'age';
  367. $match_count++;
  368. }
  369. //身高
  370. if (empty($mating['min_high']) && empty($mating['max_high'])) {
  371. $match_arr[] = 'high';
  372. $match_count++;
  373. } elseif (empty($mating['min_high'])) {
  374. if ($v['high'] <= $mating['max_high']) {
  375. $match_arr[] = 'high';
  376. $match_count++;
  377. }
  378. } elseif (empty($mating['max_high'])) {
  379. if ($v['high'] >= $mating['min_high']) {
  380. $match_arr[] = 'high';
  381. $match_count++;
  382. }
  383. } elseif ($v['high'] >= $mating['min_high'] && $v['high'] <= $mating['max_high']) {
  384. $match_arr[] = 'high';
  385. $match_count++;
  386. }
  387. //体重
  388. if (empty($mating['min_weight']) && empty($mating['max_weight'])) {
  389. $match_arr[] = 'weight';
  390. $match_count++;
  391. } elseif (empty($mating['min_weight'])) {
  392. if ($v['weight'] <= $mating['max_weight']) {
  393. $match_arr[] = 'weight';
  394. $match_count++;
  395. }
  396. } elseif (empty($mating['max_weight'])) {
  397. if ($v['weight'] >= $mating['min_weight']) {
  398. $match_arr[] = 'weight';
  399. $match_count++;
  400. }
  401. } elseif ($v['weight'] >= $mating['min_weight'] && $v['weight'] <= $mating['max_weight']) {
  402. $match_arr[] = 'weight';
  403. $match_count++;
  404. }
  405. //籍贯 ['晋江籍','非晋江籍','无要求']
  406. if ($mating['native'] == '无要求') {
  407. $match_arr[] = 'native';
  408. $match_count++;
  409. } elseif ($mating['native'] == '晋江籍') {
  410. if (strpos($v['native'], '晋江') !== false) {
  411. $match_arr[] = 'native';
  412. $match_count++;
  413. }
  414. } else {
  415. if (strpos($v['native'], '晋江') === false) {
  416. $match_arr[] = 'native';
  417. $match_count++;
  418. }
  419. }
  420. //学历 ['本科以下','本科','硕士','博士','无要求']
  421. if ($mating['education'] == '无要求' || $mating['education'] == '本科以下') {
  422. $match_arr[] = 'education';
  423. $match_count++;
  424. } elseif ($mating['education'] == '本科') {
  425. if (in_array($v['education'], ['本科', '硕士', '博士'])) {
  426. $match_arr[] = 'education';
  427. $match_count++;
  428. }
  429. } elseif ($mating['education'] == '硕士') {
  430. if (in_array($v['education'], ['硕士', '博士'])) {
  431. $match_arr[] = 'education';
  432. $match_count++;
  433. }
  434. } elseif ($mating['education'] == '博士') {
  435. if ($v['education'] == '博士') {
  436. $match_arr[] = 'education';
  437. $match_count++;
  438. }
  439. }
  440. //年收入 ['不限', '5万以下', '5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上']
  441. if ($mating['income'] == '不限' || $mating['income'] == '5万以下') {
  442. $match_arr[] = 'income';
  443. $match_count++;
  444. } elseif ($mating['income'] == '5-10万') {
  445. if (in_array($v['income'], ['5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上'])) {
  446. $match_arr[] = 'income';
  447. $match_count++;
  448. }
  449. } elseif ($mating['income'] == '10-20万') {
  450. if (in_array($v['income'], ['10-20万', '20-30万', '30-50万', '50-100万', '100万以上'])) {
  451. $match_arr[] = 'income';
  452. $match_count++;
  453. }
  454. } elseif ($mating['income'] == '20-30万') {
  455. if (in_array($v['income'], ['20-30万', '30-50万', '50-100万', '100万以上'])) {
  456. $match_arr[] = 'income';
  457. $match_count++;
  458. }
  459. } elseif ($mating['income'] == '30-50万') {
  460. if (in_array($v['income'], ['30-50万', '50-100万', '100万以上'])) {
  461. $match_arr[] = 'income';
  462. $match_count++;
  463. }
  464. } elseif ($mating['income'] == '50-100万') {
  465. if (in_array($v['income'], ['50-100万', '100万以上'])) {
  466. $match_arr[] = 'income';
  467. $match_count++;
  468. }
  469. } elseif ($mating['income'] == '100万以上') {
  470. if (in_array($v['income'], ['100万以上'])) {
  471. $match_arr[] = 'income';
  472. $match_count++;
  473. }
  474. }
  475. //是否有房 ['不限', '是', '否']
  476. if ($mating['have_house'] == 1) {
  477. if ($v['have_house'] == 1) {
  478. $match_arr[] = 'have_house';
  479. $match_count++;
  480. }
  481. } else {
  482. $match_arr[] = 'have_house';
  483. $match_count++;
  484. }
  485. //是否有车 ['不限', '是', '否']
  486. if ($mating['have_car'] == 1) {
  487. if ($v['have_car'] == 1) {
  488. $match_arr[] = 'have_car';
  489. $match_count++;
  490. }
  491. } else {
  492. $match_arr[] = 'have_car';
  493. $match_count++;
  494. }
  495. //是否婚后愿意与父母同住 ['不限', '是', '否']
  496. if ($mating['with_parent_live'] == 2) {
  497. if ($v['with_parent_live'] == 2) {
  498. $match_arr[] = 'with_parent_live';
  499. $match_count++;
  500. }
  501. } else {
  502. $match_arr[] = 'with_parent_live';
  503. $match_count++;
  504. }
  505. //是否吸烟 ['不吸,很反感', '不吸烟,但不反感', '社交时偶尔吸烟','烟不离手']
  506. if ($mating['smoke'] == '不反感' || $mating['smoke'] == '') {
  507. $match_arr[] = 'smoke';
  508. $match_count++;
  509. } elseif ($mating['smoke'] == '接受偶尔') {
  510. if (in_array($v['smoke'], ['社交时偶尔吸烟', '不吸烟,但不反感', '不吸,很反感'])) {
  511. $match_arr[] = 'smoke';
  512. $match_count++;
  513. }
  514. } elseif ($mating['smoke'] == '反感') {
  515. if (in_array($v['smoke'], ['不吸,很反感', '不吸烟,但不反感'])) {
  516. $match_arr[] = 'smoke';
  517. $match_count++;
  518. }
  519. }
  520. //是否喝酒 ['不喝酒', '社交需要喝', '兴致时小酌', '酒不离口']
  521. if ($mating['drink'] == '不反感' || $mating['drink'] == '') {
  522. $match_arr[] = 'drink';
  523. $match_count++;
  524. } elseif ($mating['drink'] == '接受偶尔') {
  525. if (in_array($v['drink'], ['不喝酒', '社交需要喝', '兴致时小酌'])) {
  526. $match_arr[] = 'drink';
  527. $match_count++;
  528. }
  529. } elseif ($mating['drink'] == '反感') {
  530. if ($v['drink'] == '不喝酒') {
  531. $match_arr[] = 'drink';
  532. $match_count++;
  533. }
  534. }
  535. $list[$k]['age'] = $age;
  536. $list[$k]['match_arr'] = $match_arr;
  537. $list[$k]['match_count'] = $match_count;
  538. //姓名
  539. if (mb_strlen($list[$k]['realname'], 'utf-8') > 1) {
  540. $list[$k]['realname'] = $this->_replaceSecondChineseCharWithAsterisk($list[$k]['realname']); // 将第二个字符替换为星号
  541. }
  542. }
  543. //排序
  544. usort($list, function ($a, $b) {
  545. if ($a['match_count'] < $b['match_count']) {
  546. return 1;
  547. } elseif ($a['match_count'] > $b['match_count']) {
  548. return -1;
  549. } else {
  550. return 0;
  551. }
  552. });
  553. //获取前10
  554. if (count($list) > 10) {
  555. return array_slice($list, 0, 10);
  556. } else {
  557. return $list;
  558. }
  559. }
  560. private function _replaceSecondChineseCharWithAsterisk($string)
  561. {
  562. $length = mb_strlen($string, 'utf-8');
  563. $arr = [];
  564. for ($i = 0; $i < $length; $i++) {
  565. $arr[] = mb_substr($string, $i, 1, 'utf-8');
  566. }
  567. $arr[1] = '*';
  568. return implode('', $arr);
  569. }
  570. }