MallAddons.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace common\models\mall;
  3. use common\enums\addons\MallAddonsEnum;
  4. use common\enums\AddonsEnum;
  5. use common\enums\RedisKeyEnum;
  6. use common\events\mall\AddonsInstallEvent;
  7. use common\models\base\BaseActiveRecord;
  8. use common\enums\StatusEnum;
  9. use common\models\common\Addons;
  10. use common\models\common\PosterCode;
  11. use Yii;
  12. use yii\db\Exception;
  13. /**
  14. * This is the model class for table "{{%mall_addons}}".
  15. *
  16. *
  17. * @property int $id 主键
  18. * @property int|null $mall_id 商城id
  19. * @property string|null $addons_name 插件名或标识
  20. * @property int|null $expire_time 过期时间,-1=永久有效
  21. * @property string|null version 插件版本号
  22. * @property int|null $addons_status 状态[-1:已过期;0:卸载;1:待安装;2:已安装;]
  23. * @property int|null $status 状态[-1:删除;0:禁用(卸载);1启用;]
  24. * @property int|null $created_at 创建时间
  25. * @property int|null $updated_at 修改时间
  26. */
  27. class MallAddons extends BaseActiveRecord
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return '{{%mall_addons}}';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id', 'expire_time', 'addons_status', 'status', 'created_at', 'updated_at'], 'integer'],
  43. [['version'], 'string'],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => '主键',
  53. 'mall_id' => '商城id',
  54. 'addons_name' => '插件名或标识',
  55. 'expire_time' => '过期时间,-1=永久有效',
  56. 'version' => '插件版本号',
  57. 'addons_status' => '状态[-1:已过期;0:卸载;1:待安装;2:已安装;]',
  58. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  59. 'created_at' => '创建时间',
  60. 'updated_at' => '修改时间',
  61. ];
  62. }
  63. /**关联模型 */
  64. public function getAddons()
  65. {
  66. return $this->hasOne(Addons::class, ['name' => 'addons_name'])
  67. ->where(['is_online' => 1, 'status' => StatusEnum::ENABLED]);
  68. }
  69. /**
  70. * 获取商城已有的插件列表
  71. * @Author lun
  72. * @DateTime 2021-09-10
  73. * @copyright: Copyright (c) 2020 广东七件事集团
  74. * @param array $cond
  75. * @param array $with
  76. * @param integer $is_array
  77. * @return array|null
  78. */
  79. public static function getMallAddons(array $cond,array $with=[],int $is_array,string $select='*'){
  80. $default_cond = [['<>','status',StatusEnum::DELETE]];
  81. $cond = array_merge($default_cond,$cond);
  82. $query = self::find()->select($select);
  83. self::paramPack(['where'=>$cond,'with'=>$with],$query);
  84. $list = $query->asArray($is_array)->all();
  85. return $list;
  86. }
  87. /**
  88. * 保存数据
  89. * @param $params
  90. * @return bool
  91. */
  92. public static function setData($params)
  93. {
  94. $trans = Yii::$app->db->beginTransaction();
  95. try{
  96. // 判断商城是否已安装此插件
  97. $model = self::find()->where(['mall_id' => $params['mall_id'] ,'addons_name' => $params['addons_name']])->andWhere(['<>','status',StatusEnum::DELETE])->one();
  98. if (empty($model)) {
  99. $model = new self();
  100. $model->mall_id = $params['mall_id'];
  101. $model->addons_name = $params['addons_name'];
  102. $model->expire_time = 0;// 初始化暂定为0
  103. $model->loadDefaultValues();
  104. }
  105. $temp_expire_time = $model->expire_time;
  106. // 设置过期时间
  107. if ($model->expire_time != -1 && !empty($params['month'])) {
  108. $time = time();// 当前时间
  109. if ($params['month'] == -1) {// 永久有效
  110. $model->expire_time = $params['month'];
  111. } elseif (empty($model->expire_time)) {// 首次生成过期时间
  112. $model->expire_time = strtotime("+{$params['month']} month", $time);
  113. } elseif ($model->expire_time > 0) {// 已存在过期时间
  114. if ($model->expire_time <= $time) {// 购买
  115. $model->expire_time = strtotime("+{$params['month']} month", $time);
  116. } else {// 续费
  117. $model->expire_time = strtotime("+{$params['month']} month", $model->expire_time);
  118. }
  119. }
  120. // 若应用状态为已过期则改为待安装
  121. if ($model->addons_status == MallAddonsEnum::EXPIRE && $model->expire_time > $time) $model->addons_status = MallAddonsEnum::TO_BE_INSTALL;
  122. }
  123. // 保存数据
  124. if(!$model->load($params, '') || !$model->save(false)){
  125. throw new \Exception($model->getErrorMessage());
  126. }
  127. $trans->commit();
  128. // 存入缓存
  129. self::addonsIntoCache($params['mall_id'], $params['addons_name'], $model->toArray());
  130. return true;
  131. }catch(\Exception $e){
  132. $trans->rollBack();
  133. self::setStaticError($e->getMessage());
  134. return false;
  135. }
  136. }
  137. /**
  138. * 将已安装的插件存入缓存中
  139. * @Author: lun
  140. * @DateTime: 2022/2/24 0024 10:29
  141. * @Copyright: copyright (c) 2021 广东七件事集团
  142. * @param $mall_id
  143. * @param $addons_name
  144. * @param array $data
  145. * @return bool
  146. */
  147. public static function addonsIntoCache($mall_id, $addons_name, $data = [])
  148. {
  149. if (empty($data)) return false;
  150. // 存入缓存
  151. $time = time();
  152. $cache = Yii::$app->cache;
  153. $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL,$addons_name . $mall_id,true);
  154. $expire_time = $data['expire_time'] == -1 ? null : $data['expire_time'] - $time;
  155. $cache->set($cache_key, $data['version'], $expire_time);
  156. // 将所有商城存入redis有序集合中
  157. $expire_time = $data['expire_time'] == -1 ? AddonsEnum::ADDONS_MAX_TIME : $data['expire_time'];
  158. $redisKey = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL_ARR, ':' . $mall_id,true);
  159. Yii::$app->redis->zadd($redisKey, $expire_time, $addons_name);
  160. return true;
  161. }
  162. /**
  163. * 是否安装了该插件
  164. * @param $mall_id
  165. * @param $name
  166. * @return int
  167. */
  168. public static function isInstall($mall_id,$name){
  169. $cache = Yii::$app->cache;
  170. $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL, $name . $mall_id,true);
  171. if ($cache->get($cache_key)) return true;
  172. // 缓存无法查询则对数据表进行查询
  173. $detail = Addons::getOne([['name' => $name], ['is_online' => 1]], true, ['mallAddons' => function ($query) use ($mall_id) {
  174. $query->where(['mall_id' => $mall_id])->andWhere(['<>', 'status', StatusEnum::DELETE]);
  175. }]);
  176. $mallAddons = $detail['mallAddons'] ?? [];
  177. $is_install = 0;
  178. if (!empty($mallAddons) && in_array($mallAddons['addons_status'], [MallAddonsEnum::TO_BE_INSTALL,MallAddonsEnum::INSTALL])) {
  179. if ($mallAddons['expire_time'] == MallAddonsEnum::EXPIRE) {
  180. $is_install = 1;
  181. } else {
  182. if ($mallAddons['expire_time'] > time()) $is_install = 1;
  183. }
  184. }
  185. // 若数据表中显示插件已安装则将插入缓存中
  186. if ($is_install == 1) {
  187. self::addonsIntoCache($mall_id,$name,$mallAddons);
  188. }
  189. return $is_install;
  190. }
  191. /**
  192. * 获取商城中所有有效的商城
  193. * @Author: lun
  194. * @DateTime: 2022/1/14 0014 15:02
  195. * @Copyright: copyright (c) 2021 广东七件事集团
  196. * @param $mall_id
  197. * @return array|bool|string|null
  198. * @throws Exception
  199. */
  200. public static function getValidAddons($mall_id)
  201. {
  202. $redis = Yii::$app->redis;
  203. $redisKey = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL_ARR, ':' . $mall_id,true);
  204. //key格式:addonsInstallArr:1
  205. $arr = $redis->executeCommand('zrangebyscore', [$redisKey, time(), '+inf']);
  206. // 正常情况下商城都会有插件,很少出现没有安装插件情况,所以缓存没有插件的时候需要到数据库中获取
  207. if (empty($arr)) $arr = self::getInstallAddonsByMysql($mall_id);
  208. return $arr;
  209. }
  210. /**
  211. * 从数据库中读取商城已安装的有效插件
  212. * @param $mall_id
  213. * @return array
  214. */
  215. public static function getInstallAddonsByMysql($mall_id)
  216. {
  217. $data = [];
  218. $enable_addons = self::find()->select('mall_id,addons_name,expire_time,version')
  219. ->where(['mall_id' => $mall_id, 'addons_status' => MallAddonsEnum::INSTALL, 'status' => StatusEnum::ENABLED])
  220. ->asArray()->all();
  221. if ($enable_addons) {
  222. $time = time();
  223. foreach ($enable_addons as $addons) {
  224. // 判断插件是否过期
  225. if ($addons['expire_time'] == -1 || $addons['expire_time'] > $time) {
  226. $res = self::addonsIntoCache($mall_id, $addons['addons_name'], $addons);// 插入redis缓存中
  227. if ($res === true) $data[] = $addons['addons_name'];// 插入成功返回对应的插件标识
  228. }
  229. }
  230. }
  231. return $data;
  232. }
  233. /**
  234. * 获取商城已安装的有效插件名称
  235. * @param $mall_id
  236. * @param $name
  237. * @return array
  238. */
  239. public static function getEnableAddonsName($mall_id){
  240. return self::find()->select('addons_name')->where(['mall_id' => $mall_id, 'addons_status' => MallAddonsEnum::INSTALL, 'status'=>StatusEnum::ENABLED])->column();
  241. }
  242. /**
  243. * 获取个人主页可显示插件
  244. * @Author bing
  245. * @DateTime 2021-12-23 17:32:25
  246. * @copyright: Copyright (c) 2020 广东七件事集团
  247. * @param [type] $mall_id
  248. * @param [type] $addons_name_map
  249. * @return array
  250. */
  251. public static function getPersonalHomeAddons($mall_id,$addons_name_map){
  252. return self::find()->select('addons_name')->where(['mall_id' => $mall_id,'status'=>StatusEnum::ENABLED,'addons_name'=>$addons_name_map])->column();
  253. }
  254. /**
  255. * 应用卸载
  256. * @Author: lun
  257. * @DateTime: 2022/3/1 0001 10:09
  258. * @Copyright: copyright (c) 2021 广东七件事集团
  259. * @param $mall_id
  260. * @param $addons_name
  261. * @throws \Exception
  262. */
  263. public static function Uninstall($mall_id, $addons_name)
  264. {
  265. try {
  266. // 查询应用是否已安装
  267. $addons = MallAddons::findOne(['mall_id' => $mall_id, 'addons_name' => $addons_name, 'addons_status' => MallAddonsEnum::INSTALL]);
  268. if (empty($addons)) throw new \Exception('应用无法卸载');
  269. // 判断应用是否已过期
  270. if ($addons->expire_time > 0 && $addons->expire_time < time()) {
  271. $addons->addons_status = MallAddonsEnum::EXPIRE;
  272. if (!$addons->save()) throw new \Exception(MallAddons::getStaticError());
  273. throw new \Exception('应用已过期无法卸载');
  274. }
  275. // 保存数据
  276. $addons->addons_status = MallAddonsEnum::UNINSTALL;
  277. if (!$addons->save()) throw new \Exception(MallAddons::getStaticError());
  278. // 删除缓存
  279. $cache = Yii::$app->cache;
  280. $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL, $addons_name . $mall_id,true);
  281. $cache->delete($cache_key);
  282. // 删除redis中有效的商城
  283. $redisKey = RedisKeyEnum::suffix(RedisKeyEnum::ADDONS_INSTALL_ARR, ':' . $mall_id,true);
  284. Yii::$app->redis->zrem($redisKey, $addons_name);
  285. // 初始化卸载插件
  286. $uninstall_key = "addons\\" . $addons_name . "\Uninstall";
  287. $uninstall = new $uninstall_key();
  288. $uninstall->runApp($mall_id, $addons_name);
  289. return true;
  290. } catch (\Exception $e) {
  291. self::setStaticError($e->getMessage());
  292. return false;
  293. }
  294. }
  295. /**
  296. * 批量安装插件
  297. * @Author: lun
  298. * @DateTime: 2023/2/1 0001 11:20
  299. * @Copyright: copyright (c) 2021 广东七件事集团
  300. * @param $mallId
  301. * @param $addons
  302. * @return bool
  303. */
  304. public static function batchInstall($mallId, $addons)
  305. {
  306. try {
  307. if(empty($addons)) return true;
  308. // 获取目前已安装的插件
  309. $mall_addons = MallAddons::find()->select('addons_name')->where(['mall_id' => $mallId])->andWhere(['>', 'addons_status', 0])->column();
  310. $install_addons = array_diff($addons, $mall_addons);// 待安装的插件
  311. $uninstall_addons = array_diff($mall_addons, $addons);// 需要删除的插件
  312. // 安装插件
  313. if ($install_addons) {
  314. foreach ($install_addons as $addons_name) {
  315. // 安装插入数据
  316. $res = MallAddons::setData([
  317. 'mall_id' => $mallId,
  318. 'addons_name' => $addons_name,
  319. 'addons_status' => MallAddonsEnum::INSTALL,
  320. 'month' => StatusEnum::DELETE,
  321. ]);
  322. if ($res == false) throw new \Exception(MallAddons::getStaticError());
  323. // 初始化安装插件
  324. $install_key = "addons\\" . $addons_name . "\Install";
  325. $install = new $install_key();
  326. $install->runApp($mallId, $addons_name);
  327. // 异步初始化商城
  328. $event = new AddonsInstallEvent($mallId);
  329. $install_addons_data = [];
  330. $install_addons_data['mall_id'] = $mallId;
  331. $install_addons_data['addons_name'] = $addons_name;
  332. Yii::$app->services->events->dispatch($event,$install_addons_data,$event->listeners);
  333. }
  334. }
  335. // 删除插件
  336. if ($uninstall_addons) {
  337. foreach ($uninstall_addons as $addons_name) {
  338. self::Uninstall($mallId, $addons_name);
  339. // 如果删除插件为门店管家。这删除该商城下所有二维码链接
  340. if ($addons_name == 'StoreSteward') {
  341. PosterCode::updateAll(['applet_code' => ''], ['mall_id' => $mallId, 'status' => StatusEnum::ENABLED]);
  342. }
  343. }
  344. self::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $mallId, 'addons_name' => $uninstall_addons]);
  345. }
  346. return true;
  347. } catch (\Exception $e) {
  348. self::setStaticError($e->getMessage());
  349. return false;
  350. }
  351. }
  352. }