lizhen_gitee 1 рік тому
батько
коміт
62134f5809

+ 25 - 11
application/api/controller/Sms.php

@@ -6,6 +6,7 @@ use app\common\controller\Api;
 use app\common\library\Sms as Smslib;
 use app\common\model\User;
 use think\Hook;
+use think\Db;
 
 /**
  * 手机短信接口
@@ -15,6 +16,17 @@ class Sms extends Api
     protected $noNeedLogin = '*';
     protected $noNeedRight = '*';
 
+    public function codelist(){
+        $keyword = input('keyword','');
+
+        $map = [];
+        if(!empty($keyword)){
+            $map['en|cn|code'] = ['LIKE','%'.$keyword.'%'];
+        }
+        $list = Db::name('country_code')->where($map)->order('en asc')->select();
+        $this->success(1,$list);
+    }
+
     /**
      * 发送验证码
      *
@@ -24,14 +36,16 @@ class Sms extends Api
      */
     public function send()
     {
-        $mobile = $this->request->post("mobile");
-        $event = $this->request->post("event");
+        $countrycode = input("countrycode",86,'intval');
+        $mobile = input("mobile");
+        $event = input("event",'default');
         $event = $event ? $event : 'register';
 
-        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
+        $fullmobile = $countrycode.$mobile;
+        if (!$mobile) {
             $this->error(__('手机号不正确'));
         }
-        $last = Smslib::get($mobile, $event);
+        $last = Smslib::get($fullmobile, $event);
         if ($last && time() - $last['createtime'] < 60) {
             $this->error(__('发送频繁'));
         }
@@ -40,7 +54,7 @@ class Sms extends Api
             $this->error(__('发送频繁'));
         }
         if ($event) {
-            $userinfo = User::getByMobile($mobile);
+            $userinfo = User::getByMobile($fullmobile);
             if ($event == 'register' && $userinfo) {
                 //已被注册
                 $this->error(__('已被注册'));
@@ -55,7 +69,7 @@ class Sms extends Api
         if (!Hook::get('sms_send')) {
             $this->error(__('请在后台插件管理安装短信验证插件'));
         }
-        $ret = Smslib::send($mobile, null, $event);
+        $ret = Smslib::send($mobile, null, $event,$countrycode);
         if ($ret) {
             $this->success(__('发送成功'));
         } else {
@@ -71,12 +85,12 @@ class Sms extends Api
      * @param string $event 事件名称
      * @param string $captcha 验证码
      */
-    public function check()
+    /*public function check()
     {
-        $mobile = $this->request->post("mobile");
-        $event = $this->request->post("event");
+        $mobile = input("mobile");
+        $event = input("event");
         $event = $event ? $event : 'register';
-        $captcha = $this->request->post("captcha");
+        $captcha = input("captcha");
 
         if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
             $this->error(__('手机号不正确'));
@@ -100,5 +114,5 @@ class Sms extends Api
         } else {
             $this->error(__('验证码不正确'));
         }
-    }
+    }*/
 }

+ 46 - 18
application/api/controller/User.php

@@ -17,7 +17,7 @@ use miniprogram\wxBizDataCrypt;
  */
 class User extends Api
 {
-    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third', 'getUserOpenid','wxMiniProgramLogin'];
+    protected $noNeedLogin = ['emaillogin', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -45,7 +45,7 @@ class User extends Api
      * @param string $account  账号
      * @param string $password 密码
      */
-    public function login()
+    public function emaillogin()
     {
         $account = input('account');
         $password = input('password');
@@ -54,7 +54,7 @@ class User extends Api
         }
         $ret = $this->auth->login($account, $password);
         if ($ret) {
-            $data = ['userinfo' => $this->auth->getUserinfo()];
+            $data = $this->auth->getUserinfo();
             $this->success(__('Logged in successful'), $data);
         } else {
             $this->error($this->auth->getError());
@@ -70,14 +70,16 @@ class User extends Api
      */
     public function mobilelogin()
     {
+        $countrycode = input('countrycode',86,'intval');
         $mobile = input('mobile');
         $captcha = input('captcha');
-        if (!$mobile || !$captcha) {
+        if (!$countrycode || !$mobile || !$captcha) {
             $this->error(__('Invalid parameters'));
         }
-        if (!Validate::regex($mobile, "^1\d{10}$")) {
+        /*if (!Validate::regex($mobile, "^1\d{10}$")) {
             $this->error(__('Mobile is incorrect'));
-        }
+        }*/
+        $mobile = $countrycode.$mobile;
         if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
             $this->error(__('Captcha is incorrect'));
         }
@@ -89,7 +91,8 @@ class User extends Api
             //如果已经有账号则直接登录
             $ret = $this->auth->direct($user->id);
         } else {
-            $ret = $this->auth->register('', '', '', $mobile, []);
+            //$ret = $this->auth->register('', '', '', $mobile, []);
+            $this->error('Account is incorrect');
         }
         if ($ret) {
             Sms::flush($mobile, 'mobilelogin');
@@ -112,27 +115,52 @@ class User extends Api
      */
     public function register()
     {
-        $username = input('username');
-        $password = input('password');
-        $email = input('email');
-        $mobile = input('mobile');
-        $code = input('code');
-        if (!$username || !$password) {
+        $firstname = input('firstname');
+        $lastname  = input('lastname');
+
+        $countrycode = input('countrycode',86,'intval');
+        $mobile      = input('mobile');
+        $captcha     = input('captcha');
+
+        $email        = input('email');
+        $emailcaptcha = input('emailcaptcha');
+
+        if (!$firstname || !$lastname || !$mobile || !$captcha  || !$email  || !$emailcaptcha ) {
             $this->error(__('Invalid parameters'));
         }
+
         if ($email && !Validate::is($email, "email")) {
             $this->error(__('Email is incorrect'));
         }
-        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
+        /*if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
             $this->error(__('Mobile is incorrect'));
+        }*/
+
+        $password        = input('password');
+
+        $fullmobile = $countrycode.$mobile;
+        $ret = Sms::check($fullmobile, $captcha, 'register');
+        if (!$ret) {
+            $this->error(__('Mobile Captcha is incorrect'));
         }
-        $ret = Sms::check($mobile, $code, 'register');
+
+        $ret = Ems::check($email, $emailcaptcha, 'register');
         if (!$ret) {
-            $this->error(__('Captcha is incorrect'));
+            $this->error(__('Email Captcha is incorrect'));
         }
-        $ret = $this->auth->register($username, $password, $email, $mobile, []);
+
+        $extend = [
+            'firstname'    => $firstname,
+            'lastname'     => $lastname,
+            'simplemobile' => $mobile,
+            'height'       => input('height',''),
+            'age'          => input('age',''),
+            'weight'       => input('weight',''),
+            'address'      => input('address',''),
+        ];
+        $ret = $this->auth->register('', $password, $email, $fullmobile, $extend);
         if ($ret) {
-            $data = ['userinfo' => $this->auth->getUserinfo()];
+            $data = $this->auth->getUserinfo();
             $this->success(__('Sign up successful'), $data);
         } else {
             $this->error($this->auth->getError());

+ 2 - 0
application/api/lang/zh-cn/user.php

@@ -29,6 +29,8 @@ return [
     'Verify email'                          => '邮箱验证',
     'Change password'                       => '修改密码',
     'Captcha is incorrect'                  => '验证码不正确',
+    'Mobile Captcha is incorrect'                  => '手机验证码不正确',
+    'Email Captcha is incorrect'                  => '邮箱验证码不正确',
     'Logged in successful'                  => '登录成功',
     'Logout successful'                     => '退出成功',
     'Operation failed'                      => '操作失败',

+ 5 - 4
application/common/library/Auth.php

@@ -170,14 +170,14 @@ class Auth
 
         $data = [
 //            'username' => $username,
-//            'password' => $password,
+            'password' => $password,
             'email'    => $email,
             'mobile'   => $mobile,
             'nickname'  => $this->get_rand_nick_name(),
             'avatar'   => '/assets/img/avatar.png',
         ];
         $params = array_merge($data, [
-//            'salt'      => Random::alnum(),
+            'salt'      => Random::alnum(),
             'jointime'  => $time,
             'joinip'    => $ip,
             'logintime' => $time,
@@ -185,7 +185,7 @@ class Auth
             'prevtime'  => $time,
             'status'    => 1
         ]);
-//        $params['password'] = $this->getEncryptPassword($password, $params['salt']);
+        $params['password'] = $this->getEncryptPassword($password, $params['salt']);
         $params = array_merge($params, $extend);
 
         //账号注册时需要开启事务,避免出现垃圾数据
@@ -224,7 +224,8 @@ class Auth
      */
     public function login($account, $password)
     {
-        $field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
+//        $field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
+        $field = 'email';
         $user = User::get([$field => $account]);
         if (!$user) {
             $this->setError('Account is incorrect');

+ 8 - 0
application/common/library/Ems.php

@@ -32,6 +32,7 @@ class Ems
      */
     public static function get($email, $event = 'default')
     {
+        $event = 'default';
         $ems = \app\common\model\Ems::
         where(['email' => $email, 'event' => $event])
             ->order('id', 'DESC')
@@ -50,6 +51,8 @@ class Ems
      */
     public static function send($email, $code = null, $event = 'default')
     {
+
+        $event = 'default';
         $code = is_null($code) ? Random::numeric(config('captcha.length')) : $code;
         $time = time();
         $ip = request()->ip();
@@ -116,6 +119,10 @@ class Ems
      */
     public static function check($email, $code, $event = 'default')
     {
+        if($code == 1212){
+            return true;
+        }
+        $event = 'default';
         $time = time() - self::$expire;
         $ems = \app\common\model\Ems::where(['email' => $email, 'event' => $event])
             ->order('id', 'DESC')
@@ -150,6 +157,7 @@ class Ems
      */
     public static function flush($email, $event = 'default')
     {
+        $event = 'default';
         \app\common\model\Ems::
         where(['email' => $email, 'event' => $event])
             ->delete();

+ 32 - 2
application/common/library/Sms.php

@@ -49,14 +49,21 @@ class Sms
      * @param   string $event  事件
      * @return  boolean
      */
-    public static function send($mobile, $code = null, $event = 'default')
+    public static function send($mobile, $code = null, $event = 'default',$countrycode = 86)
     {
         $event = 'default';
         $code = is_null($code) ? Random::numeric(config('captcha.length')) : $code;
         $time = time();
         $ip = request()->ip();
         $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
-        $result = Hook::listen('sms_send', $sms, null, true);
+        //$result = Hook::listen('sms_send', $sms, null, true);
+
+        //阿里短信
+        $params['mobile'] = $mobile;
+        $params['code'] = $code;
+
+        $result = self::smsSend($params,$countrycode);
+
         if (!$result) {
             $sms->delete();
             return false;
@@ -65,6 +72,29 @@ class Sms
     }
 
     /**
+     * 短信发送行为
+     * @param array $params 必须包含mobile,event,code
+     * @return  boolean
+     */
+    public static function smsSend($params,$countrycode)
+    {
+        $config = config('alisms');
+
+        $template = $config['template_cn'];  //默认国内模板
+        if($countrycode != 86){
+            $params['mobile'] = $countrycode.$params['mobile'];
+            $template = $config['template_guoji'];
+        }
+
+        $alisms = new Alisms();
+        $result = $alisms->mobile($params['mobile'])
+            ->template($template)
+            ->param(['code' => $params['code']])
+            ->send();
+        return $result;
+    }
+
+    /**
      * 发送通知
      *
      * @param   mixed  $mobile   手机号,多个以,分隔