123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857 |
- <?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;
- }
- }
- }
|