审查视图

app/portal/controller/PersonalcenterController.php 33.6 KB
anyv authored
1 2 3 4 5 6 7 8 9 10 11
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\WeChatBaseController;
anyv authored
12
use EasyWeChat\Foundation\Application;
anyv authored
13
use think\Db;
anyv authored
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

class sendAPI {
    public $data;	//发送数据
    public $timeout = 30; //超时
    private $apiUrl;	//发送地址
    private $username;	//用户名
    private $password;	//密码

    function __construct($url, $username, $password) {
        $this->apiUrl 	= $url;
        $this->username = $username;
        $this->password = $password;
    }

    private function httpGet() {
        $url = $this->apiUrl . '?' . http_build_query($this->data);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Error GET '.curl_error($curl);
        }
        curl_close($curl);
        return $res;
    }

    private function httpPost(){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $this->apiUrl); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS,  http_build_query($this->data)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $result = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Error POST'.curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        return $result; // 返回数据
    }

    /**
     * @param $type|提交类型 POST/GET
     * @param $isTranscoding|是否需要转 $isTranscoding 是否需要转utf-8 默认 false
     * @return mixed
     */
    public function sendSMS($type, $isTranscoding = false) {
        $this->data['content'] 	= $isTranscoding === true ? mb_convert_encoding($this->data['content'], "UTF-8") : $this->data['content'];
        $this->data['username'] = $this->username;
        $this->data['tkey'] 	= date('YmdHis');
        $this->data['password'] = md5(md5($this->password) . $this->data['tkey']);
        return  $type == "POST" ? $this->httpPost() : $this->httpGet();
    }

}
anyv authored
77 78 79 80 81 82

class PersonalcenterController extends WeChatBaseController{

