<?php
|
|
namespace app\admin\controller;
|
|
use app\admin\validate\SupplierAccountAdd;
|
use app\admin\validate\SupplierAccountEdit;
|
use think\db\exception\DataNotFoundException;
|
use think\db\exception\DbException;
|
use think\db\exception\ModelNotFoundException;
|
use think\Exception;
|
use think\facade\Db;
|
use think\facade\View;
|
use think\facade\Request;
|
|
class Supplier extends Common {
|
|
public function index() {
|
////////
|
View::assign('menuitem', strtolower('supplier-index'));
|
|
$other['title'] = '供应商';
|
$guide['one']['text'] = '供应商管理';
|
$guide['two']['text'] = '供应商列表';
|
View::assign('guide', $guide);
|
|
|
View::assign('other', $other);
|
return View::fetch('index');
|
}
|
|
/**
|
* @throws ModelNotFoundException
|
* @throws DbException
|
* @throws DataNotFoundException
|
*/
|
public function supplier_list() {
|
if (Request::isPost()) {
|
$kw = input('kw');
|
$condition[] = ['initialize', '=', 1];
|
if (!empty($kw)) {
|
$condition[] = array('user_name|real_name', 'like', "%$kw%");
|
}
|
$p = input('p', 1);
|
$_GET['p'] = $p;
|
$other['page_size'] = input('page_size', 20);
|
$order = 'b.id asc';
|
|
$count = Db::name('supplier')
|
->alias('a')
|
->join('supplier_account b', 'a.id = b.supplier_id')
|
->where($condition)->count();
|
|
$list = Db::name('supplier')
|
->alias('a')
|
->join('supplier_account b', 'a.id = b.supplier_id')
|
->field("a.id,b.supplier_id,b.user_name,a.real_name,ban_access,a.phone,supplier_name,initialize,login_time,login_ip,a.end_time,a.start_time,a.audit_status")
|
->where($condition)
|
->order($order)
|
->page($p, $other['page_size'])
|
->select()
|
->toArray();
|
|
$other['count'] = $count;
|
if (!$list) {
|
$list = [];
|
} else {
|
foreach ($list as &$item) {
|
$item['endtime'] = date('Y-m-d', $item['end_time']);
|
}
|
}
|
$other['admin_info'] = $this->cinfo;
|
$d['code'] = 200;
|
$d['list'] = $list;
|
$d['other'] = $other;
|
return $d;
|
}
|
}
|
|
public function update_ban_access() {
|
header('Content-Type:text/html;charset=utf-8');
|
if (Request::isPost()) {
|
|
$id = input('id');
|
$sub_ban_access = input('ban_access');
|
$info = Db::name('supplier')->field('id')->where('id', $id)->find();
|
if (empty($info)) {
|
$d['code'] = 400;
|
$d['message'] = '信息不存在';
|
return $d;
|
}
|
///
|
|
if (is_numeric($id)) {
|
if ($sub_ban_access == '1') {
|
$dd['ban_access'] = 1;
|
$d['ban_access'] = 1;
|
$d['message'] = '允许登录';
|
} else {
|
$dd['ban_access'] = 0;
|
$d['ban_access'] = 0;
|
$d['message'] = '禁止登录';
|
}
|
$backval = Db::name('supplier')->where('id', $id)->save($dd);
|
if ($backval === FALSE) {
|
$d['code'] = 400;
|
} else {
|
$d['code'] = 200;
|
}
|
} else {
|
$d['code'] = 400;
|
}
|
} else {
|
$d['code'] = 400;
|
}
|
return $d;
|
}
|
|
|
public function setAuditStatus() {
|
header('Content-Type:text/html;charset=utf-8');
|
if (Request::isPost()) {
|
|
$id = input('id');
|
$audit_status = input('audit_status');
|
$info = Db::name('supplier')->field('id')->where('id', $id)->find();
|
if (empty($info)) {
|
$d['code'] = 400;
|
$d['message'] = '信息不存在';
|
return $d;
|
}
|
///
|
|
if (is_numeric($id)) {
|
if ($audit_status == '1') {
|
$dd['audit_status'] = 1;
|
$d['audit_status'] = 1;
|
$d['message'] = '通过审核';
|
} else {
|
$dd['audit_status'] = 0;
|
$d['audit_status'] = 0;
|
$d['message'] = '未审核';
|
}
|
$backval = Db::name('supplier')->where('id', $id)->save($dd);
|
if ($backval === FALSE) {
|
$d['code'] = 400;
|
} else {
|
$d['code'] = 200;
|
}
|
} else {
|
$d['code'] = 400;
|
}
|
} else {
|
$d['code'] = 400;
|
}
|
return $d;
|
}
|
/**
|
* 保存供应商信息(废弃)
|
* @return array
|
*/
|
public function save_supplier() {
|
if (Request::isPost()) {
|
header('Content-Type:text/html;charset=utf-8');
|
$data = Request::post();
|
$id = input('id');
|
$password = input('password');
|
if (empty($password)) {
|
} else {
|
$data['password'] = joinmd5($password);
|
}
|
|
if (is_numeric($id)) {//更新数据
|
$supplier_id = input('supplier_id');
|
/////验证密码复杂度///////////
|
$validate = new SupplierAccountEdit();
|
$result = $validate->scene('edit')->check($data);
|
if (!$result) {
|
$d['code'] = 400;
|
$d['message'] = $validate->getError();
|
return $d;
|
}
|
//////保存供应商信息////////////
|
$supplier_data['supplier_name'] = $data['supplier_name'];
|
$supplier_data['real_name'] = $data['real_name'];
|
$supplier_data['phone'] = $data['phone'];
|
$supplier_data['end_time'] = strtotime($data['endtime']);
|
Db::name('supplier')->where('id', $supplier_id)->save($supplier_data);
|
////////////////////////
|
if (!empty($password)) {
|
/////修改密码////////////////
|
$supplier_account_data['password'] = $data['password'];
|
Db::name('supplier_account')->where('id', $id)->save($supplier_account_data);
|
}
|
$d['code'] = 200;
|
$d['end_time'] = strtotime($data['endtime']);
|
$d['message'] = '编辑成功';
|
} else {
|
unset($data['id']);
|
$validate = new SupplierAccountAdd();
|
$result = $validate->check($data);
|
if (!$result) {
|
$d['code'] = 400;
|
$d['message'] = $validate->getError();
|
return $d;
|
}
|
//////////////////////
|
|
Db::startTrans();
|
try {
|
$end_time = getcurrdate() + 3600 * 24 * 30;
|
$supplier_data['phone'] = $data['phone'];
|
$supplier_data['supplier_name'] = $data['supplier_name'];
|
$supplier_data['real_name'] = $data['real_name'];
|
$supplier_data['add_time'] = time();
|
$supplier_data['start_time'] = time();
|
$supplier_data['end_time'] = $end_time;
|
$supplier_id = Db::name('supplier')->insertGetId($supplier_data);
|
/////--------------------////
|
$supplier_account_data['supplier_id'] = $supplier_id;
|
$supplier_account_data['user_name'] = $data['user_name'];
|
$supplier_account_data['password'] = $data['password'];
|
$supplier_account_data['phone'] = $data['phone'];
|
$supplier_account_data['real_name'] = $data['real_name'];
|
$supplier_account_data['add_time'] = time();
|
$supplier_account_data['initialize'] = 1;
|
$supplier_account_id = Db::name('supplier_account')->insertGetId($supplier_account_data);
|
if ($supplier_id && $supplier_account_id) {
|
///////执行成功,提交事务///////
|
Db::commit();
|
$d['code'] = 200;
|
$d['message'] = '供应商添加成功';
|
} else {
|
//任一执行失败,执行回滚操作,相当于均不执行
|
Db::rollback();
|
$d['code'] = 400;
|
$d['message'] = '供应商添加失败';
|
}
|
|
} catch (\Exception $e) {
|
// 回滚事务
|
Db::rollback();
|
$d['code'] = 400;
|
$d['message'] = '供应商添加失败';
|
}
|
}
|
} else {
|
$d['code'] = 400;
|
$d['message'] = '非法请求';
|
}
|
return $d;
|
}
|
|
public function delete_admin() {
|
if (Request::isPost()) {
|
$id = input('id');
|
if (is_numeric($id)) {
|
$condition['id'] = $id;
|
$info = Db::name('administrators')->field('id,initialize')->where($condition)->find();
|
if ($info) {
|
///////////////////
|
if ($info['initialize'] == '1') {
|
$d['code'] = 400;
|
$d['message'] = '该账号不能删除!';
|
return $d;
|
}
|
if ($this->cinfo['initialize'] == 1 || $this->admin_id == $info['id']) {
|
Db::name('administrators')->where('id', $id)->delete();
|
/////////////////////////////
|
$d['code'] = 200;
|
$d['message'] = '删除成功!';
|
} else {
|
$d['code'] = 400;
|
$d['message'] = '您没有删除权限';
|
}
|
} else {
|
$d['code'] = 400;
|
$d['message'] = '子账号不存在';
|
}
|
} else {
|
$d['code'] = 400;
|
$d['message'] = '参数有误!';
|
}
|
return $d;
|
}
|
}
|
|
/**
|
* 供应商编辑页
|
* @param int $id
|
* @return string
|
*/
|
public function add(int $id = 0): string {
|
View::assign('menuitem', strtolower('supplier-index'));
|
if ($id && is_numeric($id)) {
|
$other['title'] = '编辑供应商信息';
|
} else {
|
$other['title'] = '添加供应商信息';
|
}
|
$guide['one']['text'] = '供应商管理';
|
$guide['two']['text'] = $other['title'];
|
|
$other['id'] = $id;
|
View::assign('guide', $guide);
|
View::assign('other', $other);
|
return View::fetch('add');
|
}
|
|
/**
|
* 获取供应商详细信息
|
* @return array
|
*/
|
public function get_supplier_info(): array {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception("请求方式有误");
|
}
|
$supplier_id = input('id');
|
if (!$supplier_id || !is_numeric($supplier_id)) {
|
throw new Exception("参数有误");
|
}
|
$where = [
|
'a.id' => $supplier_id
|
];
|
$supplier_info = Db::name('supplier')
|
->alias("a")
|
->field("a.*,b.id as supplier_account_id,b.user_name,b.sub_ban_access,b.phone as account_phone,b.real_name as account_name")
|
->join("web_supplier_account b", "b.supplier_id = a.id and b.initialize = 1")
|
->where($where)
|
->find();
|
if (!$supplier_info) {
|
throw new Exception("供应商信息有误");
|
}
|
// $supplier_info['sub_ban_access'] = (bool)$supplier_info['sub_ban_access'];
|
$supplier_info['end_time'] = date("Y-m-d", $supplier_info['end_time']);
|
|
$result = [
|
'code' => 200,
|
'message' => "成功",
|
'data' => $supplier_info
|
];
|
} catch (Exception $exc) {
|
$result = [
|
'code' => $exc->getCode(),
|
'message' => $exc->getMessage()
|
];
|
}
|
return $result;
|
}
|
|
/**
|
* 保存供应商信息
|
* @return array
|
*/
|
public function save_supplier_info(): array {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception("请求方式有误");
|
}
|
$params = Request::param();
|
if (!$params['supplier_name']) {
|
throw new Exception("请填写供应商名称");
|
}
|
if (!$params['user_name']) {
|
throw new Exception("请填写供应商账号");
|
}
|
if (!$params['account_name']) {
|
throw new Exception("请填写供应商联系人姓名");
|
}
|
if (!$params['account_phone']) {
|
throw new Exception("请填写供应商联系人手机号");
|
}
|
|
// dump($params['sub_ban_access']);exit;
|
//
|
// if ($params['sub_ban_access']) {
|
// $params['sub_ban_access'] = 1;
|
// } else {
|
// $params['sub_ban_access'] = 0;
|
// }
|
|
Db::startTrans();
|
if ($params['id']) {
|
/////更新/////
|
$where = [
|
'id' => $params['id']
|
];
|
$supplier_info = Db::name('supplier')->where($where)->find();
|
if (!$supplier_info) {
|
throw new Exception("供应商信息有误");
|
}
|
/////更新供应商信息/////
|
$update_supplier_data = [
|
'supplier_name' => $params['supplier_name'],/////供应商名称/////
|
'supplier_en_name' => $params['supplier_en_name'],/////供应商英文名称/////
|
'real_name' => $params['real_name'],/////供应商联系人姓名/////
|
'phone' => $params['phone'],/////供应商联系人手机/////
|
'email' => $params['email'],/////供应商联系人邮箱/////
|
'phone_number' => $params['phone_number'],/////供应商联系人座机/////
|
'unique_code' => $params['unique_code'],/////供应商唯一代码。营业执照上的统一代码/////
|
'legal_person_name' => $params['legal_person_name'],/////供应商法人/////
|
'legal_person_id_number' => $params['legal_person_id_number'],/////供应商法人身份证号/////
|
'address' => $params['address'],/////供应商地址/////
|
'postal_code' => $params['postal_code'],/////邮政编码/////
|
'end_time' => strtotime($params['end_time']) + 86400 - 1,/////到期时间/////
|
'ban_access' => $params['sub_ban_access'],/////账号状态/////
|
'account_executive' => $params['account_executive'],/////业务经理/////
|
'account_executive_phone' => $params['account_executive_phone'],///业务经理联系方式////
|
];
|
$update_supplier_result = Db::name('supplier')->where($where)->update($update_supplier_data);
|
if ($update_supplier_result === FALSE) {
|
throw new Exception("更新供应商信息失败");
|
}
|
|
/////更新供应商主账号信息/////
|
$supplier_account_where = [
|
'id' => $params['supplier_account_id'],
|
'supplier_id' => $params['id'],
|
];
|
$supplier_account_info = Db::name('supplier_account')->where($supplier_account_where)->find();
|
if (!$supplier_account_info) {
|
throw new Exception("供应商主账号信息有误");
|
}
|
|
if ($params['user_name'] != $supplier_account_info['user_name']) {
|
/////账号有修改,检查新账号是否存在/////
|
$check_supplier_account_where = [
|
'user_name' => $params['user_name'],
|
];
|
$check_supplier_account_result = Db::name('supplier_account')->where($check_supplier_account_where)->find();
|
if ($check_supplier_account_result) {
|
throw new Exception("此账号已存在,请换一个账号");
|
}
|
}
|
$updae_supplier_account_data = [
|
'user_name' => $params['user_name'],
|
'real_name' => $params['account_name'],
|
'phone' => $params['account_phone'],
|
'sub_ban_access' => $params['sub_ban_access'],/////账号状态/////
|
];
|
if ($params['password']) {
|
$updae_supplier_account_data['password'] = joinmd5($params['password']);
|
}
|
$update_supplier_account_result = Db::name('supplier_account')->where($supplier_account_where)->data($updae_supplier_account_data)->update();
|
if ($update_supplier_account_result === FALSE) {
|
throw new Exception("更新供应商主账号信息失败");
|
}
|
$jump_url = "";
|
} else {
|
/////添加/////
|
|
/////1、先添加供应商/////
|
$add_supplier_data = [
|
'supplier_name' => $params['supplier_name'],
|
'real_name' => $params['account_name'],
|
'phone' => $params['account_phone'],
|
'start_time' => time(),
|
'end_time' => strtotime($params['end_time']) + 86400 - 1,
|
'ban_access' => $params['sub_ban_access'] ? 1 : 0,
|
'account_executive' => $params['account_executive'],/////业务经理/////
|
'account_executive_phone' => $params['account_executive_phone'],///业务经理联系方式////
|
'add_time' => time()
|
];
|
$add_supplier_result = Db::name('supplier')->insertGetId($add_supplier_data);
|
if (!$add_supplier_result) {
|
throw new Exception("添加供应商失败");
|
}
|
/////2、再添加供应商账号/////
|
$check_user_name_where = [
|
'user_name' => $params['user_name'],
|
];
|
if (Db::name('supplier_account')->where($check_user_name_where)->find()) {
|
throw new Exception("账号已存在,请换一个账号");
|
}
|
|
$add_supplier_account_data = [
|
'supplier_id' => $add_supplier_result,
|
'user_name' => $params['user_name'],
|
'password' => joinmd5($params['password']),
|
'real_name' => $params['account_name'],
|
'phone' => $params['account_phone'],
|
'initialize' => 1,
|
'sub_ban_access' => $params['sub_ban_access'] ? 1 : 0,
|
'add_time' => time()
|
];
|
$add_supplier_account_result = Db::name('supplier_account')->insertGetId($add_supplier_account_data);
|
if (!$add_supplier_account_result) {
|
throw new Exception("添加供应商账号失败");
|
}
|
$jump_url = url('admin/supplier/add/id/' . $add_supplier_result);
|
}
|
|
Db::commit();
|
$result = [
|
'code' => 200,
|
'message' => "保存成功",
|
'jump_url' => $jump_url
|
];
|
} catch (Exception $exc) {
|
Db::rollback();
|
$result = [
|
'code' => $exc->getCode(),
|
'message' => $exc->getMessage()
|
];
|
}
|
return $result;
|
}
|
|
}
|