| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- namespace common\models\mall;
- use common\enums\addons\MallAddonsEnum;
- use common\enums\AddonsEnum;
- use common\enums\RedisKeyEnum;
- use common\events\mall\AddonsInstallEvent;
- use common\models\base\BaseActiveRecord;
- use common\enums\StatusEnum;
- use common\models\common\Addons;
- use common\models\common\PosterCode;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%mall_addons}}".
- *
- *
- * @property int $id 主键
- * @property int|null $mall_id 商城id
- * @property string|null $addons_name 插件名或标识
- * @property int|null $expire_time 过期时间,-1=永久有效
- * @property string|null version 插件版本号
- * @property int|null $addons_status 状态[-1:已过期;0:卸载;1:待安装;2:已安装;]
- * @property int|null $status 状态[-1:删除;0:禁用(卸载);1启用;]
- * @property int|null $created_at 创建时间
- * @property int|null $updated_at 修改时间
- */
- class MallAddons extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%mall_addons}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'expire_time', 'addons_status', 'status', 'created_at', 'updated_at'], 'integer'],
- [['version'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'mall_id' => '商城id',
- 'addons_name' => '插件名或标识',
- 'expire_time' => '过期时间,-1=永久有效',
- 'version' => '插件版本号',
- 'addons_status' => '状态[-1:已过期;0:卸载;1:待安装;2:已安装;]',
- 'status' => '状态[-1:删除;0:禁用;1启用;]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**关联模型 */
- public function getAddons()
- {
- return $this->hasOne(Addons::class, ['name' => 'addons_name'])
- ->where(['is_online' => 1, 'status' => StatusEnum::ENABLED]);
- }
- /**
- * 获取商城已有的插件列表
- * @Author lun
- * @DateTime 2021-09-10
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param array $cond
- * @param array $with
- * @param integer $is_array
- * @return array|null
- */
- public static function getMallAddons(array $cond,array $with=[],int $is_array,string $select='*'){
- $default_cond = [['<>','status',StatusEnum::DELETE]];
- $cond = array_merge($default_cond,$cond);
- $query = self::find()->select($select);
- self::paramPack(['where'=>$cond,'with'=>$with],$query);
- $list = $query->asArray($is_array)->all();
- return $list;
- }
- /**
- * 保存数据
- * @param $params
- * @return bool
- */
- public static function setData($params)
- {
- $trans = Yii::$app->db->beginTransaction();
- try{
- // 判断商城是否已安装此插件
- $model = self::find()->where(['mall_id' => $params['mall_id'] ,'addons_name' => $params['addons_name']])->andWhere(['<>','status',StatusEnum::DELETE])->one();
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->addons_name = $params['addons_name'];
- $model->expire_time = 0;// 初始化暂定为0
- $model->loadDefaultValues();
- }
- $temp_expire_time = $model->expire_time;
- // 设置过期时间
- if ($model->expire_time != -1 && !empty($params['month'])) {
- $time = time();// 当前时间
- if ($params['month'] == -1) {// 永久有效
- $model->expire_time = $params['month'];
- } elseif (empty($model->expire_time)) {// 首次生成过期时间
- $model->expire_time = strtotime("+{$params['month']} month", $time);
- } elseif ($model->expire_time > 0) {// 已存在过期时间
- if ($model->expire_time <= $time) {// 购买
- $model->expire_time = strtotime("+{$params['month']} month", $time);
- } else {// 续费
- $model->expire_time = strtotime("+{$params['month']} month", $model->expire_time);
- }
- }
- // 若应用状态为已过期则改为待安装
- if ($model->addons_status == MallAddonsEnum::EXPIRE && $model->expire_time > $time) $model->addons_status = MallAddonsEnum::TO_BE_INSTALL;
- }
- // 保存数据
- if(!$model->load($params, '') || !$model->save(false)){
- throw new \Exception($model->getErrorMessage());
- }
- $trans->commit();
- // 存入缓存
- self::addonsIntoCache($params['mall_id'], $params['addons_name'], $model->toArray());
- return true;
- }catch(\Exception $e){
- $trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 将已安装的插件存入缓存中
- * @Author: lun
- * @DateTime: 2022/2/24 0024 10:29
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $addons_name
- * @param array $data
- * @return bool
- */
- public static function addonsIntoCache($mall_id, $addons_name, $data = [])
- {
- if (empty($data)) return false;
- // 存入缓存
- $time = time();
- $cache = Yii::$app->cache;
- $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL,$addons_name . $mall_id,true);
- $expire_time = $data['expire_time'] == -1 ? null : $data['expire_time'] - $time;
- $cache->set($cache_key, $data['version'], $expire_time);
- // 将所有商城存入redis有序集合中
- $expire_time = $data['expire_time'] == -1 ? AddonsEnum::ADDONS_MAX_TIME : $data['expire_time'];
- $redisKey = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL_ARR, ':' . $mall_id,true);
- Yii::$app->redis->zadd($redisKey, $expire_time, $addons_name);
- return true;
- }
- /**
- * 是否安装了该插件
- * @param $mall_id
- * @param $name
- * @return int
- */
- public static function isInstall($mall_id,$name){
- $cache = Yii::$app->cache;
- $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL, $name . $mall_id,true);
- if ($cache->get($cache_key)) return true;
- // 缓存无法查询则对数据表进行查询
- $detail = Addons::getOne([['name' => $name], ['is_online' => 1]], true, ['mallAddons' => function ($query) use ($mall_id) {
- $query->where(['mall_id' => $mall_id])->andWhere(['<>', 'status', StatusEnum::DELETE]);
- }]);
- $mallAddons = $detail['mallAddons'] ?? [];
- $is_install = 0;
- if (!empty($mallAddons) && in_array($mallAddons['addons_status'], [MallAddonsEnum::TO_BE_INSTALL,MallAddonsEnum::INSTALL])) {
- if ($mallAddons['expire_time'] == MallAddonsEnum::EXPIRE) {
- $is_install = 1;
- } else {
- if ($mallAddons['expire_time'] > time()) $is_install = 1;
- }
- }
- // 若数据表中显示插件已安装则将插入缓存中
- if ($is_install == 1) {
- self::addonsIntoCache($mall_id,$name,$mallAddons);
- }
- return $is_install;
- }
- /**
- * 获取商城中所有有效的商城
- * @Author: lun
- * @DateTime: 2022/1/14 0014 15:02
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @return array|bool|string|null
- * @throws Exception
- */
- public static function getValidAddons($mall_id)
- {
- $redis = Yii::$app->redis;
- $redisKey = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL_ARR, ':' . $mall_id,true);
- //key格式:addonsInstallArr:1
- $arr = $redis->executeCommand('zrangebyscore', [$redisKey, time(), '+inf']);
- // 正常情况下商城都会有插件,很少出现没有安装插件情况,所以缓存没有插件的时候需要到数据库中获取
- if (empty($arr)) $arr = self::getInstallAddonsByMysql($mall_id);
- return $arr;
- }
- /**
- * 从数据库中读取商城已安装的有效插件
- * @param $mall_id
- * @return array
- */
- public static function getInstallAddonsByMysql($mall_id)
- {
- $data = [];
- $enable_addons = self::find()->select('mall_id,addons_name,expire_time,version')
- ->where(['mall_id' => $mall_id, 'addons_status' => MallAddonsEnum::INSTALL, 'status' => StatusEnum::ENABLED])
- ->asArray()->all();
- if ($enable_addons) {
- $time = time();
- foreach ($enable_addons as $addons) {
- // 判断插件是否过期
- if ($addons['expire_time'] == -1 || $addons['expire_time'] > $time) {
- $res = self::addonsIntoCache($mall_id, $addons['addons_name'], $addons);// 插入redis缓存中
- if ($res === true) $data[] = $addons['addons_name'];// 插入成功返回对应的插件标识
- }
- }
- }
- return $data;
- }
- /**
- * 获取商城已安装的有效插件名称
- * @param $mall_id
- * @param $name
- * @return array
- */
- public static function getEnableAddonsName($mall_id){
- return self::find()->select('addons_name')->where(['mall_id' => $mall_id, 'addons_status' => MallAddonsEnum::INSTALL, 'status'=>StatusEnum::ENABLED])->column();
- }
- /**
- * 获取个人主页可显示插件
- * @Author bing
- * @DateTime 2021-12-23 17:32:25
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $mall_id
- * @param [type] $addons_name_map
- * @return array
- */
- public static function getPersonalHomeAddons($mall_id,$addons_name_map){
- return self::find()->select('addons_name')->where(['mall_id' => $mall_id,'status'=>StatusEnum::ENABLED,'addons_name'=>$addons_name_map])->column();
- }
- /**
- * 应用卸载
- * @Author: lun
- * @DateTime: 2022/3/1 0001 10:09
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $addons_name
- * @throws \Exception
- */
- public static function Uninstall($mall_id, $addons_name)
- {
- try {
- // 查询应用是否已安装
- $addons = MallAddons::findOne(['mall_id' => $mall_id, 'addons_name' => $addons_name, 'addons_status' => MallAddonsEnum::INSTALL]);
- if (empty($addons)) throw new \Exception('应用无法卸载');
- // 判断应用是否已过期
- if ($addons->expire_time > 0 && $addons->expire_time < time()) {
- $addons->addons_status = MallAddonsEnum::EXPIRE;
- if (!$addons->save()) throw new \Exception(MallAddons::getStaticError());
- throw new \Exception('应用已过期无法卸载');
- }
- // 保存数据
- $addons->addons_status = MallAddonsEnum::UNINSTALL;
- if (!$addons->save()) throw new \Exception(MallAddons::getStaticError());
- // 删除缓存
- $cache = Yii::$app->cache;
- $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL, $addons_name . $mall_id,true);
- $cache->delete($cache_key);
- // 删除redis中有效的商城
- $redisKey = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL_ARR, ':' . $mall_id,true);
- Yii::$app->redis->zrem($redisKey, $addons_name);
- // 初始化卸载插件
- $uninstall_key = "addons\\" . $addons_name . "\Uninstall";
- $uninstall = new $uninstall_key();
- $uninstall->runApp($mall_id, $addons_name);
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 批量安装插件
- * @Author: lun
- * @DateTime: 2023/2/1 0001 11:20
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mallId
- * @param $addons
- * @return bool
- */
- public static function batchInstall($mallId, $addons)
- {
- try {
- if(empty($addons)) return true;
- // 获取目前已安装的插件
- $mall_addons = MallAddons::find()->select('addons_name')->where(['mall_id' => $mallId])->andWhere(['>', 'addons_status', 0])->column();
- $install_addons = array_diff($addons, $mall_addons);// 待安装的插件
- $uninstall_addons = array_diff($mall_addons, $addons);// 需要删除的插件
- // 安装插件
- if ($install_addons) {
- foreach ($install_addons as $addons_name) {
- // 安装插入数据
- $res = MallAddons::setData([
- 'mall_id' => $mallId,
- 'addons_name' => $addons_name,
- 'addons_status' => MallAddonsEnum::INSTALL,
- 'month' => StatusEnum::DELETE,
- ]);
- if ($res == false) throw new \Exception(MallAddons::getStaticError());
- // 初始化安装插件
- $install_key = "addons\\" . $addons_name . "\Install";
- $install = new $install_key();
- $install->runApp($mallId, $addons_name);
- // 异步初始化商城
- $event = new AddonsInstallEvent($mallId);
- $install_addons_data = [];
- $install_addons_data['mall_id'] = $mallId;
- $install_addons_data['addons_name'] = $addons_name;
- Yii::$app->services->events->dispatch($event,$install_addons_data,$event->listeners);
- }
- }
- // 删除插件
- if ($uninstall_addons) {
- foreach ($uninstall_addons as $addons_name) {
- self::Uninstall($mallId, $addons_name);
- // 如果删除插件为门店管家。这删除该商城下所有二维码链接
- if ($addons_name == 'StoreSteward') {
- PosterCode::updateAll(['applet_code' => ''], ['mall_id' => $mallId, 'status' => StatusEnum::ENABLED]);
- }
- }
- self::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $mallId, 'addons_name' => $uninstall_addons]);
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|