Custom.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\CommonService;
  4. use app\admin\service\ExclusiveService;
  5. use app\common\model\Book;
  6. use app\admin\service\CustomService;
  7. use app\common\constants\Common;
  8. use app\common\constants\Menu;
  9. use app\common\constants\Message;
  10. use app\common\constants\OfficialAccount;
  11. use app\common\controller\Backend;
  12. use app\common\model\AdminConfig;
  13. use app\common\constants\Custom as CustomConstants;
  14. use app\common\library\WeChatObject;
  15. use app\main\constants\AdminConstants;
  16. use app\main\constants\BookConstants;
  17. use app\main\service\AdminService;
  18. use app\main\service\MiniProgramService;
  19. use app\main\service\UrlService;
  20. use think\Controller;
  21. use think\db\Query;
  22. use think\Log;
  23. use think\Request;
  24. use app\common\controller\Api;
  25. use think\Config;
  26. use think\Model;
  27. use EasyWeChat\Factory;
  28. use app\common\library\Redis;
  29. use EasyWeChat\Kernel\Messages\News;
  30. use EasyWeChat\Kernel\Messages\NewsItem;
  31. use Symfony\Component\Cache\Simple\RedisCache;
  32. /**
  33. * 客服消息管理
  34. *
  35. * @icon fa fa-circle-o
  36. */
  37. class Custom extends Backend
  38. {
  39. /**
  40. * @var \app\admin\model\Custom
  41. */
  42. protected $model = null;
  43. /**
  44. * @var AdminConfig
  45. */
  46. protected $adminConfigModel = null;
  47. /**
  48. * @var \app\admin\model\CustomMedia
  49. */
  50. protected $customMediaModel = null;
  51. /**
  52. * @var \app\admin\model\CustomMediaPush
  53. */
  54. protected $customMediaPushModel = null;
  55. /**
  56. * @var \app\admin\model\CustomUrl
  57. */
  58. protected $customUrlModel = null;
  59. /**
  60. * @var \app\common\model\Referral
  61. */
  62. protected $referralModel = null;
  63. /**
  64. * @var \app\common\model\Book
  65. */
  66. protected $bookModel = null;
  67. /**
  68. * 是否开启数据限制
  69. * 支持auth/personal
  70. * 表示按权限判断/仅限个人
  71. * 默认为禁用,若启用请务必保证表中存在admin_id字段
  72. */
  73. protected $dataLimit = 'personal';
  74. public function _initialize()
  75. {
  76. parent::_initialize();
  77. $this->model = model('Custom');
  78. $this->customMediaModel = model('CustomMedia');
  79. $this->adminConfigModel = model('AdminConfig');
  80. $this->customUrlModel = model('CustomUrl');
  81. $this->customMediaPushModel = model('CustomMediaPush');
  82. $this->referralModel = model('Referral');
  83. $this->bookModel = model('Book');
  84. $this->view->assign("statueList", $this->model->getStatueList());
  85. $this->assign('admin_id', $this->auth->id);
  86. $this->assignconfig('admin_id', $this->auth->id);
  87. $this->assignconfig('nowtime', time());
  88. $miniOpen = MiniProgramService::instance()->miniWrite($this->auth->id,$this->group);
  89. $miniPages = MiniProgramService::instance()->getMiniPage();
  90. $this->assign('miniOpen',$miniOpen);
  91. $this->assign('miniPages',$miniPages);
  92. }
  93. public function index()
  94. {
  95. //设置过滤方法
  96. $this->request->filter(['strip_tags']);
  97. if ($this->request->isAjax()) {
  98. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  99. $total = $this->model
  100. ->where($where)
  101. ->order($sort, $order)
  102. ->where('created_from', CustomConstants::CUSTOM_CREATED_FROM_ADD)
  103. ->count();
  104. $list = $this->model
  105. ->where($where)
  106. ->order($sort, $order)
  107. ->limit($offset, $limit)
  108. ->where('created_from', CustomConstants::CUSTOM_CREATED_FROM_ADD)
  109. ->select();
  110. $result = array("total" => $total, "rows" => $list);
  111. return json($result);
  112. }
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 测试发送客服消息
  117. * @param int|array $admin_id 渠道id
  118. * @param int $user_id 公众号id
  119. * @param string $cdata 素材内容
  120. * @param string $ids 素材id
  121. * @param string $msg_type 素材类型
  122. * @return \think\response\Json
  123. */
  124. public function sent(
  125. $admin_id,
  126. $user_id,
  127. $cdata = '',
  128. $ids = 0,
  129. $msg_type = Message::MESSAGE_TYPE_IMAGE_TEXT,
  130. $official_type = OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE
  131. ) {
  132. if ($this->request->get('table') == 'push') {
  133. $ids = $this->customMediaPushModel->where(['id' => $ids])->column('custom_media_id');
  134. $ids = array_pop($ids);
  135. }
  136. if ($official_type == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) {
  137. $params = $this->request->post();
  138. $result = CustomService::instance()->sendMessageSubscribeToUser($admin_id, $params['sub_id'], $user_id,
  139. $cdata, $ids, $msg_type);
  140. } else {
  141. $result = CustomService::instance()->sendMessageServiceToUser((array)$admin_id, $user_id, $cdata, $ids,
  142. $msg_type);
  143. }
  144. return json($result);
  145. }
  146. /**
  147. * 客服消息根据条件筛选会员人数
  148. */
  149. public function ajaxcustomajaxcustom()
  150. {
  151. return 0; // user分库分表之后无法再统计人数
  152. // $admin_id = $this->auth->id;
  153. // $sex = $this->request->post('sex');
  154. // $tag = $this->request->post('tag');
  155. // $consume = $this->request->post('consume');
  156. // $kandian = $this->request->post('kandian');
  157. // $subscribe_time = $this->request->post('subscribe_time');
  158. // $condition = " where channel_id = ".$admin_id;
  159. // $condition.= " and state = '1'";
  160. // $condition.= " and is_subscribe = '1' ";
  161. // if($sex!=-1){
  162. // $condition.= " and sex = '{$sex}' ";
  163. // }
  164. // if($tag > 0){
  165. // $condition.= " and FIND_IN_SET(".$tag.",book_category_ids)";
  166. // }
  167. // if($consume==1){ //未付款
  168. // $condition.= " and is_pay='0'";
  169. // }elseif($consume == 2){ //普通已付款
  170. // $condition.= " and is_pay='1'";
  171. // }elseif($consume == 3){ //vip会员
  172. // $condition.= " and vip_endtime > ".time();
  173. // }
  174. //
  175. // if($kandian ==1){ //书币低于500
  176. // $condition.= " and kandian < 500";
  177. // }elseif($kandian == 2){ //书币低于2000
  178. // $condition.= " and kandian < 2000";
  179. // }elseif($kandian == 3){ //书币大于2000
  180. // $condition.= " and kandian >= 2000";
  181. // }
  182. //
  183. // $oneWeek = time()-86400*7; //一周内
  184. // $twoWeek = time()-86400*14; //两周内
  185. // $oneMonth = time()-86400*30; //一月内
  186. // $twoMonth = time()-86400*60; //两月内
  187. //
  188. // if($subscribe_time == 1){ //一周内
  189. // $condition.= " and subscribe_time >= {$oneWeek}";
  190. // }elseif($subscribe_time == 2){ //两周内
  191. // $condition.= " and subscribe_time >= {$twoWeek}";
  192. // }elseif($subscribe_time == 3){ //一月内
  193. // $condition.= " and subscribe_time >= {$oneMonth}";
  194. // }elseif($subscribe_time == 4){ //两月内
  195. // $condition.= " and subscribe_time >= {$twoMonth}";
  196. // }elseif($subscribe_time == 5){ //更早
  197. // $condition.= " and subscribe_time < {$twoMonth}";
  198. // }
  199. //
  200. // $sql = "select count(id) as ids from user ".$condition;
  201. //
  202. // $result = model('User')->query($sql);
  203. // return json($result[0]);
  204. }
  205. /**
  206. * 异步获取category列表 //客服消息tag标签接口
  207. * @return \think\response\Json
  208. */
  209. public function ajaxcategory()
  210. {
  211. $list = model('BookCategory')
  212. ->where('status', '=', 'normal')
  213. ->order('weigh', 'desc')
  214. ->select();
  215. $categorylist = [];
  216. foreach ($list as $key => $value) {
  217. if ($value['sex'] == '1') {
  218. $sex = '男频';
  219. } else {
  220. $sex = '女频';
  221. }
  222. $categorylist[] = ['id' => $value['id'], 'name' => $sex . '-' . $value['name']];
  223. }
  224. $res = [];
  225. $res['data'] = $categorylist;
  226. //$ids = model('User')->where(['sex'=>1,'channel_id'=>$this->auth->id])->count();
  227. $res['ids'] = '--';
  228. return json($res);
  229. }
  230. /**
  231. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  232. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  233. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  234. */
  235. /**
  236. * 添加
  237. */
  238. public function add()
  239. {
  240. header("Content-Type: text/html;charset=utf-8");
  241. if ($this->request->isPost()) {
  242. $params = $this->request->post("row/a");
  243. $params['is_save_media'] = 0;
  244. #客服消息为文字链模式时,没有message_json字段
  245. $msgType = $params['message_type'];
  246. if ($msgType == Message::MESSAGE_TYPE_IMAGE_TEXT) {
  247. $msgContent = !empty($params['message_json']) ? $params['message_json'] : json_encode(array());
  248. } else {
  249. $msgContent = !empty($params['message_text']) ? $params['message_text'] : json_encode(array());
  250. }
  251. if ($params) {
  252. $sendtime = $params['sendtime'];
  253. if (strtotime($sendtime) < time()) {
  254. $this->error('发送时间不能小于当前时间');
  255. }
  256. if ($this->dataLimit) {
  257. $params[$this->dataLimitField] = $this->auth->id;
  258. }
  259. //是否采用模型验证
  260. if ($this->modelValidate) {
  261. $name = basename(str_replace('\\', '/', get_class($this->model)));
  262. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  263. $this->model->validate($validate);
  264. }
  265. $sendTime = $params['sendtime'];
  266. $iAdminId = $this->auth->id;
  267. $channelIds = [$this->auth->id];
  268. CustomService::instance()->createPushService($sendTime, $channelIds, $iAdminId, $msgType, $msgContent,
  269. 0, $params['is_save_media'] == Common::YES, $params['title'], $params['user_json']);
  270. $jumpUrl = '';
  271. if ($params['is_save_media'] == Common::YES) {
  272. switch ($msgType) {
  273. case Message::MESSAGE_TYPE_IMAGE_TEXT:
  274. $jumpUrl = '/admin/messagecustom/imagetextmedia/index?ref=addtabs';
  275. break;
  276. case Message::MESSAGE_TYPE_LINK:
  277. $jumpUrl = '/admin/messagecustom/linktextmedia/index?ref=addtabs';
  278. break;
  279. default:
  280. break;
  281. }
  282. } else {
  283. $jumpUrl = '/admin/custom?ref=addtabs';
  284. }
  285. $this->success('操作成功', null, ['jump' => $jumpUrl]);
  286. }
  287. $this->error(__('Parameter %s can not be empty', ''));
  288. }
  289. $input = $this->request->param();
  290. $addtype = !empty($input['addtype']) ? $input['addtype'] : 'typeimg';
  291. $this->assign('addtype', $addtype);
  292. return $this->view->fetch();
  293. }
  294. /**
  295. *
  296. * @return \think\response\Json
  297. */
  298. public function autocompletebookname()
  299. {
  300. if ($this->request->isAjax()) {
  301. $keyword = $this->request->post('name') ?? $this->request->param('pkey_value');
  302. $data = [];
  303. $keyword = trim($keyword);
  304. $bookModel = model('Book');
  305. $map = [];
  306. if ($this->group > 2) {
  307. $map['cansee'] = 1;
  308. }
  309. //获取独家书籍id
  310. ExclusiveService::instance()->getExclusiveNotidsWithWhere($this->group, $this->auth->id,$map);
  311. if (!empty($keyword)) {
  312. $bookList = $bookModel
  313. ->where('state', 'neq', BookConstants::BOOK_STATE_OFF_SALE)
  314. ->where($map)
  315. ->where(function (Query $query) use ($keyword) {
  316. $query->where('name', 'like', "%{$keyword}%")
  317. ->whereOr('id', $keyword);
  318. })
  319. ->limit(0, 10)->select();
  320. } else {
  321. $bookList = $bookModel->where('state', 'neq',
  322. BookConstants::BOOK_STATE_OFF_SALE)->where($map)->order('book.idx desc,book.idxx desc')->limit(0, 10)->select();
  323. }
  324. $data = $bookList;
  325. return json(['list' => $data, 'total' => count($data)]);
  326. }
  327. }
  328. public function addimagetext()
  329. {
  330. $channelId = $this->auth->id;
  331. if ($this->request->isPost()) {
  332. $params = $this->request->post();
  333. $type = $params['row']['imgtxt_type'];
  334. $result = ['type' => $type];
  335. if ($type == Message::MESSAGE_LINK_TYPE_BOOK) {
  336. $result = array_merge($result, $params['book']);
  337. //$result['book_name'] = $params['book_name'];
  338. } elseif ($type == Message::MESSAGE_LINK_TYPE_ACTIVITY) {
  339. $result = array_merge($result, $params['activity']);
  340. } elseif ($type == Message::MESSAGE_LINK_TYPE_RECENT) {
  341. $result = array_merge($result, $params['recent']);
  342. } elseif ($type == Message::MESSAGE_LINK_TYPE_MENU) {
  343. $result = array_merge($result, $params['menu']);
  344. } elseif ($type == Message::MESSAGE_LINK_TYPE_NOURL) {
  345. $result = array_merge($result, $params['no_url']);
  346. } elseif ($type == Message::MESSAGE_LINK_TYPE_URL) {
  347. if (strlen($params['url']['url']) > 255) {
  348. $this->error("自定义链接最大长度不能超过255个字符");
  349. }
  350. $result = array_merge($result, $params['url']);
  351. }elseif ($type == Message::MESSAGE_LINK_TYPE_DAILY_LINK) {
  352. $result = array_merge($result, $params['daily_url']);
  353. }
  354. if ($type == Message::MESSAGE_LINK_TYPE_MINI){
  355. $params['mini']['title'] = $params['mini']['title'] ?? '小程序码';
  356. $params['mini']['image'] = $params['mini']['image'] ?? '';
  357. $result = array_merge($result,$params['mini']);
  358. $result['url'] = $params['mini']['image'];
  359. if ($result['mini_type'] == 3 && empty($result['url'])){
  360. $this->error('图片必须上传');
  361. }
  362. }
  363. /*$customService = new CustomService();
  364. $url = $customService->buildUrl($channelId, $result);
  365. $result['url'] = $url;*/
  366. $this->success('', null, $result);
  367. } else {
  368. $referralModel = model('Referral');
  369. $wxTypeList = $referralModel->getWxTypeList();
  370. $pushList = $referralModel->getPushList();
  371. $recentUrl = Message::getRecentFullPath($channelId);
  372. $aActivity = CommonService::instance()->getValidActivities($this->auth->id);
  373. $menuList = [];
  374. foreach (Menu::$allLinks as $key => $link) {
  375. $menuList[$key] = $link['title'];
  376. }
  377. //常用链接地址列表
  378. $dailyLink = $this->getCurrentUserDailyLink();
  379. $this->view->assign("dailyLink", $dailyLink);
  380. $this->assign('acc_type', Message::MESSAGE_TYPE_IMAGE_TEXT);
  381. $this->view->assign("menuList", $menuList);
  382. $this->view->assign("wxTypeList", $wxTypeList);
  383. $this->view->assign("activityList", $aActivity);
  384. $this->view->assign("pushList", $pushList);
  385. $this->view->assign("recent_url", $recentUrl);
  386. }
  387. return $this->view->fetch();
  388. }
  389. public function addlinktext()
  390. {
  391. $channelId = $this->auth->id;
  392. if ($this->request->isPost()) {
  393. $params = $this->request->post();
  394. $type = $params['row']['imgtxt_type'];
  395. $result = ['type' => $type];
  396. if ($type == Message::MESSAGE_LINK_TYPE_BOOK) {
  397. $result = array_merge($result, $params['book']);
  398. #$result['book_name'] = $params['book_book_id_text'];
  399. } elseif ($type == Message::MESSAGE_LINK_TYPE_ACTIVITY) {
  400. $result = array_merge($result, $params['activity']);
  401. } elseif ($type == Message::MESSAGE_LINK_TYPE_RECENT) {
  402. $result = array_merge($result, $params['recent']);
  403. } elseif ($type == Message::MESSAGE_LINK_TYPE_MENU) {
  404. $result = array_merge($result, $params['menu']);
  405. } elseif ($type == Message::MESSAGE_LINK_TYPE_NOURL) {
  406. $result = array_merge($result, $params['no_url']);
  407. } elseif ($type == Message::MESSAGE_LINK_TYPE_URL) {
  408. if (strlen($params['url']['url']) > 255) {
  409. $this->error("自定义链接最大长度不能超过255个字符");
  410. }
  411. $result = array_merge($result, $params['url']);
  412. } elseif ($type == Message::MESSAGE_LINK_TYPE_SIGN) {
  413. $params['sign']['url'] = "weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid=" . time();
  414. $result = array_merge($result, $params['sign']);
  415. }elseif ($type == Message::MESSAGE_LINK_TYPE_DAILY_LINK) {
  416. $result = array_merge($result, $params['daily_url']);
  417. }elseif ($type == Message::MESSAGE_LINK_TYPE_MINI){
  418. $result = array_merge($result,$params['mini']);
  419. }
  420. $this->success('', null, $result);
  421. } else {
  422. $referralModel = model('Referral');
  423. $wxTypeList = $referralModel->getWxTypeList();
  424. $pushList = $referralModel->getPushList();
  425. $recentUrl = getCurrentDomain($channelId, '/index/user/recent');
  426. $aActivity = CommonService::instance()->getValidActivities($this->auth->id);
  427. $menuList = [];
  428. foreach (Menu::$allLinks as $key => $link) {
  429. $menuList[$key] = $link['title'];
  430. }
  431. //常用链接地址列表
  432. $dailyLink = $this->getCurrentUserDailyLink();
  433. $this->view->assign("dailyLink", $dailyLink);
  434. $this->assign('acc_type', Message::MESSAGE_TYPE_LINK);
  435. $this->view->assign("menuList", $menuList);
  436. $this->view->assign("wxTypeList", $wxTypeList);
  437. $this->view->assign("activityList", $aActivity);
  438. $this->view->assign("pushList", $pushList);
  439. $this->view->assign("recent_url", $recentUrl);
  440. }
  441. return $this->view->fetch();
  442. }
  443. /**
  444. * 编辑
  445. */
  446. public function edit($ids = null)
  447. {
  448. $row = $this->model->get($ids);
  449. if (!$row) {
  450. $this->error(__('No Results were found'));
  451. }
  452. $officialAccountId = $row->official_account_id;
  453. $official_account_type = $row->official_account_type;
  454. $adminIds = $this->getDataLimitAdminIds();
  455. if (is_array($adminIds)) {
  456. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  457. $this->error(__('You have no permission'));
  458. }
  459. }
  460. $canEdit = 0;
  461. if ($row['sendtime'] <= time()) { //已发送,不能修改
  462. $canEdit = 1;
  463. }
  464. if ($row['sendtime'] > time() && $row['sendtime'] - time() <= 600) { //发送前10分钟内不能修改
  465. $canEdit = 1;
  466. }
  467. $this->assign("canedit", $canEdit);
  468. if ($this->request->isPost()) {
  469. $params = $this->request->post("row/a");
  470. if ($params) {
  471. if (strtotime($params['sendtime']) < time()) {
  472. $this->error('发送时间不能小于当前时间');
  473. }
  474. try {
  475. //是否采用模型验证
  476. if ($this->modelValidate) {
  477. $name = basename(str_replace('\\', '/', get_class($this->model)));
  478. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  479. $row->validate($validate);
  480. }
  481. #region 更改客服消息
  482. #1. 通过$ids 获取 custom_media_push_id
  483. $customData = $this->model->get(['id' => $ids]);
  484. $customMediaPushId = $customData->custom_media_push_id;
  485. #2. 更新 custom_media_push 表 内容
  486. if (!$params['message_type']) {
  487. // 图文
  488. $customMediaPushData = [
  489. 'message_json' => $params['message_json'],
  490. 'user_json' => $params['user_json'],
  491. 'sendtime' => strtotime($params['sendtime']),
  492. 'updatetime' => time()
  493. ];
  494. } else {
  495. $customMediaPushData = [
  496. 'message_text' => $params['message_text'],
  497. 'user_json' => $params['user_json'],
  498. 'sendtime' => strtotime($params['sendtime']),
  499. 'updatetime' => time()
  500. ];
  501. }
  502. $this->customMediaPushModel->save($customMediaPushData, ['id' => $customMediaPushId]);
  503. $customService = new CustomService();
  504. #3. 更新 custom 表 内容
  505. $customData = [
  506. 'user_json' => $params['user_json'],
  507. 'sendtime' => strtotime($params['sendtime']),
  508. 'updatetime' => time(),
  509. 'message_type' => $params['message_type'],
  510. 'title' => $params['title'],
  511. ];
  512. if (!$params['message_type']) {
  513. // 图文
  514. $customData['message_json'] = $params['message_json'];
  515. $aMessageJson = json_decode($customData['message_json'], true);
  516. # 这里的bookIds 可能为空
  517. $bookIds = array_column($aMessageJson, 'book_id');
  518. $msgType = Message::MESSAGE_TYPE_IMAGE_TEXT;
  519. $aMessageJson = json_encode($aMessageJson);
  520. } else {
  521. $customData['message_text'] = $params['message_text'];
  522. $aMessageJson = json_decode($customData['message_text'], true);
  523. # 这里的bookIds 可能为空
  524. $bookIds = array_column($aMessageJson, 'book_id');
  525. $msgType = Message::MESSAGE_TYPE_LINK;
  526. $aMessageJson = json_encode($aMessageJson);
  527. }
  528. #4. 更新 custom_url表中的数据
  529. $aMessageJson = json_decode(CustomService::instance()->processUrlForChannelId($officialAccountId,
  530. $aMessageJson), true);
  531. $this->customUrlModel->where('custom_id', 'eq', $ids)->delete();
  532. CustomService::instance()->createCustomUrl(
  533. $bookIds,
  534. $ids,
  535. $customMediaPushId,
  536. $msgType,
  537. $officialAccountId,
  538. $official_account_type,
  539. $aMessageJson,
  540. $params['sendtime']
  541. );
  542. $result = $customService->updateCustomMessage($customData, $ids, $params['admin_id']);
  543. #endregion 更改客服消息
  544. //$result = $row->allowField(true)->save($params);
  545. if ($result !== false) {
  546. $this->success();
  547. } else {
  548. $this->error($row->getError());
  549. }
  550. } catch (\think\exception\PDOException $e) {
  551. $this->error($e->getMessage());
  552. }
  553. }
  554. $this->error(__('Parameter %s can not be empty', ''));
  555. }
  556. if (!empty($row['message_json'])) {
  557. $row['message_json'] = UrlService::instance()->replaceReferralHost($this->auth->id, $row['message_json'],
  558. false)->data;
  559. }
  560. if (!empty($row['message_text'])) {
  561. $row['message_text'] = UrlService::instance()->replaceReferralHost($this->auth->id, $row['message_text'],
  562. false)->data;
  563. }
  564. $customService = new CustomService();
  565. $customMediaInfo = $customService->getCustomMessageByPushId($row['custom_media_push_id']);
  566. $this->assign("customMediaInfo", $customMediaInfo);
  567. $this->assign("canedit", $canEdit);
  568. $this->view->assign("row", $row);
  569. $json = json_decode($row['user_json'], true);
  570. $from = $to = '';
  571. if ($json['all'] == 0 && !$json['subscribe_time'] && $json['subscribe_range']) {
  572. list($from, $to) = explode('-', $json['subscribe_range']);
  573. if($from == 'NaN'){
  574. $from = '';
  575. }
  576. if($to == 'NaN'){
  577. $to = '';
  578. }
  579. if($from){
  580. $from = date('Y-m-d H:i:s', $from);
  581. }
  582. if($to){
  583. $to = date('Y-m-d H:i:s', $to);
  584. }
  585. }
  586. $this->view->assign('from', $from);
  587. $this->view->assign('to', $to);
  588. return $this->view->fetch();
  589. }
  590. // 编辑图文次条
  591. public function editImageText()
  592. {
  593. $channelId = $this->auth->id;
  594. if ($this->request->isPost()) {
  595. $params = $this->request->post();
  596. $type = $params['row']['imgtxt_type'];
  597. $result = ['type' => $type];
  598. if ($type == Message::MESSAGE_LINK_TYPE_BOOK) {
  599. $result = array_merge($result, $params['book']);
  600. #$result['book_name'] = $params['book_book_id_text'];
  601. } elseif ($type == Message::MESSAGE_LINK_TYPE_ACTIVITY) {
  602. $result = array_merge($result, $params['activity']);
  603. } elseif ($type == Message::MESSAGE_LINK_TYPE_RECENT) {
  604. $result = array_merge($result, $params['recent']);
  605. } elseif ($type == Message::MESSAGE_LINK_TYPE_MENU) {
  606. $result = array_merge($result, $params['menu']);
  607. } elseif ($type == Message::MESSAGE_LINK_TYPE_NOURL) {
  608. $result = array_merge($result, $params['no_url']);
  609. } elseif ($type == Message::MESSAGE_LINK_TYPE_URL) {
  610. $result = array_merge($result, $params['url']);
  611. }elseif ($type == Message::MESSAGE_LINK_TYPE_DAILY_LINK) {
  612. $result = array_merge($result, $params['daily_url']);
  613. }if ($type == Message::MESSAGE_LINK_TYPE_MINI){
  614. $params['mini']['title'] = $params['mini']['title'] ?? '小程序码';
  615. $params['mini']['image'] = $params['mini']['image'] ?? '';
  616. $result = array_merge($result,$params['mini']);
  617. $result['url'] = $params['mini']['image'];
  618. if ($result['mini_type'] == 3 && empty($result['url'])){
  619. $this->error('图片必须上传');
  620. }
  621. }
  622. $this->success('', null, $result);
  623. } else {
  624. $_params = $this->request->get();
  625. $params = [];
  626. if (!empty($_params['media_id'])) {
  627. //$media_id = $this->request->get('media_id');
  628. $media_id = $_params['media_id'];
  629. $media_data = $this->customMediaModel->get(['id' => $media_id]);
  630. $json_str = $media_data->message_json;
  631. $json_data = json_decode($json_str, true, 512, JSON_BIGINT_AS_STRING);
  632. if ($json_data) {
  633. $params = array_pop($json_data);
  634. }
  635. } else {
  636. $json_str = $_params['json'];
  637. $params = json_decode($json_str, true, 512, JSON_BIGINT_AS_STRING);
  638. }
  639. $params['page'] = $params['page']?? '';
  640. $params['mini_type'] = $params['mini_type'] ?? 3;
  641. $referralModel = model('Referral');
  642. $wxTypeList = $referralModel->getWxTypeList();
  643. $pushList = $referralModel->getPushList();
  644. $recentUrl = Message::getRecentFullPath($channelId);
  645. $aActivity = CommonService::instance()->getValidActivities($this->auth->id);
  646. $menuList = [];
  647. foreach (Menu::$allLinks as $key => $link) {
  648. $menuList[$key] = $link['title'];
  649. }
  650. if (array_key_exists('book_id', $params)) {
  651. $oBook = $this->bookModel->where(['id' => $params['book_id']])->find();
  652. $this->assign('sex_text', $oBook->sex_text);
  653. }
  654. //获取独家书籍id
  655. $bnotin= [];
  656. ExclusiveService::instance()->getExclusiveNotidsWithoutWhere($this->group, $this->auth->id,$bnotin);
  657. $this->assignconfig('bnotin', $bnotin);
  658. //常用链接地址列表
  659. $dailyLink = $this->getCurrentUserDailyLink();
  660. $this->view->assign("dailyLink", $dailyLink);
  661. $this->assign('acc_type', Message::MESSAGE_TYPE_IMAGE_TEXT);
  662. $this->view->assign("params", $params);
  663. $this->view->assign("type", $params['type']);
  664. $this->view->assign("menuList", $menuList);
  665. $this->view->assign("wxTypeList", $wxTypeList);
  666. $this->view->assign("activityList", $aActivity);
  667. $this->view->assign("pushList", $pushList);
  668. $this->view->assign("recent_url", $recentUrl);
  669. }
  670. return $this->view->fetch();
  671. }
  672. public function editlinktext()
  673. {
  674. $channelId = $this->auth->id;
  675. if ($this->request->isPost()) {
  676. $params = $this->request->post();
  677. $params['push'] = '0';
  678. $type = $params['row']['imgtxt_type'];
  679. $result = ['type' => $type];
  680. if ($type == Message::MESSAGE_LINK_TYPE_BOOK) {
  681. $result = array_merge($result, $params['book']);
  682. #$result['book_name'] = $params['book_book_id_text'];
  683. } elseif ($type == Message::MESSAGE_LINK_TYPE_ACTIVITY) {
  684. $result = array_merge($result, $params['activity']);
  685. } elseif ($type == Message::MESSAGE_LINK_TYPE_RECENT) {
  686. $result = array_merge($result, $params['recent']);
  687. } elseif ($type == Message::MESSAGE_LINK_TYPE_MENU) {
  688. $result = array_merge($result, $params['menu']);
  689. } elseif ($type == Message::MESSAGE_LINK_TYPE_NOURL) {
  690. $result = array_merge($result, $params['no_url']);
  691. } elseif ($type == Message::MESSAGE_LINK_TYPE_URL) {
  692. $result = array_merge($result, $params['url']);
  693. } elseif ($type == Message::MESSAGE_LINK_TYPE_SIGN) {
  694. $params['sign']['url'] = "weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid=" . time();
  695. $result = array_merge($result, $params['sign']);
  696. }elseif ($type == Message::MESSAGE_LINK_TYPE_DAILY_LINK) {
  697. $result = array_merge($result, $params['daily_url']);
  698. } elseif ($type == Message::MESSAGE_LINK_TYPE_MINI){
  699. $result = array_merge($result,$params['mini']);
  700. }
  701. $this->success('', null, $result);
  702. } else {
  703. $json_str = $this->request->get('json');
  704. $params = json_decode($json_str, true, 512, JSON_BIGINT_AS_STRING);
  705. $params['page'] = $params['page']?? '';
  706. $referralModel = model('Referral');
  707. $wxTypeList = $referralModel->getWxTypeList();
  708. $pushList = $referralModel->getPushList();
  709. $recentUrl = getCurrentDomain($channelId, '/index/user/recent');
  710. $aActivity = CommonService::instance()->getValidActivities($this->auth->id);
  711. $menuList = [];
  712. foreach (Menu::$allLinks as $key => $link) {
  713. $menuList[$key] = $link['title'];
  714. }
  715. if (array_key_exists('book_id', $params)) {
  716. $oBook = $this->bookModel->where(['id' => $params['book_id']])->find();
  717. $this->assign('sex_text', $oBook->sex_text);
  718. }
  719. //常用链接地址列表
  720. $dailyLink = $this->getCurrentUserDailyLink();
  721. $this->view->assign("dailyLink", $dailyLink);
  722. //获取独家书籍id
  723. $bnotin= [];
  724. ExclusiveService::instance()->getExclusiveNotidsWithoutWhere($this->group, $this->auth->id,$bnotin);
  725. $this->assignconfig('bnotin', $bnotin);
  726. $this->assign('acc_type', Message::MESSAGE_TYPE_LINK);
  727. $this->view->assign("params", $params);
  728. $this->view->assign("type", $params['type']);
  729. $this->view->assign("menuList", $menuList);
  730. $this->view->assign("wxTypeList", $wxTypeList);
  731. $this->view->assign("activityList", $aActivity);
  732. $this->view->assign("pushList", $pushList);
  733. $this->view->assign("recent_url", $recentUrl);
  734. }
  735. return $this->view->fetch();
  736. }
  737. public function messageJson($content, $resolve = true)
  738. {
  739. $channelId = $this->getCurrentAccountChannelId();
  740. $contentData = json_decode($content, true)[0];
  741. $url = $contentData['url'];
  742. $transferResult = UrlService::instance()->replaceReferralHost($channelId, $url, $resolve);
  743. $transferUrl = $transferResult->data;
  744. $contentData['url'] = $transferUrl;
  745. return json_encode([$contentData], JSON_UNESCAPED_UNICODE);
  746. }
  747. /**
  748. * 编辑
  749. */
  750. public function editold($ids = null)
  751. {
  752. $canEdit = 0;
  753. $row = $this->model->get($ids);
  754. if (!$row) {
  755. $this->error(__('No Results were found'));
  756. }
  757. $adminIds = $this->getDataLimitAdminIds();
  758. if (is_array($adminIds)) {
  759. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  760. $this->error(__('You have no permission'));
  761. }
  762. }
  763. if ($row['sendtime'] <= time()) { //已发送,不能修改
  764. $canEdit = 1;
  765. }
  766. if ($row['sendtime'] > time() && $row['sendtime'] - time() <= 600) { //发送前10分钟内不能修改
  767. $canEdit = 1;
  768. }
  769. // 占位符 返解析为域名
  770. $row['message_json'] = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(),
  771. $row['message_json'], false)->data;
  772. if ($this->request->isPost()) {
  773. $this->error("新版客服消息上线后,老数据无法编辑");
  774. }
  775. $this->assign("canedit", $canEdit);
  776. $this->view->assign("row", $row);
  777. return $this->view->fetch();
  778. }
  779. /**
  780. * 获取当前用户可用的链接地址
  781. * @return array
  782. */
  783. protected function getCurrentUserDailyLink()
  784. {
  785. //常用链接地址
  786. $dailyLink = CommonService::instance()->getTmpDailyLink();
  787. $channelId = null;
  788. if ($this->auth->agent_id) { //配号代理商
  789. $channelId = $this->auth->agent_id;
  790. } elseif ($this->group == 4) { //代理商
  791. $channelId = $this->auth->channel_id;
  792. } elseif ($this->group == 3) { //渠道商
  793. $channelId = $this->auth->channel_id;
  794. }
  795. if($channelId){
  796. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channelId);
  797. if (!($adminConfig['vip_state'] ?? false) && ($adminConfig['vip_goods_id'] ?? null)){
  798. unset($dailyLink['vip']);
  799. }
  800. $configChannelIds = Config::get('site.smallpay_channel_id') ?? '';
  801. if ($configChannelIds == '*') {
  802. $open = true;
  803. } else {
  804. $list = explode(',', $configChannelIds);
  805. $open = in_array($channelId, $list);
  806. }
  807. if ($open) {
  808. $adminConfig['small_goods_id'] = Config::get('site.smallpay_goods_id');
  809. }
  810. if(!($adminConfig['small_goods_id'] ?? null)){
  811. unset($dailyLink['smallpay']);
  812. }
  813. }
  814. return $dailyLink;
  815. }
  816. }