<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller\merchant_admin;

use app\api\controller\Base;
use think\Db;
use think\Loader;

class MerchantBase extends Base
{

    protected $merchant = null;
    protected $model = null;
    protected $mid = 0;
    protected $noMerchantNeedRight = [];
    
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Merchant;
        $this->initMerchant();
    }
    
    public function initMerchant(){
        $identity = $this->param("identity/d");
        $set = $this->model->getSet();
        if($set['status'] != 1){
            __error(-1,"商户功能未启用");
        }
        
        $user = $this->user;
        if(empty($user)){
            __error("请先登录");
        }
        if($user['id'] == 13){
            // $user = \app\common\model\bbfxshop\User::getInfo(3);
        }

        $mid = $this->param("mid/d");


        $actionname = strtolower($this->request->action());

        $where = ['uniacid'=>UNIACID,'user_id'=>$user['id']];
        if($mid > 0){
            $where['id'] = $mid;
        }

        $merchant = Db::name("bbfx_merchant")->where($where)->find();
        if(empty($merchant)){
            $where = ['uniacid'=>UNIACID,'hexiao_user_id'=>['like','%,'.$user['id'].',%']];
            if($mid > 0){
                $where['id'] = $mid;
            }

            $merchant = Db::name("bbfx_merchant")->where($where)->find();
            if(!empty($merchant)){
                if(!in_array($actionname,['hexiao','checkhexiaocode','checkHexiaoCode','detail'])){
                    // __error("无权限操作");
                }
            }
        }
        if(!empty($merchant)){
            $this->merchant = $merchant;
            $this->mid = $merchant['id'];
        }

        $noMerchantNeedRight = $this->noMerchantNeedRight;
        if($noMerchantNeedRight != '*' && $noMerchantNeedRight[0] != '*'){
            
            
            foreach ($noMerchantNeedRight as $k => &$v){
                $v = strtolower($v);
            }
            unset($v);

            if(!in_array($actionname,$noMerchantNeedRight)){
                if(empty($merchant)){
                    __error(-1,"您还不是商户身份");
                }
                if($merchant['sh_status'] == 0 && $merchant['is_shenhe'] != 1){
                    __error(-2,"您申请的商户还在审核中");
                }
                if($merchant['sh_status'] == 2 && $merchant['is_shenhe'] != 1){
                    __error(-3,"您申请的商户未审核通过");
                }
                if($merchant['status'] != 1){
                    __error(-4,"您申请的商户已被禁用");
                }
            }
        }
    }
}