User.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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'], isset($data['e']) ? $data['e'] : '');
  15. $info['s'] = $this->skillModel->getUserSkill($info['i']);
  16. // $last_login = strtotime($info['t']);
  17. // $now = time();
  18. // $offline_time = $now - $last_login > 21600 ? 21600 : $now - $last_login;
  19. // $info['time'] = $offline_time; //暂时限制离线时间上限,修正一个客户端版本的错误问题
  20. $info['time'] = time();
  21. return json($info);
  22. }
  23. public function sdk_login()
  24. {
  25. $data = decode($this->request->post());
  26. $arr = explode("|", $data['e']);
  27. $sdk_id = $arr[0];
  28. $ts = $arr[1];
  29. $sign = $arr[2];
  30. if($sign != md5($sdk_id.$ts."CzNLahsrSMBcSJF6"))
  31. {
  32. return json(['error'=>1009]);
  33. }
  34. $info = $this->userModel->loginBySdk($sdk_id);
  35. $info['time'] = time();
  36. return json($info);
  37. }
  38. public function getskill(){
  39. $data = decode($this->request->post());
  40. $info = $this->skillModel->getSkillByInfo($data['i']);
  41. return json($info);
  42. }
  43. public function addskill(){
  44. $data = decode($this->request->post());
  45. $info = $this->skillModel->addUserSkill($data['u'], $data['s']);
  46. return json($info);
  47. }
  48. public function other(){
  49. $data = decode($this->request->post());
  50. $res = $this->userModel->getUserById($data['u']);
  51. $path = "load/".$res['id'].'.txt';
  52. $file = fopen($path, "r") or die(json(['error'=>1009]));
  53. $info = fread($file, filesize($path));
  54. fclose($file);
  55. return json(['l'=>$info, 'p'=>$res['praise']]);
  56. }
  57. public function load(){
  58. $data = decode($this->request->post());
  59. $id = $data['u'];
  60. if(strlen($id) > 10){
  61. $res = $this->userModel->getUserById($id);
  62. }else{
  63. $res = $this->userModel->getUserByCode($id);
  64. }
  65. if(empty($res)) return json(['error'=>1023]);
  66. $path = "load/".$res['id'].".txt";
  67. if(!is_file($path)) $path = "load/".$res['short'].".txt";
  68. if(!is_file($path))
  69. return json(['error'=>1009]);
  70. $file = fopen($path, "r") or die(json(['error'=>1009]));
  71. $info = fread($file, filesize($path));
  72. fclose($file);
  73. return json(['l'=>$info, 'p'=>$res['praise']]);
  74. }
  75. public function save(){
  76. $data = decode($this->request->post());
  77. $id = $data['u'];
  78. $version = $data['v'];
  79. $path = "load/$id.txt";
  80. $res = $this->userModel->getUserById($id);
  81. if(empty($res)) return json(['error'=>1023]);
  82. if(!$res['nickname'] && isset($data['n']))
  83. {
  84. $this->userModel->where('id', $id)->update(['nickname'=>$data['n']]);
  85. }
  86. $oldversion = $res['version'];
  87. if($version > $oldversion && $oldversion > 0){
  88. // 读取旧文件
  89. $old_file = fopen($path, "r") or die(json(['error'=>1009]));
  90. $info = fread($old_file, filesize($path));
  91. fclose($old_file);
  92. // 备份旧文件
  93. $backup_file = fopen("backup/$id-$oldversion.txt", 'w') or die(json(['error'=>1009]));
  94. fwrite($backup_file, $info);
  95. fclose($backup_file);
  96. $this->userModel->update(['version'=>$version], ['id'=>$id]);
  97. }
  98. $file = fopen($path, 'w') or die(json(['error'=>1009]));
  99. fwrite($file, $data['l']);
  100. fclose($file);
  101. return json(['error'=>0]);
  102. }
  103. public function rand(){
  104. $data = decode($this->request->post());
  105. $my_id = isset($data['i']) ? $data['i'] : 0;
  106. $id = $this->userModel->randOtherInfoById($data['i']);
  107. if(strlen($id) > 10){
  108. $res = $this->userModel->getUserById($id);
  109. }else{
  110. $res = $this->userModel->getUserByCode($id);
  111. }
  112. $path = "load/$id.txt";
  113. $file = fopen($path, "r") or die(json(['error'=>1009]));
  114. $info = fread($file, filesize($path));
  115. fclose($file);
  116. return json(['l'=>$info, 'p'=>$res['praise'], 'i'=>$res['id']]);
  117. }
  118. public function delete(){
  119. $data = decode($this->request->post());
  120. $id = $data['s'];
  121. $path = "load/$id.txt";
  122. $res = unlink($path);
  123. return json(['r'=>(($res)?1:0)]);
  124. }
  125. public function look(){
  126. $res = $this->userModel->lookList();
  127. return $res;
  128. }
  129. public function nickname(){
  130. $data = decode($this->request->post());
  131. $nickname = trim($data['n']);
  132. $res = $this->userModel->findUserByName($nickname);
  133. if($res) return json(['error'=>2020]);
  134. $this->userModel->addAndSave(['id'=>$data['u'], 'nickname'=>$nickname]);
  135. return json(['error'=>0]);
  136. }
  137. public function phone()
  138. {
  139. $data = decode($this->request->post());
  140. $phone = trim($data['p']);
  141. $zone = trim($data['z']);
  142. $code = trim($data['c']);
  143. $user_id = $data['u'];
  144. $api = 'https://webapi.sms.mob.com';
  145. // 发送验证码
  146. $response = $this->post_request( $api . '/sms/verify', array(
  147. 'appkey' => '20348b3a105da',
  148. 'phone' => $phone,
  149. 'zone' => $zone,
  150. 'code' => $code,
  151. ) );
  152. $response = json_decode($response, true);
  153. if($response['status'] != 200)
  154. {
  155. return json(['error'=>1010, 'msg'=>$response]);
  156. }
  157. $res = $this->userModel->where('mobile', $phone)->find();
  158. if($res)
  159. {
  160. $path = "load/".$res['id'].".txt";
  161. if(!is_file($path)) $path = "load/".$res['short'].".txt";
  162. if(!is_file($path))
  163. return json(['error'=>1009]);
  164. $file = fopen($path, "r") or die(json(['error'=>1009]));
  165. $info = fread($file, filesize($path));
  166. fclose($file);
  167. return json(['l'=>$info, 'p'=>$res['praise'], 'mobile'=>$phone]);
  168. }
  169. $this->userModel->where('id', $user_id)->update(['mobile'=>$phone]);
  170. return json(['mobile'=>$phone, 'res'=>$response]);
  171. }
  172. /**
  173. * 发起一个post请求到指定接口
  174. *
  175. * @param string $api 请求的接口
  176. * @param array $params post参数
  177. * @param int $timeout 超时时间
  178. * @return string 请求结果
  179. */
  180. private function post_request( $api, array $params = array(), $timeout = 30 ) {
  181. $ch = curl_init();
  182. curl_setopt( $ch, CURLOPT_URL, $api );
  183. // 以返回的形式接收信息
  184. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  185. // 设置为POST方式
  186. curl_setopt( $ch, CURLOPT_POST, 1 );
  187. curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
  188. // 不验证https证书
  189. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
  190. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
  191. curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
  192. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  193. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
  194. 'Accept: application/json',
  195. ) );
  196. // 发送数据
  197. $response = curl_exec( $ch );
  198. // 不要忘记释放资源
  199. curl_close( $ch );
  200. return $response;
  201. }
  202. }