123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- namespace app\newhome\controller;
- use think\Controller;
- use app\user\model\UserSkill;
- class User extends Controller {
-
- private $userModel, $skillModel;
-
- public function _initialize(){
- $this->skillModel = new UserSkill();
- $this->userModel = new \app\user\model\User();
-
- }
-
- public function login()
- {
- $data = decode($this->request->post());
-
- $info = $this->userModel->loginByUid($data['u'], isset($data['e']) ? $data['e'] : '');
- $info['s'] = $this->skillModel->getUserSkill($info['i']);
- // $last_login = strtotime($info['t']);
- // $now = time();
- // $offline_time = $now - $last_login > 21600 ? 21600 : $now - $last_login;
- // $info['time'] = $offline_time; //暂时限制离线时间上限,修正一个客户端版本的错误问题
- $info['time'] = time();
-
- return json($info);
- }
- public function sdk_login()
- {
- $data = decode($this->request->post());
- $arr = explode("|", $data['e']);
- $sdk_id = $arr[0];
- $ts = $arr[1];
- $sign = $arr[2];
- if($sign != md5($sdk_id.$ts."CzNLahsrSMBcSJF6"))
- {
- return json(['error'=>1009]);
- }
- $info = $this->userModel->loginBySdk($sdk_id);
- $info['time'] = time();
- return json($info);
- }
-
- public function getskill(){
- $data = decode($this->request->post());
-
- $info = $this->skillModel->getSkillByInfo($data['i']);
- return json($info);
- }
-
- public function addskill(){
- $data = decode($this->request->post());
-
- $info = $this->skillModel->addUserSkill($data['u'], $data['s']);
- return json($info);
- }
-
- public function other(){
- $data = decode($this->request->post());
-
- $res = $this->userModel->getUserById($data['u']);
- $path = "load/".$res['id'].'.txt';
-
- $file = fopen($path, "r") or die(json(['error'=>1009]));
- $info = fread($file, filesize($path));
- fclose($file);
-
- return json(['l'=>$info, 'p'=>$res['praise']]);
- }
-
- public function load(){
- $data = decode($this->request->post());
-
- $id = $data['u'];
-
- if(strlen($id) > 10){
- $res = $this->userModel->getUserById($id);
- }else{
- $res = $this->userModel->getUserByCode($id);
- }
-
- if(empty($res)) return json(['error'=>1023]);
- $path = "load/".$res['id'].".txt";
-
- if(!is_file($path)) $path = "load/".$res['short'].".txt";
- if(!is_file($path))
- return json(['error'=>1009]);
-
- $file = fopen($path, "r") or die(json(['error'=>1009]));
- $info = fread($file, filesize($path));
- fclose($file);
-
- return json(['l'=>$info, 'p'=>$res['praise']]);
- }
- //老用户找回账户
- public function load_back(){
- $data = decode($this->request->post());
- $id = $data['u'];
- if(!empty($data['e']))
- {
- $arr = explode("|", $data['e']);
- if(count($arr) < 2)
- {
- return json(['error'=>1009]);
- }
- $sdk_id = $arr[0];
- $ts = $arr[1];
- $sign = $arr[2];
- if($sign != md5($sdk_id.$ts."CzNLahsrSMBcSJF6"))
- {
- return json(['error'=>1009]);
- }
- }
- $res = $this->userModel->getUserByCode($id);
- // if(!empty($res['ext_id']))
- // {
- // return json(['error'=>1023]);
- // }
- if(empty($res)) return json(['error'=>1023]);
- $path = "load/".$res['id'].".txt";
- if(!is_file($path)) $path = "load/".$res['short'].".txt";
- if(!is_file($path))
- return json(['error'=>1009]);
- $file = fopen($path, "r") or die(json(['error'=>1009]));
- $info = fread($file, filesize($path));
- fclose($file);
- //更新sdk账号id至新的账号
- if(!empty($sdk_id))
- {
- $this->userModel->where(['ext_id'=>$sdk_id])->update(["ext_id"=>""]);
- $this->userModel->where(["id"=>$res['id']])->update(["ext_id"=>$sdk_id]);
- }
- return json(['l'=>$info, 'p'=>$res['praise']]);
- }
- public function save(){
- $data = decode($this->request->post());
- $id = $data['u'];
- $version = $data['v'];
- $path = "load/$id.txt";
- $res = $this->userModel->getUserById($id);
- if(empty($res)) return json(['error'=>1023]);
- if(!$res['nickname'] && isset($data['n']))
- {
- $this->userModel->where('id', $id)->update(['nickname'=>$data['n']]);
- }
- $oldversion = $res['version'];
- if($version > $oldversion && $oldversion > 0){
- // 读取旧文件
- $old_file = fopen($path, "r") or die(json(['error'=>1009]));
- $info = fread($old_file, filesize($path));
- fclose($old_file);
- // 备份旧文件
- $backup_file = fopen("backup/$id-$oldversion.txt", 'w') or die(json(['error'=>1009]));
- fwrite($backup_file, $info);
- fclose($backup_file);
- $this->userModel->update(['version'=>$version], ['id'=>$id]);
- }
- $file = fopen($path, 'w') or die(json(['error'=>1009]));
- fwrite($file, $data['l']);
- fclose($file);
- return json(['error'=>0]);
- }
-
- public function rand(){
- $data = decode($this->request->post());
- $my_id = isset($data['i']) ? $data['i'] : 0;
- $id = $this->userModel->randOtherInfoById($data['i']);
- if(strlen($id) > 10){
- $res = $this->userModel->getUserById($id);
- }else{
- $res = $this->userModel->getUserByCode($id);
- }
-
- $path = "load/$id.txt";
- $file = fopen($path, "r") or die(json(['error'=>1009]));
- $info = fread($file, filesize($path));
- fclose($file);
-
- return json(['l'=>$info, 'p'=>$res['praise'], 'i'=>$res['id']]);
- }
-
- public function delete(){
- $data = decode($this->request->post());
-
- $id = $data['s'];
-
- $path = "load/$id.txt";
- $res = unlink($path);
-
- return json(['r'=>(($res)?1:0)]);
- }
-
- public function look(){
- $res = $this->userModel->lookList();
-
- return $res;
- }
-
- public function nickname(){
- $data = decode($this->request->post());
-
- $nickname = trim($data['n']);
- $res = $this->userModel->findUserByName($nickname);
- if($res) return json(['error'=>2020]);
- $this->userModel->addAndSave(['id'=>$data['u'], 'nickname'=>$nickname]);
-
- return json(['error'=>0]);
- }
- public function phone()
- {
- $data = decode($this->request->post());
- $phone = trim($data['p']);
- $zone = trim($data['z']);
- $code = trim($data['c']);
- $user_id = $data['u'];
- $api = 'https://webapi.sms.mob.com';
- // 发送验证码
- $response = $this->post_request( $api . '/sms/verify', array(
- 'appkey' => '20348b3a105da',
- 'phone' => $phone,
- 'zone' => $zone,
- 'code' => $code,
- ) );
- $response = json_decode($response, true);
- if($response['status'] != 200)
- {
- return json(['error'=>1010, 'msg'=>$response]);
- }
- $res = $this->userModel->where('mobile', $phone)->find();
- if($res)
- {
- $path = "load/".$res['id'].".txt";
- if(!is_file($path)) $path = "load/".$res['short'].".txt";
- if(!is_file($path))
- return json(['error'=>1009]);
- $file = fopen($path, "r") or die(json(['error'=>1009]));
- $info = fread($file, filesize($path));
- fclose($file);
- return json(['l'=>$info, 'p'=>$res['praise'], 'mobile'=>$phone]);
- }
- $this->userModel->where('id', $user_id)->update(['mobile'=>$phone]);
- return json(['mobile'=>$phone, 'res'=>$response]);
- }
- public function idcard()
- {
- $data = decode($this->request->post());
- $user_id = trim($data['u']);
- $real_name = trim($data['r']);
- $id_number = trim($data['n']);
- $res = $this->userModel->where('id', $user_id)->find();
- if($res)
- {
- $this->userModel->where('id', $user_id)->update(['real_name'=>$real_name, 'id_number'=>$id_number]);
- return json(['r'=>$real_name, 'n'=>$id_number]);
- }
- return json(['r'=>'', 'n'=>'']);
- }
- /**
- * 发起一个post请求到指定接口
- *
- * @param string $api 请求的接口
- * @param array $params post参数
- * @param int $timeout 超时时间
- * @return string 请求结果
- */
- private function post_request( $api, array $params = array(), $timeout = 30 ) {
- $ch = curl_init();
- curl_setopt( $ch, CURLOPT_URL, $api );
- // 以返回的形式接收信息
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
- // 设置为POST方式
- curl_setopt( $ch, CURLOPT_POST, 1 );
- curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
- // 不验证https证书
- curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
- curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
- curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
- curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
- 'Accept: application/json',
- ) );
- // 发送数据
- $response = curl_exec( $ch );
- // 不要忘记释放资源
- curl_close( $ch );
- return $response;
- }
- }
|