Thinkphp6.0对接腾讯IM

官方TLSSigAPIv2.php类用于校验用户签名和生成用户签名,将此类放在您适合放置的位置:

<?php

namespace app\api\controller;

if ( version_compare( PHP_VERSION, '5.1.2' ) < 0 ) {
    trigger_error( 'need php 5.1.2 or newer', E_USER_ERROR );
}

class TLSSigAPIv2 {

    private $key = false;
    private $sdkappid = 0;

    /**
     *【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
     *
     *【参数说明】
     * @param string userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
     * @param string expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
     * @return string 签名字符串
     * @throws \Exception
     */

    public function genUserSig( $userid, $expire = 86400*180 ) {
        return $this->__genSig( $userid, $expire, '', false );
    }

    /**
     *【功能说明】
     * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
     * PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
     *  - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
     *  - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
     * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
     *
     *【参数说明】
     * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
     * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
     * @param roomid - 房间号,用于指定该 userid 可以进入的房间号
     * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
     *  - 第 1 位:0000 0001 = 1,创建房间的权限
     *  - 第 2 位:0000 0010 = 2,加入房间的权限
     *  - 第 3 位:0000 0100 = 4,发送语音的权限
     *  - 第 4 位:0000 1000 = 8,接收语音的权限
     *  - 第 5 位:0001 0000 = 16,发送视频的权限
     *  - 第 6 位:0010 0000 = 32,接收视频的权限
     *  - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
     *  - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
     *  - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
     *  - privilegeMap == 0010 1010 == 42  代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
     */

    public function genPrivateMapKey( $userid, $expire, $roomid, $privilegeMap ) {
        $userbuf = $this->__genUserBuf( $userid, $roomid, $expire, $privilegeMap, 0, '' );
        return $this->__genSig( $userid, $expire, $userbuf, true );
    }
    /**
     *【功能说明】
     * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
     * PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
     *  - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
     *  - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
     * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
     *
     *【参数说明】
     * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
     * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
     * @param roomstr - 房间号,用于指定该 userid 可以进入的房间号
     * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
     *  - 第 1 位:0000 0001 = 1,创建房间的权限
     *  - 第 2 位:0000 0010 = 2,加入房间的权限
     *  - 第 3 位:0000 0100 = 4,发送语音的权限
     *  - 第 4 位:0000 1000 = 8,接收语音的权限
     *  - 第 5 位:0001 0000 = 16,发送视频的权限
     *  - 第 6 位:0010 0000 = 32,接收视频的权限
     *  - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
     *  - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
     *  - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
     *  - privilegeMap == 0010 1010 == 42  代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
     */

    public function genPrivateMapKeyWithStringRoomID( $userid, $expire, $roomstr, $privilegeMap ) {
        $userbuf = $this->__genUserBuf( $userid, 0, $expire, $privilegeMap, 0, $roomstr );
        return $this->__genSig( $userid, $expire, $userbuf, true );
    }

    public function __construct( $sdkappid, $key ) {
        $this->sdkappid = $sdkappid;
        $this->key = $key;
    }

    /**
     * 用于 url 的 base64 encode
     * '+' => '*', '/' => '-', '=' => '_'
     * @param string $string 需要编码的数据
     * @return string 编码后的base64串,失败返回false
     * @throws \Exception
     */

    private function base64_url_encode( $string ) {
        static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
        $base64 = base64_encode( $string );
        if ( $base64 === false ) {
            throw new \Exception( 'base64_encode error' );
        }
        return str_replace( array_keys( $replace ), array_values( $replace ), $base64 );
    }

    /**
     * 用于 url 的 base64 decode
     * '+' => '*', '/' => '-', '=' => '_'
     * @param string $base64 需要解码的base64串
     * @return string 解码后的数据,失败返回false
     * @throws \Exception
     */

