<?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\love\controller; use app\common\Constant; use app\common\Fun; use app\love\model\LotteryPrizeModel; use app\love\model\UserFavoriteModel; use app\love\model\UserFriendModel; use app\love\model\UserGiftModel; use app\love\model\UserInviteModel; use app\love\model\UserMarryModel; use app\love\model\UserMatingModel; use app\love\model\UserMessageModel; use app\love\model\UserModel; use app\love\model\UserSelectLogModel; use app\love\model\UserSelectModel; use app\love\model\UserVisitModel; use think\Db; class MyController extends LoveBaseController { private $base_arr = ['nation', 'company', 'job', 'hobby', 'smoke', 'drink']; /** * 我的 */ public function index() { //用户信息 $user_id = cmf_get_current_user_id(); $this->assign('user_id', $user_id); $user = UserModel::get($user_id); unset($user['user_pass']); $this->assign('user', json_encode($user)); //完善资料 $is_perfect = 'false'; foreach ($this->base_arr as $v) { if (empty($user[$v])) { $is_perfect = 'true'; break; } } $this->assign('is_perfect', $is_perfect); //择偶 // $mating = UserMatingModel::get(['user_id' => cmf_get_current_user_id()]); $is_mating = 'false'; /*foreach ($this->mating_arr as $v) { if (empty($mating[$v])) { $is_mating = 'true'; break; } }*/ $this->assign('is_mating', $is_mating); //未读消息数 $unread_num = UserFriendModel::where('user_id', $user_id)->sum('unread_num'); $invite_num = UserInviteModel::where('to_id', $user_id) ->where('status', 1) ->count(); $this->assign('unread_num', $unread_num + $invite_num); //我的访客 $visit_where = [ ['to_id', '=', $user_id], ['status', '=', 2], ]; $visit_count = UserVisitModel::where($visit_where)->count(); $this->assign('visit_count', $visit_count ?: ''); //收到的礼物 $gift_count = UserGiftModel::where($visit_where)->count(); $this->assign('gift_count', $gift_count ?: ''); //收到的选择 $select_count = UserSelectModel::where([ ['uid', '=', $user_id], ['status', '=', 2], ])->count(); $this->assign('select_count', $select_count ?: ''); //是否互选 $select_log = 'false'; if ($this->user['sex'] == 1 && $this->user['use_ticket'] == 2) { $log_check = UserSelectLogModel::where('user_id1', $this->user['id'])->find(); if (!empty($log_check)) { $select_log = 'true'; } } $this->assign('select_log', $select_log); return $this->fetch(); } /** * 审核 */ public function check() { //用户信息 $user = UserModel::get(cmf_get_current_user_id()); unset($user['user_pass']); $this->assign('user', json_encode($user)); return $this->fetch(); } /** * 个人资料 */ public function profile() { //用户信息 $user = UserModel::get(cmf_get_current_user_id()); $user['birthday'] = date('Y-m-d', $user['birthday']); unset($user['user_pass']); $this->assign('user', json_encode($user)); $data = [ '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 profilePost() { $param = $this->request->post(); $param['user_nickname'] = $param['realname']; $param['birthday'] = strtotime($param['birthday']); UserModel::update($param, ['id' => cmf_get_current_user_id()]); $this->success('操作成功'); } /** * 择偶要求 */ public function cond() { $mating = UserMatingModel::get(['user_id' => cmf_get_current_user_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 condPost() { $param = $this->request->post(); UserMatingModel::update($param, ['user_id' => cmf_get_current_user_id()]); $this->success('操作成功'); } /** * 自我介绍 */ public function intro() { $user = UserModel::field('main_image,main_image_thumb,signature')->where('id', cmf_get_current_user_id())->find(); $this->assign('user', json_encode($user)); return $this->fetch(); } /** * 自我介绍提交 */ public function introPost() { $param = $this->request->post(); $param['avatar'] = $param['main_image_thumb']; UserModel::update($param, ['id' => cmf_get_current_user_id()], ['avatar', 'main_image', 'main_image_thumb', 'signature']); $this->success('操作成功'); } /** * 相册 */ public function photo() { $user = UserModel::get(cmf_get_current_user_id()); if (empty($user['more'])) { $user['more'] = []; } $more = array_filter($user['more']); $this->assign('more', json_encode($more)); return $this->fetch(); } /** * 相册提交 */ public function photoPost() { $param = $this->request->post(); if (empty($param)) { $param = ['more' => '']; } UserModel::update($param, ['id' => cmf_get_current_user_id()], 'more'); $this->success('操作成功'); } /** * 我的访客 */ public function visitme() { //清除未读 UserVisitModel::update(['status' => 1], ['to_id' => cmf_get_current_user_id(), 'status' => 2]); $list = UserVisitModel::with('to_user') ->where('from_id', '=', cmf_get_current_user_id()) ->limit(10) ->order('last_visit_time desc') ->select(); foreach ($list as $v) { $v['user'] = $v['to_user']; $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']); $v['last_visit_time'] = date('m-d H:i', $v['last_visit_time']); } $this->assign('list1', $list); $list = UserVisitModel::with('from_user') ->where('to_id', '=', cmf_get_current_user_id()) ->limit(10) ->order('last_visit_time desc') ->select(); foreach ($list as $v) { $v['user'] = $v['from_user']; $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']); $v['last_visit_time'] = date('m-d H:i', $v['last_visit_time']); } $this->assign('list2', $list); return $this->fetch(); } /** * 我的访客列表 */ public function visitmeList() { $param = $this->request->post(); $user_id = cmf_get_current_user_id(); $where = []; $with = 'to_user'; if ($param['status'] == 0) { $where[] = ['from_id', '=', $user_id]; } elseif ($param['status'] == 1) { $where[] = ['to_id', '=', $user_id]; $with = 'from_user'; } $list = UserVisitModel::with($with) ->where($where) ->page($param['page']) ->limit(10) ->order('last_visit_time desc') ->select(); foreach ($list as $v) { if ($param['status'] == 0) { $v['user'] = $v['to_user']; } elseif ($param['status'] == 1) { $v['user'] = $v['from_user']; } $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']); $v['last_visit_time'] = date('m-d H:i', $v['last_visit_time']); } $this->result($list, 1); } /** * 我的收藏 */ public function star() { $list = UserFavoriteModel::with('to_user') ->where('user_id', cmf_get_current_user_id()) ->limit(10) ->order('create_time desc') ->select(); foreach ($list as $v) { $v['to_user']['age'] = empty($v['to_user']['birthday']) ? 0 : date('Y') - date('Y', $v['to_user']['birthday']); $v['create_time'] = date('m-d H:i', $v['create_time']); } $this->assign('list', $list); return $this->fetch(); } /** * 我的收藏列表 */ public function starList() { $param = $this->request->post(); $list = UserFavoriteModel::with('to_user') ->where('user_id', cmf_get_current_user_id()) ->page($param['page']) ->limit(10) ->order('create_time desc') ->select(); foreach ($list as $v) { $v['to_user']['age'] = empty($v['to_user']['birthday']) ? 0 : date('Y') - date('Y', $v['to_user']['birthday']); $v['create_time'] = date('m-d H:i', $v['create_time']); } $this->result($list, 1); } /** * 我的互选 */ public function select() { //清除未读 $user_id = cmf_get_current_user_id(); UserSelectModel::update(['status' => 1], ['uid' => $user_id, 'status' => 2]); $select = UserSelectModel::where('user_id|uid', '=', $user_id) ->where('is_confirm', 1) ->find(); //奖品 $has_prize = LotteryPrizeModel::all(); $this->assign('has_prize', $has_prize->isEmpty() ? 'false' : 'true'); if (!empty($select)) { //已选择 if ($select['user_id'] == $user_id) { $uid = $select['uid']; } else { $uid = $select['user_id']; } $user = UserModel::get($uid); $user['age'] = Fun::getAgeByBirth($user['birthday']); $this->assign('user', $user); $this->assign('list1', '[]'); $this->assign('list2', '[]'); $this->assign('is_select', 'true'); } else { //未选择 $this->assign('user', '[]'); $list = UserSelectModel::with('to_user') ->where('uid', '=', cmf_get_current_user_id()) ->limit(10) ->order('create_time desc') ->select(); foreach ($list as $v) { $v['user'] = $v['to_user']; $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']); $v['create_time'] = date('m-d H:i', $v['create_time']); } $this->assign('list1', $list); $list = UserSelectModel::with('from_user') ->where('user_id', '=', cmf_get_current_user_id()) ->limit(10) ->order('create_time desc') ->select(); foreach ($list as $v) { $v['user'] = $v['from_user']; $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']); $v['create_time'] = date('m-d H:i', $v['create_time']); } $this->assign('list2', $list); $this->assign('is_select', 'false'); } return $this->fetch(); } /** * 我的访客列表 */ public function selectList() { $param = $this->request->post(); $user_id = cmf_get_current_user_id(); $where = []; $with = 'to_user'; if ($param['status'] == 0) { $where[] = ['user_id', '=', $user_id]; } elseif ($param['status'] == 1) { $where[] = ['uid', '=', $user_id]; $with = 'from_user'; } $list = UserSelectModel::with($with) ->where($where) ->page($param['page']) ->limit(10) ->order('create_time desc') ->select(); foreach ($list as $v) { if ($param['status'] == 0) { $v['user'] = $v['to_user']; } elseif ($param['status'] == 1) { $v['user'] = $v['from_user']; } $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']); $v['create_time'] = date('m-d H:i', $v['create_time']); } $this->result($list, 1); } /** * 取消选择 */ public function selectCancel() { $id = $this->request->post('id'); $user_id = cmf_get_current_user_id(); $user = UserSelectModel::get(['user_id' => $user_id, 'uid' => $id]); //20230423增加两个userid对调的查询,因为双方都有权利取消 if (empty($user)) { $user = UserSelectModel::get(['user_id' => $id, 'uid' => $user_id]); } if (!empty($user)) { UserSelectLogModel::where('user_id1|user_id2', $id)->update(['delete_time'=>time()]); $user->delete(); } $this->success('操作成功'); } /** * 确认选择 */ public function selectConfirm() { $id = $this->request->post('id'); $user_id = cmf_get_current_user_id(); if ($user_id == $id) { $this->error('不可以选择自己!'); } $user = UserSelectModel::get(['user_id' => $id, 'uid' => $user_id]); $check = UserSelectLogModel::where('user_id1|user_id2', $id)->find(); if (!empty($check)) { $this->error('对方已被选择!'); } if (empty($user)) { $this->error('该数据不存在!'); } $user->is_confirm = 1; $user->confirm_time = time(); $user->save(); //增加互选记录 $log = ['confirm_time' => time()]; if ($this->user['sex'] == 1) { $log['user_id1'] = $user_id; $log['user_id2'] = $id; } else { $log['user_id1'] = $id; $log['user_id2'] = $user_id; } UserSelectLogModel::create($log); //查看是否为好友 $friend = UserFriendModel::where([ ['user_id', '=', $user_id], ['friend_id', '=', $id], ])->find(); if (empty($friend)) { //查看是否发起过聊天申请 $apply = UserInviteModel::where(function ($query) use ($user_id, $id) { $query->where([ ['from_id', '=', $user_id], ['to_id', '=', $id], ]); })->whereOr(function ($query) use ($user_id, $id) { $query->where([ ['to_id', '=', $user_id], ['from_id', '=', $id], ]); })->find(); if (!empty($apply)) { $apply->status = 2; } //添加好友 UserFriendModel::create([ 'user_id' => $id, 'friend_id' => $user_id, 'last_msg_time' => time(), ]); UserFriendModel::create([ 'user_id' => $user_id, 'friend_id' => $id, 'last_msg_time' => time(), ]); } //增加互选次数 Db::name('config')->where('id', 1)->setInc('select_num'); //发送提示 $form_id = $this->user->sex = 1 ? $user_id : $id; $to_id = $this->user->sex = 1 ? $id : $user_id; $time = time(); UserMessageModel::create([ 'from_id' => $form_id, 'to_id' => $to_id, 'message' => '【系统消息】系统赠送了两张咖啡券,约个时间一起去喝咖啡吧。', 'create_time' => $time, ]); //增加最后聊天消息 UserFriendModel::update(['last_msg' => '我:【系统消息】系统赠送了两张咖啡券,约个时间一起去喝咖啡吧。', 'last_msg_time' => $time], ['user_id' => $form_id, 'friend_id' => $to_id]); $friend = UserFriendModel::get(['user_id' => $to_id, 'friend_id' => $form_id]); $friend->last_msg = '对方:【系统消息】系统赠送了两张咖啡券,约个时间一起去喝咖啡吧。'; $friend->last_msg_time = $time; $friend->unread_num++; $friend->save(); $other = UserModel::get($id); $this->success('操作成功', '', ['mobile' => $other['mobile']]); } /** * 结婚 */ public function marry() { $user_id = cmf_get_current_user_id(); $log = UserSelectLogModel::where('user_id1|user_id2', $user_id)->find(); if (empty($log)) { $this->error('请先确认关系后再结婚!'); } $id = $log['user_id1'] == $user_id ? $log['user_id2'] : $log['user_id1']; $marry = UserMarryModel::where(function ($query) use ($user_id, $id) { $query->where([ ['user_id1', '=', $user_id], ['user_id2', '=', $id], ]); })->whereOr(function ($query) use ($user_id, $id) { $query->where([ ['user_id2', '=', $user_id], ['user_id1', '=', $id], ]); })->find(); //20230423增加两个userid对调的查询,因为双方都有权利取消 if (empty($marry)) { UserMarryModel::create(['user_id1' => $user_id, 'user_id2' => $id, 'create_time' => time()]); UserModel::where('id', 'in', [$id, $user_id])->update(['is_marry' => 1]); } //增加结婚次数 Db::name('config')->where('id', 1)->setInc('marry_num'); $this->success('操作成功'); } /** * 收到的礼物 */ public function gift() { //清除未读 UserGiftModel::update(['status' => 1], ['to_id' => cmf_get_current_user_id(), 'status' => 2]); $list = UserGiftModel::with('gift,from_user') ->where('to_id', '=', cmf_get_current_user_id()) ->limit(10) ->order('create_time desc') ->select(); foreach ($list as $v) { $v['user'] = $v['from_user']; $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']); $v['create_time'] = date('m-d H:i', $v['create_time']); } $this->assign('list1', $list); $list = UserGiftModel::with('gift,to_user') ->where('from_id', '=', cmf_get_current_user_id()) ->limit(10) ->order('create_time desc') ->select(); foreach ($list as $v) { $v['user'] = $v['to_user']; $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']); $v['create_time'] = date('m-d H:i', $v['create_time']); } $this->assign('list2', $list); return $this->fetch(); } /** * 收到的礼物列表 */ public function giftList() { $param = $this->request->post(); $user_id = cmf_get_current_user_id(); $where = []; $with = 'gift,from_user'; if ($param['status'] == 0) { $where[] = ['to_id', '=', $user_id]; } elseif ($param['status'] == 1) { $where[] = ['from_id', '=', $user_id]; $with = 'gift,to_user'; } $list = UserGiftModel::with($with) ->where($where) ->page($param['page']) ->limit(10) ->order('create_time desc') ->select(); foreach ($list as $v) { if ($param['status'] == 0) { $v['user'] = $v['from_user']; } elseif ($param['status'] == 1) { $v['user'] = $v['to_user']; } $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']); $v['create_time'] = date('m-d H:i', $v['create_time']); } $this->result($list, 1); } /** * 图片上传 */ 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('上传失败,请重新上传'); } //缩略图 $thumb_file = $this->_file_name($ext); $image = \think\Image::open($file_path); $image->thumb(220, 270)->save($upload_dir . '/' . $thumb_file); $main_image = cmf_get_user_avatar_url('image' . '/' . date('Ymd') . '/' . $file_name); $main_image_thumb = cmf_get_user_avatar_url('image' . '/' . date('Ymd') . '/' . $thumb_file); $this->result([ 'main_image' => cmf_get_user_avatar_url($main_image), 'main_image_thumb' => cmf_get_user_avatar_url($main_image_thumb), ], 1); } else { $this->error('请上传文件'); } } /** * 用户头像上传 */ public function avatarUpload() { $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' . '/' . 'avatar'; 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); $is_upload = $this->_base64_image_content($file, $upload_dir . '/' . $file_name); if (!$is_upload) { $this->error('上传失败,请重新上传'); } //更新头像 $avatar = cmf_get_user_avatar_url('avatar' . '/' . date('Ymd') . '/' . $file_name); UserModel::update(['avatar' => $avatar], ['id' => cmf_get_current_user_id()]); $this->result(cmf_get_user_avatar_url($avatar), 1); } else { $this->error('请上传文件'); } } /** * 用户相册上传 */ public function photoUpload() { $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' . '/' . 'photo'; 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); $is_upload = $this->_base64_image_content($file, $upload_dir . '/' . $file_name); if (!$is_upload) { $this->error('上传失败,请重新上传'); } $photo = cmf_get_user_avatar_url('photo' . '/' . date('Ymd') . '/' . $file_name); $this->result(cmf_get_user_avatar_url($photo), 1); } else { $this->error('请上传文件'); } } /** * 咖啡券 */ public function ticket() { //是否互选 if ($this->user['sex'] == 2) { $this->error('暂无咖啡券', url('index')); } $log_check = UserSelectLogModel::where('user_id1', $this->user['id'])->find(); if (empty($log_check)) { $this->error('暂无咖啡券', url('index')); } return $this->fetch(); } public function useTicket() { $this->user->use_ticket = 1; $this->user->save(); $this->success('核销成功!'); } 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; } } }