浏览代码

新增密码相关、团队接口

Jason 1 年之前
父节点
当前提交
ffb744ffa4

+ 31 - 0
application/api/controller/Index.php

@@ -4,6 +4,8 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use app\common\model\Config;
+use app\common\model\Moneylog;
+use app\common\model\Order as OrderModel;
 
 /**
  * 首页接口
@@ -53,4 +55,33 @@ class Index extends Api
         ];
         $this->success(__('请求成功'), $data);
     }
+
+    /**
+     * 团队
+     * @return void
+     */
+    public function team()
+    {
+        $user = $this->auth->getUser();
+
+        $info_list = (new \app\common\model\User())
+            ->alias('u')
+            ->join('users_path p', 'p.user_id = u.id')
+            ->where('p.parent_id', $user['id'])
+            ->field('u.avatar,u.bonus_sum,u.mobile,u.nickname')
+            ->order('u.bonus_sum DESC')
+            ->paginate($this->pageSize);
+//        foreach ($info_list as $k => $v) {
+//            $paginator[$k]['status_name'] = (new OrderModel())->getStatusNames($v['status']);
+//        }
+        $res_data = $this->buildResp($info_list->total(), $info_list->currentPage(), $info_list->items());
+        $res_data['bonus_today'] = Moneylog::where('user_id', $user['id'])
+                                        ->where('create_time', '>=', strtotime('today'))
+                                        ->where('action', 'in', [1,2])
+                                        ->sum('amount');
+
+        $res_data['bonus_sum']  = $user['bonus_sum'];
+        $res_data['team_num']   = $user['team_num'];
+        $this->success('', $res_data);
+    }
 }

+ 97 - 57
application/api/controller/Sms.php

@@ -4,6 +4,7 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use app\common\library\Sms as Smslib;
+use app\common\model\Sms AS SmsModel;
 use app\common\model\User;
 use think\Hook;
 use fast\Random;
@@ -11,6 +12,7 @@ use Exception;
 use think\Cache;
 use think\exception\PDOException;
 use think\exception\ValidateException;
+use think\Log;
 
 
 /**
@@ -20,6 +22,17 @@ class Sms extends Api
 {
     protected $noNeedLogin = '*';
     protected $noNeedRight = '*';
+    /**
+     * 验证码有效时长
+     * @var int
+     */
+    protected static $expire = 120;
+
+    /**
+     * 最大允许检测的次数
+     * @var int
+     */
+    protected static $maxCheckNums = 10;
 
     /**
      * 发送验证码
@@ -70,62 +83,99 @@ class Sms extends Api
     }
 
     /**
-     * 检测验证码
-     *
-     * @ApiMethod (POST)
-     * @param string $mobile 手机号
-     * @param string $event 事件名称
-     * @param string $captcha 验证码
+     * 向国际手机号码发放短信
+     * @return void
+     * @throws \think\Exception
      */
