yii2框架增删改查案例
//解除绑定蓝牙
//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框架增删改查案例的更多相关文章
- yii2.0增删改查实例讲解
yii2.0增删改查实例讲解一.创建数据库文件. 创建表 CREATE TABLE `resource` ( `id` int(10) NOT NULL AUTO_INCREMENT, `textur ...
- 快速入门GreenDao框架并实现增删改查案例
大家的项目中不可避免的使用到SQLite,为此我们要花费心思编写一个增删改查框架.而一个好的ORM框架则能够给我们带来极大的方便,今天给大家讲解一个非常火热的ORM-GreenDao. 基本概念 Gr ...
- ssm项目框架搭建(增删改查案例实现)——(SpringMVC+Spring+mybatis项目整合)
Spring 常用注解 内容 一.基本概念 1. Spring 2. SpringMVC 3. MyBatis 二.开发环境搭建 1. 创建 maven 项目 2. SSM整合 2.1 项目结构图 2 ...
- Mybatis的简介+简单实现增删改查案例
@ 目录 总结内容 1. 基本概念 2. Mybatis的使用 需求 配置文件简介 总结 总结内容 1. 基本概念 Mybatis是一款优秀的持久层框架,它支持定制化SQL.存储过程以及高级映射.My ...
- YII2生成增删改查
下载完成后在basic/db.php配置数据库参数. 1.配置虚拟主机后进入YII入口文件 index.php 进行get传值 ?r=gii ,进入创建界面 2.点击 Model Generator下 ...
- Spring4.0+Hibernate4.0+Struts2.3整合包括增删改查案例,解决整合中出现的异常
源码下载:http://download.csdn.net/detail/cmcc_1234/7034775 ======================Application.xml======== ...
- YII2的增删改查
insert into table (field1,field2)values('1','2');delete from table where condition update table s ...
- IOS Sqlite用户界面增删改查案例
1.案例简单介绍 对SQLite操作进行了简单的封装,将对数据表操作转变成对对象的操作,并通过UI界面完毕对用户表的增.删.改.查,执行界面例如以下图所看到的 a 2.项目project文件夹 Use ...
- Laravel框架——增删改查
增: //save返回true false $res = new member(); res->username = 'a'; $res->password = 'b'; dd($res- ...
随机推荐
- sql入门基础
好用的mysql客户端 https://www.quora.com/What-is-the-best-free-DB-schema-design-tool https://www.quora.com/ ...
- [SonarQube]小结
新的项目用了这个,以前从来没用过,配置了一下,看看这个到底是个什么东东. 先学习一下英文单词: sonar 声纳, qube 方盒子, 连起来应该叫声纳盒, SonarQube一看就是一个监测诊断设备 ...
- js windows.open()模拟POST提交
function openPostWindow (url,name, data1, data2) { var tempForm = document.createE ...
- CocoaPods for Xcode
http://www.cnblogs.com/MrRed/p/5728933.html 非常方便的Xcode pods插件.可以很方便的在Xcode通过pods安装各种objective-c第三方库, ...
- 更新与升级 FreeBSD
https://www.freebsd.org/doc/zh_CN/books/handbook/updating-upgrading-freebsdupdate.html 安全补丁存储在远程的机器上 ...
- notepad++快捷键大全
Ctrl+C 复制 Ctrl+X 剪切 Ctrl+V 粘贴 Ctrl+Z 撤消 Ctrl+Y 恢复 Ctrl+A 全选 Ctrl+F 键查找对话框启动 Ctrl+H 查找/替换对话框 Ctrl+D 复 ...
- 常见的java类
String System StringBuilder Thread Math ArrayList LinkedList HashMap HashSet Scanner Calendar Date F ...
- p6 备忘录
1.报表执行过程:PROC_PM_RP_Implent,PROC_PM_RP_Implent 2.新增用户无法获取p6 计划,主要是因为没有项目信息.分类码授权(计划分类).
- [原创]在Framelayout中放置button控件出现的覆盖问题
android Framelayout(帧布局)是很常用的布局,主要用来处理需要多个view叠加显示的情况. 然而在使用中,我发现Framelayout中的Button控件,会挡住所有其他控件,而不论 ...
- android.graphic.Path
类path是一个封装的几何学路径包括直线,二次曲线,三次曲线.它可以通过函数canvas.drawPath(path, paint)画出来,可以通过填充方式或者画线方式(由paint的style决定) ...