<?php
|
|
namespace app\branch\model\branch;
|
|
use app\common\model\branch\Branch as BranchModel;
|
use app\common\model\branch\User as BranchUserModel;
|
use app\common\model\user\User as UserModel;
|
|
/**
|
* 后台管理员登录模型
|
*/
|
class Branch extends BranchModel
|
{
|
/**
|
* 获取列表数据
|
*/
|
public function getList($params, $branch_id)
|
{
|
$model = $this;
|
if (isset($params['search']) && $params['search']) {
|
$model = $model->where('name', 'like', '%' . $params['search'] . '%');
|
}
|
if(isset($params['branch_ids'])&&$params['branch_ids']){
|
$model = $model->where('branch_id', 'in', $params['branch_ids']);
|
}
|
if(isset($params['is_takeout']) && $params['is_takeout'] > -1){
|
$model = $model->where('is_takeout', '=', $params['is_takeout']);
|
}
|
// 查询列表数据
|
return $model->with(['superUser', 'logo'])
|
->where('parent_branch_id', '=', $branch_id)
|
->where('is_delete', '=', '0')
|
->order(['create_time' => 'desc'])
|
->paginate($params);
|
}
|
|
/**
|
* 获取列表数据
|
*/
|
public static function getAll($params = [], $branch_id)
|
{
|
$model = new static();
|
if (!empty($params['branch_type'])) {
|
$model = $model->where('branch_type', '=', $params['branch_type']);
|
}
|
// 查询列表数据
|
return $model->field(['branch_id,name'])
|
->where('parent_branch_id', '=', $branch_id)
|
->where('is_delete', '=', '0')
|
->order(['create_time' => 'desc'])
|
->select();
|
}
|
|
/**
|
* 添加
|
*/
|
public function add($data, $branch_id)
|
{
|
// 开启事务
|
$this->startTrans();
|
try {
|
$branch = $data['branch'];
|
if (BranchModel::checkExist($branch['name'])) {
|
$this->error = '分会名称已存在,请更换';
|
return false;
|
}
|
if (BranchUserModel::checkExist($branch['user_name'])) {
|
$this->error = '登录账号已存在,请更换';
|
return false;
|
}
|
// $branch = $this->createData($branch);
|
// 添加分会
|
$branch['app_id'] = self::$app_id;
|
$branch['parent_branch_id'] = $branch_id;
|
$this->save($branch);
|
// 添加登录用户
|
$user_model = new BranchUserModel();
|
$user_model->save([
|
'user_id' => $branch['user_id'],
|
'user_name' => $branch['user_name'],
|
'password' => salt_hash($branch['password']),
|
'real_name' => $branch['link_name'],
|
'branch_id' => $this['branch_id'],
|
'is_super' => 1,
|
'app_id' => self::$app_id,
|
]);
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
/**
|
* 创建数据
|
*/
|
private function createData($data)
|
{
|
$data['app_id'] = self::$app_id;
|
|
return $data;
|
}
|
|
/**
|
* 修改
|
*/
|
public function edit($data)
|
{
|
// 开启事务
|
$this->startTrans();
|
try {
|
$branch = $data['branch'];
|
if ($this['superUser'] && $branch['user_name'] != $this['superUser']['user_name'] && BranchUserModel::checkExist($branch['user_name'])) {
|
$this->error = '登录账号已存在';
|
return false;
|
}
|
|
// 修改分会
|
$this->save($branch);
|
|
// 修改登录用户
|
$user_model = $this['superUser'];
|
$user_data = [
|
'user_id' => $branch['user_id'],
|
'user_name' => $branch['user_name']
|
];
|
if (isset($branch['password']) && !empty($branch['password'])) {
|
$user_data['password'] = salt_hash($branch['password']);
|
}
|
$user_model->save($user_data);
|
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
/**
|
* 软删除
|
*/
|
public function setDelete()
|
{
|
return $this->save(['is_delete' => 1]);
|
}
|
/**
|
* 开启禁止
|
*/
|
public function setRecycle($is_recycle)
|
{
|
// 开启事务
|
$this->startTrans();
|
try {
|
//更改分会状态
|
$this->save(['is_recycle' => $is_recycle]);
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
|
|
}
|
|
/**
|
*检查登录
|
*/
|
public function checkLogin($params)
|
{
|
$where['user_name'] = $params['username'];
|
$where['password'] = $params['password'];
|
$where['is_delete'] = 0;
|
|
if (!$branch = $this->where($where)->with(['app'])->find()) {
|
return false;
|
}
|
if (empty($branch['app'])) {
|
$this->error = '登录失败, 未找到应用信息';
|
return false;
|
}
|
if ($branch['app']['is_recycle']) {
|
$this->error = '登录失败, 当前应用已删除';
|
return false;
|
}
|
// 保存登录状态
|
$this->loginState($branch);
|
return true;
|
}
|
|
|
/*
|
* 修改密码
|
*/
|
public function editPass($data, $user)
|
{
|
$user_info = User::detail($user['branch_user_id']);
|
if ($data['password'] != $data['confirmPass']) {
|
$this->error = '密码错误';
|
return false;
|
}
|
if ($user_info['password'] != salt_hash($data['oldpass'])) {
|
$this->error = '两次密码不相同';
|
return false;
|
}
|
$date['password'] = salt_hash($data['password']);
|
$user_info->save($date);
|
return true;
|
}
|
|
/**
|
* 保存登录状态
|
*/
|
public function loginState($branch)
|
{
|
$app = $branch['app'];
|
// 保存登录状态
|
$session = array(
|
'branch' => [
|
'branch_id' => $branch['branch_id'],
|
'user_name' => $branch['user_name'],
|
'branch_name' => $branch['name']
|
],
|
'app' => $app->toArray(),
|
'is_login' => true,
|
);
|
session('jjjshop_branch', $session);
|
}
|
|
/**
|
* 修改
|
*/
|
// public function editBranch($data){
|
// $isexist = $this->where('name','=',$data['name'])->where('branch_id','<>',$data['branch_id'])->find();
|
// if($isexist){
|
// $this->error = '店铺名称已存在';
|
// return false;
|
// }
|
// return $this->save([
|
// 'link_name' => $data['link_name'],
|
// 'link_phone' => $data['link_phone'],
|
// 'address' => $data['address'],
|
// 'description' => $data['description'],
|
// 'logo_id' => $data['logo_id'],
|
// 'business_id' => $data['business_id'],
|
// 'app_id' => self::$app_id,
|
// 'name' => $data['name'],
|
// 'is_full' => 1,
|
// 'notice' => $data['notice']
|
// ]);
|
// }
|
|
/**
|
* 资金冻结
|
*/
|
public function freezeMoney($money)
|
{
|
return $this->save([
|
'money' => $this['money'] - $money,
|
'freeze_money' => $this['freeze_money'] + $money,
|
]);
|
}
|
|
|
}
|