APICloud開發者進階之路 | UIPickerView 模塊示例demo

本文出自APICloud官方論壇

rongCloud2 3.2.8 版本更新後添加了發送小視頻接口,發送文件接口。

rongCloud2 概述

融雲是國內首家專業的即時通訊雲服務提供商,專注爲互聯網、移動互聯網開發者提供即時通訊基礎能力和雲端服務。通過融雲平臺,開發者不必搭建服務端硬件環境,就可以將即時通訊、實時網絡能力快速集成至應用中。

rongCloud2 封裝了融雲即時通訊能力庫 IMLib SDK 的 API,對融雲的相關接口做了一一對應的封裝,功能詳情可參考目錄。

使用 rongCloud2 模塊之前,請先 註冊 融雲的開發者帳號並申請創建 App,創建 App 後,可以在 開發者後臺 獲取 App Key 和 App Secret 用於開發。
開發前請先認真閱讀相關的 融雲開發文檔和視頻。(請在PC端打開)

由於 (Android )融雲sdk 不支持中文路徑,需要處理一下,用fs 模塊 調用rename接口重命名一下,爲了不影響原文件需要先複製一下再進行處理。
方法不唯一,提供參考

所用 模塊 rongcloud2、fs 模塊、fileBrowser 模塊
以下是頁面源碼 包括簡易登陸獲取token頁面主功能頁面。

