CommonModel.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * CommonModel.php UTF-8
  4. *
  5. *
  6. * @date : 2020/9/18 21:31
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK-CPL 1.0
  11. */
  12. namespace huomp\model\weixin;
  13. use huomp\model\common\NewCommonModel;
  14. use think\Cache;
  15. use think\Loader;
  16. class CommonModel extends NewCommonModel {
  17. protected $name = '';
  18. protected $pk = 'id';
  19. /* 开启自动写入时间戳字段 */
  20. protected $autoWriteTimestamp = true;
  21. protected $cache_key_prefix = '';
  22. protected $type
  23. = [
  24. ];
  25. public function getFieldType() {
  26. return $this->type;
  27. }
  28. public function __call($method, $args) {
  29. if (true == $this->isGetFunc($method)) {
  30. /* 获取字段 */
  31. $_id = $args[0];
  32. if (empty($_id)) {
  33. return null;
  34. }
  35. $_name = substr(strstr($method, 'ById', true), 3);
  36. $_field = Loader::parseName($_name);
  37. $this->data = $this->getInfoById($_id);
  38. return $this->getAttr($_field);
  39. } elseif (true == $this->isUpdateFunc($method)) {
  40. /* 更新字段 */
  41. $_id = $args[1];
  42. if (empty($_id)) {
  43. return false;
  44. }
  45. $_name = substr(strstr($method, 'ById', true), 6);
  46. $_field = Loader::parseName($_name);
  47. $_data = $this->getInfoById($_id);
  48. if (empty($_data)) {
  49. return false;
  50. }
  51. $_update_data[$_field] = $args[0];
  52. return $this->updateData($_update_data, $_id);
  53. }
  54. return parent::__call($method, $args);
  55. }
  56. private function isGetFunc($method) {
  57. $_rs = substr_compare($method, 'ById', -strlen('ById'));
  58. $_start_str = substr($method, 0, strlen('get'));
  59. if (0 === $_rs && 'get' == $_start_str) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. private function isUpdateFunc($method) {
  65. $_rs = substr_compare($method, 'ById', -strlen('ById'));
  66. $_start_str = substr($method, 0, strlen('update'));
  67. if (0 === $_rs && 'update' == $_start_str) {
  68. return true;
  69. }
  70. return false;
  71. }
  72. /**
  73. * 获取单条记录缓存key
  74. *
  75. * @param int $id ID
  76. *
  77. * @return string
  78. */
  79. protected function getSingleCacheKey($id) {
  80. return $this->cache_key_prefix.$id;
  81. }
  82. /**
  83. * 添加数据
  84. *
  85. * @param array $data 需要添加的数据
  86. *
  87. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  88. */
  89. public function addData($data) {
  90. $_data = $data;
  91. $_id = parent::addData($_data);
  92. if (false === $_id) {
  93. return false;
  94. }
  95. /* TAG缓存操作 */
  96. return $_id;
  97. }
  98. /**
  99. * 通过ID获取信息
  100. *
  101. * @param int $id 主键ID
  102. *
  103. * @return array|false
  104. */
  105. public function getInfoById($id) {
  106. /* 缓存操作 */
  107. $_single_cache_key = $this->getSingleCacheKey($id);
  108. $_data = Cache::get($_single_cache_key);
  109. if (!empty($_data)) {
  110. return $_data;
  111. }
  112. $_data = parent::getInfoById($id);
  113. if (empty($_data)) {
  114. return [];
  115. }
  116. Cache::set($_single_cache_key, $_data);
  117. return $_data;
  118. }
  119. /**
  120. * 更新单条数据
  121. *
  122. * @param array $data 数据
  123. * @param int $id ID
  124. *
  125. * @return bool
  126. */
  127. public function updateData($data, $id) {
  128. $_data = $data;
  129. $_rs = parent::updateData($_data, $id);
  130. if (false === $_rs) {
  131. return false;
  132. }
  133. /* 缓存操作 */
  134. $_single_cache_key = $this->getSingleCacheKey($id);
  135. Cache::rm($_single_cache_key);
  136. /* TAG缓存操作 */
  137. return true;
  138. }
  139. /**
  140. * 删除单条数据
  141. *
  142. * @param int $id ID
  143. * @param bool $is_complete 是否完成删除
  144. *
  145. * @return bool
  146. */
  147. public function deleteData($id, $is_complete = true) {
  148. $_rs = parent::deleteData($id, $is_complete);
  149. if (false == $_rs) {
  150. return false;
  151. }
  152. /* 缓存操作 */
  153. $_single_cache_key = $this->getSingleCacheKey($id);
  154. Cache::rm($_single_cache_key);
  155. /* TAG缓存操作 */
  156. return $_rs;
  157. }
  158. }