Member.php 13.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/23
 * Time: 18:02
 */

namespace app\home\controller;


use app\common\controller\WechatBase;
use EasyWeChat\Foundation\Application;
use think\Db;

class Member extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
        $user_id = 3;//get_current_user_id();
        if(empty($user_id)){
            $this->redirect('user/authorization_view');
        }
        $this->user_id = $user_id;
    }

    /**
     * 个人中心
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        //转换为人民币
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        $user['rmb'] = round($user['exp']*$exp_ratio['ratio']*0.01,2);
        //今日获得的红包券
        $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $today_exp = Db::name('user_exp_log')->where(['user_id'=>$this->user_id,'type'=>['in',[4,5]]])->where('createtime',['>',$beginToday],['<>',$endToday])->sum('exp');
        $user['today_exp'] = $today_exp;
        $this->assign('user',$user);
        $this->assign('title','个人中心');
        return $this->fetch();
    }

    /**
     * 个人信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function my_information(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('404');
        }
        $birthday = explode('-',$user['birthday']);
        $user['year'] = empty($birthday[0]) ? '' : $birthday[0];
        $user['day'] = empty($birthday[1]) ? '' : $birthday[1];
        $this->assign('user',$user);
        $options = [
            'app_id' => config('wechat.app_id'),
            'secret' => config('wechat.secret'),
        ];
        $app = new Application($options);
        $js = $app->js;
        $jssdk = $js->config(['getLocation','openLocation','chooseImage', 'uploadImage', 'previewImage'], $debug = false, $beta = false, $json = true);
        $this->assign('jssdk',$jssdk);
        return $this->fetch();
    }

    /**
     * 更新个人信息
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function update_information(){
        $param = $this->request->param();
        $validate = new \think\Validate([
            'user_id' => 'require',
            'nickname' => 'require',
            'avatar' => 'require',
            'gender' => 'require',
        ]);
        $validate->message([
            'user_id' => 'user_id参数错误',
            'nickname' => 'nickname参数错误',
            'avatar.require' => 'avatar参数错误',
            'gender.require' => 'gender参数错误',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $user_id = $param['user_id'];
        $user = Db::name('user')->where(['id'=>$user_id])->find();
        $param['birthday'] = "$param[year]-$param[day]";
        unset($param['year']);
        unset($param['day']);
        unset($param['user_id']);
        Db::name('user')->where(['id'=>$user_id])->update($param);
        $this->success('SUCCESS');
    }

    /**
     * 我的钱包
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function my_wallet(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        //转换为人民币
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        $user['rmb'] = round($user['exp']*$exp_ratio['ratio']*0.01,2);
        $this->assign('user',$user);
        $this->assign('title','我的钱包');
        return $this->fetch();
    }

    /**
     * 提现页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function my_withdraw(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        //转换为人民币
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        $user['rmb'] = round($user['exp']*$exp_ratio['ratio']*0.01,2);
        $arr = [10,20,30,40,50];
        $data = [];
        foreach($arr as $key => $a){
            $data[$a] = $a*$exp_ratio['ratio']*0.01;
        }
        dump($data);
        $this->assign('user',$user);
        $this->assign('data',$data);
        $this->assign('exp_ratio',$exp_ratio);
        $this->assign('title','提现');
        return $this->fetch();
    }

    /**
     * 提交提现申请
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdraw(){
        $user_id = $this->request->param('user_id',0,'intval');
        $exp = $this->request->param('exp',0,'intval');
        if(empty($user_id) || empty($exp)){
            $this->error('404');
        }
        $user = Db::name('user')->where(['id'=>$user_id])->find();
        if($user['exp'] < $exp){
            $this->error("您的红包券不足$exp");
        }
        //判断是否完善提现信息
        if(empty($user['name']) || empty($user['card']) || empty($user['mobile'])){
            $this->error('请完善提现信息','');
        }
        $arr['user_id'] = $user_id;
        $arr['before_exp'] = $user['exp'];
        $arr['exp'] = $exp;
        $arr['after_exp'] = $user['exp'] - $exp;
        $arr['type'] = 1;
        $arr['createtime'] = time();
        $result = Db::name('user_exp_log')->insert();
        if(empty($result)){
            $this->error('sql执行失败');
        }
        $this->success('SUCCESS');
    }

    /**
     * 完善提现信息页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdraw_information(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        $this->assign('user',$user);
        $this->assign('title','完善提现信息');
        return $this->fetch();
    }

    /**
     * 更新提现信息
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update_withdraw_information(){
        $param = $this->request->param();
        $validate = new \think\Validate([
            'user_id' => 'require',
            'name' => 'require',
            'card' => 'require',
            'mobile' => 'require',
        ]);
        $validate->message([
            'user_id' => 'user_id参数错误',
            'name' => 'name参数错误',
            'card' => 'card参数错误',
            'mobile' => 'mobile参数错误',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        Db::name('user')->where(['id'=>$param['user_id']])->update($param);
        $this->success('SUCCESS');
    }

    /**
     * 收益记录页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function earnings_log(){
        $data = Db::name('user_exp_log')->where(['type'=>['in',[4,5]]])->order('id desc')->page(1,5)->select();
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
        }
        $this->assign('data',$data);
        $this->assign('title','收益记录');
        return $this->fetch();
    }

    /**
     * 提现记录页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdraw_log(){
        $data = Db::name('user_exp_log')->where(['type'=>['in',[1,2,3]]])->order('id desc')->page(1,5)->select();
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
        }
        $this->assign('data',$data);
        $this->assign('title','提现记录');
        return $this->fetch();
    }

    /**
     * 商家入驻页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function merchant_enter_view(){
        $data = Db::name('page')->where(['id'=>1])->find();
        $this->assign('data',$data);
        $this->assign('title','商家入驻');
        return $this->fetch();
    }

    /**
     * 商家入驻表单页面
     * @return mixed
     */
    public function merchant_enter_form(){
        $data = Db::name('merchant_audit')->where(['user_id'=>$this->user_id])->find();
        $this->assign('user_id',$this->user_id);
        $this->assign('data',$data);
        $this->assign('title','商家入驻');
        return $this->fetch();
    }

    /**
     * 提交申请
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function merchant_enter_submit(){
        $param = $this->request->param();
        $validate = new \think\Validate([
            'user_id' => 'require',
            'merchant_title' => 'require',
            'merchant_name' => 'require',
            'merchant_mobile' => 'require',
            'business_images' => 'require',
        ]);
        $validate->message([
            'user_id' => 'user_id参数错误',
            'merchant_title' => 'merchant_title参数错误',
            'merchant_name' => 'merchant_name参数错误',
            'merchant_mobile' => 'merchant_mobile参数错误',
            'business_images' => 'business_images参数错误',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $data = Db::name('merchant_audit')->where(['user_id'=>$this->user_id])->find();
        if(!empty($data)){
            if($data['expiration_time'] > time() && $data['status'] == 3){
                $this->error("请于$data[expiration_time]后再来提交");
            }else if($data['status'] == 2){
                $this->error("您已经是商家了");
            }else if($data['status'] == 1){
                $this->error('正在审核中');
            }else{
                $param['status'] = 1;
                Db::name('merchant_audit')->where(['user_id'=>$param['user_id']])->update($param);
            }
        }else{
            $param['createtime'] = time();
            Db::name('merchant_audit')->insert($param);
        }
        $this->success('success');
    }
    public function get_city(){
        $user_id = $this->request->param('user_id');
        $longitude = $this->request->param('longitude');
        $latitude = $this->request->param('latitude');
        if(empty($longitude) || empty($latitude) || empty($user_id)){
            $this->error('404');
        }
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=$latitude,$longitude&key=JMPBZ-DV7WU-WT4V7-BYCU2-XG3E5-OZB7T";
        $result = self::http_get($url);
        $data = json_decode($result,true);
        if($data['status'] != 0){
            $this->error('请求失败');
        }
        $arr['province'] = $data['result']['address_component']['province'];
        $arr['city'] = $data['result']['address_component']['city'];
        $arr['county'] = $data['result']['address_component']['district'];
        $arr['latitude'] = $latitude;
        $arr['longitude'] = $longitude;
        //修改用户信息
        Db::name('user')->where(['id'=>$user_id])->update($arr);
        $this->success('SUCCESS','',$data['result']['address_component']);
    }
    public static function http_get($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        $result = curl_exec($ch);
        // grab URL, and print
        if(curl_errno($ch)){
            print curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }
}