SubscribeVipDelayService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2020/3/23
  6. * Time: 下午7:22
  7. */
  8. namespace app\main\service;
  9. use app\common\constants\Message;
  10. use app\main\constants\AdminConstants;
  11. use app\main\constants\ErrorCodeConstants;
  12. use app\main\constants\SubscribeDelayConstants;
  13. use app\main\model\object\ReturnObject;
  14. use app\admin\service\CustomService;
  15. class SubscribeVipDelayService extends BaseService
  16. {
  17. /**
  18. * 定义属性
  19. *
  20. * @var SubscribeVipDelayService
  21. */
  22. protected static $self = null;
  23. /**
  24. * 返回实例
  25. *
  26. * @return SubscribeVipDelayService
  27. */
  28. public static function instance()
  29. {
  30. if (self::$self == null) {
  31. self::$self = new self();
  32. }
  33. return self::$self;
  34. }
  35. public function getVipChannelIds($admin_id, $adminGroup = [])
  36. {
  37. if ($adminGroup) {
  38. $channel_ids = model('vip_group_info')->whereIn('group_id', $adminGroup)->column('channel_id');
  39. } else {
  40. $channel_ids = AdminService::instance()->getAdminId($admin_id)->data;
  41. }
  42. $channel_ids = array_unique($channel_ids);
  43. return $this->setData($channel_ids)->getReturn();
  44. }
  45. public function createDelayReply($params, $vipId)
  46. {
  47. $group = $params['admin_group'];
  48. $channelIds = $this->getVipChannelIds($vipId, $group)->data;
  49. if (!$channelIds) {
  50. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('未查询到可以同步的渠道')->getReturn();
  51. }
  52. LogService::info('同步的渠道'.json_encode($channelIds));
  53. $updateChannelDelay = [];
  54. $error = '';
  55. unset($params['id']);
  56. $hasMini = 0;
  57. //查看是否包含小程序类型 6是小程序类型
  58. if (isset($params['url_type']) && $params['url_type'] == 6){
  59. $hasMini = 1;
  60. }
  61. if ($params['content_type'] == 2 && !empty($params['text_content'])){
  62. $urlTypes = array_column($params['text_content'],'url_type');
  63. $hasMini = in_array(6,$urlTypes) ? 1 : 0;
  64. }
  65. foreach ($channelIds as $channelId) {
  66. if ($channelId != -1) {
  67. $isOpen = 1;
  68. if ($hasMini){
  69. $isOpen = MiniProgramService::instance()->miniWrite($channelId,3);
  70. }
  71. if (!$isOpen){
  72. LogService::info('vip同步延迟客服消息失败'.$channelId.'未开通小程序权限');
  73. continue;
  74. }
  75. $return = $this->updateDelayCustom($params, $channelId);
  76. if ($return->code == ErrorCodeConstants::SUCCESS) {
  77. $updateChannelDelay[] = $return->data;
  78. } else {
  79. $error = $return->msg;
  80. }
  81. } else {
  82. $error = '未查询到需要同步的渠道';
  83. }
  84. }
  85. if (!$updateChannelDelay) {
  86. if (!$error) {
  87. $error = '更新失败';
  88. }
  89. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg($error)->getReturn();
  90. }
  91. $result = $this->updateDelayCustom($params, $vipId, true);
  92. if ($result->code == ErrorCodeConstants::SUCCESS) {
  93. model('subscribe_delay_push')->whereIn('id', $updateChannelDelay)->update(['pid'=>$result->data]);
  94. return $this->setData($result->data)->getReturn();
  95. }
  96. return $this->setCode(ErrorCodeConstants::DB_ERROR_UPDATE)->setMsg('更新失败')->getReturn();
  97. }
  98. /**
  99. * @param $params
  100. * @param $channel_id
  101. * @param $is_vip
  102. * @return ReturnObject
  103. */
  104. public function updateDelayCustom($params, $channel_id, $is_vip = false)
  105. {
  106. $params['url_type'] = (int)$params['url_type'] ?? SubscribeDelayConstants::URL_TYPE_REFERRAL;
  107. $params['user_type'] = (int)$params['user_type'] ?? SubscribeDelayConstants::USER_TYPE_ALL;
  108. $content = [
  109. 'title' => $params['title'],
  110. 'image' => $params['image'],
  111. 'description' => $params['description'],
  112. 'user_type' => $params['user_type'],
  113. 'url_type' => $params['url_type'],
  114. 'sex' => $params['sex'],
  115. 'mobile_system' => $params['mobile_system'],
  116. 'mini_type' =>$params['mini_type'] ?? 0,
  117. 'text_tip_word'=>$params['text_tip_word'] ?? '',
  118. ];
  119. if ($is_vip && !empty($params['id'])) {
  120. $row = model('subscribe_delay_push')->where('id', $params['id'])->find();
  121. if (!$row) {
  122. return $this->setMsg('客服消息不存在')->setCode(ErrorCodeConstants::DB_ERROR_SELECT)->getReturn();
  123. }
  124. } else {
  125. $check = [
  126. 'admin_id' => $channel_id,
  127. 'type' => $params['unit'],
  128. 'status' => 'normal',
  129. 'delay' => $params['current_time'],
  130. ];
  131. $row = model('subscribe_delay_push')->where($check)->find();
  132. }
  133. if (!$is_vip) {
  134. if ($params['content_type'] == 2){ //文本类型
  135. $content['description'] = $this->getContent($params, $channel_id);
  136. }else{//图文类型
  137. //添加链接
  138. switch ($params['url_type']) {
  139. case SubscribeDelayConstants::URL_TYPE_REFERRAL:
  140. $referralId = ReferralService::instance()->getChannelReferralFromVip($params['v_id'], $channel_id)->data;
  141. if (!$referralId) {
  142. LogService::info('vip同步延迟客服消息失败('.$channel_id.')分流链接无效');
  143. return $this->setMsg('分流链接无效')->setCode(ErrorCodeConstants::RESULT_EMPTY)->getReturn();
  144. }
  145. $params['referral_url'] = $content['url'] = getCurrentDomain($channel_id, '/t/' . $referralId);
  146. break;
  147. case SubscribeDelayConstants::URL_TYPE_ACTIVITY:
  148. $url = CustomService::instance()->_buildVipActivityUrl($channel_id, $params['activity_id']);
  149. if (!$url) {
  150. LogService::info('vip同步延迟客服消息失败('.$channel_id.')活动不存在');
  151. return $this->setMsg('活动不存在')->setCode(ErrorCodeConstants::RESULT_EMPTY)->getReturn();
  152. }
  153. $params['activity_url'] = $content['url'] = $url;
  154. break;
  155. case SubscribeDelayConstants::URL_TYPE_RECENTLY_READ:
  156. $content['url'] = getCurrentDomain($channel_id, '/index/book/chapter');
  157. break;
  158. case SubscribeDelayConstants::URL_TYPE_SELF:
  159. $content['url'] = $params['self_url'];
  160. break;
  161. case SubscribeDelayConstants::URL_TYPE_SIGN:
  162. $content['url'] = "weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}";
  163. break;
  164. case SubscribeDelayConstants::URL_TYPE_MINI:
  165. $content['mini_content'] = MiniProgramService::instance()->getMiniContent($channel_id,$params);
  166. break;
  167. }
  168. }
  169. }
  170. $id = false;
  171. if ($row) {
  172. if (!$is_vip) {
  173. $content = json_encode($content, JSON_UNESCAPED_UNICODE);
  174. $content = UrlService::instance()->replaceReferralHost($channel_id, $content)->data;
  175. } else {
  176. $content = '';
  177. }
  178. $data = [
  179. 'type' => $params['unit'],
  180. 'delay' => $params['current_time'],
  181. 'content' => $content,
  182. 'news_set' => json_encode($params, JSON_UNESCAPED_UNICODE),
  183. 'status' => 'normal',
  184. 'content_type'=>$params['content_type'],
  185. 'updatetime' => time()
  186. ];
  187. $result = model('subscribe_delay_push')->update($data, ['id'=>$row['id']]);
  188. if ($result !== false) {
  189. $id = $row['id'];
  190. }
  191. } else {
  192. //新增
  193. if (!$is_vip) {
  194. $content = json_encode($content, JSON_UNESCAPED_UNICODE);
  195. $content = UrlService::instance()->replaceReferralHost($channel_id, $content)->data;
  196. } else {
  197. $content = '';
  198. }
  199. $data = [
  200. 'admin_id' => $channel_id,
  201. 'type' => $params['unit'],
  202. 'delay' => $params['current_time'],
  203. 'content' => $content,
  204. 'news_set' => json_encode($params, JSON_UNESCAPED_UNICODE),
  205. 'content_type'=>$params['content_type'],
  206. 'status' => 'normal',
  207. 'updatetime' => time(),
  208. 'createtime' => time(),
  209. ];
  210. $id = model('subscribe_delay_push')->insertGetId($data);
  211. }
  212. if ($id !== false) {
  213. //需要清理一下缓存
  214. model('subscribe_delay_push')->delCache($channel_id);
  215. return $this->setData($id)->getReturn();
  216. } else {
  217. return $this->setMsg(model('subscribe_delay_push')->getError())->setCode(ErrorCodeConstants::DB_ERROR_UPDATE)->getReturn();
  218. }
  219. }
  220. /**
  221. * 延迟客服消息-处理文本类型
  222. * @param $args
  223. * @return string
  224. */
  225. public function getContent($args,$channelId)
  226. {
  227. if (!isset($args['text_content']) || $args['text_content'] == false){
  228. return '';
  229. }
  230. $wxJson = model("AdminConfig")
  231. ->where('admin_id', $channelId)
  232. ->value('json');
  233. $gzh_name = '';
  234. if (!empty($wxJson)) {
  235. $wxInfo = json_decode($wxJson, true);
  236. $gzh_name = $wxInfo['authorizer_info']['nick_name'];
  237. }
  238. $content = $args['text_content'];
  239. if (isset($args['text_tip_word']) && $args['text_tip_word'] != false){
  240. $str = "{$args['text_tip_word']}\r\n";
  241. }else{
  242. $str = " 欢迎关注「{$gzh_name}」,点击下方继续阅读\r\n";
  243. }
  244. foreach($content as $key =>$value){
  245. if ($value['url_type'] == 6){
  246. $str .= MiniProgramService::instance()->getMiniText2($value['url'],$value['name'],$channelId)."\r\n";
  247. continue;
  248. }
  249. if ($value['url_type'] == 3){
  250. $value['url'] = getCurrentDomain($channelId, '/index/book/chapter');
  251. }
  252. if ($value['url_type'] == 5){
  253. $value['url'] = "weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}";
  254. }
  255. if ($key == 1){
  256. $str .= "\r\n更多精彩内容,点击下方蓝字内容\r\n";
  257. }
  258. $str .= "<a href='{$value['url']}'>{$value['name']}</a>\r\n";
  259. }
  260. $str .= "为了方便下次阅读,请置顶公众号\r\n";
  261. if ($args['is_sign'] == 2){
  262. $str .= "<a href='weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}\"'>{$args['sign_content']}</a>";
  263. }else{
  264. $str .= '回复签到,获得书币';
  265. }
  266. return $str;
  267. }
  268. }