WillController.class.php 10.0 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/4/9
 * Time: 9:33
 */

namespace Portal\Controller;

use Common\Controller\HomebaseController;

class WillController extends HomebaseController {

    protected $posts_model;
    protected $will_mission_model;
    protected $will_mission_apply_model;
    protected $will_events_model;
    protected $will_events_apply_model;
    protected $will_produce_model;
    protected $will_user_apply_model;

    function _initialize() {
        parent::_initialize();
        $this->posts_model = D("Portal/Posts");
        $this->will_mission_model = D("Common/WillMission");
        $this->will_mission_apply_model = D("Common/WillMissionApply");
        $this->will_events_model = D("Common/WillEvents");
        $this->will_events_apply_model = D("Common/WillEventsApply");
        $this->will_produce_model = D("Common/WillProduce");
        $this->will_user_apply_model = D("Common/WillUserApply");
    }

    // 入会说明
    public function explain() {
        $post=$this->posts_model->where(array("id=10"))->find();
        $smeta = json_decode($post['smeta'], true);
        $post['thumb'] = $smeta['thumb'];
        $this->assign($post);
        $this->display();
    }

    // 任务发布列表
    public function mission_list() {
        $this->assign('list', $this->will_mission_model->getList());
        $this->display();
    }

    // 任务发布详情
    public function mission_detail() {
        $id = I("get.id",0,'intval');

        $this->assign($this->will_mission_model->getInfo($id));
        $this->display();
    }

    // 近期活动列表
    public function events_list() {
        $list = $this->will_events_model->getList();
        $user_id = 1;
//            $user_id = sp_get_current_userid();
//            if(!$user_id) {
//                $this->ajaxReturn(array('status'=>false,'msg'=>'用户未登录'));
//            }

        // 计算剩余报名人数及当前用户是否报名
        foreach($list as $k=>$v) {
            $list[$k]['is_apply'] = 0;
            $count = $this->will_events_apply_model->where(array('eid'=>$v['id']))->count();
            $list[$k]['surplus'] = $v['num'] - $count;
            if($this->will_events_apply_model->where(array('user_id'=>$user_id))->count()) {
                $list[$k]['is_apply'] = 1;
            }
        }
        $this->assign('list', $list);
        $this->display();
    }

    // 近期活动详情
    public function events_detail() {
        $id = I("get.id",0,'intval');
        $user_id = 1;
//            $user_id = sp_get_current_userid();
//            if(!$user_id) {
//                $this->ajaxReturn(array('status'=>false,'msg'=>'用户未登录'));
//            }

        $info = $this->will_events_model->getInfo($id);
        $info['is_apply'] = 0;
        $count = $this->will_events_apply_model->where(array('eid'=>$info['id']))->count();
        $info['surplus'] = $info['num'] - $count;
        if($this->will_events_apply_model->where(array('user_id'=>$user_id))->count()) {
            $info['is_apply'] = 1;
        }
        $this->assign($info);
        $this->display();
    }