    private function base64_url_decode( $base64 ) {
        static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
        $string = str_replace( array_values( $replace ), array_keys( $replace ), $base64 );
        $result = base64_decode( $string );
        if ( $result == false ) {
            throw new \Exception( 'base64_url_decode error' );
        }
        return $result;
    }
    /**
     * TRTC业务进房权限加密串使用用户定义的userbuf
     * @brief 生成 userbuf
     * @param account 用户名
     * @param dwSdkappid sdkappid
     * @param dwAuthID  数字房间号
     * @param dwExpTime 过期时间:该权限加密串的过期时间. 过期时间 = now+dwExpTime
     * @param dwPrivilegeMap 用户权限,255表示所有权限
     * @param dwAccountType 用户类型, 默认为0
     * @param roomStr 字符串房间号
     * @return userbuf string  返回的userbuf
     */

    private function __genUserBuf( $account, $dwAuthID, $dwExpTime, $dwPrivilegeMap, $dwAccountType,$roomStr ) {

        //cVer  unsigned char/1 版本号,填0
        if($roomStr == '')
            $userbuf = pack( 'C1', '0' );
        else
            $userbuf = pack( 'C1', '1' );

        $userbuf .= pack( 'n', strlen( $account ) );
        //wAccountLen   unsigned short /2   第三方自己的帐号长度
        $userbuf .= pack( 'a'.strlen( $account ), $account );
        //buffAccount   wAccountLen 第三方自己的帐号字符
        $userbuf .= pack( 'N', $this->sdkappid );
        //dwSdkAppid    unsigned int/4  sdkappid
        $userbuf .= pack( 'N', $dwAuthID );
        //dwAuthId  unsigned int/4  群组号码/音视频房间号
        $expire = $dwExpTime + time();
        $userbuf .= pack( 'N', $expire );
        //dwExpTime unsigned int/4  过期时间 (当前时间 + 有效期(单位:秒,建议300秒))
        $userbuf .= pack( 'N', $dwPrivilegeMap );
        //dwPrivilegeMap unsigned int/4  权限位
        $userbuf .= pack( 'N', $dwAccountType );
        //dwAccountType  unsigned int/4
        if($roomStr != '')
        {
            $userbuf .= pack( 'n', strlen( $roomStr ) );
            //roomStrLen   unsigned short /2   字符串房间号长度
            $userbuf .= pack( 'a'.strlen( $roomStr ), $roomStr );
            //roomStr   roomStrLen 字符串房间号
        }
        return $userbuf;
    }
    /**
     * 使用 hmac sha256 生成 sig 字段内容,经过 base64 编码
     * @param $identifier 用户名,utf-8 编码
     * @param $curr_time 当前生成 sig 的 unix 时间戳
     * @param $expire 有效期,单位秒
     * @param $base64_userbuf base64 编码后的 userbuf
     * @param $userbuf_enabled 是否开启 userbuf
     * @return string base64 后的 sig
     */

