until.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. // vuex使用
  2. import store from '@/store/index.js'
  3. import request from '@/api/request.js'
  4. import amapFile from "@/libs/amap-wx.130.js";
  5. // 跳转tab
  6. function beforeSwitch(e) {
  7. // 返回一个promisea
  8. let assitindex = new Promise((resolve, reject) => {
  9. //验证是否是当前页面重复跳转
  10. let lastcurrentpro = store.state.lastcurrentpro
  11. let currentpro = store.state.currentpro
  12. if (lastcurrentpro === null) {
  13. resolve('赋值tabbar下标成功')
  14. } else {
  15. // 非首次登录,验证是否重复跳转
  16. if (lastcurrentpro !== e) {
  17. resolve('赋值tabbar下标成功')
  18. } else {
  19. reject('重复跳转')
  20. }
  21. }
  22. })
  23. //赋值完毕跳转
  24. assitindex.then(function(data) {
  25. uni.switchTab({
  26. url: store.state.vuex_tabbar[store.state.currentpro].pagePath
  27. })
  28. }).catch(function(reason) {
  29. console.log(reason)
  30. })
  31. }
  32. //跳转登录
  33. function gologin(text) {
  34. //清除缓存
  35. uni.clearStorage()
  36. uni.showToast({
  37. icon: 'none',
  38. title: text,
  39. duration: 600
  40. })
  41. setTimeout(() => {
  42. uni.redirectTo({
  43. url: '/pages/usercenter/login/login'
  44. })
  45. }, 600)
  46. }
  47. //修正路径tabber
  48. function pageaddress(route) {
  49. store.state.vuex_tabbar.forEach((item, index) => {
  50. if (item.pagePath === route) {
  51. store.commit('changecurrentprot', index)
  52. }
  53. })
  54. }
  55. //发送指令(向PC)
  56. function postMessage(pdata) {
  57. // #ifdef H5
  58. parent.postMessage(JSON.stringify(pdata), "*");
  59. // #endif
  60. }
  61. // 获取配置
  62. function getsetting(data) {
  63. return request({
  64. url: 'api/index/setting',
  65. method: 'post',
  66. data
  67. })
  68. }
  69. //微信授权登录
  70. function h5_wx_auth(appid, redirect_uri) {
  71. //固定方法 打开授权页面
  72. uni.showLoading({
  73. title: '获取授权'
  74. });
  75. setTimeout(function() {
  76. uni.hideLoading();
  77. window.location.href =
  78. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  79. appid +
  80. '&redirect_uri=' +
  81. redirect_uri +
  82. '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
  83. }, 1000);
  84. }
  85. // 扫一扫
  86. function getscan(data) {
  87. return request({
  88. url: 'api/vip/getscan',
  89. method: 'post',
  90. data
  91. })
  92. }
  93. //单选更改数据通用处理方法
  94. function radiochangedata(e, operationdata) {
  95. let params = {
  96. item: null,
  97. list: operationdata
  98. }
  99. operationdata.forEach((item, index) => {
  100. if (item.id === e.id) {
  101. item.selected = 1
  102. params.item = item
  103. return params
  104. } else {
  105. item.selected = 0
  106. }
  107. })
  108. return params
  109. }
  110. // 多选更改数据通用处理方法
  111. function multisedata(e, operationdata) {
  112. let seedlengh = operationdata.filter(item => item.selected === 1)
  113. let selength = seedlengh.filter(item => item.id === e.id).length
  114. if (selength > 0) {
  115. operationdata.forEach((item, index) => {
  116. if (item.id === e.id) {
  117. if (item.selected === 1) {
  118. item.selected = 0
  119. }
  120. }
  121. })
  122. return operationdata
  123. } else {
  124. if (seedlengh.length < 5) {
  125. operationdata.forEach((item, index) => {
  126. if (item.id === e.id) {
  127. if (item.selected === 0) {
  128. item.selected = 1
  129. }
  130. }
  131. })
  132. } else {
  133. uni.showToast({
  134. title: '最多选五项',
  135. icon: 'none'
  136. })
  137. }
  138. }
  139. return operationdata
  140. }
  141. // form多选装载数据
  142. function setdata(data) {
  143. // type等于1则是单选装载数据,否则是多选装载数据
  144. if (data.length === 0) {
  145. uni.showToast({
  146. title: '请选择数据',
  147. icon: 'none'
  148. })
  149. return ''
  150. } else {
  151. let arraystring = ''
  152. let adnum = 0
  153. data.forEach((item, index) => {
  154. if (item.selected === 1) {
  155. if (index !== data.length && adnum !== 0) {
  156. arraystring = arraystring + ','
  157. }
  158. arraystring = arraystring + item.name
  159. adnum = adnum + 1
  160. }
  161. })
  162. return arraystring
  163. }
  164. }
  165. // form多选装载数据
  166. function setdata_(data) {
  167. // type等于1则是单选装载数据,否则是多选装载数据
  168. if (data.length === 0) {
  169. uni.showToast({
  170. title: '请选择数据',
  171. icon: 'none'
  172. })
  173. return ''
  174. } else {
  175. let Arr_ = []
  176. data.forEach((item, index) => {
  177. let Arr = {
  178. id: '',
  179. name: ''
  180. }
  181. if (item.selected == 1) {
  182. Arr.id = item.id
  183. Arr.name = item.name
  184. Arr_.push(Arr)
  185. }
  186. })
  187. return Arr_
  188. }
  189. }
  190. function setdata_Cake(data) {
  191. var cake = data[0].code
  192. // type等于1则是单选装载数据,否则是多选装载数据
  193. if (data.length === 0) {
  194. uni.showToast({
  195. title: '请选择数据',
  196. icon: 'none'
  197. })
  198. return ''
  199. } else {
  200. let Arr_ = {
  201. facilitator_key: '',
  202. typecode: '',
  203. type: []
  204. }
  205. Arr_.facilitator_key = cake
  206. Arr_.typecode = cake
  207. data.forEach((item, index) => {
  208. let Arr = {
  209. typeid: '',
  210. typename: ''
  211. }
  212. if (item.selected == 1) {
  213. Arr.typeid = item.id
  214. Arr.typename = item.name
  215. Arr_.type.push(Arr)
  216. }
  217. })
  218. return Arr_
  219. }
  220. }
  221. function push_code(data) {
  222. var cake = data[0].code
  223. // type等于1则是单选装载数据,否则是多选装载数据
  224. let Arr_ = {
  225. push_key: '',
  226. typecode: '',
  227. type: []
  228. }
  229. Arr_.push_key = cake
  230. Arr_.typecode = cake
  231. data.forEach((item, index) => {
  232. let Arr = {
  233. typeid: '',
  234. typename: ''
  235. }
  236. if (item.selected == true) {
  237. Arr.typeid = item.id
  238. Arr.typename = item.name
  239. Arr_.type.push(Arr)
  240. }
  241. })
  242. return Arr_
  243. }
  244. function park_code(data) {
  245. var cake = data[0].code
  246. // type等于1则是单选装载数据,否则是多选装载数据
  247. let Arr_ = {
  248. park_key: '',
  249. typecode: '',
  250. type: []
  251. }
  252. Arr_.park_key = cake
  253. Arr_.typecode = cake
  254. data.forEach((item, index) => {
  255. let Arr = {
  256. typeid: '',
  257. typename: ''
  258. }
  259. if (item.selected == true) {
  260. Arr.typeid = item.id
  261. Arr.typename = item.name
  262. Arr_.type.push(Arr)
  263. }
  264. })
  265. return Arr_
  266. }
  267. let jumplist = {
  268. // 集市
  269. countryfair: {
  270. type: 1,
  271. url: '/pages/mine/market/market',
  272. typecode: 'countryfair',
  273. tabcode: 'resourcezhtype'
  274. },
  275. // 资源置换
  276. replacement: {
  277. type: 1,
  278. url: '/pages/index/screendetil/screendetil',
  279. typecode: 'replacement',
  280. tabcode: 'resourcezhtype'
  281. },
  282. // 效果甲方
  283. aeffectparty: {
  284. type: 1,
  285. url: '/pages/index/screendetil/screendetil',
  286. typecode: 'aeffectparty',
  287. tabcode: 'cooperationtype'
  288. },
  289. // 效果乙方
  290. beffectparty: {
  291. type: 1,
  292. url: '/pages/index/screendetil/screendetil',
  293. typecode: 'beffectparty',
  294. tabcode: 'generalization'
  295. },
  296. // 产品寻销
  297. pmarketing: {
  298. type: 1,
  299. url: '/pages/index/screendetil/screendetil_cpxx',
  300. typecode: 'pmarketing',
  301. tabcode: 'cooperationcxxs'
  302. },
  303. // 销货渠道
  304. saleschannel: {
  305. type: 1,
  306. url: '/pages/index/screendetil/screendetil_xhqd',
  307. typecode: 'saleschannel',
  308. tabcode: 'providechannels'
  309. },
  310. // 免费专区
  311. industryepe: {
  312. type: 1,
  313. url: '/pages/index/freezone/freezone',
  314. typecode: 'replacement',
  315. tabcode: 'industryepe'
  316. },
  317. // 活动合作
  318. activityhz: {
  319. type: 1,
  320. url: '/pages/index/screendetil/screendetil',
  321. typecode: 'activityhz',
  322. tabcode: 'activitytype'
  323. },
  324. // 场地资源
  325. siteresources: {
  326. type: 1,
  327. url: '/pages/index/screendetil/screendetil',
  328. typecode: 'siteresources',
  329. tabcode: 'sitetype'
  330. },
  331. // 线下媒体
  332. offlinemedia: {
  333. type: 1,
  334. url: '/pages/index/screendetil/screendetil',
  335. typecode: 'offlinemedia',
  336. tabcode: 'advertisingtypexx'
  337. },
  338. // 采购需求
  339. ptdemand: {
  340. type: 1,
  341. url: '/pages/index/screendetil/screendetil',
  342. typecode: 'ptdemand',
  343. tabcode: 'resourcezhtype'
  344. },
  345. // 企业服务
  346. cpservices: {
  347. type: 1,
  348. url: '/pages/index/screendetil/screendetil',
  349. typecode: 'cpservices',
  350. tabcode: 'facilitatortype'
  351. },
  352. // 招商加盟
  353. itpromotion: {
  354. type: 1,
  355. url: '/pages/index/screendetil/screendetil_zsjm',
  356. typecode: 'itpromotion',
  357. tabcode: 'cooperationtype'
  358. },
  359. // 线上媒体
  360. onlinemedia: {
  361. type: 1,
  362. url: '/pages/index/screendetil/screendetil',
  363. typecode: 'onlinemedia',
  364. tabcode: 'advertisingtypexs'
  365. },
  366. // 项目找代理
  367. projectagent: {
  368. type: 1,
  369. url: '/pages/index/screendetil/screendetil',
  370. typecode: 'projectagent',
  371. tabcode: 'joinagentfee'
  372. },
  373. // 项目找合伙人
  374. pproject: {
  375. type: 1,
  376. url: '/pages/index/screendetil/screendetil',
  377. typecode: 'pproject',
  378. tabcode: 'roletype'
  379. },
  380. // 渠道商
  381. channelbusines: {
  382. type: 2,
  383. url: '/pages/index/merchant/merchant_qds'
  384. },
  385. // 服务商
  386. serviceprovider: {
  387. type: 2,
  388. url: '/pages/index/merchant/merchant_fws'
  389. },
  390. // 品牌主
  391. brandowner: {
  392. type: 2,
  393. url: '/pages/index/merchant/merchant_ppz'
  394. },
  395. // 媒体
  396. media: {
  397. type: 2,
  398. url: '/pages/index/merchant/merchant_mt'
  399. },
  400. // 金牌会员
  401. openmember: {
  402. type: 3,
  403. url: '/pages/usercenter/openmember/openmember'
  404. },
  405. //道具商店
  406. propsmall: {
  407. type: 3,
  408. url: '/pages/usercenter/propsmall/propsmall'
  409. },
  410. // 邀请有礼
  411. sharecenter: {
  412. type: 3,
  413. url: '/pages/usercenter/sharecenter/sharecenter'
  414. },
  415. // 活动
  416. activitylist: {
  417. type: 3,
  418. url: '/pages/usercenter/activitylist/findactivity/findactivity'
  419. }
  420. }
  421. function charinit(bytedesk) {
  422. var appsetting = uni.getStorageSync('appsetting')
  423. if (!appsetting) {
  424. // 获取系统配置
  425. getsetting().then(res => {
  426. if (res.code === 1) {
  427. uni.setStorageSync('appsetting', res.data)
  428. var kf = res.data.set.kf
  429. if (kf.status == 1) {
  430. // 登录后台->渠道管理-》uniapp中创建应用获取
  431. bytedesk.init(kf.subdomain, kf.appKey)
  432. }
  433. }
  434. })
  435. } else {
  436. if (appsetting.set && appsetting.set.kf) {
  437. var kf = appsetting.set.kf
  438. if (kf.status == 1) {
  439. // 登录后台->渠道管理-》uniapp中创建应用获取
  440. bytedesk.init(kf.subdomain, kf.appKey)
  441. }
  442. }
  443. }
  444. }
  445. function toChar() {
  446. var appsetting = uni.getStorageSync('appsetting')
  447. if (!appsetting) {
  448. // 获取系统配置
  449. getsetting().then(res => {
  450. if (res.code === 1) {
  451. uni.setStorageSync('appsetting', res.data)
  452. var kf = res.data.set.kf
  453. console.log(JSON.stringify(kf))
  454. if (kf.status == 1) {
  455. uni.navigateTo({
  456. url: '/components/bytedesk_kefu/chat-kf?wid=' + kf.wid +
  457. '&type=workGroup&aid=&title=在线客服'
  458. })
  459. } else {
  460. uni.showToast({
  461. icon: 'none',
  462. title: '客服暂未开通'
  463. })
  464. }
  465. }
  466. })
  467. } else {
  468. var kf = appsetting.set.kf
  469. if (kf.status == 1) {
  470. uni.navigateTo({
  471. url: '/components/bytedesk_kefu/chat-kf?wid=' + kf.wid + '&type=workGroup&aid=&title=在线客服'
  472. })
  473. console.log(JSON.stringify(kf))
  474. } else {
  475. uni.showToast({
  476. icon: 'none',
  477. title: '客服暂未开通'
  478. })
  479. }
  480. }
  481. }
  482. function onNetworkStatusChange() {
  483. // uni.getNetworkType({
  484. // success: (res) => {
  485. // if (res.networkType == 'none'||res.networkType == 'unknown') {
  486. // uni.navigateTo({
  487. // url: '/pages/index/nonetwork'
  488. // })
  489. // }
  490. // }
  491. // })
  492. }
  493. // 综合跳转方法
  494. function jumphref(jumpitem) {
  495. console.log(JSON.stringify(jumpitem))
  496. if (jumpitem.url != '#' && jumpitem.url != '') {
  497. //跳入内部制定好的页面
  498. if (jumpitem.opentype === 'inside') {
  499. let jumpurl = jumpitem.url
  500. var regPhone = (/pages/)
  501. if (!regPhone.test(jumpurl)) {
  502. let pageitem = jumplist[jumpitem.url]
  503. //tab筛选页面
  504. let params = {
  505. title: jumpitem.modularname,
  506. typecode: pageitem.typecode,
  507. tabcode: pageitem.tabcode
  508. }
  509. jumpurl = pageitem.url + '?params=' + JSON.stringify(params)
  510. }
  511. uni.navigateTo({
  512. url: jumpurl
  513. })
  514. }
  515. //跳入外部链接
  516. else if (jumpitem.opentype === 'web') {
  517. console.log(jumpitem.url)
  518. uni.navigateTo({
  519. url: '/pages/index/webpage?title=' + (jumpitem.modularname || jumpitem.title) + '&url=' +
  520. jumpitem.url
  521. })
  522. }
  523. //跳入文章
  524. else if (jumpitem.opentype === 'article') {
  525. uni.navigateTo({
  526. url: '/pages/release/agreement/agreement?type=3&title=' + jumpitem.modularname + '&id=' +
  527. jumpitem.url
  528. })
  529. }
  530. }
  531. }
  532. function toarticle(title, id = 0, type = 1) {
  533. uni.navigateTo({
  534. url: '/pagesC/pages/index/message/message?id=' + id + '&title=' + title + '&type=' + type
  535. })
  536. }
  537. function EACh(data, res) {
  538. var cake = data[0].code
  539. // type等于1则是单选装载数据,否则是多选装载数据
  540. let Arr_ = {
  541. type: [],
  542. title: ''
  543. }
  544. let name = ''
  545. data.forEach((item, index) => {
  546. res.forEach(d => {
  547. if (item.name == d.name) {
  548. name = d.name + ',' + name
  549. item.selected = 1
  550. }
  551. })
  552. })
  553. Arr_.type = data
  554. Arr_.title = name
  555. return Arr_
  556. }
  557. function u_gopage(key, item) {
  558. // console.log('key', key)
  559. // console.log('u_gopage', JSON.stringify(item))
  560. switch (key) {
  561. case 'U_cube':
  562. case 'U_slideshow':
  563. case 'U_button':
  564. case 'U_monitor':
  565. if (item.is_minApp == 1) {
  566. toUrl(item.h5_link);
  567. } else if (item.is_minApp == 2) {
  568. //跳转小程序
  569. skip(item.appid, item.minApp_link)
  570. }
  571. break;
  572. case 'U_goods':
  573. case 'U_goods2':
  574. toUrl('/pagesD/pages/product/product?id=' + item.id);
  575. break;
  576. case 'U_simplegraph':
  577. if (item.choose_style == 2) {
  578. if (item.value.is_minApp == 1) {
  579. toUrl(item.value.h5_link);
  580. } else if (item.value.is_minApp == 2) {
  581. //跳转小程序
  582. skip(item.value.appid, item.value.minApp_link)
  583. }
  584. } else if (item.choose_style == 3) {
  585. if (item.value.hotArea) {
  586. item = item.value.hotArea[0]
  587. if (item.is_minApp == 1) {
  588. toUrl(item.h5_link);
  589. } else if (item.is_minApp == 2) {
  590. //跳转小程序
  591. skip(item.appid, item.minApp_link)
  592. }
  593. }
  594. }
  595. break;
  596. }
  597. }
  598. function toUrl(_url) {
  599. //处理老版本更新 分包 url 变更
  600. let allurl = {
  601. '/pages/broadcast/broadcast': '/pagesE/pages/broadcast/broadcast',
  602. '/pages/mall/list': '/pagesE/pages/mall/list',
  603. '/pages/mall/group': '/pagesE/pages/mall/group',
  604. '/pages/mall/integral': '/pagesE/pages/mall/integral',
  605. '/pages/user/coupon/coupon': '/pagesE/pages/user/coupon/coupon',
  606. '/pages/index/signin/signin': '/pagesE/pages/index/signin/signin',
  607. '/pages/mynews/mynews': '/pagesE/pages/mynews/mynews',
  608. '/pages/user/problem/problem': '/pagesE/pages/user/problem/problem',
  609. '/pages/cart/cart': '/pagesE/pages/cart/cart',
  610. };
  611. let okeys = Object.keys(allurl)
  612. let tlist = uni.getStorageSync("tabbar")
  613. let flag = false
  614. if (tlist) {
  615. tlist.forEach((item, index) => {
  616. if (flag == false && _url.indexOf(item.url) > -1) {
  617. flag = true
  618. }
  619. })
  620. }
  621. okeys.forEach(function(item) {
  622. if (item == _url) {
  623. _url = allurl[item];
  624. return;
  625. }
  626. })
  627. // console.log('_url2', _url)
  628. // console.log('flag', flag)
  629. if (flag) {
  630. uni.switchTab({
  631. url: _url,
  632. fail: function(e) {
  633. uni.navigateTo({
  634. url: _url
  635. })
  636. }
  637. })
  638. } else {
  639. uni.navigateTo({
  640. url: _url,
  641. fail: function(e) {
  642. uni.switchTab({
  643. url: _url
  644. })
  645. }
  646. })
  647. }
  648. }
  649. //小程序跳转小程序
  650. function skip(appId, path) {
  651. // #ifdef MP-WEIXIN
  652. uni.navigateToMiniProgram({
  653. // appid 写你要跳转的小程序的 appid
  654. appId: appId,
  655. // 路径写 src下的路径,假如你跳转的是pages下的页面,就可以写pages/index
  656. path: path,
  657. extraData: {
  658. // 'type': 'out'
  659. },
  660. // 这个不写的话会显示开发环境,不能正常跳转,写上就能正常跳转了
  661. envVersion: 'develop',
  662. success(res) {
  663. // 打开成功
  664. uni.showToast({
  665. title: '跳转成功'
  666. })
  667. },
  668. fail(err) {
  669. // 打开失败/取消
  670. uni.showToast({
  671. title: '跳转不成功'
  672. })
  673. }
  674. })
  675. // #endif
  676. }
  677. function getselectlist(list1, list2) {
  678. for (var i = 0; i < list1.length; i++) {
  679. var item = list1[i]
  680. if (list2 && list2.length > 0) {
  681. for (var ii = 0; ii < list2.length; ii++) {
  682. var item2 = list2[ii]
  683. if (item.id == item2) {
  684. list1[i].selected = 1
  685. }
  686. }
  687. }
  688. }
  689. return list1
  690. }
  691. function myCache(k, v, t = 3600 * 24) {
  692. let dtime = '_deadtime';
  693. if (v) {
  694. uni.setStorageSync(k, v)
  695. var seconds = parseInt(t);
  696. if (seconds > 0) {
  697. var timestamp = Date.parse(new Date()); // 获取时间戳的毫秒数
  698. timestamp = timestamp / 1000 + seconds; // 时间戳 + 过期时间
  699. uni.setStorageSync(k + dtime, timestamp + "")
  700. } else {
  701. uni.removeStorageSync(k + dtime)
  702. }
  703. } else {
  704. var deadtime = parseInt(uni.getStorageSync(k + dtime)) // 获取缓存的时间
  705. if (deadtime) {
  706. if (parseInt(deadtime) < Date.parse(new Date()) / 1000) { // 如果缓存时间小于 当前时间戳
  707. return null;
  708. }
  709. }
  710. var res = uni.getStorageSync(k); // 否则直接读取真正的缓存名, 并且返回出去
  711. if (res) {
  712. return res;
  713. } else {
  714. return null;
  715. }
  716. }
  717. }
  718. // 位置授权(小程序)
  719. function getAuthorizeInfo() {
  720. uni.authorize({
  721. scope: 'scope.userLocation',
  722. success() { // 允许授权
  723. uni.getLocation({
  724. success: function(res) {
  725. console.log("经纬度", res);
  726. },
  727. })
  728. },
  729. fail() { // 拒绝授权
  730. console.log("你拒绝了授权,无法获得周边信息")
  731. }
  732. })
  733. }
  734. // 获取地理位置
  735. function getLocationInfo(mapkey, fun) {
  736. let amapObject = new amapFile.AMapWX({
  737. key: mapkey
  738. })
  739. amapObject.getRegeo({
  740. iconPath: "../../static/marker.png",
  741. success: (res) => {
  742. console.log(res, res[0].regeocodeData.formatted_address)
  743. // 移动到当前位置
  744. if (fun) {
  745. fun(res)
  746. }
  747. },
  748. fail: (err) => {
  749. console.log(err)
  750. }
  751. })
  752. }
  753. //改变地图中心位置
  754. function toLocation(mapCtx, obj) {
  755. // 改变地图中心位置
  756. console.log('改变地图中心位置', obj)
  757. mapCtx.moveToLocation(obj)
  758. // 移动标记点并添加动画效果
  759. mapCtx.translateMarker({
  760. markerId: 1,
  761. autoRotate: false,
  762. duration: 100,
  763. destination: {
  764. latitude: obj.latitude,
  765. longitude: obj.longitude,
  766. },
  767. animationEnd() {
  768. console.log('animation end')
  769. }
  770. })
  771. }
  772. function chooseLocation(fun) {
  773. uni.chooseLocation({
  774. success: (res) => {
  775. if (fun) {
  776. fun(res);
  777. }
  778. },
  779. fail: () => {
  780. // 如果用uni.chooseLocation没有获取到地理位置,则需要获取当前的授权信息,判断是否有地理授权信息
  781. uni.getSetting({
  782. success: (res) => {
  783. console.log('getSetting', JSON.stringify(res));
  784. var status = res.authSetting;
  785. if (!status['scope.userLocation']) {
  786. // 如果授权信息中没有地理位置的授权,则需要弹窗提示用户需要授权地理信息
  787. uni.showModal({
  788. title: "是否授权当前位置",
  789. content: "需要获取您的地理位置,请确认授权,否则地图功能将无法使用",
  790. success: (tip) => {
  791. if (tip.confirm) {
  792. // 如果用户同意授权地理信息,则打开授权设置页面,判断用户的操作
  793. uni.openSetting({
  794. success: (data) => {
  795. // 如果用户授权了地理信息在,则提示授权成功
  796. if (data
  797. .authSetting[
  798. 'scope.userLocation'
  799. ] === true
  800. ) {
  801. uni.showToast({
  802. title: "授权成功",
  803. icon: "success",
  804. duration: 1000
  805. })
  806. // 授权成功后,然后再次chooseLocation获取信息
  807. uni.chooseLocation({
  808. success: (
  809. res
  810. ) => {
  811. if (
  812. fun
  813. ) {
  814. fun(
  815. res
  816. );
  817. }
  818. }
  819. })
  820. } else {
  821. uni.showToast({
  822. title: "授权失败",
  823. icon: "none",
  824. duration: 1000
  825. })
  826. }
  827. }
  828. })
  829. }
  830. }
  831. })
  832. }
  833. },
  834. fail: (res) => {
  835. console.log('chooseLocationfail', JSON.stringify(res));
  836. uni.showToast({
  837. title: "调用授权窗口失败",
  838. icon: "none",
  839. duration: 1000
  840. })
  841. }
  842. })
  843. }
  844. });
  845. }
  846. function getnewurl() {
  847. let url = document.location.href;
  848. let reg = /[^\w](state|code)=?([^&|^#]*)/g;
  849. url = url.replace(reg, "");
  850. reg = /&&/g;
  851. url = url.replace(reg, "");
  852. reg = /&#/g;
  853. url = url.replace(reg, "#");
  854. reg = /\?#/g;
  855. url = url.replace(reg, "#");
  856. reg = /\?#/g;
  857. url = url.replace(reg, "#");
  858. return url;
  859. }
  860. function requestSubscribeMessage(tids, tmplId, callback) {
  861. // #ifdef MP-WEIXIN
  862. uni.getSetting({
  863. withSubscriptions: true,
  864. success(res) {
  865. var itemSettings = res.subscriptionsSetting.itemSettings ? res.subscriptionsSetting
  866. .itemSettings : "";
  867. var flag = false;
  868. //校验是否
  869. if (tids && tids.length > 0) {
  870. if (itemSettings) {
  871. tids.forEach((item, index) => {
  872. if (itemSettings[item] != 'accept') {
  873. flag = true;
  874. }
  875. });
  876. } else {
  877. flag = true;
  878. }
  879. }
  880. if (flag) {
  881. //未授权唤起授权
  882. uni.requestSubscribeMessage({
  883. tmplIds: tids,
  884. success(res) {},
  885. fail(err) {},
  886. complete() {
  887. callback ? callback() : null;
  888. }
  889. })
  890. } else {
  891. callback ? callback() : null;
  892. }
  893. }
  894. })
  895. // #endif
  896. // #ifndef MP-WEIXIN
  897. callback ? callback() : null;
  898. // #endif
  899. }
  900. export default {
  901. requestSubscribeMessage,
  902. getnewurl,
  903. chooseLocation,
  904. getAuthorizeInfo,
  905. getLocationInfo,
  906. toLocation,
  907. park_code,
  908. myCache,
  909. getselectlist,
  910. EACh,
  911. beforeSwitch,
  912. getsetting,
  913. radiochangedata,
  914. multisedata,
  915. setdata,
  916. setdata_,
  917. setdata_Cake,
  918. push_code,
  919. jumphref,
  920. toChar,
  921. charinit,
  922. pageaddress,
  923. getscan,
  924. toarticle,
  925. h5_wx_auth,
  926. onNetworkStatusChange,
  927. gologin,
  928. postMessage,
  929. toUrl,
  930. u_gopage
  931. }