|
@@ -1,6 +1,6 @@
|
|
|
import AV from 'leancloud-storage';
|
|
|
import {TypedMessagesPlugin} from 'leancloud-realtime-plugin-typed-messages'
|
|
|
-import {Realtime} from "leancloud-realtime";
|
|
|
+import {Realtime,Event} from "leancloud-realtime";
|
|
|
import {AV_APP_ID as appId, AV_APP_KEY as appKey} from '../config'
|
|
|
import {Toast} from 'antd-mobile-rn';
|
|
|
import {AsyncStorage} from 'react-native'
|
|
@@ -9,9 +9,9 @@ class LeanCloudAPI {
|
|
|
this.AV = null;
|
|
|
this.realtime = null;
|
|
|
this.imClient = null;
|
|
|
- this.username = null;
|
|
|
- this.userID = null ;
|
|
|
+ this.userId = null ;
|
|
|
this.password = null;
|
|
|
+ this.user=null;
|
|
|
}
|
|
|
|
|
|
init() {
|
|
@@ -20,42 +20,43 @@ class LeanCloudAPI {
|
|
|
this.realtime = new Realtime({
|
|
|
appId,
|
|
|
appKey,
|
|
|
- plugins: [TypedMessagesPlugin],
|
|
|
+ plugins: [TypedMessagesPlugin]
|
|
|
});
|
|
|
}
|
|
|
|
|
|
login(telphone,password,navigation) {
|
|
|
- Toast.loading('登录中',20)
|
|
|
+ Toast.loading('登录中..',20)
|
|
|
this.AV.User.logInWithMobilePhone(telphone, password).then((res)=>{
|
|
|
Toast.success('登录成功',1)
|
|
|
- this.user = res.attributes
|
|
|
- this.userID = res.id
|
|
|
- this.password = password
|
|
|
- AsyncStorage.setItem('USERINFO',JSON.stringify({telphone,password}))
|
|
|
- AsyncStorage.setItem('USER',JSON.stringify(this.user))
|
|
|
- navigation.dispatch({type:'AV',AV:this.AV})
|
|
|
- navigation.dispatch({type:'USER',user:this.user})
|
|
|
+ this.user = res
|
|
|
+ this.userId = res.id
|
|
|
+ this.password = password;
|
|
|
+ this.createIMClient()
|
|
|
+ AsyncStorage.setItem('Password',password)
|
|
|
+ navigation.dispatch({type:'USER',user:res.attributes})
|
|
|
navigation.navigate({ routeName:"StackNavigator" })
|
|
|
},(err)=>{
|
|
|
Toast.info(err.rawMessage)
|
|
|
})
|
|
|
}
|
|
|
- queryPassword(){
|
|
|
- const query = new AV.Query('_User');
|
|
|
- query.get(this.userID).then(function (res) {
|
|
|
- console.log(res)
|
|
|
- // 成功获得实例
|
|
|
- // todo 就是 id 为 57328ca079bc44005c2472d0 的 Todo 对象实例
|
|
|
- }, function (error) {
|
|
|
- // 异常处理
|
|
|
- });
|
|
|
+ async isLogin(){
|
|
|
+ let user = await this.AV.User.currentAsync()
|
|
|
+ if(!user){
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ this.password= await AsyncStorage.getItem('Password')
|
|
|
+ this.user = user
|
|
|
+ this.userId = user.id
|
|
|
+ return JSON.parse(await AsyncStorage.getItem('USER'))
|
|
|
}
|
|
|
+
|
|
|
updateUser(key,val,callback){
|
|
|
- this.AV.Query.doCloudQuery(`update _User set ${key}="${val}" where objectId="${this.userID}"`)
|
|
|
+ this.AV.Query.doCloudQuery(`update _User set ${key}="${val}" where objectId="${this.userId}"`)
|
|
|
.then(function (data) {
|
|
|
// data 中的 results 是本次查询返回的结果,AV.Object 实例列表
|
|
|
var results = data.results;
|
|
|
callback(results)
|
|
|
+ Toast.success('修改成功',1)
|
|
|
}, function (err) {
|
|
|
// 异常处理
|
|
|
Toast.info(err.rawMessage)
|
|
@@ -78,48 +79,58 @@ class LeanCloudAPI {
|
|
|
|
|
|
async logout() {
|
|
|
if (this.imClient) {
|
|
|
- return await imClient.close();
|
|
|
+ return await this.imClient.close();
|
|
|
}
|
|
|
if (this.AV) {
|
|
|
return await this.AV.User.logOut();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // async createIMClient(dispatch) {
|
|
|
- // console.log('init im client');
|
|
|
- // if (!this.username) {
|
|
|
- // throw new Error('Please login first.');
|
|
|
- // }
|
|
|
- // this.imClient = await this.realtime.createIMClient(this.username);
|
|
|
+ async createIMClient(dispatch) {
|
|
|
+
|
|
|
+ console.log('init im client');
|
|
|
+
|
|
|
+ if (!this.userId) {
|
|
|
+ throw new Error('Please login first.');
|
|
|
+ }
|
|
|
+ this.imClient = await this.realtime.createIMClient(this.userId);
|
|
|
+ console.log(this.imClient)
|
|
|
+
|
|
|
+
|
|
|
+ for(var i in Event){
|
|
|
+ this.imClient.on(Event[i], function (msg) {
|
|
|
+ console.log(Event[i] ,msg);
|
|
|
+ // dispath && dispatch(receivedWsMsg(msg))
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- // this.realtime.on('message', function (msg) {
|
|
|
- // console.log('msg: ' + msg);
|
|
|
- // dispath && dispatch(receivedWsMsg(msg))
|
|
|
- // });
|
|
|
- // this.realtime.on('disconnect', function () {
|
|
|
- // console.log('服务器连接已断开');
|
|
|
- // dispath && dispatch(offline());
|
|
|
- // });
|
|
|
- // this.realtime.on('offline', function () {
|
|
|
- // console.log('离线(网络连接已断开)');
|
|
|
- // dispath && dispatch(offline());
|
|
|
- // });
|
|
|
- // this.realtime.on('online', function () {
|
|
|
- // console.log('已恢复在线');
|
|
|
- // dispath && dispatch(online());
|
|
|
- // });
|
|
|
- // this.realtime.on('schedule', function (attempt, delay) {
|
|
|
- // console.log(delay + 'ms 后进行第' + (attempt + 1) + '次重连');
|
|
|
- // });
|
|
|
- // this.realtime.on('retry', function (attempt) {
|
|
|
- // console.log('正在进行第' + (attempt + 1) + '次重连');
|
|
|
- // });
|
|
|
- // this.realtime.on('reconnect', function () {
|
|
|
- // console.log('与服务端连接恢复');
|
|
|
- // });
|
|
|
+ // this.imClient.on('disconnect', function () {
|
|
|
+ // console.log('服务器连接已断开');
|
|
|
+ // // dispath && dispatch(offline());
|
|
|
+ // });
|
|
|
+ // this.imClient.on('offline', function () {
|
|
|
+ // console.log('离线(网络连接已断开)');
|
|
|
+ // // dispath && dispatch(offline());
|
|
|
+ // });
|
|
|
+ // this.imClient.on('online', function () {
|
|
|
+ // console.log('已恢复在线');
|
|
|
+ // // dispath && dispatch(online());
|
|
|
+ // });
|
|
|
+ // this.imClient.on('schedule', function (attempt, delay) {
|
|
|
+ // console.log(delay + 'ms 后进行第' + (attempt + 1) + '次重连');
|
|
|
+ // });
|
|
|
+ // this.imClient.on('retry', function (attempt) {
|
|
|
+ // console.log('正在进行第' + (attempt + 1) + '次重连');
|
|
|
+ // });
|
|
|
+ // this.imClient.on('reconnect', function () {
|
|
|
+ // console.log('与服务端连接恢复');
|
|
|
+ // });
|
|
|
+ // this.imClient.on('conflict', function () {
|
|
|
+ // console.log('被强制下线');
|
|
|
+ // });
|
|
|
|
|
|
- // return this.imClient;
|
|
|
- // }
|
|
|
+ return this.imClient;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export default new LeanCloudAPI();
|