MCommonApp.Class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * 用户常用应用Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2020/01/11
  7. * Time: 09:53
  8. */
  9. namespace JinDouYun\Model\SystemSettings;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use JinDouYun\Cache\CacheMan;
  13. use JinDouYun\Dao\SystemSettings\DCommonApp;
  14. class MCommonApp
  15. {
  16. // 授权回调地址
  17. private $objDCommonApp;
  18. private $objCacheMan;
  19. private $cacheKey = 'CommonApp';
  20. private $userCenterId;
  21. private $enterpriseId;
  22. public function __construct($enterpriseId, $userCenterId)
  23. {
  24. $this->enterpriseId = $enterpriseId;
  25. $this->userCenterId = $userCenterId;
  26. $this->objDCommonApp = new DCommonApp('default');
  27. //按照企业分表
  28. $this->objDCommonApp->setTable($this->objDCommonApp->get_Table().'_'.$this->enterpriseId);
  29. //引入redis
  30. $this->objCacheMan = new CacheMan($this->enterpriseId, $this->userCenterId);
  31. }
  32. /**
  33. * 修改常用应用
  34. * @param $params
  35. * @param $where
  36. * @return ResultWrapper
  37. */
  38. public function updateCommonApp($params, $where)
  39. {
  40. $dbResult = $this->objDCommonApp->get($where);
  41. if($dbResult === false){
  42. return ResultWrapper::fail($this->objDCommonApp->error(), ErrorCode::$dberror);
  43. }
  44. $commonData = $dbResult;
  45. unset($dbResult);
  46. if($commonData){
  47. //修改
  48. $updateData = [
  49. 'content' => json_encode($params),
  50. 'updateTime' => time(),
  51. ];
  52. $dbResult = $this->objDCommonApp->update($updateData, $where);
  53. //删除redis
  54. $this->objCacheMan->delCacheHash($where['userCenterId'], $this->cacheKey);
  55. }else{
  56. //新增
  57. $addCommonData = [
  58. 'userCenterId' => $where['userCenterId'],
  59. 'enterpriseId' => $where['enterpriseId'],
  60. 'content' => json_encode($params),
  61. 'updateTime' => time(),
  62. 'createTime' => time(),
  63. ];
  64. $dbResult = $this->objDCommonApp->insert($addCommonData);
  65. //增加redis
  66. $this->objCacheMan->addCacheHash($where['userCenterId'], $addCommonData['content'], $this->cacheKey);
  67. }
  68. if($dbResult === false){
  69. return ResultWrapper::fail($this->objDCommonApp->error(), ErrorCode::$dberror);
  70. }
  71. return ResultWrapper::success($dbResult);
  72. }
  73. /**
  74. * 常用应用详情
  75. * @param $where
  76. * @return ResultWrapper
  77. */
  78. public function getCommonAppInfo($where)
  79. {
  80. //获取redis
  81. $cacheResult = $this->objCacheMan->getCacheHash($where['userCenterId'], $this->cacheKey);
  82. if(!empty($cacheResult)){
  83. return ResultWrapper::success($cacheResult);
  84. }
  85. $dbResult = $this->objDCommonApp->get($where);
  86. if ($dbResult === false) {
  87. return ResultWrapper::fail($this->objDCommonApp->error(), ErrorCode::$dberror);
  88. }
  89. if(empty($dbResult)){
  90. return ResultWrapper::success($dbResult);
  91. }
  92. $dbResult['content'] = json_decode($dbResult['content'], true);
  93. //保存redis
  94. if($dbResult){
  95. $this->objCacheMan->addCacheHash($where['userCenterId'], $dbResult['content'], $this->cacheKey);
  96. }
  97. return ResultWrapper::success($dbResult['content']);
  98. }
  99. }