Coach.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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['mobile'])) {
  38. if($changed['mobile']){
  39. $exists = db('coach')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
  40. if ($exists) {
  41. abort(500,'手机已经被使用');
  42. }
  43. }
  44. }
  45. });
  46. self::beforeInsert(function ($row) {
  47. if (isset($row['password'])) {
  48. if ($row['password']) {
  49. $salt = \fast\Random::alnum();
  50. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($row['password'], $salt);
  51. $row->salt = $salt;
  52. } else {
  53. unset($row->password);
  54. }
  55. }
  56. //mobile查重
  57. if($row['mobile']){
  58. $exists = db('coach')->where('mobile', $row['mobile'])->find();
  59. if ($exists) {
  60. abort(500,'手机已经被使用');
  61. }
  62. }else{
  63. abort(500,'手机不能为空');
  64. }
  65. });
  66. }
  67. public function getGenderList()
  68. {
  69. return ['1' => __('Gender 1'), '0' => __('Gender 0')];
  70. }
  71. public function getStatusList()
  72. {
  73. return ['0' => __('Status 0'), '1' => __('Status 1')];
  74. }
  75. public function getGenderTextAttr($value, $data)
  76. {
  77. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  78. $list = $this->getGenderList();
  79. return isset($list[$value]) ? $list[$value] : '';
  80. }
  81. public function getPrevtimeTextAttr($value, $data)
  82. {
  83. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  84. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  85. }
  86. public function getLogintimeTextAttr($value, $data)
  87. {
  88. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  89. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  90. }
  91. public function getJointimeTextAttr($value, $data)
  92. {
  93. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  94. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  95. }
  96. public function getStatusTextAttr($value, $data)
  97. {
  98. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  99. $list = $this->getStatusList();
  100. return isset($list[$value]) ? $list[$value] : '';
  101. }
  102. protected function setPrevtimeAttr($value)
  103. {
  104. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  105. }
  106. protected function setLogintimeAttr($value)
  107. {
  108. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  109. }
  110. protected function setJointimeAttr($value)
  111. {
  112. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  113. }
  114. }