action.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import http from "./http.js"
  2. class Action {
  3. /** 播放音效 */
  4. playVoice(path){
  5. var innerAudioContext = uni.createInnerAudioContext();
  6. innerAudioContext.autoplay = true;
  7. innerAudioContext.src=path;
  8. innerAudioContext.play();
  9. }
  10. toChat(message){
  11. if(uni.getStorageSync('ispush')===true){
  12. uni.setStorageSync('ispush',false)
  13. if(uni.getStorageSync('access_token')>0){
  14. if(message.id>0){
  15. var url="../friend/chat?id="+message.id;
  16. if(message.type=='group') url="../group/chat?id="+message.id;
  17. else if(message.type=='request') {
  18. url="../friend/request?type="+message.senderid;
  19. }
  20. uni.navigateTo({
  21. url:url
  22. })
  23. }
  24. else{
  25. uni.reLaunch({
  26. url:'../index/index'
  27. })
  28. }
  29. }
  30. else{
  31. uni.navigateTo({
  32. url:'../login/index'
  33. })
  34. }
  35. }
  36. }
  37. timestampToTime(timestamp) {
  38. var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  39. var Y = date.getFullYear() ;
  40. var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
  41. var D = date.getDate();
  42. var h = date.getHours();
  43. var m = date.getMinutes();
  44. var s = date.getSeconds();
  45. if(D<10) D='0'+D;
  46. if(h<10) h='0'+h;
  47. if(m<10) m='0'+m;
  48. if(s<10) s='0'+s;
  49. return Y+'-'+M+'-'+D+' '+h+':'+m+':'+s;
  50. }
  51. /** 时间戳转换 */
  52. timestampFormat( timestamp ) {
  53. let curTimestamp = parseInt(new Date().getTime() / 1000), //当前时间戳
  54. timestampDiff = curTimestamp - timestamp, // 参数时间戳与当前时间戳相差秒数
  55. curDate = new Date( curTimestamp * 1000 ), // 当前时间日期对象
  56. tmDate = new Date( timestamp * 1000 ), // 参数时间戳转换成的日期对象
  57. Y = tmDate.getFullYear(),
  58. m = tmDate.getMonth() + 1, d = tmDate.getDate(),
  59. H = tmDate.getHours(), i = tmDate.getMinutes(),
  60. s = tmDate.getSeconds();
  61. if ( timestampDiff < 60 ) { // 一分钟以内
  62. return "刚刚";
  63. } else if( timestampDiff < 3600 ) { // 一小时前之内
  64. return Math.floor( timestampDiff / 60 ) + "分钟前";
  65. } else if ( curDate.getFullYear() == Y && curDate.getMonth()+1 == m && curDate.getDate() == d ) {
  66. return '今天 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') + i);
  67. } else {
  68. var newDate = new Date( (curTimestamp - 86400) * 1000 ); // 参数中的时间戳加一天转换成的日期对象
  69. if ( newDate.getFullYear() == Y && newDate.getMonth()+1 == m && newDate.getDate() == d ) {
  70. return '昨天 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') + i);
  71. } else if ( curDate.getFullYear() == Y ) {
  72. return ((String(m).length == 1 ? '0' : '') + m) + '月' + ((String(d).length == 1 ? '0' : '') + d) + '日 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') + i);
  73. } else {
  74. return Y + '年' + ((String(m).length == 1 ? '0' : '') + m) + '月' + ((String(d).length == 1 ? '0' : '') + d) + '日 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') + i);
  75. }
  76. }
  77. }
  78. /** 显示状态通知提醒 */
  79. setStatusTips(){
  80. let pages = getCurrentPages();
  81. if(pages.length < 1){
  82. return;
  83. }
  84. let route = pages[pages.length - 1].route;
  85. var num=0;
  86. if(uni.getStorageSync('access_token')){
  87. try{
  88. var msg_key=uni.getStorageSync('access_token')+'_chat_msglist';
  89. var msg_list=uni.getStorageSync(msg_key);
  90. msg_list.map(item=>{
  91. num+=parseInt(item.msg.unread)
  92. })
  93. }catch(e){
  94. }
  95. if(num){
  96. uni.setTabBarBadge({
  97. index: 0,
  98. text: (num + ''),
  99. });
  100. if(route == 'pages/index/index'){
  101. uni.setNavigationBarTitle({
  102. title: '消息' + '(' + num +')',
  103. });
  104. }
  105. }
  106. else{
  107. uni.removeTabBarBadge({ index: 0 });
  108. if(route == 'pages/index/index'){
  109. uni.setNavigationBarTitle({
  110. title: '消息' ,
  111. });
  112. }
  113. }
  114. }
  115. // #ifdef APP-PLUS
  116. if(plus.os.name=='iOS')
  117. plus.runtime.setBadgeNumber(num);
  118. // #endif
  119. return num;
  120. }
  121. /** 路由守卫执行方法 */
  122. routeTool() {
  123. let token = uni.getStorageSync('access_token');
  124. /** 没有token就跳转到登陆去获得token */
  125. if(!token){
  126. uni.reLaunch({
  127. url: '/pages/login/index'
  128. });
  129. return;
  130. }
  131. /** 如果没有连接上socket,则连接 */
  132. if(!_data.data('socket_state')) {
  133. _mixins.methods.$socketSend();
  134. }
  135. }
  136. addFrend(userid,mark){
  137. }
  138. circleUpate(data){
  139. var k=uni.getStorageSync('access_token')+'_circle_data';
  140. var circle_data=uni.getStorageSync(k);
  141. var newdata=[];
  142. var info=data.info;
  143. try{
  144. circle_data.map(item=>{
  145. if(parseInt(item.id)==parseInt(data.id)){
  146. if(data.action!='delete'){
  147. item.like=info.like;
  148. item.comment=info.comment;
  149. newdata.push(item);
  150. }
  151. }else{
  152. newdata.push(item);
  153. }
  154. })
  155. uni.setStorageSync(k,newdata);
  156. }catch(e){
  157. //TODO handle the exception
  158. }
  159. }
  160. image_cache(image_url,call_back) {
  161. var store=uni.getStorageSync(image_url);
  162. if(store){
  163. console.log('本地存在缓存',store);
  164. }else{
  165. console.log('本地没有缓存,需要下载');
  166. uni.downloadFile({
  167. url: image_url,
  168. success: (res) => {
  169. console.log('图片下载成功',res);
  170. if (res.statusCode === 200) {
  171. uni.setStorageSync(res);
  172. }
  173. }
  174. });
  175. }
  176. }
  177. init(){
  178. http.setWait(false).get('index.php?act=init',{}).then(res=>{
  179. var data=res.data;
  180. uni.setStorageSync('system',data.system);
  181. uni.setStorageSync('note',data.note);
  182. })
  183. // #ifdef APP-PLUS
  184. this.bindcid()
  185. // #endif
  186. }
  187. bindcid(){
  188. // #ifdef APP-PLUS
  189. var user=uni.getStorageSync('userInfo');
  190. if(user.id>0){
  191. /*var clientid= plus.push.getClientInfo().clientid;
  192. http.setWait(false).get('index.php?act=bindcid',{cid:clientid,userid:user.id,osname:plus.os.name}).then(res=>{
  193. })*/
  194. }
  195. // #endif
  196. }
  197. quitcid(){
  198. // #ifdef APP-PLUS
  199. /* var clientid= plus.push.getClientInfo().clientid;
  200. http.setWait(false).get('index.php?act=quitcid',{cid:clientid}).then(res=>{
  201. }) */
  202. // #endif
  203. }
  204. profileTips(tips,path){
  205. uni.showModal({
  206. title: '提示',
  207. content: tips,
  208. showCancel: true,
  209. cancelText: '取消',
  210. confirmText: '去设置',
  211. success: res => {
  212. if(res.confirm) {
  213. uni.setStorageSync('profileback',path);
  214. uni.redirectTo({
  215. url:"/pages/mine/profile"
  216. })
  217. }
  218. }
  219. });
  220. }
  221. loginTips(tips,path){
  222. if(uni.getStorageSync('access_token')>0) {
  223. return true;
  224. }
  225. else{
  226. uni.showModal({
  227. title: '提示',
  228. content: tips,
  229. showCancel: true,
  230. cancelText: '取消',
  231. confirmText: '登录',
  232. success: res => {
  233. if(res.confirm) {
  234. uni.setStorageSync('gourl',path);
  235. uni.redirectTo({
  236. url:"/pages/login/index"
  237. })
  238. }
  239. }
  240. });
  241. return false;
  242. }
  243. }
  244. //检查用户是否被冻结
  245. check_userlock(){
  246. var user= uni.getStorageSync('userInfo');
  247. if(user.lock_time==undefined || user.lock_time==null) user.lock_time=0;
  248. if(user.status==1 || (user.status==2 && parseInt(user.lock_time)>= Date.parse(new Date())/1000)){
  249. uni.showModal({
  250. title: '提示',
  251. content: "您的账户已被冻结,无法完成此项操作\n如有问题,请联系客服",
  252. showCancel: true,
  253. cancelText: '取消',
  254. confirmText: '联系客服',
  255. success: res => {
  256. if(res.confirm) {
  257. var system=uni.getStorageSync('system');
  258. uni.redirectTo({
  259. url:"/pages/friend/chat?id="+system['admin_id']
  260. })
  261. }
  262. }
  263. });
  264. return false;
  265. }
  266. else{
  267. return true;
  268. }
  269. }
  270. }
  271. export default new Action();