UserController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. ];
  35. foreach ($data as &$v) {
  36. $v = json_encode($v);
  37. unset($v);
  38. }
  39. return $this->fetch('', $data);
  40. }
  41. public function infoAddPost()
  42. {
  43. $value = $this->request->post();
  44. $value['birthday'] = strtotime($value['birthday']);
  45. $value['matchmaker_id'] = $this->matchmaker['id'];
  46. $user = MatchmakerUserModel::create($value);
  47. MatchmakerUserMatingModel::create(['user_id' => $user['id']]);
  48. $this->success('', '', ['id' => $user['id']]);
  49. }
  50. public function matingAdd()
  51. {
  52. $id = $this->request->param('id');
  53. if (empty($id)) {
  54. $this->error('参数错误');
  55. }
  56. $mating = MatchmakerUserMatingModel::get(['user_id' => $id]);
  57. $this->assign('mating', $mating);
  58. $education = Constant::EDUCATION;
  59. $education[] = '无要求';
  60. $data = [
  61. 'min_age' => Constant::MIN_AGE,
  62. 'max_age' => Constant::MAX_AGE,
  63. 'min_high' => Constant::MIN_HIGH,
  64. 'max_high' => Constant::MAX_HIGH,
  65. 'min_weight' => Constant::MIN_WEIGHT,
  66. 'max_weight' => Constant::MAX_WEIGHT,
  67. 'native' => Constant::NATIVE,
  68. 'education' => $education,
  69. 'income' => Constant::COND_INCOME,
  70. 'smoke' => Constant::SMOKE,
  71. 'drink' => Constant::DRINK,
  72. 'tinyint' => Constant::COND_TINYINT,
  73. ];
  74. foreach ($data as &$v) {
  75. $v = json_encode($v);
  76. unset($v);
  77. }
  78. return $this->fetch('', $data);
  79. }
  80. public function matingAddPost()
  81. {
  82. $param = $this->request->post();
  83. MatchmakerUserMatingModel::update($param);
  84. $this->success('操作成功');
  85. }
  86. public function infoEdit()
  87. {
  88. $id = $this->request->param('id');
  89. if (empty($id)) {
  90. $this->error('参数错误');
  91. }
  92. $user = MatchmakerUserModel::get($id);
  93. if (empty($user)) {
  94. $this->error('该用户不存在或已删除');
  95. }
  96. $user['birthday'] = date('Y-m-d', $user['birthday']);
  97. $this->assign('user', json_encode($user));
  98. $data = [
  99. 'sex' => Constant::SEX_MATCHMAKER,
  100. 'marry' => Constant::MARRY,
  101. 'high' => Constant::HIGH,
  102. 'weight' => Constant::WEIGHT,
  103. 'education' => Constant::EDUCATION,
  104. 'income' => Constant::INCOME,
  105. 'nation' => Constant::NATION,
  106. 'smoke' => Constant::SMOKE,
  107. 'drink' => Constant::DRINK,
  108. 'family' => Constant::FAMILY,
  109. 'tinyint' => Constant::TINYINT,
  110. ];
  111. foreach ($data as &$v) {
  112. $v = json_encode($v);
  113. unset($v);
  114. }
  115. return $this->fetch('', $data);
  116. }
  117. public function infoEditPost()
  118. {
  119. $value = $this->request->post();
  120. $value['birthday'] = strtotime($value['birthday']);
  121. MatchmakerUserModel::update($value);
  122. $this->success();
  123. }
  124. public function matingEdit()
  125. {
  126. $id = $this->request->param('id');
  127. if (empty($id)) {
  128. $this->error('参数错误');
  129. }
  130. $mating = MatchmakerUserMatingModel::get(['user_id' => $id]);
  131. $this->assign('mating', $mating);
  132. $education = Constant::EDUCATION;
  133. $education[] = '无要求';
  134. $data = [
  135. 'min_age' => Constant::MIN_AGE,
  136. 'max_age' => Constant::MAX_AGE,
  137. 'min_high' => Constant::MIN_HIGH,
  138. 'max_high' => Constant::MAX_HIGH,
  139. 'min_weight' => Constant::MIN_WEIGHT,
  140. 'max_weight' => Constant::MAX_WEIGHT,
  141. 'native' => Constant::NATIVE,
  142. 'education' => $education,
  143. 'income' => Constant::COND_INCOME,
  144. 'smoke' => Constant::SMOKE,
  145. 'drink' => Constant::DRINK,
  146. 'tinyint' => Constant::COND_TINYINT,
  147. ];
  148. foreach ($data as &$v) {
  149. $v = json_encode($v);
  150. unset($v);
  151. }
  152. return $this->fetch('', $data);
  153. }
  154. public function matingEditPost()
  155. {
  156. $param = $this->request->post();
  157. MatchmakerUserMatingModel::update($param);
  158. $this->success('操作成功');
  159. }
  160. /**
  161. * 图片上传
  162. */
  163. public function imageUpload()
  164. {
  165. $file = $this->request->post('file');
  166. $ext = pathinfo($this->request->post('name'))['extension'];
  167. if (!in_array($ext, ['jpg', 'jpeg', 'png'])) {
  168. $this->error('文件后缀必须为jpg,jpeg,png');
  169. }
  170. if ($file) {
  171. //创建目录
  172. $upload_dir = WEB_ROOT . 'upload/image';
  173. if (!is_dir($upload_dir)) {
  174. @mkdir($upload_dir);
  175. }
  176. $upload_dir = $upload_dir . '/' . date('Ymd');
  177. if (!is_dir($upload_dir)) {
  178. @mkdir($upload_dir);
  179. }
  180. //保存文件
  181. $file_name = $this->_file_name($ext);
  182. $file_path = $upload_dir . '/' . $file_name;
  183. $is_upload = $this->_base64_image_content($file, $file_path);
  184. if (!$is_upload) {
  185. $this->error('上传失败,请重新上传');
  186. }
  187. $avatar = cmf_get_user_avatar_url('image' . '/' . date('Ymd') . '/' . $file_name);
  188. $this->result([
  189. 'avatar' => $avatar,
  190. ], 1);
  191. } else {
  192. $this->error('请上传文件');
  193. }
  194. }
  195. /**
  196. * 跟进
  197. */
  198. public function follow()
  199. {
  200. $id = $this->request->get('id');
  201. $list = MatchmakerUserFollowModel::where('user_id', $id)->append(['create_time_text'])->order('create_time desc')->select();
  202. $info = MatchmakerUserModel::get($id);
  203. $info['age'] = Fun::getAgeByBirth($info['birthday']);
  204. $info['sex_text'] = Constant::SEX[$info['sex']];
  205. return $this->fetch('', ['list' => $list, 'id' => $id, 'info' => $info]);
  206. }
  207. /**
  208. * 跟进提交
  209. */
  210. public function followPost()
  211. {
  212. $data = $this->request->param();
  213. if (empty($data['user_id'])) {
  214. $this->error('数据异常,请刷新重试');
  215. }
  216. if (empty($data['content'])) {
  217. $this->error('请输入内容');
  218. }
  219. $data['create_time'] = time();
  220. MatchmakerUserFollowModel::create($data);
  221. $this->success('操作成功');
  222. }
  223. /**
  224. * 匹配
  225. */
  226. public function match()
  227. {
  228. $id = $this->request->param('id');
  229. $type = $this->request->param('type', 0);
  230. if (empty($id)) {
  231. $this->error('该用户不存在');
  232. }
  233. $user = MatchmakerUserModel::get($id);
  234. if (empty($user)) {
  235. $this->error('该用户不存在');
  236. }
  237. if ($type == 0) {
  238. //自有库
  239. $list = MatchmakerUserModel::where('matchmaker_id', $this->matchmaker->id)
  240. ->where('id', '<>', $id)
  241. ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
  242. ->append(['sex_text'])
  243. ->select();
  244. } elseif ($type == 1) {
  245. //其他红娘库
  246. $list = MatchmakerUserModel::with('matchmaker')
  247. ->where('matchmaker_id', '<>', $this->matchmaker->id)
  248. ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
  249. ->append(['sex_text'])
  250. ->select();
  251. } else {
  252. //系统库
  253. $list = UserModel::where('user_type', 2)
  254. ->where('user_status', 1)
  255. ->where('sex', '=', $user['sex'] == 1 ? 1 : 2)
  256. ->append(['sex_text'])
  257. ->select();
  258. }
  259. $mating = MatchmakerUserMatingModel::where('user_id', $id)->find();
  260. $res = $this->_orderMatchUser($list->toArray(), $mating);
  261. return $this->fetch('', [
  262. 'list' => json_encode($res),
  263. 'type' => $type,
  264. 'user' => $user,
  265. 'id' => $id,
  266. 'mating' => $mating,
  267. 'tinyint' => json_encode(Constant::TINYINT),
  268. 'cond_tinyint' => json_encode(Constant::COND_TINYINT),
  269. ]);
  270. }
  271. private function _file_name($ext)
  272. {
  273. //生成随机文件名
  274. //定义一个包含大小写字母数字的字符串
  275. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  276. //把字符串分割成数组
  277. $newchars = str_split($chars);
  278. //打乱数组
  279. shuffle($newchars);
  280. //从数组中随机取出15个字符
  281. $chars_key = array_rand($newchars, 15);
  282. //把取出的字符重新组成字符串
  283. $fnstr = '';
  284. for ($i = 0; $i < 15; $i++) {
  285. $fnstr .= $newchars[$chars_key[$i]];
  286. }
  287. //输出文件名并做MD5加密
  288. return md5($fnstr . microtime(true) * 1000) . '.' . $ext;
  289. }
  290. private function _base64_image_content($base64_image_content, $file_path)
  291. {
  292. //匹配出图片的格式
  293. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  294. if (file_put_contents($file_path, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  295. return true;
  296. } else {
  297. return false;
  298. }
  299. } else {
  300. return false;
  301. }
  302. }
  303. /**
  304. * 排序
  305. */
  306. private function _orderMatchUser($list, $mating)
  307. {
  308. //匹配
  309. foreach ($list as $k => $v) {
  310. $match_arr = [];
  311. $match_count = 0;
  312. //年龄匹配
  313. $age = Fun::getAgeByBirth($v['birthday']);
  314. if (empty($mating['min_age']) && empty($mating['max_age'])) {
  315. $match_arr[] = 'age';
  316. $match_count++;
  317. } elseif (empty($mating['min_age'])) {
  318. if ($age <= $mating['max_age']) {
  319. $match_arr[] = 'age';
  320. $match_count++;
  321. }
  322. } elseif (empty($mating['max_age'])) {
  323. if ($age >= $mating['min_age']) {
  324. $match_arr[] = 'age';
  325. $match_count++;
  326. }
  327. } elseif ($age >= $mating['min_age'] && $age <= $mating['max_age']) {
  328. $match_arr[] = 'age';
  329. $match_count++;
  330. }
  331. //身高
  332. if (empty($mating['min_high']) && empty($mating['max_high'])) {
  333. $match_arr[] = 'high';
  334. $match_count++;
  335. } elseif (empty($mating['min_high'])) {
  336. if ($v['high'] <= $mating['max_high']) {
  337. $match_arr[] = 'high';
  338. $match_count++;
  339. }
  340. } elseif (empty($mating['max_high'])) {
  341. if ($v['high'] >= $mating['min_high']) {
  342. $match_arr[] = 'high';
  343. $match_count++;
  344. }
  345. } elseif ($v['high'] >= $mating['min_high'] && $v['high'] <= $mating['max_high']) {
  346. $match_arr[] = 'high';
  347. $match_count++;
  348. }
  349. //体重
  350. if (empty($mating['min_weight']) && empty($mating['max_weight'])) {
  351. $match_arr[] = 'weight';
  352. $match_count++;
  353. } elseif (empty($mating['min_weight'])) {
  354. if ($v['weight'] <= $mating['max_weight']) {
  355. $match_arr[] = 'weight';
  356. $match_count++;
  357. }
  358. } elseif (empty($mating['max_weight'])) {
  359. if ($v['weight'] >= $mating['min_weight']) {
  360. $match_arr[] = 'weight';
  361. $match_count++;
  362. }
  363. } elseif ($v['weight'] >= $mating['min_weight'] && $v['weight'] <= $mating['max_weight']) {
  364. $match_arr[] = 'weight';
  365. $match_count++;
  366. }
  367. //籍贯 ['晋江籍','非晋江籍','无要求']
  368. if ($mating['native'] == '无要求') {
  369. $match_arr[] = 'native';
  370. $match_count++;
  371. } elseif ($mating['native'] == '晋江籍') {
  372. if (strpos($v['native'], '晋江') !== false) {
  373. $match_arr[] = 'native';
  374. $match_count++;
  375. }
  376. } else {
  377. if (strpos($v['native'], '晋江') === false) {
  378. $match_arr[] = 'native';
  379. $match_count++;
  380. }
  381. }
  382. //学历 ['本科以下','本科','硕士','博士','无要求']
  383. if ($mating['education'] == '无要求' || $mating['education'] == '本科以下') {
  384. $match_arr[] = 'education';
  385. $match_count++;
  386. } elseif ($mating['education'] == '本科') {
  387. if (in_array($v['education'], ['本科', '硕士', '博士'])) {
  388. $match_arr[] = 'education';
  389. $match_count++;
  390. }
  391. } elseif ($mating['education'] == '硕士') {
  392. if (in_array($v['education'], ['硕士', '博士'])) {
  393. $match_arr[] = 'education';
  394. $match_count++;
  395. }
  396. } elseif ($mating['education'] == '博士') {
  397. if ($v['education'] == '博士') {
  398. $match_arr[] = 'education';
  399. $match_count++;
  400. }
  401. }
  402. //年收入 ['不限', '5万以下', '5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上']
  403. if ($mating['income'] == '不限' || $mating['income'] == '5万以下') {
  404. $match_arr[] = 'income';
  405. $match_count++;
  406. } elseif ($mating['income'] == '5-10万') {
  407. if (in_array($v['income'], ['5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上'])) {
  408. $match_arr[] = 'income';
  409. $match_count++;
  410. }
  411. } elseif ($mating['income'] == '10-20万') {
  412. if (in_array($v['income'], ['10-20万', '20-30万', '30-50万', '50-100万', '100万以上'])) {
  413. $match_arr[] = 'income';
  414. $match_count++;
  415. }
  416. } elseif ($mating['income'] == '20-30万') {
  417. if (in_array($v['income'], ['20-30万', '30-50万', '50-100万', '100万以上'])) {
  418. $match_arr[] = 'income';
  419. $match_count++;
  420. }
  421. } elseif ($mating['income'] == '30-50万') {
  422. if (in_array($v['income'], ['30-50万', '50-100万', '100万以上'])) {
  423. $match_arr[] = 'income';
  424. $match_count++;
  425. }
  426. } elseif ($mating['income'] == '50-100万') {
  427. if (in_array($v['income'], ['50-100万', '100万以上'])) {
  428. $match_arr[] = 'income';
  429. $match_count++;
  430. }
  431. } elseif ($mating['income'] == '100万以上') {
  432. if (in_array($v['income'], ['100万以上'])) {
  433. $match_arr[] = 'income';
  434. $match_count++;
  435. }
  436. }
  437. //是否有房 ['不限', '是', '否']
  438. if ($mating['have_house'] == 1) {
  439. if ($v['have_house'] == 1) {
  440. $match_arr[] = 'have_house';
  441. $match_count++;
  442. }
  443. } else {
  444. $match_arr[] = 'have_house';
  445. $match_count++;
  446. }
  447. //是否有车 ['不限', '是', '否']
  448. if ($mating['have_car'] == 1) {
  449. if ($v['have_car'] == 1) {
  450. $match_arr[] = 'have_car';
  451. $match_count++;
  452. }
  453. } else {
  454. $match_arr[] = 'have_car';
  455. $match_count++;
  456. }
  457. //是否婚后愿意与父母同住 ['不限', '是', '否']
  458. if ($mating['with_parent_live'] == 2) {
  459. if ($v['with_parent_live'] == 2) {
  460. $match_arr[] = 'with_parent_live';
  461. $match_count++;
  462. }
  463. } else {
  464. $match_arr[] = 'with_parent_live';
  465. $match_count++;
  466. }
  467. //是否吸烟 ['不吸,很反感', '不吸烟,但不反感', '社交时偶尔吸烟','烟不离手']
  468. if ($mating['smoke'] == '烟不离手' || $mating['smoke'] == '' || $mating['smoke'] == '不吸烟,但不反感') {
  469. $match_arr[] = 'smoke';
  470. $match_count++;
  471. } elseif ($mating['smoke'] == '社交时偶尔吸烟') {
  472. if (in_array($v['smoke'], ['社交时偶尔吸烟', '不吸烟,但不反感', '不吸,很反感'])) {
  473. $match_arr[] = 'smoke';
  474. $match_count++;
  475. }
  476. } elseif ($mating['smoke'] == '不吸,很反感') {
  477. if (in_array($v['smoke'], ['不吸,很反感', '不吸烟,但不反感'])) {
  478. $match_arr[] = 'smoke';
  479. $match_count++;
  480. }
  481. }
  482. //是否喝酒 ['不喝酒', '社交需要喝', '兴致时小酌', '酒不离口']
  483. if ($mating['drink'] == '酒不离口' || $mating['drink'] == '') {
  484. $match_arr[] = 'drink';
  485. $match_count++;
  486. } elseif ($mating['drink'] == '兴致时小酌' || $mating['drink'] == '社交需要喝') {
  487. if ($v['drink'] == '兴致时小酌' || $v['drink'] == '社交需要喝') {
  488. $match_arr[] = 'drink';
  489. $match_count++;
  490. }
  491. } elseif ($mating['drink'] == '不喝酒') {
  492. if ($v['drink'] == '不喝酒') {
  493. $match_arr[] = 'drink';
  494. $match_count++;
  495. }
  496. }
  497. $list[$k]['age'] = $age;
  498. $list[$k]['match_arr'] = $match_arr;
  499. $list[$k]['match_count'] = $match_count;
  500. }
  501. //排序
  502. usort($list, function ($a, $b) {
  503. if ($a['match_count'] < $b['match_count']) {
  504. return 1;
  505. } elseif ($a['match_count'] > $b['match_count']) {
  506. return -1;
  507. } else {
  508. return 0;
  509. }
  510. });
  511. //获取前10
  512. if (count($list) > 10) {
  513. return array_slice($list, 0, 10);
  514. } else {
  515. return $list;
  516. }
  517. }
  518. }