UserRelation.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class UserRelation extends Model
  5. {
  6. // 表名
  7. protected $name = 'user_relation';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = false;
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'sex_text',
  17. 'subscribe_text',
  18. 'subscribe_time_text',
  19. 'user_type_text'
  20. ];
  21. public function getSexList()
  22. {
  23. return ['1' => __('Sex 1'), '2' => __('Sex 2'), '0' => __('Sex 0')];
  24. }
  25. public function getSubscribeList()
  26. {
  27. return ['0' => __('Subscribe 0'), '1' => __('Subscribe 1')];
  28. }
  29. public function getUserTypeList()
  30. {
  31. return ['0' => __('User_type 0'), '1' => __('User_type 1'), '2' => __('User_type 2'), '3' => __('User_type 3')];
  32. }
  33. public function getSexTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
  36. $list = $this->getSexList();
  37. return isset($list[$value]) ? $list[$value] : '';
  38. }
  39. public function getSubscribeTextAttr($value, $data)
  40. {
  41. $value = $value ? $value : (isset($data['subscribe']) ? $data['subscribe'] : '');
  42. $list = $this->getSubscribeList();
  43. return isset($list[$value]) ? $list[$value] : '';
  44. }
  45. public function getSubscribeTimeTextAttr($value, $data)
  46. {
  47. $value = $value ? $value : (isset($data['subscribe_time']) ? $data['subscribe_time'] : '');
  48. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  49. }
  50. public function getUserTypeTextAttr($value, $data)
  51. {
  52. $value = $value ? $value : (isset($data['user_type']) ? $data['user_type'] : '');
  53. $list = $this->getUserTypeList();
  54. return isset($list[$value]) ? $list[$value] : '';
  55. }
  56. protected function setSubscribeTimeAttr($value)
  57. {
  58. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  59. }
  60. public static function openidToUid($openid,$field="openid")
  61. {
  62. $model = new self;
  63. return $model->where($field,$openid)->value('user_id');
  64. }
  65. }