Site.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\models\system;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use think\facade\Env;
  10. /**
  11. * 物流公司
  12. * Class Express
  13. * @package app\models\system
  14. */
  15. class Site extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'site';
  27. use ModelTrait;
  28. public static function lst()
  29. {
  30. $model = new self;
  31. $model = $model->where('status', 1);
  32. return $model->select();
  33. }
  34. public static function addSite($name, $domain)
  35. {
  36. if (self::be(['domain' => $domain])) return self::setErrorInfo('已存在同名站点');
  37. return self::create([
  38. 'name' => $name,
  39. 'appid' => self::getNewAppId(),
  40. 'appsecret' => self::getNewSecret(),
  41. 'add_time' => time(),
  42. 'domain' => $domain,
  43. ]);
  44. }
  45. public static function getNewAppId()
  46. {
  47. do {
  48. $str = 'La' . strtoupper(substr(md5(time() . rand(0, 100000) . self::getLastInsID()), 0, 12));
  49. } while (self::be(['appid' => $str]));
  50. return $str;
  51. }
  52. public static function getNewSecret()
  53. {
  54. return strtoupper(md5(time() . rand(0, 100000) . self::getLastInsID()));
  55. }
  56. public static function checkSign($sign, $appid, $time, $action)
  57. {
  58. // var_dump(Env::get('app_debug'));
  59. if ($action != 'register' && $action != 'verify' && !Env::get('app_debug') && (strlen((string)$time) >= 13 ? $time / 1000 : $time) + 10 < time()) return self::setErrorInfo('签名已过期');
  60. return $sign === self::getSign($appid, $time) ? self::where('appid', $appid)->value('id') : self::setErrorInfo('无效的签名');
  61. }
  62. public static function getSign($appid, $time)
  63. {
  64. $appSecret = self::where('status', 1)->where('appid', $appid)->value('appsecret');
  65. if (!$appSecret) {
  66. return self::setErrorInfo('站点异常');
  67. }
  68. return strtoupper(md5(md5($appid . $appSecret . $time . $appid . $time . $appSecret . $appSecret . $appid)));
  69. }
  70. }