Imagetextmedia.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace app\admin\controller\messagecustom;
  3. use app\admin\service\AdminConfigService;
  4. use app\admin\service\CommonService;
  5. use app\admin\service\CustomService;
  6. use app\common\constants\Common;
  7. use app\common\constants\ErrorCode;
  8. use app\common\constants\Message;
  9. use app\common\constants\OfficialAccount;
  10. use app\common\controller\Backend;
  11. use app\common\model\SubscriptionRelation;
  12. use app\common\model\VipAdminBind;
  13. /**
  14. * 客服图文消息素材库
  15. *
  16. * @icon fa fa-circle-o
  17. */
  18. class Imagetextmedia extends Backend{
  19. /**
  20. * @var \app\admin\model\CustomMedia
  21. */
  22. protected $model = null;
  23. protected $action_model = null;
  24. protected $action_status = null;
  25. protected $custom_media_push_model = null;
  26. protected $custom_service = null;
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = model('CustomMedia');
  31. $this->custom_media_push_model = model('CustomMediaPush');
  32. $this->custom_service = new CustomService();
  33. $this->assign('admin_id',$this->auth->id);
  34. }
  35. /**
  36. * 查看
  37. */
  38. public function index(){
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('pkey_name'))
  44. {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $channelId = $this->auth->id;
  49. $condition = [
  50. 'message_type' => Message::MESSAGE_TYPE_IMAGE_TEXT,
  51. 'status' => Common::STATUS_NORMAL,
  52. 'created_admin_id' => $channelId,
  53. ];
  54. $total = $this->model
  55. ->where($where)
  56. ->where($condition)
  57. ->count();
  58. $list = $this->model
  59. ->where($where)
  60. ->where($condition)
  61. ->order($sort, $order)
  62. ->limit($offset, $limit)
  63. ->select();
  64. $result = array("total" => $total, "rows" => $list);
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 图文客服消息素材库 群发消息
  71. * @param string $ids
  72. * @param string $official_type 0 =>群发服务号 1=>群发订阅号
  73. */
  74. public function multiSendMessage($ids = '', $official_type = 0)
  75. {
  76. if ($ids) {
  77. if ($this->request->isPost()) {
  78. $params = $this->request->param();
  79. $sendTime = $params['sendtime'];
  80. $iAdminId = $this->auth->id;
  81. if ($official_type == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {
  82. $channelIds[] = $this->auth->id;
  83. $result = CustomService::instance()->multiSendMessageService($ids, $sendTime, $channelIds, $iAdminId);
  84. } else {
  85. if(empty($params['row'])){
  86. $this->error('请选择需要发送的订阅号');
  87. }
  88. $subIds = $params['row']['sub_id'];
  89. $channelId = $this->auth->id;
  90. $result = CustomService::instance()->multiSendMessageSub($ids, $sendTime, $subIds, $channelId, $iAdminId);
  91. }
  92. if ($result['error'] == ErrorCode::SUCCESS) {
  93. $this->success();
  94. } else {
  95. $this->error($result['msg']);
  96. }
  97. } else {
  98. if($official_type){
  99. # 订阅号
  100. # 查找订阅号列表
  101. $subScriptionRelationInstance = new SubscriptionRelation();
  102. $sublist = $subScriptionRelationInstance->getSubListByAdminId($this->auth->id);
  103. $this->assign('sublist', $sublist);
  104. } else{
  105. # 服务号
  106. $mVipBind = new VipAdminBind();
  107. $sublist = $mVipBind->getSerListByAdminId($this->auth->id);
  108. $this->assign('acclist', $sublist);
  109. }
  110. $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit();
  111. $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']);
  112. $this->assign('acc_type', $official_type);
  113. return $this->fetch();
  114. }
  115. }
  116. $this->error(__('Parameter %s can not be empty', 'ids'));
  117. }
  118. /**
  119. * 查看详情
  120. * @param string $ids
  121. */
  122. public function checkDetails($ids = ''){
  123. if($ids){
  124. $this->success('Ok');
  125. }
  126. $this->error(__('Parameter %s can not be empty', 'ids'));
  127. }
  128. /**
  129. * 查看消息详情
  130. * @param string $id
  131. */
  132. public function viewDetail($ids = '')
  133. {
  134. if($ids) {
  135. if ($this->request->isAjax()) {
  136. $result = CustomService::instance()->getCustomMediaDetail($ids);
  137. return json($result);
  138. }
  139. return $this->fetch();
  140. }
  141. $this->error(__('Parameter %s can not be empty', 'ids'));
  142. }
  143. /**
  144. * 编辑标题
  145. * @param string $ids
  146. */
  147. public function editTitle($ids = '', $title){
  148. if($ids && $title){
  149. $media = $this->model->get(['id'=>$ids]);
  150. $message_json = json_decode($media->message_json);
  151. $message_obj = $message_json[0];
  152. $message_obj->title = $title;
  153. $message_json[0] = $message_obj;
  154. $rst = $this->model->save(['message_json' => json_encode($message_json)], ['id'=>$ids]);
  155. if($rst){
  156. return json(['code'=>0, 'msg'=>'修改成功', 'data'=>$title]);
  157. }else{
  158. return json(['code'=>1, 'msg'=>'修改失敗']);
  159. }
  160. }
  161. $this->error(__('Parameter %s can not be empty', 'ids 和 title'));
  162. }
  163. /**
  164. * 编辑封面
  165. * @param string $ids
  166. */
  167. public function editCover($ids = '', $cover = ''){
  168. if($ids && $cover){
  169. $media = $this->model->get(['id'=>$ids]);
  170. $message_json = json_decode($media->message_json);
  171. $message_obj = $message_json[0];
  172. $message_obj->image = $cover;
  173. $message_json[0] = $message_obj;
  174. $rst = $this->model->save(['message_json' => json_encode($message_json)], ['id'=>$ids]);
  175. if($rst){
  176. return json(['code'=>0, 'msg'=>'修改成功', 'data'=>$cover]);
  177. }else{
  178. return json(['code'=>1, 'msg'=>'修改失敗']);
  179. }
  180. }
  181. $this->error(__('Parameter %s can not be empty', 'ids 和 cover'));
  182. }
  183. /**
  184. * 获取素材 message
  185. * @param string $ids
  186. */
  187. public function getMediaMessage($ids = '')
  188. {
  189. $media = $this->model->get(['id' => $ids]);
  190. $message_json_arr = json_decode($media->message_json);
  191. $msg_data = json_encode($message_json_arr[0]);
  192. $data = [
  193. 'media_id' => $media->id,
  194. 'msg_data' =>$msg_data
  195. ];
  196. if ($media->message_json) {
  197. return json(['code'=> 0, 'msg'=>'success', 'data'=> $data]);
  198. } else {
  199. return json(['code'=> 1, 'msg'=>'fail', 'data'=> '']);
  200. }
  201. }
  202. /**
  203. * 编辑链接
  204. * @param string $ids
  205. */
  206. public function editLink($ids = '', $message_json = '')
  207. {
  208. if ($ids && $message_json) {
  209. $media = $this->model->get(['id'=>$ids]);
  210. $media_arr = json_decode($media ->message_json, true);
  211. # 替换值
  212. $media_arr[0] = json_decode($message_json, true);
  213. $rst = $this->model->save(['message_json' => json_encode($media_arr)], ['id'=>$ids]);
  214. if($rst){
  215. return json(['code'=>0, 'msg'=>'修改成功']);
  216. }else{
  217. return json(['code'=>1, 'msg'=>'修改失敗']);
  218. }
  219. }
  220. $this->error(__('Parameter %s can not be empty', 'ids 和 message_json'));
  221. }
  222. public function del($ids = ''){
  223. if ($ids) {
  224. $pk = $this->model->getPk();
  225. $list = $this->model->where($pk, 'in', $ids)->select();
  226. $count = 0;
  227. foreach ($list as $k => $v) {
  228. #$count += $v->delete();
  229. $count += $v->deleteMedia($v->id);
  230. }
  231. if ($count) {
  232. $this->success();
  233. }else {
  234. $this->error(__('No rows were deleted'));
  235. }
  236. }
  237. $this->error(__('Parameter %s can not be empty', 'ids'));
  238. }
  239. /**
  240. * 测试发送客服消息
  241. * @param int|array $admin_id 渠道id
  242. * @param int $user_id 公众号id
  243. * @param string $cdata 素材内容
  244. * @param string $ids 素材id
  245. * @param string $msg_type 素材类型
  246. * @return \think\response\Json
  247. */
  248. public function sent($admin_id, $user_id, $cdata = '', $ids = 0, $msg_type = Message::MESSAGE_TYPE_IMAGE_TEXT, $official_type = OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE)
  249. {
  250. if($this->request->get('table') == 'push'){
  251. $ids = $this->custom_media_push_model->where(['id' => $ids])->column('custom_media_id');
  252. $ids = array_pop($ids);
  253. }
  254. if ($official_type == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {
  255. $params = $this->request->post();
  256. $result = CustomService::instance()->sendMessageSubscribeToUser($admin_id, $params['sub_id'], $user_id, $cdata, $ids, $msg_type);
  257. } else {
  258. $result = CustomService::instance()->sendMessageServiceToUser((array)$admin_id, $user_id, $cdata, $ids, $msg_type);
  259. }
  260. return json($result);
  261. }
  262. }