123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace app\admin\service;
- use app\common\library\Redis;
- use think\Log;
- use think\exception\HttpException;
- class RedisService
- {
- const REDIS_KEY_SHORT_URL = 'SU:';
- public function __construct()
- {
- }
- /**
- * 获取客服消息自增id
- * @return int
- */
- public function getAutoCustomId()
- {
- $redisAuto = Redis::instanceAuto();
- $newCustomId = $redisAuto->incr('ACID');
- if (!$newCustomId) {
- $strErr = '客服消息自增ID获取失败!';
- Log::error($strErr);
- throw new HttpException(500, $strErr);
- }
- return $newCustomId;
- }
- /**
- * 获取模板消息自增id
- * @return int
- */
- public function getTemplateMessageId()
- {
- $redisAuto = Redis::instanceAuto();
- $newTemplateMessageId = $redisAuto->incr('TMID');
- if (!$newTemplateMessageId) {
- $strErr = '模板消息自增ID获取失败';
- Log::error($strErr);
- throw new HttpException(500, $strErr);
- }
- return $newTemplateMessageId;
- }
- /**
- * 获取推广链接自增id
- * @return int
- */
- public function getAutoReferralId()
- {
- $redisAuto = Redis::instanceAuto();
- $newReferralId = $redisAuto->incr('RFID');
- if (!$newReferralId) {
- $strErr = '推广链接自增ID获取失败!';
- Log::error($strErr);
- throw new HttpException(500, $strErr);
- }
- return $newReferralId;
- }
- /**
- * 获取短链接自增id
- * @return int
- */
- public function getAutoShortUrlId()
- {
- $redisAuto = Redis::instanceAuto();
- $newShortUrlId = $redisAuto->incr('SUID');
- if (!$newShortUrlId) {
- $strErr = '短链接自增ID获取失败!';
- Log::error($strErr);
- throw new HttpException(500, $strErr);
- }
- return $newShortUrlId;
- }
- #region 短链接
- /**
- * 获取短链接redis-key
- * @param $id
- * @return string
- */
- private function _getShortUrlKey($id)
- {
- return self::REDIS_KEY_SHORT_URL . $id;
- }
- /**
- * 从redis获取短链接
- * @param $id
- * @return bool|string
- */
- public function getShortUrl($id)
- {
- $redis = Redis::instance();
- $key = $this->_getShortUrlKey($id);
- return $redis->get($key);
- }
- /**
- * 设置redis短链接
- * @param $id
- * @param $url
- * @return bool
- * @throws \Exception
- */
- public function setShortUrl($id, $url)
- {
- $redis = Redis::instance();
- $key = $this->_getShortUrlKey($id);
- $result = $redis->set($key, $url, 3600);
- if (!$result) {
- throw new \Exception('设置短连接redis失败');
- }
- return $result;
- }
- #endregion
- }
|