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- ...
随机推荐
- oracle数据库使用三个月的总结
存储过程定义,举个例子如下: create or replace procedure test_person(id in Number, Ename In Varchar2, age In Varch ...
- 事务处理-回滚(转账操作)(转自http://www.cnblogs.com/void-m/p/6143540.html)
JDBC事务处理-四大原则 原子性一致性隔离性持久性 第一步:实现转账操作 假设在账户中,盖伦有余额5000元,赵信有余额2000元, 盖伦要向赵信转账1000元. 1 2 3 4 5 6 7 8 9 ...
- 提高Baidu Map聚合的效率
百度的MAP的例子里提供了一个聚合效果,地址是http://developer.baidu.com/map/jsdemo.htm#c1_4 ,效果图如下图: 这个效果很赞,但效率很低,当数据量达到50 ...
- Delphi以及三方控件的源代码规模
这些项目大多数使用C++或者C编写,使用SourceCounter-3.5.33.73工具来统计源代码数量,本来是这里下载的: https://code.google.com/p/boomworks/ ...
- android 屏幕分辨率 更改
手头上有一个320x240的LCD.运行android时,显示内容过大,需要更改屏幕的分辨率. 参考链接 http://www.bkjia.com/Androidjc/899396.html http ...
- CS193P - 2016年秋 第一讲 课程简介
Stanford 的 CS193P 课程可能是最好的 ios 入门开发视频了.iOS 更新很快,这个课程的最新内容也通常是一年以内发布的. 最新的课程发布于2016年春季.目前可以通过 iTunes ...
- ios10 xcode8 适配的那些事
1.首先open Url 废弃了. http://www.tuicool.com/articles/jiMr2qA
- react 调用 native 的callShareAllFunc()方法,实现分享
let shareName = { '0': '微信', '1': '朋友圈', '2': '新浪微博', '3': ' QQ', '4': 'QQ空间'};render(){ //分享YztApp. ...
- Linux opencv安装与编译
参考http://blog.csdn.net/solomon1558/article/details/51967280 1安装cmake以及依赖库 $ sudo apt-get install cma ...
- Android 多媒体视频播放一( 多媒体理解与经验分享)
前言 说到android的多媒体,一把辛酸一把泪,当初听说会多媒体的比较牛掰,公司也有需求,于是乎我也积极的加入研究android多媒体的行列,记得以前刚接触的时候,最开始还是比较头大的,主要是但是很 ...