angularJs 操作indexedDB数据库用于存放邀约聊天列表和聊天详情数据

1.angularJs中 用于 操作indexedDB数据库

import { Injectable } from '@angular/core';
import { HttpService } from '../../shared/service/http.service';
@Injectable({
    providedIn: 'root',
})
export class IndexedDBService {
    private isdbSupported;
    private db;
    private defaultStore = "chat";
    private userInfo = "user";
    private unreadchat = "unreadchat";
    private unreadNum = "unreadNum";
    // private readyToSend = "readyToSend";
    private dbName = "xw_im";

    public request;

    constructor(private http: HttpService) {
        this.onPageLoad();
    }

    onPageLoad() {
        try {
            const indexedDB = window.indexedDB;
            if (indexedDB) {
                this.isdbSupported = indexedDB;
            }
            const openReq = indexedDB.open(this.dbName, 1);

            openReq.onupgradeneeded = (e) => {
                // tslint:disable-next-line:no-string-literal
                const thisDb = e.target['result'];
                if (!thisDb.objectStoreNames.contains(this.defaultStore)) {
                    thisDb.createObjectStore(this.defaultStore);
                }
                if (!thisDb.objectStoreNames.contains(this.userInfo)) {
                    thisDb.createObjectStore(this.userInfo);
                }
                if (!thisDb.objectStoreNames.contains(this.unreadchat)) {
                    thisDb.createObjectStore(this.unreadchat);
                }
                if (!thisDb.objectStoreNames.contains(this.unreadNum)) {
                    thisDb.createObjectStore(this.unreadNum);
                }
                // if (!thisDb.objectStoreNames.contains(this.readyToSend)) {
                //     thisDb.createObjectStore(this.readyToSend);
                // }
            };
            openReq.onsuccess = (e) => {
                console.log(" msg -----> indexedDBService.onPageLoad Success");
                // tslint:disable-next-line:no-string-literal
                this.db = e.target['result'];
                // this.savePerson();
                // this.getByKey(1);
                // this.deleteByKey(1);
            };
            openReq.onerror = (e) => {
                console.log("Error");
                console.dir(e);
            };
        } catch (error) {
            console.log('msg-----> error:' + JSON.stringify(error));
        }

    }

    // tslint:disable-next-line:ban-types
    getByKey(key, storeName = this.defaultStore, callbackFunc: Function) {
        try {
            const transaction = this.db.transaction([storeName], "readonly");
            const store = transaction.objectStore(storeName);
            const obj = store.get(key);
            callbackFunc(obj);
        } catch (error) {
            console.log('msg-----> error:' + JSON.stringify(error));
        }

    }

    // tslint:disable-next-line:ban-types
    getAllValues(storeName = this.defaultStore, callbackFunc: Function) {

        try {
            const transaction = this.db.transaction([storeName], "readonly");
            const store = transaction.objectStore(storeName);
            const obj = store.openCursor();
            callbackFunc(obj);
        } catch (error) {
            console.log('msg-----> error:' + JSON.stringify(error));
        }

    }

    // tslint:disable-next-line:ban-types
    addCallbak(content, key, storeName = this.defaultStore, callbackFunc: Function) {
        try {
            const transaction = this.db.transaction([storeName], "readwrite");
            const store = transaction.objectStore(storeName);
            const req = store.add(content, key);
            req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
            req.onsuccess = (e) => { callbackFunc(); console.log("save complete callbak 添加成功 " + storeName); };
        } catch (error) {
            console.log('msg-----> error:' + JSON.stringify(error));
        }

    }

    // tslint:disable-next-line:ban-types
    putByKeyCallbak(content, key, storeName = this.defaultStore, callbackFunc: Function) {
        try {
            const transaction = this.db.transaction([storeName], "readwrite");
            const store = transaction.objectStore(storeName);
            const req = store.put(content, key);
            req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
            req.onsuccess = (e) => { callbackFunc(); console.log("update complete callbak"); };
        } catch (error) {
            console.log('msg-----> error:' + JSON.stringify(error));
        }

    }

    // tslint:disable-next-line:ban-types
    deleteByKeyCallbak(key, storeName = this.defaultStore, callbackFunc: Function) {
        try {
            const transaction = this.db.transaction([storeName], "readwrite");
            const store = transaction.objectStore(storeName);
            const req = store.delete(key);
            req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
            req.onsuccess = (e) => { callbackFunc(); console.log("delete complete"); };
        } catch (error) {
            console.log('msg-----> error:' + JSON.stringify(error));
        }

    }

    // tslint:disable-next-line:ban-types
    add(content, key, storeName = this.defaultStore) {
        try {
            const transaction = this.db.transaction([storeName], "readwrite");
            const store = transaction.objectStore(storeName);
            const req = store.add(content, key);
            req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
            req.onsuccess = (e) => { console.log("save complete " + storeName + " " + content); };
        } catch (error) {
            console.log('msg-----> error:' + JSON.stringify(error));
        }

    }

    // tslint:disable-next-line:ban-types
    putByKey(content, key, storeName = this.defaultStore) {
        try {
            const transaction = this.db.transaction([storeName], "readwrite");
            const store = transaction.objectStore(storeName);
            const req = store.put(content, key);
            req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
            req.onsuccess = (e) => { console.log("update complete"); };
        } catch (error) {
            console.log('msg-----> error:' + JSON.stringify(error));
        }

    }

    deleteByKey(key, storeName = this.defaultStore) {
        try {
            const transaction = this.db.transaction([storeName], "readwrite");
            const store = transaction.objectStore(storeName);
            const req = store.delete(key);
            req.onerror = (e) => console.log("Error", e.target.error.name + '|' + storeName);
            req.onsuccess = (e) => console.log("delete complete");
        } catch (error) {
            console.log('msg-----> error:' + JSON.stringify(error));
        }

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值