Vipimagetextmedia.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. namespace app\admin\controller\vipcustom;
  3. use app\admin\service\AdminConfigService;
  4. use app\admin\service\CommonService;
  5. use app\admin\service\CustomService;
  6. use app\admin\service\ExclusiveService;
  7. use app\common\constants\Common;
  8. use app\common\constants\ErrorCode;
  9. use app\common\constants\Menu;
  10. use app\common\constants\Message;
  11. use app\common\constants\OfficialAccount;
  12. use app\common\controller\Backend;
  13. use app\common\model\Book;
  14. use app\common\model\SubscriptionRelation;
  15. use app\common\model\VipAdminBind;
  16. use app\main\constants\AdminConstants;
  17. use app\main\service\MiniProgramService;
  18. /**
  19. * 客服图文消息素材库
  20. *
  21. * @icon fa fa-circle-o
  22. */
  23. class Vipimagetextmedia extends Backend{
  24. /**
  25. * @var \app\admin\model\CustomMedia
  26. */
  27. protected $model = null;
  28. /**
  29. * @var \app\common\model\VipAdminBind
  30. */
  31. protected $vipBindModel = null;
  32. /**
  33. * @var \app\common\model\AdminConfig
  34. */
  35. protected $adminConfigModel = null;
  36. /**
  37. * @var \app\common\model\SubscriptionRelation
  38. */
  39. protected $subscriptionRelationModel = null;
  40. /**
  41. * @var \app\admin\model\Custom
  42. */
  43. protected $customModel;
  44. /**
  45. * @var CustomService
  46. */
  47. protected $custom_service = null;
  48. public function _initialize()
  49. {
  50. parent::_initialize();
  51. $this->model = model('CustomMedia');
  52. $this->custom_service = new CustomService();
  53. $this->vipBindModel = model('VipAdminBind');
  54. $this->adminConfigModel = model('AdminConfig');
  55. $this->subscriptionRelationModel = model('SubscriptionRelation');
  56. $this->customModel = model('Custom');
  57. $miniOpen = MiniProgramService::instance()->miniWrite($this->auth->id,$this->group);
  58. $miniPages = MiniProgramService::instance()->getMiniPage();
  59. $this->assign('miniOpen',$miniOpen);
  60. $this->assign('miniPages',$miniPages);
  61. }
  62. /**
  63. * 查看
  64. */
  65. public function index(){
  66. //设置过滤方法
  67. $this->request->filter(['strip_tags']);
  68. if ($this->request->isAjax()) {
  69. //如果发送的来源是Selectpage,则转发到Selectpage
  70. if ($this->request->request('pkey_name'))
  71. {
  72. return $this->selectpage();
  73. }
  74. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  75. $channelIds = $this->vipBindModel->getChannelIds($this->auth->id);
  76. $channelIds[] = $this->auth->id;
  77. $condition = [
  78. 'message_type' => Message::MESSAGE_TYPE_IMAGE_TEXT,
  79. 'status' => Common::STATUS_NORMAL,
  80. ];
  81. $condition['created_admin_id'] = ['in', $channelIds];
  82. $filter = $this->request->get("filter", '');
  83. $filter = json_decode($filter, true);
  84. $where = [];
  85. if ($filter) {
  86. foreach ($filter as $k => $v) {
  87. switch ($k) {
  88. case 'id':
  89. $where[$k] = $v;
  90. break;
  91. case 'message_json[*].book_name':
  92. $where["message_json->'$[*].book_name'"] = ['like', "%$v%"];
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. }
  99. $total = $this->model
  100. ->where($where)
  101. ->where($condition)
  102. ->count();
  103. $list = $this->model
  104. ->where($where)
  105. ->where($condition)
  106. ->order($sort, $order)
  107. ->limit($offset, $limit)
  108. ->select();
  109. $result = array("total" => $total, "rows" => $list);
  110. return json($result);
  111. }
  112. return $this->view->fetch();
  113. }
  114. /**
  115. * 图文客服消息素材库 群发消息
  116. * @param string $ids
  117. * @param string $official_type 0 =>群发服务号 1=>群发订阅号
  118. */
  119. public function multiSendMessage($ids = '', $official_type = 0)
  120. {
  121. if ($ids) {
  122. if ($this->request->isPost()) {
  123. $params = $this->request->param();
  124. $sendTime = $params['row']['sendtime'];
  125. $iAdminId = $this->auth->id;
  126. if ($official_type == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {
  127. if ($params['row']['select_type'] == 1) {
  128. //分组 拉取公众号ID
  129. $groupIds = trim($params['row']['group_id'], ',');
  130. $channelRows = model("VipGroupInfo")->where('group_id', 'in', $groupIds)->field("channel_id")->select();
  131. $channelIds = array_unique(array_column($channelRows, 'channel_id'));
  132. } else {
  133. $channelIds = $params['row']['service_id'];
  134. }
  135. $result = CustomService::instance()->multiSendMessageService($ids, $sendTime, $channelIds, $iAdminId, ['group_id' => $params['row']['group_id'], 'select_type' => $params['row']['select_type']]);
  136. } else {
  137. $subIds = $params['row']['sub_id'];
  138. $channelId = $params['row']['channel_id'];
  139. $result = CustomService::instance()->multiSendMessageSub($ids, $sendTime, $subIds, $channelId, $iAdminId);
  140. }
  141. if ($result['error'] == ErrorCode::SUCCESS) {
  142. $this->success();
  143. } else {
  144. $this->error($result['msg']);
  145. }
  146. } else {
  147. $vipId = $this->auth->id;
  148. $adminIds = $this->vipBindModel->getChannelIds($vipId);
  149. if ($official_type == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {//公众号
  150. $accList = $this->adminConfigModel->getListByAcc($adminIds);
  151. $this->assign("acclist", $accList);
  152. } elseif ($official_type == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {//订阅号
  153. $adminIds[] = $vipId;
  154. $subList = $this->subscriptionRelationModel->getSubListByAdminId($adminIds);
  155. $this->assign("sublist", $subList);
  156. }
  157. $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit();
  158. $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']);
  159. $this->assign('service_limited', $wechatAccountLimit['serLimitNum']);
  160. $this->assign('acc_type', $official_type);
  161. return $this->fetch();
  162. }
  163. }
  164. $this->error(__('Parameter %s can not be empty', 'ids'));
  165. }
  166. /**
  167. * 查看详情
  168. * @param string $ids
  169. */
  170. public function checkDetails($ids = ''){
  171. if($ids){
  172. $this->success('Ok');
  173. }
  174. $this->error(__('Parameter %s can not be empty', 'ids'));
  175. }
  176. /**
  177. * 查看消息详情
  178. * @param string $id
  179. */
  180. public function viewDetail($ids = '')
  181. {
  182. if($ids) {
  183. if ($this->request->isAjax()) {
  184. $result = CustomService::instance()->getCustomMediaDetail($ids);
  185. return json($result);
  186. }
  187. return $this->fetch();
  188. }
  189. $this->error(__('Parameter %s can not be empty', 'ids'));
  190. }
  191. /**
  192. * 编辑标题
  193. * @param string $ids
  194. */
  195. public function editTitle($ids = '', $index = 0, $title){
  196. if($ids && $title){
  197. $media = $this->model->get(['id'=>$ids]);
  198. $message_json = json_decode($media->message_json);
  199. $message_obj = $message_json[$index];
  200. $message_obj->title = $title;
  201. $message_json[$index] = $message_obj;
  202. $rst = $this->model->save(['message_json' => json_encode($message_json)], ['id'=>$ids]);
  203. if($rst){
  204. return json(['code'=>0, 'msg'=>'修改成功', 'data'=>$title]);
  205. }else{
  206. return json(['code'=>1, 'msg'=>'修改失敗']);
  207. }
  208. }
  209. $this->error(__('Parameter %s can not be empty', 'ids 和 title'));
  210. }
  211. /**
  212. * 编辑封面
  213. * @param string $ids
  214. */
  215. public function editCover($ids = '', $index = 0, $cover = ''){
  216. if($ids && $cover){
  217. $media = $this->model->get(['id'=>$ids]);
  218. $message_json = json_decode($media->message_json);
  219. $message_obj = $message_json[$index];
  220. $message_obj->image = $cover;
  221. $message_json[$index] = $message_obj;
  222. $rst = $this->model->save(['message_json' => json_encode($message_json)], ['id'=>$ids]);
  223. if($rst){
  224. return json(['code'=>0, 'msg'=>'修改成功', 'data'=>$cover]);
  225. }else{
  226. return json(['code'=>1, 'msg'=>'修改失敗']);
  227. }
  228. }
  229. $this->error(__('Parameter %s can not be empty', 'ids 和 cover'));
  230. }
  231. // 编辑图文次条
  232. public function editImageText()
  233. {
  234. $channelId = $this->auth->id;
  235. if ($this->request->isPost()) {
  236. $params = $this->request->post();
  237. $type = $params['row']['imgtxt_type'];
  238. $result = ['type' => $type];
  239. if ($type == Message::MESSAGE_LINK_TYPE_BOOK) {
  240. $result = array_merge($result, $params['book']);
  241. #$result['book_name'] = $params['book_book_id_text'];
  242. } elseif ($type == Message::MESSAGE_LINK_TYPE_ACTIVITY) {
  243. $result = array_merge($result, $params['activity']);
  244. } elseif ($type == Message::MESSAGE_LINK_TYPE_RECENT) {
  245. $result = array_merge($result, $params['recent']);
  246. } elseif ($type == Message::MESSAGE_LINK_TYPE_MENU) {
  247. $result = array_merge($result, $params['menu']);
  248. } elseif ($type == Message::MESSAGE_LINK_TYPE_NOURL) {
  249. $result = array_merge($result, $params['no_url']);
  250. } elseif ($type == Message::MESSAGE_LINK_TYPE_CUSTOMIZE_ACTIVITY) {
  251. $result = array_merge($result, $params['customize_activity']);
  252. } elseif ($type == Message::MESSAGE_LINK_TYPE_DAILY_LINK) {
  253. $result = array_merge($result, $params['daily_url']);
  254. }elseif ($type == Message::MESSAGE_LINK_TYPE_MINI){
  255. if ($params['mini'] && $params['mini']['mini_type'] ==4){
  256. $params['mini']['title'] = !empty($params['mini']['title']) ? $params['mini']['title'] : '小程序码';
  257. }
  258. $result = array_merge($result,$params['mini']);
  259. }
  260. $this->success('', null, $result);
  261. } else {
  262. // 获取编辑时,传入的参数
  263. $json_str = $this->request->get('json');
  264. $params = json_decode($json_str, true, 512, JSON_BIGINT_AS_STRING );
  265. $params['page'] = $params['page'] ?? '';
  266. $params['mini_type'] = $params['mini_type'] ?? 3;
  267. $referralModel = model('Referral');
  268. $wxTypeList = $referralModel->getWxTypeList();
  269. $pushList = $referralModel->getPushList();
  270. //$recentUrl = Message::getRecentFullPath($channelId);
  271. $aActivity = CommonService::instance()->getValidActivities($this->auth->id);
  272. $customActivity = CommonService::instance()->getVipActivityList($this->auth->id);
  273. $menuList = [];
  274. foreach (Menu::$allLinks as $key => $link) {
  275. $menuList[$key] = $link['title'];
  276. }
  277. if(array_key_exists('book_id', $params)){
  278. $mBook = new Book();
  279. $oBook = $mBook->where(['id'=>$params['book_id']])->find();
  280. $this->assign('sex_text', $oBook->sex_text);
  281. }
  282. $bnotin= [];
  283. ExclusiveService::instance()->getExclusiveNotidsWithoutWhere($this->group, $this->auth->id,$bnotin);
  284. $this->assignconfig('bnotin', $bnotin);
  285. //常用链接地址列表
  286. $dailyLink = CommonService::instance()->getTmpDailyLink();
  287. $this->assign('acc_type', Message::MESSAGE_TYPE_IMAGE_TEXT);
  288. $this->view->assign("params", $params);
  289. $this->view->assign("type", $params['type']);
  290. $this->view->assign("menuList", $menuList);
  291. $this->view->assign("wxTypeList", $wxTypeList);
  292. $this->view->assign("activityList", $aActivity);
  293. $this->view->assign("customActivityList", $customActivity);
  294. $this->view->assign("pushList", $pushList);
  295. $this->view->assign("dailyLink", $dailyLink);
  296. //$this->view->assign("recent_url", $recentUrl);
  297. }
  298. return $this->view->fetch();
  299. }
  300. /**
  301. * 获取素材 message
  302. * @param string $ids
  303. */
  304. public function getMediaMessage($ids = '', $index = 0)
  305. {
  306. $media = $this->model->get(['id' => $ids]);
  307. $message_json_arr = json_decode($media->message_json, true);
  308. //兼容description
  309. if (!isset($message_json_arr[$index]['description'])) {
  310. $message_json_arr[$index]['description'] = '';
  311. }
  312. $data = json_encode($message_json_arr[$index]);
  313. if ($media->message_json) {
  314. return json(['code'=> 0, 'msg'=>'success', 'data'=> $data]);
  315. } else {
  316. return json(['code'=> 1, 'msg'=>'fail', 'data'=> '']);
  317. }
  318. }
  319. /**
  320. * 编辑链接
  321. * @param string $ids
  322. */
  323. public function editLink($index = 0, $ids = '', $message_json = '')
  324. {
  325. if ($ids && $message_json) {
  326. $media = $this->model->get(['id'=>$ids]);
  327. $media_arr = json_decode($media ->message_json, true);
  328. # 替换值
  329. $media_arr[$index] = json_decode($message_json, true);
  330. $rst = $this->model->save(['message_json' => json_encode($media_arr)], ['id'=>$ids]);
  331. if($rst){
  332. return json(['code'=>0, 'msg'=>'修改成功']);
  333. }else{
  334. return json(['code'=>1, 'msg'=>'修改失敗']);
  335. }
  336. }
  337. $this->error(__('Parameter %s can not be empty', 'ids 和 message_json'));
  338. }
  339. public function del($ids = ''){
  340. if ($ids) {
  341. $pk = $this->model->getPk();
  342. $list = $this->model->where($pk, 'in', $ids)->select();
  343. $count = 0;
  344. foreach ($list as $k => $v) {
  345. #$count += $v->delete();
  346. $count += $v->deleteMedia($v->id);
  347. }
  348. if ($count) {
  349. $this->success();
  350. }else {
  351. $this->error(__('No rows were deleted'));
  352. }
  353. }
  354. $this->error(__('Parameter %s can not be empty', 'ids'));
  355. }
  356. }