cxfoot/modules/admin/models/report/StoreActionForm.php
2023-10-25 17:19:39 +08:00

247 lines
7.0 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2020-11-5
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\admin\models\store;
use app\components\FlashStorage;
use app\components\SiteHelper;
use app\components\ZipFile;
use app\models\auth\RoleUser;
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\models\StoreCityForm;
class StoreActionForm 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();
}
$t = \Yii::$app->db->beginTransaction();
foreach ($this->ids as $item){
$model = Store::findOne([
'id' => $item,
'is_delete' => 0
]);
if($model == null){
return Model::asReturnError('该门店不存在或已被清理');
}
if($model->is_delete == 1){
continue;
}
$model->is_delete = 1;
$model->deleted_at = time();
if(!$model->save()){
$t->rollBack();
return Model::getModelErrorInfo($model);
}
Admin::updateAll(['is_delete' => 1],['is_delete' => 0, 'user_id' => $item]);
// 删除数据表
$redis_name = \Yii::$app->params['cacheKeyPrefix'].":api:store:zset";
try{
$res = \Yii::$app->redis->ZREM($redis_name,$model->id);
}catch (\Exception $e){
}
$exists = StoreUser::find()->where(['store_id' => $model->id, 'is_delete' => 0,])->exists();
if($exists){
$is_update = StoreUser::updateAll(['is_delete' => 1],['store_id' => $model->id, 'is_delete' => 0,]);
if(!$is_update){
$t->rollBack();
return Model::asReturnError('清理门店操作失败');
}
}
}
$t->commit();
// 删除城市缓存
$obj = new StoreCityForm();
$cache_name = $obj->getCacheName();
FlashStorage::deleteCache($cache_name);
return Model::asReturnSuccess('操作成功');
}
public function status_yes()
{
if(!$this->validate()){
return $this->getModelError();
}
foreach ($this->ids as $item){
$model = Store::findOne([
'id' => $item,
'is_delete' => 0,
'status' => 0
]);
if($model == null){
return Model::asReturnError('该门店不存在或已被清理');
}
if($model->status == 1){
continue;
}
$model->status = 1;
$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 = Store::findOne([
'id' => $item,
'is_delete' => 0,
'status' => 1
]);
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 getStoreQrcode()
{
$store = Store::findOne($this->ids);
if($store == null){
return ['code' => 1,'msg' => '门店不存在'];
}
return $this->getQrcode($store);
}
public function getQrcode($store)
{
$path = 'wxqrcode/store_qrcode';
if(!is_dir($path)){
mkdir($path,0777,true);
}
$body['page'] = 'pages/index/indexTwo';
$body['scene'] = "store_id={$store->id}";
$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()
{
$store = Store::find()->select('id,name')->where(['is_delete' => 0])->all();
if($store == null){
return ['code' => 1,'msg' => '门店不存在'];
}
$path = 'wxqrcode/store_qrcode_zip';
if(!is_dir($path)){
mkdir($path,0777,true);
}
$paths = [];
foreach ($store as $value){
$r = $this->getQrcode($value);
if($r['code'] != 0){
return $r['msg'];
}
array_push($paths,['name'=>$value->name.'.png','path' => $r['data']]);
}
if(empty($paths)){
return '无下载数据';
}
$zipName = '门店二维码_'.date('Y年m月d日').'下载' . '.zip'; // 压缩包文件名
return (new ZipFile())->downloadZipImg($zipName,$path,$paths);
}
}