| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- <?php
- namespace app\index\controller;
- use think\Controller;
- class User extends Controller
- {
- // 用户首页
- public function index()
- {
- $token = input("param.token/s");
- $res = model('Accounts')->checktoken($token);
- if($res == -1){
- return $res;
- }
- $user_id = explode('/',base64_decode($token))['2'];
- $userInfo = db('accounts')->where('id', $user_id)->find();
- //print_r($userInfo);exit;
- $this->assign([
- 'userInfo' => $userInfo
- ]);
- return $this->fetch();
- }
- // 修改密码
- public function uqdatePwd()
- {
- $token = input("param.token/s");
- $res = model('Accounts')->checktoken($token);
- if($res == -1){
- return $res;
- }
- $user_id = explode('/',base64_decode($token))['2'];
- if(request()->isPost()){
- $password = input("param.password/s");
- $newPassword = input("param.newPassword/s");
- $confirmPassword = input("param.confirmPassword/s");
- }
- if(empty($password)){
- return json(['code' => -1, 'data' => '', 'msg' => '原密码不能为空']);
- }
- if(empty($newPassword)){
- return json(['code' => -2, 'data' => '', 'msg' => '新密码不能为空']);
- }
- if(empty($confirmPassword)){
- return json(['code' => -3, 'data' => '', 'msg' => '确认新密码不能为空']);
- }
- if($newPassword != $confirmPassword){
- return json(['code' => -3, 'data' => '', 'msg' => '新密码不一致']);
- }
- $userInfo = db('accounts')->where('id', $user_id)->find();
- if(md5($password . config('salt')) != $userInfo['password']){
- return json(['code' => -3, 'data' => '', 'msg' => '原密码不正确']);
- }
- $param = [
- 'password' => md5($newPassword . config('salt'))
- ];
- try{
- db('accounts')->where('id', $user_id)->update($param);
- }catch(\Exception $e){
- return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
- }
- return json(['code' => 1, 'data' => url('user/index'), 'msg' => '密码修改成功']);
- }
- // 用户留言
- public function LeavingMessage()
- {
- if(request()->isPost()){
- $account_id = input("param.account_id/s");
- $nick_name = input("param.nick_name/s");
- $email = input("param.email/s");
- $phone = input("param.phone/s");
- $content = input("param.content/s");
- $account_ip = $_SERVER["REMOTE_ADDR"];
- //获得访问者浏览器
- $browse = $this->browse_info();
- //获得访客操作系统
- $system = $this->get_os();
- $image = input("param.file/s");
- if(empty($account_id)){
- return json(['code' => -1, 'data' => '', 'msg' => '用户id不能为空']);
- }
- if(empty($nick_name)){
- return json(['code' => -2, 'data' => '', 'msg' => '昵称不能为空']);
- }
- // if(empty($qq)){
- // return json(['code' => -3, 'data' => '', 'msg' => 'QQ不能为空']);
- // }
- // if(empty($wechat)){
- // return json(['code' => -4, 'data' => '', 'msg' => '微信不能为空']);
- // }
- if(empty($content)){
- return json(['code' => -5, 'data' => '', 'msg' => '内容不能为空']);
- }
- // if(empty($phone)){
- // return json(['code' => -6, 'data' => '', 'msg' => '电话不能为空']);
- // }
- // if(empty($image)){
- // return json(['code' => -7, 'data' => '', 'msg' => '附件不能为空']);
- // }
- $param = [
- 'account_id' => $account_id,
- 'nick_name' => $nick_name,
- 'content' => $content,
- 'account_ip' => $account_ip,
- 'browse' => $browse,
- 'system' => $system,
- 'image' => $image,
- 'message_status' => 0,
- 'add_time' => time()
- ];
- $account = db('accounts')->where('id',$account_id)->find();
- $info = array();
- $messageinfo = array();
- $info['nick_name'] = $nick_name;
- $messageinfo['nick_name'] = $nick_name;
- if(!empty($email)){
- $param['email'] = $email;
- $info['account_email'] = $email;
- $messageinfo['email'] = $email;
- }else{
- if(!empty($account)){
- $param['email'] = $account['account_email'];
- $messageinfo['email'] = $account['account_email'];
- }
- }
- if(!empty($phone)){
- $param['phone'] = $phone;
- $info['account_phone'] = $phone;
- $messageinfo['phone'] = $phone;
- }else{
- if(!empty($account)){
- $param['phone'] = $account['account_phone'];
- $messageinfo['phone'] = $account['account_phone'];
- }
- }
- try{
- db('accountsmessage')->insertGetId($param);
- if(!empty($info)){
- db('accounts')->where('id',$account_id)->update($info);
- db('accountsmessage')->where('account_id',$account_id)->update($messageinfo);
- }
- }catch(\Exception $e){
- return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
- }
- return json(['code' => 1, 'data' => url('user/index'), 'msg' => '留言成功']);
- }
- }
- // 用户消息记录
- public function historyNews()
- {
- if(request()->isPost()){
- $time = input("param.time/s");
- $user_id = input("param.user_id/s");
- $time = strtotime($time);
- // $user_id = 2;
- }
- $news = db('chat_log')->where(function($query) use($user_id) {
- $query->where('from_id', $user_id)->whereor('to_id',$user_id);
- })->where('time_line' ,'<=' ,$time)->order('time_line','desc')->limit(10)->select();
- for($i=0;$i<count($news);$i++){
- $news[$i]['time_line'] = date('Y-m-d H:i:s',$news[$i]['time_line']);
- }
- return json(['code' => 1, 'data' => $news, 'msg' => '成功']);
- }
- // 上传图片
- public function uplodeImg()
- {
- if(request()->isPost()){
- $file = request()->file('file');
- if(empty($file)){
- return json(['code' => -7, 'data' => '', 'msg' => '附件不能为空']);
- }
- $fileInfo = $file->getInfo();
- /*if($fileInfo['size'] > 1024 * 1024 * 2){
- // 上传失败获取错误信息
- return json( ['code' => -8, 'data' => '', 'msg' => '文件超过2M'] );
- }*/
- //检测图片格式
- $ext = explode('.', $fileInfo['name']);
- $ext = array_pop($ext);
- $extArr = explode('|', 'jpg|png|gif|jpeg');
- if(!in_array($ext, $extArr)){
- return json(['code' => -9, 'data' => '', 'msg' => '只能上传jpg|png|gif|jpeg的文件']);
- }
- // 移动到框架应用根目录/public/uploads/ 目录下
- $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
- if($info){
- $src = '/uploads' . '/' . date('Ymd') . '/' . $info->getFilename();
- }else{
- // 上传失败获取错误信息
- return json(['code' => -10, 'data' => '', 'msg' => $file->getError()]);
- }
- }
- }
- /**
- * 获得访客操作系统
- * @return string
- */
- // public static function get_os(){
- // if (!empty($_SESSION['userAgent'])) {
- // $os = $_SESSION['userAgent'];
- // if (preg_match('/win/i', $os)) {
- // $os = 'Windows';
- // } else if (preg_match('/mac/i', $os)) {
- // $os = 'MAC';
- // } else if (preg_match('/linux/i', $os)) {
- // $os = 'Linux';
- // } else if (preg_match('/unix/i', $os)) {
- // $os = 'Unix';
- // } else if (preg_match('/bsd/i', $os)) {
- // $os = 'BSD';
- // } else {
- // $os = 'Other';
- // }
- // return $os;
- // } else {
- // return 'unknow';
- // }
- // }
- function get_os() {
- $agent = $_SERVER['HTTP_USER_AGENT'];
- $os = false;
- if (stristr($agent,'win')) {
- $os = 'Windows';
- }
- else if (stristr($agent,'win') && stristr($agent, '95')) {
- $os = 'Windows 95';
- }
- else if (stristr($agent,'win 9x') && stristr($agent, '4.90')) {
- $os = 'Windows ME';
- }
- else if (stristr($agent,'win') && stristr($agent,'98')) {
- $os = 'Windows 98';
- }
- else if (stristr($agent,'win') && stristr($agent,'nt 5.1')) {
- $os = 'Windows XP';
- }
- else if (stristr($agent,'win') && stristr($agent,'nt 5')) {
- $os = 'Windows 2000';
- }
- else if (stristr($agent,'win') && stristr($agent,'nt')) {
- $os = 'Windows NT';
- }
- else if (stristr($agent,'win') && stristr($agent,'32')) {
- $os = 'Windows 32';
- } else if (stristr($agent,'linux')) {
- $os = 'Linux';
- }
- else if (stristr($agent,'unix')) {
- $os = 'Unix';
- }
- else if (stristr($agent,'sun') && stristr($agent,'os')) {
- $os = 'SunOS';
- }
- else if (stristr($agent,'ibm') && stristr($agent,'os')) {
- $os = 'IBM OS/2';
- }
- else if (stristr($agent,'Mac')) {
- $os = 'Mac OS X';
- }
- else if (stristr($agent,'PowerPC')) {
- $os = 'PowerPC';
- }
- else if (stristr($agent,'AIX')) {
- $os = 'AIX';
- }
- else if (stristr($agent,'HPUX')) {
- $os = 'HPUX';
- }
- else if (stristr($agent,'NetBSD')) {
- $os = 'NetBSD';
- }
- else if (stristr($agent,'BSD')) {
- $os = 'BSD';
- }
- else if (stristr($agent,'OSF1')) {
- $os = 'OSF1';
- }
- else if (stristr($agent,'IRIX')) {
- $os = 'IRIX';
- }
- else if (stristr($agent,'FreeBSD')) {
- $os = 'FreeBSD';
- }
- else if (stristr($agent,'teleport')) {
- $os = 'teleport';
- }
- else if (stristr($agent,'flashget')) {
- $os = 'flashget';
- }
- else if (stristr($agent,'webzip')) {
- $os = 'webzip';
- }
- else if (stristr($agent,'offline')) {
- $os = 'offline';
- }
- else{
- $os = '';
- }
- return $os;
- }
- /**
- * 获得访问者浏览器
- * @return string
- */
- // public static function browse_info(){
- // if (!empty($_SESSION['userAgent'])) {
- // $br = $_SESSION['userAgent'];
- // if (preg_match('/MSIE/i', $br)) {
- // $br = 'MSIE';
- // } else if (preg_match('/Firefox/i', $br)) {
- // $br = 'Firefox';
- // } else if (preg_match('/Chrome/i', $br)) {
- // $br = 'Chrome';
- // } else if (preg_match('/Safari/i', $br)) {
- // $br = 'Safari';
- // } else if (preg_match('/Opera/i', $br)) {
- // $br = 'Opera';
- // } else {
- // $br = 'Other';
- // }
- // return $br;
- // } else {
- // return 'unknow';
- // }
- // }
- public function browse_info(){
- if(!empty($_SERVER['HTTP_USER_AGENT'])){
- $br = $_SERVER['HTTP_USER_AGENT'];
- if (preg_match('/MSIE/i',$br)) {
- $br = 'MSIE';
- }
- elseif (preg_match('/Firefox/i',$br)) {
- $br = 'Firefox';
- }
- elseif (preg_match('/Chrome/i',$br)) {
- $br = 'Chrome';
- }
- elseif (preg_match('/Safari/i',$br)) {
- $br = 'Safari';
- }
- elseif (preg_match('/Opera/i',$br)) {
- $br = 'Opera';
- }else {
- $br = 'Other';
- }
- return $br;
- }
- else{
- return "";
- }
- }
- }
|