YII——存储用户操作记录

controllers

接收传入参数,创建模型类对象,调用新增方法,将接收到的参数作为参数传入。

public function actionUserActionRecord()
        {
            $datas = Yii::$app->request->post();
            $uor = new UserOperationRecord();
            $records=$uor->addRecord($datas);
            if(!empty($records)){
                return json_encode([
                    'status'=>true,
                    'msg'=>$records
                ]);
            }else{
                return json_encode([
                    'status'=>false
                ]);
            }
        }

models

首先获取记录条数,然后获取记录并将其转换为JSON格式的数组(如果是在AJAX中参数格式为JSON格式不需要转换,项目中为安卓请求需要转换),最后在循环中将所有记录保存。将存储成功的记录ID保存到数组中返回给客户端

public function addRecord($datas)
    {
        $num = $datas['recordCount'];
        $records = array();
        
        $operation_record = json_decode($datas['operation_record'],true);
        //$operation_record = $datas['operation_record'];
        for ($i = 0; $i < $num; $i++) {
            $uor = new UserOperationRecord();
            $data = $operation_record[$i];
            $uor->app_id = $data['appId'];
            $uor->record_id = $data['recordId'];
            $uor->app_name = $data['appName'];
            $uor->module_id = $data['moduleId'];
            $uor->module_name = $data['moduleName'];
            $uor->action = $data['action'];
            if (isset($data['vin'])) {
                $uor->vin = $data['vin'];
            }
            if (isset($data['e_uid'])) {
                $uor->e_uid = $data['e_uid'];
            }
            $uor->operating_time = $data['operatingTime'];
            if ($uor->save()) {
                array_push($records, $uor->record_id);
            }
        }
        return $records;
    }

AJAX

注意:如果是以下方式请求,models中不需要json_decode方法进行转换。

$.ajax({
                url:"http://localhost:8088/framework/web/index.php?r=VehicleTerminalApplication/interface/user-action-record",
                type:"post",
                data:{
                    "recordCount":7,
                    "operation_record":[
                        {"action":"update","appId":1,"appName":"VehicleAPP","moduleId":4,"moduleName":"version","operatingTime":"2018-10-12 06:55:17","recordId":1},
                        {"action":"update","appId":1,"appName":"VehicleAPP","moduleId":4,"moduleName":"version","operatingTime":"2018-10-12 06:58:12","recordId":2},
                        {"action":"update","appId":1,"appName":"VehicleAPP","moduleId":4,"moduleName":"version","operatingTime":"2018-10-12 06:58:16","recordId":3},
                        {"action":"update","appId":1,"appName":"VehicleAPP","moduleId":4,"moduleName":"version","operatingTime":"2018-10-12 06:58:35","recordId":4},
                        {"action":"update","appId":1,"appName":"VehicleAPP","moduleId":4,"moduleName":"version","operatingTime":"2018-10-12 06:58:36","recordId":5},
                        {"action":"update","appId":1,"appName":"VehicleAPP","moduleId":4,"moduleName":"version","operatingTime":"2018-10-12 06:58:36","recordId":6},
                        {"action":"update","appId":1,"appName":"VehicleAPP","moduleId":4,"moduleName":"version","operatingTime":"2018-10-12 07:18:06","recordId":7}
                    ]
                },
                success: function (data){
                }
            });

响应结果

{"status":true,"msg":["1","2","3","4","5","6","7"]}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值