Viplinktextmedia.php 14 KB

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