Linktextmedia.php 9.0 KB

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