User.php 11 KB

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