322 lines
10 KiB
PHP
322 lines
10 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\components\XtOpenApi;
|
||
use app\models\Balance;
|
||
use app\models\BalanceLog;
|
||
use app\models\Coach;
|
||
use app\models\DeviceUniqueBindStore;
|
||
use app\models\DeviceUniqueBindUser;
|
||
use app\models\DevList;
|
||
use app\models\Level;
|
||
use app\models\Order;
|
||
use app\models\OrderDetail;
|
||
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 $sn;
|
||
public $id;
|
||
public $type;
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['cx_mch_id', 'user_id','id'], 'integer'],
|
||
[['code','unique','type','sn'], 'string'],
|
||
];
|
||
}
|
||
|
||
public function attributeLabels() {
|
||
return [
|
||
'code' => '二维码参数',
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @ Author : Lw
|
||
* @ CreateTime : 2022-07-11
|
||
* @ Info : 获取二维码信息
|
||
*/
|
||
public function actionQrInfo(){
|
||
if(empty($this->sn)){
|
||
return $this->apiReturnError('请求错误');
|
||
}
|
||
switch ($this->type){
|
||
case "xt": # 小兔
|
||
return $this->actionXtInfo();
|
||
break;
|
||
case 'yp': # 友朋
|
||
return $this->actionYpInfo();
|
||
break;
|
||
}
|
||
return $this->apiReturnError('请求错误');
|
||
}
|
||
|
||
/**
|
||
* @ 小兔开门信息
|
||
*/
|
||
public function actionXtInfo(){
|
||
// 获取设备
|
||
$find_store = Store::findOne([
|
||
'sn' => $this->sn,
|
||
'is_delete' => 0,
|
||
]);
|
||
if(empty($find_store)){
|
||
return $this->apiReturnError('云盒不是大门信息,不能扫码开门');
|
||
}
|
||
$arr = [
|
||
'money' => floatval($find_store->open_money),
|
||
'status' => 1,
|
||
'title' => $find_store->name,
|
||
'content' => '点击开门,成功后开自动打开',
|
||
'sn' => $this->sn,
|
||
'type' => 'xt',
|
||
'btn_str' => "点我开门",
|
||
];
|
||
// 如果金额不为0,则需要进行充值
|
||
if(!empty($arr['money']) && !empty($this->user_id)){
|
||
// 判断今日是否有待使用订单
|
||
$find_order = Order::find()
|
||
->alias('o')
|
||
->join('inner join',['od'=>OrderDetail::tableName()],'o.id=od.order_id')
|
||
->andWhere([
|
||
'o.user_id' => $this->user_id,
|
||
'o.plugin_sign' => 'box_book',
|
||
'o.is_pay' => 1,
|
||
'o.is_delete' => 0,
|
||
'o.cancel_status' => 0,
|
||
'o.store_id' => $find_store->id,
|
||
])
|
||
->select('od.start_time,od.end_time')
|
||
->orderBy(['o.id'=>SORT_DESC])
|
||
->asArray()
|
||
->one();
|
||
if(!empty($find_order)){
|
||
//判断时间是否在今日
|
||
if(time()>strtotime($find_order['start_time']) && time()<strtotime($find_order['end_time'])){
|
||
// 不需要充值
|
||
$arr['money'] = 0;
|
||
}
|
||
}
|
||
// 判断今日是否有充值10元以上
|
||
if(!empty($arr['money'])){
|
||
$find_balance = BalanceLog::find()->andWhere([
|
||
'scene' => Balance::$cxBalanceSceneUserWallet,
|
||
'user_id' => $this->user_id,
|
||
'type' => 1,
|
||
'is_delete' => 0,
|
||
])->andWhere([
|
||
'>=','created_at',strtotime(date('Y-m-d')),
|
||
])->sum('money')??0;
|
||
if($find_balance > $arr['money']){
|
||
// 不需要充值
|
||
$arr['money'] = 0;
|
||
}
|
||
}
|
||
// 判断用户当前vip等级是否达标
|
||
if(!empty($arr['money'])){
|
||
$find_user = User::findOne([
|
||
'id' => $this->user_id,
|
||
]);
|
||
if(!empty($find_user->level_id)){
|
||
$find_level = Level::findOne([
|
||
'id' => $find_user->level_id,
|
||
]);
|
||
# vip等级大于多少级别则不需要充值
|
||
if($find_level->level >= 3){
|
||
// 不需要充值
|
||
$arr['money'] = 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if(!empty($arr['money'])){
|
||
$arr['status'] = 4; # 需要进入充值
|
||
$arr['btn_str'] = "充值{$arr['money']}元";
|
||
}
|
||
if(empty($this->user_id)){
|
||
$arr['status'] = 5; # 需要登录
|
||
$arr['btn_str'] = "登录";
|
||
$arr['content'] = "请先登录";
|
||
}
|
||
return $this->apiReturnSuccess('ok',$arr);
|
||
}
|
||
|
||
/**
|
||
* @ 友朋开门信息
|
||
*/
|
||
public function actionYpInfo(){
|
||
// 获取设备
|
||
$find_dev = DevList::findOne([
|
||
'dev_id' => $this->sn,
|
||
'is_delete' => 0,
|
||
]);
|
||
if(empty($find_dev)){
|
||
return $this->apiReturnError('设备不存在');
|
||
}
|
||
if(empty($find_dev->store_id)){
|
||
return $this->apiReturnError('设备暂未绑定门店');
|
||
}
|
||
$find_store = Store::findOne([
|
||
'id' => $find_dev->store_id,
|
||
]);
|
||
$arr = [
|
||
'money' => 0,
|
||
'status' => 1,
|
||
'title' => $find_store->name,
|
||
'content' => '开门需注意:拿到的酒水在关门后统一结算,订单会推送',
|
||
'sn' => $this->sn,
|
||
'type' => 'yp',
|
||
"btn_str" => "点我开柜",
|
||
];
|
||
// 判断设备状态
|
||
/*$find_order = Order::find()->andWhere([
|
||
'dev_id' => $find_dev->id,
|
||
'is_delete' => 1,
|
||
'status' => 1,
|
||
'plugin_sign' => 'goods',
|
||
'is_recycle' => 0,
|
||
])->one();
|
||
if(!empty($find_order)){
|
||
$arr['status'] = 2; // 进行中
|
||
$arr['btn_str'] = "货柜使用中"; // 进行中
|
||
$arr['content'] = "货柜使用中,请稍后重试";
|
||
}*/
|
||
// 当前用户的订单状态
|
||
/*$find_order = Order::find()->andWhere([
|
||
'is_delete' => 1,
|
||
'status' => 1,
|
||
'plugin_sign' => 'goods',
|
||
'user_id' => $this->user_id,
|
||
])->one();
|
||
if(!empty($find_order)){
|
||
$arr['status'] = 2; // 进行中
|
||
$arr['btn_str'] = "订单生成中"; // 进行中
|
||
}*/
|
||
if($arr['status'] !== 2){
|
||
$exists = Order::find()->where(['user_id' => $this->user_id,'status' => 1,'plugin_sign' => 'goods','is_delete'=>0])->exists();
|
||
if($exists){
|
||
$arr['status'] = 4; // 进行中
|
||
$arr['btn_str'] = "等待支付"; // 进行中
|
||
$arr['content'] = "您有一笔待支付订单,请先支付";
|
||
}
|
||
}
|
||
if(empty($this->user_id)){
|
||
$arr['status'] = 5; # 需要登录
|
||
$arr['btn_str'] = "登录";
|
||
$arr['content'] = "请先登录";
|
||
}
|
||
return $this->apiReturnSuccess('ok',$arr);
|
||
}
|
||
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog 小程序扫码
|
||
* @title 小兔开门
|
||
* @description 本接口提供设备开门操作,仅限于大门
|
||
* @method get
|
||
* @url /api/scan-qr/xt-open
|
||
* @param sn 必选 string 设备唯一码
|
||
* @return {"code":0,"msg":"ok"}
|
||
* @remark
|
||
*/
|
||
public function actionXtOpen(){
|
||
// 获取设备
|
||
$find_store = Store::findOne([
|
||
'sn' => $this->sn,
|
||
'is_delete' => 0,
|
||
]);
|
||
if(empty($find_store)){
|
||
return $this->apiReturnError('云盒不是大门信息,不能扫码开门');
|
||
}
|
||
// 判断今日是否有充值10元以上
|
||
$arr = [
|
||
'money' => floatval($find_store->open_money),
|
||
];
|
||
// 如果金额不为0,则需要进行充值
|
||
if(!empty($arr['money'])){
|
||
// 判断今日是否有待使用订单
|
||
$find_order = Order::find()
|
||
->alias('o')
|
||
->join('inner join',['od'=>OrderDetail::tableName()],'o.id=od.order_id')
|
||
->andWhere([
|
||
'o.user_id' => $this->user_id,
|
||
'o.plugin_sign' => 'box_book',
|
||
'o.is_pay' => 1,
|
||
'o.is_delete' => 0,
|
||
'o.cancel_status' => 0,
|
||
'o.store_id' => $find_store->id,
|
||
])
|
||
->select('od.start_time,od.end_time')
|
||
->orderBy(['o.id'=>SORT_DESC])
|
||
->asArray()
|
||
->one();
|
||
if(!empty($find_order)){
|
||
//判断时间是否在今日
|
||
if(time()>strtotime($find_order['start_time']) && time()<strtotime($find_order['end_time'])){
|
||
// 不需要充值
|
||
$arr['money'] = 0;
|
||
}
|
||
}
|
||
// 判断今日是否有充值10元以上
|
||
if(!empty($arr['money'])){
|
||
$find_balance = BalanceLog::find()->andWhere([
|
||
'scene' => Balance::$cxBalanceSceneUserWallet,
|
||
'user_id' => $this->user_id,
|
||
'type' => 1,
|
||
'is_delete' => 0,
|
||
])->andWhere([
|
||
'>=','created_at',strtotime(date('Y-m-d')),
|
||
])->sum('money')??0;
|
||
if($find_balance > $arr['money']){
|
||
// 不需要充值
|
||
$arr['money'] = 0;
|
||
}
|
||
}
|
||
// 判断用户当前vip等级是否达标
|
||
if(!empty($arr['money'])){
|
||
$find_user = User::findOne([
|
||
'id' => $this->user_id,
|
||
]);
|
||
if(!empty($find_user->level_id)){
|
||
$find_level = Level::findOne([
|
||
'id' => $find_user->level_id,
|
||
]);
|
||
# vip等级大于多少级别则不需要充值
|
||
if($find_level->level >= 3){
|
||
// 不需要充值
|
||
$arr['money'] = 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if(!empty($arr['money'])){
|
||
return $this->apiReturnError('请先充值后再开门');
|
||
}
|
||
$obj = new XtOpenApi();
|
||
$obj->user_id = $this->user_id;
|
||
return $obj->Open($find_store->sn);
|
||
}
|
||
|
||
|
||
} |