码迷,mamicode.com
首页 > 其他好文 > 详细

yii2框架增删改查案例

时间:2016-12-12 19:56:38      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:nis   his   asc   cin   model2   on()   color   www   ping   

//解除绑定蓝牙
//http://www.520m.com.cn/api/pet/remove-binding?healthy_id=72&pet_id=100477&access-token=YWdqPCWdt3_IqkrTK-U5vS458ZWfcdeT
public function actionRemoveBinding(){
$healthy_id = Yii::$app->request->get(‘healthy_id‘);
if(empty($healthy_id)){
return Helper::format_data(PARAM_ERROR, [‘healthy_id‘=>‘无效的参数healthy_id‘]);
}

$pet_id = Yii::$app->request->get(‘pet_id‘);
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, [‘pet_id‘=>‘无效的参数pet_id‘]);
}

$healthyinfo = Healthy::find()->where([‘healthy_id‘=>$healthy_id])->asArray()->one();
if($healthyinfo[‘is_activation‘]==‘2‘){
return Helper::format_data(PARAM_ERROR, [‘is_activation‘=>‘已经执行过解除绑定操作‘]);
}

$access_token = Yii::$app->request->get(‘access-token‘);
$userinfo = UserOperate::find()->where([‘access_token‘=>$access_token])->asArray()->one();
$user_id = $userinfo[‘id‘];
$time = time();

$model = new Healthy();
$data = [‘remove_binding‘=>$time,‘longitude‘=>‘‘,‘latitude‘=>‘‘,‘is_activation‘=>‘2‘];
$where = [‘healthy_id‘ => $healthy_id,‘pet_id‘ => $pet_id,‘user_id‘ => $user_id];

if($model->updateAll($data,$where)){
$model = new Pet();
$data = [‘bluetooth_id‘=>‘‘];
$where = [‘id‘ => $pet_id,‘user_id‘ => $user_id];
$model->updateAll($data,$where);
return Helper::format_data(SUCC,null);
}else{
return Helper::format_data(PARAM_ERROR,null);
}
}


//绑定MAC地址
//http://www.520m.com.cn/api/pet/binding-mac?ak=OTAzMTY3Nzl=A1&pet_id=100477&mac=07-16-76-00-02-86&access-token=A20AgFR0C3Au0hS8zJ1kG2dH0bTXcQ6B
public function actionBindingMac(){

$data = Yii::$app->request->get();
$ak = $data[‘ak‘];
if(empty($ak)){
return Helper::format_data(PARAM_ERROR, [‘msg‘=>‘无效的参数ak‘]);
}

$count = Healthy::find()->where([‘ak‘=>$ak])->count();
if($count>=1){
return Helper::format_data(PARAM_ERROR, [‘msg‘=>‘二维码已经被使用‘]);
}

$mac = $data[‘mac‘];
if(empty($mac)){
return Helper::format_data(PARAM_ERROR, [‘msg‘=>‘无效的参数mac‘]);
}

//从右向左截第一个来判断设备颜色
$color = substr($ak,-1);
if($color==‘1‘){
$hardware_color = ‘白色‘;
}else if($color==‘2‘){
$hardware_color = ‘黑色‘;
}else if($color==‘3‘){
$hardware_color = ‘金色‘;
}else{
$hardware_color = ‘粉色‘;
}

//从右向左截第一个来判断设备类型
$name = substr($ak,-2,1);
if($name==‘A‘){
$hardware_name = ‘蓝牙铃铛‘;
}else{
$hardware_name = ‘未识别的设备‘;
}

$pet_id = $data[‘pet_id‘];
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, [‘msg‘=>‘无效的参数pet_id‘]);
}

//根据pet_id查询宠物的信息
$petinfo=PetOperate::find()->where([‘id‘=>$pet_id])->asArray()->one();
if(empty($petinfo)){
return Helper::format_data(PARAM_ERROR, [‘pet_id‘=>‘无效的参数pet_id‘]);
}
$user_id = $petinfo[‘user_id‘];
$pet_name = $petinfo[‘pet_name‘];
$bluetooth_id = $petinfo[‘bluetooth_id‘];
if($bluetooth_id!=null){
return Helper::format_data(PARAM_ERROR, [‘bluetooth_id‘=>‘已经绑定‘]);
}