    /**
     * 显示个人中心页
     */
4  
anyv authored
83
    public function personal_center(){
5  
anyv authored
84
5  
anyv authored
85 86
        $uid = cmf_get_current_user_id();
        $my_user_status = Db::name('my_user') -> where('uid',$uid) -> find();
anyv authored
87 88
        $weixin = Db::name('user') -> where('id',$uid) -> find();
        $this -> assign('weixin',$weixin);
5  
anyv authored
89
        if($my_user_status['status'] == 0 || $my_user_status['status'] == 1 || $my_user_status['status'] == 3 || $my_user_status['status'] == 4 || $my_user_status['status'] == 5 || $my_user_status['status'] == 6){
5  
anyv authored
90 91 92
            if($my_user_status['status'] == 0){
                $this -> assign('status',0);
            }
5  
anyv authored
93
            if($my_user_status['status'] == 1 || $my_user_status['status'] == 5 || $my_user_status['status'] == 6){
5  
anyv authored
94 95 96 97 98 99 100 101 102 103
                $this -> assign('status',1);
            }
            if($my_user_status['status'] == 3){
                $this -> assign('status',3);
            }
            if($my_user_status['status'] == 4){
                $this -> assign('status',4);
            }
            return $this -> fetch();
        }
5  
anyv authored
104
        //业务员个人中心页
5  
anyv authored
105
        if($my_user_status['status'] == 2){
1  
anyv authored
106
            $this -> assign('weixin',$weixin);
5  
anyv authored
107
            return $this -> fetch('personalcenter/salesman_center');
5  
anyv authored
108
        }
5  
anyv authored
109
8  
anyv authored
110
    }
5  
anyv authored
111
8  
anyv authored
112 113 114 115
    /**
     * 完善个人信息页
     */
    public function perfect_information(){
5  
anyv authored
116
8  
anyv authored
117
        $uid = cmf_get_current_user_id();
5  
anyv authored
118
        $my_user_status = Db::name('my_user') -> where('uid',$uid) -> find();
5  
anyv authored
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        if($my_user_status['status'] == 2){
            $this -> redirect('Personalcenter/personal_center');
        }else{
            if($my_user_status['status'] == 1){
                $this -> assign('status',1);
            }
            if($my_user_status['status'] == 5){
                $this -> assign('status',5);
            }
            if($my_user_status['status'] == 6){
                $this -> assign('status',6);
            }
            $weixin = Db::name('user') -> where('id',$uid) -> find();
            $this -> assign('weixin',$weixin);
            return $this -> fetch();
5  
anyv authored
134
        }
5  
anyv authored
135
anyv authored
136 137
    }
8  
anyv authored
138
7  
anyv authored
139 140 141 142 143
    /**
     * 我的收藏页
     */
    public function personal_collect(){
4  
anyv authored
144
        $uid = cmf_get_current_user_id();
anyv authored
145
        $data = Db::name('collect') -> where("uid =".$uid) -> select() -> toArray();
5  
anyv authored
146
4  
anyv authored
147 148
        if(!empty($data)){
            foreach($data as $key => $val){
5  
anyv authored
149 150 151 152
                 $collect_goods = Db::name('goods') -> where("id =".$val['goods_id']) -> where("is_out = 1") -> find();
                if($collect_goods != null){
                    $data_goods[] =  $collect_goods;
                }
4  
anyv authored
153
            }
5  
anyv authored
154 155 156 157 158 159 160 161 162 163 164
            if(!empty($data_goods)){
                foreach ($data_goods as $key => $val){
                    $price = explode('.',$val['price']);
                    $pricing = explode('.',$val['pricing']);
                    $data_goods[$key]['price0'] = $price[0];
                    $data_goods[$key]['price1'] = $price[1];
                    $data_goods[$key]['pricing0'] = $pricing[0];
                    $data_goods[$key]['pricing1'] = $pricing[1];
                }
            }else{
                $data_goods = '';
8  
anyv authored
165
            }
4  
anyv authored
166 167
        }else{
            $data_goods = '';
4  
anyv authored
168
        }
5  
anyv authored
169 170 171 172 173
        if(!empty($data_goods)){
            $data_count = count($data_goods);
        }else{
            $data_count = 0;
        }
5  
anyv authored
174
        $this -> assign('data_count',$data_count);
8  
anyv authored
175
        $this -> assign('data_goods',$data_goods);
7  
anyv authored
176 177 178 179
        return $this -> fetch();

    }
5  
anyv authored
180 181 182 183 184
    /**
     * 填写个人信息页
     */
    public function add_information(){
anyv authored
185 186 187 188 189
        $options=config('wechat_config');
        $app = new Application($options);
        $js = $app->js;
        $jssdk=$js->config(array('chooseImage','uploadImage'), false,false,true);
        $this->assign('jssdk',$jssdk);
5  
anyv authored
190 191 192
        return $this -> fetch();

    }
7  
anyv authored
193
anyv authored
194
4  
anyv authored
195 196 197 198 199
    /**
     * 浏览记录
     */
    public function browsing_history(){
5  
anyv authored
200
        $uid = cmf_get_current_user_id();
5  
anyv authored
201
        $data = Db::name('browsing_history') -> where("uid =".$uid) -> select();
5  
anyv authored
202
        foreach($data as $key => $val){
5  
anyv authored
203 204 205 206
            $browsing_history_goods = Db::name('goods') -> where('id',$val['goods_id']) -> where("is_out = 1") -> find();
            if($browsing_history_goods != null){
                $data_goods[] = $browsing_history_goods;
            }
5  
anyv authored
207
        }
5  
anyv authored
208 209 210 211 212 213 214 215
        if(!empty($data_goods)){
            foreach($data_goods as $key => $val){
                $price = explode('.',$val['price']);
                $classification_name = Db::name('classification') -> where('id',$val['classify_id']) -> find();
                $data_goods[$key]['classification_name'] = $classification_name['name'];
                $data_goods[$key]['price0'] = $price[0];
                $data_goods[$key]['price1'] = $price[1];
            }
anyv authored
216 217
        }else{
            $data_goods = '';
4  
anyv authored
218
        }
5  
anyv authored
219
        $this -> assign('data_goods',$data_goods);
4  
anyv authored
220 221 222
        return $this -> fetch();

    }
7  
anyv authored
223
5  
anyv authored
224 225 226 227 228
    /**
     * 收货地址页
     */
    public function shop_address(){
anyv authored
229 230
        $uid = cmf_get_current_user_id();
        $data = Db::name('address') -> where("delete_time = 0 and uid =".$uid) ->  select() -> toArray();
5  
anyv authored
231
        $this -> assign('data',$data);
5  
anyv authored
232 233 234
        return $this -> fetch();

    }
7  
anyv authored
235
5  
anyv authored
236 237 238 239 240
    /**
     * 新增地址页
     */
    public function add_shop_address(){
5  
anyv authored
241
        if($this -> request -> isPost()){
5  
anyv authored
242 243
            $uid = cmf_get_current_user_id();
            $_POST['uid'] = $uid;
5  
anyv authored
244 245 246 247 248 249
            $add = Db::name('address') -> insert($_POST);
            if($add){
               return true;
            }else{
                return false;
            }
5  
anyv authored
250 251 252 253
        }else{
            return $this -> fetch();
        }
5  
anyv authored
254 255 256 257 258 259 260 261
    }

