IdentifyGameModel.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * IdentifyGameModel.php UTF-8
  4. *
  5. *
  6. * @date : 2021-03-11 15:25
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK 9.0
  11. */
  12. namespace huoIdentify\model;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use think\Cache;
  16. class IdentifyGameModel extends CommonModel {
  17. protected $name = 'identify_game';
  18. protected $pk = 'id';
  19. /* 开启自动写入时间戳字段 */
  20. protected $autoWriteTimestamp = true;
  21. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_GAME_PREFIX;
  22. protected $type
  23. = [
  24. 'id' => 'integer',
  25. 'app_id' => 'integer',
  26. 'app_key' => 'string',
  27. 'app_secret' => 'string',
  28. 'name' => 'string',
  29. 'ext_info' => 'array',
  30. 'create_time' => 'timestamp',
  31. 'update_time' => 'timestamp',
  32. ];
  33. /**
  34. * 获取单条记录缓存key
  35. *
  36. * @param int $id ID
  37. *
  38. * @return string :string
  39. */
  40. protected function getSingleCacheKey($id) {
  41. return $this->cache_key_prefix.$id;
  42. }
  43. public function getCacheKeyByAppId($app_id) {
  44. return $this->cache_key_prefix.'_ai_'.$app_id;
  45. }
  46. /**
  47. * 通过ID获取信息
  48. *
  49. * @param int $id 主键ID
  50. *
  51. * @return array
  52. */
  53. public function getInfoById($id) {
  54. $_data = parent::getInfoById($id);
  55. return $_data;
  56. }
  57. /**
  58. * 加入数据库
  59. *
  60. * @param array $data
  61. *
  62. * @return bool|int
  63. */
  64. public function addData($data) {
  65. $_data = $data;
  66. $_rs = parent::addData($_data);
  67. if (empty($_rs)) {
  68. return false;
  69. }
  70. return $_rs;
  71. }
  72. /**
  73. * 更新单条数据
  74. *
  75. * @param array $data 数据
  76. * @param int $id ID
  77. *
  78. * @return bool
  79. */
  80. public function updateData($data, $id) {
  81. $_data = $data;
  82. $_old_data = $this->getInfoById($id);
  83. $_ext_info = get_val($_old_data, 'ext_info', []);
  84. $_ext_info = array_merge($_ext_info, $_data['ext_info'] ?? []);
  85. $_data = array_merge($_old_data, $_data);
  86. $_data['ext_info'] = $_ext_info;
  87. $_rs = parent::updateData($_data, $id);
  88. if (false === $_rs) {
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * 删除单条数据
  95. *
  96. * @param $id
  97. * @param bool $is_complete
  98. *
  99. * @return bool|int
  100. */
  101. public function deleteData($id, $is_complete = true) {
  102. $_old_data = $this->getInfoById($id);
  103. $_rs = parent::deleteData($id, $is_complete);
  104. if (false !== $_rs) {
  105. $_cache_key = $this->getSingleCacheKey($id);
  106. Cache::rm($_cache_key);
  107. $_cache_key = $this->getCacheKeyByAppId($_old_data['app_id']);
  108. Cache::rm($_cache_key);
  109. }
  110. return $_rs;
  111. }
  112. /**
  113. * 通过app_id获取ID
  114. *
  115. * @param string $app_id app_id
  116. *
  117. * @return int
  118. */
  119. public function getIdByAppId($app_id) {
  120. $_cache_key = $this->getCacheKeyByAppId($app_id);
  121. $_id = Cache::get($_cache_key);
  122. if (!empty($_id)) {
  123. return $_id;
  124. }
  125. /* 取默认 */
  126. $_map = [
  127. 'app_id' => $app_id
  128. ];
  129. $_id = $this->where($_map)->value('id');
  130. if (empty($_id)) {
  131. $_id = 0;
  132. }
  133. Cache::set($_cache_key, $_id);
  134. return $_id;
  135. }
  136. public function getInfoByAppId($app_id) {
  137. $_id = $this->getIdByAppId($app_id);
  138. if (empty($_id)) {
  139. return [];
  140. }
  141. return $this->getInfoById($_id);
  142. }
  143. /**
  144. * 通过ID获取app_key
  145. *
  146. * @param int $id ID
  147. *
  148. * @return string
  149. */
  150. public function getAppKeyById($id) {
  151. $_data = $this->getInfoById($id);
  152. return get_val($_data, 'app_key', '');
  153. }
  154. /**
  155. * 通过ID获取name
  156. *
  157. * @param int $id ID
  158. *
  159. * @return string
  160. */
  161. public function getNameById($id) {
  162. $_data = $this->getInfoById($id);
  163. return get_val($_data, 'name', '');
  164. }
  165. /**
  166. * 通过ID获取app_secret
  167. *
  168. * @param int $id ID
  169. *
  170. * @return string
  171. */
  172. public function getAppSecretById($id) {
  173. $_data = $this->getInfoById($id);
  174. return get_val($_data, 'app_secret', '');
  175. }
  176. /**
  177. * 通过ID获取app_id
  178. *
  179. * @param int $id ID
  180. *
  181. * @return string
  182. */
  183. public function getAppIdById($id) {
  184. $_data = $this->getInfoById($id);
  185. return get_val($_data, 'app_id', 0);
  186. }
  187. /**
  188. * 关联应用
  189. */
  190. public function game() {
  191. $_field = [
  192. 'id',
  193. 'apple_id',
  194. 'name',
  195. 'icon',
  196. 'classify'
  197. ];
  198. return $this->belongsTo('\huoIdentify\model\GameModel', 'app_id', 'id')->field($_field);
  199. }
  200. public function getIdsByWhere($where, $column = 'app_id') {
  201. return $this->where($where)->column($column);
  202. }
  203. public function getList($where = [], $field = [], $order = 'id desc') {
  204. $_list = $this->where($where)->field($field)->order($order)->select();
  205. if (is_object($_list)) {
  206. $_list = $_list->toArray();
  207. }
  208. return $_list;
  209. }
  210. }