Member.php 16.2 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
<?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 = get_current_user_id();
        if(empty($user_id)){
            $target_url = rawurlencode(url('',false,true));
            $this->redirect('user/authorization_view',array('target_url'=>$target_url));
        }
        $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;
        }
        $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('请完善提现信息','home/member/withdraw_information');
        }
        $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($arr);
        if(empty($result)){
            $this->error('sql执行失败');
        }
        //修改用户剩余红包券
        $result2 = Db::name('user')->where(['id'=>$user_id])->setDec('exp',$exp);
        if(empty($result2)){
            $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());
        }
        $user_id = $param['user_id'];
        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 earnings_log(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        $data = Db::name('user_exp_log')->where(['type'=>['in',[4,5]],'user_id'=>$this->user_id])->order('id desc')->page(1,5)->select();
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
            //转换为人民币
            $data[$key]['rmb'] = round($vo['exp']*$exp_ratio['ratio']*0.01,2);
        }
        $this->assign('user',$user);
        $this->assign('data',$data);
        $this->assign('title','收益记录');
        return $this->fetch();
    }

    /**
     * 更多收益记录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function more_earnings_log(){
        $user_id = $this->request->param('user_id',0,'intval');
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',5,'intval');
        if(empty($user_id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('user_exp_log')->where(['type'=>['in',[4,5]],'user_id'=>$user_id])->order('id desc')->page($page,$pageNum)->select();
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
            //转换为人民币
            $data[$key]['rmb'] = round($vo['exp']*$exp_ratio['ratio']*0.01,2);
        }
        $this->success('SUCCESS','',$data);
    }

    /**
     * 提现记录页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdraw_log(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        $data = Db::name('user_exp_log')->where(['type'=>['in',[1,2,3]],'user_id'=>$this->user_id])->order('id desc')->page(1,5)->select();
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
            //转换为人民币
            $data[$key]['rmb'] = round($vo['exp']*$exp_ratio['ratio']*0.01,2);
        }
        $this->assign('user',$user);
        $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 more_withdraw_log(){
        $user_id = $this->request->param('user_id',0,'intval');
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',5,'intval');
        if(empty($user_id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('user_exp_log')->where(['type'=>['in',[1,2,3]],'user_id'=>$user_id])->order('id desc')->page($page,$pageNum)->select();
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
            //转换为人民币
            $data[$key]['rmb'] = round($vo['exp']*$exp_ratio['ratio']*0.01,2);
        }
        $this->success('SUCCESS','',$data);
    }

    /**
     * 商家入驻页面
     * @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;
    }
}