AdminMerchant.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Models;
  3. use App\Utils\Admin;
  4. class AdminMerchant extends Model
  5. {
  6. // protected $table = "admin_merchant";
  7. protected $fillable = ['admin_id', 'wxapp_name', 'wxapp_desc',
  8. 'wxapp_app_id', 'wxapp_app_secret', 'phone', 'name', 'email', 'address',
  9. 'proportion', 'wxapp_url', 'pay_mch_id', 'pay_key', 'private_key_file',
  10. 'certificate_file', 'brand', 'cash', 'is_refund', 'username', 'area_name', 'mck_key',
  11. 'order_key', 'refund_callback_url', 'wxapp_qr_code', 'car_qr_code_url', 'factory_qr_code'
  12. ];
  13. const PAST_DUE = 0;//过期状态
  14. const NORMAL = 1;// 正常状态
  15. // ->where(AdminMerchant::getMerchantWhere())
  16. public function admin_user()
  17. {
  18. return $this->hasOne(AdminUser::class, 'id', 'admin_id');
  19. }
  20. /**
  21. * 获取登录账号的商户id
  22. * @return int|mixed
  23. */
  24. public static function getMchId()
  25. {
  26. $merchant_id = 0;
  27. if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
  28. // 技术
  29. $merchant_id = 0;
  30. } else if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) {
  31. // 商户
  32. $merchant_id = AdminMerchant::query()->where(['admin_id' => Admin::user()->id])->first(['id'])['id'];
  33. } else {
  34. // 非商户
  35. $merchant_id = Admin::user()->merchant_id;
  36. }
  37. return $merchant_id;
  38. }
  39. /**
  40. * 查询 merchant_id where 条件 数组
  41. * @return array
  42. */
  43. public static function getMerchantWhere()
  44. {
  45. $where = [];
  46. if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
  47. // 技术
  48. $where = [];
  49. } else if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) {
  50. // 商户
  51. $merchant_id = \Session::get('sh_merchant_id');
  52. if (empty($merchant_id)) {
  53. $merchant_id = AdminMerchant::query()->where(['admin_id' => Admin::user()->id])->first(['id'])['id'];
  54. \Session::put('sh_merchant_id', $merchant_id);
  55. }
  56. $where['merchant_id'] = $merchant_id;
  57. } else {
  58. // 非商户
  59. $where['merchant_id'] = Admin::user()->merchant_id;
  60. }
  61. return $where;
  62. }
  63. /**
  64. * 获取登录着的商户id
  65. * @return int|mixed
  66. */
  67. public static function putMerchantId()
  68. {
  69. if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
  70. // 技术
  71. $merchant_id = 0;
  72. } else if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) {
  73. // 商户
  74. $merchant_id = AdminMerchant::query()->where(['admin_id' => Admin::user()->id])->first(['id'])['id'];
  75. } else {
  76. // 非商户
  77. $merchant_id = Admin::user()->merchant_id;
  78. }
  79. return $merchant_id;
  80. }
  81. // 判断用户是否属于商户
  82. public static function isUserToMch($user_id)
  83. {
  84. $merchant_id = self::putMerchantId();
  85. if ($merchant_id == 0) {
  86. // 超级管理员
  87. return false;
  88. }
  89. $user_merchant_id = User::query()->where(['id' => $user_id, 'merchant_id' => $merchant_id]);
  90. if ($user_merchant_id) {
  91. return false;
  92. }
  93. // 不存在提示错误
  94. return true;
  95. }
  96. }