UserController.php 20 KB

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