cxgj/modules/admin/models/ball/BallActionForm.php
2023-11-27 09:45:13 +08:00

242 lines
7.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @author Any
* @description KISS
* @date 2020-11-5
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\admin\models\ball;
use app\components\FlashStorage;
use app\components\SiteHelper;
use app\components\ZipFile;
use app\models\auth\RoleUser;
use app\models\BallCart;
use app\models\Coach;
use app\models\District;
use app\models\Model;
use app\models\Store;
use app\models\StoreUser;
use app\models\User;
use app\models\Admin;
use app\modules\admin\models\AdminModel;
use app\models\common\CommonUserEditForm;
use app\modules\api\components\Mqtt;
use app\modules\api\models\StoreCityForm;
class BallActionForm extends AdminModel
{
public $ids;
public function rules()
{
return [
[['ids'], 'required'],
[['ids'], 'safe'],
];
}
public function attributeLabels()
{
return [
'ids' => '选择项',
];
}
public function delete()
{
if (!$this->validate()) {
return $this->getModelError();
}
foreach ($this->ids as $item) {
$model = BallCart::findOne([
'id' => $item,
'is_delete' => 0
]);
if ($model == null) {
break;
}
if ($model->is_delete == 1) {
continue;
}
$model->is_delete = 1;
$model->deleted_at = time();
if (!$model->save()) {
return Model::getModelErrorInfo($model);
}
}
return Model::asReturnSuccess('操作成功');
}
public function status_yes()
{
if (!$this->validate()) {
return $this->getModelError();
}
foreach ($this->ids as $item) {
$model = BallCart::findOne([
'id' => $item,
'is_delete' => 0,
]);
if ($model == null) {
return Model::asReturnError('该球车不存在或已被清理');
}
if ($model->status == 0) {
continue;
}
$model->status = 0;
$model->updated_at = time();
if (!$model->save()) {
return Model::getModelErrorInfo($model);
}
}
return Model::asReturnSuccess('操作成功');
}
public function status_no()
{
if (!$this->validate()) {
return $this->getModelError();
}
foreach ($this->ids as $item) {
$model = BallCart::findOne([
'id' => $item,
'is_delete' => 0,
]);
if ($model == null) {
return Model::asReturnError('该球车不存在或已被清理');
}
if ($model->status == 2) {
continue;
}
if ($model->status == 1) {
return Model::asReturnError('该球车正在租赁中,请等还车后在停用');
}
$model->status = 2;
$model->updated_at = time();
if (!$model->save()) {
return Model::getModelErrorInfo($model);
}
// 写redis变更当前状态未0
$redis_ball_cart_status_name = "api:cxaibc:ball_cart:{$model->ball_number}";
$get = \Yii::$app->redis->get($redis_ball_cart_status_name);
$redis_value = "0:0:admin";
$OPERATION_ENABLE = 0;
$OPERATION_LEVEL = 0;
if (!empty($get)) {
$explode = explode(':', $get);
$explode[0] = "0";
$OPERATION_LEVEL = $explode[1];
$redis_value = implode(":", $explode);
}
\Yii::$app->redis->set($redis_ball_cart_status_name, $redis_value);
}
return Model::asReturnSuccess('操作成功');
}
public function getBallQrcode()
{
$ball = BallCart::findOne($this->ids);
if($ball == null){
return ['code' => 1,'msg' => '该球车不存在'];
}
$store = Store::findOne($ball->store_id);
if($store == null){
return ['code' => 1,'msg' => '该球车所在门店不存在'];
}
return $this->getQrcode($store,$ball);
}
public function getQrcode($store,$ball)
{
$path = 'wxqrcode/ball_qrcode';
if(!is_dir($path)){
mkdir($path,0777,true);
}
$body['page'] = 'pages/hire/hiretwo';
$body['scene'] = "s={$store->id}&n={$ball->ball_number}";
$json_body = md5(json_encode($body));
$path.='/'.$json_body.'.png';
if(!file_exists($path)){
$res = self::wxmpQrcode($body);
if (json_encode($res) !== false) {
$res = json_decode($res,true);
return ['code' => 1,'msg' => $res['errmsg']];
} else {
$file = fopen($path, "w");//打开文件准备写入
fwrite($file, $res);//写入,$res为图片二进制内容
fclose($file);//关闭
//图片是否存在
if (!file_exists($path)) {
return ['code' => 1,'msg' => '生成二维码失败'];
} else {
return ['code' => 0,'msg' => 'ok','data' => SiteHelper::getFullUrl($path)];
}
}
}else{
return ['code' => 0,'msg' => 'ok','data' => SiteHelper::getFullUrl($path)];
}
}
//获取小程序二维码
public function wxmpQrcode($body)
{
$plugin = new \app\models\common\PluginService();
$wxmpService = $plugin->getWxmpService(0);
$accessToken = $wxmpService->getAccessToken();
if(empty($accessToken)){
$accessToken = $wxmpService->getAccessToken(true);
}
$body = json_encode($body);
return $wxmpService->Qrcode->getWxappQrcode($accessToken,$body);
}
public function BatchQrcode()
{
$ball = BallCart::find()->select('id,store_id,name,ball_number')->where(['is_delete' => 0])->all();
if($ball == null){
return ['code' => 1,'msg' => '该球车不存在'];
}
$path = 'wxqrcode/ball_qrcode_zip';
if(!is_dir($path)){
mkdir($path,0777,true);
}
$paths = [];
foreach ($ball as $value){
$store = Store::findOne($value->store_id);
if($store == null){
return $value->ball_number.'编号球车所在门店不存在';
}
$r = $this->getQrcode($store,$value);
if($r['code'] != 0){
return $r['msg'];
}
$name = $store->name.'_'.$value->ball_number.'.png';
array_push($paths,['name'=>$name,'path' => $r['data']]);
}
if(empty($paths)){
return '无下载数据';
}
$zipName = '球车二维码_'.date('Y年m月d日').'下载' . '.zip'; // 压缩包文件名
return (new ZipFile())->downloadZipImg($zipName,$path,$paths);
}
}