// +---------------------------------------------------------------------- namespace app\matchmaker\controller; use app\common\Constant; use app\common\Fun; use app\love\model\UserModel; use app\matchmaker\model\MatchmakerUserFollowModel; use app\matchmaker\model\MatchmakerUserMatingModel; use app\matchmaker\model\MatchmakerUserModel; class UserController extends MatchmakerBaseController { public function infoAdd() { $data = [ 'sex' => Constant::SEX_MATCHMAKER, 'marry' => Constant::MARRY, 'high' => Constant::HIGH, 'weight' => Constant::WEIGHT, 'education' => Constant::EDUCATION, 'income' => Constant::INCOME, 'nation' => Constant::NATION, 'smoke' => Constant::SMOKE, 'drink' => Constant::DRINK, 'family' => Constant::FAMILY, 'tinyint' => Constant::TINYINT, 'animal' => Constant::ANIMAL, ]; foreach ($data as &$v) { $v = json_encode($v); unset($v); } return $this->fetch('', $data); } public function infoAddPost() { $value = $this->request->post(); $value['birthday'] = strtotime($value['birthday']); $value['matchmaker_id'] = $this->matchmaker['id']; $user = MatchmakerUserModel::create($value); MatchmakerUserMatingModel::create(['user_id' => $user['id'], 'animal' => []]); $this->success('', '', ['id' => $user['id']]); } public function matingAdd() { $id = $this->request->param('id'); if (empty($id)) { $this->error('参数错误'); } $mating = MatchmakerUserMatingModel::get(['user_id' => $id]); if (empty($mating['animal'])) { $mating['animal'] = []; } $this->assign('mating', $mating); $education = Constant::EDUCATION; $education[] = '无要求'; $data = [ 'min_age' => Constant::MIN_AGE, 'max_age' => Constant::MAX_AGE, 'min_high' => Constant::MIN_HIGH, 'max_high' => Constant::MAX_HIGH, 'min_weight' => Constant::MIN_WEIGHT, 'max_weight' => Constant::MAX_WEIGHT, 'native' => Constant::NATIVE, 'education' => $education, 'income' => Constant::COND_INCOME, 'smoke' => Constant::MATTING_SMOKING, 'drink' => Constant::MATTING_DRINK, 'tinyint' => Constant::COND_TINYINT, 'animal' => Constant::ANIMAL, ]; foreach ($data as &$v) { $v = json_encode($v); unset($v); } return $this->fetch('', $data); } public function matingAddPost() { $param = $this->request->post(); if (empty($param['animal'])) { $param['animal'] = []; } MatchmakerUserMatingModel::update($param); $this->success('操作成功'); } public function infoEdit() { $id = $this->request->param('id'); if (empty($id)) { $this->error('参数错误'); } $user = MatchmakerUserModel::get($id); if (empty($user)) { $this->error('该用户不存在或已删除'); } $user['birthday'] = date('Y-m-d', $user['birthday']); $this->assign('user', json_encode($user)); $data = [ 'sex' => Constant::SEX_MATCHMAKER, 'marry' => Constant::MARRY, 'high' => Constant::HIGH, 'weight' => Constant::WEIGHT, 'education' => Constant::EDUCATION, 'income' => Constant::INCOME, 'nation' => Constant::NATION, 'smoke' => Constant::SMOKE, 'drink' => Constant::DRINK, 'family' => Constant::FAMILY, 'tinyint' => Constant::TINYINT, 'animal' => Constant::ANIMAL, ]; foreach ($data as &$v) { $v = json_encode($v); unset($v); } return $this->fetch('', $data); } public function infoEditPost() { $value = $this->request->post(); $value['birthday'] = strtotime($value['birthday']); MatchmakerUserModel::update($value); $this->success(); } public function matingEdit() { $id = $this->request->param('id'); if (empty($id)) { $this->error('参数错误'); } $mating = MatchmakerUserMatingModel::get(['user_id' => $id]); if (empty($mating['animal'])) { $mating['animal'] = []; } $this->assign('mating', $mating); $education = Constant::EDUCATION; $education[] = '无要求'; $data = [ 'min_age' => Constant::MIN_AGE, 'max_age' => Constant::MAX_AGE, 'min_high' => Constant::MIN_HIGH, 'max_high' => Constant::MAX_HIGH, 'min_weight' => Constant::MIN_WEIGHT, 'max_weight' => Constant::MAX_WEIGHT, 'native' => Constant::NATIVE, 'education' => $education, 'income' => Constant::COND_INCOME, 'smoke' => Constant::MATTING_SMOKING, 'drink' => Constant::MATTING_DRINK, 'tinyint' => Constant::COND_TINYINT, 'animal' => Constant::ANIMAL, ]; foreach ($data as &$v) { $v = json_encode($v); unset($v); } return $this->fetch('', $data); } public function matingEditPost() { $param = $this->request->post(); if (empty($param['animal'])) { $param['animal'] = []; } MatchmakerUserMatingModel::update($param); $this->success('操作成功'); } /** * 图片上传 */ public function imageUpload() { $file = $this->request->post('file'); $ext = pathinfo($this->request->post('name'))['extension']; if (!in_array($ext, ['jpg', 'jpeg', 'png'])) { $this->error('文件后缀必须为jpg,jpeg,png'); } if ($file) { //创建目录 $upload_dir = WEB_ROOT . 'upload/image'; if (!is_dir($upload_dir)) { @mkdir($upload_dir); } $upload_dir = $upload_dir . '/' . date('Ymd'); if (!is_dir($upload_dir)) { @mkdir($upload_dir); } //保存文件 $file_name = $this->_file_name($ext); $file_path = $upload_dir . '/' . $file_name; $is_upload = $this->_base64_image_content($file, $file_path); if (!$is_upload) { $this->error('上传失败,请重新上传'); } $avatar = cmf_get_user_avatar_url('image' . '/' . date('Ymd') . '/' . $file_name); $this->result([ 'avatar' => $avatar, ], 1); } else { $this->error('请上传文件'); } } /** * 跟进 */ public function follow() { $id = $this->request->get('id'); $list = MatchmakerUserFollowModel::where('user_id', $id)->append(['create_time_text'])->order('create_time desc')->select(); $info = MatchmakerUserModel::get($id); $info['age'] = Fun::getAgeByBirth($info['birthday']); $info['sex_text'] = Constant::SEX[$info['sex']]; return $this->fetch('', ['list' => $list, 'id' => $id, 'info' => $info]); } /** * 跟进提交 */ public function followPost() { $data = $this->request->param(); if (empty($data['user_id'])) { $this->error('数据异常,请刷新重试'); } if (empty($data['content'])) { $this->error('请输入内容'); } $data['create_time'] = time(); MatchmakerUserFollowModel::create($data); $this->success('操作成功'); } public function deleteUser() { $id = $this->request->post('id', 0); if (empty($id)) { $this->error('数据异常,请刷新重试'); } MatchmakerUserModel::destroy($id); MatchmakerUserMatingModel::destroy(['user_id' => $id]); MatchmakerUserFollowModel::destroy(['user_id' => $id]); $this->success('操作成功'); } public function statusUser() { $id = $this->request->post('id', 0); $status = $this->request->post('status', 0); if (empty($id) || empty($status)) { $this->error('数据异常,请刷新重试'); } MatchmakerUserModel::update(['status' => $status], ['id' => $id]); $this->success('操作成功'); } /** * 匹配 */ public function match() { $id = $this->request->param('id'); $type = $this->request->param('type', 0); $match_field = $this->request->param('match_field', ''); $mating = MatchmakerUserMatingModel::where('user_id', $id)->find(); $where = $this->_matchWhere($match_field, $mating); if (empty($id)) { $this->error('该用户不存在'); } $user = MatchmakerUserModel::get($id); if (empty($user)) { $this->error('该用户不存在'); } if ($type == 0) { //自有库 $list = MatchmakerUserModel::where('matchmaker_id', $this->matchmaker->id) ->where($where) ->where('id', '<>', $id) ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2) ->where('status',2) ->append(['sex_text']) ->select(); } elseif ($type == 1) { //其他红娘库 $list = MatchmakerUserModel::with('matchmaker') ->where($where) ->where('matchmaker_id', '<>', $this->matchmaker->id) ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2) ->where('status',2) ->append(['sex_text']) ->select(); } else { //系统库 $list = UserModel::where('user_type', 2) ->where($where) ->where('user_status', 1) ->where('want_status',2) ->where('is_complete', 1) ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2) ->append(['sex_text']) ->select(); } $res = $this->_orderMatchUser($list->toArray(), $mating); return $this->fetch('', [ 'list' => json_encode($res), 'type' => $type, 'user' => $user, 'id' => $id, 'mating' => $mating, 'match_field' => $match_field, 'tinyint' => json_encode(Constant::TINYINT), 'cond_tinyint' => json_encode(Constant::COND_TINYINT), ]); } private function _file_name($ext) { //生成随机文件名 //定义一个包含大小写字母数字的字符串 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; //把字符串分割成数组 $newchars = str_split($chars); //打乱数组 shuffle($newchars); //从数组中随机取出15个字符 $chars_key = array_rand($newchars, 15); //把取出的字符重新组成字符串 $fnstr = ''; for ($i = 0; $i < 15; $i++) { $fnstr .= $newchars[$chars_key[$i]]; } //输出文件名并做MD5加密 return md5($fnstr . microtime(true) * 1000) . '.' . $ext; } private function _base64_image_content($base64_image_content, $file_path) { //匹配出图片的格式 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) { if (file_put_contents($file_path, base64_decode(str_replace($result[1], '', $base64_image_content)))) { return true; } else { return false; } } else { return false; } } private function _matchWhere($field, $mating) { $where = []; $match_field = []; if (!empty($field)) { $match_field = explode(',', $field); } //年龄 if (in_array('age', $match_field)) { $year = date('Y'); if (!empty($mating['min_age'])) { $min_year = $year - $mating['min_age']; $min_year_time = strtotime(date($min_year . '-m-d H:i:s')); $where[] = ['birthday', '<', $min_year_time]; } if (!empty($mating['max_age'])) { $max_year = $year - $mating['max_age']; $max_year_time = strtotime(date($max_year . '-m-d H:i:s')); $where[] = ['birthday', '>', $max_year_time]; } } //身高 if (in_array('high', $match_field)) { if (!empty($mating['min_high'])) { $where[] = ['high', '>', $mating['min_high']]; } if (!empty($mating['max_high'])) { $where[] = ['high', '<', $mating['max_high']]; } } //体重 if (in_array('weight', $match_field)) { if (!empty($mating['min_weight'])) { $where[] = ['weight', '>', $mating['min_weight']]; } if (!empty($mating['max_weight'])) { $where[] = ['weight', '<', $mating['max_weight']]; } } //生肖 if (in_array('animal', $match_field)) { if (!empty($mating['animal'])) { foreach ($mating['animal'] as $animal) { $where[] = ['animal', '<>', $animal]; } } } //籍贯 if (in_array('native', $match_field)) { if ($mating['native'] == '晋江籍') { $where[] = ['native', 'like', "%晋江%"]; } if ($mating['native'] == '非晋江籍') { $where[] = ['native', 'not like', "%晋江%"]; } } //学历 if (in_array('education', $match_field)) { if ($mating['education'] == '本科') { $where[] = ['education', 'in', ['本科', '硕士', '博士']]; } if ($mating['education'] == '硕士') { $where[] = ['education', 'in', ['硕士', '博士']]; } if ($mating['education'] == '博士') { $where[] = ['education', '=', '博士']; } } //年收入 if (in_array('income', $match_field)) { if ($mating['income'] == '5-10万') { $where[] = ['income', 'in', ['5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上']]; } if ($mating['income'] == '10-20万') { $where[] = ['income', 'in', ['10-20万', '20-30万', '30-50万', '50-100万', '100万以上']]; } if ($mating['income'] == '20-30万') { $where[] = ['income', 'in', ['20-30万', '30-50万', '50-100万', '100万以上']]; } if ($mating['income'] == '30-50万') { $where[] = ['income', 'in', ['30-50万', '50-100万', '100万以上']]; } if ($mating['income'] == '50-100万') { $where[] = ['income', 'in', ['50-100万', '100万以上']]; } if ($mating['income'] == '100万以上') { $where[] = ['income', '=', '100万以上']; } } //是否有房 if (in_array('have_house', $match_field)) { if ($mating['have_house'] == 1) { $where[] = ['have_house', '=', 1]; } } //是否有车 if (in_array('have_car', $match_field)) { if ($mating['have_car'] == 1) { $where[] = ['have_car', '=', 1]; } } //婚后是否愿意与父母同住 if (in_array('with_parent_live', $match_field)) { if ($mating['with_parent_live'] == 1) { $where[] = ['with_parent_live', '=', 1]; } } //是否吸烟 if (in_array('smoke', $match_field)) { if ($mating['smoke'] == '接受偶尔') { $where[] = ['smoke', 'in', ['社交时偶尔吸烟', '不吸烟,但不反感', '不吸,很反感']]; } if ($mating['smoke'] == '反感') { $where[] = ['smoke', 'in', ['不吸,很反感', '不吸烟,但不反感']]; } } //是否喝酒 if (in_array('drink', $match_field)) { if ($mating['drink'] == '接受偶尔') { $where[] = ['drink', 'in', ['不喝酒', '社交需要喝', '兴致时小酌']]; } if ($mating['drink'] == '反感') { $where[] = ['drink', 'in', ['不喝酒']]; } } //婚后是否愿意与父母同住 if (in_array('want_birth', $match_field)) { if ($mating['want_birth'] == 1) { $where[] = ['want_birth', '=', 1]; } } return $where; } /** * 排序 */ private function _orderMatchUser($list, $mating) { //匹配 foreach ($list as $k => $v) { $match_arr = []; $match_count = 0; //年龄匹配 $age = Fun::getAgeByBirth($v['birthday']); if (empty($mating['min_age']) && empty($mating['max_age'])) { $match_arr[] = 'age'; $match_count++; } elseif (empty($mating['min_age'])) { if ($age <= $mating['max_age']) { $match_arr[] = 'age'; $match_count++; } } elseif (empty($mating['max_age'])) { if ($age >= $mating['min_age']) { $match_arr[] = 'age'; $match_count++; } } elseif ($age >= $mating['min_age'] && $age <= $mating['max_age']) { $match_arr[] = 'age'; $match_count++; } //身高 if (empty($mating['min_high']) && empty($mating['max_high'])) { $match_arr[] = 'high'; $match_count++; } elseif (empty($mating['min_high'])) { if ($v['high'] <= $mating['max_high']) { $match_arr[] = 'high'; $match_count++; } } elseif (empty($mating['max_high'])) { if ($v['high'] >= $mating['min_high']) { $match_arr[] = 'high'; $match_count++; } } elseif ($v['high'] >= $mating['min_high'] && $v['high'] <= $mating['max_high']) { $match_arr[] = 'high'; $match_count++; } //体重 if (empty($mating['min_weight']) && empty($mating['max_weight'])) { $match_arr[] = 'weight'; $match_count++; } elseif (empty($mating['min_weight'])) { if ($v['weight'] <= $mating['max_weight']) { $match_arr[] = 'weight'; $match_count++; } } elseif (empty($mating['max_weight'])) { if ($v['weight'] >= $mating['min_weight']) { $match_arr[] = 'weight'; $match_count++; } } elseif ($v['weight'] >= $mating['min_weight'] && $v['weight'] <= $mating['max_weight']) { $match_arr[] = 'weight'; $match_count++; } //生肖 if (empty($mating['animal'])) { $match_arr[] = 'animal'; $match_count++; } else { if (!in_array($v['animal'], $mating['animal'])) { $match_arr[] = 'animal'; $match_count++; } } //籍贯 ['晋江籍','非晋江籍','无要求'] if ($mating['native'] == '无要求') { $match_arr[] = 'native'; $match_count++; } elseif ($mating['native'] == '晋江籍') { if (strpos($v['native'], '晋江') !== false) { $match_arr[] = 'native'; $match_count++; } } else { if (strpos($v['native'], '晋江') === false) { $match_arr[] = 'native'; $match_count++; } } //学历 ['本科以下','本科','硕士','博士','无要求'] if ($mating['education'] == '无要求' || $mating['education'] == '本科以下') { $match_arr[] = 'education'; $match_count++; } elseif ($mating['education'] == '本科') { if (in_array($v['education'], ['本科', '硕士', '博士'])) { $match_arr[] = 'education'; $match_count++; } } elseif ($mating['education'] == '硕士') { if (in_array($v['education'], ['硕士', '博士'])) { $match_arr[] = 'education'; $match_count++; } } elseif ($mating['education'] == '博士') { if ($v['education'] == '博士') { $match_arr[] = 'education'; $match_count++; } } //年收入 ['不限', '5万以下', '5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上'] if ($mating['income'] == '不限' || $mating['income'] == '5万以下') { $match_arr[] = 'income'; $match_count++; } elseif ($mating['income'] == '5-10万') { if (in_array($v['income'], ['5-10万', '10-20万', '20-30万', '30-50万', '50-100万', '100万以上'])) { $match_arr[] = 'income'; $match_count++; } } elseif ($mating['income'] == '10-20万') { if (in_array($v['income'], ['10-20万', '20-30万', '30-50万', '50-100万', '100万以上'])) { $match_arr[] = 'income'; $match_count++; } } elseif ($mating['income'] == '20-30万') { if (in_array($v['income'], ['20-30万', '30-50万', '50-100万', '100万以上'])) { $match_arr[] = 'income'; $match_count++; } } elseif ($mating['income'] == '30-50万') { if (in_array($v['income'], ['30-50万', '50-100万', '100万以上'])) { $match_arr[] = 'income'; $match_count++; } } elseif ($mating['income'] == '50-100万') { if (in_array($v['income'], ['50-100万', '100万以上'])) { $match_arr[] = 'income'; $match_count++; } } elseif ($mating['income'] == '100万以上') { if (in_array($v['income'], ['100万以上'])) { $match_arr[] = 'income'; $match_count++; } } //是否有房 ['不限', '是', '否'] if ($mating['have_house'] == 1) { if ($v['have_house'] == 1) { $match_arr[] = 'have_house'; $match_count++; } } else { $match_arr[] = 'have_house'; $match_count++; } //是否有车 ['不限', '是', '否'] if ($mating['have_car'] == 1) { if ($v['have_car'] == 1) { $match_arr[] = 'have_car'; $match_count++; } } else { $match_arr[] = 'have_car'; $match_count++; } //是否婚后愿意与父母同住 ['不限', '是', '否'] if ($mating['with_parent_live'] == 2) { if ($v['with_parent_live'] == 2) { $match_arr[] = 'with_parent_live'; $match_count++; } } else { $match_arr[] = 'with_parent_live'; $match_count++; } //是否吸烟 ['不反感', '接受偶尔', '反感'] if ($mating['smoke'] == '不反感' || $mating['smoke'] == '') { $match_arr[] = 'smoke'; $match_count++; } elseif ($mating['smoke'] == '接受偶尔') { if (in_array($v['smoke'], ['社交时偶尔吸烟', '不吸烟,但不反感', '不吸,很反感'])) { $match_arr[] = 'smoke'; $match_count++; } } elseif ($mating['smoke'] == '反感') { if (in_array($v['smoke'], ['不吸,很反感', '不吸烟,但不反感'])) { $match_arr[] = 'smoke'; $match_count++; } } //是否喝酒 ['不反感', '接受偶尔', '反感'] if ($mating['drink'] == '不反感' || $mating['drink'] == '') { $match_arr[] = 'drink'; $match_count++; } elseif ($mating['drink'] == '接受偶尔') { if (in_array($v['drink'], ['不喝酒', '社交需要喝', '兴致时小酌'])) { $match_arr[] = 'drink'; $match_count++; } } elseif ($mating['drink'] == '反感') { if ($v['drink'] == '不喝酒') { $match_arr[] = 'drink'; $match_count++; } } //是否生育 if ($mating['want_birth'] != 1) { $match_arr[] = 'want_birth'; $match_count++; } else { if ($v['want_birth'] == 1) { $match_arr[] = 'want_birth'; $match_count++; } } $list[$k]['age'] = $age; $list[$k]['match_arr'] = $match_arr; $list[$k]['match_count'] = $match_count; //姓名 if (mb_strlen($list[$k]['realname'], 'utf-8') > 1) { $list[$k]['realname'] = $this->_replaceSecondChineseCharWithAsterisk($list[$k]['realname']); // 将第二个字符替换为星号 } } //排序 usort($list, function ($a, $b) { if ($a['match_count'] < $b['match_count']) { return 1; } elseif ($a['match_count'] > $b['match_count']) { return -1; } else { return 0; } }); //获取前10 if (count($list) > 10) { return array_slice($list, 0, 10); } else { return $list; } } private function _replaceSecondChineseCharWithAsterisk($string) { $length = mb_strlen($string, 'utf-8'); $arr = []; for ($i = 0; $i < $length; $i++) { $arr[] = mb_substr($string, $i, 1, 'utf-8'); } $arr[1] = '*'; return implode('', $arr); } }