VolunteerController.php 12.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/25
 * Time: 11:43
 */

namespace app\admin\controller;


use app\admin\model\Excel;
use cmf\controller\AdminBaseController;
use think\Validate;
use think\Db;
use EasyWeChat\Foundation\Application;
use think\Image;

class VolunteerController extends AdminBaseController
{
    public function index()
    {
        $where = [];
        $param = $this->request->param();
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }
        $category = empty($param['category']) ? '' : $param['category'];
        if (!empty($category)) {
            $where['status'] = ['eq', "$category"];
        }
        $type = empty($param['type']) ? '' : $param['type'];
        if (!empty($type)) {
            $where['type'] = ['eq', "$type"];
        }
        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['name|province|city|county|email|move_phone|fixed_phone|card_number|school|postcode|nation|politics|sex|urgency_phone|qq'] = ['like', "%$keyword%"];
        }
        $addr = empty($param['addr']) ? '' : $param['addr'];
        if (!empty($addr)) {
            $where['addr'] = ['like', "%$addr%"];
        }
        $list = Db::name('volunteer')
            ->where('delete_time', 0)
            ->where($where)
            ->order('create_time desc')
            ->paginate(10);
        $list->appends($param);
        $data=$list->items();
        $year=date('Y');
        foreach ($data as $k=>$v){
            $data[$k]['age']=$year-date('Y',strtotime($v['birthday']));
            $data[$k]['level']=get_level($v['work_time']);
        }
        $this->assign([
            'data' => $data,
            'page' => $list ->render(),
        ]);

        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('addr', isset($param['addr']) ? $param['addr'] : '');
        $this->assign('category', isset($param['category']) ? $param['category'] : '');
        $this->assign('type', isset($param['type']) ? $param['type'] : '');
        return $this->fetch();
    }


    //编辑
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        $volunteer_org=\db('volunteer')->where('id',$id)->find();
        if ($this->request->isPost()) {
            $param = $this->request->param();
            /*更新级别*/
            $level_map['min'] = ['<=', $param['work_time']];
            $level_map['max'] = ['>', $param['work_time']];
            $level = db('level')->where($level_map)->value('level');
            $param['level'] = $level;
           $result= Db::name('volunteer')
                ->where('id', $id)
                ->update($param);
            /*发送模板消息进行通知*/
            if ($result>0 && $volunteer_org['status']!=$param['status']){
                $volunteer=\db('volunteer')->where('id',$id)->find();
                $userMap['user_id']=$volunteer['user_id'];
                $openid=db('third_party_user')->where($userMap)->value('openid');
                $formMap['user_id']=$volunteer['user_id'];
                $formMap['use_time']=0;
                $formMap['expire_time']=['>',time()];
                $form_id=\db('form_id')->where($formMap)->find();
                if (!empty($form_id)&& $param['status']!=1){
                    \db('form_id')->where('form_id',$form_id['form_id'])->update(['use_time'=>time()]);
                    $data['keyword1']= $volunteer['name'];
                    $data['keyword2']='志愿者审核';
                    if ($param['status']==2){
                        $data['keyword3']='通过';
                    }
                    if ($param['status']==3){
                        $data['keyword3']='未通过 原因:'.$param['refund_reason'];
                    }
                    $data['keyword4']='申请成为志愿者';
                    $message = [
                        'touser' => $openid,
                        'template_id' => '1FYkLuMK8LNKO4_nxJ_DuNQhgIjaaJQlAukgt92C-uw',
                        'page' => 'pages/index/index',
                        'form_id' => $form_id['value'],
                        'data' => $data,
                        'emphasis_keyword' =>$data['keyword3']='状态',
                    ];
                    $this->wxappNotice($message);
                }

            }
            $this->success('操作成功!');





            $this->success('更新成功!');
        } else {
            $data = Db::name('volunteer')
                ->where('id', $id)
                ->find();
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }
    }

    /**
     * 模板消息发送
     * @param $message
     * @return \EasyWeChat\Support\Collection
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
     */
    protected function wxappNotice($message){
        $appId     = 'wx7a3bf4e0796c070a';
        $secret = '755478f064e09a8301345e0c427c6cf3';
        $config = [
            'app_id'  => $appId,
            'secret'  => $secret,
        ];
        $wechat=new Application($config);
        $notice=$wechat->mini_program->notice;
        $result= $notice->send($message);
        return $result;

        try{
            $wechat=new Application($config);
            $notice=$wechat->mini_program->notice;
            $result= $notice->send($message);
        }catch(Exception $e){
            $e->getMessage();
        }
        return $result;
    }
    //删除
    public function delete()
    {
        $param = $this->request->param();

        if (isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            $resultPortal = Db::name('volunteer')
                ->where(['id' => $id])
                ->delete();
            if ($resultPortal) {
                $this->success("删除成功!", '');
            } else {
                $this->error("删除失败!", '');
            }

        }

        if (isset($param['ids'])) {
            $ids = $this->request->param('ids/a');
            $result = Db::name('volunteer')
                ->where(['id' => ['in', $ids]])
                ->delete();
            if ($result) {
                $this->success("删除成功!", '');
            } else {
                $this->error("删除失败!", '');
            }
        }
    }

    //发布
    public function publish()
    {
        $param = $this->request->param();
        $ids = $this->request->param('ids/a');
        if (!empty($ids) && !empty($param['yes'])) {
            Db::name('volunteer')->where(['id' => ['in', $ids]])->update(array('status' => 2));
            $this->success('操作成功!', '');
        } else {
            Db::name('volunteer')->where(['id' => ['in', $ids]])->update(array('status' => 3));
            $this->success('操作成功!', '');
        }
    }

    public function excels()
    {
        $where = [];
        $param = $this->request->param();
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }
        $category = empty($param['category']) ? '' : $param['category'];
        if (!empty($category)) {
            $where['status'] = ['eq', "$category"];
        }
        $type = empty($param['type']) ? '' : $param['type'];
        if (!empty($type)) {
            $where['type'] = ['eq', "$type"];
        }
        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['name|province|city|county|email|move_phone|fixed_phone|card_number|school|postcode|nation|politics|sex|urgency_phone|qq'] = ['like', "%$keyword%"];
        }

        $data = Db::name('volunteer')
            ->where('delete_time', 0)
            ->where($where)
            ->order('create_time asc')
            ->select()->toArray();

        foreach ($data as $k => $v) {
            switch ($v['sex']) {
                case 1:
                    $data[$k]['sex'] = '男';
                    break;
                case 2:
                    $data[$k]['sex'] = '女';
                    break;
                default:
                    $data[$k]['sex'] = '保密';
            }
            $data[$k]['card_number'] = '`' . $v['card_number'];
        }
        $title = [
            ['ID', 15, 'id', 1],
            ['姓名', 15, 'name', 1],
            ['性别', 15, 'sex', 1],
            ['生日', 15, 'birthday', 1],
            ['民族', 15, 'nation', 1],
            ['政治面貌', 15, 'politics', 1],
            ['学历', 15, 'education', 1],
            ['工作单位', 15, 'unit', 1],
            ['职务', 15, 'duty', 1],
            ['通讯地址', 15, 'addr', 1],
            ['邮编', 15, 'postcode', 1],
            ['毕业院校', 15, 'school', 1],
            ['身份证号', 25, 'card_number', 1],
            ['固定电话', 15, 'fixed_phone', 1],
            ['移动电话', 15, 'move_phone', 1],
            ['邮箱', 15, 'email', 1],
            ['紧急联系人电话', 15, 'urgency_phone', 1],
            ['qq号', 15, 'qq', 1],
            ['省', 15, 'province', 1],
            ['市', 15, 'city', 1],
            ['区县', 15, 'county', 1],
            ['详细地址', 15, 'address', 1],
        ];

        $Excel = new Excel();
        $letter = $Excel->excelAcross(count($title));
        $Excel->excel($letter, $title, $data, 'volunteers');
    }

    public function sendnotice(){
        return $this->fetch();
    }


    public function sendsms(){
        $data = Db::name('volunteer')
            ->where('delete_time', 0)
            ->order('create_time desc')
            ->select();
        $this->assign([
            'list' => $data,
        ]);
      return  $this->fetch();
    }

    public function smsPost(){
        $user=input('to_user_id');
        $content=input('description');
        if (empty($user)||$user==0){
            $this->error('请选择用户!');
        }
        if (empty($content)){
            $this->error('内容不能为空!');
        }
        $users=explode(',',$user);
        $result=send_duanxinbao($content,$users);
        if ($result==0){
            $this->success('发送成功!');
        }else{
            $this->error('发送失败!');
        }
    }

    public function pass(){
        $id=input('id');
        $update['status']='2';
        $result=\db('volunteer')->where('id',$id)->update($update);
        if ($result>0){
            $this->success('编辑成功!');
        }else{
            $this->error('编辑失败');
        }
    }
    public function unpass(){
        $id=input('id');
        $update['status']='3';
        $result=\db('volunteer')->where('id',$id)->update($update);
        if ($result>0){
            $this->success('编辑成功!');
        }else{
            $this->error('编辑失败');
        }
    }


}