前几天接到了个一个大学毕业的毕设软件,整体报价1000块,需要用到laravel和vue,于是就有了以下的简单界面,整体项目花了2天时间,加上数据录入等等
首先呢,采用前后端分离,vue作为前端界面,也就一个页面,但是需要登录和注册, 前端通过vuex做为状态记录,记录到cookie的Authorization里,所有的请求直接访问后端lavarel
laravel如果发现token失效,那么返回状态401, 告诉前端登录失效,直接跳转到login,注册和登录得接口通过laravle得passport来取得
php artisan passport:install
具体passport 参考下列文章
https://laravelacademy.org/post/19485.html
核心的几个点:
1. 数据库的设计, 因为是MITI职业心理,他有8个维度,于是有了下面比较丑陋的数据库结构
Schema::create('question_part', function (Blueprint $table) {
$table->id();
$table->string('title')->default('');
$table->timestamps();
});
//
Schema::create('questions', function (Blueprint $table) {
$table->integer('id')->unsigned();
$table->primary('id');
$table->string('title');
$table->unsignedBigInteger('part_id')->index()->nullable();
$table->timestamps();
$table->foreign('part_id')->references('id')->on('question_part')->onDelete('cascade');
});
Schema::create('q_answers', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->unsignedInteger('q_id')->index()->nullable();
$table->enum('type', ['A', 'B']);
$table->unsignedTinyInteger('E')->default(0);
$table->unsignedTinyInteger('I')->default(0);
$table->unsignedTinyInteger('S')->default(0);
$table->unsignedTinyInteger('N')->default(0);
$table->unsignedTinyInteger('T')->default(0);
$table->unsignedTinyInteger('F')->default(0);
$table->unsignedTinyInteger('J')->default(0);
$table->unsignedTinyInteger('P')->default(0);
$table->timestamps();
$table->foreign('q_id')->references('id')->on('questions')->onDelete('cascade');
});
2. 最后为了计算测试结果
$result = DB::table('user_answers')
->select(DB::raw('sum(E) as E, sum(I) as I, sum(S) as S,sum(N) as N ,sum(T) as T, sum(F) as F ,sum(J) as J, sum(P) as P'))
->where('user_id', $uid)
->groupBy('user_id')
->first();
//$a = json_decode(json_encode($result), true);
// var_dump($a);
// asort($a);
// $keys = array_keys($a);
// $final = [$keys[7], $keys[6], $keys[5], $keys[4]];
// return implode('', $final);
$final = [];
if ($result->E > $result->I) {
$final[] = 'E';
} else {
$final[] = 'I';
}
if ($result->S > $result->N) {
$final[] = 'S';
} else {
$final[] = 'N';
}
if ($result->T > $result->F) {
$final[] = 'T';
} else {
$final[] = 'F';
}
if ($result->J > $result->P) {
$final[] = 'J';
} else {
$final[] = 'P';
}
好吧,1000块钱也不好赚。