Coach.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class Coach extends Model
  5. {
  6. // 表名
  7. protected $table = 'coach';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'integer';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'gender_text',
  17. 'prevtime_text',
  18. 'logintime_text',
  19. 'jointime_text',
  20. 'status_text'
  21. ];
  22. protected static function init()
  23. {
  24. self::beforeUpdate(function ($row) {
  25. $changed = $row->getChangedData();
  26. //如果有修改密码
  27. if (isset($changed['password'])) {
  28. if ($changed['password']) {
  29. $salt = \fast\Random::alnum();
  30. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  31. $row->salt = $salt;
  32. } else {
  33. unset($row->password);
  34. }
  35. }
  36. //email查重
  37. if (isset($changed['email'])) {
  38. if($changed['email']){
  39. $exists = db('coach')->where('email', $changed['email'])->where('id', '<>', $row->id)->find();
  40. if ($exists) {
  41. abort(500,'email已经被使用');
  42. }
  43. $exists = db('user')->where('email', $changed['email'])->find();
  44. if ($exists) {
  45. abort(500,'email已经被用户使用');
  46. }
  47. }
  48. }
  49. });
  50. self::beforeInsert(function ($row) {
  51. if (isset($row['password'])) {
  52. if ($row['password']) {
  53. $salt = \fast\Random::alnum();
  54. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($row['password'], $salt);
  55. $row->salt = $salt;
  56. } else {
  57. unset($row->password);
  58. }
  59. }
  60. //email查重
  61. if($row['email']){
  62. $exists = db('coach')->where('email', $row['email'])->find();
  63. if ($exists) {
  64. abort(500,'email已经被使用');
  65. }
  66. $exists = db('user')->where('email', $row['email'])->find();
  67. if ($exists) {
  68. abort(500,'email已经被用户使用');
  69. }
  70. }else{
  71. abort(500,'email不能为空');
  72. }
  73. });
  74. }
  75. public function getGenderList()
  76. {
  77. return ['1' => __('Gender 1'), '0' => __('Gender 0')];
  78. }
  79. public function getStatusList()
  80. {
  81. return ['0' => __('Status 0'), '1' => __('Status 1')];
  82. }
  83. public function getGenderTextAttr($value, $data)
  84. {
  85. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  86. $list = $this->getGenderList();
  87. return isset($list[$value]) ? $list[$value] : '';
  88. }
  89. public function getPrevtimeTextAttr($value, $data)
  90. {
  91. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  92. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  93. }
  94. public function getLogintimeTextAttr($value, $data)
  95. {
  96. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  97. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  98. }
  99. public function getJointimeTextAttr($value, $data)
  100. {
  101. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  102. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  103. }
  104. public function getStatusTextAttr($value, $data)
  105. {
  106. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  107. $list = $this->getStatusList();
  108. return isset($list[$value]) ? $list[$value] : '';
  109. }
  110. protected function setPrevtimeAttr($value)
  111. {
  112. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  113. }
  114. protected function setLogintimeAttr($value)
  115. {
  116. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  117. }
  118. protected function setJointimeAttr($value)
  119. {
  120. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  121. }
  122. public function tag()
  123. {
  124. return $this->belongsTo('app\admin\model\Coachtag', 'tag_ids', 'id', [], 'LEFT')->setEagerlyType(0);
  125. }
  126. }