cxfoot/modules/api/models/ScanQrForm.php
2023-10-24 14:54:18 +08:00

479 lines
17 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2020-12-2
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\models;
use app\components\FlashStorage;
use app\models\Coach;
use app\models\DeviceUniqueBindStore;
use app\models\DeviceUniqueBindUser;
use app\models\Order;
use app\models\Store;
use app\models\User;
use app\models\UserBallArm;
class ScanQrForm extends ApiModel
{
public $code;
public $user_id;
public $cx_mch_id;
public $unique;
public $id;
public $type;
public function rules()
{
return [
[['cx_mch_id', 'user_id','id'], 'integer'],
[['code','unique','type'], 'string'],
];
}
public function attributeLabels() {
return [
'code' => '二维码参数',
];
}
/**
* @ Author : Lw
* @ CreateTime : 2022-07-11
* @ Info : 获取二维码信息
*/
public function actionQrInfo(){
if(empty($this->code)){
return $this->apiReturnError('二维码失效');
}
$base64 = base64_decode($this->code);
if(empty($base64)){
return $this->apiReturnError('二维码失效');
}
try{
$json = json_decode($base64,true);
if(empty($json)){
return $this->apiReturnError('二维码失效');
}
if($json['type'] == 'qiugan_qr' && !empty($json['unique'])){
// 球杆检测
$find = UserBallArm::findOne([
'order_id' => $json['unique'],
'is_delete' => 0,
]);
if(intval($find->status) !== 2 && intval($find->status) !== 1){
return $this->apiReturnError('二维码已检测');
}
}
if($json['type'] == 'jiqiu_qr' && !empty($json['unique'])){
// 球杆检测
$find = Order::findOne([
'id' => $json['unique'],
'is_delete' => 0,
]);
if(empty($find->status)){
return $this->apiReturnError('二维码完成');
}
}
}catch (\Exception $e){
return $this->apiReturnError('二维码失效',['error'=>$e->getMessage()]);
}catch (\Error $e){
return $this->apiReturnError('二维码失效',['error'=>$e->getMessage()]);
}
return $this->apiReturnSuccess('ok',$json);
}
/**
* @ Author : Lw
* @ CreateTime : 2022-07-11
* @ Info : 判断设备状态
*/
public function actionQrWindowsDevice(){
if(empty($this->unique)){
return $this->apiReturnError('二维码失效');
}
$windows_api_obj = new WindowsApiForm();
$windows_api_obj->unique = $this->unique;
$cache = $windows_api_obj->getCacheName();
$get = FlashStorage::getCache($cache['bind_store']);
if($get == 'error'){
if(empty($this->user_id)){
return $this->apiReturnError('店铺未绑定');
}
// 判断是否为教练
$find = Coach::findOne([
'user_id' => $this->user_id,
'is_delete' => 0,
]);
if(empty($find) || empty($find['store_id'])){
return $this->apiReturnError('店铺未绑定');
}
return $this->apiReturnSuccess('可以绑定',['title'=>'您是教练身份,是否绑定此设备','type'=>1]);
}
if(empty($get)){
$find = DeviceUniqueBindStore::findOne([
'unique' => $this->unique,
'is_delete' => 0,
]);
if(empty($find)){
FlashStorage::setCache($cache['bind_store'],'error',60*60*24);
return $this->actionQrWindowsDevice();
}else{
$get = json_encode($find->toArray(),JSON_UNESCAPED_UNICODE);
FlashStorage::setCache($cache['bind_store'],$get,60*60*24);
}
}
$json_de = json_decode($get,True);
$find_bind = DeviceUniqueBindUser::find()
->andWhere([
'unique_id' => $json_de['id'],
'is_delete' => 0,
])->andWhere([
'in','status',[1,2]
])->select('*')->one();
if(!empty($find_bind)){
if($find_bind->uid != $this->user_id){
return $this->apiReturnError('此设备已被绑定');
}
}
if($this->type == 'v1'){
// 判断当前用户是否有创建过数据
$find_order = Order::findOne([
'store_id' => $json_de['store_id'],
'status' => 1,
'is_delete' => 0,
]);
if(empty($find_order)){
return $this->apiReturnError('请先购买击球服务',['url_type'=>1]);
}
}
return $this->apiReturnSuccess('可以绑定',['title'=>'是否绑定此设备','type'=>2]);
}
/**
* @ Author : Lw
* @ CreateTime : 2022-07-11
* @ Info : 用户绑定二维码
*/
public function actionQrWindowsDeviceBindUser(){
if(empty($this->unique)){
return $this->apiReturnError('二维码失效');
}
$windows_api_obj = new WindowsApiForm();
$windows_api_obj->unique = $this->unique;
$cache = $windows_api_obj->getCacheName();
$get = FlashStorage::getCache($cache['bind_store']);
if($get == 'error'){
if(empty($this->user_id)){
return $this->apiReturnError('店铺未绑定');
}
// 判断是否为教练
$find = Coach::findOne([
'user_id' => $this->user_id,
'is_delete' => 0,
]);
if(empty($find) || empty($find['store_id'])){
return $this->apiReturnError('店铺未绑定');
}
// 绑定门店
$obj = new DeviceUniqueBindStore();
$obj->unique = $this->unique;
$obj->store_id = $find['store_id'];
$obj->uid = $this->user_id;
$obj->created_at = time();
$obj->is_delete = 0;
$obj->deleted_at = 0;
if($obj->save()){
FlashStorage::deleteCache($cache['bind_store']);
return $this->apiReturnSuccess('店铺绑定成功');
}
return $this->apiReturnError('店铺绑定失败,请重试');
}
if(empty($get)){
$find = DeviceUniqueBindStore::findOne([
'unique' => $this->unique,
'is_delete' => 0,
]);
if(empty($find)){
FlashStorage::setCache($cache['bind_store'],'error',60*60*24);
return $this->actionQrWindowsDeviceBindUser();
}else{
$get = json_encode($find->toArray(),JSON_UNESCAPED_UNICODE);
FlashStorage::setCache($cache['bind_store'],$get,60*60*24);
}
}
$json_de = json_decode($get,True);
$find_bind = DeviceUniqueBindUser::find()
->andWhere([
'unique_id' => $json_de['id'],
'is_delete' => 0,
])->andWhere([
'in','status',[1,2]
])->select('*')->one();
if(!empty($find_bind)){
if($find_bind->uid != $this->user_id){
return $this->apiReturnError('此设备已被绑定');
}
return $this->apiReturnSuccess('绑定成功',['id'=>$find_bind->id]);
}
$order_id = 0; # 订单号
$c_id = $json_de['uid']; # 教练id
if($this->type == 'v1'){
// 判断当前用户是否有创建过数据
$find_order = Order::findOne([
'store_id' => $json_de['store_id'],
'status' => 1,
'is_delete' => 0,
'plugin_sign' => 'hit_ball',
'user_id' => $this->user_id,
]);
if(empty($find_order)){
return $this->apiReturnError('请先购买击球服务',['url_type'=>1]);
}
if(empty($find_order->coach_id)){
return $this->apiReturnError('请出示二维码让门店教练扫码绑定',['url_type'=>2,'id'=>$find_order->id]);
}
$order_id = $find_order->id;
$c_id = $find_order->coach_id;
}
// 解绑其他操作
DeviceUniqueBindUser::updateAll([
'status' => 4,
'end_at' => time(),
],[
'uid' => $this->user_id,
'status' => 1,
'is_delete' => 0,
]);
// 获取已开启的球杆
$select_id = UserBallArm::find()
->andWhere([
'status'=>3,
'is_delete' => 0,
'uid' => $this->user_id,
])->select('id')->column();
// 创建新的
$obj = new DeviceUniqueBindUser();
$obj->unique_id = $json_de['id'];
$obj->uid = $this->user_id;
$obj->c_id = $c_id;
$obj->created_at = time();
$obj->start_at = time();
$obj->end_at = 0;
$obj->status = 1;
$obj->is_delete = 0;
$obj->deleted_at = 0;
$obj->is_show = 0;
$obj->ball_arm_ids = implode(',',$select_id);
$obj->order_id = $order_id;
if($obj->save()){
return $this->apiReturnSuccess('绑定成功',['id'=>$obj->id]);
}
return $this->apiReturnError('绑定失败,请重新扫码',$this->getModelError($obj));
}
/**
* @catalog 小程序扫码
* @title 当前用户是否在场次中
*/
public function actionQrWindowsDeviceUserInit(){
if(empty($this->user_id)){
return $this->apiReturnError('账户未登录');
}
$find_bind = DeviceUniqueBindUser::find()
->andWhere([
'uid' => $this->user_id,
'is_delete' => 0,
])->andWhere([
'in','status',[1,2,3]
])->select('*')->orderBy(['id'=>SORT_DESC])->one();
if(empty($find_bind)){
return $this->apiReturnError('暂无场次');
}
if($find_bind->is_show == 1){
return $this->apiReturnError('暂无场次');
}
return $this->apiReturnSuccess('ok',['id'=>$find_bind->id]);
}
/**
* showdoc
* @catalog 小程序扫码
* @title 判断当前是否结束场次
*/
public function actionQrWindowsDeviceBindStatus(){
if(empty($this->id)){
return $this->apiReturnError('没有传入id');
}
$find = DeviceUniqueBindUser::findOne([
'id' => $this->id,
'uid' => $this->user_id,
'is_delete' => 0,
]);
if(empty($find)){
return $this->apiReturnError('没有找到任何数据');
}
switch ($find->status){
case 1:
case 2:
$return = [
'status' => 1,
'msg' => '进行中',
];
break;
case 3:
$return = [
'status' => 2,
'msg' => '已结束',
];
break;
default:
return $this->apiReturnError('手动结束');
break;
}
return $this->apiReturnSuccess('ok',$return);
}
/**
* @ Author : Lw
* @ CreateTime : 2022-07-28
* @ Info : 击球检测二维码扫码
*/
public function actionQrJiqiuStatus(){
if(empty($this->unique)){
return $this->apiReturnError('二维码失效');
}
$where = ['c.status' => 1, 'c.is_delete' => 0,'c.user_id'=>$this->user_id];
$coachData = Coach::find()->alias('c')
->innerJoin(['s' => Store::tableName()], 'c.store_id=s.id')
->innerJoin(['u' => User::tableName()], 'c.user_id=u.id')
->select('u.id,s.name as store_name,c.coach_photo,u.real_name,s.id as c_id')
->where($where)
->andWhere([
'c.is_delete' => 0,
'c.status' => 1
])->asArray()->one();
if(empty($coachData)){
return $this->apiReturnError('非教练不能操作');
}
// 获取订单
$find_order = Order::findOne([
'id' => $this->unique,
]);
if(empty($find_order)){
return $this->apiReturnError('订单不存在');
}
if($find_order->is_pay == 0){
return $this->apiReturnError('订单未支付');
}
if($find_order->status == 0){
return $this->apiReturnError('订单已完成击球检测,不能操作');
}
if($find_order->plugin_sign != 'hit_ball'){
return $this->apiReturnError('此订单不是击球检测');
}
if(intval($find_order->store_id) !== intval($coachData['c_id'])){
return $this->apiReturnError('非本门店购买服务');
}
// 判断是否正在进行中
$find_device = DeviceUniqueBindUser::find()
->andWhere([
'order_id' => $find_order->id,
'is_delete' => 0,
])->andWhere([
'in','status',[1,2,3],
])->asArray()->one();
if(!empty($find_device)){
if(intval($find_device['status']) === 3){
// 已完成消费,变更状态
$find_order->status = 0;
$find_order->save();
return $this->apiReturnError('订单已完成');
}
return $this->apiReturnError('订单正在进行中');
}
return $this->apiReturnSuccess('绑定击球检测');
// 变更为此教练信息
// $find_order->coach_id = $coachData['id'];
}
/**
* @ Author : Lw
* @ CreateTime : 2022-07-28
* @ Info : 击球检测绑定教练
*/
public function actionQrJiqiuBind(){
if(empty($this->unique)){
return $this->apiReturnError('二维码失效');
}
$where = ['c.status' => 1, 'c.is_delete' => 0,'c.user_id'=>$this->user_id];
$coachData = Coach::find()->alias('c')
->innerJoin(['s' => Store::tableName()], 'c.store_id=s.id')
->innerJoin(['u' => User::tableName()], 'c.user_id=u.id')
->select('u.id,s.name as store_name,c.coach_photo,u.real_name,s.id as c_id')
->where($where)
->andWhere([
'c.is_delete' => 0,
'c.status' => 1
])->asArray()->one();
if(empty($coachData)){
return $this->apiReturnError('非教练不能操作');
}
// 获取订单
$find_order = Order::findOne([
'id' => $this->unique,
]);
if(empty($find_order)){
return $this->apiReturnError('订单不存在');
}
if($find_order->is_pay == 0){
return $this->apiReturnError('订单未支付');
}
if($find_order->status == 0){
return $this->apiReturnError('订单已完成');
}
if($find_order->plugin_sign != 'hit_ball'){
return $this->apiReturnError('此订单不是击球检测');
}
if(intval($find_order->store_id) !== intval($coachData['c_id'])){
return $this->apiReturnError('非本门店购买服务');
}
// 判断是否正在进行中
$find_device = DeviceUniqueBindUser::find()
->andWhere([
'order_id' => $find_order->id,
'is_delete' => 0,
])->andWhere([
'in','status',[1,2,3],
])->asArray()->one();
if(!empty($find_device)){
if(intval($find_device['status']) === 3){
// 已完成消费,变更状态
$find_order->status = 0;
$find_order->save();
return $this->apiReturnError('订单已完成');
}
return $this->apiReturnError('订单正在进行中');
}
// 进入心跳
$key = "api:user_event_data:{$find_order->user_id}";
\Yii::$app->redis->hset($key,'popop_goto_scan',time());
$find_order->coach_id = $coachData['id'];
if($find_order->save()){
return $this->apiReturnSuccess('绑定成功,请让用户扫描设备码');
}
return $this->apiReturnSuccess('绑定失败',$this->getModelError($find_order));
}
}