App.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <!-- com.encryptedIM.im com.aiim.app.demo 6OQrPSweuBcdbhg -->
  2. <script setup lang="ts">//setup lang="ts"
  3. import {onHide, onLaunch, onShow} from "@dcloudio/uni-app";
  4. import {getCurrentInstance,watch} from "vue";
  5. import {storeToRefs} from "pinia";
  6. import {useSysStore} from "@/store/sysStore";
  7. import Auth from "@/api/Auth";
  8. import {clearVoipTk} from "@/api/Login";
  9. import VimPlugin from "@/plugins/VimPlugin";
  10. import WebrtcCtr from "@/imcall/WebrtcCtr";
  11. import {useWsStore} from "@/store/WsStore";
  12. import ChatType from "@/utils/ChatType";
  13. import type VideoSendInfo from "@/plugins/video/mode/VideoSendInfo";
  14. import SendVideoCode from "@/plugins/video/SendVideoCode";
  15. const bugly = uni.requireNativePlugin("LX-Bugly");
  16. import dbactApi from '@/api/DBactApi';
  17. import {useUserStore} from "@/store/userStore";
  18. import UserApi from '@/api/UserApi';
  19. import { usePeerStore } from '@/store/peerStore';
  20. import {
  21. UTSVoipMgr,
  22. UTSLocalNotification
  23. } from "@/uni_modules/wrs-uts-voip";
  24. import {
  25. UTSWebRTC
  26. } from "@/uni_modules/wrs-uts-webrtc";
  27. let sendInfo=null;
  28. let isRc=false;
  29. let isclearVoipTk=false;
  30. var mtpushModule = uni.requireNativePlugin("EL-MTPush");
  31. const wsStore = useWsStore();
  32. const peerStore = usePeerStore();
  33. const {isClose} = storeToRefs(peerStore);
  34. watch(isClose, (newVal) => {
  35. console.log('isCloseisClose APP',newVal)
  36. if (newVal) {
  37. if(uni.getSystemInfoSync().platform == "ios"&&isRc==false){
  38. UTSVoipMgr.endCall({
  39. uuid: uni.getStorageSync("voip_UUID")
  40. }, (resp: any)=>{
  41. if(resp.false){
  42. }
  43. else{
  44. usePeerStore().updateCloseStatus(false);
  45. }
  46. })
  47. }
  48. }
  49. })
  50. onLaunch(() => {
  51. plus.screen.lockOrientation('portrait-primary');
  52. if(uni.getSystemInfoSync().platform == 'android'){
  53. bugly.init("ed19003953","true");
  54. }
  55. UTSWebRTC.peerConnectionFactoryInit();
  56. setInterval(() => {
  57. uni.getNetworkType({
  58. success: function (res) {
  59. const networkType = res.networkType;
  60. if (networkType === 'none') {
  61. //console.log('networkType N')
  62. isclearVoipTk=false;
  63. if(Auth.getToken()!=''){
  64. Auth.logout();//无法连接网络,跳转登录页面
  65. }
  66. } else {
  67. // 有网络
  68. if(Auth.getToken()==''&&Auth.getvoipTk()!=''&&!isclearVoipTk){
  69. var data={
  70. "voipTk":Auth.getvoipTk()
  71. }
  72. clearVoipTk(JSON.stringify(data)).then((res)=>{
  73. if(res.code==200){
  74. isclearVoipTk=true;
  75. }
  76. console.log('clearVoipTk1',res);
  77. })
  78. .catch((err) => {
  79. console.log('clearVoipTk2',err);
  80. });
  81. }
  82. //console.log('networkType Y')
  83. }
  84. }
  85. });
  86. }, 5000);
  87. uni.getSystemInfo({
  88. success: function (e) {
  89. //@ts-ignore
  90. const {proxy} = getCurrentInstance();
  91. // #ifndef MP
  92. proxy.statusBar = e.statusBarHeight;
  93. if (e.platform == 'android') {
  94. //@ts-ignore
  95. proxy.customBar = e.statusBarHeight + 50;
  96. } else {
  97. //@ts-ignore
  98. proxy.customBar = e.statusBarHeight + 45;
  99. }
  100. ;
  101. // #endif
  102. // #ifdef MP-WEIXIN
  103. proxy.statusBar = e.statusBarHeight;
  104. //@ts-ignore
  105. let custom = wx.getMenuButtonBoundingClientRect();
  106. proxy.Custom = custom;
  107. //@ts-ignore
  108. proxy.customBar = custom.bottom + custom.top - e.statusBarHeight;
  109. // #endif
  110. // #ifdef MP-ALIPAY
  111. proxy.statusBar = e.statusBarHeight;
  112. //@ts-ignore
  113. proxy.customBar = e.statusBarHeight + e.titleBarHeight;
  114. // #endif
  115. // #ifdef APP-PLUS
  116. //自动更新 安卓
  117. if (e.platform === 'android') {
  118. // app_upgrade(async () => {
  119. // //查询是否更新
  120. // const {statusCode, data} = await uni.request({
  121. // url: update.checkUrl,
  122. // method: 'GET'
  123. // })
  124. // const version = packageJson.version
  125. // if (statusCode === 200) {
  126. // return {
  127. // changelog: data.changelog,
  128. // status: data.android > version ? 1 : 0, // 0 无新版本 | 1 有新版本
  129. // path: data.url // 新apk地址
  130. // }
  131. // }
  132. // })
  133. }
  134. // #endif
  135. }
  136. })
  137. //极光推送
  138. console.log('App Launch')
  139. var platformType='a';
  140. if(uni.getSystemInfoSync().platform == "ios"){
  141. platformType='i';
  142. mtpushModule.requestNotificationAuthorization((result)=>{
  143. let status = result.status
  144. if (status < 2) {
  145. uni.showToast({
  146. icon: 'none',
  147. title: '您还没有打开通知权限',
  148. duration: 3000
  149. })
  150. }
  151. })
  152. }
  153. mtpushModule.initPushService();
  154. mtpushModule.setLoggerEnable(true);
  155. console.log(mtpushModule);
  156. mtpushModule.addConnectEventListener(result=>{
  157. console.log(result);
  158. let connectEnable = result.connectEnable
  159. uni.$emit('connectStatusChange',connectEnable)
  160. mtpushModule.getRegistrationID(resultID=>{
  161. let registerID = resultID.registerID
  162. console.log(registerID)
  163. Auth.setregisterID(registerID);
  164. if(Auth.getToken()&&Auth.getToken()!=''){
  165. const currentUser = useUserStore().getUser();
  166. currentUser.registrationId=registerID;
  167. var data={
  168. "userId": currentUser?.id,
  169. "registrationId":registerID,
  170. "platformType":platformType//a---(android),i---(ios)
  171. }
  172. UserApi.setUser(JSON.stringify(data)).then((res)=>{
  173. console.log('setuser',res);
  174. });
  175. }
  176. })
  177. });
  178. mtpushModule.addNotificationListener(result=>{
  179. console.log(result);
  180. // let notificationEventType = result.notificationEventType
  181. // let messageID = result.messageID
  182. // let title = result.title
  183. // let content = result.content
  184. // let extras = result.extras
  185. // uni.showToast({
  186. // icon: 'none',
  187. // title: JSON.stringify(result),
  188. // duration: 3000
  189. // })
  190. });
  191. mtpushModule.addCustomMessageListener(result=>{
  192. console.log(result);
  193. // let type = result.type
  194. // let messageType = result.messageType
  195. // let content = result.content
  196. // uni.showToast({
  197. // icon: 'none',
  198. // title: JSON.stringify(result),
  199. // duration: 3000
  200. // })
  201. })
  202. if(uni.getSystemInfoSync().platform == "ios"){
  203. mtpushModule.addLocalNotificationListener(result=>{
  204. console.log(result);
  205. // let messageID = result.messageID
  206. // let title = result.title
  207. // let content = result.content
  208. // let extras = result.extras
  209. // uni.showToast({
  210. // icon: 'none',
  211. // title: JSON.stringify(result),
  212. // duration: 3000
  213. // })
  214. })
  215. }
  216. if(platformType!='i'){
  217. return;
  218. }
  219. // 请求通知权限
  220. UTSLocalNotification.requestAuthorization({
  221. types: ["badge", "sound", "alert"]
  222. }, (resp) => {
  223. let flag = resp.flag
  224. if (!flag) { // 请求权限失败
  225. console.log("requestAuthorization:" + JSON.stringify(resp))
  226. }
  227. })
  228. console.log('App Launch')
  229. UTSVoipMgr.onCallback((resp:any) => {
  230. let opt = resp.opt
  231. console.log("onCallback:",resp)
  232. switch (opt) {
  233. // 获取到本机token,需要上传给后端,后端发送voip时需要token参数,token可以理解为手机的ID
  234. case "didUpdatePushCredentials":
  235. let token = resp.token
  236. console.log("token:" + token)
  237. Auth.setvoipTk(token)
  238. if(Auth.getToken()&&Auth.getToken()!=''){
  239. const currentUser = useUserStore().getUser();
  240. var data={
  241. "userId": currentUser?.id,
  242. "voipTk":Auth.getvoipTk(),
  243. }
  244. console.log(data)
  245. UserApi.setUser(JSON.stringify(data)).then((res)=>{
  246. console.log('setuser',res);
  247. });
  248. }
  249. break;
  250. // 用户接听了电话
  251. case "performAnswerCall":
  252. console.log("performAnswerCall:",resp)
  253. if(sendInfo&&sendInfo!=''){
  254. WebrtcCtr.messageListener(sendInfo.message);//新的通话方式
  255. //VimPlugin.messageListener(sendInfo);
  256. isRc=true;
  257. }
  258. break;
  259. // 电话挂断
  260. case "performEndCall":
  261. console.log("performEndCall:",isRc)
  262. if(isRc){
  263. return;
  264. }
  265. if(sendInfo&&sendInfo!=''){
  266. const userId = useUserStore().getUser()?.id
  267. if (userId) {
  268. const closeMessage: VideoSendInfo<VideoClose> = {
  269. code: SendVideoCode.CLOSE,
  270. message: {
  271. chatId:sendInfo.message.fromId,
  272. fromId: sendInfo.message.chatId,
  273. timestamp: new Date().getTime(),
  274. type: ChatType.FRIEND
  275. }
  276. }
  277. wsStore.send(JSON.stringify(closeMessage))
  278. }
  279. }
  280. break;
  281. // 拉起接听电话界面结果
  282. case "startCall":
  283. Auth.setvoipUUID(resp.uuid);
  284. isRc=false;
  285. console.log("startCall:", resp)
  286. UTSVoipMgr.deleteWaitingResponseUuid(resp.uuid);
  287. break;
  288. default:
  289. console.log('default',resp,)
  290. sendInfo=resp.payload.imMsg;
  291. break;
  292. }
  293. })
  294. });
  295. onShow(() => {
  296. if(uni.getSystemInfoSync().platform == 'android'){
  297. //bugly.report("上报内容11112")
  298. }
  299. plus.runtime.setBadgeNumber(0);
  300. if (Auth.getToken()) {
  301. if (wsStore.wsRequest.socket?.readyState === 1) {
  302. wsStore.checkStatus()
  303. } else {
  304. wsStore.close(true)
  305. wsStore.init()
  306. }
  307. }
  308. if(dbactApi.DBisOpen()){
  309. //console.log('dbactApi.DBisOpen')
  310. if (Auth.getToken()){
  311. dbactApi.createMSGtable();
  312. dbactApi.createchattable();
  313. dbactApi.createLocalMSGtable();
  314. }
  315. }else{
  316. dbactApi.openDb().then((res)=>{
  317. if (Auth.getToken()){
  318. dbactApi.createMSGtable();
  319. dbactApi.createchattable();
  320. dbactApi.createLocalMSGtable();
  321. }
  322. });
  323. }
  324. useSysStore().setShow(true)
  325. });
  326. onHide(() => {
  327. console.log('onHide')
  328. useSysStore().setShow(false)
  329. dbactApi.closeDB();
  330. });
  331. </script>
  332. <style>
  333. @import "colorui/main.css";
  334. @import "colorui/icon.css";
  335. @import "static/css/font.css";
  336. page {
  337. height: 100%;
  338. }
  339. .tab-bar-mar {
  340. margin-bottom: 140upx
  341. }
  342. .contentInRowL{
  343. display: flex;
  344. flex-direction: row;
  345. justify-content: flex-start;
  346. }
  347. .contentInRowC{
  348. display: flex;
  349. flex-direction: row;
  350. justify-content: center;
  351. align-items: center;
  352. }
  353. .contentInRowR{
  354. display: flex;
  355. flex-direction: row;
  356. justify-content: flex-end;
  357. }
  358. .contentInRowS{
  359. display: flex;
  360. flex-direction: row;
  361. align-items: center;
  362. justify-content: space-between;
  363. }
  364. .contentColumn{
  365. display: flex;
  366. flex-direction: column;
  367. }
  368. .contentColumnC{
  369. display: flex;
  370. flex-direction: column;
  371. align-items: center;
  372. justify-content: center;
  373. }
  374. </style>