123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php
- namespace app\admin\controller\vipcustom;
- use app\admin\service\AdminConfigService;
- use app\admin\service\CommonService;
- use app\admin\service\CustomService;
- use app\common\constants\Custom;
- use app\common\constants\Message;
- use app\common\constants\OfficialAccount;
- use app\common\controller\Backend;
- use app\common\model\Activity;
- use app\common\model\BookCategory;
- class Viplinktextmediapush extends Backend
- {
- /**
- * @var \app\admin\model\CustomMediaPush
- */
- protected $model = null;
- /**
- * @var \app\common\model\AdminConfig
- */
- protected $adminConfigModel = null;
- /**
- * @var \app\common\model\Subscription
- */
- protected $subscriptionModel = null;
- /**
- * @var \app\common\model\SubscriptionRelation
- */
- protected $subscriptionRelationModel = null;
- /**
- * @var \app\admin\model\Custom
- */
- protected $customModel;
- /**
- * @var \app\common\model\VipAdminBind
- */
- protected $vipBindModel = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('CustomMediaPush');
- $this->adminConfigModel = model('AdminConfig');
- $this->subscriptionModel = model('Subscription');
- $this->vipBindModel = model('VipAdminBind');
- $this->subscriptionRelationModel = model('SubscriptionRelation');
- $this->customModel = model('Custom');
- }
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- $status = $this->request->param('status') ?? 0;
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $filter = $this->request->get("filter", '');
- $filter = json_decode($filter, true);
- $where = [];
- $subId = $adminId = false;
- if ($filter) {
- foreach ($filter as $k => $v) {
- switch ($k) {
- case 'sendtime':
- $v = str_replace(' - ', ',', $v);
- $arr = array_slice(explode(',', $v), 0, 2);
- if (stripos($v, ',') === false || !array_filter($arr)) {
- continue;
- }
- $arr[0] = strtotime($arr[0]);
- $arr[1] = strtotime($arr[1]);
- $where[$k] = ['BETWEEN', "{$arr[0]},{$arr[1]}"];
- break;
- case 'official_account_type':
- $where[$k] = $v;
- break;
- case 'wechat_name':
- if (isset($filter['official_account_type'])) {
- if ($filter['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {
- $adminId = $this->adminConfigModel->getAdminIdByOfficialName($v);
- } elseif ($filter['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {
- $subId = $this->subscriptionModel->getIdByOfficialName($v);
- }
- } else {
- $adminId = $this->adminConfigModel->getAdminIdByOfficialName($v);
- $subId = $this->subscriptionModel->getIdByOfficialName($v);
- }
- break;
- case 'id':
- $where[$k] = $v;
- break;
- case 'message_text$[*].book_name':
- $where["message_text->'$[*].book_name'"] = ['like', "%$v%"];
- break;
- default:
- break;
- }
- }
- }
- $officialConditions = [];
- if ($adminId) {
- $officialConditions[] = "find_in_set($adminId,official_account_service_ids)";
- }
- if ($subId) {
- $officialConditions[] = "find_in_set($subId,official_account_subscribe_ids)";
- }
- $officialCondition = implode(' OR ', $officialConditions);
- $countFetchObj = $this->_buildPushObj($where, $officialCondition);
- $total = $countFetchObj
- ->count();
- $listFetchObj = $this->_buildPushObj($where, $officialCondition);
- $list = $listFetchObj
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- $this->assign('status', $status);
- $this->assignconfig('status', $status);
- return $this->view->fetch();
- }
- /**
- * 编辑待发送的图文消息
- */
- public function editWaitingImgMsg($ids = '')
- {
- if ($ids) {
- if ($this->request->isAjax()) {
- $params = $this->request->post("row/a");
- $customService = new CustomService();
- $pushId = $ids;
- $result = $customService->editCustomMediaPush($pushId, $params);
- if ($result) {
- $this->success();
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- $vipId = $this->auth->id;
- $oMedia = $this->model->find($ids);
- $adminIds = $this->vipBindModel->getChannelIds($vipId);
- if ($oMedia['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {//公众号
- $accList = $this->adminConfigModel->getListByAcc($adminIds);
- $this->assign("acclist", $accList);
- if ($oMedia['select_type'] == 0) {
- $oMedia['group_id'] = '';
- $this->assign("selected", explode(',', $oMedia['official_account_service_ids']));
- } else {
- $this->assign("selected", []);
- }
- } elseif ($oMedia['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {//订阅号
- $adminIds[] = $vipId;
- $subList = $this->subscriptionRelationModel->getSubListByAdminId($adminIds);
- $this->assign("sublist", $subList);
- $this->assign("selected", explode(',', $oMedia['official_account_subscribe_ids']));
- $custom = $this->customModel->where(['custom_media_push_id'=>$ids])->find();
- $this->assign("service_name", CustomService::instance()->getServiceNameByChannelId($custom->admin_id));
- }
- $groupList = [];
- if ($oMedia['group_id']) {
- $groupList = model("VipGroup")->where('id', 'in', trim($oMedia['group_id'], ','))->select();
- if (empty($groupList)) {
- $oMedia['group_id'] = '';
- }
- }
- $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit();
- $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']);
- $this->assign('service_limited', $wechatAccountLimit['serLimitNum']);
- $this->assign('acc_type', $oMedia['official_account_type']);
- $this->assign('select_type', $oMedia['select_type']);
- $this->assign('group_id', $oMedia['group_id']);
- $this->assign('group_list', $groupList);
- $this->assign('row', $oMedia);
- $from = $to = '';
- $json = json_decode($oMedia['user_json'], true);
- if ($json['all'] == 0 && !$json['subscribe_time'] && $json['subscribe_range']) {
- list($from, $to) = explode('-', $json['subscribe_range']);
- if($from == 'NaN'){
- $from = '';
- }
- if($to == 'NaN'){
- $to = '';
- }
- if($from){
- $from = date('Y-m-d H:i:s', $from);
- }
- if($to){
- $to = date('Y-m-d H:i:s', $to);
- }
- }
- $this->view->assign('from', $from);
- $this->view->assign('to', $to);
- $this->assign('ids', $ids);
- return $this->fetch();
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * @param string $ids custom_media_push ID
- * @param string $idx 数组下标
- */
- public function getMediaPushMessage($ids = '', $idx = '')
- {
- $media = $this->model->get(['id' => $ids]);
- $message_json_arr = json_decode($media->message_text);
- $data = json_encode($message_json_arr[$idx]);
- if ($media->message_text) {
- return json(['code'=> 0, 'msg'=>'success', 'data'=> $data]);
- } else {
- return json(['code'=> 1, 'msg'=>'fail', 'data'=> '']);
- }
- }
- /**
- * 待发送文字客服消息,删除单条
- * @param string $ids custom_media_push ID
- * @param string $idx 数组下标
- */
- public function delSinglePushPiece($ids = '', $idx = 0)
- {
- if ($ids) {
- $media = $this->model->get(['id' => $ids]);
- $media_arr = json_decode($media->message_text, true);
- unset($media_arr[$idx]);
- $media_arr = array_values($media_arr);
- # 替换值
- $rst = $this->model->save(['message_text' => json_encode($media_arr)], ['id' => $ids]);
- if ($rst) {
- return json(['code' => 0, 'msg' => '删除成功']);
- } else {
- return json(['code' => 1, 'msg' => '删除失敗']);
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * 删除 客服消息素材推送记录
- * @param string $ids
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function del($ids = "")
- {
- if ($ids) {
- $customService = new CustomService();
- $result = $customService->delCustomMediaPush($ids);
- if ($result) {
- $this->success();
- }else {
- $this->error(__('No rows were deleted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * 查看详情
- * @param string $ids
- * @return \think\response\Json
- * @throws \think\Exception
- * @throws \think\exception\DbException
- */
- public function viewDetail($ids = '')
- {
- if ($ids) {
- $obj_arr = $this->model->get(['id' => $ids]) ->toArray();
- if($this->request->isAjax())
- {
- $data = json_decode($obj_arr['message_text'], true);
- foreach($data as $index=>$item){
- $item['position'] = $index + 1;
- switch ($item['type']) {
- case Message::MESSAGE_LINK_TYPE_BOOK:
- $item['name'] = $item['book_name'];
- break;
- case Message::MESSAGE_LINK_TYPE_ACTIVITY:
- $model = Activity::get($item['activity_id']);
- $item['name'] = $model->name;
- break;
- case Message::MESSAGE_LINK_TYPE_RECENT:
- $item['name'] = '继续阅读';
- break;
- case Message::MESSAGE_LINK_TYPE_MENU:
- $item['name'] = $item['channel_name'];;
- break;
- case Message::MESSAGE_LINK_TYPE_NOURL:
- $item['name'] = '无链接';;
- break;
- case Message::MESSAGE_LINK_TYPE_CUSTOMIZE_ACTIVITY:
- $model = Activity::get($item['activity_id']);
- $item['name'] = $model->name;
- break;
- }
- $data[$index] = $item;
- }
- $json_data = array('total' => count($data), 'rows' => $data);
- return json($json_data);
- }
- $this->assign('success', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_HIDDEN));
- $this->assign('fail', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_FAIL));
- $this->assign('obj', $obj_arr);
- return $this->fetch();
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- private function _buildPushObj($where, $officialCondition)
- {
- $status = $this->request->param('status') ?? Custom::CUSTOM_MEDIA_PUSH_STATUS_PENDING;
- $channelIds = $this->vipBindModel->getChannelIds($this->auth->id);
- $channelIds = array_merge($channelIds, [$this->auth->id]);
- $condition = [
- 'message_type' => Message::MESSAGE_TYPE_LINK,
- 'created_from' => Custom::CUSTOM_CREATED_FROM_GROUP_SEND,
- 'status' => $status,
- 'created_admin_id' => ['in', $channelIds],
- ];
- $obj = $this->model
- ->where($where)
- ->where($condition);
- if (!empty($officialCondition)) {
- $obj->where($officialCondition);
- }
- return $obj;
- }
- }
|