IdentifyInQueueModel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * IdentifyInQueueModel.php UTF-8
  4. * 实名认证队列中信息
  5. *
  6. * @date : 2021/4/28 14:33
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK-IDENTITY 1.0
  11. */
  12. namespace huoIdentify\model;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use think\Cache;
  16. class IdentifyInQueueModel extends CommonModel {
  17. protected $name = 'identify_in_queue';
  18. protected $pk = 'id';
  19. /* 开启自动写入时间戳字段 */
  20. protected $autoWriteTimestamp = true;
  21. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_IN_QUEUE_PREFIX;
  22. protected $type
  23. = [
  24. 'id' => 'integer',
  25. 'uid' => 'string',
  26. 'channel_code' => 'string',
  27. 'real_name' => 'string',
  28. 'id_card' => 'string',
  29. 'identify_pi' => 'string',
  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. /**
  44. * 获取单条记录缓存key
  45. *
  46. * @param $uid
  47. * @param $channel_code
  48. * @param $id_card
  49. *
  50. * @return string :string
  51. */
  52. protected function getCacheKeyByUidCodeCard($uid, $channel_code, $id_card) {
  53. $uid = (string)$uid;
  54. $channel_code = (string)$channel_code;
  55. $id_card = (string)$id_card;
  56. $_array = [$uid, $channel_code, $id_card];
  57. return $this->cache_key_prefix.'unc_'.md5(json_encode($_array));
  58. }
  59. /**
  60. * 加入数据库
  61. *
  62. * @param array $data
  63. *
  64. * @return bool|int
  65. */
  66. public function addData($data) {
  67. $_data = $data;
  68. $_rs = parent::addData($_data);
  69. if (empty($_rs)) {
  70. return false;
  71. }
  72. $_cache_key = $this->getCacheKeyByUidCodeCard(
  73. $_data['uid'], $_data['channel_code'], $_data['id_card']
  74. );
  75. Cache::set($_cache_key, $_rs);
  76. return $_rs;
  77. }
  78. /**
  79. * 通过ID获取信息
  80. *
  81. * @param int $id 主键ID
  82. *
  83. * @return array
  84. */
  85. public function getInfoById($id) {
  86. /* 缓存操作 */
  87. $_single_cache_key = $this->getSingleCacheKey($id);
  88. $_data = Cache::get($_single_cache_key);
  89. if (!empty($_data)) {
  90. return $_data;
  91. }
  92. $_data = parent::getInfoById($id);
  93. if (empty($_data)) {
  94. return [];
  95. }
  96. Cache::set($_single_cache_key, $_data);
  97. return $_data;
  98. }
  99. /**
  100. * 更新单条数据
  101. *
  102. * @param array $data 数据
  103. * @param int $id ID
  104. *
  105. * @return bool
  106. */
  107. public function updateData($data, $id) {
  108. $_data = $data;
  109. $_rs = parent::updateData($_data, $id);
  110. if (false === $_rs) {
  111. return false;
  112. }
  113. /* 缓存操作 */
  114. $_single_cache_key = $this->getSingleCacheKey($id);
  115. Cache::rm($_single_cache_key);
  116. return true;
  117. }
  118. /**
  119. * 删除单条数据
  120. *
  121. * @param $id
  122. * @param bool $is_complete
  123. *
  124. * @return bool|int
  125. */
  126. public function deleteData($id, $is_complete = true) {
  127. $_old_data = $this->getInfoById($id);
  128. $_rs = parent::deleteData($id, $is_complete);
  129. if (false !== $_rs) {
  130. $_cache_key = $this->getSingleCacheKey($id);
  131. Cache::rm($_cache_key);
  132. $_cache_key = $this->getCacheKeyByUidCodeCard(
  133. $_old_data['uid'], $_old_data['channel_code'], $_old_data['id_card']
  134. );
  135. Cache::set($_cache_key, 0);
  136. }
  137. return $_rs;
  138. }
  139. /**
  140. * 根据uid real_name id_card 获取id
  141. *
  142. * @param $uid
  143. * @param $channel_code
  144. * @param $id_card
  145. *
  146. * @return int|mixed
  147. */
  148. public function getIdByUidCodeCard($uid, $channel_code, $id_card) {
  149. $_cache_key = $this->getCacheKeyByUidCodeCard($uid, $channel_code, $id_card);
  150. if (Cache::has($_cache_key)) {
  151. return Cache::get($_cache_key);
  152. }
  153. $_map = [
  154. 'uid' => $uid,
  155. 'channel_code' => $channel_code,
  156. 'id_card' => $id_card,
  157. ];
  158. $_id = $this->where($_map)->value($this->pk);
  159. if (empty($_id)) {
  160. $_id = 0;
  161. }
  162. Cache::set($_cache_key, $_id);
  163. return $_id;
  164. }
  165. }