User.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\home\controller;
  3. use think\Controller;
  4. use app\user\model\UserSkill;
  5. class User extends Controller {
  6. private $userModel, $skillModel;
  7. public function _initialize(){
  8. $this->skillModel = new UserSkill();
  9. $this->userModel = new \app\user\model\User();
  10. }
  11. public function login()
  12. {
  13. $data = decode($this->request->post());
  14. $info = $this->userModel->loginByUid($data['u']);
  15. $info['s'] = $this->skillModel->getUserSkill($info['i']);
  16. $info['time'] = time();
  17. return json($info);
  18. }
  19. public function getskill(){
  20. $data = decode($this->request->post());
  21. $info = $this->skillModel->getSkillByInfo($data['i']);
  22. return json($info);
  23. }
  24. public function addskill(){
  25. $data = decode($this->request->post());
  26. $info = $this->skillModel->addUserSkill($data['u'], $data['s']);
  27. return json($info);
  28. }
  29. public function other(){
  30. $data = decode($this->request->post());
  31. $res = $this->userModel->getUserById($data['u']);
  32. $path = "load/".$res['short'].'.txt';
  33. $file = fopen($path, "r") or die(json(['error'=>1009]));
  34. $info = fread($file, filesize($path));
  35. fclose($file);
  36. return json(['l'=>$info, 'p'=>$res['praise']]);
  37. }
  38. public function load(){
  39. $data = decode($this->request->post());
  40. $id = $data['u'];
  41. $path = "load/$id.txt";
  42. $res = $this->userModel->getUserByCode($id);
  43. $file = fopen($path, "r") or die(json(['error'=>1009]));
  44. $info = fread($file, filesize($path));
  45. fclose($file);
  46. return json(['l'=>$info, 'p'=>$res['praise']]);
  47. }
  48. public function save(){
  49. $data = decode($this->request->post());
  50. $id = $data['u'];
  51. $path = "load/$id.txt";
  52. $file = fopen($path, 'w') or die(json(['error'=>1009]));
  53. fwrite($file, $data['l']);
  54. fclose($file);
  55. return json(['error'=>0]);
  56. }
  57. public function rand(){
  58. $data = decode($this->request->post());
  59. $id = $this->userModel->randOtherInfoByShort($data['i']);
  60. if(strlen($id) > 10){
  61. $res = $this->userModel->getUserById($id);
  62. }else{
  63. $res = $this->userModel->getUserByCode($id);
  64. }
  65. $path = "load/$id.txt";
  66. $file = fopen($path, "r") or die(json(['error'=>1009]));
  67. $info = fread($file, filesize($path));
  68. fclose($file);
  69. return json(['l'=>$info, 'p'=>$res['praise'], 'i'=>$res['id']]);
  70. }
  71. public function delete(){
  72. $data = decode($this->request->post());
  73. $short = $data['s'];
  74. $path = "load/$short.txt";
  75. $res = unlink($path);
  76. return json(['r'=>(($res)?1:0)]);
  77. }
  78. public function look(){
  79. $res = $this->userModel->lookList();
  80. return json(['l'=>$res]);
  81. }
  82. public function nickname(){
  83. $data = decode($this->request->post());
  84. $nickname = trim($data['n']);
  85. $res = $this->userModel->findUserByName($nickname);
  86. if($res) return json(['error'=>2020]);
  87. $this->userModel->addAndSave(['id'=>$data['u'], 'nickname'=>$nickname]);
  88. return json(['error'=>0]);
  89. }
  90. }