<?php
namespace eapie\source\request;
use eapie\main;
use eapie\error;
class merchant extends main
{
const AUTHORITY_MERCHANT_READ = 'merchant_read';
const AUTHORITY_MERCHANT_ADD = 'merchant_add';
const AUTHORITY_MERCHANT_EDIT = 'merchant_edit';
const AUTHORITY_MERCHANT_REMOVE = 'merchant_remove';
const AUTHORITY_MERCHANT_USER_READ = 'merchant_user_read';
const AUTHORITY_MERCHANT_USER_ADD = 'merchant_user_add';
const AUTHORITY_MERCHANT_USER_EDIT = 'merchant_user_edit';
const AUTHORITY_MERCHANT_USER_REMOVE = 'merchant_user_remove';
const AUTHORITY_IMAGE_UPLOAD = "merchant_image_upload" const AUTHORITY_IMAGE_REMOVE = "merchant_image_remove" const AUTHORITY_IMAGE_EDIT = "merchant_image_edit"
const AUTHORITY_CREDIT_READ = 'merchant_credit_read';
const AUTHORITY_CREDIT_EDIT = 'merchant_credit_edit';
const AUTHORITY_MONEY_READ = 'merchant_money_read';
const AUTHORITY_MONEY_EDIT = 'merchant_money_edit';
const AUTHORITY_WITHDRAW_READ = 'merchant_withdraw_read';
const AUTHORITY_WITHDRAW_STATE = 'merchant_withdraw_state';
const AUTHORITY_TALLY_READ = 'merchant_tally_read'
const AUTHORITY_CONFIG_READ = 'merchant_config_read';
const AUTHORITY_CONFIG_EDIT = 'merchant_config_edit';
const AUTHORITY_CASHIER_LIST = 'merchant_cashier_list' const AUTHORITY_CASHIER_STATE = 'merchant_cashier_state' const AUTHORITY_CASHIER_REMOVE = 'merchant_cashier_remove'
protected function check_role($input = array())
{
object(parent::REQUEST_USER)->check();
$user_id = $_SESSION['user_id'];
$mch_id = null;
if (isset($input['merchant_id'])) {
object(parent::ERROR)->check($input, 'merchant_id', parent::TABLE_MERCHANT, array('args'));
$mch_id = $input['merchant_id'];
} else {
$mch_ids = object(parent::TABLE_MERCHANT_USER)->get_mch_ids($user_id);
if (empty($mch_ids))
throw new error('不是商家用户');
else
$mch_id = $mch_ids[0];
}
if (object(parent::TABLE_MERCHANT_USER)->check_exist($user_id, $mch_id, true))
return $mch_id;
else
throw new error('权限不足');
}
public function check($merchant_id, $return_bool = false){
if( empty($return_bool) ){
object(parent::REQUEST_USER)->check();
}else{
$bool = object(parent::REQUEST_USER)->check(true);
if( empty($bool) ){
return false;
}
}
$merchant = object(parent::TABLE_MERCHANT)->find($merchant_id);
if( empty($merchant) ){
if( empty($return_bool) ){
throw new error('商家不存在');
}else{
return false;
}
}
if( $merchant['merchant_state'] != 1 ){
if( empty($return_bool) ){
throw new error('商家未认证');
}else{
return false;
}
}
$merchant_user = object(parent::TABLE_MERCHANT_USER)->find_where(array(
array('merchant_id = [+]', $merchant_id),
array('[and] user_id = [+]', $_SESSION['user_id'])
));
if( empty($merchant_user) ){
if( empty($return_bool) ){
throw new error('不是商家用户');
}else{
return false;
}
}
if( $merchant_user['merchant_user_state'] != 1 ){
if( empty($return_bool) ){
throw new error('商家用户未认证');
}else{
return false;
}
}
$_SESSION['merchant'] = $merchant;
$_SESSION['merchant'] = array_merge($_SESSION['merchant'], $merchant_user);
return true;
}
protected function _check_merchant_user_state_($merchant_id = '')
{
object(parent::REQUEST_USER)->check();
$user_id = $_SESSION['user_id'];
if (!empty($merchant_id)) {
$mch_ids = object(parent::TABLE_MERCHANT_USER)->get_mch_ids($user_id);
if (empty($mch_ids)) {
throw new error('不是商家用户');
} else {
$merchant_id = $mch_ids[0];
}
}
$merchant = object(parent::TABLE_MERCHANT)->find($merchant_id);
if (empty($merchant))
throw new error('商家不存在');
if ($merchant['merchant_state'] != 1)
throw new error('商家未认证');
$merchant_user = object(parent::TABLE_MERCHANT_USER)->find_where(array(
array('merchant_id = [+]', $merchant_id),
array('[and] user_id = [+]', $_SESSION['user_id'])
));
if (empty($merchant_user))
throw new error('不是商家用户');
if ($merchant_user['merchant_user_state'] != 1)
throw new error('商家用户未认证');
return $merchant_id;
}
}