审查视图

application/home/controller/Member.php 16.2 KB
王晓刚 authored
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/23
 * Time: 18:02
 */

namespace app\home\controller;


use app\common\controller\WechatBase;
王晓刚 authored
13
use EasyWeChat\Foundation\Application;
王晓刚 authored
14 15 16 17 18 19 20 21 22
use think\Db;

class Member extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
王晓刚 authored
23
        $user_id = get_current_user_id();
王晓刚 authored
24
        if(empty($user_id)){
王晓刚 authored
25 26
            $target_url = rawurlencode(url('',false,true));
            $this->redirect('user/authorization_view',array('target_url'=>$target_url));
王晓刚 authored
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        }
        $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();
王晓刚 authored
45
        $user['rmb'] = round($user['exp']*$exp_ratio['ratio']*0.01,2);
王晓刚 authored
46
        //今日获得的红包券
王晓刚 authored
47 48 49
        $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');
王晓刚 authored
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        $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');
        }
王晓刚 authored
68
        $birthday = explode('-',$user['birthday']);
王晓刚 authored
69 70
        $user['year'] = empty($birthday[0]) ? '' : $birthday[0];
        $user['day'] = empty($birthday[1]) ? '' : $birthday[1];
王晓刚 authored
71 72 73 74 75 76 77
        $this->assign('user',$user);
        $options = [
            'app_id' => config('wechat.app_id'),
            'secret' => config('wechat.secret'),
        ];
        $app = new Application($options);
        $js = $app->js;
王晓刚 authored
78
        $jssdk = $js->config(['getLocation','openLocation','chooseImage', 'uploadImage', 'previewImage'], $debug = false, $beta = false, $json = true);
王晓刚 authored
79
        $this->assign('jssdk',$jssdk);
王晓刚 authored
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
        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());
        }
王晓刚 authored
108 109
        $user_id = $param['user_id'];
        $user = Db::name('user')->where(['id'=>$user_id])->find();
王晓刚 authored
110 111 112
        $param['birthday'] = "$param[year]-$param[day]";
        unset($param['year']);
        unset($param['day']);
王晓刚 authored
113 114
        unset($param['user_id']);
        Db::name('user')->where(['id'=>$user_id])->update($param);
王晓刚 authored
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        $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();
王晓刚 authored
132
        $user['rmb'] = round($user['exp']*$exp_ratio['ratio']*0.01,2);
王晓刚 authored
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        $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();
王晓刚 authored
152
        $user['rmb'] = round($user['exp']*$exp_ratio['ratio']*0.01,2);
王晓刚 authored
153 154 155
        $arr = [10,20,30,40,50];
        $data = [];
        foreach($arr as $key => $a){
王晓刚 authored
156
            $data[$a] = $a*$exp_ratio['ratio']/0.01;
王晓刚 authored
157
        }
王晓刚 authored
158
        $this->assign('user',$user);
王晓刚 authored
159
        $this->assign('data',$data);
王晓刚 authored
160 161 162 163 164 165 166 167 168 169 170
        $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
     */
王晓刚 authored
171
    public function withdraw(){
王晓刚 authored
172 173 174 175 176 177 178 179 180 181 182
        $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'])){
王晓刚 authored
183
            $this->error('请完善提现信息','home/member/withdraw_information');
王晓刚 authored
184 185 186 187 188 189 190
        }
        $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();
王晓刚 authored
191
        $result = Db::name('user_exp_log')->insert($arr);
王晓刚 authored
192 193 194
        if(empty($result)){
            $this->error('sql执行失败');
        }
王晓刚 authored
195 196 197 198 199
        //修改用户剩余红包券
        $result2 = Db::name('user')->where(['id'=>$user_id])->setDec('exp',$exp);
        if(empty($result2)){
            $this->error('sql执行失败');
        }
王晓刚 authored
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
        $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());
        }
