| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- /**
- * @link:http://www.gdqijianshi.com/
- * @copyright: Copyright (c) 2020 广东七件事集团
- * 基础model
- * Author: zal
- * Date: 2020-04-08
- * Time: 15:12
- */
- namespace common\models\mall;
- use backend\models\AdminRole;
- use common\enums\AppEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\events\mall\MallCreateEvent;
- use common\helpers\Base64Helper;
- use common\helpers\StringHelper;
- use common\models\addons\MallAddonsComboRelation;
- use common\models\backend\Admin;
- use common\models\base\BaseActiveRecord;
- use common\models\common\MallSetting;
- use common\models\rbac\AuthItem;
- use common\models\rbac\AuthItemChild;
- use common\models\rbac\AuthRole;
- use PHPUnit\Util\Exception;
- use Yii;
- /**
- * This is the model class for table "{{%mall}}".
- *
- *
- * @property integer $id
- * @property integer $created_at
- * @property integer $updated_at
- * @property integer $expired_at
- * @property string $name
- * @property string $contact_tel
- * @property integer $admin_id
- * @property integer $is_recycle
- * @property integer $is_disable
- * @property integer $status
- * @property integer $is_show_copyright
- * @property Admin $admin
- * @property MallSetting[] $option
- */
- class Mall extends BaseActiveRecord
- {
- public $options;
- /** @var int 是否回收 0否 */
- const IS_RECYLE_NO = 0;
- /** @var int 是否回收 1是 */
- const IS_RECYLE_YES = 1;
- /** @var int 是否禁用 0否 */
- const IS_DISABLE_ON = 0;
- /** @var int 是否回收 1是 */
- const IS_DISABLE_OFF = 1;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%mall}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['created_at', 'updated_at', 'expired_at'], 'safe'],
- [['admin_id', 'is_recycle', 'is_disable','status','is_show_copyright','is_compliance'], 'integer'],
- [['name','contact_tel'], 'string', 'max' => 20],
- [['mall_sign'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'name' => '商城名称',
- 'contact_tel' => '客服电话',
- 'admin_id' => '管理员ID',
- 'is_recycle' => '是否回收',
- 'is_disable' => '是否禁用',
- 'expired_at' => '过期时间',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'status' => '状态',
- 'is_show_copyright' => '是否显示版权:1;显示;0不显示',
- 'is_compliance' => '合规模式:1;是;0否',
- ];
- }
- public function getAdmin()
- {
- return $this->hasOne(Admin::class, ['id' => 'admin_id']);
- }
- public function getMallHost()
- {
- return $this->hasOne(MallHost::class, ['mall_id' => 'id']);
- }
- /**
- * 获取有效商城列表
- * @Author bing
- * @DateTime 2020-12-23 19:42:28
- * @param boolean $is_array
- * @return mixed|void
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- public static function getEnableMallList($is_array = true, $cond = [])
- {
- $default_cond = [['=','status',StatusEnum::ENABLED],['=','is_recycle',StatusEnum::DISABLED]];
- $cond = array_merge($default_cond,$cond);
- $query = self::find();
- self::paramPack(['where'=>$cond],$query);
- $data = $query->asArray($is_array)->all();
- return $data;
- }
- public static function getAllMallIds()
- {
- return self::find()->select('id')->column();
- }
- public function getAdonsComboRelation()
- {
- return $this->hasOne(MallAddonsComboRelation::class, ['mall_id' => 'id'])->select('id,addons_combo_id,mall_id');
- }
- public static function getEnableMallIds()
- {
- return self::find()->where(array('status' => StatusEnum::ENABLED, 'is_recycle' => 0,))->select('id')->column();
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/1/10 0010 9:57
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return bool
- */
- public function beforeSave($insert){
- $old_name = $this->getOldAttribute('name');
- if($this->name != $old_name){
- Yii::$app->db->createCommand()->update(
- MallSetting::tableName(),
- ['value'=> $this->name, 'updated_at' => time()],
- ['mall_id' => $this->id,'group' => 'basic','name' =>'name']
- )->execute();
- }
- return parent::beforeSave($insert);
- }
- /**
- * 保存数据
- * @param array $params
- * @return bool|Mall
- */
- public static function setData(array $params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id']]);
- if (empty($model)) throw new Exception('服务不存在或已删除');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- $params['admin_id'] = 0;
- if(!empty(Yii::$app->user->identity))$params['admin_id'] = Yii::$app->user->identity->getId();
- }
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- if(empty($params['id'])) {
- while (true){
- $mall_sign = StringHelper::createRandomString(3);
- $res = Mall::findOne(['mall_sign'=>$mall_sign]);
- if(empty($res)) break;
- }
- if ($model->name == '商城系统') $model->name = $model->name.$model->id;
- $model->mall_sign = $mall_sign;
- $model->save();
- //创建商城
- //添加角色
- $param =[];
- $model_auth_role = new AuthRole();
- $param['title'] = '管理员';
- $param['app_id'] = AppEnum::BACKEND;
- $param['pid'] = 0;
- $param['sort'] = 1;
- $param['is_default'] = 1;
- $param['status'] = 1;
- $param['mall_id'] = $model->id;
- $model_auth_role->attributes = $param;
- $res = $model_auth_role->save();
- if($res==false) throw new Exception('创建默认角色错误');
- //添加默认权限
- $values_arr = [];
- $item_list = AuthItem::findAll(['cate_id'=> AppEnum::MALL,'status'=>StatusEnum::ENABLED]);
- foreach ($item_list as $val){
- $info = [
- $model_auth_role->id,
- $val['id'],
- AppEnum::BACKEND,
- $val['name'],
- $val['is_addon'],
- $val['addons_name'],
- time(),
- time()
- ];
- $values_arr[] = $info;
- }
- $item_list = AuthItem::findAll(['is_addon'=> 1,'status'=>StatusEnum::ENABLED]);
- foreach ($item_list as $val){
- $info = [
- $model_auth_role->id,
- $val['id'],
- AppEnum::BACKEND,
- $val['name'],
- $val['is_addon'],
- $val['addons_name'],
- time(),
- time()
- ];
- $values_arr[] = $info;
- }
- $fields = ['role_id', 'item_id', 'app_id', 'name', 'is_addon', 'addons_name','created_at', 'updated_at'];
- Yii::$app->db->createCommand()->batchInsert(
- AuthItemChild::tableName(),
- $fields,
- $values_arr
- )->execute();
- //添加账号
- $param =[];
- $param['title'] = '管理员';
- $param['remark'] = $params['remark']??'';
- $param['mall_id'] = $model->id;
- $param['username'] =$params['mobile'];
- $param['account'] = $params['mobile'];
- $param['mobile'] = $params['mobile'];
- $param['is_default'] = 1;
- $param['mch_id'] = 0;//预留
- $param['store_id'] = 0;//预留
- $param['role_id'] = $model_auth_role->id;
- $param['admin_type'] = Admin::ADMIN_TYPE_OPERATE;
- $param['source'] = $params['source']??0;
- $param['reference_id'] = $params['reference_id']??0;
- if (empty($params['password'])) {
- $password = substr($params['mobile'],-6,6);
- $param['password'] = Yii::$app->security->generatePasswordHash($password);
- } else {
- $param['password'] = Yii::$app->security->generatePasswordHash($params['password']);
- }
- $model_admin = new Admin();
- $model_admin->attributes = $param;
- $res = $model_admin->save();
- if($res==false) throw new Exception($model_admin->getErrorMessage().'创建默认账号错误');
- //添加账号与角色关联
- $res = AdminRole::setData(['id' => '', 'role_id' => $param['role_id'], 'admin_id' => $model_admin->id]);
- if ($res === false) throw new Exception(AdminRole::getStaticError());
- } else {
- // 修改密码
- if (!empty($params['password'])) {
- $admin = Admin::getDefaultByMallId($model->id);
- $res = $admin->setNewPassword($params['password']);
- if ($res === false) throw new Exception('修改密码失败');
- }
- }
- if(isset($params['is_recycle']) && !$params['is_recycle'])Admin::updateAll(['status' => StatusEnum::ENABLED],['id'=>$model->admin_id]);
- if(!empty($params['id'])&&!empty($params['status'])&&$params['status']==StatusEnum::DELETE){
- Admin::updateAll(['status'=>StatusEnum::DELETE],['mall_id'=>$params['id']]);
- }
- // 当用户在回收站恢复是恢复其对于管理员账号
- if(is_numeric($params['is_recycle']) && $params['id'] && $params['is_recycle']==0) {
- $admin_model = Admin::findOne(['mall_id' => $model->id]);
- $admin_model->status = StatusEnum::ENABLED;
- $admin_model->save();
- }
- $t->commit();
- $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::BACKEND_MALL,$model['mall_sign'],true);
- !empty($params['is_disable'])&&$params['is_disable']==1?Yii::$app->cache->delete($cache_key):Yii::$app->cache->set($cache_key, $model->toArray());
- if(empty($params['id'])){
- // 异步初始化商城
- $event = new MallCreateEvent($model->id);
- $init['id'] = $model->id;
- $init['operator_id'] = $params['operator_id'];
- $init['operator_username'] = $params['operator_username'];
- $init['addons_combo_id'] = $params['addons_combo_id'] ?? 0;
- $init['is_sync_mall_data'] = $params['is_sync_mall_data'] ?? 0;
- Yii::$app->services->events->dispatch($event,$init,$event->listeners);
- }
- return $model;
- } catch (Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @desc:获取商城信息,优先读取缓存
- * @Author: hua
- * @Date: 2022/1/13
- * @Time: 14:44
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_sign
- * @return mixed|void
- */
- public static function getMallInfo($mall_sign){
- $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::BACKEND_MALL,$mall_sign,true);
- $data = Yii::$app->cache->get($cache_key);
- if(empty($data)) $data = Mall::getOne([['mall_sign' => $mall_sign], ['status' => StatusEnum::ENABLED]],true);
- return $data;
- }
- /**
- * 获取总页数
- * @Author: lun
- * @DateTime: 2023/4/15 0015 17:50
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return false|float|int
- */
- public static function getMallTotalPage($params)
- {
- $query = self::find();
- self::paramPack($params, $query);
- $count = $query->count();
- return $count == 0 ? 0 : ceil($count / $params['limit']);
- }
- /**
- * 获取有期限的商城
- *
- * @return array|static[]
- */
- public static function getTimeLimitMall()
- {
- $condition = [
- 'and',
- ['>', 'expired_at', '0'],
- ['=', 'status', '1'],
- ];
- return static::find()->where($condition)->all();
- }
- /**
- * 获取商城是否过期
- *
- * @param integer $mall_id
- * @return boolean
- */
- public static function getIsExpired(int $mall_id)
- {
- $key = sprintf('mall_is_expired:%s', $mall_id);
- $redis = Yii::$app->redis;
- if (($is_sxpired = $redis->get($key)) !== null) return (boolean) $is_sxpired;
- $condition = [
- 'and',
- ['=', 'id', $mall_id],
- ['>', 'expired_at', '0'],
- ['=', 'status', '1'],
- ['<', 'expired_at', time()],
- ];
- $is_sxpired = static::find()->where($condition)->select('id')->exists();
- $redis->setex($key, 10, $is_sxpired);
- return $is_sxpired;
- }
- /**
- * 获取商城总数
- * @return bool|int|string|null
- */
- public static function getMallCount()
- {
- return static::find()->where(['status' => [StatusEnum::ENABLED], 'is_recycle' => 0])->count();
- }
- }
|