    private function hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled ) {
        $content_to_be_signed = 'TLS.identifier:' . $identifier . "\n"
            . 'TLS.sdkappid:' . $this->sdkappid . "\n"
            . 'TLS.time:' . $curr_time . "\n"
            . 'TLS.expire:' . $expire . "\n";
        if ( true == $userbuf_enabled ) {
            $content_to_be_signed .= 'TLS.userbuf:' . $base64_userbuf . "\n";
        }
        return base64_encode( hash_hmac( 'sha256', $content_to_be_signed, $this->key, true ) );
    }

    /**
     * 生成签名。
     *
     * @param $identifier 用户账号
     * @param int $expire 过期时间,单位秒,默认 180 天
     * @param $userbuf base64 编码后的 userbuf
     * @param $userbuf_enabled 是否开启 userbuf
     * @return string 签名字符串
     * @throws \Exception
     */

    private function __genSig( $identifier, $expire, $userbuf, $userbuf_enabled ) {
        $curr_time = time();
        $sig_array = Array(
            'TLS.ver' => '2.0',
            'TLS.identifier' => strval( $identifier ),
            'TLS.sdkappid' => intval( $this->sdkappid ),
            'TLS.expire' => intval( $expire ),
            'TLS.time' => intval( $curr_time )
        );

        $base64_userbuf = '';
        if ( true == $userbuf_enabled ) {
            $base64_userbuf = base64_encode( $userbuf );
            $sig_array['TLS.userbuf'] = strval( $base64_userbuf );
        }

        $sig_array['TLS.sig'] = $this->hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled );
        if ( $sig_array['TLS.sig'] === false ) {
            throw new \Exception( 'base64_encode error' );
        }
        $json_str_sig = json_encode( $sig_array );
        if ( $json_str_sig === false ) {
            throw new \Exception( 'json_encode error' );
        }
        $compressed = gzcompress( $json_str_sig );
        if ( $compressed === false ) {
            throw new \Exception( 'gzcompress error' );
        }
        return $this->base64_url_encode( $compressed );
    }

    /**
     * 验证签名。
     *
     * @param string $sig 签名内容
     * @param string $identifier 需要验证用户名,utf-8 编码
     * @param int $init_time 返回的生成时间,unix 时间戳
     * @param int $expire_time 返回的有效期,单位秒
     * @param string $userbuf 返回的用户数据
     * @param string $error_msg 失败时的错误信息
     * @return boolean 验证是否成功
     * @throws \Exception
     */

    private function __verifySig( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
        try {
            $error_msg = '';
            $compressed_sig = $this->base64_url_decode( $sig );
            $pre_level = error_reporting( E_ERROR );
            $uncompressed_sig = gzuncompress( $compressed_sig );
            error_reporting( $pre_level );
            if ( $uncompressed_sig === false ) {
                throw new \Exception( 'gzuncompress error' );
            }
            $sig_doc = json_decode( $uncompressed_sig );
            if ( $sig_doc == false ) {
                throw new \Exception( 'json_decode error' );
            }
            $sig_doc = ( array )$sig_doc;
            if ( $sig_doc['TLS.identifier'] !== $identifier ) {
                throw new \Exception( "identifier dosen't match" );
            }
            if ( $sig_doc['TLS.sdkappid'] != $this->sdkappid ) {
                throw new \Exception( "sdkappid dosen't match" );
            }
            $sig = $sig_doc['TLS.sig'];
            if ( $sig == false ) {
                throw new \Exception( 'sig field is missing' );
            }

            $init_time = $sig_doc['TLS.time'];
            $expire_time = $sig_doc['TLS.expire'];

            $curr_time = time();
            if ( $curr_time > $init_time+$expire_time ) {
                throw new \Exception( 'sig expired' );
            }

            $userbuf_enabled = false;
            $base64_userbuf = '';
            if ( isset( $sig_doc['TLS.userbuf'] ) ) {
                $base64_userbuf = $sig_doc['TLS.userbuf'];
                $userbuf = base64_decode( $base64_userbuf );
                $userbuf_enabled = true;
            }
            $sigCalculated = $this->hmacsha256( $identifier, $init_time, $expire_time, $base64_userbuf, $userbuf_enabled );

            if ( $sig != $sigCalculated ) {
                throw new \Exception( 'verify failed' );
            }

            return true;
        } catch ( \Exception $ex ) {
            $error_msg = $ex->getMessage();
            return false;
        }
    }

    /**
     * 带 userbuf 验证签名。
     *
     * @param string $sig 签名内容
     * @param string $identifier 需要验证用户名,utf-8 编码
     * @param int $init_time 返回的生成时间,unix 时间戳
     * @param int $expire_time 返回的有效期,单位秒
     * @param string $error_msg 失败时的错误信息
     * @return boolean 验证是否成功
     * @throws \Exception
     */

    public function verifySig( $sig, $identifier, &$init_time, &$expire_time, &$error_msg ) {
        $userbuf = '';
        return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
    }

    /**
     * 验证签名
     * @param string $sig 签名内容
     * @param string $identifier 需要验证用户名,utf-8 编码
     * @param int $init_time 返回的生成时间,unix 时间戳
     * @param int $expire_time 返回的有效期,单位秒
     * @param string $userbuf 返回的用户数据
     * @param string $error_msg 失败时的错误信息
     * @return boolean 验证是否成功
     * @throws \Exception
     */

    public function verifySigWithUserBuf( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
        return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
    }
}

