Bläddra i källkod

:sparkles: group sqlite->pg

visuddhinanda@gmail.com 4 år sedan
förälder
incheckning
a530394be8

+ 11 - 0
app/Models/GroupInfo.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class GroupInfo extends Model
+{
+    use HasFactory;
+}

+ 11 - 0
app/Models/GroupMember.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class GroupMember extends Model
+{
+    use HasFactory;
+}

+ 41 - 0
database/migrations/2022_02_05_020339_create_group_infos_table.php

@@ -0,0 +1,41 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateGroupInfosTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('group_infos', function (Blueprint $table) {
+			#使用雪花id
+            $table->bigInteger('id')->primary();
+
+            $table->string('uid',36)->uniqid()->index();
+            $table->string('name',64)->uniqid()->index();
+            $table->string('description',1024)->nullable();
+            $table->integer('status')->default(30);
+            $table->string('owner',36)->index();
+            $table->bigInteger('create_time');
+            $table->bigInteger('modify_time');
+            $table->timestamp('created_at')->useCurrent()->index();
+			$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate()->index();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('group_infos');
+    }
+}

+ 43 - 0
database/migrations/2022_02_05_024244_create_group_members_table.php

@@ -0,0 +1,43 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateGroupMembersTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('group_members', function (Blueprint $table) {
+			#使用雪花id
+            $table->bigInteger('id')->primary();
+
+            $table->string('user_id',36)->index();
+            $table->string('group_id',36)->index();
+            $table->string('group_name',64)->nullable();
+
+            $table->integer('power')->default(1);
+            $table->integer('level')->default(0);
+            $table->integer('status')->default(1)->index();
+            $table->timestamp('created_at')->useCurrent()->index();
+			$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate()->index();
+
+            $table->unique(['user_id','group_id']);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('group_members');
+    }
+}

+ 5 - 5
public/app/config.table.php

@@ -307,8 +307,8 @@ define("_SQLITE_TABLE_GROUP_INFO_", "group_info");
 define("_SQLITE_TABLE_GROUP_MEMBER_", "group_member");
 
 define("_PG_DB_GROUP_", _PDO_DB_DSN_);
-define("_PG_TABLE_GROUP_INFO_", "group_info");
-define("_PG_TABLE_GROUP_MEMBER_", "group_member");
+define("_PG_TABLE_GROUP_INFO_", "group_infos");
+define("_PG_TABLE_GROUP_MEMBER_", "group_members");
 
 # 逐词解析文件索引
 define("_SQLITE_DB_FILEINDEX_", "sqlite:" . __DIR__ . "/../tmp/user/fileindex.db");
@@ -651,9 +651,9 @@ define("_FILE_DB_USER_SHARE_", _PG_DB_USER_SHARE_);
 define("_TABLE_USER_SHARE_", _PG_TABLE_USER_SHARE_);
 
 # 工作组
-define("_FILE_DB_GROUP_", _SQLITE_DB_GROUP_);
-define("_TABLE_GROUP_INFO_", _SQLITE_TABLE_GROUP_INFO_);
-define("_TABLE_GROUP_MEMBER_", _SQLITE_TABLE_GROUP_MEMBER_);
+define("_FILE_DB_GROUP_", _PG_DB_GROUP_);
+define("_TABLE_GROUP_INFO_", _PG_TABLE_GROUP_INFO_);
+define("_TABLE_GROUP_MEMBER_", _PG_TABLE_GROUP_MEMBER_);
 
 # 逐词解析文件索引
 define("_FILE_DB_FILEINDEX_", _SQLITE_DB_FILEINDEX_);

+ 11 - 7
public/app/group/function.php

