Userinfo.php 15.9 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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/17
 * Time: 9:41
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;
use think\Validate;

/**
 * 个人中心模块
 */
class Userinfo extends Api
{
    /**
     * @ApiTitle    (修改用户头像)
     * @ApiSummary  (修改用户头像)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/userinfo/updthumbnail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="thumbnail", type="string", required=true, description="头像")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        },
        })
     */
    public function updthumbnail()
    {
        $user_id = $this->getUserId();
        $thumbnail = $this->request->param('thumbnail');
        if(empty($thumbnail)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('user')
            ->where('id',$user_id)
            ->update(['avatar'=>$thumbnail]);
        if(empty($data)){
            $this->error('sql运行失败');
        }else{
            $this->success('修改成功');
        }
    }

    /**
     * @ApiTitle    (修改用户昵称)
     * @ApiSummary  (修改用户昵称)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/userinfo/updnickname)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="nickname", type="string", required=true, description="昵称")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    },
    })
     */
    public function updnickname()
    {
        $user_id = $this->getUserId();
//        print_r($user_id);die;
        $nickname = $this->request->param('nickname');
        if(empty($nickname)){
            $this->error('缺少必要参数');
        }

        Db::startTrans();
        $data = Db::name('user')
            ->where('id',$user_id)
            ->update(['nickname'=>$nickname]);
        $res = Db::name('third')
            ->where('user_id',$user_id)
            ->update(['nickname'=>$nickname]);

        if($data && $res){
            Db::commit();
            $this->success('修改成功');
        }else{
            Db::rollback();
            $this->error('sql运行失败');
        }
    }

    /**
     * @ApiTitle    (绑定用户手机号)
     * @ApiSummary  (绑定用户手机号)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/userinfo/updmobile)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        },
        })
     */
    public function updmobile()
    {
        $user_id = $this->getUserId();
        $mobile = $this->request->param('mobile');
        if(empty($mobile)){
            $this->error('缺少必要参数');
        }
        $phone = Db::name('user')->where('id',$user_id)->find();
        $score = $phone['score'] + 2;
        if(empty($phone['mobile'])){
            $data = Db::name('user')
                ->where('id',$user_id)
                ->update(['mobile'=>$mobile]);
            Db::name('user')
                ->where('id',$user_id)
                ->update(['score'=>$score]);
            if(empty($data)){
                $this->error('sql运行失败');
            }else{
                $this->success('修改成功');
            }
        }
        $data = Db::name('user')
            ->where('id',$user_id)
            ->update(['mobile'=>$mobile]);
        if(empty($data)){
            $this->error('sql运行失败');
        }else{
            $this->success('修改成功');
        }
    }

    /**
     * @ApiTitle    (收货地址列表)
     * @ApiSummary  (收货地址列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/userinfo/get_all)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":"id",//收货地址id
            "user_id":"user_id",//用户id
            "name":"name",//姓名
            "mobile":"mobile",//手机号
            "province":"province",//省
            "city":"city",//市
            "county":"county",//区/县
            "address":"address",//详细地址
    }
    })
     */
    public function get_all(){
        $user_id = $this->getUserId();
        $where['user_id'] = ['eq',$user_id];
        $data = Db::name('address')->where($where)->order('id desc')->select();
        $this->success('SUCCESS',$data);
    }
    /**
     * @ApiTitle    (单个收货地址)
     * @ApiSummary  (单个收货地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/userinfo/get_one)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="take_id", type="int", required=true, description="收货地址id")
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":"id",//收货地址id
            "user_id":"user_id",//用户id
            "name":"name",//姓名
            "mobile":"mobile",//手机号
            "province":"province",//省
            "city":"city",//市
            "county":"county",//区/县
            "address":"address",//详细地址
    }
    })
     */
    public function get_one(){
        $user_id = $this->getUserId();
        $take_id = $this->request->param('take_id',0,'intval');
        if(empty($take_id)){
            $this->error('缺少必要参数');
        }
        $where['user_id'] = ['eq',$user_id];
        $where['id'] = ['eq',$take_id];
        $data = Db::name('address')->where($where)->find();
        $this->success('SUCCESS',$data);
    }
    /**
     * @ApiTitle    (新增收货地址)
     * @ApiSummary  (新增收货地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/userinfo/create_take)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="name", type="string", required=true, description="姓名")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="province", type="string", required=true, description="省")
     * @ApiParams   (name="city", type="string", required=true, description="市")
     * @ApiParams   (name="county", type="string", required=true, description="区/县")
     * @ApiParams   (name="address", type="string", required=true, description="详细地址")
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {

        }
        })
     */
    public function create_take(){
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $validate = new \think\Validate([
            'name' => 'require',
            'mobile' => 'require',
            'province' => 'require',
            'city' => 'require',
            'county' => 'require',
            'address' => 'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $param['createtime'] = time();
        $param['updatetime'] = time();
        $param['user_id'] = $user_id;
        $result = Db::name('address')->insert($param);
        if(empty($result)){
            $this->error('sql执行失败');
        }
        $this->success('SUCCESS');
    }
    /**
     * @ApiTitle    (更新收货地址)
     * @ApiSummary  (更新收货地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/userinfo/update_take)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="take_id", type="string", required=true, description="收货地址id")
     * @ApiParams   (name="name", type="string", required=true, description="姓名")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="province", type="string", required=true, description="省")
     * @ApiParams   (name="city", type="string", required=true, description="市")
     * @ApiParams   (name="county", type="string", required=true, description="区/县")
     * @ApiParams   (name="address", type="string", required=true, description="详细地址")
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {

        }
    })
     */
    public function update_take(){
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $validate = new Validate([
            'take_id' => 'require',
            'name' => 'require',
            'mobile' => 'require',
            'province' => 'require',
            'city' => 'require',
            'county' => 'require',
            'address' => 'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $where['user_id'] = ['eq',$user_id];
        $where['id'] = ['eq',$param['take_id']];
        unset($param['take_id']);
        $param['updatetime'] = time();
        $result = Db::name('address')->where($where)->update($param);
        if(empty($result)){
            $this->error('sql执行失败');
        }
        $this->success('SUCCESS');
    }
    /**
     * @ApiTitle    (删除收货地址)
     * @ApiSummary  (删除收货地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/userinfo/delete_take)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="take_id", type="string", required=true, description="收货地址id")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {

    }
    })
     */
    public function delete_take(){
        $user_id = $this->getUserId();
        $take_id = $this->request->param('take_id',0,'intval');
        $where['id'] = ['eq',$take_id];
        $where['user_id'] = ['eq',$user_id];
        $result = Db::name('address')->where($where)->delete();
        if(empty($result)){
            $this->error('sql执行失败');
        }else{
            $this->success('SUCCESS');
        }
    }

    /**
     * @ApiTitle    (生成用户邀请好友二维码)
     * @ApiSummary  (生成用户邀请好友二维码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/userinfo/imgcode)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="route", type="string", required=true, description="页面路径跟参数")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        },
        })
     */
    public function imgcode()
    {
        $domain_name = $this->request->domain();//域名
        $user_id = $this->getUserId();
        $url = $this->request->param('route');
        $url .= $url.'?user_id='.$user_id;
        if(empty($url)){
            $this->error('缺少必要参数');
        }
        $savePath = './../public/upload/poster/';
        if (!file_exists($savePath)){
            mkdir($savePath, 0777,true);
        }
        if(!file_exists("./upload/qrcode/code_$user_id.jpg")){
            $code = $this->getusercode($user_id,$url);
        }else{
            $code = "qrcode/code_$user_id.jpg";
        }
        $data = $domain_name.'/upload/'.$code;
        $this->success('SUCCESS',$data);
    }
    public function getusercode($user_id,$url){
        $savePath = './../public/upload/get_code/';
        $filename = "code_$user_id.jpg";
        if(!file_exists($savePath.$filename)){
            $access_token = $this->get_access_token();
            $qrcode = $this->imgShare($user_id,$access_token,$url);
        }elsE{
            $qrcode = "qrcode/code_".$user_id.".jpg";
        }
        return $qrcode;
    }
    public function imgShare($user_id,$access_token,$path){
        $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token;
        $post['path'] = $path;
        $post['width'] = 280;
        $result = $this->curl($url, json_encode($post));
        $array = json_decode($result, true);
        if($array['errmsg']) {
            return false;
        }
        // 分享二维码保存到用户数据
        if (!file_exists(ROOT_PATH . 'public' . DS . 'upload/qrcode/')) {
            mkdir(ROOT_PATH . 'public' . DS . 'upload/qrcode/', 0777, true);
        }
        $filename = "code_".$user_id.".jpg";
        $furl = ROOT_PATH . 'public' . DS . 'upload/qrcode/'.$filename;
        file_put_contents($furl, $result);
        return "qrcode/".$filename;
    }
    protected function curl($url, $post = null, $header = null) {
        // 初始化
        $curl = curl_init();
        // 设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        // 请求头
        if($header) {
            curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
        }
        // 设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_NOBODY, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        // 设置获取的信息以文件流的形式返回,而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            //把POST的变量加上
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
        }
        // 执行命令
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            $data = curl_error($curl);
        }
        return $data;
    }
    //获取access_token
    public function get_access_token()
    {
        $app_id = config('app_id');
        $app_secret = config('app_secret');
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$app_id.'&secret='.$app_secret;
        $res = $this->http_get($url);
        $json_arr = json_decode($res,true);
        $token = $json_arr['access_token'];
        return $token;
    }
    //curl  get请求
    public function http_get($url){
        $curl = curl_init();//启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $result = curl_exec($curl); //执行操作
        curl_close($curl);
        return $result;
    }
    //curl post请求
    public function http_post($url,$data){
        $curl = curl_init();//启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false); // 开启header
        //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $result = curl_exec($curl); //执行操作
        curl_close($curl);
        return $result;
    }
}