//实现入库操作
$model = new Healthy();
$model->run = ‘0‘;
$model->go = ‘0‘;
$model->jump = ‘0‘;
$model->sleep = ‘0‘;
$model->pet_id = $pet_id;
$model->user_id = $user_id;
$model->pet_name = $pet_name;
$model->hardware_name = $hardware_name;
$model->hardware_color = $hardware_color;
$model->remove_binding = ‘‘;
$model->mac = $mac;
$model->longitude = ‘‘;
$model->latitude = ‘‘;
$model->is_activation = ‘1‘;
$model->ak = $ak;
if($model->insert()){
$id = $model->attributes[‘healthy_id‘];
}

//给宠物表填充bluetooth_id
$model2 = new Pet();
$data = [‘bluetooth_id‘=>$id];
$where = [‘id‘=>$pet_id];

if($model2->updateAll($data,$where)){
$dat[‘healthy_id‘] = $id;
$dat[‘pet_name‘] = $pet_name;
$dat[‘hardware_name‘] = $hardware_name;
$dat[‘hardware_color‘] = $hardware_color;
return Helper::format_data(SUCC,$dat);
}else{
return Helper::format_data(PARAM_ERROR,null);
}
}


//mac激活的时候专用接口
//http://www.520m.com.cn/api/pet/mac?healthy_id=106&mac=07-16-76-00-02-86&pet_id=100477&access-token=A20AgFR0C3Au0hS8zJ1kG2dH0bTXcQ6B
/*public function actionMac(){
$healthy_id = Yii::$app->request->get(‘healthy_id‘);
if(empty($healthy_id)){
return Helper::format_data(PARAM_ERROR, [‘healthy_id‘=>‘无效的参数healthy_id‘]);
}

$mac = Yii::$app->request->get(‘mac‘);
if(empty($mac)){
return Helper::format_data(PARAM_ERROR, [‘mac‘=>‘无效的参数mac‘]);
}

$pet_id = Yii::$app->request->get(‘pet_id‘);
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, [‘pet_id‘=>‘无效的参数pet_id‘]);
}

$macinfo = Healthy::find()->where([‘healthy_id‘=>$healthy_id])->asArray()->one();
//print_r($macinfo);die;
if($macinfo[‘mac‘]!=null){
return Helper::format_data(PARAM_ERROR, [‘mac‘=>‘已经绑定mac地址‘]);
}

$access_token = Yii::$app->request->get(‘access-token‘);
$userinfo = UserOperate::find()->where([‘access_token‘=>$access_token])->asArray()->one();
$user_id = $userinfo[‘id‘];

$model = new Healthy();
$data = [‘mac‘=>$mac,‘is_activation‘=>‘1‘];
$where = [‘healthy_id‘ => $healthy_id,‘pet_id‘ => $pet_id,‘user_id‘ => $user_id];

if($model->updateAll($data,$where)){
$petinfo=PetOperate::find()->where([‘id‘=>$pet_id])->asArray()->one();
$pet_name = $petinfo[‘pet_name‘];

$healthyinfo = Healthy::find()->where([‘mac‘=>$mac])->asArray()->one();

$data[‘pet_name‘] = $pet_name;
$data[‘hardware_name‘] = $healthyinfo[‘hardware_name‘];
$data[‘hardware_color‘] = $healthyinfo[‘hardware_color‘];

return Helper::format_data(SUCC,$data);
}else{
return Helper::format_data(PARAM_ERROR,null);
}
}*/


//扫描时判断mac地址是否存在
//http://www.520m.com.cn/api/pet/is-mac?mac=07-16-76-00-02-86&access-token=YWdqPCWdt3_IqkrTK-U5vS458ZWfcdeT
public function actionIsMac(){
$mac = Yii::$app->request->get(‘mac‘);
if(empty($mac)){
return Helper::format_data(PARAM_ERROR, [‘mac‘=>‘无效的参数mac‘]);
}
$healthyinfo = Healthy::find()->where([‘mac‘=>$mac])->asArray()->one();
//print_r($healthyinfo);die;
$pet_id = $healthyinfo[‘pet_id‘];
$petinfo=PetOperate::find()->where([‘id‘=>$pet_id])->asArray()->one();
//print_r($petinfo);die;
$pet_name = $petinfo[‘pet_name‘];

if(empty($healthyinfo)){
return Helper::format_data(SUCC,null);
}else{
$data[‘pet_name‘] = $pet_name;
$data[‘hardware_name‘] = $healthyinfo[‘hardware_name‘];
$data[‘hardware_color‘] = $healthyinfo[‘hardware_color‘];
return Helper::format_data(SUCC,$data);
}
}


