Formid.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace app\common\model\wxapp;
  3. use app\common\model\BaseModel;
  4. /**
  5. * form_id 模型
  6. * Class Apply
  7. * @package app\common\model\dealer
  8. */
  9. class Formid extends BaseModel
  10. {
  11. protected $name = 'wxapp_formid';
  12. /**
  13. * 关联用户表
  14. * @return \think\model\relation\BelongsTo
  15. */
  16. public function user()
  17. {
  18. $module = self::getCalledModule() ?: 'common';
  19. return $this->belongsTo("app\\{$module}\\model\\User");
  20. }
  21. /**
  22. * 获取一个可用的formid
  23. * @param $userId
  24. * @return array|false|\PDOStatement|string|\think\Model|static
  25. */
  26. public static function getAvailable($userId)
  27. {
  28. return (new static)->where([
  29. 'user_id' => $userId,
  30. 'is_used' => 0,
  31. 'expiry_time' => ['>=', time()]
  32. ])->order(['create_time' => 'asc'])->find();
  33. }
  34. /**
  35. * 标记为已使用
  36. * @param $id
  37. * @return Formid
  38. */
  39. public static function setIsUsed($id)
  40. {
  41. return static::update(['is_used' => 1], compact('id'));
  42. }
  43. }