msg.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. function openMsgSqlite(){
  2. //创建数据库或者打开
  3. return new Promise((resolve,reject) =>{
  4. plus.sqlite.openDatabase({
  5. name:'msg', //数据库名称
  6. path:'_doc/msg.db',
  7. //数据库地址,uniapp推荐以下划线为开头,这到底存在哪里去了,我也不清楚,哈哈
  8. success(e){
  9. resolve(e); //成功回调
  10. },
  11. fail(e){
  12. reject(e); //失败回调
  13. }
  14. })
  15. })
  16. }
  17. //在该数据库里创建表格, 这一步也必须要!
  18. function createMsgSQL(groupId){
  19. return new Promise((resolve,reject) =>{
  20. //创建表格在executeSql方法里写
  21. plus.sqlite.executeSql({
  22. name:'msg',
  23. //表格创建或者打开,后面为表格结构
  24. 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))',
  25. success(e){
  26. resolve(e);
  27. },
  28. fail(e){
  29. reject(e);
  30. }
  31. })
  32. })
  33. }
  34. //向表格里添加数据
  35. function addMsgSQL(obj){
  36. //判断有没有传参
  37. if(obj !== undefined){
  38. //判断传的参是否有值
  39. var b = (JSON.stringify(obj) == "{}");
  40. if(!b){
  41. //obj传来的参数对象
  42. var id = obj.id || null; //id
  43. var groupId = obj.groupId || null; //名称
  44. var msgType = obj.msgType || 0; //性别
  45. var sendUid = obj.sendUid || null; //发送
  46. var msgContext = obj.msgContext || null; //内容
  47. var operTime = obj.operTime || null; //操作时间
  48. var userName = obj.userName || null; //用户昵称
  49. var avatar = obj.avatar || null; //头像
  50. var groupNickName = obj.groupNickName || null; //群昵称
  51. var status = obj.status || 0; //状态
  52. return new Promise((resolve,reject) =>{
  53. plus.sqlite.executeSql({
  54. name:'msg',
  55. 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 + "')",
  56. success(e){
  57. resolve(e);
  58. },
  59. fail(e){
  60. reject(e);
  61. }
  62. })
  63. })
  64. }else{
  65. return new Promise((resolve,reject) =>{reject("错误添加")})
  66. }
  67. }else{
  68. return new Promise((resolve,reject) =>{reject("错误添加")})
  69. }
  70. }
  71. //查询获取数据库里的数据
  72. function selectMsgSQL(groupId){
  73. if(groupId !== undefined){
  74. //第一个是表单名称,后两个参数是列表名,用来检索
  75. var sql = 'select * from s_group_msg_' + groupId;
  76. return new Promise((resolve,reject)=>{
  77. plus.sqlite.selectSql({
  78. name:'msg',
  79. sql:sql,
  80. success(e){
  81. resolve(e);
  82. },
  83. fail(e){
  84. reject(e);
  85. }
  86. })
  87. })
  88. }else{
  89. return new Promise((resolve,reject) =>{reject("错误查询")});
  90. }
  91. }
  92. //删除数据库里的数据
  93. function deleteMsgSQL(name,sol,qq,ww,ee){
  94. if(name !== undefined && sol !== undefined){
  95. //listId为表名,后面两个是列表名,检索用的
  96. if(ww !== undefined){
  97. //两个检索条件
  98. var sql = 'delete from '+name+' where '+sol+'="'+qq+'" and '+ww+'='+ee+'';
  99. }else{
  100. //一个检索条件
  101. var sql = 'delete from '+name+' where '+sol+'="'+qq+'"';
  102. }
  103. return new Promise((resolve,reject) =>{
  104. plus.sqlite.executeSql({
  105. name:'msg',
  106. sql:sql,
  107. success(e){
  108. resolve(e);
  109. },
  110. fail(e){
  111. reject(e);
  112. }
  113. })
  114. })
  115. }else{
  116. return new Promise((resolve,reject) =>{reject("错误删除")});
  117. }
  118. }
  119. //修改数据表里的数据
  120. function modifyMsgSQL(listName,name,cont,use,sel){
  121. //表格名,要修改地方的列名,修改后的内容,修改条件查询,列名,内容
  122. var sql;
  123. if(use == undefined){
  124. sql ='update '+listName+' set '+name+'="'+cont+'"';
  125. }else{
  126. sql ='update '+listName+' set '+name+'="'+cont+'" where '+use+'="'+sel+'"';
  127. }
  128. //where前面的是要修改的,后面的是条件,选择哪个
  129. return new Promise((resolve,reject) =>{
  130. plus.sqlite.executeSql({
  131. name:'msg',
  132. sql:sql,
  133. success(e){
  134. resolve(e);
  135. },
  136. fail(e){
  137. reject(e);
  138. }
  139. })
  140. })
  141. }
  142. //关闭数据库
  143. function closeSQL(name){
  144. return new Promise((resolve,reject) =>{
  145. plus.sqlite.closeDatabase({
  146. name:'msg',
  147. success(e){
  148. resolve(e);
  149. },
  150. fail(e){
  151. reject(e);
  152. }
  153. })
  154. })
  155. }
  156. //监听数据库是否开启
  157. function isOpen(name,path){
  158. var ss = name || 'msg';
  159. var qq = path || '_doc/msg.db';
  160. //数据库打开了就返回true,否则返回false
  161. var open = plus.sqlite.isOpenDatabase({
  162. name:ss,
  163. path:qq
  164. })
  165. return open;
  166. }
  167. //一次获取指定数据条数
  168. function pullSQL(id,num){
  169. //id为表名,num为跳过多少条数据
  170. //根据list来倒序拿数据,跳过num条拿取15条
  171. return new Promise((resolve,reject) =>{
  172. plus.sqlite.selectSql({
  173. name:'msg',
  174. sql:'select * from '+id+' order by list desc limit 15 offset '+num+'',
  175. success(e){
  176. resolve(e);
  177. },
  178. fail(e){
  179. reject(e);
  180. }
  181. })
  182. })
  183. }
  184. //把这些方法导出去
  185. export{
  186. openMsgSqlite,
  187. createMsgSQL,
  188. addMsgSQL,
  189. selectMsgSQL
  190. }