Worker.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. use app\common\library\Tenim;
  6. class Worker extends Model
  7. {
  8. use SoftDelete;
  9. // 表名
  10. protected $table = 'worker';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = false;
  13. // 定义时间戳字段名
  14. protected $createTime = false;
  15. protected $updateTime = false;
  16. protected $deleteTime = 'deletetime';
  17. // 追加属性
  18. protected $append = [
  19. 'status_text',
  20. 'prevtime_text',
  21. 'logintime_text'
  22. ];
  23. protected static function init()
  24. {
  25. self::afterInsert(function($row){
  26. $worker_id = $row['id'];
  27. //注册到im
  28. //user_用户端小程序,master_师傅,kefu_客服
  29. $tenim = new Tenim();
  30. $rs = $tenim->register('master_'. $worker_id, $row['truename'], localpath_to_netpath($row['avatar']));
  31. });
  32. self::afterUpdate(function ($row) {
  33. $changed = $row->getChangedData();
  34. //如果有修改头像或昵称,同步到im
  35. if (isset($changed['truename']) || isset($changed['avatar'])) {
  36. //user_用户端小程序,master_师傅,kefu_客服
  37. $tenim = new Tenim();
  38. $rs = $tenim->useredit('master_'. $row['id'], $row['truename'], localpath_to_netpath($row['avatar']));
  39. }
  40. });
  41. }
  42. public function getStatusList()
  43. {
  44. return ['1' => __('Status 1'), '0' => __('Status 0'), '-1' => __('Status -1')];
  45. }
  46. public function getStatusTextAttr($value, $data)
  47. {
  48. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  49. $list = $this->getStatusList();
  50. return isset($list[$value]) ? $list[$value] : '';
  51. }
  52. public function getPrevtimeTextAttr($value, $data)
  53. {
  54. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  55. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  56. }
  57. public function getLogintimeTextAttr($value, $data)
  58. {
  59. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  60. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  61. }
  62. protected function setPrevtimeAttr($value)
  63. {
  64. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  65. }
  66. protected function setLogintimeAttr($value)
  67. {
  68. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  69. }
  70. public function company()
  71. {
  72. return $this->belongsTo('Company', 'company_id', 'id', [], 'LEFT')->setEagerlyType(0);
  73. }
  74. }