Cert.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2023/8/23
  6. * @time: 17:04
  7. */
  8. namespace app\models\user;
  9. use app\admin\model\system\SystemAttachment;
  10. use app\admin\model\user\CertType;
  11. use crmeb\basic\BaseModel;
  12. use crmeb\services\FormBuilder;
  13. use crmeb\services\UtilService;
  14. use crmeb\traits\ModelTrait;
  15. use think\facade\Route;
  16. class Cert extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'cert';
  28. public static $level = [
  29. 0 => '无',
  30. 1 => 'I级',
  31. 2 => 'II级',
  32. 3 => 'III级',
  33. 4 => '特级',
  34. ];
  35. public static $gender = [
  36. 0 => '男',
  37. 1 => '女',
  38. ];
  39. protected $append = [
  40. 'level_name',
  41. 'gender_name',
  42. ];
  43. use ModelTrait;
  44. public function getLevelNameAttr($value, $data)
  45. {
  46. return self::$level[$data['level'] ?: 0];
  47. }
  48. public function getAddTimeAttr($value)
  49. {
  50. return date('Y-m-d H:i:s', $value);
  51. }
  52. public function getGenderNameAttr($value, $data)
  53. {
  54. return self::$gender[$data['gender'] ?: 0];
  55. }
  56. public static function getList($where)
  57. {
  58. if (isset($where['status']) && $where['status'] != '') {
  59. $model = self::where('status', $where['status']);
  60. } else {
  61. $model = new self();
  62. }
  63. if (isset($where['uid']) && $where['uid'] != '') {
  64. $model = $model->where('uid', $where['uid']);
  65. }
  66. $count = $model->count();
  67. $data = $model->page((int)$where['page'], (int)$where['limit'])->order('id desc')->select();
  68. return compact('count', 'data');
  69. }
  70. public static function add_cert($uid, $name, $gender, $idcard, $job, $photo, $level = '')
  71. {
  72. // if (self::be(['idcard' => $idcard, 'status' => [0, 1]])) {
  73. // return self::setErrorInfo('已有身份证号码相同的记录存在');
  74. // }
  75. $add_time = time();
  76. return self::create(compact('uid', 'name', 'gender', 'idcard', 'job', 'photo', 'level', 'add_time'));
  77. }
  78. public static function search($name)
  79. {
  80. return self::where('name|iscard|code', 'like', "%$name%")->where('status', 1)->find();
  81. }
  82. public static function check_cert($id, $data)
  83. {
  84. $info = self::get($id);
  85. // if ($info['status'] != 0) return self::setErrorInfo('记录已处理');
  86. if (!$info['code'] && $data['status'] == 1) {
  87. $date = date('Y-m-d');
  88. $code = self::createNewCode($data);
  89. $data['date'] = $date;
  90. $data['code'] = $code;
  91. if (!$info['qrcode']) {
  92. $name = 'cert_' . $info['uid'] . '_' . $code . '.jpg';
  93. $imageInfo = SystemAttachment::getInfo($name, 'name');
  94. if (!$imageInfo) {
  95. $codeUrl = UtilService::setHttpType(sys_config('site_url') . '/index/pages/certificate/showCertificate?id=' . $id, 1);//二维码链接
  96. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  97. if (!$imageInfo) return self::setErrorInfo('二维码生成失败');
  98. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  99. $url = $imageInfo['dir'];
  100. } else $url = $imageInfo['att_dir'];
  101. $data['qrcode'] = $url;
  102. if ($imageInfo['image_type'] == 1) $data['qrcode'] = sys_config('site_url') . $url;
  103. }
  104. $res = self::where('id', $id)->update($data);
  105. } else {
  106. $res = self::where('id', $id)->update($data);
  107. }
  108. return $res;
  109. }
  110. public static function createNewCode($info)
  111. {
  112. $str = CertType::where('cert_name', $info['job'])->value('cert_code');
  113. if (!$str) $str = 'UK';
  114. $str .= ($info['level'] == 4 ? '特' : ('0' . $info['level']));
  115. $str .= '-021-' . substr($info['idcard'], strlen($info['idcard']) - 4, 4);
  116. $str .= '-' . str_pad((self::where('status', 1)->count() + 1), 4, 0, STR_PAD_LEFT);
  117. // do {
  118. // $str = date('ymd') . strtoupper(substr(md5($id . rand(100000, 999999)), 3, 8));
  119. // } while (self::be(['code' => $str]));
  120. return $str;
  121. }
  122. }