    /**
     * 设置默认地址
     */
    public function set_default_address(){

        $uid = cmf_get_current_user_id();
anyv authored
262
        Db::name('address') -> where('uid',$uid) -> update(['default_address'=>0]);
5  
anyv authored
263 264 265 266 267 268
        $data = Db::name('address') -> where('id',$_POST['id']) -> update(['default_address'=>1]);
        if($data){
            return true;
        }else{
            return false;
        }
5  
anyv authored
269 270

    }
7  
anyv authored
271
5  
anyv authored
272
    /**
5  
anyv authored
273
     * 删除地址
5  
anyv authored
274 275
     */
    public function address_del(){
7  
anyv authored
276
5  
anyv authored
277 278
        $delete_time = time();
        $data = Db::name('address') -> where('id',$_POST['id']) ->  update(['delete_time'=>$delete_time]);
5  
anyv authored
279 280 281 282 283
        if($data){
            return true;
        }else{
            return false;
        }
7  
anyv authored
284
5  
anyv authored
285
    }
7  
anyv authored
286
5  
anyv authored
287 288 289 290 291
    /**
     * 编辑地址
     */
    public function address_edit(){
anyv authored
292 293 294 295 296 297 298 299 300 301 302 303 304
        if($this -> request -> isPost()){
            $data_update = Db::name('address') -> update($_POST);
            if($data_update){
               return true;
            }else{
                return false;
            }
        }else{
            $id = $this -> request -> param();
            $data = Db::name('address') -> where('id',$id['id']) -> find();
            $this -> assign('data',$data);
            return $this -> fetch();
        }
5  
anyv authored
305 306

    }
7  
anyv authored
307
anyv authored
308 309 310 311 312 313 314 315 316 317 318 319 320 321
    /**
     * 发送短信
     */
    public function send_message(){

        $url 		= "http://www.ztsms.cn/sendNSms.do";//提交地址
        $username 	= 'xuekaowuyou';//用户名
        $password 	= 'Cxz307312';//原密码
        $sendAPI = new sendAPI($url, $username, $password);
        $key = '';
        $pattern='1234567890';
        for( $i=0; $i<6; $i++ ) {
            $key .= $pattern[mt_rand(0, 9)];
        }
5  
anyv authored
322
        session('code',$key);
anyv authored
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
        $rand_name = "验证码:".$key."【学考无忧】";
        $phone = $_POST['phone'];
        $data = array(
            'content' 	=> $rand_name,//短信内容
            'mobile' 	=> $phone,//手机号码
            'productid' => '676767',//产品id
            'xh'		=> ''//小号
        );
        $sendAPI->data = $data;//初始化数据包
        $return = $sendAPI->sendSMS('POST');//GET or POST
        if($return){
           return true;
        }else{
            return false;
        }
7  
anyv authored
338
anyv authored
339
    }
7  
anyv authored
340
5  
anyv authored
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    /**
     * 添加审核信息
     */
    public function add_audit(){

        $code = session('code');
        if($code == $_POST['code']){
            $data['name'] = $_POST['name'];
            $data['phone'] = $_POST['phone'];
            $data['id_number'] = $_POST['id_num'];
            $data['img_front'] = $_POST['img0'];
            $data['img_back'] = $_POST['img1'];
            $data['create_time'] = time();
            $data['uid'] = cmf_get_current_user_id();
            $inser = Db::name('sale_audit') -> insert($data);
            if($inser){
5  
anyv authored
357
                Db::name('my_user') -> where('uid',$data['uid']) -> update(['status'=>5,'phone'=>$data['phone']]);
5  
anyv authored
358 359 360 361 362 363 364 365 366
                return 1;
            }else{
               return 2;
            }
        }else{
            return 3;
        }

    }
7  
anyv authored
367
5  
anyv authored
368 369 370 371 372
    /**
     * 我的钱包页
     */
    public function my_wallet(){
anyv authored
373 374
        $uid = cmf_get_current_user_id();
        $balance = Db::name('my_user') -> where("uid",$uid) -> find();
5  
anyv authored
375 376 377
        $b_money = $balance['balance'];
        $b_money = sprintf("%.2f",$b_money);
        $this -> assign('b_money',$b_money);
5  
anyv authored
378
        $balance['balance'] = $balance['balance'] - $balance['balance']*0.006;
7  
anyv authored
379
        $balance['balance'] = sprintf("%.2f", $balance['balance']);
1  
anyv authored
380 381 382 383 384
        $money_income = Db::name("money_income") -> where('uid',$uid) -> select();
        $cumulative_money = 0;
        foreach ($money_income as $key => $val){
            $cumulative_money += $val['money'];
        }
5  
anyv authored
385 386 387
        $cumulative_moneyss = $cumulative_money;
        $cumulative_moneyss = sprintf("%.2f",$cumulative_moneyss);
        $this -> assign('cumulative_moneyss',$cumulative_moneyss);
1  
anyv authored
388
        $cumulative_money = $cumulative_money-$cumulative_money*0.006;
5  
anyv authored
389
        $cumulative_money = sprintf("%.2f",$cumulative_money);
5  
anyv authored
390
        $money_ratio = Db::name('money_ratio') -> where('id',1) -> find();
4  
anyv authored
391 392
        $money_ratio['platform_ratio'] = $money_ratio['platform_ratio']*100;
        $money_ratio['platform_ratio'] = $money_ratio['platform_ratio'].'%';
5  
anyv authored
393
        $this -> assign('money_ratio',$money_ratio);
anyv authored
394
        $this -> assign('balance',$balance['balance']);
1  
anyv authored
395
        $this -> assign('cumulative_money',$cumulative_money);
6  
anyv authored
396
        $money_expend = Db::name('money_expend') -> where('uid='.$uid." and state=0") -> select();
5  
anyv authored
397 398 399 400
        $money = 0;
        foreach ($money_expend as $key => $val){
            $money += $val['money'];
        }
anyv authored
401
        $money = number_format($money,2);
anyv authored
402
        $this -> assign('status',$balance['status']);
5  
anyv authored
403
        $this -> assign('money',$money);
5  
anyv authored
404 405 406
        return $this -> fetch();

    }
7  
anyv authored
407
5  
anyv authored
408 409 410 411 412 413 414 415
    /**
     * 将提现金额存入支出明细表
     */
    public function add_money_expend(){

        $_POST['uid'] = cmf_get_current_user_id();
        $_POST['create_time'] = time();
        $_POST['state'] = 0;
2  
anyv authored
416 417 418 419 420
        $user_balance = Db::name('my_user') -> where('uid',$_POST['uid']) -> find();
        $user_money_expend = Db::name('money_expend') -> where("uid=".$_POST['uid']." and state =0") -> select();
        $money_expend = $_POST['money'];
        foreach ($user_money_expend as $key => $val){
            $money_expend += $val['money'];
5  
anyv authored
421
        }
2  
anyv authored
422 423
        $user_balance['balance'] = $user_balance['balance']-$user_balance['balance']*0.006;
        if($money_expend > $user_balance['balance'] ){
5  
anyv authored
424
            return false;
2  
anyv authored
425 426 427 428 429 430 431 432 433 434 435 436
        }else{
            $data = Db::name('money_expend') -> insert($_POST);
            $money_expend = Db::name('money_expend') -> where('uid='.$_POST['uid']." and state=0") -> select();
            $money = 0;
            foreach ($money_expend as $key => $val){
                $money += $val['money'];
            }
            if($data){
                return $money;
            }else{
                return false;
            }
5  
anyv authored
437 438 439 440
        }

    }
anyv authored
441 442 443 444 445
    /**
     * 邀请名单
     */
    public function invitation_list(){
5  
anyv authored
446 447 448
        $uid = cmf_get_current_user_id();
        $my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        if($my_user['status'] == 3){
anyv authored
449
            $student = Db::name('my_user') -> alias('a') -> field("a.*,b.user_nickname,b.avatar") -> join("user b","a.uid = b.id",'LEFT') -> where("a.pid",$my_user['id']) -> select() -> toArray();
anyv authored
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
            if(!empty($student)){
                foreach ($student as $key => $val){
                    $student[$key]['indent'] = Db::name('indent') -> where('uid',$val['uid']) -> where("state = 2 or state = 3 or state = 5") -> select() -> toArray();
                    $name = $val['user_nickname'];
                    $avatar = $val['avatar'];
                    $cumulative_money = 0;
                    foreach ($student[$key]['indent'] as $key1 => $val1){
                        $student[$key]['indent'][$key1]['user_name'] = $name;
                        $student[$key]['indent'][$key1]['avatar'] = $avatar;
                        $money_income = Db::name('money_income') -> where("indent_id =".$val1['id']." and uid = ".$uid) -> select() -> toArray();
                        $money = 0;
                        foreach ($money_income as $key2 => $val2){
                            $money += $val2['money'];
                        }
                        $student[$key]['indent'][$key1]['total_money'] = $money;
                        $cumulative_money += $money;
anyv authored
466
                    }
anyv authored
467
                    $student[$key]['cumulative_money'] = $cumulative_money;
anyv authored
468
anyv authored
469 470 471 472 473
                }
            }else{
                $student = null;
            }
            $this -> assign('type',3);
anyv authored
474 475
            $this -> assign('student',$student);
        }elseif ($my_user['status'] == 2){
anyv authored
476 477 478 479 480 481 482 483 484 485 486 487 488 489
            //查找业务员下级老师
            $teacher = Db::name('my_user') -> alias('a') -> field("a.*,b.user_nickname,b.avatar") -> join("user b","a.uid = b.id",'LEFT') -> where("a.pid",$my_user['id']) -> select() -> toArray();
            if(!empty($teacher)){
                foreach ($teacher as $key => $val){
                    $money_income = Db::name('money_income') -> where("uid",$val['uid']) -> select();
                    $total_commission = 0;
                    foreach ($money_income as $key1 => $val1){
                        $total_commission += $val1['money'];
                    }
                    $teacher[$key]['total_commission'] = $total_commission;
                    //查找老师下面的学生
                    $teacher[$key]['student'] = Db::name('my_user') -> alias('a') -> field("a.*,b.user_nickname,b.avatar") -> join("user b","a.uid = b.id",'LEFT') -> where("a.pid",$val['id']) -> select() -> toArray();
                    if(!empty($teacher[$key]['student'])){
                        foreach ($teacher[$key]['student'] as $key2 => $val2){
anyv authored
490 491 492
                            $teacher[$key]['student'][$key2]['indent'] = Db::name('indent') -> where('uid',$val2['uid']) -> where("state = 2 or state = 3 or state = 5") -> select() -> toArray();
                            $student_indent_money = 0;
                            foreach ($teacher[$key]['student'][$key2]['indent'] as $key3 => $val3){
anyv authored
493
                                $student_money_income = Db::name('money_income') -> where("indent_id =".$val3['id']." and uid = ".$val['uid']) -> select() -> toArray();
anyv authored
494 495 496 497 498 499
                                $student_money_income_money = 0;
                                foreach ($student_money_income as $key4 => $val4){
                                    $student_money_income_money += $val4['money'];
                                }
                                $teacher[$key]['student'][$key2]['indent']['student_money_income_money'] = $student_money_income_money;
                                $student_indent_money += $student_money_income_money;
anyv authored
500
                            }
anyv authored
501
                            $teacher[$key]['student'][$key2]['indent_money'] = $student_indent_money;
anyv authored
502 503 504 505 506 507
                        }
                    }
                }
            }else{
                $teacher = null;
            }
anyv authored
508
            $this -> assign('type',2);
anyv authored
509
            $this -> assign('teacher',$teacher);
anyv authored
510 511
        }
        return $this -> fetch();
anyv authored
512
anyv authored
513
    }
5  
anyv authored
514
anyv authored
515 516 517 518 519 520
    /**
     * 收入记录
     */
    public function income_record(){

        $uid = cmf_get_current_user_id();
5  
anyv authored
521
        $get = $this -> request -> param();
anyv authored
522
        $my_user = Db::name('my_user') -> where('uid',$uid) -> find();
4  
anyv authored
523
        //搜索老师下级学生用户
anyv authored
524 525
        $student = Db::name('my_user') -> alias('a') -> field("a.*,b.user_nickname,b.avatar") -> join("user b","a.uid = b.id",'LEFT') -> where("a.pid",$my_user['id']) -> select() -> toArray();
        foreach ($student as $key => $val){
5  
anyv authored
526 527 528 529 530 531 532
            if(!empty($get['startime']) && !empty($get['endtime'])){
                $startime = strtotime($get['startime']);
                $endtime = strtotime($get['endtime']);
                $data[$key] = Db::name('indent') -> where('uid',$val['uid']) -> where("state = 2 or state = 3 or state = 5") -> where("pay_time>=".$startime." and pay_time<=".$endtime) -> select() -> toArray();
            }else{
                $data[$key] = Db::name('indent') -> where('uid',$val['uid']) -> where("state = 2 or state = 3 or state = 5") -> select() -> toArray();
            }
anyv authored
533 534
            $name = $val['user_nickname'];
            $avatar = $val['avatar'];
5  
anyv authored
535 536 537 538 539 540 541 542 543 544 545 546 547
            if(count($data[$key])!=0){
                foreach ($data[$key] as $key1 => $val1){
                    $book_name = Db::name('indent_goods') -> where("indent_id",$val1['id']) -> find();
                    $data[$key][$key1]['user_name'] = $name;
                    $data[$key][$key1]['avatar'] = $avatar;
                    $data[$key][$key1]['book_name'] = $book_name['book_name'];
                    $money_income = Db::name('money_income') -> where("indent_id =".$val1['id']." and uid = ".$uid) -> select() -> toArray();
                    $money = 0;
                    foreach ($money_income as $key2 => $val2){
                        $money += $val2['money'];
                    }
                    $data[$key][$key1]['total_money'] = $money;
                    $res[] = $data[$key][$key1];
anyv authored
548 549
                }
            }
5  
anyv authored
550 551

        }
anyv authored
552
        if(empty($res)){
4  
anyv authored
553
            $res = array();
anyv authored
554
        }
5  
anyv authored
555
//        $this -> assign('res',$res);
4  
anyv authored
556 557
        //搜索老师用户
        $teacher_avatar = Db::name('user') -> where('id',$uid) -> find();
5  
anyv authored
558 559 560 561 562 563 564 565
        if(!empty($get['startime']) && !empty($get['endtime'])){
            $startime = strtotime($get['startime']);
            $endtime = strtotime($get['endtime']);
            $teacher = Db::name('indent') -> where('uid',$uid) -> where("pay_time>=".$startime." and pay_time<=".$endtime) -> where("state = 2 or state = 3 or state = 5") -> select() -> toArray();
        }else{
            $teacher = Db::name('indent') -> where('uid',$uid) -> where("state = 2 or state = 3 or state = 5") -> select() -> toArray();
        }
4  
anyv authored
566
        foreach($teacher as $key => $val){
2  
anyv authored
567
            $teacher_book_name = Db::name('indent_goods') -> where("indent_id",$val['id']) -> find();
4  
anyv authored
568 569
            $teacher_data[$key]['user_name'] = $teacher_avatar['user_nickname'];
            $teacher_data[$key]['avatar'] = $teacher_avatar['avatar'];
5  
anyv authored
570
            $teacher_data[$key]['book_name'] = $teacher_book_name['book_name'];
4  
anyv authored
571 572 573
            $tea_money_income = Db::name('money_income') -> where("indent_id =".$val['id']." and uid = ".$uid) -> select() -> toArray();
            $money = 0;
            foreach ($tea_money_income as $key2 => $val2){
5  
anyv authored
574
                $money += $val2['money'];
4  
anyv authored
575
            }
5  
anyv authored
576 577 578 579
            $teacher_data[$key]['total_money'] = $money;
            $teacher_data[$key]['indent_type'] = $val['indent_type'];
            $teacher_data[$key]['create_time'] = $val['create_time'];
            $my_res[] = $teacher_data[$key];
4  
anyv authored
580
        }
5  
anyv authored
581
        if(empty($my_res)){
4  
anyv authored
582
            $my_res = array();
5  
anyv authored
583
        }
5  
anyv authored
584 585 586
        $aaaa = array_merge($res,$my_res);
        $bb = array_column($aaaa,'create_time');
        $cc = array_multisort($bb,SORT_DESC,$aaaa);
4  
anyv authored
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
        if(count($res)!=0 || count($my_res)!=0){
            foreach ($aaaa as $key=>$val){
                if($val['indent_type'] == 1){
                    $pingtai[] = $val;
                }else{
                    $yewu[] = $val;
                }
            }
            $this -> assign('pingtai',$pingtai);
            $this -> assign('yewu',$yewu);
        }else{
            $this -> assign('pingtai',array());
            $this -> assign('yewu',array());
        }
//        $this -> assign('res',$aaaa);
5  
anyv authored
602
//        $this -> assign('my_res',$my_res);
anyv authored
603 604 605
        return $this -> fetch();

    }
5  
anyv authored
606
anyv authored
607 608 609
    /**
     * 提现明细
     */
4  
anyv authored
610
    public function withdrawal_subsidiary(){
5  
anyv authored
611
anyv authored
612
        $uid = cmf_get_current_user_id();
4  
anyv authored
613
        $data = Db::name('money_expend') -> where('uid='.$uid) -> where("state = 1 or state = 2")  -> select();
anyv authored
614
        $this -> assign('data',$data);
4  
anyv authored
615 616 617
        return $this -> fetch();

    }
5  
anyv authored
618
anyv authored
619 620 621 622 623 624 625 626 627 628 629 630
    /**
     * 显示商场盈利
     */
    public function mall_profit(){

        $uid = cmf_get_current_user_id();
        $my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        if($my_user['status'] == 2){
            $res = $this -> request -> param();
            if(count($res) != 0){
                $where = [
                    'a.indent_type' => 2,
1  
anyv authored
631
                    'a.salesman_uid' => $uid,
anyv authored
632
                ];
4  
anyv authored
633 634 635 636 637
                if($res['school'] != ''){
                    $where['a.school'] = $res['school'];
                }
                if($res['grade'] != ''){
                    $school_grade_class = explode('-',$res['grade']);
4  
anyv authored
638 639
                    $where['a.grade'] = trim($school_grade_class[0]);
                    $where['a.class'] = trim($school_grade_class[1]);
anyv authored
640
                }
anyv authored
641
4  
anyv authored
642 643 644 645 646 647 648 649 650
                if($res['is_courier'] != ''){
                    if($res['is_courier'] == '统一配送'){
                        $where['a.is_courier'] = 1;
                    }
                    if($res['is_courier'] == '快递配送'){
                        $where['a.is_courier'] = 2;
                    }
                }
anyv authored
651 652 653 654 655
                if($res['start_time'] != '' && $res['end_time'] != ''){
                    $start_time = strtotime($res['start_time']);
                    $end_time = strtotime($res['end_time']);
                    $where['a.pay_time'] = [['>=',$start_time],['<=',$end_time]];
                }
4  
anyv authored
656
                /*$indent_goods = Db::name('indent') -> alias('a') -> field("a.*,b.indent_id,b.book_name,b.pricing,b.price,b.number,b.thumbnail") -> join('indent_goods b','a.id=b.indent_id','LEFT') -> where($where) -> where("a.state =2 or a.state=3 or a.state=5") -> select();*/
4  
anyv authored
657
                $indent_money = Db::name('indent') -> alias('a') -> field("a.id,a.order_number,a.pay_time,b.uid,b.money,b.type,b.book_num,b.indent_id,a.school,a.grade,a.class") -> join('money_income b','a.id=b.indent_id','LEFT') -> where($where) -> where("a.state =2 or a.state=3 or a.state=5") -> where("b.uid =".$uid) -> order('a.pay_time desc') -> select() -> toArray();
4  
anyv authored
658
4  
anyv authored
659 660 661 662 663 664
                if(count($indent_money)!=0){
                    foreach ($indent_money as $key => $val){
                        $indent_money[$key]['book_name'] = Db::name('indent_goods') -> where('indent_id',$val['id']) -> select() -> toArray();
                    }
                }
                $this -> assign('indent_goods',$indent_money);
anyv authored
665
            }else{
anyv authored
666
4  
anyv authored
667
                /*$indent_goods = Db::name('indent') -> alias('a') -> field("a.*,b.indent_id,b.book_name,b.pricing,b.price,b.number,b.thumbnail") -> join('indent_goods b','a.id=b.indent_id','LEFT') -> where('a.indent_type=2 and a.salesman_uid='.$uid) -> where("a.state =2 or a.state=3 or a.state=5") -> select();*/
4  
anyv authored
668
                $indent_goods = Db::name('indent') -> alias('a') -> field("a.id,a.order_number,a.pay_time,b.uid,b.money,b.type,b.book_num,b.indent_id") -> join('money_income b','a.id=b.indent_id','LEFT') -> where('a.indent_type=2 and a.salesman_uid='.$uid) -> where("a.state =2 or a.state=3 or a.state=5") -> where("b.uid =".$uid) -> order('a.pay_time desc') -> select() -> toArray();
4  
anyv authored
669 670 671 672 673
                if(count($indent_goods)!=0){
                    foreach ($indent_goods as $key => $val){
                        $indent_goods[$key]['book_name'] = Db::name('indent_goods') -> where('indent_id',$val['id']) -> select() -> toArray();
                    }
                }
4  
anyv authored
674
4  
anyv authored
675
                $this -> assign('indent_goods',$indent_goods);
1  
anyv authored
676 677
//                $indent_money = Db::name('money_income') -> where("uid =".$uid) -> select();
                $indent_money = $indent_goods;
anyv authored
678 679 680 681 682 683
            }

            $total_money = 0;
            foreach ($indent_money as $key => $val){
                $total_money += $val['money'];
            }
anyv authored
684 685
            //保留两位小数
            $total_money = number_format($total_money,2);
5  
anyv authored
686
//            $total_money = $total_money-$total_money*0.006;
anyv authored
687
            $this -> assign('total_money',$total_money);
anyv authored
688
anyv authored
689
            //显示学校年级
anyv authored
690 691 692 693 694 695 696 697
            $school = Db::name('school') -> where('uid',$uid) -> select() -> toArray();
            if(count($school) != 0){
                $grade_class = Db::name('grade_class') -> where("school_id",$school[0]['id']) -> select();
                foreach ($grade_class as $key => $val){
                    $class = explode('-',$val['class']);
                    for($i=$class[0];$i<=$class[1];$i++){
                        $grade_class_show[] = $val['grade'].' - '.$i."班";
                    }
anyv authored
698
                }
4  
anyv authored
699 700 701 702
                foreach ($school as $key => $val){
                    $schools[$key]['value'] = $val['id'];
                    $schools[$key]['text'] = $val['school'];
                }
anyv authored
703
            }else{
2  
anyv authored
704
                $schools = '';
anyv authored
705
                $grade_class_show = '';
anyv authored
706
            }
4  
anyv authored
707
            $this -> assign('school',json_encode($schools));
anyv authored
708 709 710 711 712
            $this -> assign('grade_class_show',$grade_class_show);
            //显示平台盈利
            //查询业务员下级所有老师的订单
            $teacher_user = Db::name('my_user') -> where("pid",$my_user['id']) -> select() -> toArray();
            foreach ($teacher_user as $key => $val){
anyv authored
713
                $teacher_indent[$key] = Db::name('indent') -> where("indent_type=1 and uid=".$val['uid']) -> where("state = 2 or state = 3 or state = 5") -> select() -> toArray();
anyv authored
714 715 716 717 718 719 720 721 722 723 724 725 726
                foreach ($teacher_indent[$key] as $key1 => $val1){
                    $money_income = Db::name('money_income') -> where("indent_id=".$val1['id']." and uid=".$my_user['uid']) -> select();
                    foreach ($money_income as $key3 => $val3){
                        $platform_money[] = $val3;
                    }
                }
            }
            //查询老师下学生的订单
            foreach ($teacher_user as $key => $val){
                //查询老师下的所有学生
                $student[$key] = Db::name('my_user') -> where("pid",$val['id']) -> select();
                //查询学生的所有订单
                foreach ($student[$key] as $key1 => $val1){
anyv authored
727
                    $student_indent[$key1] = Db::name('indent') -> where("indent_type=1 and uid=".$val1['uid']) -> where("state = 2 or state = 3 or state = 5") -> select();
anyv authored
728 729 730 731 732 733 734 735 736
                    //查询学生订单下属于业务员的收入记录
                    foreach ($student_indent[$key1] as $key2 => $val2){
                        $student_money_income = Db::name('money_income') -> where("indent_id=".$val2['id']." and uid=".$my_user['uid']) -> select();
                        foreach ($student_money_income as $key3 => $val3){
                            $platform_money[] = $val3;
                        }
                    }
                }
            }
anyv authored
737 738 739
            if(empty($platform_money)){
                $platform_money = null;
            }
anyv authored
740 741 742 743 744
            $this -> assign('platform_money',$platform_money);
         }else{
            $this -> error('非业务员不能进去此页');
        }
4  
anyv authored
745
anyv authored
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
        return $this -> fetch();

    }

