User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. $info['nick_name'] = $nick_name;
  112. if(!empty($email)){
  113. $param['email'] = $email;
  114. $info['account_email'] = $email;
  115. }else{
  116. if(!empty($account)){
  117. $param['email'] = $account['account_email'];
  118. }
  119. }
  120. if(!empty($phone)){
  121. $param['phone'] = $phone;
  122. $info['account_phone'] = $phone;
  123. }else{
  124. if(!empty($account)){
  125. $param['phone'] = $account['account_phone'];
  126. }
  127. }
  128. try{
  129. db('accountsmessage')->insertGetId($param);
  130. if(!empty($info)){
  131. db('accounts')->where('id',$account_id)->update($info);
  132. }
  133. }catch(\Exception $e){
  134. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  135. }
  136. return json(['code' => 1, 'data' => url('user/index'), 'msg' => '留言成功']);
  137. }
  138. }
  139. // 上传图片
  140. public function uplodeImg()
  141. {
  142. if(request()->isPost()){
  143. $file = request()->file('file');
  144. if(empty($file)){
  145. return json(['code' => -7, 'data' => '', 'msg' => '附件不能为空']);
  146. }
  147. $fileInfo = $file->getInfo();
  148. /*if($fileInfo['size'] > 1024 * 1024 * 2){
  149. // 上传失败获取错误信息
  150. return json( ['code' => -8, 'data' => '', 'msg' => '文件超过2M'] );
  151. }*/
  152. //检测图片格式
  153. $ext = explode('.', $fileInfo['name']);
  154. $ext = array_pop($ext);
  155. $extArr = explode('|', 'jpg|png|gif|jpeg');
  156. if(!in_array($ext, $extArr)){
  157. return json(['code' => -9, 'data' => '', 'msg' => '只能上传jpg|png|gif|jpeg的文件']);
  158. }
  159. // 移动到框架应用根目录/public/uploads/ 目录下
  160. $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
  161. if($info){
  162. $src = '/uploads' . '/' . date('Ymd') . '/' . $info->getFilename();
  163. }else{
  164. // 上传失败获取错误信息
  165. return json(['code' => -10, 'data' => '', 'msg' => $file->getError()]);
  166. }
  167. }
  168. }
  169. /**
  170. * 获得访客操作系统
  171. * @return string
  172. */
  173. // public static function get_os(){
  174. // if (!empty($_SESSION['userAgent'])) {
  175. // $os = $_SESSION['userAgent'];
  176. // if (preg_match('/win/i', $os)) {
  177. // $os = 'Windows';
  178. // } else if (preg_match('/mac/i', $os)) {
  179. // $os = 'MAC';
  180. // } else if (preg_match('/linux/i', $os)) {
  181. // $os = 'Linux';
  182. // } else if (preg_match('/unix/i', $os)) {
  183. // $os = 'Unix';
  184. // } else if (preg_match('/bsd/i', $os)) {
  185. // $os = 'BSD';
  186. // } else {
  187. // $os = 'Other';
  188. // }
  189. // return $os;
  190. // } else {
  191. // return 'unknow';
  192. // }
  193. // }
  194. function get_os() {
  195. $agent = $_SERVER['HTTP_USER_AGENT'];
  196. $os = false;
  197. if (stristr($agent,'win')) {
  198. $os = 'Windows';
  199. }
  200. else if (stristr($agent,'win') && stristr($agent, '95')) {
  201. $os = 'Windows 95';
  202. }
  203. else if (stristr($agent,'win 9x') && stristr($agent, '4.90')) {
  204. $os = 'Windows ME';
  205. }
  206. else if (stristr($agent,'win') && stristr($agent,'98')) {
  207. $os = 'Windows 98';
  208. }
  209. else if (stristr($agent,'win') && stristr($agent,'nt 5.1')) {
  210. $os = 'Windows XP';
  211. }
  212. else if (stristr($agent,'win') && stristr($agent,'nt 5')) {
  213. $os = 'Windows 2000';
  214. }
  215. else if (stristr($agent,'win') && stristr($agent,'nt')) {
  216. $os = 'Windows NT';
  217. }
  218. else if (stristr($agent,'win') && stristr($agent,'32')) {
  219. $os = 'Windows 32';
  220. } else if (stristr($agent,'linux')) {
  221. $os = 'Linux';
  222. }
  223. else if (stristr($agent,'unix')) {
  224. $os = 'Unix';
  225. }
  226. else if (stristr($agent,'sun') && stristr($agent,'os')) {
  227. $os = 'SunOS';
  228. }
  229. else if (stristr($agent,'ibm') && stristr($agent,'os')) {
  230. $os = 'IBM OS/2';
  231. }
  232. else if (stristr($agent,'Mac')) {
  233. $os = 'Mac OS X';
  234. }
  235. else if (stristr($agent,'PowerPC')) {
  236. $os = 'PowerPC';
  237. }
  238. else if (stristr($agent,'AIX')) {
  239. $os = 'AIX';
  240. }
  241. else if (stristr($agent,'HPUX')) {
  242. $os = 'HPUX';
  243. }
  244. else if (stristr($agent,'NetBSD')) {
  245. $os = 'NetBSD';
  246. }
  247. else if (stristr($agent,'BSD')) {
  248. $os = 'BSD';
  249. }
  250. else if (stristr($agent,'OSF1')) {
  251. $os = 'OSF1';
  252. }
  253. else if (stristr($agent,'IRIX')) {
  254. $os = 'IRIX';
  255. }
  256. else if (stristr($agent,'FreeBSD')) {
  257. $os = 'FreeBSD';
  258. }
  259. else if (stristr($agent,'teleport')) {
  260. $os = 'teleport';
  261. }
  262. else if (stristr($agent,'flashget')) {
  263. $os = 'flashget';
  264. }
  265. else if (stristr($agent,'webzip')) {
  266. $os = 'webzip';
  267. }
  268. else if (stristr($agent,'offline')) {
  269. $os = 'offline';
  270. }
  271. else{
  272. $os = '';
  273. }
  274. return $os;
  275. }
  276. /**
  277. * 获得访问者浏览器
  278. * @return string
  279. */
  280. // public static function browse_info(){
  281. // if (!empty($_SESSION['userAgent'])) {
  282. // $br = $_SESSION['userAgent'];
  283. // if (preg_match('/MSIE/i', $br)) {
  284. // $br = 'MSIE';
  285. // } else if (preg_match('/Firefox/i', $br)) {
  286. // $br = 'Firefox';
  287. // } else if (preg_match('/Chrome/i', $br)) {
  288. // $br = 'Chrome';
  289. // } else if (preg_match('/Safari/i', $br)) {
  290. // $br = 'Safari';
  291. // } else if (preg_match('/Opera/i', $br)) {
  292. // $br = 'Opera';
  293. // } else {
  294. // $br = 'Other';
  295. // }
  296. // return $br;
  297. // } else {
  298. // return 'unknow';
  299. // }
  300. // }
  301. public function browse_info(){
  302. if(!empty($_SERVER['HTTP_USER_AGENT'])){
  303. $br = $_SERVER['HTTP_USER_AGENT'];
  304. if (preg_match('/MSIE/i',$br)) {
  305. $br = 'MSIE';
  306. }
  307. elseif (preg_match('/Firefox/i',$br)) {
  308. $br = 'Firefox';
  309. }
  310. elseif (preg_match('/Chrome/i',$br)) {
  311. $br = 'Chrome';
  312. }
  313. elseif (preg_match('/Safari/i',$br)) {
  314. $br = 'Safari';
  315. }
  316. elseif (preg_match('/Opera/i',$br)) {
  317. $br = 'Opera';
  318. }else {
  319. $br = 'Other';
  320. }
  321. return $br;
  322. }
  323. else{
  324. return "";
  325. }
  326. }
  327. }