Viplinktextmediapush.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace app\admin\controller\vipcustom;
  3. use app\admin\service\AdminConfigService;
  4. use app\admin\service\CommonService;
  5. use app\admin\service\CustomService;
  6. use app\common\constants\Custom;
  7. use app\common\constants\Message;
  8. use app\common\constants\OfficialAccount;
  9. use app\common\controller\Backend;
  10. use app\common\model\Activity;
  11. use app\common\model\BookCategory;
  12. class Viplinktextmediapush extends Backend
  13. {
  14. /**
  15. * @var \app\admin\model\CustomMediaPush
  16. */
  17. protected $model = null;
  18. /**
  19. * @var \app\common\model\AdminConfig
  20. */
  21. protected $adminConfigModel = null;
  22. /**
  23. * @var \app\common\model\Subscription
  24. */
  25. protected $subscriptionModel = null;
  26. /**
  27. * @var \app\common\model\SubscriptionRelation
  28. */
  29. protected $subscriptionRelationModel = null;
  30. /**
  31. * @var \app\admin\model\Custom
  32. */
  33. protected $customModel;
  34. /**
  35. * @var \app\common\model\VipAdminBind
  36. */
  37. protected $vipBindModel = null;
  38. public function _initialize()
  39. {
  40. parent::_initialize();
  41. $this->model = model('CustomMediaPush');
  42. $this->adminConfigModel = model('AdminConfig');
  43. $this->subscriptionModel = model('Subscription');
  44. $this->vipBindModel = model('VipAdminBind');
  45. $this->subscriptionRelationModel = model('SubscriptionRelation');
  46. $this->customModel = model('Custom');
  47. }
  48. public function index()
  49. {
  50. //设置过滤方法
  51. $this->request->filter(['strip_tags']);
  52. $status = $this->request->param('status') ?? 0;
  53. if ($this->request->isAjax()) {
  54. //如果发送的来源是Selectpage,则转发到Selectpage
  55. if ($this->request->request('pkey_name')) {
  56. return $this->selectpage();
  57. }
  58. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  59. $filter = $this->request->get("filter", '');
  60. $filter = json_decode($filter, true);
  61. $where = [];
  62. $subId = $adminId = false;
  63. if ($filter) {
  64. foreach ($filter as $k => $v) {
  65. switch ($k) {
  66. case 'sendtime':
  67. $v = str_replace(' - ', ',', $v);
  68. $arr = array_slice(explode(',', $v), 0, 2);
  69. if (stripos($v, ',') === false || !array_filter($arr)) {
  70. continue;
  71. }
  72. $arr[0] = strtotime($arr[0]);
  73. $arr[1] = strtotime($arr[1]);
  74. $where[$k] = ['BETWEEN', "{$arr[0]},{$arr[1]}"];
  75. break;
  76. case 'official_account_type':
  77. $where[$k] = $v;
  78. break;
  79. case 'wechat_name':
  80. if (isset($filter['official_account_type'])) {
  81. if ($filter['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {
  82. $adminId = $this->adminConfigModel->getAdminIdByOfficialName($v);
  83. } elseif ($filter['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {
  84. $subId = $this->subscriptionModel->getIdByOfficialName($v);
  85. }
  86. } else {
  87. $adminId = $this->adminConfigModel->getAdminIdByOfficialName($v);
  88. $subId = $this->subscriptionModel->getIdByOfficialName($v);
  89. }
  90. break;
  91. case 'id':
  92. $where[$k] = $v;
  93. break;
  94. case 'message_text$[*].book_name':
  95. $where["message_text->'$[*].book_name'"] = ['like', "%$v%"];
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. }
  102. $officialConditions = [];
  103. if ($adminId) {
  104. $officialConditions[] = "find_in_set($adminId,official_account_service_ids)";
  105. }
  106. if ($subId) {
  107. $officialConditions[] = "find_in_set($subId,official_account_subscribe_ids)";
  108. }
  109. $officialCondition = implode(' OR ', $officialConditions);
  110. $countFetchObj = $this->_buildPushObj($where, $officialCondition);
  111. $total = $countFetchObj
  112. ->count();
  113. $listFetchObj = $this->_buildPushObj($where, $officialCondition);
  114. $list = $listFetchObj
  115. ->order($sort, $order)
  116. ->limit($offset, $limit)
  117. ->select();
  118. $result = array("total" => $total, "rows" => $list);
  119. return json($result);
  120. }
  121. $this->assign('status', $status);
  122. $this->assignconfig('status', $status);
  123. return $this->view->fetch();
  124. }
  125. /**
  126. * 编辑待发送的图文消息
  127. */
  128. public function editWaitingImgMsg($ids = '')
  129. {
  130. if ($ids) {
  131. if ($this->request->isAjax()) {
  132. $params = $this->request->post("row/a");
  133. $customService = new CustomService();
  134. $pushId = $ids;
  135. $result = $customService->editCustomMediaPush($pushId, $params);
  136. if ($result) {
  137. $this->success();
  138. } else {
  139. $this->error(__('No rows were deleted'));
  140. }
  141. }
  142. $vipId = $this->auth->id;
  143. $oMedia = $this->model->find($ids);
  144. $adminIds = $this->vipBindModel->getChannelIds($vipId);
  145. if ($oMedia['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {//公众号
  146. $accList = $this->adminConfigModel->getListByAcc($adminIds);
  147. $this->assign("acclist", $accList);
  148. if ($oMedia['select_type'] == 0) {
  149. $oMedia['group_id'] = '';
  150. $this->assign("selected", explode(',', $oMedia['official_account_service_ids']));
  151. } else {
  152. $this->assign("selected", []);
  153. }
  154. } elseif ($oMedia['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {//订阅号
  155. $adminIds[] = $vipId;
  156. $subList = $this->subscriptionRelationModel->getSubListByAdminId($adminIds);
  157. $this->assign("sublist", $subList);
  158. $this->assign("selected", explode(',', $oMedia['official_account_subscribe_ids']));
  159. $custom = $this->customModel->where(['custom_media_push_id'=>$ids])->find();
  160. $this->assign("service_name", CustomService::instance()->getServiceNameByChannelId($custom->admin_id));
  161. }
  162. $groupList = [];
  163. if ($oMedia['group_id']) {
  164. $groupList = model("VipGroup")->where('id', 'in', trim($oMedia['group_id'], ','))->select();
  165. if (empty($groupList)) {
  166. $oMedia['group_id'] = '';
  167. }
  168. }
  169. $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit();
  170. $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']);
  171. $this->assign('service_limited', $wechatAccountLimit['serLimitNum']);
  172. $this->assign('acc_type', $oMedia['official_account_type']);
  173. $this->assign('select_type', $oMedia['select_type']);
  174. $this->assign('group_id', $oMedia['group_id']);
  175. $this->assign('group_list', $groupList);
  176. $this->assign('row', $oMedia);
  177. $from = $to = '';
  178. $json = json_decode($oMedia['user_json'], true);
  179. if ($json['all'] == 0 && !$json['subscribe_time'] && $json['subscribe_range']) {
  180. list($from, $to) = explode('-', $json['subscribe_range']);
  181. if($from == 'NaN'){
  182. $from = '';
  183. }
  184. if($to == 'NaN'){
  185. $to = '';
  186. }
  187. if($from){
  188. $from = date('Y-m-d H:i:s', $from);
  189. }
  190. if($to){
  191. $to = date('Y-m-d H:i:s', $to);
  192. }
  193. }
  194. $this->view->assign('from', $from);
  195. $this->view->assign('to', $to);
  196. $this->assign('ids', $ids);
  197. return $this->fetch();
  198. }
  199. $this->error(__('Parameter %s can not be empty', 'ids'));
  200. }
  201. /**
  202. * @param string $ids custom_media_push ID
  203. * @param string $idx 数组下标
  204. */
  205. public function getMediaPushMessage($ids = '', $idx = '')
  206. {
  207. $media = $this->model->get(['id' => $ids]);
  208. $message_json_arr = json_decode($media->message_text);
  209. $data = json_encode($message_json_arr[$idx]);
  210. if ($media->message_text) {
  211. return json(['code'=> 0, 'msg'=>'success', 'data'=> $data]);
  212. } else {
  213. return json(['code'=> 1, 'msg'=>'fail', 'data'=> '']);
  214. }
  215. }
  216. /**
  217. * 待发送文字客服消息,删除单条
  218. * @param string $ids custom_media_push ID
  219. * @param string $idx 数组下标
  220. */
  221. public function delSinglePushPiece($ids = '', $idx = 0)
  222. {
  223. if ($ids) {
  224. $media = $this->model->get(['id' => $ids]);
  225. $media_arr = json_decode($media->message_text, true);
  226. unset($media_arr[$idx]);
  227. $media_arr = array_values($media_arr);
  228. # 替换值
  229. $rst = $this->model->save(['message_text' => json_encode($media_arr)], ['id' => $ids]);
  230. if ($rst) {
  231. return json(['code' => 0, 'msg' => '删除成功']);
  232. } else {
  233. return json(['code' => 1, 'msg' => '删除失敗']);
  234. }
  235. }
  236. $this->error(__('Parameter %s can not be empty', 'ids'));
  237. }
  238. /**
  239. * 删除 客服消息素材推送记录
  240. * @param string $ids
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\ModelNotFoundException
  243. * @throws \think\exception\DbException
  244. */
  245. public function del($ids = "")
  246. {
  247. if ($ids) {
  248. $customService = new CustomService();
  249. $result = $customService->delCustomMediaPush($ids);
  250. if ($result) {
  251. $this->success();
  252. }else {
  253. $this->error(__('No rows were deleted'));
  254. }
  255. }
  256. $this->error(__('Parameter %s can not be empty', 'ids'));
  257. }
  258. /**
  259. * 查看详情
  260. * @param string $ids
  261. * @return \think\response\Json
  262. * @throws \think\Exception
  263. * @throws \think\exception\DbException
  264. */
  265. public function viewDetail($ids = '')
  266. {
  267. if ($ids) {
  268. $obj_arr = $this->model->get(['id' => $ids]) ->toArray();
  269. if($this->request->isAjax())
  270. {
  271. $data = json_decode($obj_arr['message_text'], true);
  272. foreach($data as $index=>$item){
  273. $item['position'] = $index + 1;
  274. switch ($item['type']) {
  275. case Message::MESSAGE_LINK_TYPE_BOOK:
  276. $item['name'] = $item['book_name'];
  277. break;
  278. case Message::MESSAGE_LINK_TYPE_ACTIVITY:
  279. $model = Activity::get($item['activity_id']);
  280. $item['name'] = $model->name;
  281. break;
  282. case Message::MESSAGE_LINK_TYPE_RECENT:
  283. $item['name'] = '继续阅读';
  284. break;
  285. case Message::MESSAGE_LINK_TYPE_MENU:
  286. $item['name'] = $item['channel_name'];;
  287. break;
  288. case Message::MESSAGE_LINK_TYPE_NOURL:
  289. $item['name'] = '无链接';;
  290. break;
  291. case Message::MESSAGE_LINK_TYPE_CUSTOMIZE_ACTIVITY:
  292. $model = Activity::get($item['activity_id']);
  293. $item['name'] = $model->name;
  294. break;
  295. }
  296. $data[$index] = $item;
  297. }
  298. $json_data = array('total' => count($data), 'rows' => $data);
  299. return json($json_data);
  300. }
  301. $this->assign('success', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_HIDDEN));
  302. $this->assign('fail', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_FAIL));
  303. $this->assign('obj', $obj_arr);
  304. return $this->fetch();
  305. }
  306. $this->error(__('Parameter %s can not be empty', 'ids'));
  307. }
  308. private function _buildPushObj($where, $officialCondition)
  309. {
  310. $status = $this->request->param('status') ?? Custom::CUSTOM_MEDIA_PUSH_STATUS_PENDING;
  311. $channelIds = $this->vipBindModel->getChannelIds($this->auth->id);
  312. $channelIds = array_merge($channelIds, [$this->auth->id]);
  313. $condition = [
  314. 'message_type' => Message::MESSAGE_TYPE_LINK,
  315. 'created_from' => Custom::CUSTOM_CREATED_FROM_GROUP_SEND,
  316. 'status' => $status,
  317. 'created_admin_id' => ['in', $channelIds],
  318. ];
  319. $obj = $this->model
  320. ->where($where)
  321. ->where($condition);
  322. if (!empty($officialCondition)) {
  323. $obj->where($officialCondition);
  324. }
  325. return $obj;
  326. }
  327. }