Mechanism.php 3.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/24
 * Time: 15:30
 */

namespace app\products\controller;
use app\web\controller\BaseController;

use think\Db;
use think\Request;

class Mechanism extends BaseController
{
    //获取用户列表
    public function getMechanismList(Request $request){
        $data = $request->param();
        $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
        $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
        $offset=($page-1)*$rows;
        $data['offset']=$offset;
        $data['rows']=$rows;
        $where=[];
        if(!empty($data['id']) && isset($data['id'])){
            $where['id']=$data['id'];
        }
        if (isset($data['start']) && !empty($data['start']) &&  isset($data['end']) && !empty($data['end'])) {
            $start_time = strtotime($data['start'].' 00:00:01');
            $end_time = strtotime($data['end'].' 23:59:59');
            $where['createTime'] = ['between', [$start_time, $end_time]];
        } else if (isset($data['start']) && !empty($data['start']) && isset($data['end']) == false) {
            $start_time = strtotime($data['start'].' 00:00:01');
            $where['createTime'] = ['egt', $start_time];
        } else if (isset($data['start']) == false && isset($data['end']) && !empty($data['end'])) {
            $end_time = strtotime($data['end'].' 23:59:59');
            $where['createTime'] = ['elt', $end_time];
        }

        if(!empty($data['names'])){
            $where['mechanismName']=$data['names'];
        }
        //$org_id = session('organization.org_id');
        $list= Db::table('sos_user_mechanism')->where($where)->limit($offset,$rows)->select()->toArray();
        $total= Db::table('sos_user_mechanism')->where($where)->count();

        foreach($list  as $key=>$val ){
            $list[$key]['createTime']=$val['createTime'] == ''? '' : date('Y-m-d H:i:s',$val['createTime']);

        }
        $result["total"] =$total;
        $result['rows']=$list;
        echo json_encode($result);
    }




    /**
     * @param Request $request
     * @return array|mixed|null
     * 添加用户
     */

    public function mechanism_add(Request $request)
    {
        $data = $request->param();

        if(isset($data['password']) && !empty($data['password']) && $data['password']!='******'){
            $data['password']=md5(md5($data['password']));
        }

        if(isset($data['mechanism_id']) && !empty($data['mechanism_id'])){
            $data['updateTime']=time();
            $where['id']=$data['mechanism_id'];
            unset($data['mechanism_id']);
            $res = Db::table('sos_user_mechanism')->where($where)->update($data);
        }else{
            $row['username']=$data['username'];
            $list= Db::table('sos_user_mechanism')->where($row)->select()->toArray();
            if($list){
                $data=[
                    'code'=>100,
                    'msg'=>'登录账号已存在'
                ];
                return $data;
            }
            unset($data['mechanism_id']);
            $data['createTime']=time();

            $res = Db::table('sos_user_mechanism')->insert($data);
        }
        $json=json_decode($res,true);
        if(is_numeric($json)){
            $data=[
                'code'=>200,
                'msg'=>'成功'
            ];
        }else{
            $data=[
                'code'=>100,
                'msg'=>'失败'
            ];
        }
        return $data;
    }



}