App.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. <script>//com.cityexpress1688.ptqs
  2. const toast = uni.requireNativePlugin('Ba-Toast');//android
  3. const callerID = uni.requireNativePlugin('Ba-CallerID');//android
  4. const KJPip = uni.requireNativePlugin('KJ-Pip');//ios
  5. //const bugly = uni.requireNativePlugin("LX-Bugly");
  6. import {
  7. UTSKeepalive
  8. } from "@/uni_modules/wrs-uts-keepalive"
  9. let keepalive = new UTSKeepalive()
  10. import {
  11. setRiderposition,
  12. changeLanguages,
  13. gcj02towgs
  14. } from '@/pages/api/basic';
  15. import api from "@/pages/api/api.js"
  16. export default {
  17. data() {
  18. return {
  19. startFl:0,
  20. timer:'',
  21. cid:'',
  22. isAndroid:false,
  23. androidflType:0,
  24. isInback:false,
  25. iosInit:false,
  26. audioObj:null,
  27. isfirst:true
  28. }
  29. },
  30. globalData:{
  31. _i18n:{},
  32. $t:{},
  33. imViewOpen:false,
  34. sj:{},
  35. iosfl:{},
  36. EXViewOpen:false,
  37. auditStatus:0,
  38. isOnline:true,
  39. token:'',
  40. initgoEasy:{},
  41. goEasycnt:false,
  42. goEasy:null
  43. },
  44. onLaunch: function() {
  45. console.log('App Launch')
  46. //bugly.init("23bea5717e","true")
  47. this.globalData.$t=str=>this.$t(str);
  48. this.globalData.sj=str=>this.getTimestr();
  49. this.globalData.iosfl=index=>this.showpiaof(index);
  50. this.globalData.token = uni.getStorageSync("token");
  51. this.globalData.imViewOpen = false;
  52. this.globalData.EXViewOpen = false;
  53. this.globalData.goEasy = this.goEasy;
  54. this.globalData.initgoEasy=str=>this.connectGoEasy();
  55. let systemInfo = uni.getSystemInfoSync();
  56. this.isAndroid = systemInfo.platform.toLowerCase() === 'android';
  57. var language = uni.getStorageSync('language');
  58. if(''==language||null==language||undefined==language){
  59. uni.setStorageSync('language',"zh-Hant");
  60. uni.setLocale('zh-Hant');
  61. if(!this.isAndroid){
  62. this.$i18n.locale = 'zh-Hant';
  63. }
  64. this.uploadLanguagesSet('zh-Hant');
  65. }
  66. else{
  67. uni.setLocale(language);
  68. if(!this.isAndroid){
  69. this.$i18n.locale = language;
  70. }
  71. this.uploadLanguagesSet(language);
  72. }
  73. this.connectGoEasy();
  74. if(!this.isAndroid){
  75. this.startLocation();
  76. }
  77. if(this.isAndroid){
  78. // 设置回调
  79. UTSKeepalive.setCallback((resp)=>{
  80. console.log('--------',resp)
  81. let opt = resp.opt
  82. switch (opt){
  83. // 定位改变
  84. case "onLocationChanged":
  85. if(this.isAndroid) {
  86. let location = resp.location
  87. this.androidRecvitude(location.longitude,location.latitude);
  88. } else {
  89. let locations = resp.locations
  90. }
  91. break;
  92. default:
  93. break;
  94. }
  95. })
  96. this.kaiqibaohuo();
  97. }
  98. uni.onPushMessage((res) => {
  99. console.log("收到推送消息:",res) //监听推送消息
  100. var payload=res.data.payload;
  101. if(payload.indexOf("rcuser")!=-1){
  102. this.updataxiaoxi(payload);
  103. }
  104. if(res.type=='click'){
  105. var content=res.data.title;
  106. plus.runtime.setBadgeNumber(0);
  107. }
  108. if(res.type=='receive'){
  109. plus.runtime.setBadgeNumber(0);
  110. var content=res.data.content;
  111. if(content.indexOf("location")!=-1){
  112. var objpl=JSON.parse(payload);
  113. this.cid=objpl.cid;
  114. this.getLocationL();
  115. return
  116. }
  117. uni.createPushMessage({
  118. content:res.data.content,
  119. success: (res) => {
  120. console.log(res);
  121. },
  122. fail(er) {
  123. }
  124. });
  125. }
  126. })
  127. var that = this;
  128. //android---------------------------------------------
  129. if(that.isAndroid){
  130. that.audioObj=uni.createInnerAudioContext();
  131. that.audioObj.src='hybrid/html/xuanfu/img/y800.mp3';
  132. }
  133. var globalEvent = uni.requireNativePlugin('globalEvent');
  134. globalEvent.addEventListener('baCallerIdEvent', function(e) {
  135. console.log('baCallerIdEvent:' + JSON.stringify(e));
  136. console.log(e);
  137. if(e.tag=='call'&&e.action=='onClick'){
  138. if(that.isInback){
  139. plus.runtime.launchApplication({pname:'com.cityexpress1688.ptqs'});
  140. }
  141. if(that.androidflType==1){
  142. uni.navigateTo({
  143. url:'/pages/imcall/audioCall?iscaller=0'
  144. })
  145. }
  146. else if(that.androidflType==2){
  147. uni.navigateTo({
  148. url:'/pages/imcall/GoeasyExchange?iscaller=0'
  149. })
  150. }
  151. that.audioObj.pause();
  152. that.hideFW();
  153. }
  154. if(e.tag=='uncall'&&e.action=='onClick'){
  155. that.audioObj.pause();
  156. that.hideFW();
  157. }
  158. });
  159. //ios-------------------------------------------------------------
  160. globalEvent.addEventListener('onPip', function(res) {
  161. console.log("onPip:" + JSON.stringify(res));
  162. if (res.method == "willStartPip") {
  163. console.log("即将开启画中画");
  164. } else if (res.method == "didStartPip") {
  165. console.log("已经开启画中画");
  166. } else if (res.method == "failedStartPip") {
  167. console.log("开启画中画失败");
  168. } else if (res.method == "willStopPip") {
  169. console.log("即将关闭画中画");
  170. //that.imdef();
  171. } else if (res.method == "didStopPip") {
  172. console.log("已经关闭画中画");
  173. } else if (res.method == "restorePip") {
  174. console.log("恢复");
  175. }
  176. });
  177. globalEvent.addEventListener('onWebview', function(res) {
  178. console.log("onWebview:" + JSON.stringify(res));
  179. if (res.method == "finish") {
  180. console.log("页面加载完成");
  181. } else if (res.method == "fail") {
  182. console.log("加载失败");
  183. }
  184. });
  185. if(!this.isAndroid){
  186. this.onH5SendDataCallBack();
  187. }
  188. },
  189. onShow: function() {
  190. console.log('App Show')
  191. this.isInback=false;
  192. this.initflaotView();
  193. //bugly.report("上报内容1111")
  194. if(!this.isAndroid){
  195. this.startLocation()
  196. }
  197. },
  198. onHide: function() {
  199. console.log('App Hide')
  200. this.isInback=true;
  201. // if(!this.isAndroid){
  202. // this.openPip1();
  203. // }
  204. // setInterval(() => {
  205. // console.log("定时任务 count: " + this.count)
  206. // }, 10000);
  207. if(!this.isAndroid){
  208. this.startLocation()
  209. }
  210. },
  211. methods: {
  212. getTimestr(){
  213. var time=new Date();
  214. var n,y,r,h,m,s;
  215. n=time.getFullYear();
  216. y=time.getMonth()+1;
  217. r=time.getDate();
  218. h=time.getHours();
  219. m=time.getMinutes();
  220. s=time.getSeconds();
  221. var timsStr = n+'-'+y+'-'+r+' '+h+':'+m+':'+s;
  222. return timsStr;
  223. },
  224. startLocation(){
  225. console.log('1startLocation')
  226. var token = uni.getStorageSync("token");
  227. //console.log('2222',that.token)
  228. if(token == '' || token == undefined || token == null){
  229. return;
  230. }
  231. else{
  232. if(this.startFl==0){
  233. //console.log(that.startFl);
  234. this.startFl=1;
  235. this.timer = setInterval(() => {
  236. this.getLocationL();
  237. }, 10000);
  238. }
  239. }
  240. },
  241. getLocationL(){
  242. var yszche=uni.getStorageSync('yszche');
  243. // console.log(this.startFl,yszche);
  244. if(1!=yszche){
  245. return;
  246. }
  247. var that = this;
  248. // console.log('uni.getLocation')
  249. uni.getLocation({
  250. type: "wgs84", //默认为 wgs84 返回 gps 坐标//谷歌地图使用 wgs84 坐标,其他地图使用 gcj02 坐标
  251. geocode: "true",
  252. isHighAccuracy: "true",
  253. accuracy: "best", // 精度值为20m
  254. success: function (res) {
  255. // console.log("app定位获取:", res);
  256. if(res.longitude>-180&&res.longitude<180){
  257. if(res.latitude>-90&&res.latitude<90){
  258. if(that.cid!=''){
  259. that.pushMsg(that.cid,res.longitude,res.latitude)
  260. }
  261. that.setRiderposition(res.longitude,res.latitude);
  262. }
  263. }
  264. },
  265. fail(err) {
  266. console.log('getLocation err')
  267. }
  268. });
  269. },
  270. androidRecvitude(longitude,latitude){
  271. var yszche=uni.getStorageSync('yszche');
  272. console.log(this.startFl,yszche);
  273. if(1!=yszche){
  274. return;
  275. }
  276. var token = uni.getStorageSync("token");
  277. //console.log('2222',that.token)
  278. if(token == '' || token == undefined || token == null){
  279. return;
  280. }
  281. this.setRiderposition(longitude,latitude);
  282. },
  283. setRiderposition(longitude,latitude){
  284. this.$http.post(`${setRiderposition}`, {
  285. "longitude":longitude,
  286. "latitude":latitude
  287. },true)
  288. .then(r => {
  289. // console.log('setRiderposition',r)
  290. })
  291. .catch(err => {
  292. console.log(err)
  293. })
  294. },
  295. async uploadLanguagesSet(lang){
  296. var userId = uni.getStorageSync('userId');
  297. if(lang=='yuenan'){
  298. lang='vi';
  299. }
  300. if(lang=='zh-Hans'){
  301. lang='zh-CN';
  302. }
  303. if(lang=='zh-Hant'){
  304. lang='zh-TW';
  305. }
  306. //console.log("changeLanguages");
  307. await this.$http.get(`${changeLanguages}`, {
  308. id:userId,
  309. lang:lang
  310. },false)
  311. .then(async r => {
  312. //console.log(r)
  313. })
  314. .catch(err => {
  315. console.log(err)
  316. })
  317. },
  318. updataxiaoxi(str){
  319. var xiaoxiList = uni.getStorageSync('dachexiaoxi');
  320. if(''==xiaoxiList||null==xiaoxiList||undefined==xiaoxiList){
  321. xiaoxiList=[];
  322. }
  323. xiaoxiList.push(str);
  324. uni.setStorageSync('dachexiaoxi',xiaoxiList);
  325. },
  326. pushMsg(cid,longitude,latitude){//消息推送
  327. var payloadData={
  328. longitude:longitude,
  329. latitude:latitude
  330. }
  331. api('pushMsgYH',{
  332. cid:cid,
  333. title:'骑手定位',
  334. content:'骑手定位',
  335. payload:payloadData
  336. },res=>{
  337. this.cid='';
  338. console.log(res)
  339. },failc=>{
  340. //console.log('getadvertis----',failc)
  341. })
  342. },
  343. //imcall&&immessage------------------------------------------------
  344. initflaotView(){
  345. if(this.isAndroid){
  346. if(this.isfirst){
  347. this.isPermissionFW();
  348. this.isfirst=false
  349. }
  350. }
  351. else{//ios
  352. // var that = this;
  353. // setTimeout(function() {
  354. // // 这里写要延时执行的代码
  355. // that.initPip("landscape");
  356. // }, 2000);
  357. }
  358. },
  359. imcallrecive(str){
  360. if(str.indexOf("\\\"")!=-1){
  361. str=str.replace(/\\\"/g, '\"');
  362. str = str.replace(/\"{/g, '{');
  363. str = str.replace(/}\"/g, '}');
  364. }
  365. var obj = JSON.parse(str)
  366. console.log('imcallrecive',obj)
  367. if(this.isAndroid){
  368. if(this.androidflType!=0){
  369. return;
  370. }
  371. var uobj={
  372. name:obj.nickName,
  373. content:this.$t('audioCall.cteyhcall'),
  374. call:this.$t('audioCall.jietingyy'),
  375. uncall:this.$t('audioCall.jujuejieting')
  376. }
  377. this.androidflType=1;
  378. this.showFW(uobj,false);
  379. this.audioObj.play();
  380. }
  381. else{//ios
  382. this.imCall();
  383. }
  384. },
  385. immessagerecive(str){
  386. if(str.indexOf("\\\"")!=-1){
  387. str=str.replace(/\\\"/g, '\"');
  388. str = str.replace(/\"{/g, '{');
  389. str = str.replace(/}\"/g, '}');
  390. }
  391. var obj = JSON.parse(str);
  392. if(this.isAndroid){
  393. if(this.androidflType!=0){
  394. return;
  395. }
  396. var uobj={
  397. name:obj.nickName,
  398. content:this.$t('exchange.cteyhmsg'),
  399. call:this.$t('exchange.chakanxinxi'),
  400. uncall:this.$t('exchange.quxiao')
  401. }
  402. this.androidflType=2;
  403. this.showFW(uobj,false);
  404. this.audioObj.play();
  405. }
  406. else{//ios
  407. this.imMessage();
  408. }
  409. },
  410. //android-------------------------------------------------
  411. showFW(obj,isInit) { //显示
  412. var that = this;
  413. callerID.show({
  414. gravity: 1, //显示位置:0中间 1上 2下
  415. name:obj.name,//用户昵称 ,
  416. content:obj.content,//说明是骑手来电、来信息
  417. call: obj.call,//接听IM,查看IM信息
  418. uncall: obj.uncall,//拒绝接听IM,拒绝查看IM信息
  419. empty: "",
  420. avatar: "/static/logo.png",
  421. totalHint: "",
  422. tel: "",
  423. list:[]
  424. },
  425. (res) => {
  426. console.log(res);
  427. if(isInit){
  428. that.hideFW()
  429. }
  430. // uni.showToast({
  431. // title: res.msg,
  432. // icon: "none",
  433. // duration: 3000
  434. // })
  435. });
  436. },
  437. hideFW() { //隐藏
  438. this.androidflType=0;
  439. callerID.hide(
  440. (res) => {
  441. console.log(res);
  442. // uni.showToast({
  443. // title: res.msg,
  444. // icon: "none",
  445. // duration: 3000
  446. // })
  447. });
  448. },
  449. permissionFW() { //申请悬浮窗权限
  450. callerID.permission(
  451. (res) => {
  452. console.log(res);
  453. });
  454. },
  455. goPermissionFW() { //跳转到悬浮窗权限页面
  456. callerID.goPermission(
  457. (res) => {
  458. console.log(res);
  459. });
  460. },
  461. isPermissionFW() { //是否申请悬浮窗权限
  462. var that = this;
  463. callerID.isPermission(
  464. (res) => {
  465. console.log(res);
  466. if (res.data) {
  467. if(res.data.isPermission){
  468. var uobj={
  469. name:'CTE',
  470. content:'',
  471. call:'',
  472. uncall:''
  473. }
  474. that.showFW(uobj,true);
  475. }
  476. else{
  477. that.goPermissionFW();
  478. }
  479. }
  480. });
  481. },
  482. //ios--------------------------------------
  483. showpiaof(index){
  484. var that = this;
  485. if(index==1){
  486. setTimeout(function() {
  487. // 这里写要延时执行的代码
  488. that.initPip("landscape");
  489. }, 800);
  490. }
  491. if(index==2){
  492. that.closePip();
  493. }
  494. if(index==3){
  495. var language = uni.getStorageSync('language');
  496. if(language=='yuenan'){
  497. var dic = {
  498. "js": "changeImgvi()"
  499. }
  500. KJPip.webView_evaluateJavaScript(dic, (res) => {
  501. console.log("webView_evaluateJavaScript:" + JSON.stringify(res));
  502. })
  503. }
  504. else{
  505. var dic = {
  506. "js": "changeImg()"
  507. }
  508. KJPip.webView_evaluateJavaScript(dic, (res) => {
  509. console.log("webView_evaluateJavaScript:" + JSON.stringify(res));
  510. })
  511. }
  512. }
  513. if(index==4){
  514. var language = uni.getStorageSync('language');
  515. if(language=='yuenan'){
  516. var dic = {
  517. "js": "changeImgMSvi()"
  518. }
  519. KJPip.webView_evaluateJavaScript(dic, (res) => {
  520. console.log("webView_evaluateJavaScript:" + JSON.stringify(res));
  521. })
  522. }
  523. else{
  524. var dic = {
  525. "js": "changeImgMS()"
  526. }
  527. KJPip.webView_evaluateJavaScript(dic, (res) => {
  528. console.log("webView_evaluateJavaScript:" + JSON.stringify(res));
  529. })
  530. }
  531. }
  532. return '';
  533. },
  534. initPip(shape) {
  535. var dic = {
  536. "shape": shape, //弹窗形状 square(正方形) landscape(横向) portrait(竖向)
  537. }
  538. KJPip.initPip(dic)
  539. console.log('initPip')
  540. var that = this;
  541. that.iosInit=true;
  542. setTimeout(function() {
  543. // 这里写要延时执行的代码
  544. that.openPip1();
  545. }, 2000);
  546. },
  547. openPip1() {//打开,加载本地页面?language='+language
  548. var language = uni.getStorageSync('language');
  549. this.openPip(plus.io.convertLocalFileSystemURL("hybrid/html/xuanfu/index.html"));
  550. },
  551. openPip(url) {
  552. var that = this;
  553. KJPip.isPictureInPictureActive((res) => {
  554. console.log("isPictureInPictureActive:" + JSON.stringify(res));
  555. })
  556. KJPip.isPictureInPictureSuspended((res) => {
  557. console.log("isPictureInPictureSuspended:" + JSON.stringify(res));
  558. })
  559. KJPip.isPictureInPicturePossible((res) => {
  560. console.log("isPictureInPicturePossible:" + JSON.stringify(res));
  561. // if(!res.result){
  562. // setTimeout(function() {
  563. // // 这里写要延时执行的代码
  564. // //that.initPip("landscape");
  565. // }, 800);
  566. // }
  567. })
  568. KJPip.isPictureInPictureSupported((res) => {
  569. console.log("isPictureInPictureSupported:" + JSON.stringify(res));
  570. })
  571. var dic = {
  572. "url": url
  573. }
  574. KJPip.openPip(dic, (res) => {
  575. console.log("openPip:" + JSON.stringify(res));
  576. })
  577. },
  578. closePip() {//关闭弹窗
  579. KJPip.closePip()
  580. },
  581. imCall(){//收到IM语音通话请求
  582. uni.navigateTo({
  583. url:'/pages/imcall/audioCall?iscaller=0'
  584. })
  585. },
  586. imMessage(){//收到im文字聊天请求
  587. // plus.runtime.launchApplication({action:'cityexpress1688ptqs://'},
  588. // function(e){
  589. // console.log('launchApplication',e);
  590. // });
  591. // plus.runtime.openURL('cityexpress1688ptqs://', function(res) {
  592. // console.log('123123',res);
  593. // });
  594. uni.navigateTo({
  595. url:'/pages/imcall/GoeasyExchange?iscaller=0'
  596. })
  597. },
  598. imdef(){//收到im文字聊天请求
  599. var dic = {
  600. "js": "changeImgdef()"
  601. }
  602. KJPip.webView_evaluateJavaScript(dic, (res) => {
  603. console.log("webView_evaluateJavaScript:" + JSON.stringify(res));
  604. })
  605. },
  606. onH5SendDataCallBack() { //注意ios16之后,弹出里的内容不能点击
  607. var that = this;
  608. KJPip.onH5SendDataCallBack((res) => {
  609. console.log("onH5SendDataCallBack:",res);
  610. // if(res.result){
  611. // var obj = JSON.parse(res.result);
  612. // if(obj.key==1){
  613. // uni.navigateTo({
  614. // url:'/pages/imcall/audioCall?iscaller=0'
  615. // })
  616. // that.imdef();
  617. // }
  618. // else if(obj.key==2){
  619. // uni.navigateTo({
  620. // url:'/pages/imcall/ExchangeView?iscaller=0'
  621. // })
  622. // that.imdef();
  623. // }
  624. // }
  625. })
  626. },
  627. //GoEasy--------------------------------------------
  628. connectGoEasy() {
  629. var that = this;
  630. if(that.globalData.goEasycnt==true){
  631. that.goeasydisconnect();
  632. return;
  633. }
  634. var userInfo=uni.getStorageSync('userInfo');
  635. if(''==userInfo||null==userInfo||undefined==userInfo){
  636. return;
  637. }
  638. this.goEasy.connect({
  639. id:'userid_'+userInfo.userId,
  640. data: {
  641. name:userInfo.nickName,
  642. avatar:userInfo.avatar
  643. },
  644. onSuccess: () => {
  645. console.log('GoEasy connect successfully.')
  646. that.goEasyPush();
  647. that.globalData.goEasycnt=true;
  648. },
  649. onFailed: (error) => {
  650. console.log('Failed to connect GoEasy, code:' + error.code + ',error:' + error.content);
  651. },
  652. onProgress: (attempts) => {
  653. console.log('GoEasy is connecting', attempts);
  654. }
  655. });
  656. },
  657. goEasyPush(){
  658. var userInfo=uni.getStorageSync('userInfo');
  659. if(''==userInfo||null==userInfo||undefined==userInfo){
  660. return;
  661. }
  662. //console.log(userInfo);
  663. var that = this;
  664. var pubsub = this.goEasy.pubsub;
  665. pubsub.subscribe({
  666. channel:'userid_'+userInfo.userId,
  667. onMessage: function (message) {
  668. if(message.content){
  669. var obj = JSON.parse(message.content);
  670. if(obj.ptype==1){
  671. if(that.globalData.imViewOpen){
  672. return;
  673. }
  674. uni.setStorageSync('impayload',message.content);
  675. that.imcallrecive(message.content);
  676. }
  677. if(obj.ptype==2){
  678. if(that.globalData.EXViewOpen){
  679. return;
  680. }
  681. uni.setStorageSync('impayload',message.content);
  682. that.immessagerecive(message.content);
  683. }
  684. }
  685. console.log("Channel:" + message.channel + " content:" + message.content)
  686. },
  687. onSuccess: function () {
  688. console.log("Subscribe successfully.")
  689. },
  690. onFailed: function () {
  691. console.log("Subscribe successfully.")
  692. }
  693. });
  694. },
  695. //断开连接
  696. goeasydisconnect(){
  697. var that = this;
  698. this.goEasy.disconnect({
  699. onSuccess: function(){
  700. console.log("GoEasy disconnect successfully.")
  701. that.globalData.goEasycnt=false;
  702. that.connectGoEasy();
  703. },
  704. onFailed: function(error){
  705. console.log("Failed to disconnect GoEasy, code:"+error.code+ ",error:"+error.content);
  706. }
  707. });
  708. },
  709. //开启保活---------------------------------------------------------------------------------
  710. kaiqibaohuo(){
  711. if (this.isAndroid) {
  712. this.requestPermission([
  713. "android.permission.FOREGROUND_SERVICE",
  714. "android.permission.POST_NOTIFICATIONS",
  715. "android.permission.ACCESS_COARSE_LOCATION",
  716. "android.permission.ACCESS_FINE_LOCATION",
  717. "android.permission.WRITE_EXTERNAL_STORAGE",
  718. "android.permission.ACCESS_BACKGROUND_LOCATION"
  719. ]);
  720. } else {
  721. var authorizationStatus = UTSKeepalive.getAuthorizationStatus();
  722. if (authorizationStatus == 0) {// 没有授权过定位时,请求定位权限
  723. keepalive.requestLocationAuthorization()
  724. }
  725. // 监听app前后台状态
  726. UTSKeepalive.listenerApp((resp) => {
  727. let opt = resp.opt
  728. switch (opt) {
  729. // app进入后台
  730. case "enterBackground": {
  731. var authorizationStatus = UTSKeepalive.getAuthorizationStatus();
  732. console.log("authorizationStatus:" + authorizationStatus)
  733. // app进入后台,此时采用定位保活还是采用播放音乐保活可以根据自己业务决定
  734. if (authorizationStatus == 3) { // 当定位权限是总是一直定位时采用定位来保活
  735. console.log("定位保活")
  736. keepalive.startLocation({
  737. desiredAccuracy: "NearestTenMeters",
  738. allowsBackgroundLocationUpdates: true, // 允许后台更新位置
  739. pausesLocationUpdatesAutomatically: false // 不允许自动暂停更新位置
  740. })
  741. } else { // 采用播放音乐保活
  742. console.log("播放音乐保活")
  743. UTSKeepalive.setCategory("playback")
  744. var url = plus.io.convertLocalFileSystemURL('_www/static/bgmusic.mp3');
  745. keepalive.playMusic({
  746. url: url,
  747. numberOfLoops: -1, // 循环此时,-1表示无限循环
  748. volume: 0 // 音量,0
  749. });
  750. }
  751. // 通知系统开始后台任务
  752. UTSKeepalive.beginBackgroundTask()
  753. }
  754. break;
  755. // app回到前台
  756. case "enterForeground": {
  757. // 停止播放音乐
  758. keepalive.stopMusic()
  759. // 停止定位
  760. keepalive.stopLocation()
  761. // 通知系统结束后台任务
  762. UTSKeepalive.endBackgroundTask()
  763. }
  764. break;
  765. default:
  766. break;
  767. }
  768. })
  769. }
  770. },
  771. requestPermission: function(permissions) {
  772. var that = this;
  773. plus.android.requestPermissions(
  774. permissions,
  775. function(resultObj) {
  776. for (var i = 0; i < resultObj.granted.length; i++) {
  777. var grantedPermission = resultObj.granted[i];
  778. //console.log('已获取的权限:' + grantedPermission);
  779. }
  780. if(resultObj.granted.length>=5){
  781. that.keepAlive();
  782. return;
  783. }
  784. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  785. var deniedPresentPermission = resultObj.deniedPresent[i];
  786. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  787. }
  788. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  789. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  790. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  791. }
  792. // 若所需权限被永久拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  793. if (resultObj.deniedAlways.length > 0) {
  794. uni.showModal({
  795. cancelText: that.$t('index.quxiao'), // 取消按钮的文字
  796. confirmText: that.$t('index.queren'), // 确认按钮的文字
  797. title: that.$t('api.message'),
  798. content: that.$t('user.dingweiquanxian'),
  799. success: function (res) {
  800. if (res.confirm) { //这里是点击了确定以后
  801. that.kaiqidingwei();
  802. } else { //这里是点击了取消以后
  803. console.log('用户点击取消')
  804. }
  805. }
  806. })
  807. }
  808. },
  809. function(error) {
  810. console.log('申请权限错误:' + error.code + " = " + error.message);
  811. });
  812. },
  813. keepAlive() {
  814. console.log('keepAlive')
  815. let params = this.getAndroidNotification();
  816. console.log(params)
  817. UTSKeepalive.startForegroundService(params)
  818. UTSKeepalive.startJobService(params)
  819. UTSKeepalive.startLocalService(params)
  820. UTSKeepalive.startRemoteService(params)
  821. UTSKeepalive.startScreenLockReceiver();
  822. // 后台定位功能,用不到后台定位可以不需要调用
  823. let locationParams = {}
  824. locationParams.notification = params
  825. locationParams.location = {
  826. provider: "gps", // 支持gps、fused
  827. minTimeMs:10000, // 间隔时间,这里为了测试效果,设置为500毫秒,实际情况根据自己业务设置
  828. minDistanceM: 5.0 // 间隔距离,这里为了测试效果,设置为0.5米,实际情况根据自己业务设置
  829. }
  830. UTSKeepalive.startLocstionService(locationParams)
  831. },
  832. //开启定位权限------------------------------
  833. kaiqidingwei(){
  834. var Intent = plus.android.importClass("android.content.Intent");
  835. var Settings = plus.android.importClass("android.provider.Settings");
  836. var Uri = plus.android.importClass("android.net.Uri");
  837. var mainActivity = plus.android.runtimeMainActivity();
  838. var intent = new Intent();
  839. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  840. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  841. intent.setData(uri);
  842. mainActivity.startActivity(intent);
  843. },
  844. getAndroidNotification() {
  845. let channelId = 10
  846. let params = {}
  847. params.identifier = "123" // 通知ID,主要用于修改、删除通知,android里的identifier必须是数字,iOS的identifier可以是任意字符串
  848. params.channel = { // 如果已经channelId的channel则不创建,如果没有则会自动创建
  849. channelId: channelId,
  850. channelName: "騎手端持續為您服務",
  851. importance: 5, // 3: default 4: high 2: low 5: max 1: min 0: none
  852. lockscreenVisibility: 1, //1: public 0: private -1: secret
  853. // description: "Bảo hành"
  854. }
  855. params.notification = {
  856. channelId: channelId, // 此消息通知是属于哪个channel的
  857. contentTitle: "騎手端持續為您服務",
  858. // contentText: "Bảo hành",
  859. visibility: 1, // 1: public 0: private -1: secret
  860. smallIcon: { // 小图标,必传
  861. type: "resource", // 固定
  862. defType: "drawable", // 固定
  863. name: "not" // 文件名,不要带文件后缀,对应nativeResources/android/res/drawable文件夹下的图片
  864. },
  865. autoCancel: false
  866. }
  867. return params
  868. },
  869. },
  870. }
  871. </script>
  872. <style lang="scss">
  873. .content{
  874. display: flex;
  875. flex-direction: column;
  876. width: 94%;
  877. margin-left: 3%;
  878. background-color: white;
  879. border-radius: 16rpx;
  880. box-shadow: 0rpx 0rpx 10rpx 0rpx lightgray;
  881. }
  882. .contentInRowL{
  883. display: flex;
  884. flex-direction: row;
  885. justify-content: flex-start;
  886. }
  887. .contentInRowR{
  888. display: flex;
  889. flex-direction: row;
  890. justify-content:flex-end;
  891. }
  892. .contentInRowC{
  893. display: flex;
  894. flex-direction: row;
  895. justify-content: center;
  896. align-items: center;
  897. }
  898. .contentInRowS{
  899. display: flex;
  900. flex-direction: row;
  901. align-items: center;
  902. justify-content: space-between;
  903. }
  904. .contentColumn{
  905. display: flex;
  906. flex-direction: column;
  907. }
  908. .contentColumnC{
  909. display: flex;
  910. flex-direction: column;
  911. align-items: center;
  912. justify-content: center;
  913. }
  914. .text1row{
  915. overflow: hidden;
  916. text-overflow: ellipsis;
  917. /* #ifndef APP-PLUS-NVUE */
  918. display: -webkit-box;
  919. -webkit-line-clamp: 1;
  920. -webkit-box-orient: vertical;
  921. /* #endif */
  922. }
  923. .text2row{
  924. overflow: hidden;
  925. text-overflow: ellipsis;
  926. /* #ifndef APP-PLUS-NVUE */
  927. display: -webkit-box;
  928. -webkit-line-clamp: 2;
  929. -webkit-box-orient: vertical;
  930. /* #endif */
  931. }
  932. </style>