Coach.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 getBgcolorList(){
  84. return $bgcolorList = [
  85. ['id' => 1, 'val' => '#E6EEFF'],
  86. ['id' => 2, 'val' => '#BACFFE'],
  87. ['id' => 3, 'val' => '#2D6CFB'],
  88. ['id' => 4, 'val' => '#1380A3'],
  89. ['id' => 5, 'val' => '#125B8F'],
  90. ['id' => 6, 'val' => '#0272C1'],
  91. ['id' => 7, 'val' => '#001D86'],
  92. ['id' => 8, 'val' => '#001253'],
  93. ['id' => 9, 'val' => '#E5DEF6'],
  94. ['id' => 10, 'val' => '#C7B8EC'],
  95. ['id' => 11, 'val' => '#A231A5'],
  96. ['id' => 12, 'val' => '#C53E99'],
  97. ['id' => 13, 'val' => '#A61C79'],
  98. ['id' => 14, 'val' => '#7852D2'],
  99. ['id' => 15, 'val' => '#6033CA'],
  100. ['id' => 16, 'val' => '#6D084C'],
  101. ['id' => 17, 'val' => '#FFF7CD'],
  102. ['id' => 18, 'val' => '#FAB41D'],
  103. ['id' => 19, 'val' => '#FF9F00'],
  104. ['id' => 20, 'val' => '#D28300'],
  105. ['id' => 21, 'val' => '#FA650E'],
  106. ['id' => 22, 'val' => '#CA4B00'],
  107. ['id' => 23, 'val' => '#712A00'],
  108. ['id' => 24, 'val' => '#441900'],
  109. ['id' => 25, 'val' => '#DCEEDA'],
  110. ['id' => 26, 'val' => '#B7DDB3'],
  111. ['id' => 27, 'val' => '#46A83D'],
  112. ['id' => 28, 'val' => '#58A451'],
  113. ['id' => 29, 'val' => '#44823F'],
  114. ['id' => 30, 'val' => '#278252'],
  115. ['id' => 31, 'val' => '#244B21'],
  116. ['id' => 32, 'val' => '#0E220C'],
  117. ['id' => 33, 'val' => '#FADDE0'],
  118. ['id' => 34, 'val' => '#F4ABB4'],
  119. ['id' => 35, 'val' => '#E84056'],
  120. ['id' => 36, 'val' => '#D75B5B'],
  121. ['id' => 37, 'val' => '#FF5350'],
  122. ['id' => 38, 'val' => '#DD001D'],
  123. ['id' => 39, 'val' => '#AE2929'],
  124. ['id' => 40, 'val' => '#680707'],
  125. ['id' => 41, 'val' => '#F0F0F0'],
  126. ['id' => 42, 'val' => '#DADEE0'],
  127. ['id' => 43, 'val' => '#B7BEC3'],
  128. ['id' => 44, 'val' => '#6C7B85'],
  129. ['id' => 45, 'val' => '#42454C'],
  130. ['id' => 46, 'val' => '#32536A'],
  131. ['id' => 47, 'val' => '#162D3D'],
  132. ['id' => 48, 'val' => '#000000'],
  133. ];
  134. }
  135. public function getGenderTextAttr($value, $data)
  136. {
  137. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  138. $list = $this->getGenderList();
  139. return isset($list[$value]) ? $list[$value] : '';
  140. }
  141. public function getPrevtimeTextAttr($value, $data)
  142. {
  143. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  144. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  145. }
  146. public function getLogintimeTextAttr($value, $data)
  147. {
  148. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  149. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  150. }
  151. public function getJointimeTextAttr($value, $data)
  152. {
  153. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  154. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  155. }
  156. public function getStatusTextAttr($value, $data)
  157. {
  158. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  159. $list = $this->getStatusList();
  160. return isset($list[$value]) ? $list[$value] : '';
  161. }
  162. protected function setPrevtimeAttr($value)
  163. {
  164. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  165. }
  166. protected function setLogintimeAttr($value)
  167. {
  168. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  169. }
  170. protected function setJointimeAttr($value)
  171. {
  172. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  173. }
  174. public function tag()
  175. {
  176. return $this->belongsTo('app\admin\model\Coachtag', 'tag_ids', 'id', [], 'LEFT')->setEagerlyType(0);
  177. }
  178. }