User.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. //老用户找回账户
  76. public function load_back(){
  77. $data = decode($this->request->post());
  78. $id = $data['u'];
  79. $ext_id = $data['e'];
  80. $res = $this->userModel->getUserByCode($id);
  81. if(!empty($res['ext_id']))
  82. {
  83. return json(['error'=>1023]);
  84. }
  85. if(empty($res)) return json(['error'=>1023]);
  86. $path = "load/".$res['id'].".txt";
  87. if(!is_file($path)) $path = "load/".$res['short'].".txt";
  88. if(!is_file($path))
  89. return json(['error'=>1009]);
  90. $file = fopen($path, "r") or die(json(['error'=>1009]));
  91. $info = fread($file, filesize($path));
  92. fclose($file);
  93. //更新sdk账号id至新的账号
  94. $this->userModel->update(["ext_id"=>""])->where(['ext_id'=>$ext_id]);
  95. $this->userModel->update(["ext_id"=>$ext_id])->where(["id"=>$res['id']]);
  96. return json(['l'=>$info, 'p'=>$res['praise']]);
  97. }
  98. public function save(){
  99. $data = decode($this->request->post());
  100. $id = $data['u'];
  101. $version = $data['v'];
  102. $path = "load/$id.txt";
  103. $res = $this->userModel->getUserById($id);
  104. if(empty($res)) return json(['error'=>1023]);
  105. if(!$res['nickname'] && isset($data['n']))
  106. {
  107. $this->userModel->where('id', $id)->update(['nickname'=>$data['n']]);
  108. }
  109. $oldversion = $res['version'];
  110. if($version > $oldversion && $oldversion > 0){
  111. // 读取旧文件
  112. $old_file = fopen($path, "r") or die(json(['error'=>1009]));
  113. $info = fread($old_file, filesize($path));
  114. fclose($old_file);
  115. // 备份旧文件
  116. $backup_file = fopen("backup/$id-$oldversion.txt", 'w') or die(json(['error'=>1009]));
  117. fwrite($backup_file, $info);
  118. fclose($backup_file);
  119. $this->userModel->update(['version'=>$version], ['id'=>$id]);
  120. }
  121. $file = fopen($path, 'w') or die(json(['error'=>1009]));
  122. fwrite($file, $data['l']);
  123. fclose($file);
  124. return json(['error'=>0]);
  125. }
  126. public function rand(){
  127. $data = decode($this->request->post());
  128. $my_id = isset($data['i']) ? $data['i'] : 0;
  129. $id = $this->userModel->randOtherInfoById($data['i']);
  130. if(strlen($id) > 10){
  131. $res = $this->userModel->getUserById($id);
  132. }else{
  133. $res = $this->userModel->getUserByCode($id);
  134. }
  135. $path = "load/$id.txt";
  136. $file = fopen($path, "r") or die(json(['error'=>1009]));
  137. $info = fread($file, filesize($path));
  138. fclose($file);
  139. return json(['l'=>$info, 'p'=>$res['praise'], 'i'=>$res['id']]);
  140. }
  141. public function delete(){
  142. $data = decode($this->request->post());
  143. $id = $data['s'];
  144. $path = "load/$id.txt";
  145. $res = unlink($path);
  146. return json(['r'=>(($res)?1:0)]);
  147. }
  148. public function look(){
  149. $res = $this->userModel->lookList();
  150. return $res;
  151. }
  152. public function nickname(){
  153. $data = decode($this->request->post());
  154. $nickname = trim($data['n']);
  155. $res = $this->userModel->findUserByName($nickname);
  156. if($res) return json(['error'=>2020]);
  157. $this->userModel->addAndSave(['id'=>$data['u'], 'nickname'=>$nickname]);
  158. return json(['error'=>0]);
  159. }
  160. public function phone()
  161. {
  162. $data = decode($this->request->post());
  163. $phone = trim($data['p']);
  164. $zone = trim($data['z']);
  165. $code = trim($data['c']);
  166. $user_id = $data['u'];
  167. $api = 'https://webapi.sms.mob.com';
  168. // 发送验证码
  169. $response = $this->post_request( $api . '/sms/verify', array(
  170. 'appkey' => '20348b3a105da',
  171. 'phone' => $phone,
  172. 'zone' => $zone,
  173. 'code' => $code,
  174. ) );
  175. $response = json_decode($response, true);
  176. if($response['status'] != 200)
  177. {
  178. return json(['error'=>1010, 'msg'=>$response]);
  179. }
  180. $res = $this->userModel->where('mobile', $phone)->find();
  181. if($res)
  182. {
  183. $path = "load/".$res['id'].".txt";
  184. if(!is_file($path)) $path = "load/".$res['short'].".txt";
  185. if(!is_file($path))
  186. return json(['error'=>1009]);
  187. $file = fopen($path, "r") or die(json(['error'=>1009]));
  188. $info = fread($file, filesize($path));
  189. fclose($file);
  190. return json(['l'=>$info, 'p'=>$res['praise'], 'mobile'=>$phone]);
  191. }
  192. $this->userModel->where('id', $user_id)->update(['mobile'=>$phone]);
  193. return json(['mobile'=>$phone, 'res'=>$response]);
  194. }
  195. /**
  196. * 发起一个post请求到指定接口
  197. *
  198. * @param string $api 请求的接口
  199. * @param array $params post参数
  200. * @param int $timeout 超时时间
  201. * @return string 请求结果
  202. */
  203. private function post_request( $api, array $params = array(), $timeout = 30 ) {
  204. $ch = curl_init();
  205. curl_setopt( $ch, CURLOPT_URL, $api );
  206. // 以返回的形式接收信息
  207. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  208. // 设置为POST方式
  209. curl_setopt( $ch, CURLOPT_POST, 1 );
  210. curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
  211. // 不验证https证书
  212. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
  213. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
  214. curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
  215. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  216. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
  217. 'Accept: application/json',
  218. ) );
  219. // 发送数据
  220. $response = curl_exec( $ch );
  221. // 不要忘记释放资源
  222. curl_close( $ch );
  223. return $response;
  224. }
  225. }