lizhen_gitee 3 роки тому
батько
коміт
c41f1f9375
3 змінених файлів з 177 додано та 2 видалено
  1. 74 2
      application/api/controller/User.php
  2. 10 0
      application/config.php
  3. 93 0
      extend/onlogin/onlogin.php

+ 74 - 2
application/api/controller/User.php

@@ -12,13 +12,14 @@ use think\Validate;
 use app\common\library\Token;
 use think\Db;
 use app\common\model\UserDeviceInfo;
+use onlogin\onlogin;
 
 /**
  * 会员接口,登录,注册,修改资料等
  */
 class User extends Api
 {
-    protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
+    protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'onlogin'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -178,6 +179,74 @@ class User extends Api
         }
     }
 
+    /**
+     * 运营商一键登录
+     */
+    public function onLogin()
+    {
+        $accessToken = input('accessToken');// 运营商预取号获取到的token
+        $token = input('tokenT');// 易盾返回的token
+        if (!$accessToken || !$token) {
+            $this->error("参数获取失败!");
+        }
+
+        $params = array(
+            // 运营商预取号获取到的token
+            "accessToken" => $accessToken,
+            // 易盾返回的token
+            "token"       => $token
+        );
+
+        // 获取密钥配置
+        $configInfo = config("onLogin");
+        $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
+
+        $onret = $onlogin->check($params);
+
+//        $ret = [];
+//        $ret["code"] = 200;
+//        $ret["msg"] = "ok";
+//        $ret["data"] = [
+//            "phone" => "17574504021",
+//            "resultCode" => 0
+//        ];
+
+        if ($onret["code"] == 200) {
+            $mobile = $onret["data"]["phone"];
+            if (empty($mobile)) {
+                // 取号失败,建议进行二次验证,例如短信验证码
+                $this->error("取号登录失败,请用验证码方式登录!");
+            } else {
+                // 取号成功, 执行登录等流程
+                // 用户登录逻辑 === 开始
+
+                $user = \app\common\model\User::getByMobile($mobile);
+                if ($user) {
+                    if ($user->status != 1) {
+                        $this->error(__('Account is locked'));
+                    }
+                    //如果已经有账号则直接登录
+                    $ret = $this->auth->direct($user->id);
+                    $is_register = 0;
+                } else {
+                    $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
+                    $is_register = 1;
+                }
+
+                //结果
+                $rs['userinfo'] = $this->auth->getUserinfo();
+                $rs['is_register'] = $is_register;
+                if ($ret) {
+                    $this->success(__('Logged in successful'), $rs);
+                } else {
+                    $this->error($this->auth->getError());
+                }
+                // 用户登录逻辑 === 结束
+            }
+        } else {
+            $this->error("登录失败,请用验证码方式登录!");
+        }
+    }
 
     //用户详细资料
     public function userInfo($type = 1){
@@ -622,7 +691,7 @@ class User extends Api
         $user->save();
 
         Sms::flush($mobile, 'changemobile');
-        $this->success();
+        $this->success('success',$this->userInfo('return'));
     }
 
     /**
@@ -785,4 +854,7 @@ class User extends Api
         //首页接口调用,这里不反回信息
 //        $this->success("更新成功!");
     }
+
+
+
 }

+ 10 - 0
application/config.php

@@ -345,4 +345,14 @@ return [
         'port'      => 6379,
     ],
 
+    // 运营商一键登录
+    'onLogin'        => [
+        //
+        'secretid'     => 'bae41209948a10b6102801ed3936c38c',
+        //
+        'secretkey'      => '9f49687b769b1a689db7e9448da74deb',
+        //
+        'businessid'      => 'ff9bdf7f2ac14967a72f05c94171e8b0',
+    ],
+
 ];

+ 93 - 0
extend/onlogin/onlogin.php

@@ -0,0 +1,93 @@
+<?php
+namespace onlogin;
+
+class Onlogin
+{
+    private $secretid;
+    private $secretkey;
+    private $businessid;
+    private $api_url = "https://ye.dun.163yun.com/v1/oneclick/check";
+    private $version = "v1";
+    private $api_timeout = 5;
+    private $internal_string_charset = "auto";
+
+    /**
+     * 构造函数
+     * @param $sessionKey string 用户在小程序登录后获取的会话密钥
+     * @param $appid string 小程序的appid
+     */
+    public function __construct($secretid, $secretkey, $businessid)
+    {
+        $this->secretid = $secretid;
+        $this->secretkey = $secretkey;
+        $this->businessid = $businessid;
+        $this->api_url = "https://ye.dun.163yun.com/v1/oneclick/check";
+        $this->version = "v1";
+        $this->api_timeout = 5;
+        $this->internal_string_charset = "auto";
+    }
+
+    /**
+     * 计算参数签名
+     * $params 请求参数
+     * $secretKey secretKey
+     */
+
+    function gen_signature($secretKey, $params)
+    {
+        ksort($params);
+        $buff = "";
+        foreach ($params as $key => $value) {
+            if ($value !== null) {
+                $buff .= $key;
+                $buff .= $value;
+            }
+        }
+        $buff .= $secretKey;
+        return md5($buff);
+    }
+    /**
+     * 将输入数据的编码统一转换成utf8
+     * @params 输入的参数
+     */
+    function toUtf8($params)
+    {
+        $utf8s = array();
+        foreach ($params as $key => $value) {
+            $utf8s[$key] = is_string($value) ? mb_convert_encoding($value, "utf8", $this->internal_string_charset) : $value;
+        }
+        return $utf8s;
+    }
+    /**
+     * 易盾本机验证在线检测请求接口简单封装
+     * $params 请求参数
+     */
+    function check($params)
+    {
+        $params["secretId"] = $this->secretid;
+        $params["businessId"] = $this->businessid;
+        $params["version"] = $this->version;
+        $params["timestamp"] = sprintf("%d", round(microtime(true) * 1000));
+        // time in milliseconds
+        $params["nonce"] = substr(md5(time()), 0, 32);
+        // random int
+        $params = $this->toUtf8($params);
+        $params["signature"] = $this->gen_signature($this->secretkey, $params);
+        $options = array('http' => array(
+            'header' => "Content-type: application/x-www-form-urlencoded\r\n",
+            'method' => 'POST',
+            'timeout' => $this->api_timeout,
+            // read timeout in seconds
+            'content' => http_build_query($params)
+        ));
+        $context = stream_context_create($options);
+        $result = file_get_contents($this->api_url, false, $context);
+        if ($result === FALSE) {
+            return array("code" => 500, "msg" => "file_get_contents failed.");
+        } else {
+            return json_decode($result, true);
+        }
+    }
+
+}
+