GameMiniLogic.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * GameMiniLogic.php UTF-8
  4. * 小程序逻辑
  5. *
  6. * @date : 2018/8/9 17:25
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\logic\game;
  13. use huolib\constant\CacheConst;
  14. use huolib\constant\CommonConst;
  15. use huomp\model\common\CommonModel;
  16. use think\Cache;
  17. class GameMiniLogic extends CommonModel {
  18. protected $name = 'game_mini';
  19. // 开启自动写入时间戳字段
  20. protected $autoWriteTimestamp = true;
  21. protected $cache_tag = CacheConst::TAG_CACHE_GAME_MP;
  22. protected $cache_key_prefix = CacheConst::CACHE_GAME_MP_PREFIX;
  23. /**
  24. * 关联game表
  25. *
  26. * @return mixed
  27. */
  28. public function game() {
  29. return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id,name');
  30. }
  31. /**
  32. * 添加数据
  33. *
  34. * @param $data
  35. *
  36. * @return bool
  37. */
  38. public function addData($data) {
  39. if (empty($data) || empty($data['app_id'])) {
  40. return false;
  41. }
  42. $_data = $data;
  43. $_obj = self::create($_data, true);
  44. if ($_obj) {
  45. return true;
  46. }
  47. return false;
  48. }
  49. /**
  50. * 更新数据
  51. *
  52. * @param array $data 数据
  53. * @param int $id 应用ID
  54. *
  55. * @return bool
  56. */
  57. public function updateData($data, $id) {
  58. $_map['app_id'] = $id;
  59. $_data = $data;
  60. $_rs = self::update($_data, $_map, true);
  61. if (false == $_rs) {
  62. return false;
  63. }
  64. $_cache_key = $this->cache_key_prefix.$_data['app_id'];
  65. Cache::tag($this->cache_tag)->rm($_cache_key);
  66. return true;
  67. }
  68. /**
  69. * 获取小程序信息
  70. *
  71. * @param int $app_id 应用ID
  72. *
  73. * @return array|bool|false
  74. */
  75. public function getDataByAppId($app_id) {
  76. if (empty($app_id)) {
  77. return false;
  78. }
  79. $_cache_key = $this->cache_key_prefix.$app_id;
  80. $_data = Cache::tag($this->cache_tag)->get($_cache_key);
  81. if (!empty($_data)) {
  82. return $_data;
  83. }
  84. $_data = $this->where('app_id', $app_id)->find();
  85. if (is_object($_data)) {
  86. $_data = $_data->toArray();
  87. }
  88. Cache::tag($this->cache_tag)->set($_cache_key, $_data, CommonConst::CONST_DAY_SECONDS);
  89. return $_data;
  90. }
  91. }