    /**
     * 点击学校时获取年级班级
     */
    public function get_grade_class(){

        $school_id = $_POST['id'];
        $grade_class = Db::name('grade_class') -> where("school_id",$school_id) -> select() -> toArray();
        if(!empty($grade_class)){
            foreach ($grade_class as $key => $val){
                $class = explode('-',$val['class']);
                for($i=$class[0];$i<=$class[1];$i++){
                    $grade_class_show[] = $val['grade'].' - '.$i."班";
                }
            }
4  
anyv authored
764 765 766 767 768
            foreach ($grade_class_show as $key => $val){
                $grade_class_shows[$key]['value'] = $key;
                $grade_class_shows[$key]['text'] = $val;
            }
            return json_encode($grade_class_shows);
anyv authored
769 770 771 772 773 774
        }else{
            return false;
        }

    }
anyv authored
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    /**
     * 但图片上传
     */
    public function image_one_up(){

        $file = request()->file('avatar');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                $image = $info->getSaveName();
                $this->apiResponse(1,'成功',$image);
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

    }

    public function apiResponse($code = '', $msg = '',$data = array()){
        header('Access-Control-Allow-Origin: *');
        header('Content-Type:application/json; charset=utf-8');
        $result = array(
            'code'=>$code,
            'msg'=>$msg,
            'data'=>$data,
        );
        die(json_encode($result,JSON_UNESCAPED_UNICODE));
    }
anyv authored
809
5  
anyv authored
810 811 812 813 814 815 816 817 818 819 820 821 822 823
    /**
     *判断是否首次提现
     */
    public function id_first_withdrawal(){

        $uid = cmf_get_current_user_id();
        $my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        if($my_user['is_withdrawal'] == '1'){
            return true;
        }else{
            return false;
        }

    }
anyv authored
824
5  
anyv authored
825 826 827 828 829 830 831 832
    /**
     * 首次提现手机验证码判断
     */
    public function is_money_code(){

        $uid = cmf_get_current_user_id();
        $code = session('code');
        if($code == $_POST['yzm']){
4  
anyv authored
833 834 835 836
            $user = Db::name('my_user') -> where('uid',$uid) -> find();
            if(empty($user['phone'])){
                Db::name('my_user') -> where('uid',$uid) -> update(['phone'=>$_POST['phone']]);
            }
5  
anyv authored
837 838 839 840 841 842 843
            Db::name('my_user') -> where('uid',$uid) -> update(['is_withdrawal'=>2]);
            return true;
        }else{
            return false;
        }

    }
anyv authored
844 845 846


5  
anyv authored
847 848 849 850 851




7  
anyv authored
852 853 854 855



anyv authored
856 857 858 859



}