Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

123 linhas
2.8 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/2/9
  6. * Time: 6:29 PM
  7. */
  8. namespace addons\unishop\model;
  9. use addons\unishop\extend\Hashids;
  10. use think\Model;
  11. use traits\model\SoftDelete;
  12. class FlashSale extends Model
  13. {
  14. use SoftDelete;
  15. // 表名
  16. protected $name = 'unishop_flash_sale';
  17. // 开启自动写入时间戳字段
  18. protected $autoWriteTimestamp = 'int';
  19. // 定义时间戳字段名
  20. protected $createTime = 'createtime';
  21. protected $updateTime = 'updatetime';
  22. protected $deleteTime = 'deletetime';
  23. // 已归档
  24. const STATUS_YES = 1; // 是
  25. const STATUS_NO = 0; // 否
  26. // 已上架
  27. const SWITCH_YES = 1; // 是
  28. const SWITCH_NO = 0; // 否
  29. // 隐藏属性
  30. protected $hidden = [
  31. 'id'
  32. ];
  33. // 追加属性
  34. protected $append = [
  35. 'flash_id',
  36. 'starttime_hour',
  37. 'state',
  38. 'current',
  39. ];
  40. /**
  41. * Encode flash_id
  42. */
  43. public function getFlashIdAttr($value, $data) {
  44. return Hashids::encodeHex($data['id']);
  45. }
  46. /**
  47. * Format time 'H:i'
  48. */
  49. public function getStarttimeHourAttr($value, $data) {
  50. return date('H:i', $data['starttime']);
  51. }
  52. /**
  53. * Now in progress
  54. */
  55. public function getCurrentAttr($value, $data) {
  56. if (date('Y-m-d H:00:00', time()) == date('Y-m-d H:00:00', $data['starttime'])) {
  57. return true;
  58. } else {
  59. return false;
  60. }
  61. }
  62. /**
  63. * State
  64. */
  65. public function getStateAttr($value, $data) {
  66. $time = time();
  67. if (date('Y-m-d H:00:00', $time) == date('Y-m-d H:00:00', $data['starttime'])) {
  68. return 2; // '抢购进行中';
  69. } else if ($data['starttime'] < $time) {
  70. return 1; // '已开抢';
  71. } else {
  72. return 0; // '未开始';
  73. }
  74. }
  75. /**
  76. * 关联秒杀产品表
  77. * @return \think\model\relation\HasMany
  78. */
  79. public function product()
  80. {
  81. return $this->hasMany('flashProduct', 'flash_id', 'id');
  82. }
  83. /**
  84. * 获取离开始时间的倒计时
  85. * @param $targetTime
  86. * @return bool
  87. */
  88. public static function countdown($targetTime)
  89. {
  90. $time = $targetTime - time();
  91. if ($time > 0) {
  92. // 如果time等于0,那么时间是从1970-01-01 08:00:00开始的
  93. $countdown['day'] = intval(date('d', $time) - 1);
  94. $countdown['hour'] = intval(date('H', $time) - 8);
  95. $countdown['minute'] = intval(date('i', $time));
  96. $countdown['second'] = intval(date('s', $time));
  97. foreach ($countdown as &$item) {
  98. if ($item < 0) $item = 0;
  99. }
  100. return $countdown;
  101. }
  102. return false;
  103. }
  104. }