//每个用户的蓝牙铃铛历史绑定记录
//http://www.520m.com.cn/api/pet/history-binding?access-token=YWdqPCWdt3_IqkrTK-U5vS458ZWfcdeT
public function actionHistoryBinding(){
$access_token = Yii::$app->request->get(‘access-token‘);
$userinfo = UserOperate::find()->where([‘access_token‘=>$access_token])->asArray()->one();
$user_id = $userinfo[‘id‘];

$historyinfo = Healthy::find()->select([‘healthy_id‘,‘pet_name‘,‘hardware_name‘,‘hardware_color‘,‘remove_binding‘])->where([‘user_id‘=>$user_id,‘is_activation‘=>‘2‘])->asArray()->all();
if(empty($historyinfo)){
return Helper::format_data(SUCC,null);
}else{
return Helper::format_data(SUCC,$historyinfo);
}
}


//历史绑定设备重新激活
//http://www.520m.com.cn/api/pet/activation?pet_id=100477healthy_id=71&access-token=YWdqPCWdt3_IqkrTK-U5vS458ZWfcdeT
public function actionActivation(){
$healthy_id = Yii::$app->request->get(‘healthy_id‘);
if(empty($healthy_id)){
return Helper::format_data(PARAM_ERROR, [‘healthy_id‘=>‘无效的参数healthy_id‘]);
}

$pet_id = Yii::$app->request->get(‘pet_id‘);
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, [‘pet_id‘=>‘无效的参数pet_id‘]);
}

$bluetoothinfo=PetOperate::find()->where([‘id‘=>$pet_id])->asArray()->one();
if($bluetoothinfo[‘bluetooth_id‘]===$healthy_id){
return Helper::format_data(PARAM_ERROR, [‘bluetooth_id‘=>‘已经执行激活操作‘]);
}

$access_token = Yii::$app->request->get(‘access-token‘);
$userinfo = UserOperate::find()->where([‘access_token‘=>$access_token])->asArray()->one();
$user_id = $userinfo[‘id‘];

$healthyinfo = Healthy::find()->where([‘pet_id‘=>$pet_id,‘user_id‘=>$user_id])->asArray()->all();
foreach($healthyinfo as $key=> $v){
$is_activation[] = $v[‘is_activation‘];
}

if(in_array(‘1‘,$is_activation)){
return Helper::format_data(PARAM_ERROR, [‘is_activation‘=>‘请先解除当前绑定‘]);
}else{
$model = new Healthy();
$data = [‘is_activation‘=>‘1‘];
$where = [‘healthy_id‘=>$healthy_id];
$model->updateAll($data,$where);

$model2 = new Pet();
$data2 = [‘bluetooth_id‘=>$healthy_id];
$where2 = [‘id‘=>$pet_id];
$model2->updateAll($data2,$where2);

return Helper::format_data(SUCC,null);

}

}


//体能日志
public function actionEnergyAdd(){
$data = Yii::$app->request->get("work");
$arr = json_decode($data,true);

foreach($arr as $key => $v){
$model = new EnergyLog();
$model->time = $v[‘time‘];
$model->run = $v[‘run‘];
$model->go = $v[‘go‘];
$model->jump = $v[‘jump‘];
$model->sleeping = $v[‘sleeping‘];
$model->pet_id = $v[‘pet_id‘];
$model->save();
}
return Helper::format_data(SUCC,null);
}

 

//体能日志查询列表
public function actionEnergyList(){
$pet_id = Yii::$app->request->get(‘pet_id‘);
if(empty($pet_id)){
return Helper::format_data(PARAM_ERROR, [‘pet_id‘=>‘无效的参数pet_id‘]);
}

$energyinfo = EnergyLog::find()->select([‘time‘,‘run‘,‘go‘,‘jump‘,‘sleeping‘])->where([‘pet_id‘=>$pet_id])->orderBy(‘energy_id ASC‘)->limit(‘7‘)->asArray()->all();

$data[‘healthy‘] = ‘99‘;
$data[‘love‘] = ‘88‘;
$data[‘motion‘] = ‘89‘;
$data[‘social‘] = ‘77‘;
$data[‘neat‘] = ‘67‘;
$data[‘current_time‘] = time();
$data[‘week‘] = $energyinfo;

return Helper::format_data(SUCC,$data);
}

yii2框架增删改查案例

标签:nis   his   asc   cin   model2   on()   color   www   ping   

原文地址:http://www.cnblogs.com/hopelooking/p/6165487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!