自己写的,刚刚接触,可能不是最好的方案。
数据库
collection:user
/* 1 */
{
"_id" : ObjectId("5a3d4a7d94e7d58cd34d0823"),
"openid" : "",
"session_key" : {
"session_key" : "",
"deadline" : ""
},
"unionid" : "",
"username" : "Mandy",
"avatar_url" : "",
"token" : "mandytoken",
"truename" : "",
"school" : "",
"school_account" : "",
"school_password" : "",
"major" : "",
"dorm" : ""
}
/* 2 */
{
"_id" : ObjectId("5a3d4a9294e7d58cd34d0824"),
"openid" : "",
"session_key" : {
"session_key" : "",
"deadline" : ""
},
"unionid" : "",
"username" : "MengLi",
"avatar_url" : "",
"token" : "menglitoken",
"truename" : "",
"school" : "",
"school_account" : "",
"school_password" : "",
"major" : "",
"dorm" : ""
}
/* 3 */
{
"_id" : ObjectId("5a3d4aae94e7d58cd34d0825"),
"openid" : "",
"session_key" : {
"session_key" : "",
"deadline" : ""
},
"unionid" : "",
"username" : "LuCheng",
"avatar_url" : "",
"token" : "lctoken",
"truename" : "",
"school" : "",
"school_account" : "",
"school_password" : "",
"major" : "",
"dorm" : ""
}
collection:vote
/* 1 */
{
"_id" : ObjectId("5a3d4afe94e7d58cd34d0826"),
"object_id" : "001",
"qrcode_url" : "",
"type" : "vote",
"owner_token" : "mandytoken",
"is_school" : true,
"title" : "今天你投票了吗投票?",
"create_time" : "",
"start_time" : "",
"end_time" : "",
"question_list" : [
{
"question" : "你今天几岁啦?",
"type" : "TYPE",
"max_choose_num" : 1.0,
"options" : [
{
"option_content" : [
"YES",
"NO"
],
"selection_token_list" : [
"menglitoken",
"lctoken"
]
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("5a3f34fb11a778666d54b005"),
"object_id" : "002",
"qrcode_url" : "",
"type" : "vote",
"owner_token" : "mandytoken",
"is_school" : true,
"title" : "吃饭投票",
"create_time" : "",
"start_time" : "",
"end_time" : "",
"question_list" : [
{
"question" : "什么菜?",
"type" : "TYPE",
"max_choose_num" : 1.0,
"options" : [
{
"option_content" : [
"苦瓜",
"豆腐"
],
"selection_token_list" : [
"menglitoken",
"lctoken"
]
}
]
}
]
}
/* 3 */
{
"_id" : ObjectId("5a3f368211a778666d54b009"),
"object_id" : "003",
"qrcode_url" : "",
"type" : "vote",
"owner_token" : "lctoken",
"is_school" : true,
"title" : "睡觉投票",
"create_time" : "",
"start_time" : "",
"end_time" : "",
"question_list" : [
{
"question" : "几点睡?",
"type" : "TYPE",
"max_choose_num" : 1.0,
"options" : [
{
"option_content" : [
"22:00",
"23:00"
],
"selection_token_list" : [
"menglitoken",
"mandytoken"
]
}
]
}
]
}
collection: selection //用户与其所投的投票活动的关系
/* 1 */
{
"_id" : ObjectId("5a3d4a7d94e7d58cd34d0823"),
"openid" : "",
"session_key" : {
"session_key" : "",
"deadline" : ""
},
"unionid" : "",
"username" : "Mandy",
"avatar_url" : "",
"token" : "mandytoken",
"truename" : "",
"school" : "",
"school_account" : "",
"school_password" : "",
"major" : "",
"dorm" : ""
}
/* 2 */
{
"_id" : ObjectId("5a3d4a9294e7d58cd34d0824"),
"openid" : "",
"session_key" : {
"session_key" : "",
"deadline" : ""
},
"unionid" : "",
"username" : "MengLi",
"avatar_url" : "",
"token" : "menglitoken",
"truename" : "",
"school" : "",
"school_account" : "",
"school_password" : "",
"major" : "",
"dorm" : ""
}
/* 3 */
{
"_id" : ObjectId("5a3d4aae94e7d58cd34d0825"),
"openid" : "",
"session_key" : {
"session_key" : "",
"deadline" : ""
},
"unionid" : "",
"username" : "LuCheng",
"avatar_url" : "",
"token" : "lctoken",
"truename" : "",
"school" : "",
"school_account" : "",
"school_password" : "",
"major" : "",
"dorm" : ""
}
api:获取用户参与的投票
php7 mongodb & 扩展
PSSSSSSS:以下的变量声明得有点杂乱...
GET
请求,传参token
url:xxx.xxx.xxx.xxx/yyy/yyy/vote/yyy/join_list?token=menglitoken
public function join_list()
{
try {
// 拿到参数
$token = $_GET['token'];
// echo $token;
// 数据库查询
$mongo = new MongoDB\Driver\Manager('mongodb://localhost:27017');
// 1. 根据token去selection找object_id
$filter = ["user_token" => $token];
$options = [
"projection" => ["join_time" => 1, "object_id" => 1, "_id" => 0],
];
$query = new MongoDB\Driver\Query($filter, $options);
$res1 = $mongo->executeQuery('voteDB.selection', $query); // 对象
// var_dump($cursor);
// 打印 测试这阶段的操作 ——成功
$cursor1 = $res1->toArray(); // 数组
// cursor2 : stdClass -> Array 转换类型
$cursor2 = json_encode($cursor1); // json 数组
// printf($cursor2);
//-------------------------------------------------------------
// 2. 根据object_id 去vote 找 time, title, fist_question
// true 表示把其数组子对象全部decode
$cursor3 = json_decode($cursor2, true);
$arr = Array();
for ($i=0; $i<count($cursor3); $i++){
// var_dump($cursor3[$i]);
$arr[$i] = $cursor3[$i]['object_id'];
};
// printf(json_encode($arr));
$filter2 = ["object_id" =>['$in' => $arr]];
$options2 = [
"projection" => ["owner_token"=>1,"object_id" => 1, "title"=>1, "question_list"=>1,"_id" => 0],
];
$query2 = new MongoDB\Driver\Query($filter2, $options2);
$res2 = $mongo->executeQuery('voteDB.vote', $query2);
$res2 = json_encode($res2->toArray());
// printf($res2);
// ---------------------------------------------
// 根据owner_token 去 user 找 username
$cursor4 = json_decode($res2, true);
$arr1 = Array();
for ($i=0; $i < count($cursor4); $i++) {
$arr1[$i] = $cursor4[$i]["owner_token"];
};
$filter3 = ["token" => ['$in' => $arr1]];
$options3 = [
"projection" => ["token" =>1,"username"=>1, "_id"=>0],
];
$query3 = new MongoDB\Driver\Query($filter3, $options3);
$res3 = $mongo->executeQuery('voteDB.user', $query3);
$res3 = json_encode($res3->toArray());
// printf($res3);
$res4 = json_decode($res3, true);
// ---------------整合---------------------------------
$object_list = Array();
for ($i=0; $i < count($cursor4); $i++) {
$object_list[$i] =Array();
$object_list[$i]["username"] = $res4[$i]["username"];
$object_list[$i]["title"] = $cursor4[$i]["title"];
$object_list[$i]["time"] = $cursor3[$i]["join_time"];
$object_list[$i]["first_question"] = $cursor4[$i]["question_list"][0]["question"];
};
printf(json_encode($object_list));
} catch (\think\Exception\HttpResponseException $e) {
throw $e; //系统异常无法处理,直接向上抛出
} catch (Exception $e) {
$message = $e->getMessage();
echo Json::jsonError(1, $message); //程序内部异常输出到message中
}
}