Admin.php 19.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */

namespace app\manager\controller;

use think\Page;
use think\Verify;
use think\Db;
use think\Session;

class Admin extends Base
{

    /********************************************商家模块**************************************************************/
    /**
     * 获取商家列表
     * @return store_list
     */
    public function index()
    {
        $list = array();
        $keywords = I('keywords/s');
        $is_audit =  I('is_audit/s');
        if (empty($keywords)) {
            $res = D('store')->select();
        } else {
            $res = DB::name('store')->where('store_name', 'like', '%' . $keywords . '%')
                ->whereOr('ERPId', 'like', '%' . $keywords . '%')
                ->order('store_id')->select();
        }
        if ($is_audit !=""){
            $res = DB::name('store')->where('is_audit=0')
                ->order('store_id')->select();
        }
        foreach ($res as $val) {
            $val['add_time'] = date('Y-m-d H:i:s',$val['BillDate']);
            $list[] = $val;
        }

        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 商家管理
     * @return admin_list
     */
    public function store_info()
    {
        $store_id = I('get.store_id/d', 0);
        if ($store_id) {
            $info = D('store')->where("store_id", $store_id)->find();
            $info['BillDate'] = date('Y-m-d H:i:s',$info['BillDate']);
            $this->assign('info', $info);
            $province = M('region')->where(array('parent_id' => 0))->select();
            $city = M('region')->where(array('parent_id' => $info['province']))->select();
            $area = M('region')->where(array('parent_id' => $info['city']))->select();
            $this->assign('province', $province);
            $this->assign('city', $city);
            $this->assign('area', $area);
        } else {
            $province = M('region')->where(array('parent_id' => 0))->select();
            $this->assign('province', $province);
        }
        $act = empty($store_id) ? 'add' : 'edit';
        $this->assign('act', $act);
        return $this->fetch();
    }

    /**
     * 商家操作,新增,修改
     * @return admin_list
     */
    public function storeHandle()
    {
        $data = I('post.');
        //新增
        if ($data['act'] == 'add') {
            unset($data['store_id']);
            //$data['add_time'] = time();
            if (D('store')->where("ERPId", $data['ERPId'])->count()) {
                $this->error("此帐套已存在,请更换", U('manager/Admin/store_info'));
            } else {
                //添加商家
                $SupplyId = uuid();
                $data['BillDate'] = time();
                $data['BillIp'] = getIP();
                $data['supplyid'] = $SupplyId;
                $r = D('store')->add($data);   //新增的商家ID
                ///后续操作
                if ($r) {
                    //添加admin角色
                    $role['store_id'] = $r;
                    $role['role_name'] = '超级管理员';
                    $role['act_list'] = 'all';
                    $role['role_desc'] = '管理全站';
                    $ro = D('role')->add($role);        //新增的角色ID
                    if ($ro) {
                        //添加用户
                        $res = DB::name('store')->field('ERPId')->where('store_id', '=', $r)->select();   //获取ERP账套
                        $admin['role_id'] = $ro;
                        $admin['store_id'] = $r;
                        $admin['supplyid'] = $SupplyId;
                        $admin['ERPId'] = $res[0]['ERPId'];
                        $admin['ERPUser'] = 'admin';
                        $admin['ERPName'] = '超级管理员';
                        $admin['BillIp'] = getIP();
                        $admin['BillDate'] = time();
                        $a = D('admin')->add($admin);
                        if (!$a) {
                            $this->error("添加成功,用户插入失败", U('manager/Admin/index'));
                        }
                    } else {
                        $this->error("添加成功,插入失败", U('manager/Admin/index'));
                    }
                }
                //操作日志
                adminLog("新增商家");
            }
        }
        //修改
        if ($data['act'] == 'edit') {
            $store = D('store')->where("store_id",$data['store_id'])->find();
            if ($store['ERPId'] != $data['ERPId']){
                if (D('store')->where("ERPId", $data['ERPId'])->count()) {
                    $this->error("此ERP帐套已存在,请更换", U('manager/Admin/store_info',array('store_id'=>$data['store_id'])));
                }
            }

            $data['EditDate'] = time();
            $data['EditIp'] = getIP();
            $data['BillDate'] = strtotime($data['BillDate']);
            $r = D('store')->where('store_id', $data['store_id'])->save($data);
            adminLog("修改商家");
        }
        //删除
        if ($data['act'] == 'del' && $data['store_id'] > 1) {
            $store = DB::name('store')->where('store_id', '=', $data['store_id'])->select();
            //删除图片
            if($store[0]['store_logo']!=""){
                $dellogo=ltrim($store[0]['store_logo'],'/');
                mdelFile(ROOT_PATH.$dellogo);
            }

            $r = D('store')->where('store_id', $data['store_id'])->delete();
            $r = D('admin')->where('store_id', $data['store_id'])->delete();
            $r = D('role')->where('store_id', $data['store_id'])->delete();
            adminLog("删除商家");
            exit(json_encode(1));
        }

        if ($r) {
            $this->success("操作成功", U('manager/Admin/index'));
        } else {
            $this->error("操作失败", U('manager/Admin/index'));
        }
    }

    /**
     * 删除未保存的商家logo
     * @return store_list
     */
    public function del_logo()
    {
        $store_logo = I('store_logo/s');
        return mdelFile(ROOT_PATH.$store_logo);
    }
    /********************************************商家用户模块**********************************************************/
    /**
     * 获取商家用户列表
     * @return admin_list
     */
    public function store_admin_list()
    {
        $store_id = I('get.store_id/d', 0);
        $keywords = I('keywords/s');
        if (!$store_id) {
            $this->error("请选择商家", U('manager/Admin/index'));
        }
        if (empty($keywords)) {
            $res = DB::name('admin')->where('store_id', '=', $store_id)->order('admin_id')->select();
        } else {
            $res = DB::name('admin')->where('store_id', '=', $store_id)->where('user_name', 'like', '%' . $keywords . '%')->order('admin_id')->select();
        }
        $list = array();
        //$res = DB::name('admin')->where('store_id','=',$store_id)->order('admin_id')->select();
        foreach ($res as $val) {
            $val['add_time'] = date('Y-m-d H:i:s',$val['BillDate']);
            $list[] = $val;
        }
        $this->assign('list', $list);
        $this->assign('store_id', $store_id);
        return $this->fetch();
    }

    /**
     * 商家用户管理
     * @return admin_list
     */
    public function admin_info()
    {
        $admin_id = I('get.admin_id/d', 0);
        $store_id = I('get.store_id/d', 0);
        if ($admin_id) {
            $info = D('admin')->where("admin_id", $admin_id)->find();
            $this->assign('info', $info);
        }
        else{
            $info = D('store')->where("store_id", $store_id)->find();
            $this->assign('info', $info);
        }
        $act = empty($admin_id) ? 'add' : 'edit';
        $this->assign('act', $act);
        $role = D('role')->where('store_id', '=', $store_id)->select();
        $this->assign('role', $role);
        $this->assign('store_id', $store_id);
        return $this->fetch();

        return $this->fetch();
    }

    /**
     * 商家用户操作,新增,修改
     * @return admin_list
     */
    public function adminHandle()
    {
        $data = I('post.');
        if ($data['act'] == 'add') {
            unset($data['admin_id']);
            if (D('admin')->where(array('ERPUser'=> $data['ERPUser'],'ERPId'=> $data['ERPId']))->count()) {
                $this->error("此用户名已被注册,请更换", U('manager/Admin/admin_info'));
            } else {
                $SupplyId = DB::name('store')->field('supplyid')->where('store_id', '=', $data['store_id'])->select();
                $data['supplyid'] = $SupplyId[0]['supplyid'];
                $data['BillIp'] = getIP();
                $data['BillDate'] = time();
                $r = D('admin')->add($data);
                if($r){
                    adminLog("添加用户(" . $r . ")");
                }
            }
        }

        if ($data['act'] == 'edit') {
            $data['EditDate'] = time();
            $data['EditIp'] = getIP();
            $r = D('admin')->where('admin_id', $data['admin_id'])->save($data);
            if($r){
                adminLog("修改用户(" . $data['admin_id'] . ")");
            }
        }

        if ($data['act'] == 'del' && $data['admin_id'] > 1) {
            $r = D('admin')->where('admin_id', $data['admin_id'])->delete();
            if($r){
                adminLog("删除用户");
                exit(json_encode(1));
            }
        }

        if ($r) {
            $this->success("操作成功", U('manager/Admin/store_admin_list', 'store_id=' . $data['store_id']));
        } else {
            $this->error("操作失败", U('manager/Admin/store_admin_list', 'store_id=' . $data['store_id']));
        }
    }

    //审核操作
    public function changer_audit()
    {
        $type = I('type');
        $store_id = I('store_id');
        $data['is_audit'] = $type;
        $r = D('store')->where('store_id', $store_id)->save($data);
        if($r){
            exit(json_encode(array('status'=>1,'msg'=>'操作成功')));
        }
        else{
            exit(json_encode(array('status'=>-1,'msg'=>'操作失败')));
        }
    }


    /********************************************微信公众号模块********************************************************/

    /**
     * 微信公众号设置
     * @return \think\mixed
     */
    public function wx_setting(){
        $store_id = I('get.store_id');
        $wechat = M('wx_user')->where(array('store_id'=>$store_id))->find();
        if(IS_POST){
            $post_data = input('post.');
            //如果还没配置,则添加配置
            if(!$wechat){
                $post_data['store_id']=$store_id;
                $r = D('wx_user')->add($post_data);
                if($r){
                    adminLog("配置公众号(".$store_id.")");
                    $this->success("操作成功", U('Admin/index'));
                }
                else{
                    $this->success("操作失败", U('Admin/index'));
                }
            }
            else{
                //发送HTTP状态  先不用
                //$func = 'send_ht';
                //call_user_func($func.'tp_status','310');
                $row = M('wx_user')->where(array('store_id'=>$store_id))->update($post_data);
                if($row){
                    adminLog("配置公众号(".$store_id.")");
                    $this->success("操作成功", U('Admin/index'));
                }
                else{
                    $this->success("操作失败", U('Admin/index'));
                }
//                $row && exit($this->success("修改成功"));
//                exit($this->error("修改失败"));
            }
        }
        else{
            //URL地址
            $apiurl = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?m=Home&c=Weixin&a=index&store_id='.$store_id;
            $this->assign('wechat',$wechat);
            $this->assign('apiurl',$apiurl);
            return $this->fetch();
        }
    }


    /********************************************商家权限资源模块******************************************************/

    /**
     * 商家权限资源列表
     * @return \think\mixed
     */
    function right_list(){
        $group = array('system'=>'设置','ad'=>'广告','article'=>'文章','power'=>'权限',
            'goods'=>'商品','order'=>'订单','promotion'=>'促销','distribut'=>'分销','wechat'=>'微信','report'=>'统计'
        );
        $keywords = I('name/s');
        if(empty($keywords)){
            $right_list = M('system_menu')->select();
        }else{
            $right_list = DB::name('system_menu')->where('name','like','%'.$keywords.'%')->select();
        }
        $this->assign('right_list',$right_list);
        $this->assign('group',$group);
        return $this->fetch();
    }

    /**
     * 修改、新增商家权限资源表
     * @return \think\mixed
     */
    public function edit_right(){
        if(IS_POST){
            $data = I('post.');
            $data['right'] = implode(',',$data['right']);
            if(!empty($data['id'])){
                M('system_menu')->where(array('id'=>$data['id']))->save($data);
            }else{
                if(M('system_menu')->where(array('name'=>$data['name']))->count()>0){
                    $this->error('该权限名称已添加,请检查',U('Admin/right_list'));
                }
                unset($data['id']);
                M('system_menu')->add($data);
            }
            $this->success('操作成功',U('Admin/right_list'));
            exit;
        }
        $id = I('id');
        if($id){
            $info = M('system_menu')->where(array('id'=>$id))->find();
            $info['right'] = explode(',', $info['right']);
            $this->assign('info',$info);
        }
        $group = array('system'=>'设置','ad'=>'广告','article'=>'文章','power'=>'权限',
            'goods'=>'商品','order'=>'订单','promotion'=>'促销','distribut'=>'分销','wechat'=>'微信','report'=>'统计'
        );
        $planPath = APP_PATH.'Admin/Controller';
        $planList = array();
        $dirRes   = opendir($planPath);
        while($dir = readdir($dirRes))
        {
            if(!in_array($dir,array('.','..','.svn')))
            {
                $planList[] = basename($dir,'.class.php');
            }
        }
        $this->assign('planList',$planList);
        $this->assign('group',$group);
        return $this->fetch();
    }

    /**
     * 删除商家权限资源
     * @return \think\mixed
     */
    public function right_del(){
        $id = I('del_id');
        if(is_array($id)){
            $id = implode(',', $id);
        }
        if(!empty($id)){
            $r = M('system_menu')->where("id in ($id)")->delete();
            if($r){
                respose(1);
            }else{
                respose('删除失败');
            }
        }else{
            respose('参数有误');
        }
    }

    /**
     * 获取商家控制器下的方法
     * @return \think\mixed
     */
    function ajax_get_action()
    {
        $control = I('controller');
        $advContrl = get_class_methods("app\\Admin\\controller\\".str_replace('.php','',$control));
        dump($advContrl);
        $baseContrl = get_class_methods('app\Admin\controller\Base');
        $diffArray  = array_diff($advContrl,$baseContrl);
        $html = '';
        foreach ($diffArray as $val){
            $html .= "<option value='".$val."'>".$val."</option>";
        }
        exit($html);
    }


    /********************************************商家短信模块******************************************************/


    /**
     * 商家短信
     * @return \think\mixed
     */
    function store_sms()
    {
        $list = array();
        $keywords = I('keywords/s');
        if (empty($keywords)) {
            $res = D('store')->select();
        } else {
            $res = DB::name('store')->where('store_name', 'like', '%' . $keywords . '%')
                ->whereOr('ERPId', 'like', '%' . $keywords . '%')
                ->order('store_id')->select();
        }
        foreach ($res as $val) {
            $val['add_time'] = date('Y-m-d H:i:s',$val['BillDate']);
            $list[] = $val;
        }
        $this->assign('list', $list);
        return $this->fetch();

    }

    //短信充值
    function ajax_Sms_Recharge(){
        $qty = I('qty');
        $store_id = I('store_id');
        //更新商家短信数量
        $store = M('store')->where('store_id',$store_id)->find();
        $smsqty = $store['smsqty'];
        $smsqty = $smsqty + $qty;
        $store['smsqty'] = $smsqty;
        M('store')->where('store_id',$store_id)->update($store);
        //插入到短信操作记录表
        smsHandle($store_id,"短信充值");
        //插入短信充值记录表
        $data['store_id'] = $store_id;
        $data['qty'] = $qty;
        $data['add_time'] = time();
        $data['manager_id'] = session('manager_id');
        $data['add_ip'] = getIP();
        $r = D('sms_addlist')->add($data);
        if($r){
            exit(json_encode(array('status'=>1,'msg'=>'充值成功')));
        }
        else{
            exit(json_encode(array('status'=>-1,'msg'=>'充值失败')));
        }
    }


    function store_sms_recharge()
    {
        $list = array();
        $keywords = I('keywords/s');
        $store_id = I('store_id/d');
//        if (empty($keywords)) {
//            $res = D('sms_addlist')->select();
//        } else {
//            $res = DB::name('sms_addlist')->where('store_id', '=', $store_id)
//                ->whereOr('ERPId', 'like', '%' . $keywords . '%')
//                ->order('store_id')->select();
//        }
//        $res = D('sms_addlist')->where('store_id',$store_id)->order('add_time desc')->select();
        $res = D('sms_addlist')->alias('a')->join("wxd_manager_admin b","a.manager_id=b.manager_id")->field("a.*,b.manager_name")->where("store_id", $store_id)->order('add_time desc')->select();
        foreach ($res as $val) {
            $val['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
            $list[] = $val;
        }
        $list['store_id'] = $store_id;
        $this->assign('list', $list);
        return $this->fetch();

    }

    function store_sms_log()
    {
        $list = array();
        $keywords = I('keywords/s');
        $store_id = I('store_id/d');
//        if (empty($keywords)) {
//            $res = D('sms_addlist')->select();
//        } else {
//            $res = DB::name('sms_addlist')->where('store_id', '=', $store_id)
//                ->whereOr('ERPId', 'like', '%' . $keywords . '%')
//                ->order('store_id')->select();
//        }
        $res = D('sms_inoutlist')->where('store_id',$store_id)->order('add_time desc')->select();
        foreach ($res as $val) {
            $val['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
            $list[] = $val;
        }
        $list['store_id'] = $store_id;
        $this->assign('list', $list);
        return $this->fetch();

    }

}