Uclandingpage.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\ExclusiveService;
  4. use app\common\controller\Backend;
  5. use app\common\library\Redis;
  6. use app\main\constants\AdminConstants;
  7. use app\main\constants\BookConstants;
  8. use app\main\constants\CacheConstants;
  9. use app\main\constants\UcCacheConstants;
  10. use app\main\service\AdminService;
  11. use app\main\service\BookService;
  12. use think\Config;
  13. use think\db\Query;
  14. use think\Exception;
  15. /**
  16. * 头条落地页
  17. *
  18. * @icon fa fa-circle-o
  19. */
  20. class Uclandingpage extends Backend
  21. {
  22. /**
  23. * @var \app\admin\model\ToutiaoLandingPage
  24. */
  25. protected $model = null;
  26. protected $noNeedRight = ['tpl', 'editwx', 'callbacktime', 'autocompletebookname'];
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = model('UcLandingPage');
  31. $this->assignconfig('scheme', Config::get('site.scheme'));
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. */
  41. public function index()
  42. {
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags']);
  45. if ($this->request->isAjax()) {
  46. //如果发送的来源是Selectpage,则转发到Selectpage
  47. if ($this->request->request('pkey_name')) {
  48. return $this->selectpage();
  49. }
  50. [$where, $sort, $order, $offset, $limit] = $this->buildparams('title');
  51. $total = $this->model
  52. ->where($where)
  53. ->where('admin_id', $this->auth->id)
  54. ->order($sort, $order)
  55. ->count();
  56. $list = $this->model
  57. ->where($where)
  58. ->where('admin_id', $this->auth->id)
  59. ->order($sort, $order)
  60. ->limit($offset, $limit)
  61. ->select();
  62. $book_ids = array_column($list, 'book_id');
  63. $books = BookService::instance()->getBookModel()->getBooksInfo($book_ids);
  64. foreach ($list as $index => $item) {
  65. if (array_key_exists($item['book_id'], $books)) {
  66. $list[$index]['book_name'] = $books[$item['book_id']]['name'];
  67. $payCount = (float)Redis::instance()->get(UcCacheConstants::getGuideWxPayCount($item['id']));
  68. $payMoney = (float)Redis::instance()->get(UcCacheConstants::getGuideWxPayMoney($item['id']));
  69. $new = (float)Redis::instance()->get(UcCacheConstants::getGuideWxNewCount($item['id']));
  70. $payDayCount = (float)Redis::instance()->get(UcCacheConstants::getGuideWxDayPayCount($item['id']));
  71. $payDayMoney = (float)Redis::instance()->get(UcCacheConstants::getGuideWxDayPayMoney($item['id']));
  72. $newDay = (float)Redis::instance()->get(UcCacheConstants::getGuideWxDayNewCount($item['id']));
  73. $list[$index]['new'] = $newDay . '/' . $new;
  74. $list[$index]['payCount'] = $payDayCount . '/' . $payCount;
  75. $list[$index]['payMoney'] = $payDayMoney . '/' . $payMoney;
  76. }
  77. }
  78. $result = array("total" => $total, "rows" => $list);
  79. return json($result);
  80. }
  81. $this->assignconfig('pay_url', getCurrentDomain($this->auth->id, '/index/user/ucpaytest'));
  82. return $this->view->fetch();
  83. }
  84. /**
  85. * 添加
  86. */
  87. public function add()
  88. {
  89. if ($this->request->isPost()) {
  90. $cacheKey = UcCacheConstants::getGuideWxSetting($this->auth->id);
  91. $data = Redis::instance()->hGetAll($cacheKey);
  92. if (empty($data)) {
  93. $this->error('请先配置微信号与随机码');
  94. }
  95. $params = $this->request->post("row/a");
  96. if($params['focus_status']==2){
  97. if(!isset($data['biz']) || empty($data['biz'])){
  98. $this->error('请先配置微信号biz码');
  99. }
  100. }
  101. if ($params) {
  102. /*
  103. * 已经弃用,如果为了兼容老版可取消注释
  104. foreach ($params as $k => &$v)
  105. {
  106. $v = is_array($v) ? implode(',', $v) : $v;
  107. }
  108. */
  109. if ($this->dataLimit) {
  110. $params[$this->dataLimitField] = $this->auth->id;
  111. }
  112. $params['admin_id'] = $this->auth->id;
  113. try {
  114. //是否采用模型验证
  115. if ($this->modelValidate) {
  116. $name = basename(str_replace('\\', '/', get_class($this->model)));
  117. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  118. $this->model->validate($validate);
  119. }
  120. $params['createtime'] = $params['updatetime'] = time();
  121. $result = $this->model->allowField(true)->insertGetId($params);
  122. if ($result !== false) {
  123. $params['id'] = $result;
  124. $adminInfo = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($this->auth->id);
  125. $chapters = model('Book')::getHeadChapters($params['book_id'], $params['chapter_num']);
  126. $params['chapters'] = $chapters['data'];
  127. $params['admin'] = $adminInfo['json']['authorizer_info'];
  128. $params['appid'] = $adminInfo['appid'];
  129. if (!array_key_exists('head_img', $params['admin'])) {
  130. $params['admin']['head_img'] = asset('/img/avatar.png');
  131. }
  132. $params['admin']['admin_card'] = 'https://mp.weixin.qq.com/mp/getmasssendmsg?__biz='.$data['biz'].'#wechat_webview_type=1&wechat_redirect';
  133. $cdn_url = '';
  134. if (Config::get('site.cdnurl')) {
  135. $cdn_url = Config::get('site.cdnurl');
  136. }
  137. $params['company'] = $this->getTtSetting($params['domain']);
  138. $params['cdn_url'] = $cdn_url;
  139. $params['setting'] = $data;
  140. $this->assign($params);
  141. if($params['focus_status'] == 0){
  142. //点击关注
  143. $content = $this->fetch(APP_PATH . 'admin/view/uclandingpage/tpl.html');
  144. }elseif($params['focus_status'] == 1){
  145. // 长按复制关注
  146. $content = $this->fetch(APP_PATH . 'admin/view/uclandingpage/tpl_v2.html');
  147. }elseif($params['focus_status'] == 2){
  148. //朋友圈关注
  149. $content = $this->fetch(APP_PATH . 'admin/view/uclandingpage/tpl_v3.html');
  150. }
  151. //$content = $this->fetch(APP_PATH . 'admin/view/uclandingpage/tpl.html');
  152. @file_put_contents(ROOT_PATH . 'public/uploads/static/uc/' . $result . '.html', $content);
  153. $this->success();
  154. } else {
  155. $this->error($this->model->getError());
  156. }
  157. } catch (\think\exception\PDOException $e) {
  158. $this->error($e->getMessage());
  159. }
  160. }
  161. $this->error(__('Parameter %s can not be empty', ''));
  162. }
  163. $domain = [];
  164. $tt_url = Config::get('site.uc_url');
  165. if ($tt_url) {
  166. $list = explode(',', $tt_url);
  167. foreach ($list as $item) {
  168. $item_li = explode('/', $item);
  169. $domain[$item_li[0]] = $item_li[1];
  170. }
  171. }
  172. $this->view->assign('domain', $domain);
  173. return $this->view->fetch();
  174. }
  175. /**
  176. * 编辑
  177. */
  178. public function edit($ids = null)
  179. {
  180. $row = $this->model->get($ids);
  181. if (!$row) {
  182. $this->error(__('No Results were found'));
  183. }
  184. $adminIds = $this->getDataLimitAdminIds();
  185. if (is_array($adminIds)) {
  186. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  187. $this->error(__('You have no permission'));
  188. }
  189. }
  190. $cacheKey = UcCacheConstants::getGuideWxSetting($this->auth->id);
  191. $data = Redis::instance()->hGetAll($cacheKey);
  192. if (empty($data)) {
  193. $this->error('请先配置微信号与随机码');
  194. }
  195. if($row['focus_status'] == 2){
  196. if(!isset($data['biz']) || empty($data['biz'])){
  197. $this->error('请先配置微信号biz码');
  198. }
  199. }
  200. if ($this->request->isPost()) {
  201. $params = $this->request->post("row/a");
  202. if ($params) {
  203. /*
  204. * 已经弃用,如果为了兼容老版可取消注释
  205. foreach ($params as $k => &$v)
  206. {
  207. $v = is_array($v) ? implode(',', $v) : $v;
  208. }
  209. */
  210. try {
  211. //是否采用模型验证
  212. if ($this->modelValidate) {
  213. $name = basename(str_replace('\\', '/', get_class($this->model)));
  214. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  215. $row->validate($validate);
  216. }
  217. $result = $row->allowField(true)->save($params);
  218. if ($result !== false) {
  219. $params = array_merge($row->getData(), $params);
  220. $params['id'] = $ids;
  221. $adminInfo = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($this->auth->id);
  222. $chapters = model('Book')::getHeadChapters($params['book_id'], $params['chapter_num']);
  223. $params['chapters'] = $chapters['data'];
  224. $params['admin'] = $adminInfo['json']['authorizer_info'];
  225. $params['appid'] = $adminInfo['appid'];
  226. if (!array_key_exists('head_img', $params['admin'])) {
  227. $params['admin']['head_img'] = asset('/img/avatar.png');
  228. }
  229. if($params['focus_status'] == 2){
  230. $params['admin']['admin_card'] = 'https://mp.weixin.qq.com/mp/getmasssendmsg?__biz='.$data['biz'].'#wechat_webview_type=1&wechat_redirect';
  231. }
  232. $cdn_url = '';
  233. if (Config::get('site.cdnurl')) {
  234. $cdn_url = Config::get('site.cdnurl');
  235. }
  236. $params['company'] = $this->getTtSetting($params['domain']);
  237. $params['cdn_url'] = $cdn_url;
  238. $params['setting'] = $data;
  239. $this->assign($params);
  240. if($params['focus_status'] == 0){
  241. //点击关注
  242. $content = $this->fetch(APP_PATH . 'admin/view/uclandingpage/tpl.html');
  243. }elseif($params['focus_status'] == 1){
  244. // 长按复制关注
  245. $content = $this->fetch(APP_PATH . 'admin/view/uclandingpage/tpl_v2.html');
  246. }elseif($params['focus_status'] == 2){
  247. //朋友圈关注
  248. $content = $this->fetch(APP_PATH . 'admin/view/uclandingpage/tpl_v3.html');
  249. }
  250. //$data = $this->fetch(APP_PATH . 'admin/view/uclandingpage/tpl.html');
  251. @file_put_contents(ROOT_PATH . 'public/uploads/static/uc/' . $ids . '.html', $content);
  252. $this->success();
  253. } else {
  254. $this->error($row->getError());
  255. }
  256. } catch (\think\exception\PDOException $e) {
  257. $this->error($e->getMessage());
  258. }
  259. }
  260. $this->error(__('Parameter %s can not be empty', ''));
  261. }
  262. $row['book_id'] = $row['book_id'] > 0 ? $row['book_id'] : '';
  263. $this->view->assign("row", $row);
  264. $domain = [];
  265. $tt_url = Config::get('site.uc_url');
  266. if ($tt_url) {
  267. $list = explode(',', $tt_url);
  268. foreach ($list as $item) {
  269. $item_li = explode('/', $item);
  270. $domain[$item_li[0]] = $item_li[1];
  271. }
  272. }
  273. $bnotin = [];
  274. ExclusiveService::instance()->getExclusiveNotidsWithoutWhere($this->group, $this->auth->id,$bnotin);
  275. $this->assignconfig('bnotin',$bnotin);
  276. $this->view->assign('domain', $domain);
  277. return $this->view->fetch();
  278. }
  279. /**
  280. * 删除
  281. */
  282. public function del($ids = "")
  283. {
  284. if ($ids) {
  285. $pk = $this->model->getPk();
  286. $adminIds = $this->getDataLimitAdminIds();
  287. if (is_array($adminIds)) {
  288. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  289. }
  290. $list = $this->model->where($pk, 'in', $ids)->select();
  291. $count = 0;
  292. foreach ($list as $k => $v) {
  293. @unlink(ROOT_PATH . 'public/uploads/static/uc/' . $v['id'] . '.html');
  294. $count += $v->delete();
  295. }
  296. if ($count) {
  297. $this->success();
  298. } else {
  299. $this->error(__('No rows were deleted'));
  300. }
  301. }
  302. $this->error(__('Parameter %s can not be empty', 'ids'));
  303. }
  304. public function editwx()
  305. {
  306. $cacheKey = UcCacheConstants::getGuideWxSetting($this->auth->id);
  307. if ($this->request->isPost()) {
  308. Redis::instance()->hMSet($cacheKey, $this->request->post());
  309. $this->success();
  310. }
  311. $data = Redis::instance()->hGetAll($cacheKey);
  312. $data = array_merge(['gzh' => '', 'mask' => '', 'biz' => ''], $data);
  313. $this->assign($data);
  314. return $this->view->fetch();
  315. }
  316. public function callbacktime()
  317. {
  318. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($this->auth->id);
  319. if ($this->request->isAjax()) {
  320. AdminService::instance()->getAdminConfigModel()->update($this->request->post(), ['admin_id' => $this->auth->id]);
  321. model('AdminConfig')->delAdminInfoAllCache($this->auth->id);
  322. $this->success();
  323. }
  324. $this->view->assign('admin', $adminConfig);
  325. $adminConfig['callback_time_uc'] = $adminConfig['callback_time_uc'] ?? AdminConstants::CALLBACK_TIME_ONCE_24;
  326. return $this->view->fetch();
  327. }
  328. public function getTtSetting($domain)
  329. {
  330. $setting = Config::get('site.uc_url');
  331. $list = explode(',', $setting);
  332. $default = Config::get('site.uc_company');
  333. foreach ($list as $item) {
  334. $item_l = explode('/', $item);
  335. if (count($item_l) == 3) {
  336. $target = $item_l[2];
  337. } else {
  338. $target = $default;
  339. }
  340. if ($domain == $item_l[0]) {
  341. return $target;
  342. }
  343. }
  344. return $default;
  345. }
  346. public function autocompletebookname()
  347. {
  348. if ($this->request->isAjax()) {
  349. $keyword = $this->request->post('name') ?? $this->request->param('pkey_value');
  350. $data = [];
  351. $keyword = trim($keyword);
  352. $bookModel = model('Book');
  353. $map = [];
  354. if ($this->group > 2) {
  355. $map['cansee'] = 1;
  356. }
  357. //获取独家书籍id
  358. ExclusiveService::instance()->getExclusiveNotidsWithWhere($this->group, $this->auth->id,$map);
  359. if (!empty($keyword)) {
  360. $bookList = $bookModel
  361. ->where('state', 'neq', BookConstants::BOOK_STATE_OFF_SALE)
  362. ->where($map)
  363. ->where(function (Query $query) use ($keyword) {
  364. $query->where('name', 'like', "%{$keyword}%")
  365. ->whereOr('id', $keyword);
  366. })
  367. ->limit(0, 10)->select();
  368. } else {
  369. $bookList = $bookModel->where('state', 'neq',
  370. BookConstants::BOOK_STATE_OFF_SALE)->where($map)->order('book.idx desc,book.idxx desc')->limit(0, 10)->select();
  371. }
  372. $data = $bookList;
  373. return json(['list' => $data, 'total' => count($data)]);
  374. }
  375. }
  376. }