Createcustom.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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\Custom as constantCustom;
  8. use app\common\constants\Custom;
  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\constants\Common;
  14. use app\main\constants\AdminConstants;
  15. use app\main\constants\BookConstants;
  16. use app\main\service\MiniProgramService;
  17. use think\db\Query;
  18. class Createcustom extends Backend
  19. {
  20. /**
  21. * @var \app\admin\model\Custom
  22. */
  23. protected $model = null;
  24. /**
  25. * @var \app\common\model\VipAdminBind
  26. */
  27. protected $vipAdminBindModel = 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 \app\admin\model\CustomMediaPush
  38. */
  39. protected $customMediaPushModel = null;
  40. /**
  41. * @var \app\admin\model\CustomUrl
  42. */
  43. protected $customUrlModel = null;
  44. public function _initialize()
  45. {
  46. parent::_initialize();
  47. $this->model = model('Custom');
  48. $this->vipAdminBindModel = model('VipAdminBind');
  49. $this->adminConfigModel = model('AdminConfig');
  50. $this->subscriptionRelationModel = model('SubscriptionRelation');
  51. $this->customMediaPushModel = model('CustomMediaPush');
  52. $this->customUrlModel = model('CustomUrl');
  53. $miniOpen = MiniProgramService::instance()->miniWrite($this->auth->id,$this->group);
  54. $miniPages = MiniProgramService::instance()->getMiniPage();
  55. $this->assign('miniOpen',$miniOpen);
  56. $this->assign('miniPages',$miniPages);
  57. }
  58. /**
  59. * 测试发送客服消息
  60. * @param int|array $admin_id 渠道id
  61. * @param int $user_id 公众号id
  62. * @param string $cdata 素材内容
  63. * @param string $ids 素材id
  64. * @param string $msg_type 素材类型
  65. * @return \think\response\Json
  66. */
  67. public function sent(
  68. $admin_id,
  69. $user_id,
  70. $cdata = '',
  71. $ids = 0,
  72. $msg_type = Message::MESSAGE_TYPE_IMAGE_TEXT,
  73. $official_type = OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE,
  74. $is_group = 0,
  75. $group_id = ''
  76. ) {
  77. if ($this->request->get('table') == 'push') {
  78. $ids = $this->customMediaPushModel->where(['id' => $ids])->column('custom_media_id');
  79. $ids = array_pop($ids);
  80. }
  81. if ($official_type == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {
  82. $params = $this->request->post();
  83. $result = CustomService::instance()->sendMessageSubscribeToUser($admin_id, $params['sub_id'], $user_id,
  84. $cdata, $ids, $msg_type);
  85. } else {
  86. $params = $this->request->post();
  87. if ($params['is_group'] == 1) {
  88. $groupIds = trim($params['group_id'], ',');
  89. $channelRows = model("VipGroupInfo")->where('group_id', 'in', $groupIds)->field("channel_id")->select();
  90. $admin_id = array_unique(array_column($channelRows, 'channel_id'));
  91. }
  92. $result = CustomService::instance()->sendMessageServiceToUser((array)$admin_id, $user_id, $cdata, $ids,
  93. $msg_type);
  94. }
  95. return json($result);
  96. }
  97. public function addimagetext()
  98. {
  99. $vipId = $this->auth->id;
  100. //$officialAccountType = $this->request->get("acc_type");
  101. $officialAccountType = OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE;
  102. /*if ($officialAccountType == null || !in_array($officialAccountType, OfficialAccount::officialAccountTypes)) {
  103. return $this->view->fetch("addimagetextindex");
  104. }*/
  105. if ($this->request->isPost()) {
  106. $params = $this->request->post("row/a");
  107. if(isset($params['sub_from'])){
  108. unset($params['sub_from']);
  109. }
  110. if(isset($params['sub_to'])){
  111. unset($params['sub_to']);
  112. }
  113. $msgContent = !empty($params['message_json']) ? $params['message_json'] : json_encode(array());
  114. $sendTime = $params['sendtime'];
  115. $iAdminId = $this->auth->id;
  116. if ($officialAccountType == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {//公众号
  117. $channelIds = $params['service_id'] ?? [];
  118. if ($params['select_type'] == 1) {
  119. //分组 拉取公众号ID
  120. $groupIds = trim($params['group_id'], ',');
  121. $channelRows = model("VipGroupInfo")->where('group_id', 'in',
  122. $groupIds)->field("channel_id")->select();
  123. $channelIds = array_unique(array_column($channelRows, 'channel_id'));
  124. }
  125. CustomService::instance()->createPushService($sendTime, $channelIds, $iAdminId,
  126. Message::MESSAGE_TYPE_IMAGE_TEXT, $msgContent, 0, $params['is_save_media'], '',
  127. $params['user_json'], Custom::CUSTOM_CREATED_FROM_GROUP_SEND,
  128. ['group_id' => $params['group_id'], 'select_type' => $params['select_type']]);
  129. } elseif ($officialAccountType == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {//订阅号
  130. $subIds = $params['sub_id'];
  131. $channelId = $params['channel_id'];
  132. CustomService::instance()->createPushSub($sendTime, $subIds, $channelId, $iAdminId,
  133. Message::MESSAGE_TYPE_IMAGE_TEXT, $msgContent, 0, $params['is_save_media'],
  134. Custom::CUSTOM_CREATED_FROM_GROUP_SEND);
  135. }
  136. if ($params['is_save_media']) {
  137. $jumpUrl = '/admin/vipcustom/vipimagetextmedia/index?ref=addtabs';
  138. } else {
  139. $jumpUrl = '/admin/vipcustom/vipimagetextmediapush/index?ref=addtabs';
  140. }
  141. $this->success('操作成功', null, ['jump' => $jumpUrl]);
  142. } else {
  143. $this->assign("acc_type", $officialAccountType);
  144. $adminIds = $this->vipAdminBindModel->getChannelIds($vipId);
  145. if ($officialAccountType == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {//公众号
  146. $accList = $this->adminConfigModel->getListByAcc($adminIds);
  147. $this->assign("acclist", $accList);
  148. } elseif ($officialAccountType == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {//订阅号
  149. $adminIds[] = $vipId;
  150. $subList = $this->subscriptionRelationModel->getSubListByAdminId($adminIds);
  151. $this->assign("sublist", $subList);
  152. }
  153. $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit();
  154. $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']);
  155. $this->assign('service_limited', $wechatAccountLimit['serLimitNum']);
  156. return $this->view->fetch();
  157. }
  158. }
  159. public function addlinktext()
  160. {
  161. $vipId = $this->auth->id;
  162. //$officialAccountType = $this->request->get("acc_type");
  163. $officialAccountType = OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE;
  164. /*if ($officialAccountType == null || !in_array($officialAccountType, OfficialAccount::officialAccountTypes)) {
  165. return $this->view->fetch("addlinktextindex");
  166. }*/
  167. if ($this->request->isPost()) {
  168. $params = $this->request->post("row/a");
  169. if(isset($params['sub_from'])){
  170. unset($params['sub_from']);
  171. }
  172. if(isset($params['sub_to'])){
  173. unset($params['sub_to']);
  174. }
  175. $msgContent = !empty($params['message_text']) ? $params['message_text'] : '';
  176. $sendTime = $params['sendtime'];
  177. $iAdminId = $this->auth->id;
  178. if ($officialAccountType == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {//公众号
  179. $channelIds = $params['service_id'] ?? [];
  180. if ($params['select_type'] == 1) {
  181. //分组 拉取公众号ID
  182. $groupIds = trim($params['group_id'], ',');
  183. $channelRows = model("VipGroupInfo")->where('group_id', 'in',
  184. $groupIds)->field("channel_id")->select();
  185. $channelIds = array_unique(array_column($channelRows, 'channel_id'));
  186. }
  187. CustomService::instance()->createPushService($sendTime, $channelIds, $iAdminId,
  188. Message::MESSAGE_TYPE_LINK, $msgContent, 0, $params['is_save_media'], '',
  189. $params['user_json'] ?? '', Custom::CUSTOM_CREATED_FROM_GROUP_SEND,
  190. ['group_id' => $params['group_id'], 'select_type' => $params['select_type']]);
  191. } elseif ($officialAccountType == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {//订阅号
  192. $subIds = $params['sub_id'];
  193. $channelId = $params['channel_id'];
  194. CustomService::instance()->createPushSub($sendTime, $subIds, $channelId, $iAdminId,
  195. Message::MESSAGE_TYPE_LINK, $msgContent, 0, $params['is_save_media'],
  196. Custom::CUSTOM_CREATED_FROM_GROUP_SEND);
  197. }
  198. if ($params['is_save_media']) {
  199. $jumpUrl = '/admin/vipcustom/viplinktextmedia/index?ref=addtabs';
  200. } else {
  201. $jumpUrl = '/admin/vipcustom/viplinktextmediapush/index?ref=addtabs';
  202. }
  203. $this->success('操作成功', null, ['jump' => $jumpUrl]);
  204. } else {
  205. $this->assign("acc_type", $officialAccountType);
  206. $adminIds = $this->vipAdminBindModel->getChannelIds($vipId);
  207. if ($officialAccountType == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) {//公众号
  208. $accList = $this->adminConfigModel->getListByAcc($adminIds);
  209. $this->assign("acclist", $accList);
  210. } elseif ($officialAccountType == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {//订阅号
  211. $adminIds[] = $vipId;
  212. $subList = $this->subscriptionRelationModel->getSubListByAdminId($adminIds);
  213. $this->assign("sublist", $subList);
  214. }
  215. $wechatAccountLimit = CommonService::instance()->getWechatAccountLimit();
  216. $this->assign('sub_limited', $wechatAccountLimit['subLimitNum']);
  217. $this->assign('service_limited', $wechatAccountLimit['serLimitNum']);
  218. return $this->view->fetch();
  219. }
  220. }
  221. /**
  222. * 添加消息
  223. */
  224. public function createmessage()
  225. {
  226. $channelId = $this->auth->id;
  227. if ($this->request->isPost()) {
  228. $params = $this->request->post();
  229. $type = $params['row']['imgtxt_type'];
  230. $result = ['type' => $type];
  231. if ($type == Message::MESSAGE_LINK_TYPE_BOOK) {
  232. $result = array_merge($result, $params['book']);
  233. } elseif ($type == Message::MESSAGE_LINK_TYPE_ACTIVITY) {
  234. $result = array_merge($result, $params['activity']);
  235. } elseif ($type == Message::MESSAGE_LINK_TYPE_RECENT) {
  236. $result = array_merge($result, $params['recent']);
  237. } elseif ($type == Message::MESSAGE_LINK_TYPE_MENU) {
  238. $result = array_merge($result, $params['menu']);
  239. } elseif ($type == Message::MESSAGE_LINK_TYPE_NOURL) {
  240. $result = array_merge($result, $params['no_url']);
  241. } elseif ($type == Message::MESSAGE_LINK_TYPE_CUSTOMIZE_ACTIVITY) {
  242. $result = array_merge($result, $params['customize_activity']);
  243. }elseif ($type == Message::MESSAGE_LINK_TYPE_DAILY_LINK) {
  244. $result = array_merge($result, $params['daily_url']);
  245. }elseif ($type == Message::MESSAGE_LINK_TYPE_MINI){
  246. if ($params['mini'] && $params['mini']['mini_type'] ==4){
  247. $params['mini']['title'] = !empty($params['mini']['title']) ? $params['mini']['title'] : '小程序码';
  248. }
  249. $result = array_merge($result,$params['mini']);
  250. }
  251. $this->success('', null, $result);
  252. } else {
  253. $referralModel = model('Referral');
  254. $wxTypeList = $referralModel->getWxTypeList();
  255. $pushList = $referralModel->getPushList();
  256. //$recentUrl = Message::getRecentFullPath($channelId);
  257. $aActivity = CommonService::instance()->getValidActivities($this->auth->id);
  258. $customActivity = CommonService::instance()->getVipActivityList($this->auth->id);
  259. $menuList = [];
  260. foreach (Menu::$allLinks as $key => $link) {
  261. $menuList[$key] = $link['title'];
  262. }
  263. //常用链接地址列表
  264. $dailyLink = CommonService::instance()->getTmpDailyLink();
  265. $this->view->assign("dailyLink", $dailyLink);
  266. $this->assign('acc_type', Message::MESSAGE_TYPE_IMAGE_TEXT);
  267. $this->view->assign("menuList", $menuList);
  268. $this->view->assign("wxTypeList", $wxTypeList);
  269. $this->view->assign("activityList", $aActivity);
  270. $this->view->assign("customActivityList", $customActivity);
  271. $this->view->assign("pushList", $pushList);
  272. //$this->view->assign("recent_url", $recentUrl);
  273. }
  274. return $this->view->fetch();
  275. }
  276. /**
  277. * 添加次条
  278. * @return string
  279. * @throws \think\Exception
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\ModelNotFoundException
  282. * @throws \think\exception\DbException
  283. */
  284. public function createlinkmessage()
  285. {
  286. $channelId = $this->auth->id;
  287. if ($this->request->isPost()) {
  288. $params = $this->request->post();
  289. $type = $params['row']['imgtxt_type'];
  290. $result = ['type' => $type];
  291. if ($type == Message::MESSAGE_LINK_TYPE_BOOK) {
  292. $result = array_merge($result, $params['book']);
  293. #$result['book_name'] = $params['book_book_id_text'];
  294. } elseif ($type == Message::MESSAGE_LINK_TYPE_ACTIVITY) {
  295. $result = array_merge($result, $params['activity']);
  296. } elseif ($type == Message::MESSAGE_LINK_TYPE_RECENT) {
  297. $result = array_merge($result, $params['recent']);
  298. } elseif ($type == Message::MESSAGE_LINK_TYPE_MENU) {
  299. $result = array_merge($result, $params['menu']);
  300. } elseif ($type == Message::MESSAGE_LINK_TYPE_NOURL) {
  301. $result = array_merge($result, $params['no_url']);
  302. } elseif ($type == Message::MESSAGE_LINK_TYPE_CUSTOMIZE_ACTIVITY) {
  303. $result = array_merge($result, $params['customize_activity']);
  304. } elseif ($type == Message::MESSAGE_LINK_TYPE_SIGN) {
  305. $params['sign']['url'] = "weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid=" . time();
  306. $result = array_merge($result, $params['sign']);
  307. }elseif ($type == Message::MESSAGE_LINK_TYPE_DAILY_LINK) {
  308. $result = array_merge($result, $params['daily_url']);
  309. }elseif ($type == Message::MESSAGE_LINK_TYPE_MINI){
  310. $result = array_merge($result,$params['mini']);
  311. }
  312. $this->success('', null, $result);
  313. } else {
  314. $referralModel = model('Referral');
  315. $wxTypeList = $referralModel->getWxTypeList();
  316. $pushList = $referralModel->getPushList();
  317. $adminIds = $this->vipAdminBindModel->getChannelIds($channelId);
  318. $channelId = array_pop($adminIds);
  319. $recentUrl = getCurrentDomain($channelId, '/index/user/recent');
  320. $aActivity = CommonService::instance()->getValidActivities($this->auth->id);
  321. $customActivity = CommonService::instance()->getVipActivityList($this->auth->id);
  322. $menuList = [];
  323. foreach (Menu::$allLinks as $key => $link) {
  324. $menuList[$key] = $link['title'];
  325. }
  326. //常用链接地址列表
  327. $dailyLink = CommonService::instance()->getTmpDailyLink();
  328. $this->view->assign("dailyLink", $dailyLink);
  329. $this->assign('acc_type', Message::MESSAGE_TYPE_LINK);
  330. $this->view->assign("customActivityList", $customActivity);
  331. $this->view->assign("menuList", $menuList);
  332. $this->view->assign("wxTypeList", $wxTypeList);
  333. $this->view->assign("activityList", $aActivity);
  334. $this->view->assign("pushList", $pushList);
  335. $this->view->assign("recent_url", $recentUrl);
  336. }
  337. return $this->view->fetch();
  338. }
  339. /**
  340. *
  341. * @return \think\response\Json
  342. */
  343. public function autocompletebookname()
  344. {
  345. if ($this->request->isAjax()) {
  346. $keyword = $this->request->post('name') ?? $this->request->param('pkey_value');
  347. $data = [];
  348. $keyword = trim($keyword);
  349. $bookModel = model('Book');
  350. $map = [];
  351. if ($this->group > 2) {
  352. $map['cansee'] = 1;
  353. }
  354. //获取独家书籍id
  355. ExclusiveService::instance()->getExclusiveNotidsWithWhere($this->group, $this->auth->id,$map);
  356. if (!empty($keyword)) {
  357. $bookList = $bookModel
  358. ->where('state', 'neq', BookConstants::BOOK_STATE_OFF_SALE)
  359. ->where($map)
  360. ->where(function (Query $query) use ($keyword) {
  361. $query->where('name', 'like', "%{$keyword}%")
  362. ->whereOr('id', $keyword);
  363. })
  364. ->limit(0, 10)->select();
  365. } else {
  366. $bookList = $bookModel->where('state', 'neq',
  367. BookConstants::BOOK_STATE_OFF_SALE)->where($map)->order('book.idx desc,book.idxx desc')->limit(0, 10)->select();
  368. }
  369. $data = $bookList;
  370. return json(['list' => $data, 'total' => count($data)]);
  371. }
  372. }
  373. /**
  374. * 获取公众号列表AJAX
  375. * @return \think\response\Json
  376. * @throws \think\db\exception\DataNotFoundException
  377. * @throws \think\db\exception\ModelNotFoundException
  378. * @throws \think\exception\DbException
  379. */
  380. public function getServiceListAjax()
  381. {
  382. $acc_name = $this->request->param("acc_name") ?: '';
  383. $adminIds = $this->vipAdminBindModel->getChannelIds($this->auth->id);
  384. $res = $this->adminConfigModel->getListByAcc($adminIds, $acc_name);
  385. return json(['list' => $res, 'total' => count($res)]);
  386. }
  387. }