Browse Source

commit user gift

superbee 7 years ago
parent
commit
5829ffc96e
2 changed files with 19 additions and 7 deletions
  1. 5 0
      application/newhome/controller/Index.php
  2. 14 7
      application/newhome/controller/User.php

+ 5 - 0
application/newhome/controller/Index.php

@@ -19,6 +19,11 @@ class Index extends Controller {
 		return json(["l"=>$data]);
 	}
 	
+	public function gift(){
+		$data = Db::name('gift')->field("id, packid, pack p, diamond d, coin g, flower f, dress c, pit s, elf a, starttime, endtime")->select();
+		return json(['l'=>$data]);
+	}
+	
 	public function robot(){
 		$data = decode($this->request->post());
 		

+ 14 - 7
application/newhome/controller/User.php

@@ -80,19 +80,26 @@ class User extends Controller {
 		
 		$id = $data['u'];
 		$version = $data['v'];
-		$info = $data['l'];
+		$path = "load/$id.txt";
 		
 		$res = $this->userModel->getUserById($id);
+		if(empty($res)) return json(['error'=>1023]);
 		
-		if($version > $res['version']){
-			$path = "load/$id.txt";
+		if($version > $oldversion = $res['version']){
+			// 读取旧文件
+			$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]);
-		}else{
-			$path = "backup/$id-$version.txt";
-		}		
+		}
 		
 		$file = fopen($path, 'w') or die(json(['error'=>1009]));
-		fwrite($file, $info);
+		fwrite($file, $data['l']);
 		fclose($file);
 		
 		return json(['error'=>0]);