Commit 0f90b990 authored by 杨培's avatar 杨培

完美添加讲师等体验

为上传池表添加迁移
parent ba273b41
......@@ -12,6 +12,9 @@ use Yii;
*/
class ClassesTeacher extends \yii\db\ActiveRecord
{
public static $lastErrors;
/**
* @inheritdoc
*/
......@@ -41,4 +44,29 @@ class ClassesTeacher extends \yii\db\ActiveRecord
'teacher_id' => 'Teacher ID',
];
}
public static function makeRelation($classId, $teacherId)
{
$model = new self();
$model->teacher_id = $teacherId;
$model->class_id = $classId;
if ($model->save()) {
return true;
} else {
self::$lastErrors = $model->errors;
return false;
}
}
public static function getTeachers($classId)
{
$obj = self::find();
$teacherTable = Teacher::tableName();
return $obj->select('t.id,t.user_id,t.name,t.avatar')->join('LEFT JOIN', $teacherTable.' t', 't.id=c.teacher_id')
->from(self::tableName().' c')->where(['c.class_id'=>$classId])->asArray()->all();
}
}
......@@ -17,6 +17,9 @@ use Yii;
*/
class Teacher extends \yii\db\ActiveRecord
{
public static $lastErrors;
/**
* @inheritdoc
*/
......@@ -34,7 +37,7 @@ class Teacher extends \yii\db\ActiveRecord
[['user_id', 'name', 'avatar', 'intro', 'created_at', 'updated_at'], 'required'],
[['user_id', 'created_at', 'updated_at'], 'integer'],
[['intro'], 'string'],
[['name'], 'string', 'max' => 64],
[['name'], 'string', 'min'=>2, 'max' => 64],
[['avatar'], 'string', 'max' => 255],
];
}
......@@ -54,4 +57,22 @@ class Teacher extends \yii\db\ActiveRecord
'updated_at' => 'Updated At',
];
}
public static function simpleAddTeacher($name)
{
$model = new self();
$model->user_id = 0;
$model->name = $name;
$model->avatar = 'avatar/f6/98/c6/_tjy7DgC0641.jpg';
$model->intro = '这个家伙很懒,什么也没有写。';
$model->created_at = time();
$model->updated_at = time();
if ($model->save()) {
return $model->toArray();
} else {
self::$lastErrors = $model->errors;
return false;
}
}
}
<?php
use yii\db\Migration;
class m161009_110732_upload_pools_init extends Migration
{
public $table = 'upload_pools';
public function up()
{
$this->createTable($this->table, [
'`uuid` char(36) NOT NULL',
'skey' => $this->string(255)->notNull()->comment('文件skey,如"public:avatar/fc/abc.jpg"'),
'filesize' => $this->integer(10)->unsigned()->notNull(),
'w' => $this->smallInteger(5)->unsigned()->defaultValue(0)->comment('图片宽'),
'h' => $this->smallInteger(5)->unsigned()->defaultValue(0)->comment('图片高'),
'`status` tinyint(1) NOT NULL DEFAULT \'0\' COMMENT \'文件状态: 0未认领,1已认领,2已删除(弃用)\'',
'upload_time' => $this->integer(10)->unsigned()->notNull()->comment('上传时间'),
'PRIMARY KEY (`uuid`)',
'KEY `status` (`status`,`upload_time`)',
'KEY `skey` (`skey`)'
], 'ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT=\'上传文件状态标记表\'');
}
public function down()
{
$this->dropTable($this->table);
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
......@@ -25,7 +25,7 @@ class MyCourseController extends \yii\web\Controller
//图片上传Token
//$imgName = 'preview/'.$qiniu->genRandomDir().'/'.Yii::$app->security->generateRandomString(20);
$imgPath = 'preview/'.$qiniu->generateRandomFilePath('$(min)$(sec)$(ext)', 8);
$thumbToken = $qiniu->getUploadToken('public', $imgPath, 3600);
$thumbToken = $qiniu->getUploadToken('public', $imgPath);
//视频上传Token
//$videoName = 'source/'.$qiniu->genRandomDir().'/'.Yii::$app->security->generateRandomString(20);
......
......@@ -3,6 +3,7 @@
namespace frontend\controllers;
use common\models\Teacher;
use common\models\ClassesTeacher;
class TeacherManagerController extends \yii\web\Controller
{
......@@ -12,11 +13,49 @@ class TeacherManagerController extends \yii\web\Controller
{
return $this->render('index');
}
public function actionSimpleTeacherList()
{
//Todo: 假设数据
$classId = 1;
$response = \Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_JSON;
return ClassesTeacher::getTeachers($classId);
}
/**
* 添加讲师
*/
public function actionAddTeacher()
{
$model = new Teacher();
//Todo: 假设数据
$uid = 18;
$classId = 1;
$response = \Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_JSON;
$name = strip_tags(\Yii::$app->request->post('name'));
$teacher = Teacher::simpleAddTeacher($name);
if ($teacher) {
ClassesTeacher::makeRelation($classId, $teacher['id']);
return [
'teacher_id' => $teacher['id'],
'name' => $teacher['name']
];
} else {
return ['error'=>join("\\n", Teacher::$lastErrors)];
}
}
public function actionTest()
{
$data = ClassesTeacher::getTeachers(1);
print_r($data);
}
}
......@@ -26,7 +26,7 @@ $this->registerCss('[v-cloak] {display:none;}');
<div class="tit">讲师</div>
<div class="checkbox" v-cloak>
<label v-for="(index, item) in teachers" :class="{'m-l-10':index!=0}">
<input name="teacher[]" type="checkbox" value="{{item.id}}">{{item.name}}
<input name="teacher[]" type="checkbox" value="{{item.id}}" v-model="checkedTeachers">{{item.name}}
</label>
<div class="d-i-b" v-if="atState">
......@@ -48,13 +48,13 @@ $this->registerCss('[v-cloak] {display:none;}');
<div class="checkbox rules-set fl">
<label class="clearfix">
<input name="vip_price" type="checkbox" class="fl" value="1"><span>会员价</span> <input type="text" value="price" class="input-r-control"> <span>元</span>
<!--<input name="vip_price" type="checkbox" class="fl" value="1">--><span>会员价</span> <input type="text" name="price" class="input-r-control text-center" value="0"> <span>元</span>
</label>
</div>
<div class="checkbox rules-set m-l-20 fl">
<label class="clearfix">
<input name="free_time" type="checkbox" class="fl"><span>免费试看</span> <input type="text" class="input-r-control"> <span>分钟</span>
<!--<input name="free_time" type="checkbox" class="fl">--><span>免费试看</span> <input type="text" name="free_time" class="input-r-control text-center" value="0"> <span>分钟</span>
</label>
</div>
</div>
......
......@@ -6,20 +6,25 @@ var videoUpload = require('../components/uploader/video-upload.vue');
var thumbUpload = require('../components/uploader/thumb-upload.vue');
var form = document.forms["coursePublish"];
var layer = require("layer-js");
//$.ajaxSetup({
// beforeSend: function() {
// if (this.type == "POST") {
// this.data += "&_csrf-frontend=" + encodeURIComponent($("meta[name='csrf-token']").attr("content"));
// }
// }
//});
var vm = new Vue({
el: "#publishMain",
data: {
vuid: "",
previewKey: "",
teachers: [
{id:1, name:"杨佩佩"},
{id:2, name:"马骏"},
{id:3, name:"花木兰"},
{id:4, name:"张海"}
],
teachers: [],
atState: 0,
addTeacherName: ""
addTeacherName: "",
checkedTeachers: []
},
components: {
videoUpload: videoUpload,
......@@ -39,15 +44,30 @@ var vm = new Vue({
var self = this;
//添加讲师
$.post("/teacher-manager/add-teacher", {
name: self.addTeacherName
}, function(t) {
if (!t.error) {
self.loadTeacherList();
self.addTeacherName = "";
self.atState = 0;
self.checkedTeachers.push(t.teacher_id.toString());
} else {
layer.alert("添加讲师失败!\n" + t.error, {icon:5, title:"上传失败"});
}
}, "json");
},
loadTeacherList: function() {
$.getJSON("/teacher-manager/simple-teacher-list", function(t) {
vm.teachers = t;
});
}
},
ready: function() {
var self = this;
this.loadTeacherList();
this.$on("video-uploaded", function(name, data) {
self.vuid = data.uuid;
......@@ -65,7 +85,12 @@ require.ensure("jquery-validation", function() {
$(form).validate({
debug: true,
rules: {
required: true
name: {
required: true
}
},
submitHandler: function() {
return false;
}
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment