DataService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace service;
  14. use think\Db;
  15. use think\Request;
  16. /**
  17. * 基础数据服务
  18. * Class DataService
  19. * @package service
  20. * @author Anyon <zoujingli@qq.com>
  21. * @date 2017/03/22 15:32
  22. */
  23. class DataService
  24. {
  25. /**
  26. * 数据签名
  27. * @param array $data
  28. * @param string $apikey
  29. * @param string $prefix
  30. * @return string
  31. */
  32. public static function sign(&$data, $apikey = '', $prefix = '')
  33. {
  34. $data['_SIGNSTR_'] = strtoupper(isset($data['_SIGNSTR_']) ? $data['_SIGNSTR_'] : substr(md5(uniqid()), 22));
  35. ksort($data);
  36. foreach (array_values($data) as $string) {
  37. is_array($string) || ($prefix .= "{$string}");
  38. }
  39. return strtoupper(md5($prefix . $apikey . $data['_SIGNSTR_']));
  40. }
  41. /**
  42. * 删除指定序号
  43. * @param string $sequence
  44. * @param string $type
  45. * @return bool
  46. */
  47. public static function deleteSequence($sequence, $type = 'SYSTEM')
  48. {
  49. $data = ['sequence' => $sequence, 'type' => strtoupper($type)];
  50. return Db::name('SystemSequence')->where($data)->delete();
  51. }
  52. /**
  53. * 生成唯一序号 (失败返回 NULL )
  54. * @param int $length 序号长度
  55. * @param string $type 序号顾类型
  56. * @return string
  57. */
  58. public static function createSequence($length = 10, $type = 'SYSTEM')
  59. {
  60. $times = 0;
  61. while ($times++ < 10) {
  62. list($i, $sequence) = [0, ''];
  63. while ($i++ < $length) {
  64. $sequence .= ($i <= 1 ? rand(1, 9) : rand(0, 9));
  65. }
  66. $data = ['sequence' => $sequence, 'type' => strtoupper($type)];
  67. if (Db::name('SystemSequence')->where($data)->count() < 1) {
  68. if (Db::name('SystemSequence')->insert($data) !== false) {
  69. return $sequence;
  70. }
  71. }
  72. }
  73. return null;
  74. }
  75. /**
  76. * 数据增量保存
  77. * @param \think\db\Query|string $dbQuery 数据查询对象
  78. * @param array $data 需要保存或更新的数据
  79. * @param string $key 条件主键限制
  80. * @param array $where 其它的where条件
  81. * @return bool
  82. */
  83. public static function save($dbQuery, $data, $key = 'id', $where = [])
  84. {
  85. $db = is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery;
  86. $where[$key] = isset($data[$key]) ? $data[$key] : '';
  87. if ($db->where($where)->count() > 0) {
  88. return $db->where($where)->update($data) !== false;
  89. }
  90. return $db->insert($data) !== false;
  91. }
  92. /**
  93. * 更新数据表内容
  94. * @param \think\db\Query|string $dbQuery 数据查询对象
  95. * @param array $where 额外查询条件
  96. * @return bool|null
  97. */
  98. public static function update(&$dbQuery, $where = [])
  99. {
  100. $request = Request::instance();
  101. $db = is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery;
  102. $ids = explode(',', $request->post('id', ''));
  103. $field = $request->post('field', '');
  104. $value = $request->post('value', '');
  105. $pk = $db->getPk(['table' => $db->getTable()]);
  106. $where[empty($pk) ? 'id' : $pk] = ['in', $ids];
  107. // 删除模式,如果存在 is_deleted 字段使用软删除
  108. if ($field === 'delete') {
  109. if (method_exists($db, 'getTableFields')) {
  110. if (in_array('is_deleted', $db->getTableFields($db->getTable()))) {
  111. return false !== $db->where($where)->update(['is_deleted' => 1]);
  112. }
  113. }
  114. return false !== $db->where($where)->delete();
  115. }
  116. // 更新模式,更新指定字段内容
  117. return false !== $db->where($where)->update([$field => $value]);
  118. }
  119. }