User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. class User extends Controller
  5. {
  6. // 用户首页
  7. public function index()
  8. {
  9. $token = input("param.token/s");
  10. $res = model('Accounts')->checktoken($token);
  11. if($res == -1){
  12. return $res;
  13. }
  14. $user_id = explode('/',base64_decode($token))['2'];
  15. $userInfo = db('accounts')->where('id', $user_id)->find();
  16. //print_r($userInfo);exit;
  17. $this->assign([
  18. 'userInfo' => $userInfo
  19. ]);
  20. return $this->fetch();
  21. }
  22. // 修改密码
  23. public function uqdatePwd()
  24. {
  25. $token = input("param.token/s");
  26. $res = model('Accounts')->checktoken($token);
  27. if($res == -1){
  28. return $res;
  29. }
  30. $user_id = explode('/',base64_decode($token))['2'];
  31. if(request()->isPost()){
  32. $password = input("param.password/s");
  33. $newPassword = input("param.newPassword/s");
  34. $confirmPassword = input("param.confirmPassword/s");
  35. }
  36. if(empty($password)){
  37. return json(['code' => -1, 'data' => '', 'msg' => '原密码不能为空']);
  38. }
  39. if(empty($newPassword)){
  40. return json(['code' => -2, 'data' => '', 'msg' => '新密码不能为空']);
  41. }
  42. if(empty($confirmPassword)){
  43. return json(['code' => -3, 'data' => '', 'msg' => '确认新密码不能为空']);
  44. }
  45. if($newPassword != $confirmPassword){
  46. return json(['code' => -3, 'data' => '', 'msg' => '新密码不一致']);
  47. }
  48. $userInfo = db('accounts')->where('id', $user_id)->find();
  49. if(md5($password . config('salt')) != $userInfo['password']){
  50. return json(['code' => -3, 'data' => '', 'msg' => '原密码不正确']);
  51. }
  52. $param = [
  53. 'password' => md5($newPassword . config('salt'))
  54. ];
  55. try{
  56. db('accounts')->where('id', $user_id)->update($param);
  57. }catch(\Exception $e){
  58. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  59. }
  60. return json(['code' => 1, 'data' => url('user/index'), 'msg' => '密码修改成功']);
  61. }
  62. // 用户留言
  63. public function LeavingMessage()
  64. {
  65. if(request()->isPost()){
  66. $account_id = input("param.account_id/s");
  67. $nick_name = input("param.nick_name/s");
  68. $email = input("param.email/s");
  69. $phone = input("param.phone/s");
  70. $content = input("param.content/s");
  71. $account_ip = $_SERVER["REMOTE_ADDR"];
  72. //获得访问者浏览器
  73. $browse = $this->browse_info();
  74. //获得访客操作系统
  75. $system = $this->get_os();
  76. $image = input("param.file/s");
  77. if(empty($account_id)){
  78. return json(['code' => -1, 'data' => '', 'msg' => '用户id不能为空']);
  79. }
  80. if(empty($nick_name)){
  81. return json(['code' => -2, 'data' => '', 'msg' => '昵称不能为空']);
  82. }
  83. // if(empty($qq)){
  84. // return json(['code' => -3, 'data' => '', 'msg' => 'QQ不能为空']);
  85. // }
  86. // if(empty($wechat)){
  87. // return json(['code' => -4, 'data' => '', 'msg' => '微信不能为空']);
  88. // }
  89. if(empty($content)){
  90. return json(['code' => -5, 'data' => '', 'msg' => '内容不能为空']);
  91. }
  92. // if(empty($phone)){
  93. // return json(['code' => -6, 'data' => '', 'msg' => '电话不能为空']);
  94. // }
  95. // if(empty($image)){
  96. // return json(['code' => -7, 'data' => '', 'msg' => '附件不能为空']);
  97. // }
  98. $param = [
  99. 'account_id' => $account_id,
  100. 'nick_name' => $nick_name,
  101. 'content' => $content,
  102. 'account_ip' => $account_ip,
  103. 'browse' => $browse,
  104. 'system' => $system,
  105. 'image' => $image,
  106. 'message_status' => 0,
  107. 'add_time' => time()
  108. ];
  109. $account = db('accounts')->where('id',$account_id)->find();
  110. $info = array();
  111. $messageinfo = array();
  112. $info['nick_name'] = $nick_name;
  113. $messageinfo['nick_name'] = $nick_name;
  114. if(!empty($email)){
  115. $param['email'] = $email;
  116. $info['account_email'] = $email;
  117. $messageinfo['email'] = $email;
  118. }else{
  119. if(!empty($account)){
  120. $param['email'] = $account['account_email'];
  121. $messageinfo['email'] = $account['account_email'];
  122. }
  123. }
  124. if(!empty($phone)){
  125. $param['phone'] = $phone;
  126. $info['account_phone'] = $phone;
  127. $messageinfo['phone'] = $phone;
  128. }else{
  129. if(!empty($account)){
  130. $param['phone'] = $account['account_phone'];
  131. $messageinfo['phone'] = $account['account_phone'];
  132. }
  133. }
  134. try{
  135. db('accountsmessage')->insertGetId($param);
  136. if(!empty($info)){
  137. db('accounts')->where('id',$account_id)->update($info);
  138. db('accountsmessage')->where('account_id',$account_id)->update($messageinfo);
  139. }
  140. }catch(\Exception $e){
  141. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  142. }
  143. return json(['code' => 1, 'data' => url('user/index'), 'msg' => '留言成功']);
  144. }
  145. }
  146. // 用户消息记录
  147. public function historyNews()
  148. {
  149. if(request()->isPost()){
  150. $time = input("param.time/s");
  151. $user_id = input("param.user_id/s");
  152. $time = strtotime($time);
  153. // $user_id = 2;
  154. }
  155. $news = db('chat_log')->where(function($query) use($user_id) {
  156. $query->where('from_id', $user_id)->whereor('to_id',$user_id);
  157. })->where('time_line' ,'<=' ,$time)->order('time_line','desc')->limit(10)->select();
  158. for($i=0;$i<count($news);$i++){
  159. $news[$i]['time_line'] = date('Y-m-d H:i:s',$news[$i]['time_line']);
  160. }
  161. return json(['code' => 1, 'data' => $news, 'msg' => '成功']);
  162. }
  163. // 上传图片
  164. public function uplodeImg()
  165. {
  166. if(request()->isPost()){
  167. $file = request()->file('file');
  168. if(empty($file)){
  169. return json(['code' => -7, 'data' => '', 'msg' => '附件不能为空']);
  170. }
  171. $fileInfo = $file->getInfo();
  172. /*if($fileInfo['size'] > 1024 * 1024 * 2){
  173. // 上传失败获取错误信息
  174. return json( ['code' => -8, 'data' => '', 'msg' => '文件超过2M'] );
  175. }*/
  176. //检测图片格式
  177. $ext = explode('.', $fileInfo['name']);
  178. $ext = array_pop($ext);
  179. $extArr = explode('|', 'jpg|png|gif|jpeg');
  180. if(!in_array($ext, $extArr)){
  181. return json(['code' => -9, 'data' => '', 'msg' => '只能上传jpg|png|gif|jpeg的文件']);
  182. }
  183. // 移动到框架应用根目录/public/uploads/ 目录下
  184. $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
  185. if($info){
  186. $src = '/uploads' . '/' . date('Ymd') . '/' . $info->getFilename();
  187. }else{
  188. // 上传失败获取错误信息
  189. return json(['code' => -10, 'data' => '', 'msg' => $file->getError()]);
  190. }
  191. }
  192. }
  193. /**
  194. * 获得访客操作系统
  195. * @return string
  196. */
  197. // public static function get_os(){
  198. // if (!empty($_SESSION['userAgent'])) {
  199. // $os = $_SESSION['userAgent'];
  200. // if (preg_match('/win/i', $os)) {
  201. // $os = 'Windows';
  202. // } else if (preg_match('/mac/i', $os)) {
  203. // $os = 'MAC';
  204. // } else if (preg_match('/linux/i', $os)) {
  205. // $os = 'Linux';
  206. // } else if (preg_match('/unix/i', $os)) {
  207. // $os = 'Unix';
  208. // } else if (preg_match('/bsd/i', $os)) {
  209. // $os = 'BSD';
  210. // } else {
  211. // $os = 'Other';
  212. // }
  213. // return $os;
  214. // } else {
  215. // return 'unknow';
  216. // }
  217. // }
  218. function get_os() {
  219. $agent = $_SERVER['HTTP_USER_AGENT'];
  220. $os = false;
  221. if (stristr($agent,'win')) {
  222. $os = 'Windows';
  223. }
  224. else if (stristr($agent,'win') && stristr($agent, '95')) {
  225. $os = 'Windows 95';
  226. }
  227. else if (stristr($agent,'win 9x') && stristr($agent, '4.90')) {
  228. $os = 'Windows ME';
  229. }
  230. else if (stristr($agent,'win') && stristr($agent,'98')) {
  231. $os = 'Windows 98';
  232. }
  233. else if (stristr($agent,'win') && stristr($agent,'nt 5.1')) {
  234. $os = 'Windows XP';
  235. }
  236. else if (stristr($agent,'win') && stristr($agent,'nt 5')) {
  237. $os = 'Windows 2000';
  238. }
  239. else if (stristr($agent,'win') && stristr($agent,'nt')) {
  240. $os = 'Windows NT';
  241. }
  242. else if (stristr($agent,'win') && stristr($agent,'32')) {
  243. $os = 'Windows 32';
  244. } else if (stristr($agent,'linux')) {
  245. $os = 'Linux';
  246. }
  247. else if (stristr($agent,'unix')) {
  248. $os = 'Unix';
  249. }
  250. else if (stristr($agent,'sun') && stristr($agent,'os')) {
  251. $os = 'SunOS';
  252. }
  253. else if (stristr($agent,'ibm') && stristr($agent,'os')) {
  254. $os = 'IBM OS/2';
  255. }
  256. else if (stristr($agent,'Mac')) {
  257. $os = 'Mac OS X';
  258. }
  259. else if (stristr($agent,'PowerPC')) {
  260. $os = 'PowerPC';
  261. }
  262. else if (stristr($agent,'AIX')) {
  263. $os = 'AIX';
  264. }
  265. else if (stristr($agent,'HPUX')) {
  266. $os = 'HPUX';
  267. }
  268. else if (stristr($agent,'NetBSD')) {
  269. $os = 'NetBSD';
  270. }
  271. else if (stristr($agent,'BSD')) {
  272. $os = 'BSD';
  273. }
  274. else if (stristr($agent,'OSF1')) {
  275. $os = 'OSF1';
  276. }
  277. else if (stristr($agent,'IRIX')) {
  278. $os = 'IRIX';
  279. }
  280. else if (stristr($agent,'FreeBSD')) {
  281. $os = 'FreeBSD';
  282. }
  283. else if (stristr($agent,'teleport')) {
  284. $os = 'teleport';
  285. }
  286. else if (stristr($agent,'flashget')) {
  287. $os = 'flashget';
  288. }
  289. else if (stristr($agent,'webzip')) {
  290. $os = 'webzip';
  291. }
  292. else if (stristr($agent,'offline')) {
  293. $os = 'offline';
  294. }
  295. else{
  296. $os = '';
  297. }
  298. return $os;
  299. }
  300. /**
  301. * 获得访问者浏览器
  302. * @return string
  303. */
  304. // public static function browse_info(){
  305. // if (!empty($_SESSION['userAgent'])) {
  306. // $br = $_SESSION['userAgent'];
  307. // if (preg_match('/MSIE/i', $br)) {
  308. // $br = 'MSIE';
  309. // } else if (preg_match('/Firefox/i', $br)) {
  310. // $br = 'Firefox';
  311. // } else if (preg_match('/Chrome/i', $br)) {
  312. // $br = 'Chrome';
  313. // } else if (preg_match('/Safari/i', $br)) {
  314. // $br = 'Safari';
  315. // } else if (preg_match('/Opera/i', $br)) {
  316. // $br = 'Opera';
  317. // } else {
  318. // $br = 'Other';
  319. // }
  320. // return $br;
  321. // } else {
  322. // return 'unknow';
  323. // }
  324. // }
  325. public function browse_info(){
  326. if(!empty($_SERVER['HTTP_USER_AGENT'])){
  327. $br = $_SERVER['HTTP_USER_AGENT'];
  328. if (preg_match('/MSIE/i',$br)) {
  329. $br = 'MSIE';
  330. }
  331. elseif (preg_match('/Firefox/i',$br)) {
  332. $br = 'Firefox';
  333. }
  334. elseif (preg_match('/Chrome/i',$br)) {
  335. $br = 'Chrome';
  336. }
  337. elseif (preg_match('/Safari/i',$br)) {
  338. $br = 'Safari';
  339. }
  340. elseif (preg_match('/Opera/i',$br)) {
  341. $br = 'Opera';
  342. }else {
  343. $br = 'Other';
  344. }
  345. return $br;
  346. }
  347. else{
  348. return "";
  349. }
  350. }
  351. }