Subscribedelay.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\common\constants\Message;
  4. use app\common\controller\Backend;
  5. use app\common\model\SubscribeDelayPush;
  6. use app\main\service\HigeMessageService;
  7. use app\main\service\MiniProgramService;
  8. use app\main\service\SubscribeVipDelayService;
  9. use app\main\service\UrlService;
  10. /**
  11. * 关注延时推送设置
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Subscribedelay extends Backend
  16. {
  17. /**
  18. * @var SubscribeDelayPush
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['getminipage'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('SubscribeDelayPush');
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. $adminConfig = model("AdminConfig")->where('admin_id', 'eq', $this->auth->id)->find();
  39. $data = $this->model->where('status', 'normal')->where('admin_id', $this->auth->id)->select();
  40. usort($data, function($v1, $v2){
  41. if ($v1['type'] == '1') {
  42. $v1Delay = $v1['delay'];
  43. } else {
  44. $v1Delay = $v1['delay'] * 60;
  45. }
  46. if ($v2['type'] == '1') {
  47. $v2Delay = $v2['delay'];
  48. } else {
  49. $v2Delay = $v2['delay'] * 60;
  50. }
  51. if ($v1Delay > $v2Delay) {
  52. return 1;
  53. }
  54. });
  55. $return = [];
  56. $miniOpen = MiniProgramService::instance()->miniWrite($this->auth->id,$this->group);
  57. foreach ($data as $index=>$item) {
  58. $tmp_data = json_decode($item['news_set'], true);
  59. $tmp_data['id'] = $item['id'];
  60. $tmp_data['content_type'] = $item['content_type']??1;
  61. $tmp_data['mini_open'] = $miniOpen;
  62. $tmp_data['text_content'] = $tmp_data['text_content'] ?? [];
  63. $tmp_data['is_sign'] = (int)($tmp_data['is_sign'] ?? 1);
  64. $tmp_data['sex'] = (int)($tmp_data['sex'] ?? 0);
  65. $tmp_data['mobile_system'] = $tmp_data['mobile_system']??0;
  66. $tmp_data['user_type'] = $tmp_data['user_type']??1;
  67. //小程序默认类型
  68. $tmp_data['mini_type'] = (!isset($tmp_data['mini_type']) || $tmp_data['mini_type'] < 3) ? 3 : $tmp_data['mini_type'];
  69. $return[] = $tmp_data;
  70. }
  71. if ($this->request->isPost()) {
  72. return json([
  73. 'data' => $return,
  74. 'switch' => $adminConfig['subscribe_delay'],
  75. ]);
  76. }
  77. $this->assignconfig('mini_open', $miniOpen);
  78. $this->assignconfig('data', $return);
  79. $this->assignconfig('switch', $adminConfig['subscribe_delay']);
  80. return $this->view->fetch();
  81. }
  82. /**
  83. * 开关
  84. */
  85. public function operate()
  86. {
  87. $state = $this->request->param('state');
  88. $subscribeDelay = '1';
  89. if ($state == 'normal') {
  90. $subscribeDelay = '2';
  91. }
  92. if (model("AdminConfig")->update(['subscribe_delay' => $subscribeDelay], ['admin_id' => $this->auth->id])) {
  93. //需要删除一下缓存
  94. model("AdminConfig")->delAdminInfoAllCache($this->auth->id);
  95. $result = [
  96. 'code' => '200',
  97. 'msg' => '操作成功',
  98. ];
  99. } else {
  100. $result = [
  101. 'code' => '202',
  102. 'msg' => '操作失败',
  103. ];
  104. }
  105. //清除缓存
  106. $this->model->delCache($this->auth->id);
  107. exit(json_encode($result, JSON_UNESCAPED_UNICODE));
  108. }
  109. /**
  110. * 保存
  111. */
  112. public function add()
  113. {
  114. if ($this->request->isPost()) {
  115. $params = $this->request->post('data');
  116. $params = json_decode($params,true);
  117. if ($params)
  118. {
  119. if ($this->dataLimit)
  120. {
  121. $params[$this->dataLimitField] = $this->auth->id;
  122. }
  123. try
  124. {
  125. $params['url_type'] = (int)($params['url_type'] ?? 1);
  126. $params['user_type'] = (int)($params['user_type'] ?? 1);
  127. $params['mobile_system'] = (int)($params['mobile_system'] ?? 0);
  128. $params['content_type'] = intval($params['content_type']??1);//1是图片 2是文本
  129. $params['mini_type'] = intval($params['mini_type']??1);//小程序类型
  130. $content = [
  131. 'title' => $params['title'],
  132. 'image' => $params['image'],
  133. 'url_type' => $params['url_type'],
  134. 'description' => $params['description'],
  135. 'user_type' => $params['user_type'],
  136. 'mobile_system' => $params['mobile_system'],
  137. 'sex' =>$params['sex'],
  138. 'mini_type' =>$params['mini_type'] ?? 0,
  139. ];
  140. if ($params['content_type'] == 2){ //文本类型
  141. if (empty($params['text_content'])){
  142. $this->error('文本消息不能为空');
  143. }
  144. $content['description'] = SubscribeVipDelayService::instance()->getContent($params,$this->getCurrentAccountChannelId());
  145. }else{
  146. switch ($params['url_type']) {
  147. case '1':
  148. $content['url'] = $params['referral_url']??'';
  149. break;
  150. case '2'://活动链接
  151. $content['url'] = $params['activity_url']??'';
  152. break;
  153. case '3'://继续阅读
  154. $content['url'] = getCurrentDomain($this->getCurrentAccountChannelId(), '/index/book/chapter');
  155. break;
  156. case '4'://自定义链接
  157. $content['url'] = $params['self_url'];
  158. break;
  159. case '5'://自动签到
  160. $content['url'] = "weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}";
  161. break;
  162. case '6'://小程序
  163. $content['mini_content'] = MiniProgramService::instance()->getMiniContent($this->auth->id,$params);
  164. break;
  165. }
  166. }
  167. $content = json_encode($content, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  168. $content = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $content)->data;
  169. if (!empty($params['id'])) {
  170. $row = $this->model->get($params['id']);
  171. $id = $params['id'];
  172. } else {
  173. $where = [
  174. 'admin_id' => $this->auth->id,
  175. 'type' => $params['unit'],
  176. 'delay' => $params['current_time'],
  177. ];
  178. $row = $this->model->where($where)->find();
  179. $id = $row['id'];
  180. }
  181. if ($row) {
  182. if (!$row) {
  183. $this->error('客服消息不存在');
  184. }
  185. // $params['text_content'] = json_encode($params['text_content'], JSON_UNESCAPED_UNICODE);
  186. if (md5($content) != md5($row['content'])) {
  187. $data = [
  188. 'type' => $params['unit'],
  189. 'delay' => $params['current_time'],
  190. 'content' => $content,
  191. 'content_type'=>$params['content_type'],
  192. 'news_set' => json_encode($params, JSON_UNESCAPED_UNICODE),
  193. 'status' => 'normal',
  194. 'updatetime' => time()
  195. ];
  196. $result = $row->allowField(true)->save($data);
  197. if ($result !== false)
  198. {
  199. //需要清理缓存
  200. $this->model->delCache($this->auth->id);
  201. $this->success('', '', ['id' => $id]);
  202. }
  203. else
  204. {
  205. $this->error($row->getError());
  206. }
  207. } else {
  208. //无改动
  209. $this->success('', '', ['id' => $id]);
  210. }
  211. } else {
  212. //新增
  213. $data = [
  214. 'admin_id' => $this->auth->id,
  215. 'type' => $params['unit'],
  216. 'delay' => $params['current_time'],
  217. 'content' => $content,
  218. 'content_type'=>$params['content_type'],
  219. 'news_set' => json_encode($params, JSON_UNESCAPED_UNICODE),
  220. 'status' => 'normal',
  221. 'updatetime' => time(),
  222. 'createtime' => time(),
  223. ];
  224. $id = $this->model->allowField(true)->insertGetId($data);
  225. if ($id !== false)
  226. {
  227. //需要清理一下缓存
  228. $this->model->delCache($this->auth->id);
  229. $this->success('', '', ['id' => $id]);
  230. }
  231. else
  232. {
  233. $this->error($this->model->getError());
  234. }
  235. }
  236. }
  237. catch (\think\exception\PDOException $e)
  238. {
  239. $this->error($e->getMessage());
  240. }
  241. }
  242. $this->error(__('Parameter %s can not be empty', ''));
  243. }
  244. }
  245. /**
  246. * 删除
  247. */
  248. public function delete($ids = "")
  249. {
  250. if ($ids)
  251. {
  252. $pk = $this->model->getPk();
  253. $adminIds = $this->getDataLimitAdminIds();
  254. if (is_array($adminIds))
  255. {
  256. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  257. }
  258. $list = $this->model->where($pk, 'in', $ids)->select();
  259. $count = 0;
  260. foreach ($list as $k => $v)
  261. {
  262. $count += $v->save(['status' => 'hidden', 'updatetime' => time()]);
  263. }
  264. //清理缓存
  265. $this->model->delCache($this->auth->id);
  266. if ($count)
  267. {
  268. $this->success();
  269. }
  270. else
  271. {
  272. $this->error(__('No rows were deleted'));
  273. }
  274. }
  275. $this->error(__('Parameter %s can not be empty', 'ids'));
  276. }
  277. public function getMiniPage()
  278. {
  279. $pageStr = config('site.mini_page');
  280. if (!$pageStr){
  281. return [];
  282. }
  283. $pageArr = explode(PHP_EOL,$pageStr);
  284. foreach ($pageArr as $k=>$v){
  285. $temp = explode('@',$v);
  286. $miniPages[] = array(
  287. 'name'=>$temp[0],
  288. 'page'=>$temp[1]
  289. );
  290. }
  291. return json($miniPages);
  292. }
  293. }