AdminIndexController.php 14.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\user\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use PHPExcel;
use PHPExcel_IOFactory;

/**
 * Class AdminIndexController
 * @package app\user\controller
 *
 * @adminMenuRoot(
 *     'name'   =>'用户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10,
 *     'icon'   =>'group',
 *     'remark' =>'用户管理'
 * )
 *
 * @adminMenuRoot(
 *     'name'   =>'用户组',
 *     'action' =>'default1',
 *     'parent' =>'user/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>'用户组'
 * )
 */
class AdminIndexController extends AdminBaseController
{

    /**
     * 后台本站用户列表
     * @adminMenu(
     *     'name'   => '本站用户',
     *     'parent' => 'default1',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $where   = ['user_type'=>2];
        $where['user_status']=['neq',0];
        $request = input('request.');

        if (!empty($request['user_nickname'])) {
            $nickname=$request['user_nickname'];
            $where['user_nickname']    = ['like', "%$nickname%"];
        }
        if (!empty($request['mobile'])) {
            $keyword = $request['mobile'];

            $where['mobile']    = ['like', "%$keyword%"];
        }
        $usersQuery = Db::name('user');

        $list = $usersQuery->where($where)->order("create_time DESC")->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 本站用户拉黑
     * @adminMenu(
     *     'name'   => '本站用户拉黑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户拉黑',
     *     'param'  => ''
     * )
     */
    public function ban()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $result = Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 0);
            if ($result) {
                $this->success("会员拉黑成功!", "adminIndex/index");
            } else {
                $this->error('会员拉黑失败,会员不存在,或者是管理员!');
            }
        } else {
            $this->error('数据传入失败!');
        }
    }

    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function cancelBan()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
            $this->success("会员启用成功!", '');
        } else {
            $this->error('数据传入失败!');
        }
    }

    /**
     * 本站用户删除
     * @adminMenu(
     *     'name'   => '本站用户删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户删除',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $param=$this->request->param();
        if (!empty($param['ids'])){
            Db::name('user')->where(['id' => ['in', $param['ids']]])->delete();
            Db::name('third_party_user')->where(['user_id' => ['in', $param['ids']]])->delete();
            $this->success('删除成功');
        }
        Db::name('message')->where('id', $param['id'])->delete();
        Db::name('third_party_user')->where(['user_id' => $param['id']])->delete();
        $this->success('删除成功');
    }


    public function editCook(){
        $data=Db::name('config')->where('id',1)->find();
        $this->assign('list',$data);
        return $this->fetch();
    }

    public function  editCookPost(){
        $param=$this->request->param();
        Db::name('config')->where('id',1)->update($param);
        $this->success('编辑成功');
    }


    /**
     * 我的积分
     * @adminMenu(
     *     'name'   => '我的积分',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '我的积分',
     *     'param'  => ''
     * )
     */
    public function myScore()
    {
        $param=$this->request->param();
        $data=Db::name('user_score_log')->where('user_id', $param['id'])->order('create_time','desc')->paginate(10);
        $this->assign('list',$data);
        return $this->fetch();
    }

    /**
     * 我的积分记录删除
     * @adminMenu(
     *     'name'   => '我的积分记录删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '我的积分记录删除',
     *     'param'  => ''
     * )
     */
    public function scoreDelete()
    {
        $param=$this->request->param();
        if (!empty($param['ids'])){
            Db::name('user_score_log')->where(['id' => ['in', $param['ids']]])->delete();
            $this->success('删除成功');
        }
        Db::name('user_score_log')->where('id', $param['id'])->delete();
        $this->success('删除成功');
    }


    /**
     * 我的体重数据
     * @adminMenu(
     *     'name'   => '我的体重数据',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '我的体重数据',
     *     'param'  => ''
     * )
     */
    public function myWeight()
    {
        $param=$this->request->param();
        $data=Db::name('user_week_sign')->where('user_id', $param['id'])->order('create_time','desc')->paginate(10);;
        $this->assign('list',$data);
        return $this->fetch();
    }

    public function myWeightEdit(){
        $param=$this->request->param();
        $data=Db::name('user_week_sign')->where('id', $param['id'])->find();
        $this->assign('list',$data);
        return $this->fetch();
    }

    public function myWeightEditPost(){
        $param=$this->request->param();
        Db::name('user_week_sign')->where('id', $param['id'])->update(['weight'=>$param['weight']]);
        $this->success('编辑成功');
    }

    /**
     * 我的血糖数据
     * @adminMenu(
     *     'name'   => '我的血糖数据',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '我的血糖数据',
     *     'param'  => ''
     * )
     */
    public function myBlood()
    {
        $param=$this->request->param();
        $data=Db::name('user_week_sign')->where('id', $param['id'])->find()['more'];
        $more=json_decode($data,true);
        $this->assign('list',$more);
        return $this->fetch();
    }

    /**
     * 编辑用户信息
     * @adminMenu(
     *     'name'   => '编辑用户信息',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑用户信息',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $param=$this->request->param();
        $data=Db::name('user')->where('id', $param['id'])->find();
        $this->assign('list',$data);
        return $this->fetch();
    }

    public function editPost()
    {
        $param=$this->request->param();
        $param['last_yuejin_time']= strtotime( $param['last_yuejin_time']);
        if (empty(Db::name('user')->where('id', $param['id'])->find()['pregnant_week_time'])){
            $param['pregnant_week_time']=time();
        }
        Db::name('user')->where('id', $param['id'])->update($param);
        $this->success('编辑成功');
    }


    public function cookBook(){
        $data=Db::name('user')->where('user_type',2)->paginate(10)
            ->each(function ($item) {
                $item['cook'] = $this->getCookBook($item['before_weight'],$item['height'],$item['weight_level'],$item['pregnant_week'],$item['register_time']);
                return $item;
            });
//        foreach ($data as $k=>$v){
//            $data[$k]['cook']=$this->getCookBook($v['before_weight'],$v['height'],$v['weight_level'],$v['pregnant_week']);
//        }
        $this->assign('list',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }



    //菜谱算法
    public function getCookBook($before_weight,$height,$weight_level,$pregnant_week,$register_time){
        if (empty($before_weight)||empty($height)||empty($weight_level)||empty($pregnant_week)){
            return '数据不足';
        }
        $config=Db::name('config')->where('id',1)->find();
        $lixiangtizhong=$height-105;
        $bmi= sprintf('%.1f', $before_weight/(pow($height/100,2)));
        if($bmi<=18.5){
            $type1=0;
        }elseif (18.5<$bmi&&$bmi<=23.9){
            $type1=1;
        }elseif (24<$bmi&&$bmi<=27.9){
            $type1=2;
        }elseif ($bmi>=28){
            $type1=3;
        }
        if ($weight_level=="休息状态"){
            $type2=[30,25,20,20];
        }elseif ($weight_level=="轻体力"){
            $type2=[35,30,25,25];
        }
        elseif ($weight_level=="中体力"){
            $type2=[40,35,30,30];
        }
        elseif ($weight_level=="重体力"){
            $type2=[45,40,35,35];
        }
        $param=$type2[$type1];
        if (($pregnant_week+ceil((time()-$register_time)/24*60*60*7))<=12){
            $x=0;
        }else{
            $x=200;
        }
        $result=$lixiangtizhong*$param+$x;
        if ($result<1850){
            $result=1800;
            $cook=$config['a'];
        }else if ($result<1950&&$result>=1850){
            $result=1900;
            $cook=$config['b'];
        }
        else if ($result<2000&&$result>=1950){
            $result=1900;
            $cook=$config['c'];
        }
        else if ($result<2150&&$result>=2000){
            $result=2100;
            $cook=$config['d'];
        }
        else if ($result<2250&&$result>=2150){
            $result=2100;
            $cook=$config['e'];
        }
        else if ($result>=2250){
            $result=2300;
            $cook=$config['f'];
        }
        return $cook;
    }


    //导出数据
    public function excel()
    {
//        $param=$this->request->param();
//        if (!empty($param['ids'])){
//            $list=Db::name('user')->where(['id' => ['in', $param['ids']],'user_type'=>2])->select()
//                ->each(function ($item) {
//                    $item['cook'] = $this->getCookBook($item['before_weight'],$item['height'],$item['weight_level'],$item['pregnant_week']);
//                    return $item;
//                })->toArray();
//        }else{
//            $list=Db::name('user')->where(['id' => $param['id'],'user_type'=>2])->select()->toArray()
//                ->each(function ($item) {
//                    $item['cook'] = $this->getCookBook($item['before_weight'],$item['height'],$item['weight_level'],$item['pregnant_week']);
//                    return $item;
//                })->toArray();
//        }
        $list=Db::name('user')->where(['user_type'=>2])->select()
            ->each(function ($item) {
                $item['cook'] = $this->getCookBook($item['before_weight'],$item['height'],$item['weight_level'],$item['pregnant_week'],$item['register_time']);
                return $item;
            })->toArray();
        $path = ROOT_PATH . '/public/upload/'; //找到当前脚本所在路径
        $PHPExcel = new PHPExcel(); //实例化PHPExcel类,类似于在桌面上新建一个Excel表格
        $PHPSheet = $PHPExcel->getActiveSheet(); //获得当前活动sheet的操作对象
        $PHPSheet->setTitle('表单信息'); //给当前活动sheet设置名称
        $PHPSheet->setCellValue('A1', 'ID')
            ->setCellValue('B1', '微信名')
            ->setCellValue('C1', '手机号')
            ->setCellValue('D1', '餐谱');
        foreach ($list as $k => $v) {
            $n = $k + 2;

            $PHPSheet->setCellValue("A$n", $v['id'])
                ->setCellValue("B$n", "$v[user_nickname]")
                ->setCellValue("C$n", "$v[mobile]")
                ->setCellValue("D$n", "$v[cook]");
        }
        $PHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//设置默认宽度
        $PHPWriter = PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007');//按照指定格式生成Excel文件,‘Excel2007'表示生成2007版本的xlsx,
        $file_name='/'.date('Y-m-d',time()).'表单信息.xlsx';
        $PHPWriter->save($path.$file_name); //表示在$path路径下面生成demo.xlsx文件
        $filename ='./upload'.$file_name;
        ob_end_clean();
        $file=cmf_get_file_download_url(date('Y-m-d',time()).'表单信息.xlsx');
        header("Location: $file");
        //下载文件需要用到的头
//        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//        header('Content-Description: File Transfer');
//        header('Content-Type: application/octet-stream');
//        header('Content-Length: ' . filesize($filename));
//        header('Content-Disposition: attachment; filename=' . basename($filename));
//        readfile($filename);
        exit;
    }

}