自己写的一个类,包括了现有用户的同步,导入、删除和查询状态,加入群聊和发送消息:```php <?php namespace app\api\controller; use hg\apidoc\annotation as Apidoc; use think\input; use think\facade\Db; use think\Request; use think\facade\Filesystem;

/**

  • @Apidoc\Title("登录相关") */ class Login { private $sdkappid = ; private $identifier = "administrator"; private $secret=''; public function genUserSig($id) { $TLSSigAPIv2 = new TLSSigAPIv2($this->sdkappid,$this->identifier); return $TLSSigAPIv2->genUserSig($id); } public function randomsum($sum){ $z=""; for($i=0;$i<$sum/4;$i++){ $z .=rand(1000,9999); }

    return $z;

    }

    /**

    • curl post 模拟发送数据
    • @param $cate
    • @return array */ public function postCurls($url, $data){ $data = json_encode($data); $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //HTTP浏览器访问版本 curl_setopt($curl, CURLOPT_HEADER, false); //头部信息 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //返回的内容作为变量储存 curl_setopt($curl, CURLOPT_URL, $url); //访问网址 curl_setopt($curl, CURLOPT_POST, true); //post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //请求数据 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false); //对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //从证书中检查SSL加密算法是否存在 $result = curl_exec($curl); //抓取URL并把它传递给浏览器 curl_close($curl); //关闭当前curl return $result; }

    public function test(){ // $checkitem=[['UserID'=>'wangqiang'],['UserID'=>'wangweidong'],['UserID'=>'admin']]; // return $this->account_check($checkitem); //return $this->account_import('wangweidong','王伟东','http://xiangqin.dianjingkeji.net/storage/img/20210715/8022fabc3c0ad0a0c276b6e55190642e.jpg'); // $content['Text']='哈喽啊'; // return $this->sendmsg('wangqiang','wangweidong',$content);

    //return $this->querystate(['wangweidong','wangqiang'],1);
    $memberlist=[['Member_Account'=>'wangqiang'],['Member_Account'=>'wangweidong']];
    return $this->addqcy('@TGS#22QMC3IHV',$memberlist);

    } //查询帐号 public function account_check($CheckItem) {

    $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_check?";
    $url = $this->geturlparameter($url);

    // echo $url; // exit; $data = [ "CheckItem" => $CheckItem

    ];
    
    $s = $this->postCurls($url,$data);
    
    return $s;

    } //自有帐号导入即时通信 public function account_import($Identifier,$Nick,$FaceUrl) {

    $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import?";
    
    $url = $this->geturlparameter($url);
    
    $data = [
        "Identifier" => $Identifier,
        "Nick" => $Nick,
        "FaceUrl" => $FaceUrl
    ];
    // var_dump($data);die;
    $s = $this->postCurls($url,$data);
    return $s;

    } public function sendmsg($fromarrcount,$toarrcount,$content){ $url = "https://console.tim.qq.com/v4/openim/sendmsg?"; $url=$this->geturlparameter($url); $data=[ "SyncOtherMachine"=>1, "From_Account"=>$fromarrcount, "MsgRandom"=>rand(1000000,9999999), "To_Account"=>$toarrcount, "MsgRandom"=>60, "MsgTimeStamp"=>time(), "MsgBody"=>[['MsgType'=>"TIMTextElem","MsgContent"=>$content]], "CloudCustomData"=>"your cloud custom data"

    ];
    $s = $this->postCurls($url,$data);
    return $s;

    } //删除 public function account_delete($DeleteItem) {

    $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_delete?";
    $url = $this->geturlparameter($url);
    
    $data = [
        "DeleteItem" => $DeleteItem
    
    ];
    //  echo( json_encode($data));
    $s = $this->postCurls($url,$data);
    
    return $s;

    } //添加群成员 public function addqcy($groupid,$memberlist){ $url = "https://console.tim.qq.com/v4/group_open_http_svc/add_group_member?"; $url = $this->geturlparameter($url);

    $data = [
        "GroupId" => $groupid,
        "MemberList" => $memberlist
    ];
    //  echo( json_encode($data));
    $s = $this->postCurls($url,$data);
    
    return $s;

    } //查询帐号在线状态 public function querystate($To_Account ,$IsNeedDetail) {

    $url = "https://console.tim.qq.com/v4/openim/querystate?";
    $url = $this->geturlparameter($url);
    
    $data = [
        "IsNeedDetail" => $IsNeedDetail,
        "To_Account" => $To_Account
    ];
    //  echo( json_encode($data));
    $s = $this->postCurls($url,$data);
    
    return $s;

    }

    //链接 public function geturlparameter($url) { $TLSSigAPIv2 = new TLSSigAPIv2($this->sdkappid,false); // var_dump(1);die; $TLSSigAPIv2 = new TLSSigAPIv2($this->sdkappid,$this->secret); $zz = $url.http_build_query(array( "sdkappid" => $this->sdkappid, "identifier" => $this->identifier, "usersig" => $TLSSigAPIv2->genUserSig("administrator"), 'random' => $this->randomsum(32), 'contenttype' => 'json' ));

    return $zz;

    } /**

    • @Apidoc\Title("注册")

    • @Apidoc\Desc("相亲APP注册接口")

    • @Apidoc\Url("api/login/registe")

    • @Apidoc\Method("POST")

    • @Apidoc\Tag("这是注册接口")

    • @Apidoc\Param("username", type="string",require=true, desc="用户名" )

    • @Apidoc\Param("sex", type="int",require=true, desc="性别,1男2女0未知" )

    • @Apidoc\Param("age", type="int",require=true, desc="年龄" )

    • @Apidoc\Param("realname", type="string",require=true, desc="真实姓名" )

    • @Apidoc\Param("phone", type="string",require=true, desc="手机号" )

    • @Apidoc\Param("idcardno", type="string",require=true,desc="身份证号" )

    • @Apidoc\Param("shengid",type="int",require=true, desc="省份id")

    • @Apidoc\Param("shiid",type="int",require=true, desc="城市ID")

    • @Apidoc\Param("quid",type="int",require=true, desc="区ID")

    • @Apidoc\Param("zhiye",type="string", desc="职业")

    • @Apidoc\Param("aihao",type="string", desc="爱好")

    • @Apidoc\Param("userpwd",type="string", desc="密码") / public function registe(){ $data=input('post.'); $where['username']=input('post.username'); $iscunzai=Db::name('user')->where($where)->find(); if($iscunzai){ $arr=['code'=>'400','msg'=>'用户名已经被抢注,请重新换一个吧']; } else{ unset($data['agreement']); $data['userpwd']=md5($data['userpwd']); $data['inputtime']=time(); $data['createat']=date('Y-m-d H:i:s'); $data['viptime']=time()+23600; $data['sex']=$data['sex']=="男"?"1":"2"; Db::name('user')->insert($data); $arr=['code'=>200,'msg'=>'success']; } return makejson($arr); } /**

    • @Apidoc\Title("上传图片获取路径")

    • @Apidoc\Desc("相亲APP传图片获取路径接口")

    • @Apidoc\Url("api/login/thumbupload")

    • @Apidoc\Method("POST")

    • @Apidoc\Tag("这是获取省市区接口")

    • @Apidoc\Param("file", type="file",require=true, desc="图片本体" )

    • @Apidoc\Returned("url", type="string",desc="地址") */ public function thumbupload(Request $request) { $fileArr = request()->file('file'); //接收文件 // halt($fileArr); $file = $_FILES['file']; //得到传输的数据 $type = $file["type"]; //获得图片的类型 // $rdate = date("Ymd", time()); //获得时间 /**

      • 判断接收文件是否为空 */ if($fileArr == null){ return $this->show( config("status.failed"), config('message.failed'), '未上传图片' ); } /**
      • 截取上传文件名后缀
      • 将文件名以.分割为数组
      • 用PHP end函数取数组最后一个
      • 即可得到上传文件后缀 $temp = explode(".", $_FILES["file"]["name"]); */ $temp = explode(".",$file["name"]); $extension = end($temp); /**
      • 判断上传文件是否合法
      • 判断截取上传文件名是否为
      • jpeg,jpg,png其中之一 */ if(!in_array($extension,array("jpeg","jpg","png"))){ return $this->show( config("status.failed"), config("message.failed"), "上传图片不合法" ); } /**
      • 调用disk方法
      • 此时根目录为/public/storage 系统默认路径
      • 再把文件移动至uploads下的img文件夹里
      • 文件名用data */ $saveName = \think\facade\Filesystem::disk('public')->putFile("img",$fileArr,'data');

      $data["name"] = $file["name"]; $data["url"] = '/storage/'.$saveName; // $data["url"] = $http_x_forwarded_proto."://".$http_host.'/storage/'.$saveName; return makejson(array("code" =>0, "msg" =>"操作成功", "type" => true,"data"=>$data)); // $file = request()->file('file'); // 获取上传的文件 // if ($file==null) { // exit(json_encode(array('code'=>0, 'msg'=>'未上传图片'))); // } // // 获取文件后缀 // $temp = explode(".", $_FILES["file"]["name"]); // $extension = end($temp); // // 判断文件是否合法 // if(!in_array($extension, array("gif","jpeg","jpg","png","mp4"))){ // exit(json_encode(array('code'=>0, 'msg'=>'上传图片不合法'))); // } // $info = $file->move('/www/wwwroot/xiangqin.dianjingkeji.net/tp/public/uploads'); // 移动文件到指定目录 没有则创建 // $img = '/uploads/'.$info->getSaveName(); // $data['url']=$img; // exit(json_encode(array('code'=>200, 'data'=>$data))); } /**

    • @Apidoc\Title("获取省市区")

    • @Apidoc\Desc("相亲APP获取省市区接口")

    • @Apidoc\Url("api/login/shengshiqu")

    • @Apidoc\Method("POST")

    • @Apidoc\Tag("这是获取省市区接口")

    • @Apidoc\Param("pid", type="int",require=true, desc="父级ID" )

    • @Apidoc\Returned("name", type="int",desc="名称")

    • @Apidoc\Returned("id", type="int",desc="id") */ public function shengshiqu(){ $where['pid']=input('post.pid'); $data=Db::name('shengshiqu')->where($where)->field('name,id')->select(); $arr=['code'=>200,'msg'=>'success','data'=>$data]; return makejson($arr); } /**

    • @Apidoc\Title("登录")

    • @Apidoc\Desc("相亲APP登录接口")

    • @Apidoc\Url("api/login/index")

    • @Apidoc\Method("POST")

    • @Apidoc\Tag("这是登录接口")

    • @Apidoc\Param("username", type="string",require=true, desc="用户名" )

    • @Apidoc\Param("userpwd", type="int",require=true, desc="用户密码" )

    • @Apidoc\Returned("token", type="int",desc="用户token")

    • @Apidoc\Returned("uid", type="int",desc="用户id") */ public function index(){ if(input('post.username')!="" && input('post.userpwd')!=""){ $where['username|phone']=input('post.username'); $where['userpwd']=md5(input('post.userpwd')); $istrue=Db::name('user')->where($where)->find(); if($istrue){ $token=md5(rand(100000000,999999999)); $data['token']=$token; Db::name('user')->where($where)->update($data); $senddata=['uid'=>$istrue['id'],'token'=>$token,'username'=>$istrue['username']]; $arr=['code'=>200,'msg'=>'success','data'=>$senddata]; } else{ $arr=['code'=>400,'msg'=>'用户名或密码错误']; } } else{ $arr=['code'=>500,'msg'=>'参数有误']; } return makejson($arr); } }

评论

(= ̄ω ̄=)··· 暂无内容!

回复

您还未登录,请先登录或者注册