Customurl.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\SsdbService;
  4. use app\common\constants\BigData;
  5. use app\common\constants\Menu;
  6. use app\common\controller\Backend;
  7. use app\common\constants\Custom;
  8. use app\common\library\Ssdb;
  9. use Symfony\Component\Cache\Adapter\NullAdapter;
  10. use think\Controller;
  11. use think\Request;
  12. /**
  13. * 客服消息分链接统计表,cps系统使用
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Customurl extends Backend
  18. {
  19. /**
  20. * CustomUrl模型对象
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = model('CustomUrl');
  27. $this->view->assign("messageTypeList", $this->model->getMessageTypeList());
  28. $this->view->assign("typeList", $this->model->getTypeList());
  29. $this->view->assign("officialAccountTypeList", $this->model->getOfficialAccountTypeList());
  30. $this->view->assign("pushTypeList", $this->model->getPushTypeList());
  31. if(!empty($_GET) && !$this->request->isAjax()){
  32. $paramsStr = "";
  33. foreach ($_GET as $k => $v){
  34. if($k == "addtabs")
  35. continue;
  36. $paramsStr .= "&{$k}={$v}";
  37. }
  38. $this->assignconfig("path",$paramsStr);
  39. }
  40. }
  41. /**
  42. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  43. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  44. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  45. */
  46. /**
  47. * 查看
  48. */
  49. public function index($message_type = '0')
  50. {
  51. //设置过滤方法
  52. $this->request->filter(['strip_tags']);
  53. if ($this->request->isAjax())
  54. {
  55. //如果发送的来源是Selectpage,则转发到Selectpage
  56. if ($this->request->request('pkey_name'))
  57. {
  58. return $this->selectpage();
  59. }
  60. $subIds = model("SubscriptionRelation")->where("admin_id",$this->auth->id)->column("subscription_id");//获取订阅号id
  61. $subIds = empty($subIds)?'null':implode(",",$subIds);
  62. $adminIds = $this->auth->id;
  63. if($this->group == 7){//VIP
  64. $adminIds = model("VipAdminBind")->getAdminIdsByVip($this->auth->id);//获取服务号id
  65. $adminIds = empty($adminIds)?'null':implode(",",$adminIds);
  66. }
  67. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  68. $map['message_type'] = $this->request->get("message_type");
  69. if($push_id = $this->request->get("push_id")){
  70. $map['custom_media_push_id'] = $push_id;
  71. unset($map['message_type']);
  72. }
  73. $total = $this->model
  74. ->join("custom_url_collect c","custom_url.custom_id = c.custom_id AND custom_url.idx = c.idx","left")
  75. ->field("*,custom_url.id,custom_url.custom_id,custom_url.idx,c.id id1,c.custom_id custom_id1,c.idx idx1")
  76. ->where($map)
  77. ->where($where)
  78. ->where("(official_account_type='0' AND official_account_id in ({$adminIds})) OR (official_account_type='1' AND official_account_id in ({$subIds}))")
  79. ->where("custom_url.sendstatus = '1'")
  80. // ->order("custom_url.id", "desc")
  81. ->count();
  82. $list = $this->model
  83. ->join("custom_url_collect c","custom_url.custom_id = c.custom_id AND custom_url.idx = c.idx","left")
  84. ->field("*,custom_url.id,custom_url.custom_id,custom_url.idx,c.id id1,c.custom_id custom_id1,c.idx idx1")
  85. ->where($map)
  86. ->where($where)
  87. ->where("(official_account_type='0' AND official_account_id in ({$adminIds})) OR (official_account_type='1' AND official_account_id in ({$subIds}))")
  88. ->where("custom_url.sendstatus = '1'")
  89. ->order("custom_url.sendtime desc,custom_url.official_account_id desc,custom_url.idx")
  90. ->limit($offset, $limit)
  91. ->select();
  92. $pushIds = implode(",",array_unique(array_column($list,"custom_media_push_id")))??'null';
  93. if($message_type == Custom::CUSTOM_URL_MESSAGE_TYPE_IMG){
  94. $mediaJsonArr = model("CustomMediaPush")->where("id","IN",$pushIds)->column("id,message_json");
  95. }else{
  96. $mediaJsonArr = model("CustomMediaPush")->where("id","IN",$pushIds)->column("id,message_text");
  97. }
  98. $customIdArr = [];
  99. $ssdbKeyArr = [];
  100. $activityIds = [];
  101. $ssdbService = new SsdbService();
  102. foreach ($list as &$v){
  103. $customIdArr[] = $v['custom_id'];
  104. $json = isset($mediaJsonArr[$v['custom_media_push_id']])?json_decode($mediaJsonArr[$v['custom_media_push_id']], true):'';
  105. if($v['type'] == 1){//活动
  106. $activityIds[] = $v['activity_id'] = $json&&isset($json[$v['idx'] - 1]['activity_id'])?$json[$v['idx'] - 1]['activity_id']:0;
  107. }elseif ($v['type'] == 3){//菜单
  108. $v['menu_id'] = $json&&isset($json[$v['idx'] - 1]['menu_id'])?$json[$v['idx'] - 1]['menu_id']:0;
  109. }
  110. if($message_type == Custom::CUSTOM_URL_MESSAGE_TYPE_IMG){//SSDBkeys
  111. $v['mark'] = BigData::BIG_DATA_MARK_IMAGE_TEXT;
  112. $v['ssdbkeys'] = $ssdbService->getCustomPushImgKey($v['custom_id'], $v['idx']);
  113. $ssdbKeyArr = array_merge($ssdbKeyArr, $v['ssdbkeys']);
  114. }else{
  115. $v['mark'] = BigData::BIG_DATA_MARK_LINK;
  116. $v['ssdbkeys'] = $ssdbService->getCustomPushTextKey($v['custom_id'], $v['idx']);
  117. $ssdbKeyArr = array_merge($ssdbKeyArr, $v['ssdbkeys']);
  118. }
  119. }
  120. //活动名称
  121. $activityIds = implode(",",$activityIds)??'null';
  122. $activityNameArr = model("Activity")->where("id","in",$activityIds)->column("id,name");
  123. //发送/接收人数
  124. $nums = model("custom")
  125. ->where("id","in",$customIdArr)
  126. ->column("id,send_num,success_num,fail_num","id");
  127. //SSDB今日数据
  128. $ssdb = Ssdb::instance();
  129. $ssdbInfo = $ssdb->multi_get($ssdbKeyArr);
  130. foreach ($list as &$v){
  131. if($v['type'] == 1) {
  132. $v['activity_name'] = $activityNameArr[$v['activity_id']]??'';
  133. }else if ($v['type'] == 3) {
  134. $v['menu_name'] = Menu::$allLinks[$v['menu_id']]['title']??'';
  135. }
  136. foreach ($v['ssdbkeys'] as $vk){
  137. $tkey = substr($vk,-2,2);
  138. if(in_array($tkey,['uv','pv'])){
  139. $tkey = 'day'.$tkey;
  140. }
  141. $v[$tkey] = $ssdbInfo[$vk]??0;
  142. }
  143. $v['send_num'] = $nums[$v['custom_id']]['send_num']??0;
  144. $v['success_num'] = $nums[$v['custom_id']]['success_num']??0;
  145. $v['fail_num'] = $nums[$v['custom_id']]['fail_num']??0;
  146. unset($v['ssdbkeys']);
  147. }
  148. $result = array("total" => $total, "rows" => $list);
  149. return json($result);
  150. }
  151. $this->view->assign("message_type",$message_type);
  152. return $this->view->fetch();
  153. }
  154. }