123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- function openMsgSqlite(){
- //创建数据库或者打开
- return new Promise((resolve,reject) =>{
- plus.sqlite.openDatabase({
- name:'msg', //数据库名称
- path:'_doc/msg.db',
- //数据库地址,uniapp推荐以下划线为开头,这到底存在哪里去了,我也不清楚,哈哈
- success(e){
- resolve(e); //成功回调
- },
- fail(e){
- reject(e); //失败回调
- }
- })
- })
- }
- //在该数据库里创建表格, 这一步也必须要!
- function createMsgSQL(groupId){
- return new Promise((resolve,reject) =>{
- //创建表格在executeSql方法里写
- plus.sqlite.executeSql({
- name:'msg',
- //表格创建或者打开,后面为表格结构
- sql:'create table if not exists s_group_msg_'+groupId+' ("id" INT(11) PRIMARY KEY NOT NULL,"groupId" CHAR(255),"msgType" INT(11),"sendUid" CHAR(255),"msgContext" TEXT,"status" INT(11),"operTime" INT,"lastOperTime" INT,"operUser" CHAR(255),"lastOperUser" CHAR(255),"avatar" CHAR(255),"userName" CHAR(255),"groupNickName" CHAR(255))',
- success(e){
- resolve(e);
- },
- fail(e){
- reject(e);
- }
- })
- })
- }
- //向表格里添加数据
- function addMsgSQL(obj){
- //判断有没有传参
- if(obj !== undefined){
- //判断传的参是否有值
- var b = (JSON.stringify(obj) == "{}");
- if(!b){
- //obj传来的参数对象
- var id = obj.id || null; //id
- var groupId = obj.groupId || null; //名称
- var msgType = obj.msgType || 0; //性别
- var sendUid = obj.sendUid || null; //发送
- var msgContext = obj.msgContext || null; //内容
- var operTime = obj.operTime || null; //操作时间
- var userName = obj.userName || null; //用户昵称
- var avatar = obj.avatar || null; //头像
- var groupNickName = obj.groupNickName || null; //群昵称
- var status = obj.status || 0; //状态
- return new Promise((resolve,reject) =>{
- plus.sqlite.executeSql({
- name:'msg',
- sql:"INSERT INTO s_group_msg_" + groupId + " (`id`, `groupId`, `msgType`, `sendUid`, `msgContext`, `status`, `operTime`, `operUser`, `lastOperTime`, `lastOperUser`, `avatar`, `userName`, `groupNickName`) VALUES ('" + id + "', '" + groupId + "', '" + msgType + "', '" + sendUid + "', '" + msgContext + "', '" + status + "', '" + (operTime / 1000) + "', '" + userName + "', '" + (operTime / 1000) + "', '" + userName + "', '" + avatar + "', '" + userName + "', '" + groupNickName + "')",
- success(e){
- resolve(e);
- },
- fail(e){
- reject(e);
- }
- })
- })
- }else{
- return new Promise((resolve,reject) =>{reject("错误添加")})
- }
- }else{
- return new Promise((resolve,reject) =>{reject("错误添加")})
- }
- }
- //查询获取数据库里的数据
- function selectMsgSQL(groupId){
- if(groupId !== undefined){
- //第一个是表单名称,后两个参数是列表名,用来检索
- var sql = 'select * from s_group_msg_' + groupId;
- return new Promise((resolve,reject)=>{
- plus.sqlite.selectSql({
- name:'msg',
- sql:sql,
- success(e){
- resolve(e);
- },
- fail(e){
- reject(e);
- }
- })
- })
- }else{
- return new Promise((resolve,reject) =>{reject("错误查询")});
- }
- }
- //删除数据库里的数据
- function deleteMsgSQL(name,sol,qq,ww,ee){
- if(name !== undefined && sol !== undefined){
- //listId为表名,后面两个是列表名,检索用的
- if(ww !== undefined){
- //两个检索条件
- var sql = 'delete from '+name+' where '+sol+'="'+qq+'" and '+ww+'='+ee+'';
- }else{
- //一个检索条件
- var sql = 'delete from '+name+' where '+sol+'="'+qq+'"';
- }
- return new Promise((resolve,reject) =>{
- plus.sqlite.executeSql({
- name:'msg',
- sql:sql,
- success(e){
- resolve(e);
- },
- fail(e){
- reject(e);
- }
- })
- })
- }else{
- return new Promise((resolve,reject) =>{reject("错误删除")});
- }
- }
- //修改数据表里的数据
- function modifyMsgSQL(listName,name,cont,use,sel){
- //表格名,要修改地方的列名,修改后的内容,修改条件查询,列名,内容
- var sql;
- if(use == undefined){
- sql ='update '+listName+' set '+name+'="'+cont+'"';
- }else{
- sql ='update '+listName+' set '+name+'="'+cont+'" where '+use+'="'+sel+'"';
- }
- //where前面的是要修改的,后面的是条件,选择哪个
- return new Promise((resolve,reject) =>{
- plus.sqlite.executeSql({
- name:'msg',
- sql:sql,
- success(e){
- resolve(e);
- },
- fail(e){
- reject(e);
- }
- })
- })
- }
- //关闭数据库
- function closeSQL(name){
- return new Promise((resolve,reject) =>{
- plus.sqlite.closeDatabase({
- name:'msg',
- success(e){
- resolve(e);
- },
- fail(e){
- reject(e);
- }
- })
- })
- }
- //监听数据库是否开启
- function isOpen(name,path){
- var ss = name || 'msg';
- var qq = path || '_doc/msg.db';
- //数据库打开了就返回true,否则返回false
- var open = plus.sqlite.isOpenDatabase({
- name:ss,
- path:qq
- })
- return open;
- }
- //一次获取指定数据条数
- function pullSQL(id,num){
- //id为表名,num为跳过多少条数据
- //根据list来倒序拿数据,跳过num条拿取15条
- return new Promise((resolve,reject) =>{
- plus.sqlite.selectSql({
- name:'msg',
- sql:'select * from '+id+' order by list desc limit 15 offset '+num+'',
- success(e){
- resolve(e);
- },
- fail(e){
- reject(e);
- }
- })
- })
- }
- //把这些方法导出去
- export{
- openMsgSqlite,
- createMsgSQL,
- addMsgSQL,
- selectMsgSQL
- }
|