123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- // class CloudObject{
- // id
- // className
- // data = {}
- // constructor(className){
- // this.className = className
- // }
- // toPointer(){
- // return {"__type":"Pointer","className":this.className,"objectId":this.id}
- // }
- // set(json){
- // Object.keys(json).forEach(key=>{
- // if(["objectId","id","createdAt","updatedAt","ACL"].indexOf(key)>-1){
- // return
- // }
- // this.data[key] = json[key]
- // })
- // }
- // get(key){
- // return this.data[key] || null
- // }
- // async save(){
- // let method = "POST"
- // let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className
- // // 更新
- // if(this.id){
- // url += "/"+this.id
- // method = "PUT"
- // }
- // let body = JSON.stringify(this.data)
- // let response = await fetch(url, {
- // "headers": {
- // "content-type": "application/json;charset=UTF-8",
- // "x-parse-application-id": "dev"
- // },
- // "body": body,
- // "method": method,
- // "mode": "cors",
- // "credentials": "omit"
- // });
- // let result = await response?.json();
- // if(result?.error){
- // console.error(result?.error)
- // }
- // if(result?.objectId){this.id = result?.objectId}
- // return this
- // }
- // async destory(){
- // if(!this.id) return
- // let response = await fetch("http://dev.fmode.cn:1337/parse/classes/Doctor/"+this.id, {
- // "headers": {
- // "x-parse-application-id": "dev"
- // },
- // "body": null,
- // "method": "DELETE",
- // "mode": "cors",
- // "credentials": "omit"
- // });
- // let result = await response?.json();
- // if(result){
- // this.id = null
- // }
- // return true
- // }
- // }
- // class CloudQuery{
- // className
- // constructor(className){
- // this.className = className
- // }
- // whereOptions = {}
- // greaterThan(key,value){
- // if(!this.whereOptions[key]) this.whereOptions[key] = {}
- // this.whereOptions[key]["$gt"] = value
- // }
- // greaterThanAndEqualTo(key,value){
- // if(!this.whereOptions[key]) this.whereOptions[key] = {}
- // this.whereOptions[key]["$gte"] = value
- // }
- // lessThan(key,value){
- // if(!this.whereOptions[key]) this.whereOptions[key] = {}
- // this.whereOptions[key]["$lt"] = value
- // }
- // lessThanAndEqualTo(key,value){
- // if(!this.whereOptions[key]) this.whereOptions[key] = {}
- // this.whereOptions[key]["$lte"] = value
- // }
- // equalTo(key,value){
- // this.whereOptions[key] = value
- // }
- // async get(id){
- // let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+id+"?"
- // let response = await fetch(url, {
- // "headers": {
- // "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
- // "x-parse-application-id": "dev"
- // },
- // "body": null,
- // "method": "GET",
- // "mode": "cors",
- // "credentials": "omit"
- // });
- // let json = await response?.json();
- // return json || {}
- // }
- // async find(){
- // let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
-
- // if(Object.keys(this.whereOptions)?.length){
- // let whereStr = JSON.stringify(this.whereOptions)
- // url += `where=${whereStr}`
- // }
- // let response = await fetch(url, {
- // "headers": {
- // "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
- // "x-parse-application-id": "dev"
- // },
- // "body": null,
- // "method": "GET",
- // "mode": "cors",
- // "credentials": "omit"
- // });
- // let json = await response?.json();
- // return json?.results || []
- // }
- // async first(){
- // let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
-
- // if(Object.keys(this.whereOptions)?.length){
- // let whereStr = JSON.stringify(this.whereOptions)
- // url += `where=${whereStr}`
- // }
- // let response = await fetch(url, {
- // "headers": {
- // "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
- // "x-parse-application-id": "dev"
- // },
- // "body": null,
- // "method": "GET",
- // "mode": "cors",
- // "credentials": "omit"
- // });
- // let json = await response?.json();
- // let exists = json?.results?.[0] || null
- // if(exists){
- // let existsObject = new CloudObject(this.className)
- // existsObject.set(exists)
- // existsObject.id = exists.objectId
- // existsObject.createdAt = exists.createdAt
- // existsObject.updatedAt = exists.updatedAt
- // return existsObject
- // }
- // }
- // }
- // module.exports.CloudObject = CloudObject
- // module.exports.CloudQuery = CloudQuery
- class CloudObject{
- id
- ClassName
- data={}
- constructor(ClassName)
- {
- this.ClassName=ClassName
- }
- toPointer(){
- return {"_type":"Pointer","ClassName":this.ClassName,"objectId":this.id}
- }
- // 界面给data传数据
- set(json){
- Object.keys(json).forEach(key=>{
- if(["objectId","id","createdAt","updateAt","ACL"].indexOf(key)>-1)
- {
- return
- }
- this.data[key]=json[key]
- })
- }
- // data返回数据给界面
- get(){
- return this.data[key] || null
- }
- // data数据传给数据库,
- // 增数据 数据库生成id,给id赋值,返回整个对象
- // 改数据 修改非id数据,返回整个对象
- async save(){
- let url="http://dev.fmode.cn:1337/parse/classes/" +this.ClassName
- let method="POST"
- if(this.id)
- {
- method="PUT"
- url+="/"+this.id
- }
- let body=JSON.stringify(this.data)
- let response=await fetch(url, {
- "headers": {
- "content-type": "application/json;charset=UTF-8",
- "x-parse-application-id": "dev"
- },
- "body": body,
- "method": method,
- "mode": "cors",
- "credentials": "omit"
- });
- let json= await response?.json();
- if(json?.objectId){this.id=json?.objectId}
- return this
- }
- // 通过数据库删除,再删除id里可能存在的数据
- async destroy() {
- if(!this.id)return
- let response=await fetch("http://dev.fmode.cn:1337/parse/classes/Post/"+this.id, {
- "headers": {
- "x-parse-application-id": "dev"
- },
- "body": null,
- "method": "DELETE",
- "mode": "cors",
- "credentials": "omit"
- });
- let json=await response?.json()
- if(json)this.id=null
- return true;
- }
- }
- class CloudQuery
- {
- ClassName
- constructor(ClassName)
- {
- this.ClassName=ClassName
- }
-
- whereOptions={}
- equalTo(key,value)
- {
- this.whereOptions[key]=value
- }
- greaterThan(key,value)
- {
- if(!this.whereOptions[key])this.whereOptions[key]={}
- this.whereOptions[key]["$gt"]=value
- }
- greaterThanAndEqualTo(key,value)
- {
- if(!this.whereOptions[key])this.whereOptions[key]={}
- this.whereOptions[key]["$gte"]=value
- }
- lessThan(key,value)
- {
- if(!this.whereOptions[key])this.whereOptions[key]={}
- this.whereOptions[key]["$lt"]=value
- }
- lessThanAndEqualTo(key,value)
- {
- if(!this.whereOptions[key])this.whereOptions[key]={}
- this.whereOptions[key]["$lte"]=value
- }
- // 查询一个表中的特定id数据
- async get(id)
- {
- let response=await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.ClassName+"/"+id+"?", {
- "headers": {
- "if-none-match": "W/\"ab-2gNtNZqRYX93fdbIaSj6z5h571k\"",
- "x-parse-application-id": "dev"
- },
-
- "body": null,
- "method": "GET",
- "mode": "cors",
- "credentials": "omit"
- });
- let json=await response?.json();
- // console.log(json);
- return json || {}
- }
-
- // 查询一个表中的所有数据,高级查询
- async find(){
- let url="http://dev.fmode.cn:1337/parse/classes/"+this.ClassName+"?"
- if(Object.keys(this.whereOptions).length)
- {
- let whereStr=JSON.stringify(this.whereOptions)
- url+=`where=${whereStr}`
- }
- let response=await fetch(url, {
- "headers": {
- "if-none-match": "W/\"ab-2gNtNZqRYX93fdbIaSj6z5h571k\"",
- "x-parse-application-id": "dev"
- },
- "body": null,
- "method": "GET",
- "mode": "cors",
- "credentials": "omit"
- });
- let json=await response?.json();
- // console.log(json);
- return json?.results || []
- }
- async first(){
- let url="http://dev.fmode.cn:1337/parse/classes/"+this.ClassName+"?"
- if(Object.keys(this.whereOptions).length)
- {
- let whereStr=JSON.stringify(this.whereOptions)
- url+=`where=${whereStr}`
- }
- let response=await fetch(url, {
- "headers": {
- "if-none-match": "W/\"ab-2gNtNZqRYX93fdbIaSj6z5h571k\"",
- "x-parse-application-id": "dev"
- },
- "body": null,
- "method": "GET",
- "mode": "cors",
- "credentials": "omit"
- });
- let json=await response?.json();
- // console.log(json);
- let exists=json?.results?.[0] || null
- if(exists)
- {
- let existsObject=new CloudObject(this.ClassName)
- existsObject.set(exists)
- existsObject.id=exists.objectId
- existsObject.createdAt=exists.createdAt
- existsObject.updatedAt=exists.updatedAt
- return existsObject
- }
-
- }
- }
- module.exports.CloudObject=CloudObject
- module.exports.CloudQuery=CloudQuery
|