一、`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

  <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>

  <title>title</title>

  <link rel="stylesheet" type="text/css" href="../css/api.css"/>

  <style>

      body{

      }

      .form{

        width: 100%;

        height: 200px;

        margin-top: 100px;

        text-align: center;

      }

      input{

        width: 80%;

        height: 60px;

        text-align: center;

        font-size: 35px;

        border-radius: 5px;

        border: 1px solid #b23;

      }

      button{

        width: 100px;

        height: 50px;

        margin-left: 100px;

        background-color: green;

        color: #fff;

      }

</style>

</head>

<body>

<div class="form">

id:<input id="uid" type="text" name="" value="">

name:<input id="uname" type="text" name="" value="">

</div>

<button type="button" name="button">登陸</button>

<button type="button" name="button">發送</button>

</body>

<script type="text/javascript" src="../script/api.js"></script>

<script type="text/javascript" src="../script/sha1.js"></script>

<script type="text/javascript">

var rong = '';

  apiready = function(){

    rong = api.require("rongCloud2");

  };

  function login() {

    var uid = $api.byId('uid').value;

    var uname = $api.byId('uname').value;

    var portraitUri = 'lll'; //會員頭像

    var appKey = "*********";

    var appSecret = "***********";

    var nonce = Math.floor(Math.random() * 1000000);//隨機數

    var timestamp = Date.now(); //時間戳

    var signature = SHA1("" + appSecret + nonce + timestamp);//數據簽名(通過哈希加密計算)

    api.ajax({

            url : "http://api.cn.ronghub.com/user/getToken.json",

            method : "post",

            headers : {

                    "RC-App-Key" : appKey,

                    "RC-Nonce" : "" + nonce,

                    "RC-Timestamp" : "" + timestamp,

                    "RC-Signature" : "" + signature,

                    "Content-Type" : "application/x-www-form-urlencoded"

            },

            data : {

                    values : {

                            userId : uid,

                            name : uname,

                            portraitUri : portraitUri

                    }

            }

    }, function(ret, err) {

            if (ret) {

                    $api.setStorage('token', ret.token);//將token存儲到本地

                    api.alert({

                        title: 'testtitle',

                        msg: '以獲取token',

                    }, function(ret, err){

                        if( ret ){

                             api.openWin({

                                 name: 'rong2',

                                 url: './rong2.html',

                                 pageParam: {

                                     name: 'test'

                                 }

                             });

                        }else{

                             alert( JSON.stringify( err ) );

                        }

                    });

            } else {

                    alert("獲取token失敗")

            }

    });

  }

  function sendEv() {

    api.sendEvent({

        name: 'myEvent',

        extra: {

            key1: 'value1',

            key2: 'value2'

        }

    });

    api.openWin({

        name: 'page1',

        url: 'widget://index.html',

        pageParam: {

            name: 'test'

        }

    });

  }

</script>

</html>

複製代碼


<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8">

    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />

    <title>title</title>

    <link rel="stylesheet" type="text/css" href="../css/api.css" />

    <style>

        body {}

        .main {

            position: absolute;

            bottom: 0;

            width: 100%;

            height: 200px;

            text-align: center;

            border-top: 1px solid #b23;

        }

        button {

            margin-top: 5px;

            margin-left: 10px;

            width: 90px;

            height: 50px;

        }

        .msg {

            width: 100%;

            height: 300px;

            margin-top: 100px;

        }

        input {

            width: 90%;

            height: 50px;

            border: 1px solid #b33;

            text-align: center;

        }

        .msg button {

            width: 100px;

            height: 50px;

            background-color: green;

            color: #fff;

            border-radius: 5px;

        }
</style>

</head>

<body>

    <div class="msg">

        <input id="sendtxt" type="text" name="" value="" placeholder="發送文本內容">

        <input id="uid" type="text" name="" value="" placeholder="接收者id">

        <button type="button" name="button">發送</button>

        <button type="button" name="button">發文件</button>

        <button type="button" name="button">發視頻</button>

    </div>

    <div id="evbtn">

    </div>

</body>

<script type="text/javascript" src="../script/api.js"></script>

<script type="text/javascript">

    var token = '';

    var oldPath = '';

    var fileName = '';

    var type = '';  //按鈕

    //初始化模塊

    var rong = null;

    var fs = null;

    var fileBrowser = null;

    apiready = function() {

      //初始化模塊

        rong = api.require('rongCloud2');

        fs = api.require('fs');

        fileBrowser = api.require('fileBrowser');

        //登陸

        setTimeout(function() {

            getToken();

            msglisten();

            addlistenSta();

            toCon();

        }, 2000);

    };

    //獲取緩存token

    function getToken() {

        token = $api.getStorage('token');

        if (token) {

            toInit();

        } else {

            api.openWin({

                name: 'page1',

                url: './token.html',

                pageParam: {

                    name: 'test'

                }

            });

        }

    }

    //初始化融雲

    function toInit() {

        rong.init(function(ret, err) {

            if (ret) {

                console.log('init---ok');

            }

        });

    }

    //添加狀態監聽

    function addlistenSta() {

        rong.setConnectionStatusListener(function(ret, err) {

            if (ret) {

                console.log('監聽開啓---');

            }

        });

    }

    //連接融雲

    function toCon() {

        rong.connect({

            token: token

        }, function(ret, err) {

            alert(JSON.stringify(ret) + '---連接成功');

        });

    }

    function send ( type ) {

      type = type;

      getFilePath();

    }

    function fnEvent( path ) {

      if (type == 'file') {

        sendFileMessageR( path );

      }else {

        sendSightMessageR( path );

      }

    }

    //選取文件

    function getFilePath() {

      fileBrowser.open(function(ret) {

          if (ret) {

            alert(JSON.stringify(ret));

            oldPath = ret.url;

            fileName = ret.name;

            //判斷路徑是否包含中文

            if (escape(oldPath).indexOf( "%u" )<0){

                alert( "沒有包含中文" );

                fnEvent( oldPath );

            } else {

                existIsOk();

            }

          }

      });

    }

    //判斷文件夾是否存在

    function existIsOk() {

      fs.exist({

          path: 'fs://rongcloudfilecopy'

      }, function(ret, err) {

        alert(JSON.stringify(ret));

          if (ret.exist) {

              alert('有這個路徑');

              copyToRong();

          } else {

              alert('沒有這個路徑');

              createDirF();

          }

      });

    }

    //創建路徑

    function createDirF() {

      fs.createDir({

          path: 'fs://rongcloudfilecopy'

      }, function(ret, err) {

          if (ret.status) {

              alert(JSON.stringify(ret));

              copyToRong();

          } else {

              alert(JSON.stringify(err));

          }

      });

    }

    //複製到融雲文件夾

    function copyToRong( ) {

      fs.copyTo({

          oldPath: oldPath,

          newPath: 'fs://rongcloudfilecopy'

      }, function(ret, err) {

          if (ret.status) {

              alert(JSON.stringify(ret));

              renameRong();

          } else {

              alert(JSON.stringify(err));

          }

      });

    }

    //重命名文件

    function renameRong( ) {

      var timestamp = (new Date()).getTime();

      var srtPath1 = fileName.split('.')[0];

      var srtPath2 = fileName.split('.')[1];

      var copyOldPath = 'fs://rongcloudfilecopy/' + fileName ;

      var newPath = 'fs://rongcloudfilecopy/' + timestamp + '.' + srtPath2

      fs.rename({

          oldPath: copyOldPath,

          newPath: newPath

      }, function(ret, err) {

          if (ret.status) {

              alert(JSON.stringify(ret));

              fnEvent( newPath );

          } else {

              alert(JSON.stringify(err));

          }

      });

    }

    //發短視頻

    function sendSightMessageR( videoPath ) {

        console.log(videoPath);

        var txt = $api.byId('sendtxt').value;

        var uid = $api.byId('uid').value;

        console.log(uid);

        rong.sendSightMessage({

            conversationType: 'PRIVATE',

            targetId: uid,

            thumbPath: 'fs://picture/dog1.png',

            sightPath: videoPath,   //videoPath,

            //sightPath: 'fs://picture/666.mp4',   //videoPath,

            duration: 5,

            extra: ''

        }, function(ret, err) {

            if (ret.status == 'prepare')

                console.log(JSON.stringify(ret.result.message));

            else if (ret.status == 'progress')

                    console.log(ret.result.progress );

            else if (ret.status == 'success')

                console.log(ret.result.message.messageId);

            else if (ret.status == 'error')

                console.log(err.code);

        });

    }

    //發送文件

    function sendFileMessageR( filePath ) {

        var uid = $api.byId('uid').value;

        console.log(filePath);

        rong.sendFileMessage({

            conversationType: 'PRIVATE',

            targetId: uid,

            filePath: filePath,

            extra: ''

        }, function(ret, err) {

            if (ret.status == 'prepare')

                console.log(JSON.stringify(ret.result.message));

            else if (ret.status == 'progress')

                console.log(ret.result.progress );

            else if (ret.status == 'success')

                console.log(ret.result.message.messageId);

            else if (ret.status == 'error')

                console.log(err.code);

        });

    }

    //發送信息

    function sendmsg() {

        var txt = $api.byId('sendtxt').value;

        var uid = $api.byId('uid').value;

        rong.sendTextMessage({

            conversationType: 'PRIVATE',

            targetId: uid,

            text: txt,

            extra: ''

        }, function(ret, err) {

            if (ret.status == 'prepare')

                api.alert({

                    msg: JSON.stringify(ret.result.message)

                });

            else if (ret.status == 'success')

                api.alert({

                    msg: ret.result.message.messageId

                });

            else if (ret.status == 'error')

                api.alert({

                    msg: err.code

                });

        });

    }

    //消息監聽

    function msglisten() {

        rong.setOnReceiveMessageListener(function(ret, err) {

            // api.alert({ msg: JSON.stringify(ret.result.message) });

            alert('收到了消息');

            console.log(JSON.stringify(ret.result));

            // setInterval(function () {

            //   rong.getConnectionStatus(function(ret, err) {

            //       console.log( ret.result.connectionStatus );

            //   })

            // }, 2000);

            // var receivetxtSTR = ret.result.message.content.text;

            // tid = ret.result.message.targetId

            // console.log(tid);

            // $api.byId('receivetxt').value = receivetxtSTR;

            // rong.sendReadReceiptMessage({

            //     targetId: tid

            // }, function(ret) {

            //     api.alert({

            //         msg: JSON.stringify(ret) + '我已讀了'

            //     });

            // });

        })

    }
</script>

</html>

複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章