123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace app\admin\controller\messagecustom;
- 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\BookCategory;
- class Imagetextmediapush extends Backend
- {
- /**
- * @var \app\admin\model\CustomMediaPush
- */
- protected $model = null;
- /**
- * @var \app\common\model\AdminConfig
- */
- protected $adminConfigModel = null;
- /**
- * @var \app\common\model\SubscriptionRelation
- */
- protected $subscriptionRelationModel = null;
- /**
- * @var \app\common\model\Subscription
- */
- protected $subscriptionModel = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('CustomMediaPush');
- $this->adminConfigModel = model('AdminConfig');
- $this->subscriptionModel = model('Subscription');
- $this->subscriptionRelationModel = model('SubscriptionRelation');
- }
- 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_json$[*].book_name':
- $where[$k] = ['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();
- }
- /**
- * 删除 客服消息素材推送记录
- * @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()) {
- $json_data = CustomService::instance()->getCustomMediaPushDetail($ids);
- return json($json_data);
- }
- $obj_arr['message_json'] = json_decode($obj_arr['message_json'], true);
- $obj_arr['user_json'] = json_decode($obj_arr['user_json'], true);
- $this->assign('bookcategorylist', BookCategory::getBookCategoryList());
- $this->assign('obj', $obj_arr);
- $this->assign('officialSuccess', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_HIDDEN));
- $this->assign('officialFail', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_FAIL));
- return $this->fetch();
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * 编辑待发送的图文消息
- */
- public function editWaitingImgMsg($ids = '')
- {
- if ($ids) {
- if ($this->request->isAjax()) {
- $params = $this->request->post();
- $customService = new CustomService();
- $sendtime = strtotime($params['row']['sendtime']);
- $pushId = $params['row']['ids'];
- $result = $customService->editCustomMediaPush($pushId, $sendtime);
- if ($result) {
- $this->success();
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- $oMedia = $this->model->find($ids);
- if ($oMedia['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {
- $subList = $this->subscriptionRelationModel->getSubListByAdminId($this->auth->id);
- $this->assign("sublist", $subList);
- $this->assign("selected", explode(',', $oMedia['official_account_subscribe_ids']));
- }
- $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit();
- $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']);
- $this->assign('acc_type', $oMedia['official_account_type']);
- $this->assign('admin_id',$this->auth->id);
- $this->assign('ids', $ids);
- 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;
- $channelId = $this->auth->id;
- $condition = [
- 'created_admin_id' => $channelId,
- 'message_type' => Message::MESSAGE_TYPE_IMAGE_TEXT,
- 'created_from' => Custom::CUSTOM_CREATED_FROM_GROUP_SEND,
- 'status' => $status,
- ];
- $obj = $this->model
- ->where($where)
- ->where($condition);
- if (!empty($officialCondition)) {
- $obj->where($officialCondition);
- }
- return $obj;
- }
- }
|