User.php 9.1 KB

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