Minipro0001.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\plugins\model;
  14. use think\Model;
  15. use think\Db;
  16. /**
  17. * 模型
  18. */
  19. class Minipro0001 extends Model
  20. {
  21. // 插件标识
  22. public $nid;
  23. //初始化
  24. protected function initialize()
  25. {
  26. // 需要调用`Model`的`initialize`方法
  27. parent::initialize();
  28. $this->nid = 'Minipro0001';
  29. }
  30. /**
  31. * 全局常量
  32. * @param string $type 类型
  33. * @author 小虎哥 by 2018-8-18
  34. */
  35. public function getGlobalsConf()
  36. {
  37. $cacheKey = 'model-'.$this->nid.'-getGlobalsConf';
  38. $result = cache($cacheKey);
  39. if (empty($result)) {
  40. $webData = tpCache('web');
  41. $barlist = $this->getBarlist();
  42. $webconfig = array(
  43. 'web_name' => empty($barlist['nav_title']) ? $webData['web_name'] : $barlist['nav_title'],
  44. 'web_copyright' => empty($barlist['copyright']) ? $webData['web_copyright'] : $barlist['copyright'],
  45. );
  46. $result = array(
  47. 'webconfig' => $webconfig,
  48. 'blist' => $barlist,
  49. );
  50. cache($cacheKey, $result, null, 'minipro');
  51. }
  52. return $result;
  53. }
  54. /**
  55. * 获取配置值
  56. * @param string $type 类型
  57. * @author 小虎哥 by 2018-8-18
  58. */
  59. public function getValue($type)
  60. {
  61. // $cacheKey = 'model-'.$this->nid.'-getValue-'.$type;
  62. // $value = cache($cacheKey);
  63. // if (empty($value)) {
  64. $map = array(
  65. 'type' => $type,
  66. );
  67. $value = M('weapp_minipro0001')->where($map)->cache(true,EYOUCMS_CACHE_TIME,"minipro")->value('value');
  68. $value = (array)json_decode($value, true);
  69. // cache($cacheKey, $value, null, 'minipro');
  70. // }
  71. return $value;
  72. }
  73. /**
  74. * 首页配置
  75. */
  76. public function getHomeConf() {
  77. // 定义公共常量
  78. $result = $this->getValue('home');
  79. foreach ($result as $key => $val) {
  80. if (is_array($val)) {
  81. foreach ($val as $k2 => $v2) {
  82. /*转换图片为远程http*/
  83. if (1 == preg_match('/(_img|_selimg)$/', $k2)) {
  84. if (!is_http_url($v2)) {
  85. $result[$key][$k2] = request()->domain().$v2;
  86. }
  87. }
  88. /*--end*/
  89. }
  90. }
  91. }
  92. return $result;
  93. }
  94. /**
  95. * 获取幻灯片
  96. * @param int $num 数量
  97. * @param string $aid 文档ID,多个以逗号隔开
  98. */
  99. public function getSwipersList($aid = '')
  100. {
  101. // $cacheKey = 'model-'.$this->nid."-getSwipersList-{$aid}";
  102. // $result = cache($cacheKey);
  103. // if (empty($result)) {
  104. if (empty($aid)) {
  105. $map = array(
  106. 'is_head' => 1,
  107. 'status' => 1,
  108. 'lang' => get_current_lang(),
  109. );
  110. $num = 8;
  111. } else {
  112. $map = array(
  113. 'aid' => array('in', $aid),
  114. );
  115. $num = '';
  116. }
  117. $result = M('archives')->field('aid,litpic')
  118. ->where($map)
  119. ->order('sort_order asc, aid desc')
  120. ->limit($num)
  121. ->cache(true,EYOUCMS_CACHE_TIME,"minipro")
  122. ->select();
  123. foreach ($result as $key => $val) {
  124. $val['litpic'] = get_default_pic($val['litpic'], true);
  125. $result[$key] = $val;
  126. }
  127. // cache($cacheKey, $result, null, 'minipro');
  128. // }
  129. return $result;
  130. }
  131. /**
  132. * 获取全部栏目
  133. * @param string $channel 栏目ID,多个以逗号隔开
  134. * @param int $num 数量
  135. */
  136. public function getArctype($typeid = '', $channel = '')
  137. {
  138. $typeid = intval($typeid);
  139. $channel = intval($channel);
  140. $cacheKey = 'model-'.$this->nid."-getArctype-{$typeid}-{$channel}";
  141. $result = cache($cacheKey);
  142. if (empty($result)) {
  143. $typename = ''; // 用于分享标题
  144. if (0 < $typeid) {
  145. $result = model('Arctype')->getChannelList($typeid, 'son');
  146. $arctypeInfo = model('Arctype')->getInfo($typeid);
  147. $parent_id = $arctypeInfo['parent_id'];
  148. $typename = $arctypeInfo['typename']; // 用于分享标题
  149. if (empty($result)) {
  150. if (0 < intval($parent_id)) {
  151. $selfRow[0] = array();
  152. $selfRow[0] = model('Arctype')->getInfo($parent_id);
  153. $selfRow[0]['typename'] = '全部';
  154. $selfRow[0]['selected'] = true;
  155. $result = model('Arctype')->getChannelList($typeid, 'self');
  156. $typename = M('arctype')->where('id','eq',$typeid)->value('typename'); // 用于分享标题
  157. } else {
  158. $typename = $arctypeInfo['typename'];
  159. $selfRow[0] = array();
  160. $selfRow[0] = $arctypeInfo;
  161. $selfRow[0]['typename'] = $typename;
  162. $selfRow[0]['selected'] = true;
  163. }
  164. $result = array_merge($selfRow, $result);
  165. } else {
  166. $selfRow[0] = array();
  167. $selfRow[0] = model('Arctype')->getInfo($typeid);
  168. $selfRow[0]['typename'] = '全部';
  169. $selfRow[0]['selected'] = true;
  170. $result = array_merge($selfRow, $result);
  171. }
  172. } else { // 全部栏目
  173. $map = array();
  174. if (0 < $channel) {
  175. $map['current_channel'] = array('eq', intval($channel));
  176. }
  177. /*获取所有栏目*/
  178. $arctypeLogic = new \app\common\logic\ArctypeLogic;
  179. $result = $arctypeLogic->arctype_list(0, 0, false, 0, $map, false);
  180. /*--end*/
  181. }
  182. /*栏目层级归类成阶梯式*/
  183. /* $arr = group_same_key($result, 'parent_id');
  184. $arctype_max_level = intval(config('global.arctype_max_level'));
  185. for ($i=0; $i < $arctype_max_level; $i++) {
  186. foreach ($arr as $key => $val) {
  187. foreach ($arr[$key] as $key2 => $val2) {
  188. if (!isset($arr[$val2['id']])) continue;
  189. $val2['children'] = $arr[$val2['id']];
  190. $arr[$key][$key2] = $val2;
  191. }
  192. }
  193. }
  194. $result = $arr;*/
  195. /*--end*/
  196. $result = array(
  197. 'conf' => array(
  198. 'shareTitle' => ($typename ? $typename.'_' : '').tpCache('web.web_name'),
  199. ),
  200. 'row' => $result,
  201. );
  202. cache($cacheKey, $result, null, 'minipro');
  203. }
  204. return $result;
  205. }
  206. /**
  207. * 文档列表
  208. * @param string $param 查询条件的数组
  209. * @param int $page 页码
  210. * @param int $pagesize 每页记录数
  211. */
  212. public function getArchivesList($param = array(), $page = 1, $pagesize = null, $field = 'aid,title,litpic,seo_description,add_time')
  213. {
  214. $param['arcrank'] = isset($param['arcrank']) ? $param['arcrank'] : -1;
  215. $pagesize = empty($pagesize) ? config('paginate.list_rows') : $pagesize;
  216. $cacheKey = "model-".$this->nid."-getArchivesList-".json_encode($param)."-{$page}-{$pagesize}-{$field}";
  217. $result = cache($cacheKey);
  218. if (empty($result)) {
  219. $condition = array();
  220. // 应用搜索条件
  221. foreach (['channel','typeid','flag','arcrank'] as $key) {
  222. if (isset($param[$key]) && ('' !== $param[$key] || null !== $param[$key])) {
  223. if ('typeid' == $key) {
  224. if (!empty($param[$key])) {
  225. if (is_string($param[$key]) && stristr($param[$key], ',')) {
  226. // 指定多个栏目ID
  227. $typeid = func_preg_replace(array(','), ',', $param[$key]);
  228. $typeid = explode(',', $typeid);
  229. } else if (is_string($param[$key]) && !stristr($param[$key], ',')) {
  230. /*当前栏目ID,以及所有子栏目ID*/
  231. $channel_info = M('Arctype')->field('id,current_channel')->where(array('id'=>array('eq', $param[$key])))->find();
  232. $childrenRow = model('Arctype')->getHasChildren($param[$key]);
  233. foreach ($childrenRow as $k2 => $v2) {
  234. if ($channel_info['current_channel'] != $v2['current_channel']) {
  235. unset($childrenRow[$k2]); // 排除不是同一模型的栏目
  236. }
  237. }
  238. $typeid = get_arr_column($childrenRow, 'id');
  239. /*--end*/
  240. }
  241. $condition[$key] = array('IN', $typeid);
  242. }
  243. } else if ('channel' == $key) {
  244. if (!empty($param[$key])) {
  245. if (is_string($param[$key])) {
  246. $channel = func_preg_replace(array(','), ',', $param[$key]);
  247. $channel = explode(',', $channel);
  248. }
  249. $condition[$key] = array('IN', $channel);
  250. }
  251. } else if ('flag' == $key) {
  252. $tmp_key_arr = array();
  253. $flag_arr = explode(",", $param[$key]);
  254. foreach ($flag_arr as $k2 => $v2) {
  255. if ($v2 == "c") {
  256. array_push($tmp_key_arr, 'is_recom');
  257. } elseif ($v2 == "h") {
  258. array_push($tmp_key_arr, 'is_head');
  259. } elseif ($v2 == "a") {
  260. array_push($tmp_key_arr, 'is_special');
  261. } elseif ($v2 == "j") {
  262. array_push($tmp_key_arr, 'is_jump');
  263. }
  264. }
  265. $tmp_key_str = implode('|', $tmp_key_arr);
  266. $condition[$tmp_key_str] = array('eq', 1);
  267. } else if ('arcrank' == $key) {
  268. $condition[$key] = array('gt', $param[$key]);
  269. } else {
  270. $condition[$key] = array('eq', $param[$key]);
  271. }
  272. }
  273. }
  274. $paginate = array(
  275. 'page' => $page,
  276. );
  277. $pages = M('archives')->field($field)
  278. ->where($condition)
  279. ->where('channel != 6')
  280. ->order('sort_order asc, aid desc')
  281. ->cache(true,EYOUCMS_CACHE_TIME,"minipro")
  282. ->paginate($pagesize, false, $paginate);
  283. $list = array();
  284. foreach ($pages->items() as $key => $val) {
  285. /*封面图*/
  286. if (isset($val['litpic'])) {
  287. if (empty($val['litpic'])) {
  288. $val['is_litpic'] = 0; // 无封面图
  289. } else {
  290. $val['is_litpic'] = 1; // 有封面图
  291. }
  292. $val['litpic'] = get_default_pic($val['litpic'], true); // 默认封面图
  293. }
  294. /*--end*/
  295. if (isset($val['add_time'])) {
  296. $val['add_time'] = date('Y-m-d', $val['add_time']);
  297. }
  298. array_push($list, $val);
  299. }
  300. $result = array(
  301. 'conf' => array(
  302. 'hasMore' => ($page < $pages->lastPage()) ? 1 : 0,
  303. ),
  304. 'list' => $list,
  305. );
  306. cache($cacheKey, $result, null, 'minipro');
  307. }
  308. return $result;
  309. }
  310. /**
  311. * 文档详情
  312. * @param int $aid 文档ID
  313. */
  314. public function getArchivesView($aid = '')
  315. {
  316. $aid = intval($aid);
  317. $cacheKey = "model-".$this->nid."-getArchivesView-{$aid}";
  318. $result = cache($cacheKey);
  319. if (empty($result)) {
  320. $status = 0;
  321. $msg = 'Request Error!';
  322. $row = array();
  323. if (0 < $aid) {
  324. $archivesModel = new \app\home\model\Archives;
  325. $row = $archivesModel->getViewInfo($aid, true);
  326. $status = 1;
  327. if (0 > $row['status']) {
  328. $msg = '文档尚未审核,非作者本人无权查看';
  329. }
  330. /*--end*/
  331. $row['add_time'] = date('Y-m-d', $row['add_time']); // 格式化发布时间
  332. $row['update_time'] = date('Y-m-d', $row['update_time']); // 格式化更新时间
  333. $row['content'] = $this->get_httpimgurl($row['content']); // 转换内容图片为http路径
  334. /* 上一篇 */
  335. $preRow = M('archives')->field('a.aid, a.typeid, a.title')
  336. ->alias('a')
  337. ->where("a.typeid = ".$row['typeid']." AND a.aid < {$aid} AND a.status = 1")
  338. ->order('a.aid desc')
  339. ->find();
  340. /* 下一篇 */
  341. $nextRow = M('archives')->field('a.aid, a.typeid, a.title')
  342. ->alias('a')
  343. ->where("a.typeid = ".$row['typeid']." AND a.aid > {$aid} AND a.status = 1")
  344. ->order('a.aid asc')
  345. ->find();
  346. }
  347. $result = array(
  348. 'conf' => array(
  349. 'status' => $status,
  350. 'msg' => $msg,
  351. 'attrTitle' => '参数列表',
  352. 'contentTitle' => '详情介绍',
  353. 'shareTitle' => $row['title'].'_'.tpCache('web.web_name'),
  354. ),
  355. 'row' => $row,
  356. 'preRow' => $preRow,
  357. 'nextRow' => $nextRow,
  358. );
  359. cache($cacheKey, $result, null, 'minipro');
  360. }
  361. return $result;
  362. }
  363. /**
  364. * 单页栏目详情
  365. * @param int $typeid 栏目ID
  366. */
  367. public function getSingleView($typeid = '')
  368. {
  369. $typeid = intval($typeid);
  370. $cacheKey = "model-".$this->nid."-getSingleView-{$typeid}";
  371. $result = cache($cacheKey);
  372. if (empty($result)) {
  373. $status = 0;
  374. $msg = 'Request Error!';
  375. $row = array();
  376. if (0 < $typeid) {
  377. $archivesModel = new \app\home\model\Archives;
  378. $row = $archivesModel->getSingleInfo($typeid, true);
  379. $status = 1;
  380. if (0 == $row['status']) {
  381. $msg = '该文档已屏蔽,无权查看';
  382. }
  383. /*--end*/
  384. $row['add_time'] = date('Y-m-d', $row['add_time']); // 格式化发布时间
  385. $row['update_time'] = date('Y-m-d', $row['update_time']); // 格式化更新时间
  386. $row['content'] = $this->get_httpimgurl($row['content']); // 转换内容图片为http路径
  387. }
  388. $result = array(
  389. 'conf' => array(
  390. 'status' => $status,
  391. 'msg' => $msg,
  392. 'shareTitle' => $row['title'].'_'.tpCache('web.web_name'),
  393. ),
  394. 'row' => $row,
  395. );
  396. cache($cacheKey, $result, null, 'minipro');
  397. }
  398. return $result;
  399. }
  400. /**
  401. * 关于我们
  402. */
  403. public function getAbout()
  404. {
  405. $cacheKey = "model-".$this->nid."-getAbout";
  406. $result = cache($cacheKey);
  407. if (empty($result)) {
  408. $shareTitle = '';
  409. $row = $this->getValue('about');
  410. if ($row) {
  411. foreach ($row as $key => $val) {
  412. /*转换图片为远程http*/
  413. if (1 == preg_match('/^(logo|banner)$/', $key)) {
  414. if (!is_http_url($val)) {
  415. $row[$key] = request()->domain().$val;
  416. }
  417. }
  418. /*--end*/
  419. }
  420. $row['content'] = $this->get_httpimgurl($row['content']); // 转换内容图片为http路径
  421. $shareTitle = $row['webname'];
  422. }
  423. $result = array(
  424. 'conf' => array(
  425. 'shareTitle' => $shareTitle,
  426. ),
  427. 'row' => $row,
  428. );
  429. cache($cacheKey, $result, null, 'minipro');
  430. }
  431. return $result;
  432. }
  433. /**
  434. * 留言栏目表单
  435. * @param int $typeid 栏目ID
  436. */
  437. public function getGuestbookForm($typeid)
  438. {
  439. $typeid = intval($typeid);
  440. // $cacheKey = "model-".$this->nid."-getGuestbookForm-{$typeid}";
  441. // $result = cache($cacheKey);
  442. // if (empty($result)) {
  443. $list = array();
  444. $typename = '';
  445. if (0 < $typeid) {
  446. $typename = M('arctype')->where('id','eq',$typeid)->value('typename');
  447. $list = M('GuestbookAttribute')->field('attr_id,attr_name,attr_input_type,attr_values')
  448. ->where("typeid = $typeid")
  449. ->order('sort_order asc')
  450. ->cache(true,EYOUCMS_CACHE_TIME,"minipro")
  451. ->select();
  452. foreach ($list as $key => $val) {
  453. if (in_array($val['attr_input_type'], array(1,3))) {
  454. $val['attr_values'] = explode(PHP_EOL, $val['attr_values']);
  455. $list[$key] = $val;
  456. }
  457. }
  458. }
  459. $result = array(
  460. 'conf' => array(
  461. 'shareTitle' => $typename.'_'.tpCache('web.web_name'),
  462. ),
  463. 'row' => $list,
  464. );
  465. // cache($cacheKey, $result, null, 'minipro');
  466. // }
  467. return $result;
  468. }
  469. /**
  470. * 留言表单提交
  471. * @param array $post post数据
  472. */
  473. public function getGuestbookSubmit($post = array())
  474. {
  475. $typeid = !empty($post['typeid']) ? intval($post['typeid']) : 0;
  476. $status = 0;
  477. $msg = '表单typeid值丢失!';
  478. if (0 < $typeid) {
  479. $ip = clientIP();
  480. $map = array(
  481. 'ip' => $ip,
  482. 'typeid' => $typeid,
  483. 'add_time' => array('gt', getTime() - 60),
  484. );
  485. $count = M('guestbook')->where($map)->count('aid');
  486. if (!empty($count)) {
  487. return array(
  488. 'status' => 0,
  489. 'msg' => '同一个IP在60秒之内不能重复提交!',
  490. );
  491. }
  492. $channeltype_list = config('global.channeltype_list');
  493. $newData = array(
  494. 'typeid' => $typeid,
  495. 'channel' => $channeltype_list['guestbook'],
  496. 'ip' => $ip,
  497. 'lang' => get_current_lang(),
  498. 'add_time' => getTime(),
  499. 'update_time' => getTime(),
  500. );
  501. $aid = M('guestbook')->insertGetId($newData);
  502. if ($aid > 0) {
  503. $this->saveGuestbookAttr($post, $aid, $typeid);
  504. }
  505. $status = 1;
  506. $msg = '操作成功';
  507. }
  508. $result = array(
  509. 'gourl' => "",
  510. 'status' => $status,
  511. 'msg' => $msg,
  512. );
  513. return $result;
  514. }
  515. /**
  516. * 给指定留言添加表单值到 guestbook_attr
  517. * @param int $aid 留言id
  518. * @param int $typeid 留言栏目id
  519. */
  520. private function saveGuestbookAttr($post, $aid, $typeid)
  521. {
  522. // post 提交的属性 以 attr_id _ 和值的 组合为键名
  523. foreach($post as $k => $v)
  524. {
  525. $attr_id = str_replace('attr_','',$k);
  526. if(!strstr($k, 'attr_'))
  527. continue;
  528. //$v = str_replace('_', '', $v); // 替换特殊字符
  529. //$v = str_replace('@', '', $v); // 替换特殊字符
  530. if (is_array($v)) {
  531. $v = implode(',', $v);
  532. } else {
  533. $v = trim($v);
  534. }
  535. $adddata = array(
  536. 'aid' => $aid,
  537. 'attr_id' => $attr_id,
  538. 'attr_value' => $v,
  539. 'lang' => get_current_lang(),
  540. 'add_time' => getTime(),
  541. 'update_time' => getTime(),
  542. );
  543. M('GuestbookAttr')->add($adddata);
  544. }
  545. }
  546. /**
  547. * 图片地址替换成http路径
  548. * @param string $content 内容
  549. */
  550. private function get_httpimgurl($content = '')
  551. {
  552. $pregRule = "/<img(.*?)src(\s*)=(\s*)[\'|\"]\/(.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp|\.ico]))[\'|\"](.*?)[\/]?(\s*)>/i";
  553. $content = preg_replace($pregRule, '<img ${1} src="'.request()->domain().'/${4}" ${5} />', $content);
  554. return $content;
  555. }
  556. /**
  557. * 底部导航菜单
  558. */
  559. private function getBarlist() {
  560. // 定义公共常量
  561. $barlist = $this->getValue('global');
  562. foreach ($barlist as $key => $val) {
  563. /*转换图片为远程http*/
  564. if (1 == preg_match('/(_img|_selimg)$/', $key)) {
  565. if (!is_http_url($val)) {
  566. $barlist[$key] = request()->domain().$val;
  567. }
  568. }
  569. /*--end*/
  570. }
  571. return $barlist;
  572. }
  573. /**
  574. * 图片拼接成http路径
  575. * @param string $filename 路由地址
  576. * @param bool|string $domain 域名
  577. */
  578. public function getImgRealpath($filename, $domain = true)
  579. {
  580. $web_cmspath = tpCache('web.web_cmspath');
  581. $filename = $web_cmspath.'/'.WEAPP_DIR_NAME.'/'.$this->nid.'/template/skin/images/'.$filename;
  582. if ($domain) {
  583. $filename = request()->domain().$filename;
  584. }
  585. return $filename;
  586. }
  587. }