123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2018/11/29
- * Time: 下午5:49
- */
- namespace app\main\service;
- use app\admin\model\Custom;
- use app\admin\model\CustomUrl;
- use app\common\constants\BigData;
- use app\common\constants\Message;
- use app\common\constants\ShortUrl;
- use app\common\library\Redis;
- use app\common\model\CustomQrcode;
- use app\main\constants\ApiConstants;
- use app\main\constants\CustomConstants;
- use app\main\constants\ErrorCodeConstants;
- use app\main\helper\ArrayHelper;
- use function GuzzleHttp\Psr7\parse_query;
- use think\Config;
- class CustomService extends BaseService
- {
- /**
- * @var CustomService
- */
- protected static $self = NULL;
- /**
- * @return CustomService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @return \think\Model
- */
- public function getCustomQrcodeModel()
- {
- return new CustomQrcode();
- }
- /**
- * @return \think\Model
- */
- public function getCustomModel()
- {
- return new Custom;
- }
- /**
- * @return \think\Model
- */
- public function getCustomUrlModel()
- {
- return new CustomUrl;
- }
- /**
- * @return \think\Model
- */
- public function getShortUrlModel() {
- return model("ShortUrl");
- }
- /**
- * 检查ext参数是客服消息
- * @param $ext
- * @return \app\main\model\object\ReturnObject
- */
- public function checkExtIsCustom($ext = '')
- {
- if (!$ext) {
- $ext = UserService::instance()->getUserInfo()->ext;
- }
- if ($ext) {
- $ext_json = json_decode($ext, true);
- if ($ext_json &&
- array_key_exists('mark', $ext_json) &&
- in_array($ext_json['mark'], [BigData::BIG_DATA_MARK_IMAGE_TEXT, BigData::BIG_DATA_MARK_LINK])
- ) {
- return $this->setData(true)->getReturn();
- }
- }
- return $this->setData(false)->getReturn();
- }
- /**
- * 返回最后一条发送成功的客服消息的资源
- * @param $adminId
- * @param $appid
- * @return array
- */
- public function getLastSendSuccessCustom($adminId, $appid)
- {
- $result = [];
- $redisKey = 'CTC:'.$adminId;
- if (Redis::instance()->exists($redisKey)) {
- $result = json_decode(Redis::instance()->get($redisKey), true);
- } else {
- //48小时
- $model = $this->getCustomModel()
- ->where('admin_id', $adminId)
- ->where('sendtime', '>=', time()-3600*24*2)
- ->where('statue', 'eq', 'hidden')
- ->order("sendtime desc")
- ->find();
- $replace = [];
- if (!empty($model)) {
- $content = '';
- if ($model['message_type'] == Message::MESSAGE_TYPE_IMAGE_TEXT) {
- //图文
- $content = $model['message_json'];
- } elseif ($model['message_type'] == Message::MESSAGE_TYPE_LINK) {
- //文字
- $content = $model['message_text'];
- }
- if (empty($content) || is_array($content)) {
- Redis::instance()->set($redisKey, json_encode([]), 600);
- return [];
- }
- $shortIds = [];
- if (preg_match_all("|j\\\/(\d+)|", $content, $m)) {
- $shortIds = $m[1];
- } else if (preg_match_all("|/j/(\d+)|", $content, $m)) {
- $shortIds = $m[1];
- } else {
- Redis::instance()->set($redisKey, json_encode([]), 600);
- return [];
- }
- $shortUrlHost = Config::get("site.short_url_host");
- if (empty($shortUrlHost)) {
- //取渠道业务域名
- $shortUrlHost = trim(getCurrentDomain($adminId, null, [], true),'/');
- }
- $maps['id'] = ['in', $shortIds];
- $rows = $this->getShortUrlModel()->where($maps)->select();
- foreach ($rows as $index => $row) {
- $goUrl = sprintf('%s/%s/%s', $shortUrlHost, ShortUrl::SHORT_URL_PATH, $row['id']);
- $url = $row['url'];
- $parse = parse_url($url);
- $host = $parse['host'] ?? '';
- $path = $parse['path'] ?? '';
- if (preg_match('/^' . $appid . '/', $host)) {
- if (array_key_exists('query', $parse)) {
- $query = parse_query($parse['query']);
- if (!empty($query)) {
- if (array_key_exists('book_id', $query)) {
- $replace = [
- 'book_id' => $query['book_id'],
- 'url' => $goUrl,
- ];
- break;
- } elseif (array_key_exists('t', $query)) {
- $replace = [
- 'book_id' => '',
- 'url' => $goUrl,
- 'referral_id' => $query['t'],
- ];
- break;
- }
- }
- }
- if (!empty($path)) {
- $path = explode('/', $parse['path']);
- if ($keyIndex = array_search('t', $path)) {
- $push_id = $path[$keyIndex+1];
- $replace = [
- 'book_id' => '',
- 'url' => $goUrl,
- 'referral_id' => $push_id,
- ];
- break;
- }
- }
- }
- }
- if (!empty($replace)) {
- $replace['custom_id'] = $model['id'];
- //查询小说标题
- if (isset($replace['referral_id']) && $replace['referral_id']) {
- $referralinfo = model('referral')->getone($replace['referral_id'], false);
- if (empty($referralinfo) || !$referralinfo['book_id']) {
- Redis::instance()->set($redisKey, json_encode([]), 600);
- return [];
- }
- $replace['book_id'] = $referralinfo['book_id'];
- }
- $bookinfo = BookService::instance()->getBookModel()->BookInfo($replace['book_id']);
- if ($bookinfo['state'] == '0') {
- //下架
- Redis::instance()->set($redisKey, json_encode([]), 600);
- return [];
- } else if ($bookinfo['state'] == '-1') {
- //入库
- if ($bookinfo['cansee'] == '0') {
- //不可见
- Redis::instance()->set($redisKey, json_encode([]), 600);
- return [];
- }
- }
- $replace['book_name'] = $bookinfo['name'];
- $replace['image'] = $bookinfo['image'];
- $replace['description'] = $bookinfo['description'];
- //占位符替换
- if (strpos($url, '{$ophost}')) {
- //占位符转换
- $replace['url'] = UrlService::instance()->replaceReferralHost($adminId, $replace['url'], false)->data;
- }
- }
- }
- Redis::instance()->set($redisKey, json_encode($replace), 600);
- $result = $replace;
- }
- return $result;
- }
- public function getCustomUserJson($params)
- {
- $user_json = [];
- if (isset($params['sub_from']) && isset($params['sub_to'])) {
- $params['sub_from'] = strtotime($params['sub_from']);
- $params['sub_to'] = strtotime($params['sub_to']);
- if ($params['sub_from'] >= $params['sub_to']) {
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('结束时间不得小于开始时间');
- }
- if(isset($params['user_json'])){
- $user_json = json_decode($params['user_json'], true);
- }
- $user_json['subscirbe_range'] = $params['sub_from'] . '-' . $params['sub_to'];
- }
- $user_json = json_encode($user_json, JSON_UNESCAPED_UNICODE);
- return $this->setData($user_json)->getReturn();
- }
- /**
- * 获取满足条件的客服消息id列表
- * @param $where
- * @param $map
- * @param int $offset
- * @param int $limit
- * @return \app\main\model\object\ReturnObject
- */
- public function getCustomIds($where, $map, $offset = 0, $limit = 20)
- {
- $result = [];
- $result['total'] = model('custom')
- ->join('custom_url', 'custom_url.custom_id=custom.id')
- ->join('admin', 'custom_url.official_account_id=admin.id')
- ->where($where)
- ->where($map)
- ->group('custom.id')
- ->count();
- $result['ids'] = model('custom')
- ->join('custom_url', 'custom_url.custom_id=custom.id')
- ->join('admin', 'custom_url.official_account_id=admin.id')
- ->where($where)
- ->where($map)
- ->limit($offset, $limit)
- ->group('custom.id')
- ->order('custom.id', 'desc')
- ->column('custom.id');
- return $this->setData($result)->getReturn();
- }
- /**
- * 获取客服消息统计信息
- * @param $customIds
- * @return \app\main\model\object\ReturnObject
- */
- public function getCustomCollectData($customIds)
- {
- $customList = model('custom')
- ->alias('c')
- ->whereIn('c.id', $customIds)
- ->join('admin a', 'c.admin_id=a.id')
- ->order('custom.id', 'desc')
- ->column('c.id,c.title,a.nickname,a.username,c.sendtime,c.send_num');
- $customCollect = model('custom_url')
- ->alias('cu')
- ->join('custom_url_collect cuc', 'cu.custom_id=cuc.custom_id AND cu.idx=cuc.idx', 'LEFT')
- ->whereIn('cu.custom_id', $customIds)
- ->order('idx', 'asc')
- ->column('cu.id,cu.title,cu.custom_id,cu.idx,cu.type,cu.book_id,cu.book_name,cuc.uv,cuc.recharge_money,cu.official_account_name');
- foreach ($customList as $index => $item) {
- $customList[$index]['collect'] = [];
- }
- if (VisitLimitService::instance()->checkMigrated()) {
- $result = ApiService::instance()->getCollectFromApi(ApiConstants::API_CUSTOM, ['ids' => array_keys($customList)])->data;
- $collectList = [];
- if ($result) {
- foreach ($result as $index=>$item) {
- $collectList[$item['cuId']][$item['idx']] = [
- 'uv' => $item['uv'],
- 'day_uv' => $item['uvDay'],
- 'recharge_money' => $item['money'],
- 'day_recharge_money' => $item['moneyDay'],
- ];
- }
- }
- $default = [
- 'uv' => 0,
- 'day_uv' => 0,
- 'recharge_money' => 0,
- 'day_recharge_money' => 0,
- ];
- foreach ($customCollect as $id=>$item) {
- if ($find = ArrayHelper::array_find($collectList, $item['custom_id'] . '.' . $item['idx'])) {
- $item = array_merge($item, $find);
- } else {
- $item = array_merge($item, $default);
- }
- $customList[$item['custom_id']]['collect'][] = $item;
- }
- } else {
- foreach ($customCollect as $item) {
- $item['day_uv'] = intval(Redis::instance()->get(CustomConstants::getCustomUvToDayKey($item['custom_id'], $item['idx'])));
- $item['day_recharge_money'] = floatval(Redis::instance()->get(CustomConstants::getCustomRechargeMoneyToDayKey($item['custom_id'], $item['idx'])));
- $item['uv'] = (float)$item['uv'];
- $item['recharge_money'] = (float)$item['recharge_money'];
- $customList[$item['custom_id']]['collect'][] = $item;
- }
- }
- $customList = array_values($customList);
- return $this->setData($customList)->getReturn();
- }
- }
|