123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 老猫 <thinkcmf@126.com>
- // +----------------------------------------------------------------------
- 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,
- ];
- 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']]);
- $this->success('', '', ['id' => $user['id']]);
- }
- public function matingAdd()
- {
- $id = $this->request->param('id');
- if (empty($id)) {
- $this->error('参数错误');
- }
- $mating = MatchmakerUserMatingModel::get(['user_id' => $id]);
- $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::SMOKE,
- 'drink' => Constant::DRINK,
- 'tinyint' => Constant::COND_TINYINT,
- ];
- foreach ($data as &$v) {
- $v = json_encode($v);
- unset($v);
- }
- return $this->fetch('', $data);
- }
- public function matingAddPost()
- {
- $param = $this->request->post();
- 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,
- ];
- 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]);
- $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::SMOKE,
- 'drink' => Constant::DRINK,
- 'tinyint' => Constant::COND_TINYINT,
- ];
- foreach ($data as &$v) {
- $v = json_encode($v);
- unset($v);
- }
- return $this->fetch('', $data);
- }
- public function matingEditPost()
- {
- $param = $this->request->post();
- 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 match()
- {
- $id = $this->request->param('id');
- $type = $this->request->param('type', 0);
- 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('id', '<>', $id)
- ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
- ->append(['sex_text'])
- ->select();
- } elseif ($type == 1) {
- //其他红娘库
- $list = MatchmakerUserModel::with('matchmaker')
- ->where('matchmaker_id', '<>', $this->matchmaker->id)
- ->where('sex', '<>', $user['sex'] == 1 ? 1 : 2)
- ->append(['sex_text'])
- ->select();
- } else {
- //系统库
- $list = UserModel::where('user_type', 2)
- ->where('user_status', 1)
- ->where('sex', '=', $user['sex'] == 1 ? 1 : 2)
- ->append(['sex_text'])
- ->select();
- }
- $mating = MatchmakerUserMatingModel::where('user_id', $id)->find();
- $res = $this->_orderMatchUser($list->toArray(), $mating);
- return $this->fetch('', [
- 'list' => json_encode($res),
- 'type' => $type,
- 'user' => $user,
- 'id' => $id,
- 'mating' => $mating,
- '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 _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 ($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'] == '' || $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'] == '兴致时小酌' || $mating['drink'] == '社交需要喝') {
- if ($v['drink'] == '兴致时小酌' || $v['drink'] == '社交需要喝') {
- $match_arr[] = 'drink';
- $match_count++;
- }
- } elseif ($mating['drink'] == '不喝酒') {
- if ($v['drink'] == '不喝酒') {
- $match_arr[] = 'drink';
- $match_count++;
- }
- }
- $list[$k]['age'] = $age;
- $list[$k]['match_arr'] = $match_arr;
- $list[$k]['match_count'] = $match_count;
- }
- //排序
- 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;
- }
- }
- }
|