SlideItemModel.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 文件说明:用户-幻灯片
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: wuwu <15093565100@163.com>
  8. // +----------------------------------------------------------------------
  9. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | Date: 2017-5-25
  12. // +----------------------------------------------------------------------
  13. namespace huo\model\slide;
  14. use huo\controller\game\GameCache;
  15. use huo\model\common\CommonModel;
  16. use huolib\constant\CacheConst;
  17. use huolib\constant\GameConst;
  18. use huolib\constant\SlideConst;
  19. use huomp\model\game\GameMiniModel;
  20. use think\Cache;
  21. class SlideItemModel extends CommonModel {
  22. CONST STATUS_SHOW = '显示'; // 2显示,
  23. CONST STATUS_HIDE = '隐藏'; // 1不显示
  24. // /**
  25. // * @param $query
  26. // */
  27. // protected function base($query) {
  28. // $query->where('status', 2);
  29. // }
  30. public function slide() {
  31. return $this->belongsTo(SlideModel::className(), 'slide_id', 'id', [], 'left')
  32. ->setEagerlyType(0);
  33. }
  34. public function getStatusLabelAttr() {
  35. if (SlideConst::SLIDE_STATUS_SHOW == $this->status) {
  36. return self::STATUS_SHOW;
  37. }
  38. return self::STATUS_HIDE;
  39. }
  40. /**
  41. * image 自动转化
  42. *
  43. *
  44. * @param $value
  45. *
  46. * @return array
  47. */
  48. public function getImageAttr($value) {
  49. if (!empty($value)) {
  50. return cmf_get_image_url($value);
  51. }
  52. return $value;
  53. }
  54. /**
  55. * image 自动转化
  56. *
  57. *
  58. * @param $value
  59. *
  60. * @return array
  61. */
  62. public function setImageAttr($value) {
  63. if (!empty($value)) {
  64. return str_replace(STATICSITE.'/upload/', '', $value);
  65. }
  66. return $value;
  67. }
  68. public function addData($data) {
  69. if (empty($data)) {
  70. return false;
  71. }
  72. if ($_obj = self::create($data, true)) {
  73. return $_obj->id;
  74. } else {
  75. return false;
  76. }
  77. }
  78. /**
  79. * 获取广告列表
  80. *
  81. * @param array $map
  82. * @param string $page
  83. *
  84. * @param string $order
  85. *
  86. * @return array
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @throws \think\exception\DbException
  90. */
  91. public function getList($map = [], $page = '1,10', $order = '+list_order') {
  92. $_redata = ['count' => 0, 'list' => []];
  93. $_count = self::where($map)->count('id');
  94. if (empty($_count)) {
  95. return $_redata;
  96. }
  97. $_order = self::orderFilter($order);
  98. $_list = self::with('slide')->where($map)->order($_order)->page($page)->select();
  99. if (is_object($_list)) {
  100. $_list = $_list->toArray();
  101. }
  102. foreach ($_list as $_k => $_v) {
  103. $_list[$_k]['status_label'] = SlideConst::SLIDE_STATUS_SHOW == $_v['status'] ? self::STATUS_SHOW
  104. : self::STATUS_HIDE;
  105. }
  106. $_redata['count'] = $_count;
  107. $_redata['list'] = $_list;
  108. return $_redata;
  109. }
  110. /**
  111. * 删除
  112. *
  113. * @param $id
  114. *
  115. * @return int|mixed
  116. */
  117. public function deleteItem($id) {
  118. $_data = parent::getInfoById($id);
  119. $_rs = self::where(['id' => $id])->delete();
  120. if ($_rs != false && !empty($id)) {
  121. if (!empty($_data['slide_id'])) {
  122. $this->ClCacheBySlideId($_data['slide_id']);
  123. }
  124. }
  125. return $_rs;
  126. }
  127. /**
  128. * 更新
  129. *
  130. * @param array $map
  131. * @param array $data
  132. *
  133. * @return SlideItemModel
  134. */
  135. public function updateData($map = [], $data = []) {
  136. $_rs = self::where($map)->update($data);
  137. if ($_rs != false && !empty($map['id'])) {
  138. $_data = parent::getInfoById($map['id']);
  139. if (!empty($_data['slide_id'])) {
  140. $this->ClCacheBySlideId($_data['slide_id']);
  141. }
  142. }
  143. return $_rs;
  144. }
  145. /**
  146. *
  147. * @param $code
  148. *
  149. * @return array
  150. */
  151. public function getAdsByCode($code) {
  152. $_cache_key = CacheConst::CACHE_SLIDE_CODE_PREFIX.$code;
  153. $_rdata = json_decode(Cache::get($_cache_key), true);
  154. if (!empty($_rdata)) {
  155. return $_rdata;
  156. }
  157. $_slide_id = (new SlideModel())->getSlideIdByCode($code);
  158. $_datas = $this
  159. ->where('table_name', '=', GameConst::GAME_MP_GAME)
  160. ->where('slide_id', '=', $_slide_id)
  161. ->where('status', '=', SlideConst::SLIDE_STATUS_SHOW)
  162. ->order('list_order DESC')
  163. ->select();
  164. if (is_object($_datas)) {
  165. $_datas = $_datas->toArray();
  166. }
  167. $_app_cache = new GameCache();
  168. $_game_mini_logic = new GameMiniModel();
  169. $_rdata = [];
  170. foreach ($_datas as $_k => $_v) {
  171. $_app_info = $_app_cache->getInfoByAppId($_v['target_id']);
  172. if (empty($_app_info)) {
  173. continue;
  174. }
  175. if ($_app_info['classify'] != GameConst::GAME_MP) {
  176. continue;
  177. }
  178. $_game_mini_info = $_game_mini_logic->getDataByAppId($_app_info['id']);
  179. if (empty($_game_mini_info)) {
  180. continue;
  181. }
  182. $_rdata[] = [
  183. 'game_id' => $_app_info['id'],
  184. 'mini_app_id' => $_game_mini_info['mini_app_id'],
  185. 'need_popup' => $_game_mini_info['need_popup'],
  186. 'entrance_image' => cmf_get_image_preview_url($_game_mini_info['entrance_image']),
  187. 'gamename' => !empty($_app_info) ? $_app_info['name'] : '',
  188. 'oneword' => !empty($_app_info) ? $_app_info['publicity'] : '',
  189. 'image' => $_v['image'],
  190. ];
  191. }
  192. if (!empty($_rdata)) {
  193. Cache::set($_cache_key, json_encode($_rdata));
  194. }
  195. return $_rdata;
  196. }
  197. public function getAdByCode($code) {
  198. $_cache_key = CacheConst::CACHE_SLIDE_CODE_PREFIX.$code;
  199. $_rdata = json_decode(Cache::get($_cache_key), true);
  200. if (!empty($_rdata)) {
  201. return $_rdata;
  202. }
  203. $_slide_id = (new SlideModel())->getSlideIdByCode($code);
  204. $_datas = $this
  205. ->where('table_name', '=', GameConst::GAME_MP_GAME)
  206. ->where('slide_id', '=', $_slide_id)
  207. ->where('status', '=', SlideConst::SLIDE_STATUS_SHOW)
  208. ->order('list_order DESC')
  209. ->select();
  210. if (is_object($_datas)) {
  211. $_datas = $_datas->toArray();
  212. }
  213. $_app_cache = new GameCache();
  214. $_game_mini_logic = new GameMiniModel();
  215. $_rdata = [];
  216. foreach ($_datas as $_k => $_v) {
  217. $_app_info = $_app_cache->getInfoByAppId($_v['target_id']);
  218. $_game_mini_info = $_game_mini_logic->getDataByAppId($_app_info['id']);
  219. $_rdata[] = [
  220. 'game_id' => empty($_app_info['id']) ? 0 : $_app_info['id'],
  221. 'mini_app_id' => empty($_game_mini_info['mini_app_id']) ? '' : $_game_mini_info['mini_app_id'],
  222. 'image' => $_v['image'],
  223. ];
  224. }
  225. if (!empty($_rdata)) {
  226. Cache::set($_cache_key, json_encode($_rdata));
  227. }
  228. return $_rdata;
  229. }
  230. /**
  231. * 根据slide_id 清理缓存
  232. *
  233. * @param $slide_id
  234. *
  235. * @return bool
  236. */
  237. public function ClCacheBySlideId($slide_id) {
  238. $_code = (new SlideModel())->getCodeById($slide_id);
  239. if (empty($_code)) {
  240. return false;
  241. }
  242. return $this->ClCacheByCode($_code);
  243. }
  244. /**
  245. * 根据code 清理缓存
  246. *
  247. * @param $code
  248. *
  249. * @return bool
  250. */
  251. public function ClCacheByCode($code) {
  252. if (empty($code)) {
  253. return false;
  254. }
  255. $_cache_key = CacheConst::CACHE_SLIDE_CODE_PREFIX.$code;
  256. Cache::rm($_cache_key);
  257. return true;
  258. }
  259. }