ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
| 控制器基本操作 | | | --- | --- | ``` <?php namespace app\admin\Controller; use think\Controller; use think\Db; use think\facade\Validate; use think\facade\Cache; use think\facade\Env; use think\Request; /** * 类的基本操作 */ class Index extends Controller { /** * [_initialize 初始化函数] * @return [type] [description] */ public function _initialize() { $this->_model=request()->module(); //当前模型 $this->_controller=request()->controller(); //当前控制器 $this->_action=request()->action(); //当前方法 } /** * [affair 事务操作] * @return [type] [description] */ public function affair() { Db::startTrans(); try{ $result = [ 'statusCode' => 200, 'message' => '操作提示', 'navTabId' => '回调之后刷新的节点', 'rel' => '不知道干啥的', 'forwardUrl' => '回调的url', 'callbackType' => 'closeCurrent', //回调之后执行的函数 ]; Db::commit(); }catch (\Exception $e){ $result = [ 'statusCode' => 200, 'message' => $e->getMessage(); 'navTabId' => '回调之后刷新的节点', 'rel' => '不知道干啥的', 'forwardUrl' => '回调的url', 'callbackType' => 'closeCurrent', //回调之后执行的函数 ]; Db::rollback(); } } /** * [CURD 数据库操作] */ public function CURD() { //1、模糊查询 $data['company'] = '测试'; $where['company_name']=array('like','%'.$data['company_name'].'%'); $companyInfo = Db::name('Company')->field('id')->where($where)->find(); //多where条件 $companyInfo = DB::name('company')->where('id', 'eq', 1)->where('total_currency', 'null')->find(); $where['gyl_water_in.del'] = ['eq', 0]; $map['gyl_water_in.water_time'] = ['BETWEEN', [$start_time,$end_time]]; //CAST函数语法规则是:Cast(字段名 as 转换的类型 ),把total_currency字段转为json类型 $bank_res=Db::name('bank_account')->where('total_currency','exp', "=cast('".$bank_account['total_currency']."' as json)"); //更新其中的字段数据 $water_res=Db::name('water_in') ->where('id',$value['id']) ->where('water_status', 0) ->where("check_status",1) ->setField('water_status',1); //查询直接把时间戳转日期格式输出,以下是公司的数据库处理方法 $field[] = 'FROM_UNIXTIME(`gyl_water_in`.`water_time`,"%Y-%m-%d") as "付款日期"'; $field[]='IFNULL(sum(gyl_order_air.ctns),0) as total_ctns';//如果出仓的总箱号为空则0 $join[] = ["gyl_client", "gyl_client.id = gyl_water_in.client_id",'left']; $order = 'gyl_water_in.water_time DESC'; $map['gyl_water_in.client_id'] = ['eq', $client_id]; $data = Db::name('water_in') ->field($field) ->join($join) ->where($map) ->order($order) ->select(); } /** * [dataACtion 时间日期处理] * @return [type] [description] */ public function dataACtion() { echo '七天前整点:' . date('Y-m-d', strtotime('-7 day')) . '00:00:00'; echo '当前时间加一天:' . date('Y-m-d H:i:s', strtotime('+1 day')); } /** * [ajaxRequest ajax请求] * @return [type] [description] */ public function ajaxRequest() { if (request()->isPost()) { $data = input(); $result = [ 'statusCode' => 200, 'message' => '操作提示', 'navTabId' => '回调之后刷新的节点', 'rel' => '不知道干啥的', 'forwardUrl' => '回调的url', 'callbackType' => 'closeCurrent', //回调之后执行的函数 ] return $result; }else { return view(); } } } ```