model = model('CustomMedia'); $this->custom_media_push_model = model('CustomMediaPush'); $this->custom_service = new CustomService(); $this->assign('admin_id',$this->auth->id); } /** * 查看 */ public function index(){ //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $channelId = $this->auth->id; $condition = [ 'message_type' => Message::MESSAGE_TYPE_IMAGE_TEXT, 'status' => Common::STATUS_NORMAL, 'created_admin_id' => $channelId, ]; $total = $this->model ->where($where) ->where($condition) ->count(); $list = $this->model ->where($where) ->where($condition) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } /** * 图文客服消息素材库 群发消息 * @param string $ids * @param string $official_type 0 =>群发服务号 1=>群发订阅号 */ public function multiSendMessage($ids = '', $official_type = 0) { if ($ids) { if ($this->request->isPost()) { $params = $this->request->param(); $sendTime = $params['sendtime']; $iAdminId = $this->auth->id; if ($official_type == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) { $channelIds[] = $this->auth->id; $result = CustomService::instance()->multiSendMessageService($ids, $sendTime, $channelIds, $iAdminId); } else { if(empty($params['row'])){ $this->error('请选择需要发送的订阅号'); } $subIds = $params['row']['sub_id']; $channelId = $this->auth->id; $result = CustomService::instance()->multiSendMessageSub($ids, $sendTime, $subIds, $channelId, $iAdminId); } if ($result['error'] == ErrorCode::SUCCESS) { $this->success(); } else { $this->error($result['msg']); } } else { if($official_type){ # 订阅号 # 查找订阅号列表 $subScriptionRelationInstance = new SubscriptionRelation(); $sublist = $subScriptionRelationInstance->getSubListByAdminId($this->auth->id); $this->assign('sublist', $sublist); } else{ # 服务号 $mVipBind = new VipAdminBind(); $sublist = $mVipBind->getSerListByAdminId($this->auth->id); $this->assign('acclist', $sublist); } $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit(); $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']); $this->assign('acc_type', $official_type); return $this->fetch(); } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 查看详情 * @param string $ids */ public function checkDetails($ids = ''){ if($ids){ $this->success('Ok'); } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 查看消息详情 * @param string $id */ public function viewDetail($ids = '') { if($ids) { if ($this->request->isAjax()) { $result = CustomService::instance()->getCustomMediaDetail($ids); return json($result); } return $this->fetch(); } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 编辑标题 * @param string $ids */ public function editTitle($ids = '', $title){ if($ids && $title){ $media = $this->model->get(['id'=>$ids]); $message_json = json_decode($media->message_json); $message_obj = $message_json[0]; $message_obj->title = $title; $message_json[0] = $message_obj; $rst = $this->model->save(['message_json' => json_encode($message_json)], ['id'=>$ids]); if($rst){ return json(['code'=>0, 'msg'=>'修改成功', 'data'=>$title]); }else{ return json(['code'=>1, 'msg'=>'修改失敗']); } } $this->error(__('Parameter %s can not be empty', 'ids 和 title')); } /** * 编辑封面 * @param string $ids */ public function editCover($ids = '', $cover = ''){ if($ids && $cover){ $media = $this->model->get(['id'=>$ids]); $message_json = json_decode($media->message_json); $message_obj = $message_json[0]; $message_obj->image = $cover; $message_json[0] = $message_obj; $rst = $this->model->save(['message_json' => json_encode($message_json)], ['id'=>$ids]); if($rst){ return json(['code'=>0, 'msg'=>'修改成功', 'data'=>$cover]); }else{ return json(['code'=>1, 'msg'=>'修改失敗']); } } $this->error(__('Parameter %s can not be empty', 'ids 和 cover')); } /** * 获取素材 message * @param string $ids */ public function getMediaMessage($ids = '') { $media = $this->model->get(['id' => $ids]); $message_json_arr = json_decode($media->message_json); $msg_data = json_encode($message_json_arr[0]); $data = [ 'media_id' => $media->id, 'msg_data' =>$msg_data ]; if ($media->message_json) { return json(['code'=> 0, 'msg'=>'success', 'data'=> $data]); } else { return json(['code'=> 1, 'msg'=>'fail', 'data'=> '']); } } /** * 编辑链接 * @param string $ids */ public function editLink($ids = '', $message_json = '') { if ($ids && $message_json) { $media = $this->model->get(['id'=>$ids]); $media_arr = json_decode($media ->message_json, true); # 替换值 $media_arr[0] = json_decode($message_json, true); $rst = $this->model->save(['message_json' => 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 和 message_json')); } public function del($ids = ''){ if ($ids) { $pk = $this->model->getPk(); $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; foreach ($list as $k => $v) { #$count += $v->delete(); $count += $v->deleteMedia($v->id); } if ($count) { $this->success(); }else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 测试发送客服消息 * @param int|array $admin_id 渠道id * @param int $user_id 公众号id * @param string $cdata 素材内容 * @param string $ids 素材id * @param string $msg_type 素材类型 * @return \think\response\Json */ public function sent($admin_id, $user_id, $cdata = '', $ids = 0, $msg_type = Message::MESSAGE_TYPE_IMAGE_TEXT, $official_type = OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) { if($this->request->get('table') == 'push'){ $ids = $this->custom_media_push_model->where(['id' => $ids])->column('custom_media_id'); $ids = array_pop($ids); } if ($official_type == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) { $params = $this->request->post(); $result = CustomService::instance()->sendMessageSubscribeToUser($admin_id, $params['sub_id'], $user_id, $cdata, $ids, $msg_type); } else { $result = CustomService::instance()->sendMessageServiceToUser((array)$admin_id, $user_id, $cdata, $ids, $msg_type); } return json($result); } }