UserController.php 5.3 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/13
 * Time: 17:04
 */

namespace api\index\controller;


use cmf\controller\RestBaseController;
use think\Cache;
use think\cache\driver\Redis;
use think\Validate;
use think\Db;

/**
 * @title 个人中心
 * @description
 */
class UserController extends RestBaseController
{
    /**
     * @title 登录注册
     * @description 接口说明
     * @author Guosheng
     * @url /index/User/login
     * @method POST
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:phone type:int require:1 default: other: desc:手机号
     * @param name:code type:string require:1 default: other: desc:验证码
     *
     */
    public function login()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $param['create_time'] = time();
        $param['user_id'] = $user_id;
        $validate = new Validate([
            'phone' => 'require|max:11',
            'code'=>'require'
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        $code = Cache::get($param['phone']);
        if($code == $param['code']){
            $data = Db::name('integral')
                ->insert($param);
            if($data){
                $this->success('SUCCESS');
            }
        }else{
            $this->error(['code'=>40002,'msg'=>'验证码错误或者失效,请重新发送']);
        }
    }

    /**
     * @title 获取验证码
     * @description
     * @author Guosheng
     * @url /index/User/getcode
     * @method POST
     *
     * @param name:phone type:string require:1 other: desc:手机号码
     *
     * @return code:验证码
     */
    public function getcode()
    {
        $phone = $this->request->param('phone');
        if (empty($phone)) {
            $this->error(['code' => 40005, 'msg' => '缺少必要参数']);
        }
        if (!preg_match('/^1[0-9]{10}$/', $phone)) {
            $this->error(['code' => 40005, 'msg' => "请输入正确的手机格式!"]);
        }
        //生成验证码
        $number = generateCode();
        //发送验证码
        $data = array(
            'content' => "您的验证码是:" . $number."十分钟之内有效",//短信内容
            'mobile' => $phone,//手机号码
            'productid' => '676767',//产品id
            'xh' => ''//小号
        );
        $result = send_sms($data);
        if (substr($result, 0, strpos($result, ',')) != "1") {
            $this->error(['code' => 42001, 'msg' => $result]);
        }
        Cache::set($phone,$number,600);
//        $code = Db::name('code')
//            ->where('phone',$phone)
//            ->find();
//        if (empty($code)) {
//            $arr['phone'] = $phone;
//            $arr['create_time'] = time();
//            $arr['expire_time'] = time() + 60;
//            $arr['code'] = $number;
//            $result = Db::name('code')
//                ->insertGetId($arr);
//        } else {
//            $arr['code'] = $number;
//            $arr['update_time'] = time();
//            $arr['expire_time'] = time() + 60;
//            $result = Db::name('code')
//                ->where('phone',$phone)
//                ->update($arr);
//        }
//        if (empty($result)) {
//            $this->error(['code' => 40006, 'msg' => 'sql执行失败']);
//        }

        $this->success('SUCCESS', ['code' => $number]);
    }

    /**
     * @title 我的首页
     * @description 接口说明
     * @author Guosheng
     * @url /index/User/userInfo
     * @method POST
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @return user_nickname:昵称
     * @return avatar:头像
     * @return now:当前积分
     * @return num:参与次数
     * @return add_total:累计积分
     *
     */
    public function userInfo()
    {
        $user_id = $this->getUserId();
        $info = Db::name('integral')
            ->alias('a')
            ->join('user b','a.user_id = b.id')
            ->field('a.*,b.user_nickname,b.avatar')
            ->where('a.user_id',$user_id)
            ->find();
        if(empty($info)){
            $this->error(['code'=>40006,'msg'=>'您还没有注册,请先注册!']);
        }
        $this->success('SUCCESS',$info);

    }

    /**
     * @title 充值积分列表
     * @description 接口说明
     * @author Guosheng
     * @url /index/User/pay
     * @method POST
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @return integral:积分
     * @return money:金额
     *
     */
    public function pay(){
        $user_id = $this->getUserId();
        $data = Db::name('pay')
            ->where('delete_time',0)
            ->select();
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 提现金额列表
     * @description 接口说明
     * @author Guosheng
     * @url /index/User/money
     * @method POST
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @return integral:积分
     * @return money:金额
     *
     */
    public function money(){
        $user_id = $this->getUserId();
        $data = Db::name('money')
            ->where('delete_time',0)
            ->select();
        $this->success('SUCCESS',$data);
    }

}