    /**
     * 报名近期活动
     * @param eid 活动ID
     * @param user_id 用户ID
     */
    public function events_apply() {
        if(IS_AJAX) {
            $eid = $info['eid'] = I('post.id');
            $user_id = $info['user_id'] = 1;
//            $user_id = sp_get_current_userid();
//            if(!$user_id) {
//                $this->ajaxReturn(array('status'=>false,'msg'=>'用户未登录'));
//            }
            if(!$user_id) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'请先登录'));
            }
            // 判断用户是否报名
            if($this->will_events_apply_model->getInfoByUserId($user_id)) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'您已报名该活动'));
            }
            // 判断活动是否存在及名额是否充足
            $events = $this->will_events_model->getInfo($eid);
            if(!$events) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'活动不存在或已结束'));
            }
            if($events['num'] == 0) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'活动名额不足'));
            }
            // 插入报名表
            $info['ctime'] = time();
            if(!$this->will_events_apply_model->create($info)) {
                $this->ajaxReturn(array('status'=>false,'msg'=>$this->will_events_apply_model->getError()));
            }
            if(!$this->will_events_apply_model->add($info)) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'报名失败,请重试'));
            }
            $this->ajaxReturn(array('status'=>true,'msg'=>'报名成功'));
        } else {
            $this->error('非法操作');
        }
    }

    // 双创项目列表
    public function produce_list() {
        $this->assign('list', $this->will_produce_model->getList());
        $this->display();
    }

    // 双创项目详情
    public function produce_detail() {
        $id = I("get.id",0,'intval');

        $this->assign($this->will_produce_model->getInfo($id));
        $this->display();
    }

    /**
     * 加入鹿鸣社
     * @param post 提交的加入数据
     * @param user_id 用户ID
     */
    public function user_join() {
        if(IS_AJAX) {
            $post =I('post.');
            $user_id = $post['user_id'] = 1;
//            $user_id = $post['user_id'] = sp_get_current_userid();
//            if(!$user_id) {
//                $this->ajaxReturn(array('status'=>false,'msg'=>'用户未登录'));
//            }
            if($this->will_user_apply_model->check($user_id)) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'已提交过申请'));
            }
            $post['ctime'] = $post['utime'] = time();
            if(!$this->will_user_apply_model->create($post)) {
                $this->ajaxReturn(array('status'=>false,'msg'=>$this->will_user_apply_model->getError()));
            }
            $will_id = $this->will_user_apply_model->add($post);
            if(!$will_id) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'提交失败'));
            }
            $this->ajaxReturn(array('status'=>true,'msg'=>'提交成功'));
        } else {
            $this->error('非法操作');
        }
    }

    /**
     * 入会申请提交
     * @param post 提交的申请数据
     * @param user_id 用户ID
     */
    public function submit() {
        if(IS_AJAX) {
            $post =I('post.');
            $user_id = $post['user_id'] = 1;
//            $user_id = $post['user_id'] = sp_get_current_userid();
//            if(!$user_id) {
//                $this->ajaxReturn(array('status'=>false,'msg'=>'用户未登录'));
//            }
            $post['images'] = implode(',',I('post.images'));
            $post['ctime'] = $post['utime'] = time();
            $will_produce_apply_model = D('Common/WillProduceApply');
            if(!$will_produce_apply_model->create($post)) {
                $this->ajaxReturn(array('status'=>false,'msg'=>$will_produce_apply_model->getError()));
            }
            if(count(I('post.images'))<3 || count(I('post.images'))>5) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'图片数量不符'));
            }
            $will_id = $will_produce_apply_model->add($post);
            if(!$will_id) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'提交失败'));
            }
            $this->ajaxReturn(array('status'=>true,'msg'=>'提交成功'));
        } else {
            $this->error('非法操作');
        }
    }

    /**
     * 任务投稿提交
     * @param post 提交的申请数据
     * @param user_id 用户ID
     */
    public function submit_mission() {
        if(IS_AJAX) {
            $post =I('post.');
            $user_id = $post['user_id'] = 1;
//            $user_id = $post['user_id'] = sp_get_current_userid();
//            if(!$user_id) {
//                $this->ajaxReturn(array('status'=>false,'msg'=>'用户未登录'));
//            }
            $post['images'] = implode(',',I('post.images'));
            $post['ctime'] = $post['utime'] = time();
            if(!$this->will_mission_apply_model->create($post)) {
                $this->ajaxReturn(array('status'=>false,'msg'=>$this->will_mission_apply_model->getError(),'data'=>$post));
            }
            if(count(I('post.images'))<3 || count(I('post.images'))>5) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'图片数量不符'));
            }
            $will_id = $this->will_mission_apply_model->add($post);
            if(!$will_id) {
                $this->ajaxReturn(array('status'=>false,'msg'=>'提交失败'));
            }
            $this->ajaxReturn(array('status'=>true,'msg'=>'提交成功'));
        } else {
            $this->error('非法操作');
        }
    }

    //上传图片
    public function personalUpload() {
        if(IS_POST){
            list($status, $info, $link, $attachmentId) = $this->uploadCommon('image', C('UPLOAD_MATERIAL_IMAGE_MAX_SIZE'), 'personal', false);
            if ($status) {
                echo json_encode(array('status' => true, 'data' => $info, 'file'=>sp_get_image_preview_url($info)));exit;
            } else {
                echo json_encode(array('status' => false, 'msg'=>$info));exit;
            }
        } else {
            $this->error('非法操作');
        }
    }

    //上传附件
    public function personalUploadAtt() {
        if(IS_POST){
            list($status, $info, $link, $attachmentId) = $this->uploadCommon('file', C('UPLOAD_MAX_SIZE'), 'file', false);
            if ($status) {
                echo json_encode(array('status' => true, 'data' => $info, 'file'=>sp_get_image_preview_url($info)));exit;
            } else {
                echo json_encode(array('status' => false, 'msg'=>$info));exit;
            }
        } else {
            $this->error('非法操作');
        }
    }
}