Formid.php 1.2 KB

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