123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- namespace app\admin\controller\messagecustom;
- use app\admin\model\CustomMediaPush;
- use app\admin\service\AdminConfigService;
- use app\admin\service\CommonService;
- use app\admin\service\CustomService;
- use app\common\constants\Common;
- use app\common\constants\ErrorCode;
- use app\common\constants\Message;
- use app\common\constants\OfficialAccount;
- use app\common\controller\Backend;
- use app\common\model\SubscriptionRelation;
- class Linktextmedia extends Backend
- {
- /**
- * @var \app\admin\model\CustomMedia
- */
- protected $model = null;
- protected $action_model = null;
- protected $action_status = null;
- /**
- * @var CustomMediaPush
- */
- protected $custom_media_push_model = null;
- protected $custom_service = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->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_LINK,
- '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 $message_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{
- # 服务号
- $subScriptionRelationInstance = new SubscriptionRelation();
- $sublist = $subScriptionRelationInstance->getSubListByAdminId($this->auth->id);
- $this->assign('acclist', $sublist);
- }
- $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit();
- $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']);
- $this->assign('media_id', $ids);
- $this->assign('acc_type', $official_type);
- return $this->fetch();
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * @param string $ids
- * @return mixed|\think\response\Json
- * @throws \think\exception\DbException
- */
- public function viewDetail($ids = '')
- {
- if($ids) {
- if ($this->request->isAjax()) {
- $result = CustomService::instance()->getCustomMediaDetail($ids, Message::MESSAGE_TYPE_LINK);
- return json($result);
- }
- return $this->fetch();
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * @param string $ids 素材ID
- * @param string $idx 数组下标
- */
- public function getMediaMessage($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
- */
- public function editLink($idx = 0, $ids = '', $message_json = '')
- {
- if ($ids && $message_json) {
- $media = $this->model->get(['id' => $ids]);
- $media_arr = json_decode($media->message_text, true);
- # 替换值
- $media_arr[$idx] = json_decode($message_json, true);
- $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 和 message_json'));
- }
- /**
- * 文本素材,删除单条
- */
- public function delSinglePiece($idx = 0, $ids = '')
- {
- 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);
- # 替换值
- //$media_arr[$idx] = json_decode($message_json, true);
- $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) {
- $pk = $this->model->getPk();
- $list = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- foreach ($list as $k => $v) {
- $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);
- }
- }
|