merchant.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. class merchantControl
  3. {
  4. private $mMchid;
  5. private $mAdminid;
  6. private $mUseKey;
  7. public function __construct()
  8. {
  9. $mchid = $_POST['mchid'];
  10. $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  11. if(empty($mchinfo)) {
  12. throw new Exception("合作方ID:{$mchid}不存在");
  13. }
  14. else {
  15. $this->mAdminid = intval($mchinfo['admin_id']);
  16. }
  17. if ($mchinfo['merchant_state'] != 1) {
  18. throw new Exception("机构已被关闭。");
  19. }
  20. $ips = unserialize($mchinfo['ip_white_list']);
  21. if(!empty($ips)) {
  22. $addr = $_SERVER['REMOTE_ADDR'];
  23. Log::record("request ip:{$addr}",Log::DEBUG);
  24. if(!in_array($addr,$ips)) {
  25. throw new Exception("请求地址不在白名单中");
  26. }
  27. }
  28. $this->mUseKey = intval($mchinfo['use_key']);
  29. if($this->mUseKey && !$this->verify_md5($mchinfo['secure_key'])) {
  30. throw new UnSignException();
  31. }
  32. $this->mMchid = intval($mchid);
  33. }
  34. public function mchid() : int
  35. {
  36. return $this->mMchid;
  37. }
  38. public function adminid() : int {
  39. return $this->mAdminid;
  40. }
  41. private function verify_md5($key)
  42. {
  43. $input = $_GET;
  44. $sign = $input['sign'];
  45. $input['sign'] = null;
  46. $input['from'] = null;
  47. $body = $this->sign_body($input);
  48. if($this->mUseKey) {
  49. $body .= "&key={$key}";
  50. }
  51. return ($sign == md5($body));
  52. }
  53. protected function check_empty($value)
  54. {
  55. if (!isset($value))
  56. return true;
  57. if ($value === null)
  58. return true;
  59. if (trim($value) === "")
  60. return true;
  61. return false;
  62. }
  63. private function sign_body($params)
  64. {
  65. ksort($params);
  66. $content = "";
  67. $i = 0;
  68. foreach ($params as $k => $v)
  69. {
  70. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1))
  71. {
  72. if ($i == 0) {
  73. $content .= "{$k}" . "=" . urlencode($v);
  74. } else {
  75. $content .= "&" . "{$k}" . "=" . urlencode($v);
  76. }
  77. $i++;
  78. }
  79. }
  80. return $content;
  81. }
  82. public static function outsuccess($data)
  83. {
  84. joutput_data($data);
  85. return true;
  86. }
  87. public static function outerr($code, $msg = '')
  88. {
  89. joutput_error($code, $msg);
  90. return true;
  91. }
  92. }