@@ -8,8 +8,8 @@ require_once '../ucenter/function.php';
 function group_get_name($id)
 {
     if (isset($id)) {
-        PDO_Connect("" . _FILE_DB_GROUP_);
-        $query = "SELECT name FROM group_info  WHERE id=?";
+        PDO_Connect(_FILE_DB_GROUP_);
+        $query = "SELECT name FROM "._TABLE_GROUP_INFO_."  WHERE uid=?";
         $Fetch = PDO_FetchRow($query, array($id));
         if ($Fetch) {
             return $Fetch["name"];
@@ -27,9 +27,9 @@ class GroupInfo
     private $buffer;
     public function __construct()
     {
-        $dns = "" . _FILE_DB_GROUP_;
-        $this->dbh = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
-        $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
+        $dns = _FILE_DB_GROUP_;
+        $this->dbh = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
+        $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $buffer = array();
         $parentId = array();
     }
@@ -43,7 +43,7 @@ class GroupInfo
             return $buffer[$id];
         }
         if ($this->dbh) {
-            $query = "SELECT name FROM group_info WHERE id= ? ";
+            $query = "SELECT name FROM "._TABLE_GROUP_INFO_." WHERE uid= ? ";
             $stmt = $this->dbh->prepare($query);
             $stmt->execute(array($id));
             $user = $stmt->fetch(PDO::FETCH_ASSOC);
@@ -62,6 +62,10 @@ class GroupInfo
 	
 	public function getParentId($id)
     {
+        /*
+        */
+        return 0;
+
         if (empty($id)) {
             return "";
         }
@@ -69,7 +73,7 @@ class GroupInfo
             return $parentId[$id];
         }
         if ($this->dbh) {
-            $query = "SELECT parent FROM group_info WHERE id= ? ";
+            $query = "SELECT parent FROM "._TABLE_GROUP_INFO_." WHERE uid= ? ";
             $stmt = $this->dbh->prepare($query);
             $stmt->execute(array($id));
             $user = $stmt->fetch(PDO::FETCH_ASSOC);

+ 3 - 32
public/app/group/get.php

@@ -11,42 +11,13 @@ $output = array();
 if (isset($_GET["id"])) {
     PDO_Connect("" . _FILE_DB_GROUP_);
     $id = $_GET["id"];
-    $query = "SELECT * FROM group_info  WHERE id = ? ";
+    $query = "SELECT * FROM "._TABLE_GROUP_INFO_."  WHERE uid = ? ";
     $Fetch = PDO_FetchRow($query, array($id));
     if ($Fetch) {
         $output["info"] = $Fetch;
-        if ($Fetch["parent"] == 0) {
-            #顶级组 列出小组
-            $query = "SELECT * FROM group_info  WHERE parent = ? ";
-            $FetchList = PDO_FetchAll($query, array($id));
-            $output["children"] = $FetchList;
-        } else {
-            $output["children"] = array();
-            $query = "SELECT * FROM group_info  WHERE id = ? ";
-            $parent_group = PDO_FetchRow($query, array($Fetch["parent"]));
-            $output["parent"] = $parent_group;
-        }
         #列出组共享资源
-        {
-			/*
-            PDO_Connect("" . _FILE_DB_FILEINDEX_);
-            $query = "SELECT * FROM power  WHERE user = ? ";
-            $fileList = PDO_FetchAll($query, array($id));
-            foreach ($fileList as $key => $value) {
-                # code...
-                $query = "SELECT title FROM fileindex  WHERE id = ? ";
-                $file = PDO_FetchRow($query, array($value["doc_id"]));
-                if ($file) {
-                    $fileList[$key]["title"] = $file["title"];
-                } else {
-                    $fileList[$key]["title"] = "";
-                }
-			}
-			
-			$output["file"] = $fileList;
-			*/
-			$output["file"] =share_res_list_get($id);
-        }
+		$output["file"] =share_res_list_get($id);
+
     }
 }
 echo json_encode($output, JSON_UNESCAPED_UNICODE);

+ 1 - 1
public/app/group/get_name.php

@@ -9,7 +9,7 @@ require_once '../ucenter/function.php';
 $output = array();
 if (isset($_GET["name"])) {
     PDO_Connect("" . _FILE_DB_GROUP_);
-    $query = "SELECT * FROM group_info  WHERE name like ? and parent=0 limit 0,5";
+    $query = "SELECT * FROM "._TABLE_GROUP_INFO_."  WHERE name like ?  limit 50";
     $Fetch = PDO_FetchAll($query, array("%" . $_GET["name"] . "%"));
 
     $user_info = new UserInfo();

+ 5 - 21
public/app/group/group_del.php

@@ -7,35 +7,19 @@ $respond = array("status" => 0, "message" => "");
 if (isset($_COOKIE["userid"]) && isset($_POST["groupid"])) {
     PDO_Connect("" . _FILE_DB_GROUP_);
     #TODO 先查是否有删除权限
-    $query = "SELECT parent from group_info where id=? and owner=? ";
+    $query = "SELECT id from "._TABLE_GROUP_INFO_." where uid=? and owner=? ";
     $gInfo = PDO_FetchRow($query, array($_POST["groupid"], $_COOKIE["userid"]));
     if ($gInfo) {
         #删除group info
-        $query = "DELETE from group_info where id=? and owner=? ";
+        $query = "DELETE from "._TABLE_GROUP_INFO_." where uid=? and owner=? ";
         PDO_Execute($query, array($_POST["groupid"], $_COOKIE["userid"]));
         #删除 组员
-        $query = "DELETE from group_member where group_id=? ";
+        $query = "DELETE from "._TABLE_GROUP_MEMBER_." where group_id=? ";
         PDO_Execute($query, array($_POST["groupid"]));
         #删除到此组的分享
+        $query = "DELETE from "._TABLE_USER_SHARE_." where cooperator_id=? and cooperator_type=1 ";
+        PDO_Execute($query, array($_POST["groupid"]));
 
-        #查询是否有子项目
-        $query = "SELECT id from group_info where parent=? ";
-        $project = PDO_FetchAll($query, array($_POST["groupid"]));
-        if (count($project)) {
-            $arrProject = array();
-            foreach ($project as $key => $value) {
-                # code...
-                $arrProject[] = $value["id"];
-            }
-            $place_holders = implode(',', array_fill(0, count($arrProject), '?'));
-            #删除 parent info
-            $query = "DELETE from group_info where id IN ($place_holders) ";
-            PDO_Execute($query, $arrProject);
-            #删除 parent 组员
-            $query = "DELETE from group_member where group_id IN ($place_holders) ";
-            PDO_Execute($query, $arrProject);
-            #删除到此组的分享
-        }
     } else {
         $respond['status'] = 1;
         $respond['message'] = "no power to delete ";

+ 4 - 4
public/app/group/list.php

@@ -7,12 +7,12 @@ require_once '../public/function.php';
 require_once '../ucenter/function.php';
 
 //列出 我j参与的群组
-PDO_Connect("" . _FILE_DB_GROUP_);
-$query = "SELECT group_name,group_id,power FROM group_member  WHERE level = 0 and user_id=?";
-$Fetch = PDO_FetchAll($query, array($_COOKIE["userid"]));
+PDO_Connect(_FILE_DB_GROUP_);
+$query = "SELECT group_name,group_id,power FROM "._TABLE_GROUP_MEMBER_."  WHERE level = 0 and user_id=?";
+$Fetch = PDO_FetchAll($query, array($_COOKIE["user_uid"]));
 foreach ($Fetch as $key => $value) {
 	# code...
-	$query = "SELECT name FROM group_info  WHERE id=?";
+	$query = "SELECT name FROM "._TABLE_GROUP_INFO_."  WHERE uid=?";
 	$groupInfo = PDO_FetchRow($query, array($value["group_id"]));
 	if($groupInfo){
 		$Fetch[$key]["group_name"]=$groupInfo["name"];

+ 1 - 1
public/app/group/list_member.php

@@ -10,7 +10,7 @@ $output = array();
 if (isset($_GET["id"])) {
     PDO_Connect("" . _FILE_DB_GROUP_);
     $id = $_GET["id"];
-    $query = "SELECT user_id FROM group_member  WHERE group_id = ? ";
+    $query = "SELECT user_id FROM "._TABLE_GROUP_MEMBER_."  WHERE group_id = ? ";
     $output = PDO_FetchAll($query, array($id));
     $userinfo = new UserInfo();
     foreach ($output as $key => $value) {

+ 19 - 41
public/app/group/member_del.php

@@ -4,6 +4,13 @@ require_once "../public/_pdo.php";
 require_once '../public/function.php';
 
 $respond = array("status" => 0, "message" => "");
+set_exception_handler(function($e){
+    $respond['status'] = 1;
+    $respond['message'] = $e->getFile().$e->getLine().$e->getMessage();
+    echo json_encode($respond, JSON_UNESCAPED_UNICODE);
+	exit;
+});
+
 if (!isset($_COOKIE["userid"])) {
     $respond['status'] = 1;
     $respond['message'] = "尚未登录";
@@ -14,28 +21,22 @@ if (isset($_POST["groupid"])) {
     PDO_Connect("" . _FILE_DB_GROUP_);
     $mypower = 100;
     # 先查是否有删人权限
-    $query = "SELECT * from group_info where id=?";
+    #是否是拥有者
+    $query = "SELECT * from "._TABLE_GROUP_INFO_." where uid=?";
     $fc = PDO_FetchRow($query, array($_POST["groupid"]));
     if ($fc) {
-        if ($fc["parent"] == 0) {
-            if ($fc["owner"] == $_COOKIE["userid"]) {
-                $mypower = 0;
-            }
-        } else {
-            $query = "SELECT owner  from group_info where id=?";
-            $g_parent = PDO_FetchRow($query, array($fc["parent"]));
-            if ($g_parent && $g_parent["owner"] == $_COOKIE["userid"]) {
-                $mypower = 0;
-            }
+        if ($fc["owner"] == $_COOKIE["userid"]) {
+            $mypower = 0;
         }
     }
     if ($mypower != 0) {
         #非拥有者,看看是不是管理员
-        $query = "SELECT power from group_member where user_id=? and group_id=? ";
-        $power = PDO_FetchRow($query, array($_COOKIE["userid"], $_POST["groupid"]));
+        $query = "SELECT power from "._TABLE_GROUP_MEMBER_." where user_id=? and group_id=? ";
+        $power = PDO_FetchRow($query, array($_COOKIE["user_uid"], $_POST["groupid"]));
         if ($power) {
             $mypower = (int) $power["power"];
         }
+        #普通成员无权移除他人
         if ($mypower > 1) {
             $respond['status'] = 1;
             $respond['message'] = "no power to remove memeber";
@@ -45,7 +46,7 @@ if (isset($_POST["groupid"])) {
     }
 
     # 查询被删除人的权限
-    $query = "SELECT power from group_member where user_id=? and group_id=? ";
+    $query = "SELECT power from "._TABLE_GROUP_MEMBER_." where user_id=? and group_id=? ";
     $power = PDO_FetchRow($query, array($_POST["userid"], $_POST["groupid"]));
     $userpower = 0;
     if ($power) {
@@ -54,39 +55,16 @@ if (isset($_POST["groupid"])) {
     #操作人的权限不足
     if ($mypower >= $userpower) {
         $respond['status'] = 1;
-        $respond['message'] = "can not removed";
+        $respond['message'] = "can not removed 权限不足";
         echo json_encode($respond, JSON_UNESCAPED_UNICODE);
         exit;
     }
 
-    $query = "SELECT * from group_info where id=?";
-    $fc = PDO_FetchRow($query, array($_POST["groupid"]));
-    if ($fc) {
-        $idList = array();
-        $idList[] = $_POST["userid"];
-        $idList[] = $_POST["groupid"];
-        if ($fc["parent"] == 0) {
-            //group
-            $level = 0;
-            #查询project
-            $query = "SELECT id from group_info where parent=?";
-            $g_project = PDO_FetchAll($query, array($_POST["groupid"]));
-            foreach ($g_project as $key => $parentid) {
-                # code...
-                $idList[] = $parentid["id"];
-            }
-        }
-    }
     #删除
-    $place_holders = implode(',', array_fill(0, count($idList), '?'));
-    $query = "DELETE from group_member where user_id=? and group_id IN ($place_holders)";
-    PDO_Execute($query, $idList);
 
-    if (!$sth || ($sth && $sth->errorCode() != 0)) {
-        $error = PDO_ErrorInfo();
-        $respond['status'] = 1;
-        $respond['message'] = $error[2];
-    }
+    $query = "DELETE from "._TABLE_GROUP_MEMBER_." where user_id=? and group_id =? ";
+    PDO_Execute($query, array($_POST["userid"], $_POST["groupid"]));
+
 } else {
     $respond['status'] = 1;
     $respond['message'] = "参数不足";

+ 39 - 36
public/app/group/member_put.php

@@ -2,56 +2,59 @@
 require_once "../config.php";
 require_once "../public/_pdo.php";
 require_once '../public/function.php';
+require_once __DIR__."/../public/snowflakeid.php";
+$snowflake = new SnowFlakeId();
+
 
 $respond = array("status" => 0, "message" => "");
+
+set_exception_handler(function($e){
+    $respond['status'] = 1;
+    $respond['message'] = $e->getFile().$e->getLine().$e->getMessage();
+    echo json_encode($respond, JSON_UNESCAPED_UNICODE);
+	exit;
+});
 if (isset($_COOKIE["userid"]) && isset($_POST["groupid"])) {
     PDO_Connect("" . _FILE_DB_GROUP_);
-    #TODO 先查是否有加人权限
-    $query = "SELECT power from group_member where user_id=? and group_id=? ";
+    #先查是否有加人权限 0:拥有者,1.管理员
+    $query = "SELECT power from "._TABLE_GROUP_MEMBER_." where user_id=? and group_id=? ";
     $power = PDO_FetchRow($query, array($_COOKIE["userid"], $_POST["groupid"]));
-    if ($power) {
-        if ($power["power"] > 1) {
-            $respond['status'] = 1;
-            $respond['message'] = "no power to add memeber";
-            echo json_encode($respond, JSON_UNESCAPED_UNICODE);
-            exit;
-        }
+    if (!$power || $power["power"] > 1) {
+        $respond['status'] = 1;
+        $respond['message'] = "no power to add memeber";
+        echo json_encode($respond, JSON_UNESCAPED_UNICODE);
+        exit;
     }
 
-    $query = "SELECT * from group_info where id=?";
-    $fc = PDO_FetchRow($query, array($_POST["groupid"]));
-    if ($fc) {
-        if ($fc["parent"] == 0) {
-            $level = 0;
-        } else {
-            $level = 1;
-            #子小组要插入两条记录 第一条插入父层级
-            $query = "SELECT * from group_info where id=?";
-            $g_parent = PDO_FetchRow($query, array($fc["id"]));
-            $query = "INSERT INTO group_member (  user_id  , group_id  , power , group_name , level ,  status )  VALUES  (  ? , ? , ? , ? , ?  ,? ) ";
-            $sth = $PDO->prepare($query);
-            $sth->execute(array($_POST["userid"], $fc["parent"], 2, $$g_parent["name"], 0, 1));
-            $respond = array("status" => 0, "message" => "");
-            if (!$sth || ($sth && $sth->errorCode() != 0)) {
-                $error = PDO_ErrorInfo();
-                $respond['status'] = 1;
-                $respond['message'] = $error[2];
-            }
-        }
-    }
-    #查询这个
-    $query = "SELECT * from group_info where id=?";
+    #查询组信息
+    $query = "SELECT * from "._TABLE_GROUP_INFO_." where uid=?";
     $g_curr = PDO_FetchRow($query, array($_POST["groupid"]));
+    if (!$g_curr) {
+        $respond['status'] = 1;
+        $respond['message'] = '组不存在';
+        echo json_encode($respond, JSON_UNESCAPED_UNICODE);
+        exit;
+    }
+#查询是否有重复记录
+    $query = "SELECT * from "._TABLE_GROUP_MEMBER_." where user_id=? and group_id=?";
+    $isExist = PDO_FetchRow($query, array($_POST["userid"], $_POST["groupid"]));
+    if ($isExist) {
+        $respond['status'] = 1;
+        $respond['message'] = '组员已经存在';
+        echo json_encode($respond, JSON_UNESCAPED_UNICODE);
+        exit;
+    }
 
-    $query = "INSERT INTO group_member (  user_id  , group_id  , power , group_name , level ,  status )
-		VALUES  (  ? , ? , ? , ? , ?  ,? ) ";
+    $query = "INSERT INTO "._TABLE_GROUP_MEMBER_." (id,  user_id  , group_id  , power , group_name , level ,  status )
+		VALUES  (  ? , ? , ? , ? , ?  ,? ,?) ";
     $sth = $PDO->prepare($query);
-    $sth->execute(array($_POST["userid"], $_POST["groupid"], 2, $g_curr["name"], $level, 1));
-    $respond = array("status" => 0, "message" => "");
+    $sth->execute(array($snowflake->id(),$_POST["userid"], $_POST["groupid"], 2, $g_curr["name"], 0, 1));
     if (!$sth || ($sth && $sth->errorCode() != 0)) {
         $error = PDO_ErrorInfo();
         $respond['status'] = 1;
         $respond['message'] = $error[2];
+        echo json_encode($respond, JSON_UNESCAPED_UNICODE);
+        exit;
     }
 }
 echo json_encode($respond, JSON_UNESCAPED_UNICODE);

+ 39 - 31
public/app/group/my_group_put.php

@@ -3,41 +3,49 @@
 require_once "../config.php";
 require_once "../public/_pdo.php";
 require_once '../public/function.php';
+require_once __DIR__."/../public/snowflakeid.php";
+$snowflake = new SnowFlakeId();
+
+$respond = array("status" => 0, "message" => "ok");
+set_exception_handler(function($e){
+    $respond['status'] = 1;
+    $respond['message'] = $e->getFile().$e->getLine().$e->getMessage();
+	exit;
+});
+
 
-$respond = array("status" => 0, "message" => "");
 if (isset($_COOKIE["userid"])) {
     PDO_Connect(_FILE_DB_GROUP_);
-	#先查询是否有重复的组名
-	$query = "SELECT id FROM group_info  WHERE name = ? ";
-    $Fetch = PDO_FetchRow($query, array($_POST["name"]));
-	if ($Fetch) {
-		$respond['status'] = 1;
-        $respond['message'] = "错误:有相同的组名称,请选择另一个名称。";
-		echo json_encode($respond, JSON_UNESCAPED_UNICODE);
-		exit;
-	}
-    $query = "INSERT INTO group_info ( id,  parent  , name  , description ,  status , owner ,create_time )
-	                       VALUES  ( ?, ? , ? , ? , ? , ?  ,? ) ";
-    $sth = $PDO->prepare($query);
-    $newid = UUID::v4();
-    $sth->execute(array($newid, $_POST["parent"], $_POST["name"], "", 1, $_COOKIE["userid"], mTime()));
-    $respond = array("status" => 0, "message" => "");
-    if (!$sth || ($sth && $sth->errorCode() != 0)) {
-        $error = PDO_ErrorInfo();
-        $respond['status'] = 1;
-        $respond['message'] = $error[2];
-    }
+    try{
+        $PDO->beginTransaction();
+        #先查询是否有重复的组名
+        $query = "SELECT id FROM "._TABLE_GROUP_INFO_."  WHERE name = ? ";
+        $Fetch = PDO_FetchRow($query, array($_POST["name"]));
+        if ($Fetch) {
+            $respond['status'] = 1;
+            $respond['message'] = "错误:有相同的组名称,请选择另一个名称。";
+            echo json_encode($respond, JSON_UNESCAPED_UNICODE);
+            exit;
+        }
+        $query = "INSERT INTO "._TABLE_GROUP_INFO_." ( id, uid  , name  , description  , owner ,create_time,modify_time )
+                            VALUES  ( ?, ? , ? , ? , ? , ?  ,? ) ";
+        $sth = $PDO->prepare($query);
+        $newid = UUID::v4();
+        $sth->execute(array($snowflake->id(),$newid,  $_POST["name"], "",  $_COOKIE["user_uid"], mTime(), mTime()));
 
-	#将创建者添加到成员中
-    $query = "INSERT INTO group_member (  user_id  , group_id  , power , group_name , level ,  status )
-		VALUES  (  ? , ? , ? , ? , ?  ,? ) ";
-    $sth = $PDO->prepare($query);
-    $sth->execute(array($_COOKIE["userid"], $newid, 0, $_POST["name"], 0, 1));
-    $respond = array("status" => 0, "message" => "");
-    if (!$sth || ($sth && $sth->errorCode() != 0)) {
-        $error = PDO_ErrorInfo();
-        $respond['status'] = 1;
-        $respond['message'] = $error[2];
+
+        #将创建者添加到成员中
+        $query = "INSERT INTO "._TABLE_GROUP_MEMBER_." (id,  user_id  , group_id  , power , group_name , level ,  status )
+            VALUES  ( ? , ? , ? , ? , ? , ?  ,? ) ";
+        $sth = $PDO->prepare($query);
+        $sth->execute(array($snowflake->id(),$_COOKIE["user_uid"], $newid, 0, $_POST["name"], 0, 1));
+        $PDO->commit();    
+    }catch (Exception $e) {
+        $PDO->rollBack();
+    	$respond['status']=1;
+        $respond['message']="Failed: " . $e->getMessage();
+        echo json_encode($respond, JSON_UNESCAPED_UNICODE);
     }
+
 }
 echo json_encode($respond, JSON_UNESCAPED_UNICODE);

+ 2 - 2
public/app/share/function.php

@@ -17,7 +17,7 @@ function share_res_list_get($userid,$res_type=-1){
 	# 找我加入的群
 	$dbhGroup = new PDO(_FILE_DB_GROUP_, _DB_USERNAME_, _DB_PASSWORD_);
     $dbhGroup->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
-	$query = "SELECT group_id from group_member where user_id = ?  limit 200";
+	$query = "SELECT group_id from "._TABLE_GROUP_MEMBER_." where user_id = ?  limit 500";
 	$stmtGroup = $dbhGroup->prepare($query);
 	$stmtGroup->execute(array($userid));
 	$my_group = $stmtGroup->fetchAll(PDO::FETCH_ASSOC);
@@ -146,7 +146,7 @@ function share_get_res_power($userid,$res_id){
 		# 找我加入的群
 		$dbhGroup = new PDO(_FILE_DB_GROUP_, _DB_USERNAME_, _DB_PASSWORD_);
 		$dbhGroup->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
-		$query = "SELECT group_id from group_member where user_id = ?  limit 100";
+		$query = "SELECT group_id from "._TABLE_GROUP_MEMBER_." where user_id = ?  limit 500";
 		$stmtGroup = $dbhGroup->prepare($query);
 		$stmtGroup->execute(array($userid));
 		$my_group = $stmtGroup->fetchAll(PDO::FETCH_ASSOC);

+ 2 - 2
public/app/share/share.js

@@ -174,7 +174,7 @@ function username_search(keyword, type) {
 					for (const iterator of result) {
 						html +=
 							"<li onclick=\"user_selected('" +
-							iterator.id +
+							iterator.uid +
 							"','" +
 							iterator.username +
 							"',0)\">" +
@@ -209,7 +209,7 @@ function username_search(keyword, type) {
 					for (const iterator of result) {
 						html +=
 							"<li onclick=\"user_selected('" +
-							iterator.id +
+							iterator.uid +
 							"','" +
 							iterator.name +
 							"',1)\">" +

+ 9 - 1
public/documents/testing.md

@@ -62,4 +62,12 @@
    1. 创建[v]
    2. 删除(无)
    3. 修改[v]
-   4. 协作[v]
+   4. 协作[v]
+9. 群组
+   1. 创建[v] (不允许建立同名群组)
+   2. 删除[v] (同时删除共享资源链接)
+   3. 修改(无)
+   4. 加成员[v]
+   5. 删除成员[v] (只有群主可以删除其他成员)
+   6. 分享资源到这个群组
+10. 

+ 7 - 0
v1/scripts/install4.sh

@@ -0,0 +1,7 @@
+#!/bin/sh
+
+date
+
+php ./migrations/20220205084100_group_info_copy.php
+php ./migrations/20220205092400_group_member.php
+date

+ 181 - 0
v1/scripts/migrations/20220205084100_group_info_copy.php

@@ -0,0 +1,181 @@
+<?php
+/*
+迁移  article 库
+从旧数据表中提取数据插入到新的表
+插入时用uuid判断是否曾经插入
+曾经插入就不插入了
+*/
+require_once __DIR__."/../../../public/app/config.php";
+require_once __DIR__."/../../../public/app/public/snowflakeid.php";
+
+set_exception_handler(function($e){
+	fwrite(STDERR,"error-msg:".$e->getMessage().PHP_EOL);
+	fwrite(STDERR,"error-file:".$e->getFile().PHP_EOL);
+	fwrite(STDERR,"error-line:".$e->getLine().PHP_EOL);
+	exit;
+});
+$start = time();
+# 雪花id
+$snowflake = new SnowFlakeId();
+
+$fpError = fopen(__DIR__.'/log/'.basename($_SERVER['PHP_SELF'],'.php').".err.data.csv",'w');
+
+#user info
+$user_db=_FILE_DB_USERINFO_;#user数据库
+$user_table=_TABLE_USER_INFO_;#user表名
+
+# 
+$src_db = _SQLITE_DB_GROUP_;#源数据库
+$src_table = _SQLITE_TABLE_GROUP_INFO_;#源表名
+
+$dest_db = _PG_DB_GROUP_;#目标数据库
+$dest_table = _PG_TABLE_GROUP_INFO_;#目标表名
+
+fwrite(STDOUT,"migarate group info".PHP_EOL);
+#打开user数据库
+$PDO_USER = new PDO($user_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_USER->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open user table".PHP_EOL);
+
+#打开源数据库
+$PDO_SRC = new PDO($src_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_SRC->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open src table".PHP_EOL);
+
+#打开目标数据库
+$PDO_DEST = new PDO($dest_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_DEST->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open dest table".PHP_EOL);
+
+$queryInsert = "INSERT INTO ".$dest_table." 
+								(
+                                    id,
+									uid,
+									name,
+									description,
+									status,
+									owner,
+									create_time,
+									modify_time,
+									updated_at,
+									created_at) 
+									VALUES (? , ? , ? , ?, ? , ? , ? , ? , ? , ? )";
+$stmtDEST = $PDO_DEST->prepare($queryInsert);
+
+$commitData = [];
+$allInsertCount = 0;
+$allSrcCount = 0;
+$count = 0;
+
+#从user数据表中读取
+$query = "SELECT id  FROM ".$user_table." WHERE userid = ? ";
+$stmtUser = $PDO_USER->prepare($query);
+
+#从源数据表中读取
+$query = "SELECT *  FROM ".$src_table;
+$stmtSrc = $PDO_SRC->prepare($query);
+$stmtSrc->execute();
+while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
+	$allSrcCount++;
+
+    if($srcData["owner"]=='visuddhinanda'){
+		$srcData["owner"] = 'ba5463f3-72d1-4410-858e-eadd10884713';
+	}
+    if($srcData["owner"]=='test7'){
+		$srcData["owner"] = '6bd2f4d7-d970-419c-8ee5-f4bac42f4bc1';
+	}
+    if($srcData["owner"]=='Dhammadassi'){
+		$srcData["owner"] = 'd8538ebd-d369-4777-b99a-3ccb1aff8bfc';
+	}
+    if($srcData["owner"]=='pannava'){
+		$srcData["owner"] = '4db550c4-bc1b-43f2-a518-2740cb478f37';
+	}
+    if($srcData["owner"]=='NST'){
+		$srcData["owner"] = '5c23e629-56a3-48e9-97c7-2af73b59c3b9';
+	}
+    if($srcData["owner"]=='viranyani'){
+		$srcData["owner"] = 'C1AB2ABF-EAA8-4EEF-B4D9-3854321852B4';
+	}
+    if($srcData["owner"]=='test6'){
+		$srcData["owner"] = 'f81c7140-64b4-4025-b58c-45a3b386324a';
+	}
+	if($srcData["owner"]=='test28'){
+		$srcData["owner"] = 'df0ad9bc-c0cd-4cd9-af05-e43d23ed57f0';
+	}
+	if($srcData["owner"]=='290fd808-2f46-4b8c-b300-0367badd67ed'){
+		$srcData["owner"] = 'f81c7140-64b4-4025-b58c-45a3b386324a';
+	}
+	if($srcData["owner"]=='BA837178-9ABD-4DD4-96A0-D2C21B756DC4'){
+		$srcData["owner"] = 'ba5463f3-72d1-4410-858e-eadd10884713';
+	}
+	$stmtUser->execute(array($srcData["owner"]));
+	$userId = $stmtUser->fetch(PDO::FETCH_ASSOC);
+	if(!$userId){
+		fwrite(STDERR,time()."error,no user id {$srcData["owner"]}".PHP_EOL);
+		continue;
+	}
+	if(strlen($srcData["owner"])>36){
+		fwrite(STDERR,time().",error,user id too long {$srcData["owner"]}".PHP_EOL);
+		continue;	
+	}
+    if(empty($srcData["name"])){
+		fwrite(STDERR,time().",error,name is empty {$srcData["id"]}".PHP_EOL);
+		continue;
+	}
+
+    $srcData["status"] = 30;
+
+    if(empty($srcData["modify_time"])){
+        $srcData["modify_time"] = $srcData["create_time"];
+    }
+
+    if($srcData["create_time"] < 15987088320){
+        $srcData["create_time"] *= 1000;
+    }
+    if($srcData["modify_time"] < 15987088320){
+        $srcData["modify_time"] *= 1000;
+    }
+	//查询是否已经插入
+	$queryExsit = "SELECT id  FROM ".$dest_table." WHERE uid = ? ";
+	$getExist = $PDO_DEST->prepare($queryExsit);
+	$getExist->execute(array($srcData["id"]));
+	$exist = $getExist->fetch(PDO::FETCH_ASSOC);
+	if($exist){
+		continue;
+	}
+	#插入目标表
+	$created_at = date("Y-m-d H:i:s.",$srcData["create_time"]/1000).($srcData["create_time"]%1000)." UTC";
+	$updated_at = date("Y-m-d H:i:s.",$srcData["modify_time"]/1000).($srcData["modify_time"]%1000)." UTC";
+	$commitData = array(
+            $snowflake->id(),
+			$srcData["id"],
+			$srcData["name"],
+			$srcData["description"],
+			$srcData["status"],
+			$srcData["owner"],
+			$srcData["create_time"],
+			$srcData["modify_time"],
+			$created_at,
+			$updated_at
+		);
+	$stmtDEST->execute($commitData);
+
+	$count++;	
+	$allInsertCount++;
+
+
+	if($count ==10000){
+		#10000行输出log 一次
+		echo "finished $count".PHP_EOL;
+		$count=0;
+	}	
+}
+
+fwrite(STDOUT,"insert done $allInsertCount in $allSrcCount ".PHP_EOL) ;
+fwrite(STDOUT, "all done in ".(time()-$start)."s".PHP_EOL);
+
+fclose($fpError);
+
+
+
+

+ 170 - 0
v1/scripts/migrations/20220205092400_group_member.php

@@ -0,0 +1,170 @@
+<?php
+/*
+迁移  article 库
+从旧数据表中提取数据插入到新的表
+插入时用uuid判断是否曾经插入
+曾经插入就不插入了
+*/
+require_once __DIR__."/../../../public/app/config.php";
+require_once __DIR__."/../../../public/app/public/snowflakeid.php";
+
+set_exception_handler(function($e){
+	fwrite(STDERR,"error-msg:".$e->getMessage().PHP_EOL);
+	fwrite(STDERR,"error-file:".$e->getFile().PHP_EOL);
+	fwrite(STDERR,"error-line:".$e->getLine().PHP_EOL);
+	exit;
+});
+$start = time();
+# 雪花id
+$snowflake = new SnowFlakeId();
+
+$fpError = fopen(__DIR__.'/log/'.basename($_SERVER['PHP_SELF'],'.php').".err.data.csv",'w');
+
+#user info
+$user_db=_FILE_DB_USERINFO_;#user数据库
+$user_table=_TABLE_USER_INFO_;#user表名
+
+# 
+$src_db = _SQLITE_DB_GROUP_;#源数据库
+$src_table = _SQLITE_TABLE_GROUP_MEMBER_;#源表名
+
+$dest_db = _PG_DB_GROUP_;#目标数据库
+$dest_table = _PG_TABLE_GROUP_MEMBER_;#目标表名
+
+fwrite(STDOUT,"migarate group member ".PHP_EOL);
+#打开user数据库
+$PDO_USER = new PDO($user_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_USER->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open user table".PHP_EOL);
+
+#打开源数据库
+$PDO_SRC = new PDO($src_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_SRC->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open src table".PHP_EOL);
+
+#打开目标数据库
+$PDO_DEST = new PDO($dest_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_DEST->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open dest table".PHP_EOL);
+
+$queryInsert = "INSERT INTO ".$dest_table." 
+								(
+                                    id,
+									user_id,
+									group_id,
+									group_name,
+									power,
+									level,
+									status,
+									created_at,
+									updated_at) 
+									VALUES (? , ? , ? , ?, ? , ? ,?  , now() , now() )";
+$stmtDEST = $PDO_DEST->prepare($queryInsert);
+
+$commitData = [];
+$allInsertCount = 0;
+$allSrcCount = 0;
+$count = 0;
+
+#从user数据表中读取
+$query = "SELECT id  FROM ".$user_table." WHERE userid = ? ";
+$stmtUser = $PDO_USER->prepare($query);
+
+    
+#从源数据表中读取
+$query = "SELECT *  FROM ".$src_table;
+$stmtSrc = $PDO_SRC->prepare($query);
+$stmtSrc->execute();
+while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
+	$allSrcCount++;
+
+    $queryExist = "SELECT *  FROM ".$dest_table." where user_id=? and group_id=? ";
+    $stmtExist = $PDO_DEST->prepare($queryExist);
+    $stmtExist->execute([$srcData["user_id"],$srcData["group_id"]]);
+    $isExist = $stmtExist->fetch(PDO::FETCH_ASSOC);
+	if($isExist){
+        echo "record is existed id=".$srcData['id'].PHP_EOL;
+        continue;
+    }
+
+{
+    if($srcData["user_id"]=='visuddhinanda'){
+		$srcData["user_id"] = 'ba5463f3-72d1-4410-858e-eadd10884713';
+	}
+    if($srcData["user_id"]=='test7'){
+		$srcData["user_id"] = '6bd2f4d7-d970-419c-8ee5-f4bac42f4bc1';
+	}
+    if($srcData["user_id"]=='Dhammadassi'){
+		$srcData["user_id"] = 'd8538ebd-d369-4777-b99a-3ccb1aff8bfc';
+	}
+    if($srcData["user_id"]=='pannava'){
+		$srcData["user_id"] = '4db550c4-bc1b-43f2-a518-2740cb478f37';
+	}
+    if($srcData["user_id"]=='NST'){
+		$srcData["user_id"] = '5c23e629-56a3-48e9-97c7-2af73b59c3b9';
+	}
+    if($srcData["user_id"]=='viranyani'){
+		$srcData["user_id"] = 'C1AB2ABF-EAA8-4EEF-B4D9-3854321852B4';
+	}
+    if($srcData["user_id"]=='test6'){
+		$srcData["user_id"] = 'f81c7140-64b4-4025-b58c-45a3b386324a';
+	}
+	if($srcData["user_id"]=='test28'){
+		$srcData["user_id"] = 'df0ad9bc-c0cd-4cd9-af05-e43d23ed57f0';
+	}
+	if($srcData["user_id"]=='290fd808-2f46-4b8c-b300-0367badd67ed'){
+		$srcData["user_id"] = 'f81c7140-64b4-4025-b58c-45a3b386324a';
+	}
+	if($srcData["user_id"]=='BA837178-9ABD-4DD4-96A0-D2C21B756DC4'){
+		$srcData["user_id"] = 'ba5463f3-72d1-4410-858e-eadd10884713';
+	}
+	$stmtUser->execute(array($srcData["user_id"]));
+	$userId = $stmtUser->fetch(PDO::FETCH_ASSOC);
+	if(!$userId){
+		fwrite(STDERR,time()."error,no user id {$srcData["user_id"]}".PHP_EOL);
+		continue;
+	}
+}
+
+
+	if(strlen($srcData["user_id"])>36){
+		fwrite(STDERR,time().",error,user id too long {$srcData["user_id"]}".PHP_EOL);
+		continue;	
+	}
+
+	#插入目标表
+	$commitData = array(
+            $snowflake->id(),
+			$srcData["user_id"],
+			$srcData["group_id"],
+			$srcData["group_name"],
+			$srcData["power"],
+			$srcData["level"],
+			$srcData["status"]
+		);
+    try{
+        $stmtDEST->execute($commitData);
+    }catch (Exception $e) {
+        echo "Failed: " . $e->getMessage();
+    }
+	
+
+	$count++;	
+	$allInsertCount++;
+
+
+	if($count ==10000){
+		#10000行输出log 一次
+		echo "finished $count".PHP_EOL;
+		$count=0;
+	}	
+}
+
+fwrite(STDOUT,"insert done $allInsertCount in $allSrcCount ".PHP_EOL) ;
+fwrite(STDOUT, "all done in ".(time()-$start)."s".PHP_EOL);
+
+fclose($fpError);
+
+
+
+