PostsModel.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * PostModel.php UTF-8
  4. * 文章Model
  5. *
  6. * @date : 2017/11/23 21:27
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\posts;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\NewsConst;
  15. class PostsModel extends CommonModel {
  16. protected $name = 'posts';
  17. //可查询字段
  18. // protected $visible
  19. // = [
  20. // 'id', 'app_id', 'user_id', 'post_id', 'post_type', 'comment_status',
  21. // 'is_top', 'recommended', 'post_hits', 'post_like', 'comment_count',
  22. // 'create_time', 'update_time', 'published_time', 'post_title', 'post_keywords',
  23. // 'post_excerpt', 'post_source', 'post_content', 'more', 'user_nickname',
  24. // 'user', 'category_id'
  25. // ];
  26. //设置只读字段
  27. protected $readonly = ['user_id'];
  28. // 开启自动写入时间戳字段
  29. protected $autoWriteTimestamp = true;
  30. //类型转换
  31. protected $type
  32. = [
  33. 'more' => 'array',
  34. ];
  35. /**
  36. * 基础查询
  37. */
  38. protected function base($query) {
  39. $query->where('delete_time', 0)
  40. ->where('post_status', 2)
  41. ->whereTime('published_time', 'between', [1, time()]);
  42. }
  43. /**
  44. * 关联game表
  45. *
  46. * @return \think\model\relation\BelongsTo
  47. */
  48. public function game() {
  49. return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id,name');
  50. }
  51. /**
  52. * 关联user表
  53. *
  54. * @return \think\model\relation\HasOne
  55. */
  56. public function user() {
  57. return $this->hasOne('huo\model\user\UserModel', 'id', 'user_id', [], 'left')->setEagerlyType(0);
  58. }
  59. /**
  60. * post_content 自动转化
  61. *
  62. * @param $value
  63. *
  64. * @return string
  65. */
  66. public function getPostContentAttr($value) {
  67. return cmf_replace_content_file_url(htmlspecialchars_decode($value));
  68. }
  69. /**
  70. * post_content 自动转化
  71. *
  72. * @param $value
  73. *
  74. * @return string
  75. */
  76. public function getContentAttr($value) {
  77. return cmf_replace_content_file_url(htmlspecialchars_decode($value));
  78. }
  79. /**
  80. * post_content 自动转化
  81. *
  82. * @param $value
  83. *
  84. * @return string
  85. */
  86. public function setPostContentAttr($value) {
  87. return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
  88. }
  89. /**
  90. * @param $value
  91. *
  92. * @return array|bool|mixed
  93. */
  94. public function getTypeAttr($value) {
  95. $_rs = NewsConst::getNewsMsg($value);
  96. if (false == $_rs) {
  97. return $value;
  98. }
  99. return $_rs;
  100. }
  101. /**
  102. * more 自动转化
  103. *
  104. * @param $value
  105. *
  106. * @return array
  107. */
  108. public function getMoreAttr($value) {
  109. $more = json_decode($value, true);
  110. if (!empty($more['thumbnail'])) {
  111. $more['thumbnail'] = cmf_get_image_url($more['thumbnail']);
  112. }
  113. if (!empty($more['photos'])) {
  114. foreach ($more['photos'] as $key => $value) {
  115. $more['photos'][$key]['url'] = cmf_get_image_url($value['url']);
  116. }
  117. }
  118. if (!empty($more['files'])) {
  119. foreach ($more['files'] as $key => $value) {
  120. $more['files'][$key]['url'] = cmf_get_image_url($value['url']);
  121. }
  122. }
  123. //积分抽奖
  124. if (!empty($more['turntable'])) {
  125. $more['turntable'] = cmf_get_image_url($more['turntable']);
  126. }
  127. if (!empty($more['pointer'])) {
  128. $more['pointer'] = cmf_get_image_url($more['pointer']);
  129. }
  130. return $more;
  131. }
  132. /**
  133. * published_time 自动完成
  134. *
  135. * @param $value
  136. *
  137. * @return false|int
  138. */
  139. public function setPublishedTimeAttr($value) {
  140. return strtotime($value);
  141. }
  142. /**
  143. * published_time 自动完成
  144. *
  145. * @param $value
  146. *
  147. * @return false|int
  148. */
  149. public function setStartTimeAttr($value) {
  150. return strtotime($value);
  151. }
  152. /**
  153. * published_time 自动完成
  154. *
  155. * @param $value
  156. *
  157. * @return false|int
  158. */
  159. public function setEndTimeAttr($value) {
  160. return strtotime($value);
  161. }
  162. /**
  163. * 后台管理添加页面
  164. *
  165. * @param array $data 页面数据
  166. *
  167. * @return false|int
  168. */
  169. public function adminAddPage($data) {
  170. $data['user_id'] = cmf_get_current_admin_id();
  171. $data['post_status'] = empty($data['post_status']) ? 1 : 2;
  172. $_rs = $this->allowField(true)->data($data, true)->save();
  173. return $_rs;
  174. }
  175. /**
  176. * 后台管理编辑页面
  177. *
  178. * @param array $data 页面数据
  179. *
  180. * @return false|int
  181. */
  182. public function adminEditPage($data) {
  183. $data['user_id'] = cmf_get_current_admin_id();
  184. $data['post_status'] = empty($data['post_status']) ? 1 : 2;
  185. $data['update_time'] = time();
  186. $_rs = $this->allowField(true)->isUpdate(true)->data($data, true)->save();
  187. return $_rs;
  188. }
  189. /**
  190. * 删除文章
  191. *
  192. * @param $data
  193. *
  194. * @return bool
  195. */
  196. public function adminDeletePage($data) {
  197. if (isset($data['id'])) {
  198. $id = $data['id']; //获取删除id
  199. $_rs = $this->useGlobalScope(false)->where(['id' => $id])->update(
  200. [
  201. 'delete_time' => time()
  202. ]
  203. );
  204. } elseif (isset($data['ids'])) {
  205. $ids = $data['ids'];
  206. $_rs = $this->useGlobalScope(false)->where(['id' => ['in', $ids]])
  207. ->update(
  208. [
  209. 'delete_time' => time()
  210. ]
  211. );
  212. } else {
  213. return false;
  214. }
  215. return $_rs;
  216. }
  217. /**
  218. * 根据条件获取idName
  219. *
  220. * @param array $_map
  221. *
  222. * @return array
  223. */
  224. public function getIdName($_map = []) {
  225. $_rs = $this->where($_map)->column('post_title', 'id');
  226. return $_rs;
  227. }
  228. }