Created
May 19, 2016 17:08
-
-
Save maolion/64670c80733da34424d1e4c37023e374 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as Lodash from 'lodash'; | |
| import { Promise } from 'thenfail'; | |
| import * as Mao from 'mao-cms'; | |
| export function fetchCategories(): any { | |
| return Mao.utils.api | |
| .get(Mao.urls('apis/getCats')) | |
| .then((res) => { | |
| let categories: any[] = []; | |
| let categoriesTree: Dictionary<any> = { | |
| 0: { children: categories } | |
| }; | |
| res.data.forEach((item: any, index: number) => { | |
| let categoryItem = categoriesTree[item.id] = { | |
| id: item.id, | |
| label: item.label, | |
| father_id: item.father_id, | |
| children: null as any | |
| }; | |
| let parent = categoriesTree[item.father_id]; | |
| if (parent) { | |
| if (!parent.children) { | |
| parent.children = []; | |
| } | |
| parent.children.push(categoryItem); | |
| } | |
| }); | |
| return { course: Lodash.assign(Mao.get('data.course', {}), { | |
| categories, | |
| categoriesTree | |
| }) }; | |
| }); | |
| } | |
| export function updateIntro(data: { courseId: number; intro: string; }): any { | |
| return Mao.utils.api | |
| .patch(Mao.urls('apis/updateCourseIntro'), data) | |
| .then((res) => { | |
| let categoriesTree = Lodash.assign({}, Mao.get('data.course.categoriesTree', {})) as Dictionary<any>; | |
| categoriesTree[data.courseId] = Lodash.assign({}, categoriesTree[data.courseId], { | |
| intro: data.intro | |
| }); | |
| return { course: Lodash.assign(Mao.get('data.course', {}), { | |
| categoriesTree | |
| }) }; | |
| //其实上面的代码意思就是 | |
| //Mao.get<any>(`data.course.categoriesTree.${data.id}`).intro = data.intro; | |
| }); | |
| } | |
| export function updateCover(data: { courseId: number; cover: string; }): any { | |
| return Mao.utils.api | |
| .patch(Mao.urls('apis/updateCourseCover'), data) | |
| .then((res) => { | |
| let categoriesTree = Lodash.assign({}, Mao.get('data.course.categoriesTree', {})) as Dictionary<any>; | |
| categoriesTree[data.courseId] = Lodash.assign({}, categoriesTree[data.courseId], { | |
| cover: data.cover | |
| }); | |
| return { course: Lodash.assign(Mao.get('data.course', {}), { | |
| categoriesTree | |
| }) }; | |
| }); | |
| } | |
| export function updateVideo(data: { | |
| courseId: number; | |
| source: string; | |
| startAt: number; | |
| endAt: number; | |
| src: string; | |
| thumbSrc: string; | |
| }): any { | |
| const video = JSON.stringify({ | |
| origin: { | |
| source: data.source, | |
| startAt: data.startAt, | |
| endAt: data.endAt | |
| }, | |
| src: data.src, | |
| thumbSrc: data.thumbSrc | |
| }); | |
| return Mao.utils.api | |
| .patch(Mao.urls('apis/updateCourseVideo'), { | |
| courseId: data.courseId, | |
| video | |
| }) | |
| .then((res) => { | |
| let categoriesTree = Lodash.assign({}, Mao.get('data.course.categoriesTree', {})) as Dictionary<any>; | |
| categoriesTree[data.courseId] = Lodash.assign({}, categoriesTree[data.courseId], { | |
| video | |
| }); | |
| return { course: Lodash.assign(Mao.get('data.course', {}), { | |
| categoriesTree | |
| }) }; | |
| }); | |
| } | |
| export function recordProcessing(record: { id: number; target: string; processing: boolean; }): any { | |
| let processing = Mao.get<Dictionary<any>>('data.course.processing', {}); | |
| if (!processing[record.id]) { | |
| processing[record.id] = {}; | |
| } else { | |
| processing = Lodash.assign({}, processing); | |
| } | |
| processing[record.id][record.target] = record.processing; | |
| return { course: Lodash.assign(Mao.get('data.course', {}), { | |
| processing | |
| }) }; | |
| } | |
| export function processCourseCover(record: { id: number; bucket: string; key: string }): any { | |
| Mao.action('course/recordProcessing', { | |
| id: record.id, | |
| target: 'cover', | |
| processing: true | |
| }); | |
| return Mao.action('getQiniuResourceProcessSeverHost') | |
| .then(() => { | |
| var host = Mao.get<string>('data.qiniuResourceProcessServerHost'); | |
| return Mao.utils.api.patch( | |
| host + '/process-course-cover', | |
| { bucket: record.bucket, key: record.key } | |
| ) | |
| .then((res) => Mao.action('course/updateCover', { courseId: record.id, cover: res.data })); | |
| }) | |
| .handle((err, res) => { | |
| Mao.action('course/recordProcessing', { | |
| id: record.id, | |
| target: 'cover', | |
| processing: !err && res.state == 0 | |
| }); | |
| }); | |
| } | |
| export function processCourseVideo(data: { courseId: number, source: string, startAt: string, endAt: string }) { | |
| Mao.action('course/recordProcessing', { | |
| id: data.courseId, | |
| target: 'video', | |
| processing: true | |
| }); | |
| return Mao.action('getQiniuResourceProcessSeverHost') | |
| .then(() => { | |
| return Mao.utils.api.patch( | |
| Mao.get<string>('data.qiniuResourceProcessServerHost', '/') + '/process-course-video', | |
| data | |
| ) | |
| }) | |
| .then(res => { | |
| if (res.state != 1) { | |
| return res; | |
| } | |
| return Mao.action('course/updateVideo', { | |
| courseId: data.courseId, | |
| source: data.source, | |
| startAt: data.startAt, | |
| endAt: data.endAt, | |
| src: res.data.src, | |
| thumbSrc: res.data.thumbSrc | |
| }) | |
| .then(() => res); | |
| }) | |
| .handle((err, res) => { | |
| Mao.action('course/recordProcessing', { | |
| id: data.courseId, | |
| target: 'video', | |
| processing: !err && res.state == 0 | |
| }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment