User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\newhome\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['id'].'.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. if(strlen($id) > 10){
  42. $res = $this->userModel->getUserById($id);
  43. }else{
  44. $res = $this->userModel->getUserByCode($id);
  45. }
  46. if(empty($res)) return json(['error'=>1023]);
  47. $path = "load/".$res['id'].".txt";
  48. if(!is_file($path)) $path = "load/".$res['short'].".txt";
  49. $file = fopen($path, "r") or die(json(['error'=>1009]));
  50. $info = fread($file, filesize($path));
  51. fclose($file);
  52. return json(['l'=>$info, 'p'=>$res['praise']]);
  53. }
  54. public function save(){
  55. $data = decode($this->request->post());
  56. $id = $data['u'];
  57. $version = $data['v'];
  58. $path = "load/$id.txt";
  59. $res = $this->userModel->getUserById($id);
  60. if(empty($res)) return json(['error'=>1023]);
  61. $oldversion = isset($res['version'])?$res['version']:1;
  62. if($version > $oldversion){
  63. // 读取旧文件
  64. $old_file = fopen($path, "r") or die(json(['error'=>1009]));
  65. $info = fread($old_file, filesize($path));
  66. fclose($old_file);
  67. // 备份旧文件
  68. $backup_file = fopen("backup/$id-$oldversion.txt", 'w') or die(json(['error'=>1009]));
  69. fwrite($backup_file, $info);
  70. fclose($backup_file);
  71. $this->userModel->update(['version'=>$version], ['id'=>$id]);
  72. }
  73. $file = fopen($path, 'w') or die(json(['error'=>1009]));
  74. fwrite($file, $data['l']);
  75. fclose($file);
  76. return json(['error'=>0]);
  77. }
  78. public function rand(){
  79. $data = decode($this->request->post());
  80. $id = $this->userModel->randOtherInfoById($data['i']);
  81. if(strlen($id) > 10){
  82. $res = $this->userModel->getUserById($id);
  83. }else{
  84. $res = $this->userModel->getUserByCode($id);
  85. }
  86. $path = "load/$id.txt";
  87. $file = fopen($path, "r") or die(json(['error'=>1009]));
  88. $info = fread($file, filesize($path));
  89. fclose($file);
  90. return json(['l'=>$info, 'p'=>$res['praise'], 'i'=>$res['id']]);
  91. }
  92. public function delete(){
  93. $data = decode($this->request->post());
  94. $id = $data['s'];
  95. $path = "load/$id.txt";
  96. $res = unlink($path);
  97. return json(['r'=>(($res)?1:0)]);
  98. }
  99. public function look(){
  100. $res = $this->userModel->lookList();
  101. return $res;
  102. }
  103. public function nickname(){
  104. $data = decode($this->request->post());
  105. $nickname = trim($data['n']);
  106. $res = $this->userModel->findUserByName($nickname);
  107. if($res) return json(['error'=>2020]);
  108. $this->userModel->addAndSave(['id'=>$data['u'], 'nickname'=>$nickname]);
  109. return json(['error'=>0]);
  110. }
  111. }