123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace App\Models;
- use App\Utils\Admin;
- class AdminMerchant extends Model
- {
- // protected $table = "admin_merchant";
- protected $fillable = ['admin_id', 'wxapp_name', 'wxapp_desc',
- 'wxapp_app_id', 'wxapp_app_secret', 'phone', 'name', 'email', 'address',
- 'proportion', 'wxapp_url', 'pay_mch_id', 'pay_key', 'private_key_file',
- 'certificate_file', 'brand', 'cash', 'is_refund', 'username', 'area_name', 'mck_key',
- 'order_key', 'refund_callback_url', 'wxapp_qr_code', 'car_qr_code_url', 'factory_qr_code'
- ];
- const PAST_DUE = 0;//过期状态
- const NORMAL = 1;// 正常状态
- // ->where(AdminMerchant::getMerchantWhere())
- public function admin_user()
- {
- return $this->hasOne(AdminUser::class, 'id', 'admin_id');
- }
- /**
- * 获取登录账号的商户id
- * @return int|mixed
- */
- public static function getMchId()
- {
- $merchant_id = 0;
- if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
- // 技术
- $merchant_id = 0;
- } else if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) {
- // 商户
- $merchant_id = AdminMerchant::query()->where(['admin_id' => Admin::user()->id])->first(['id'])['id'];
- } else {
- // 非商户
- $merchant_id = Admin::user()->merchant_id;
- }
- return $merchant_id;
- }
- /**
- * 查询 merchant_id where 条件 数组
- * @return array
- */
- public static function getMerchantWhere()
- {
- $where = [];
- if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
- // 技术
- $where = [];
- } else if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) {
- // 商户
- $merchant_id = \Session::get('sh_merchant_id');
- if (empty($merchant_id)) {
- $merchant_id = AdminMerchant::query()->where(['admin_id' => Admin::user()->id])->first(['id'])['id'];
- \Session::put('sh_merchant_id', $merchant_id);
- }
- $where['merchant_id'] = $merchant_id;
- } else {
- // 非商户
- $where['merchant_id'] = Admin::user()->merchant_id;
- }
- return $where;
- }
- /**
- * 获取登录着的商户id
- * @return int|mixed
- */
- public static function putMerchantId()
- {
- if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
- // 技术
- $merchant_id = 0;
- } else if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) {
- // 商户
- $merchant_id = AdminMerchant::query()->where(['admin_id' => Admin::user()->id])->first(['id'])['id'];
- } else {
- // 非商户
- $merchant_id = Admin::user()->merchant_id;
- }
- return $merchant_id;
- }
- // 判断用户是否属于商户
- public static function isUserToMch($user_id)
- {
- $merchant_id = self::putMerchantId();
- if ($merchant_id == 0) {
- // 超级管理员
- return false;
- }
- $user_merchant_id = User::query()->where(['id' => $user_id, 'merchant_id' => $merchant_id]);
- if ($user_merchant_id) {
- return false;
- }
- // 不存在提示错误
- return true;
- }
- }
|