ucloud.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. export class CloudObject {
  2. className: string;
  3. id: string | null = null;
  4. createdAt:any;
  5. updatedAt:any;
  6. data: Record<string, any> = {};
  7. constructor(className: string) {
  8. this.className = className;
  9. }
  10. toPointer() {
  11. return { "__type": "Pointer", "className": this.className, "objectId": this.id };
  12. }
  13. set(json: Record<string, any>) {
  14. Object.keys(json).forEach(key => {
  15. if (["objectId", "id", "createdAt", "updatedAt", "ACL"].indexOf(key) > -1) {
  16. return;
  17. }
  18. this.data[key] = json[key];
  19. });
  20. }
  21. get(key: string) {
  22. return this.data[key] || null;
  23. }
  24. async save() {
  25. let method = "POST";
  26. let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}`;
  27. // 更新
  28. if (this.id) {
  29. url += `/${this.id}`;
  30. method = "PUT";
  31. }
  32. const body = JSON.stringify(this.data);
  33. const response = await fetch(url, {
  34. headers: {
  35. "content-type": "application/json;charset=UTF-8",
  36. "x-parse-application-id": "dev"
  37. },
  38. body: body,
  39. method: method,
  40. mode: "cors",
  41. credentials: "omit"
  42. });
  43. const result = await response?.json();
  44. if (result?.error) {
  45. console.error(result?.error);
  46. }
  47. if (result?.objectId) {
  48. this.id = result?.objectId;
  49. }
  50. return this;
  51. }
  52. async destroy() {
  53. if (!this.id) return;
  54. const response = await fetch(`http://dev.fmode.cn:1337/parse/classes/${this.className}/${this.id}`, {
  55. headers: {
  56. "x-parse-application-id": "dev"
  57. },
  58. body: null,
  59. method: "DELETE",
  60. mode: "cors",
  61. credentials: "omit"
  62. });
  63. const result = await response?.json();
  64. if (result) {
  65. this.id = null;
  66. }
  67. return true;
  68. }
  69. }
  70. // CloudQuery.ts
  71. export class CloudQuery {
  72. className: string;
  73. whereOptions: Record<string, any> = {};
  74. constructor(className: string) {
  75. this.className = className;
  76. }
  77. greaterThan(key: string, value: any) {
  78. if (!this.whereOptions[key]) this.whereOptions[key] = {};
  79. this.whereOptions[key]["$gt"] = value;
  80. }
  81. greaterThanAndEqualTo(key: string, value: any) {
  82. if (!this.whereOptions[key]) this.whereOptions[key] = {};
  83. this.whereOptions[key]["$gte"] = value;
  84. }
  85. lessThan(key: string, value: any) {
  86. if (!this.whereOptions[key]) this.whereOptions[key] = {};
  87. this.whereOptions[key]["$lt"] = value;
  88. }
  89. lessThanAndEqualTo(key: string, value: any) {
  90. if (!this.whereOptions[key]) this.whereOptions[key] = {};
  91. this.whereOptions[key]["$lte"] = value;
  92. }
  93. equalTo(key: string, value: any) {
  94. this.whereOptions[key] = value;
  95. }
  96. async get(id: string) {
  97. const url = `http://dev.fmode.cn:1337/parse/classes/${this.className}/${id}?`;
  98. const response = await fetch(url, {
  99. headers: {
  100. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  101. "x-parse-application-id": "dev"
  102. },
  103. body: null,
  104. method: "GET",
  105. mode: "cors",
  106. credentials: "omit"
  107. });
  108. const json = await response?.json();
  109. return json || {};
  110. }
  111. async find() {
  112. let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
  113. if (Object.keys(this.whereOptions).length) {
  114. const whereStr = JSON.stringify(this.whereOptions);
  115. url += `where=${whereStr}`;
  116. }
  117. const response = await fetch(url, {
  118. headers: {
  119. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  120. "x-parse-application-id": "dev"
  121. },
  122. body: null,
  123. method: "GET",
  124. mode: "cors",
  125. credentials: "omit"
  126. });
  127. const json = await response?.json();
  128. let list = json?.results || []
  129. let objList = list.map((item:any)=>this.dataToObj(item))
  130. return objList || [];
  131. }
  132. async first() {
  133. let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
  134. if (Object.keys(this.whereOptions).length) {
  135. const whereStr = JSON.stringify(this.whereOptions);
  136. url += `where=${whereStr}`;
  137. }
  138. const response = await fetch(url, {
  139. headers: {
  140. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  141. "x-parse-application-id": "dev"
  142. },
  143. body: null,
  144. method: "GET",
  145. mode: "cors",
  146. credentials: "omit"
  147. });
  148. const json = await response?.json();
  149. const exists = json?.results?.[0] || null;
  150. if (exists) {
  151. let existsObject = this.dataToObj(exists)
  152. return existsObject;
  153. }
  154. return null
  155. }
  156. dataToObj(exists:any):CloudObject{
  157. let existsObject = new CloudObject(this.className);
  158. existsObject.set(exists);
  159. existsObject.id = exists.objectId;
  160. existsObject.createdAt = exists.createdAt;
  161. existsObject.updatedAt = exists.updatedAt;
  162. return existsObject;
  163. }
  164. }
  165. // CloudUser.ts
  166. export class CloudUser extends CloudObject {
  167. constructor() {
  168. super("_User"); // 假设用户类在Parse中是"_User"
  169. // 读取用户缓存信息
  170. let userCacheStr = localStorage.getItem("NCloud/dev/User")
  171. if(userCacheStr){
  172. let userData = JSON.parse(userCacheStr)
  173. // 设置用户信息
  174. this.id = userData?.objectId;
  175. this.sessionToken = userData?.sessionToken;
  176. this.data = userData; // 保存用户数据
  177. }
  178. }
  179. sessionToken:string|null = ""
  180. /** 获取当前用户信息 */
  181. async current() {
  182. if (!this.sessionToken) {
  183. console.error("用户未登录");
  184. return null;
  185. }
  186. const response = await fetch(`http://dev.fmode.cn:1337/parse/users/me`, {
  187. headers: {
  188. "x-parse-application-id": "dev",
  189. "x-parse-session-token": this.sessionToken // 使用sessionToken进行身份验证
  190. },
  191. method: "GET"
  192. });
  193. const result = await response?.json();
  194. if (result?.error) {
  195. console.error(result?.error);
  196. return null;
  197. }
  198. return result;
  199. }
  200. /** 登录 */
  201. async login(username: string, password: string):Promise<CloudUser|null> {
  202. const response = await fetch(`http://dev.fmode.cn:1337/parse/login`, {
  203. headers: {
  204. "x-parse-application-id": "dev",
  205. "Content-Type": "application/json"
  206. },
  207. body: JSON.stringify({ username, password }),
  208. method: "POST"
  209. });
  210. const result = await response?.json();
  211. if (result?.error) {
  212. console.error(result?.error);
  213. return null;
  214. }
  215. // 设置用户信息
  216. this.id = result?.objectId;
  217. this.sessionToken = result?.sessionToken;
  218. this.data = result; // 保存用户数据
  219. // 缓存用户信息
  220. console.log(result)
  221. localStorage.setItem("NCloud/dev/User",JSON.stringify(result))
  222. return this;
  223. }
  224. /** 登出 */
  225. async logout() {
  226. if (!this.sessionToken) {
  227. console.error("用户未登录");
  228. return;
  229. }
  230. const response = await fetch(`http://dev.fmode.cn:1337/parse/logout`, {
  231. headers: {
  232. "x-parse-application-id": "dev",
  233. "x-parse-session-token": this.sessionToken
  234. },
  235. method: "POST"
  236. });
  237. const result = await response?.json();
  238. if (result?.error) {
  239. console.error(result?.error);
  240. return false;
  241. }
  242. // 清除用户信息
  243. localStorage.removeItem("NCloud/dev/User")
  244. this.id = null;
  245. this.sessionToken = null;
  246. this.data = {};
  247. return true;
  248. }
  249. /** 注册 */
  250. async signUp(username: string, password: string, additionalData: Record<string, any> = {}) {
  251. const userData = {
  252. username,
  253. password,
  254. ...additionalData // 合并额外的用户数据
  255. };
  256. const response = await fetch(`http://dev.fmode.cn:1337/parse/users`, {
  257. headers: {
  258. "x-parse-application-id": "dev",
  259. "Content-Type": "application/json"
  260. },
  261. body: JSON.stringify(userData),
  262. method: "POST"
  263. });
  264. const result = await response?.json();
  265. if (result?.error) {
  266. console.error(result?.error);
  267. return null;
  268. }
  269. // 设置用户信息
  270. this.id = result?.objectId;
  271. this.sessionToken = result?.sessionToken;
  272. this.data = result; // 保存用户数据
  273. return this;
  274. }
  275. }