王晓刚 authored
239 240 241
        $user_id = $param['user_id'];
        unset($param['user_id']);
        Db::name('user')->where(['id'=>$user_id])->update($param);
王晓刚 authored
242 243 244 245 246 247 248 249 250 251 252
        $this->success('SUCCESS');
    }

    /**
     * 收益记录页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function earnings_log(){
王晓刚 authored
253 254 255 256
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
王晓刚 authored
257
        $data = Db::name('user_exp_log')->where(['type'=>['in',[4,5]],'user_id'=>$this->user_id])->order('id desc')->page(1,5)->select();
王晓刚 authored
258
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
王晓刚 authored
259 260
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
王晓刚 authored
261 262
            //转换为人民币
            $data[$key]['rmb'] = round($vo['exp']*$exp_ratio['ratio']*0.01,2);
王晓刚 authored
263
        }
王晓刚 authored
264
        $this->assign('user',$user);
王晓刚 authored
265
        $this->assign('data',$data);
王晓刚 authored
266 267 268 269 270
        $this->assign('title','收益记录');
        return $this->fetch();
    }

    /**
王晓刚 authored
271 272 273 274 275 276 277 278 279 280 281 282
     * 更多收益记录
     * @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('缺少必要参数');
        }
王晓刚 authored
283
        $data = Db::name('user_exp_log')->where(['type'=>['in',[4,5]],'user_id'=>$user_id])->order('id desc')->page($page,$pageNum)->select();
王晓刚 authored
284 285 286 287 288 289
        $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);
        }
王晓刚 authored
290
        $this->success('SUCCESS','',$data);
王晓刚 authored
291 292 293
    }

    /**
王晓刚 authored
294 295 296 297 298 299 300
     * 提现记录页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdraw_log(){
王晓刚 authored
301 302 303 304
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
王晓刚 authored
305 306 307 308 309 310 311
        $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);
        }
王晓刚 authored
312
        $this->assign('user',$user);
王晓刚 authored
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
        $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();
王晓刚 authored
334 335
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
王晓刚 authored
336 337
            //转换为人民币
            $data[$key]['rmb'] = round($vo['exp']*$exp_ratio['ratio']*0.01,2);
王晓刚 authored
338
        }
王晓刚 authored
339
        $this->success('SUCCESS','',$data);
王晓刚 authored
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
    }

    /**
     * 商家入驻页面
     * @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');
    }
王晓刚 authored
413
    public function get_city(){
王晓刚 authored
414
        $user_id = $this->request->param('user_id');
王晓刚 authored
415 416
        $longitude = $this->request->param('longitude');
        $latitude = $this->request->param('latitude');
王晓刚 authored
417
        if(empty($longitude) || empty($latitude) || empty($user_id)){
王晓刚 authored
418 419
            $this->error('404');
        }
王晓刚 authored
420
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=$latitude,$longitude&key=JMPBZ-DV7WU-WT4V7-BYCU2-XG3E5-OZB7T";
王晓刚 authored
421 422
        $result = self::http_get($url);
        $data = json_decode($result,true);
王晓刚 authored
423 424 425
        if($data['status'] != 0){
            $this->error('请求失败');
        }
王晓刚 authored
426 427
        $arr['province'] = $data['result']['address_component']['province'];
        $arr['city'] = $data['result']['address_component']['city'];
王晓刚 authored
428
        $arr['county'] = $data['result']['address_component']['district'];
王晓刚 authored
429 430
        $arr['latitude'] = $latitude;
        $arr['longitude'] = $longitude;
王晓刚 authored
431 432
        //修改用户信息
        Db::name('user')->where(['id'=>$user_id])->update($arr);
王晓刚 authored
433
        $this->success('SUCCESS','',$data['result']['address_component']);
王晓刚 authored
434 435 436 437 438 439 440 441 442 443 444 445
    }
    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;
王晓刚 authored
446
    }
王晓刚 authored
447
}