PersonalcenterController.php 11.8 KB
<?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;
use think\Db;

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();
    }

}

class PersonalcenterController extends WeChatBaseController{

    /**
     * 显示个人中心页
     */
    public function personal_center(){

        $uid = cmf_get_current_user_id();
        $my_user_status = Db::name('my_user') -> where('uid',$uid) -> find();
        $weixin = Db::name('user') -> where('id',$uid) -> find();
        $this -> assign('weixin',$weixin);
        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){
            if($my_user_status['status'] == 0){
                $this -> assign('status',0);
            }
            if($my_user_status['status'] == 1 || $my_user_status['status'] == 5 || $my_user_status['status'] == 6){
                $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();
        }
        //业务员个人中心页
        if($my_user_status['status'] == 2){
            $this -> assign('weixin',$weixin);
            return $this -> fetch('personalcenter/salesman_center');
        }

    }

    /**
     * 完善个人信息页
     */
    public function perfect_information(){

        $uid = cmf_get_current_user_id();
        $my_user_status = Db::name('my_user') -> where('uid',$uid) -> find();
        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();
        }

    }


    /**
     * 我的收藏页
     */
    public function personal_collect(){

        $uid = cmf_get_current_user_id();
        $data = Db::name('collect') -> where("uid =".$uid) -> select();
        $data_count = count($data);
        if(!empty($data)){
            foreach($data as $key => $val){
                $data_goods[] = Db::name('goods') -> where("id =".$val['goods_id']) -> find();
            }
            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 = '';
        }
        $this -> assign('data_count',$data_count);
        $this -> assign('data_goods',$data_goods);
        return $this -> fetch();

    }

    /**
     * 填写个人信息页
     */
    public function add_information(){

        return $this -> fetch();

    }

    /**
     * 浏览记录
     */
    public function browsing_history(){

        $uid = cmf_get_current_user_id();
        $data = Db::name('browsing_history') -> where("uid =".$uid) -> select();
        foreach($data as $key => $val){
            $data_goods[] = Db::name('goods') -> where('id',$val['goods_id']) -> find();
        }
        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];
            }
        }else{
            $data_goods = '';
        }
        $this -> assign('data_goods',$data_goods);
        return $this -> fetch();

    }

    /**
     * 收货地址页
     */
    public function shop_address(){

        $data = Db::name('address') -> where("delete_time = 0") ->  select() -> toArray();
        if(!empty($data)){
            foreach($data as $key => $val){
                $detailed = explode(',',$val['detailed']);
                $data[$key]['detailed'] = $detailed[0].$detailed[1];
            }
        }
        $this -> assign('data',$data);
        return $this -> fetch();

    }

    /**
     * 新增地址页
     */
    public function add_shop_address(){

        if($this -> request -> isPost()){
            $uid = cmf_get_current_user_id();
            $_POST['uid'] = $uid;
            $add = Db::name('address') -> insert($_POST);
            if($add){
               return true;
            }else{
                return false;
            }
        }else{
            return $this -> fetch();
        }

    }

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

        $uid = cmf_get_current_user_id();
        Db::name('address') -> where('uid',$uid) -> update(['default_address'=>0]);
        $data = Db::name('address') -> where('id',$_POST['id']) -> update(['default_address'=>1]);
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 删除地址
     */
    public function address_del(){

        $delete_time = time();
        $data = Db::name('address') -> where('id',$_POST['id']) ->  update(['delete_time'=>$delete_time]);
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 编辑地址
     */
    public function address_edit(){

        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();
            $detailed = explode(',',$data['detailed']);
            $data['detailed0'] = $detailed[0];
            $data['detailed1'] = $detailed[1];
            $this -> assign('data',$data);
            return $this -> fetch();
        }

    }

    /**
     * 发送短信
     */
    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)];
        }
        session('code',$key);
        $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;
        }

    }

    /**
     * 添加审核信息
     */
    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){
                Db::name('my_user') -> where('uid',$data['uid']) -> update(['status'=>5]);
                return 1;
            }else{
               return 2;
            }
        }else{
            return 3;
        }

    }

    /**
     * 我的钱包页
     */
    public function my_wallet(){

        $uid = cmf_get_current_user_id();
        $balance = Db::name('my_user') -> where("uid",$uid) -> find();
        $balance['balance'] = $balance['balance'] - $balance['balance']*0.006;
        $this -> assign('balance',$balance['balance']);
        return $this -> fetch();

    }








}