RoutineQrcode.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\admin\model\wechat;
  3. use think\Model;
  4. class RoutineQrcode extends Model
  5. {
  6. // 表名
  7. protected $name = 'routine_qrcode';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = false;
  10. // 定义时间戳字段名
  11. protected $createTime = false;
  12. protected $updateTime = false;
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'add_time_text',
  17. 'url_time_text'
  18. ];
  19. public function getAddTimeTextAttr($value, $data)
  20. {
  21. $value = $value ? $value : (isset($data['add_time']) ? $data['add_time'] : '');
  22. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  23. }
  24. public function getUrlTimeTextAttr($value, $data)
  25. {
  26. $value = $value ? $value : (isset($data['url_time']) ? $data['url_time'] : '');
  27. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  28. }
  29. protected function setAddTimeAttr($value)
  30. {
  31. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  32. }
  33. protected function setUrlTimeAttr($value)
  34. {
  35. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  36. }
  37. /**
  38. * TODO 添加二维码 存在直接获取
  39. * @param int $thirdId
  40. * @param string $thirdType
  41. * @param string $page
  42. * @param string $qrCodeLink
  43. * @return array|false|object|\PDOStatement|string|\think\Model
  44. * @throws \think\Exception
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public static function routineQrCodeForever($thirdId = 0, $thirdType = 'spread', $page = '', $qrCodeLink = '')
  50. {
  51. $count = self::where('third_id', $thirdId)->where('third_type', $thirdType)->count();
  52. if ($count) return self::where('third_id', $thirdId)->where('third_type', $thirdType)->field('id')->find();
  53. return self::setRoutineQrcodeForever($thirdId, $thirdType, $page, $qrCodeLink);
  54. }
  55. /**
  56. * 添加二维码记录
  57. * @param string $thirdType
  58. * @param int $thirdId
  59. * @return object
  60. */
  61. public static function setRoutineQrcodeForever($thirdId = 0, $thirdType = 'spread', $page = '', $qrCodeLink = '')
  62. {
  63. $data['third_type'] = $thirdType;
  64. $data['third_id'] = $thirdId;
  65. $data['status'] = 1;
  66. $data['add_time'] = time();
  67. $data['page'] = $page;
  68. $data['url_time'] = '';
  69. $data['qrcode_url'] = $qrCodeLink;
  70. return self::create($data);
  71. }
  72. /**
  73. * 修改二维码地址
  74. * @param int $id
  75. * @param array $data
  76. * @return bool
  77. */
  78. public static function setRoutineQrcodeFind($id = 0, $data = array())
  79. {
  80. if (!$id) return false;
  81. $count = self::getRoutineQrcodeFind($id);
  82. if (!$count) return false;
  83. return self::edit($data, $id, 'id');
  84. }
  85. /**
  86. * 获取二维码是否存在
  87. * @param int $id
  88. * @return int|string
  89. */
  90. public static function getRoutineQrcodeFind($id = 0)
  91. {
  92. if (!$id) return 0;
  93. return self::where('id', $id)->count();
  94. }
  95. /**
  96. * 获取小程序二维码信息
  97. * @param int $id
  98. * @param string $field
  99. * @return array|bool|false|\PDOStatement|string|\think\Model
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. * @throws \think\exception\DbException
  103. */
  104. public static function getRoutineQrcodeFindType($id = 0, $field = 'third_type,third_id,page')
  105. {
  106. if (!$id) return false;
  107. $count = self::getRoutineQrcodeFind($id);
  108. if (!$count) return false;
  109. return self::where('id', $id)->where('status', 1)->field($field)->find();
  110. }
  111. }