-    public function check()
+    public function send_international_sms($country_code, $mobile, $event = '')
     {
-        $mobile = $this->request->post("mobile");
-        $event = $this->request->post("event");
-        $event = $event ? $event : 'register';
-        $captcha = $this->request->post("captcha");
+        $last = SmsModel::where(['country_code' => $country_code, 'mobile' => $mobile, 'event' => $event])
+            ->order('id', 'DESC')
+            ->find();
+        if ($last && time() - $last['create_time'] < 60) {
+            $this->error(__('发送频繁'));
+        }
 
-        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
-            $this->error(__('手机号不正确'));
+        $ipSendTotal = SmsModel::where(['ip' => $this->request->ip()])->whereTime('create_time', '-1 hours')->count();
+        if ($ipSendTotal >= 5) {
+            $this->error(__('发送频繁'));
         }
-        if ($event) {
-            $userinfo = User::getByMobile($mobile);
-            if ($event == 'register' && $userinfo) {
+
+        if ($event == 'register') {
+            $userinfo = User::getByCodeAndMobile($country_code, $mobile);
+            if (!empty($userinfo)) {
                 //已被注册
                 $this->error(__('已被注册'));
-            } elseif (in_array($event, ['changemobile']) && $userinfo) {
-                //被占用
-                $this->error(__('已被占用'));
-            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
-                //未注册
-                $this->error(__('未注册'));
             }
+//              elseif (in_array($event, ['changemobile']) && $userinfo) {
+//                //被占用
+//                $this->error(__('已被占用'));
+//            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
+//                //未注册
+//                $this->error(__('未注册'));
+//            }
         }
-        $ret = Smslib::check($mobile, $captcha, $event);
-        if ($ret) {
-            $this->success(__('成功'));
+
+//        if (!Hook::get('sms_send')) {
+//            $this->error(__('请在后台插件管理安装短信验证插件'));
+//        }
+
+        $code = Random::numeric();//验证码
+
+        $ret = $this->send_sms_unisms($country_code, $mobile, $code, $event);
+        if ($ret['code']) {
+            $time = time();
+            $ip = request()->ip();
+            $sms = SmsModel::create([
+                'country_code'  => $country_code,
+                'event'         => $event,
+                'mobile'        => $mobile,
+                'code'          => $code,
+                'ip'            => $ip,
+            ]);
+            $this->success(__('发送成功') . '-' . $code);
         } else {
-            $this->error(__('验证码不正确'));
+            Log::notice('短信发送失败');
+            Log::error($ret['msg']);
+            $this->error(__('发送失败,请检查短信配置是否正确'));
         }
     }
 
-
     /**
-     * 发送短信验证码
+     * 检测验证码
      *
-     * @ApiMethod (POST)
-     * @param string $phone     手机号
-     * @param string $scene     事件名称     
-     * @param string $countryCode 区号
-     * @param string $len       验证码长度
      */
-    public  function sendCodeSMS($phone, $scene = 'verify',$countryCode = '86', $len = '')
+    public function check($country_code, $mobile, $code, $event = 'verify'): bool
     {
+        $time = time() - self::$expire;
+        $sms = SmsModel::where(['country_code' => $country_code, 'mobile' => $mobile, 'event' => $event])
+            ->order('id', 'DESC')
+            ->find();
+        if ($sms) {
+            if ($sms['create_time'] > $time && $sms['times'] <= self::$maxCheckNums) {
+                $correct = $code == $sms['code'];
+                if (!$correct) {
+                    $sms->times = $sms->times + 1;
+                    $sms->save();
+                    return false;
+                } else {
+                    //$result = Hook::listen('sms_check', $sms, null, true);
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
 
-        $key = $countryCode.$scene . ':' . $phone;
-        $code = Random::numeric($len);
-        //$content = $content == '' ? sprintf(SMSTemplates($scene,$countryCode),$code) : sprintf($content,$code);
-        //$phone = $countryCode . $phone;
-        $phone = '+' . $countryCode . $phone;//拼接国际区号
+
+    /**
+     * 发送短信验证码
+     * 短信平台:https://unisms.apistd.com/
+     */
+    public  function send_sms_unisms($country_code, $phone, $code, $event = 'verify')
+    {
+        return _success();
+        $phone = '+' . $country_code . $phone;//拼接国际区号
         try {
             $url1 = "https://uni.apistd.com";
             $query = [
@@ -134,32 +184,22 @@ class Sms extends Api
             ];
             $url = $url1.'/?'.http_build_query($query);
             $data = [
-                'signature'=>'AEXBTC',
-                'to'=>$phone,
+                'signature'     =>  'AEXBTC',
+                'to'            =>  $phone,
                 //'content'=>$content,
-                'templateId' => 'd33f1f90',
-                'templateData' => ['code' => $code]
+                'templateId'    => 'd33f1f90',
+                'templateData'  => ['code' => $code]
             ];
             $result = xcurl($url,$data);
             $result = json_decode($result, true);
-    //        var_dump($result);
-
-    //        if($result['code'] == 0){
-    //            $result = 1;
-    //        }else {
-    //            $result = 0;
-    //        }
         } catch (ValidateException|PDOException|Exception $e){
-            $this->error($e->getMessage());
+            return _error($e->getMessage());
         }
         if ($result['code'] == 0){
-            Cache::set($key, $code,300);
-            return true;
+            //Cache::set($key, $code,300);
+            return _success();
         }else{
-            return $result['message'];
+            return _error($result['message']);
         }
     }
-
-
-
 }

+ 129 - 0
application/api/controller/User.php

@@ -8,6 +8,7 @@ use app\common\library\Sms;
 use app\common\model\Order as OrderModel;
 use fast\Random;
 use think\Config;
+use think\Env;
 use think\Validate;
 
 /**
@@ -42,6 +43,8 @@ class User extends Api
                                 ->where('user_id', $user['id'])
                                 ->where('status', OrderModel::STATUSFINISH)
                                 ->count();
+        $data['invite_link'] = Env::get('app.invite_domain') . '/?i=' . $user['invitation_code'];
+        $data['invitation_code'] = $user['invitation_code'];
 
         $this->success('', $data);
     }
@@ -71,6 +74,132 @@ class User extends Api
         }
     }
 
+    /**
+     * 重置密码
+     *
+     * @ApiMethod (POST)
+     * @param string $mobile      手机号
+     * @param string $newpassword 新密码
+     * @param string $captcha     验证码
+     */
+    public function change_login_pwd()
+    {
+        $old_pwd        = $this->request->post("old_pwd", '', null);
+        $new_pwd        = $this->request->post("new_pwd", '', null);
+        $confirm_pwd    = $this->request->post("confirm_pwd", '', null);
+
+        $rule = [
+            'old_pwd'       => 'require|regex:\S{6,30}',
+            'new_pwd'       => 'require|regex:\S{6,30}',
+            'confirm_pwd'   => 'require|regex:\S{6,30}|confirm:new_pwd',
+        ];
+
+        $msg = [
+            'new_pwd.confirm' => __('Password and confirm password don\'t match')
+        ];
+        $data = [
+            'old_pwd'       => $old_pwd,
+            'new_pwd'       => $new_pwd,
+            'confirm_pwd'   => $confirm_pwd,
+        ];
+        $field = [
+            'old_pwd'   => __('旧密码'),
+            'new_pwd'   => __('新密码'),
+            'confirm_pwd' => __('新密码')
+        ];
+        $validate = new Validate($rule, $msg, $field);
+        $result = $validate->check($data);
+        if (!$result) {
+            $this->error(__($validate->getError()));
+        }
+
+        $ret = $this->auth->changepwd($new_pwd, $old_pwd);
+        if ($ret) {
+            $this->success(__('Reset password successful'));
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
+
+    /**
+     * 发送重置密码短信验证码
+     * @return void
+     */
+    public function reset_fund_pwd_sms()
+    {
+        $login_pwd = $this->request->post("login_pwd", '', null);
+
+        $user = $this->auth->getUser();
+
+        if ($user->login_pwd != $this->auth->getEncryptPassword($login_pwd, $user->salt)) {
+            $this->error(__('Password is incorrect'));
+        }
+
+        (new \app\api\controller\Sms())->send_international_sms($user['code'], $user['mobile'], 'rest_fund_pwd');
+    }
+    /**
+     * 重置资金密码
+     *
+     * @ApiMethod (POST)
+     * @param string $mobile      手机号
+     * @param string $newpassword 新密码
+     * @param string $captcha     验证码
+     */
+    public function reset_fund_pwd()
+    {
+        $login_pwd      = $this->request->post("login_pwd", '', null);
+        $new_pwd        = $this->request->post("new_pwd", '', null);
+        $confirm_pwd    = $this->request->post("confirm_pwd", '', null);
+        $captcha        = $this->request->post("captcha");
+
+        $rule = [
+            'login_pwd'     => 'require|regex:\S{6,30}',
+            'new_pwd'       => 'require|regex:\S{6,30}',
+            'confirm_pwd'   => 'require|regex:\S{6,30}|confirm:new_pwd',
+        ];
+
+        $msg = [
+            'new_pwd.confirm' => __('Password and confirm password don\'t match')
+        ];
+        $data = [
+            'login_pwd'     => $login_pwd,
+            'new_pwd'       => $new_pwd,
+            'confirm_pwd'   => $confirm_pwd,
+        ];
+        $field = [
+            'login_pwd' => __('登录密码'),
+            'new_pwd'   => __('新资金密码'),
+            'confirm_pwd' => __('新密码')
+        ];
+        $validate = new Validate($rule, $msg, $field);
+        $result = $validate->check($data);
+        if (!$result) {
+            $this->error(__($validate->getError()));
+        }
+
+        if (empty($captcha)) {
+            $this->error(__('Invalid parameters'));
+        }
+
+        $user = $this->auth->getUser();
+
+        //验证手机验证码
+        $ret = (new \app\api\controller\Sms())->check($user['code'],$user['mobile'], $captcha, 'rest_fund_pwd');
+        if (!$ret) {
+            $this->error(__('Captcha is incorrect'));
+        }
+
+        $res = (new \app\common\model\User())
+            ->where('id', $user['id'])
+            ->update([
+                'fund_pwd' => md5($new_pwd)
+            ]);
+
+        if($res){
+            $this->success(__('资金密码重置成功'));
+        }
+        $this->error(__('资金密码重置失败'));
+    }
     /**
      * 手机验证码登录
      *

+ 1 - 0
application/api/lang/zh-cn.php

@@ -8,4 +8,5 @@ return [
     '提交成功'                                         => '提交成功,请等待处理',
     '余额不足'                                         => '您的可用余额不足',
     '资金密码有误'                                      => '资金密码不正确',
+    'Please login first'                              => '请重新登录',
 ];

+ 8 - 0
application/api/lang/zh-cn/sms.php

@@ -0,0 +1,8 @@
+<?php
+
+return [
+    '发送频繁'                 => '发送频繁',
+    '完成'                    => '完成',
+    '冻结'                    => '冻结',
+    '取消'                    => '取消',
+];

+ 8 - 1
application/api/lang/zh-cn/user.php

@@ -18,7 +18,7 @@ return [
     'Username is incorrect'                 => '用户名不正确',
     'Email is incorrect'                    => '邮箱不正确',
     'Account is locked'                     => '账户已经被锁定',
-    'Password is incorrect'                 => '密码不正确',
+    'Password is incorrect'                 => '登录密码不正确',
     'Account is incorrect'                  => '账户不正确',
     'Account not exist'                     => '账户不存在',
     'Account can not be empty'              => '账户不能为空',
@@ -39,4 +39,11 @@ return [
     '邀请码有误'                              => '邀请码不正确',
     '确认密码不一致'                           => '确认密码不一致',
     '账号不存在'                              => '账号不存在',
+    '旧密码'                                 => '旧密码',
+    '新密码'                                 => '新密码',
+    '确认密码'                               => '确认密码',
+    '登录密码'                               => '登录密码',
+    '新资金密码'                              => '新资金密码',
+    '资金密码重置成功'                         => '资金密码重置成功',
+    '资金密码重置失败'                         => '资金密码重置失败',
 ];

+ 1 - 0
application/common/lang/zh-cn.php

@@ -1,4 +1,5 @@
 <?php
 
 return [
+
 ];

+ 2 - 2
application/common/library/Auth.php

@@ -278,12 +278,12 @@ class Auth
             return false;
         }
         //判断旧密码是否正确
-        if ($this->_user->password == $this->getEncryptPassword($oldpassword, $this->_user->salt) || $ignoreoldpassword) {
+        if ($this->_user->login_pwd == $this->getEncryptPassword($oldpassword, $this->_user->salt) || $ignoreoldpassword) {
             Db::startTrans();
             try {
                 $salt = Random::alnum();
                 $newpassword = $this->getEncryptPassword($newpassword, $salt);
-                $this->_user->save(['loginfailure' => 0, 'password' => $newpassword, 'salt' => $salt]);
+                $this->_user->save(['login_pwd' => $newpassword, 'salt' => $salt]);
 
                 Token::delete($this->_token);
                 //修改密码成功的事件

+ 1 - 1
application/common/model/Sms.php

@@ -13,7 +13,7 @@ class Sms extends Model
     // 开启自动写入时间戳字段
     protected $autoWriteTimestamp = 'int';
     // 定义时间戳字段名
-    protected $createTime = 'createtime';
+    protected $createTime = 'create_time';
     protected $updateTime = false;
     // 追加属性
     protected $append = [

+ 0 - 12
application/common/model/User.php

@@ -19,7 +19,6 @@ class User extends Model
     protected $updateTime = false;
     // 追加属性
     protected $append = [
-        'url',
     ];
 
     /**
@@ -118,17 +117,6 @@ class User extends Model
         return $data;
     }
 
-    /**
-     * 获取个人URL
-     * @param string $value
-     * @param array  $data
-     * @return string
-     */
-    public function getUrlAttr($value, $data)
-    {
-        return "/u/" . $data['id'];
-    }
-
     /**
      * 获取头像
      * @param string $value