initialize - 副本.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. import { numCapture } from "@/util/Filters.ts";
  2. import keys from "./keys.js";
  3. import config from '@/config/global.config.js'
  4. import Vue from 'vue'
  5. const defaultAvatar = () => {
  6. return config.staticUrl + '/czd/default-avatar.jpg'
  7. // return config.staticUrl + '/head-on.png'
  8. }
  9. const pathHint = () => {
  10. uni.showToast({
  11. icon: 'none',
  12. title: '敬请期待'
  13. })
  14. }
  15. // const skipApps = (path) => {
  16. // if (path === "#") {
  17. // pathHint()
  18. // return false
  19. // }
  20. // uni.navigateTo({
  21. // url: `/pages/web-view/Apps?path=${encodeURIComponent(path)}`
  22. // });
  23. // }
  24. const skipAggregation = (themeId) => {
  25. if (themeId) {
  26. try {
  27. } catch (err) {
  28. console.log('未配置页面')
  29. }
  30. } else {
  31. uni.showToast({
  32. icon: 'none',
  33. title: '聚合页面配置错误'
  34. })
  35. }
  36. }
  37. const skipWeb = (path, title) => {
  38. if (path === "#") {
  39. pathHint()
  40. return false
  41. }
  42. uni.navigateTo({
  43. url: `/pages/web-view/Apps?path=${encodeURIComponent(path)}&title=${title || ''}`,
  44. complete: () => {
  45. oldApps = null;
  46. }
  47. });
  48. }
  49. const base64Encode = (input) => {
  50. const utf8 = unescape(encodeURIComponent(input));
  51. const base64 = btoa(utf8);
  52. return base64;
  53. }
  54. const toAuthPage = (val, appId) => {
  55. }
  56. const InternalPage = {
  57. }
  58. const tabbarPage = []
  59. const antiShake = true; // 是否开启防抖
  60. let oldApps = null;
  61. let openApp = null;
  62. const getAppLocation = (id, name, appId) => {
  63. openApp = id
  64. }
  65. // 定位授权
  66. const getIsAuthLocation = async () => {
  67. return new Promise((resolve, reject) => {
  68. // 1、判断手机定位服务【GPS】 是否授权
  69. uni.getSystemInfo({
  70. success(res) {
  71. // console.log("判断手机定位服务是否授权:", res);
  72. let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
  73. let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
  74. // console.log("判断手机定位服务是否开启",locationEnabled);
  75. // console.log("判断定位服务是否允许微信授权",locationAuthorized);
  76. if (locationEnabled == false || locationAuthorized == false) {
  77. //手机定位服务(GPS)未授权
  78. uni.showToast({
  79. title: '请打开手机GPS',
  80. icon: 'none',
  81. });
  82. resolve(false);
  83. } else {
  84. //手机定位服务(GPS)已授权
  85. // 2、判断微信小程序是否授权位置信息
  86. // 微信小程序已授权位置信息
  87. uni.authorize({
  88. //授权请求窗口
  89. scope: 'scope.userLocation', //授权的类型
  90. success: async (res) => {
  91. resolve(await fnGetlocation());
  92. },
  93. fail: (err) => {
  94. err = err['errMsg'];
  95. uni.showModal({
  96. content: '需要授权位置信息',
  97. confirmText: '确认授权',
  98. success: (obj) => {
  99. if (obj.confirm) {
  100. uni.openSetting({
  101. success: async (set) => {
  102. if (set.authSetting['scope.userLocation']) {
  103. // 授权成功
  104. uni.showToast({
  105. title: '授权成功',
  106. icon: 'none',
  107. });
  108. resolve(await fnGetlocation());
  109. } else {
  110. // 未授权
  111. uni.showToast({
  112. title: '授权失败,请重新授权',
  113. icon: 'none',
  114. });
  115. uni.showModal({
  116. title: '授权',
  117. content: '获取授权' + '失败,是否前往授权设置?',
  118. success: function(result) {
  119. if (result.confirm) {
  120. uni.openSetting();
  121. }
  122. },
  123. fail: function() {
  124. uni.showToast({
  125. title: '系统错误!',
  126. icon: 'none',
  127. });
  128. },
  129. });
  130. }
  131. },
  132. });
  133. } else {
  134. resolve(false);
  135. // 取消授权
  136. uni.showToast({
  137. title: '你拒绝了授权,无法获得周边信息',
  138. icon: 'none',
  139. });
  140. }
  141. }
  142. })
  143. },
  144. });
  145. }
  146. },
  147. });
  148. });
  149. };
  150. // 定位获取
  151. const fnGetlocation = async () => {
  152. return new Promise((resolve, reject) => {
  153. let that = this;
  154. let bindList = {
  155. long: '',
  156. lat: '',
  157. longlat: '',
  158. isLocated: false,
  159. };
  160. uni.getLocation({
  161. type: 'wgs84', //默认为 wgs84 返回 gps 坐标
  162. geocode: 'true',
  163. isHighAccuracy: 'true',
  164. accuracy: 'best', // 精度值为20m
  165. success: function(res) {
  166. let platform = uni.getSystemInfoSync().platform;
  167. if (platform == 'ios') {
  168. //toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。
  169. bindList.long = res.longitude.toFixed(6);
  170. bindList.lat = res.latitude.toFixed(6);
  171. } else {
  172. bindList.long = res.longitude;
  173. bindList.lat = res.latitude;
  174. }
  175. resolve(bindList);
  176. },
  177. fail(err) {
  178. if (
  179. err.errMsg ===
  180. 'getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化'
  181. ) {
  182. uni.showToast({
  183. title: '请勿频繁定位',
  184. icon: 'none',
  185. });
  186. }
  187. if (err.errMsg === 'getLocation:fail auth deny') {
  188. // 未授权
  189. uni.showToast({
  190. title: '无法定位,请重新获取位置信息',
  191. icon: 'none',
  192. });
  193. authDenyCb && authDenyCb();
  194. bindList.isLocated = false;
  195. resolve(bindList);
  196. }
  197. if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
  198. uni.showModal({
  199. content: '请开启手机定位服务',
  200. showCancel: false,
  201. });
  202. }
  203. },
  204. });
  205. });
  206. };
  207. export default {
  208. install(Vue) {
  209. Vue.prototype.$keys = keys;
  210. Vue.prototype.$skeletonTime = 500; // 骨架屏幕过度事件
  211. Vue.prototype.$defaultHadPhoto = config.defaultHadPhoto;
  212. Vue.prototype.$defaultAvatar = defaultAvatar;
  213. Vue.prototype.$getIsAuthLocation = getIsAuthLocation;
  214. Object.defineProperty(Vue.prototype, '$skipWeb', {
  215. value: (path, title) => {
  216. skipWeb(path, title)
  217. }
  218. });
  219. /**
  220. * 图片连接封装, 地址拼接
  221. *
  222. */
  223. Object.defineProperty(Vue.prototype, '$getImgPath', {
  224. value: (path = '') => {
  225. let img = ""
  226. if (path.indexOf('http://') !== 0 && path.indexOf('https://') !== 0) {
  227. if (path.indexOf('/') !== 0) {
  228. img = config.staticUrl + '/' + path
  229. } else {
  230. img = config.staticUrl + path
  231. }
  232. }
  233. return img || path
  234. // return path
  235. }
  236. // value: (path = '', str = 'http') => {
  237. // return filePathHandle(path, str)
  238. // }
  239. })
  240. Object.defineProperty(Vue.prototype, '$onDesensitization', {
  241. value: (type, text) => {
  242. let val = '';
  243. if (text) {
  244. switch (type) {
  245. case 'name':
  246. let arr = text.split("")
  247. arr = arr.map((v, i) => {
  248. return i > 0 ? '*' : v
  249. })
  250. val = `${arr.join("")}`;
  251. // val = `**${text.substring(text.length - 1)}`;
  252. break;
  253. case 'tel':
  254. const tel = '' + text;
  255. val = tel.substring(0, 3) + '****' + tel.substring(tel.length - 4);
  256. break;
  257. }
  258. }
  259. return val
  260. }
  261. })
  262. Object.defineProperty(Vue.prototype, '$editorFile', {
  263. value: (content = '') => {
  264. // const regex = /src="([^"]*)"/g;
  265. const regex = /<image\s+[^>]*src="([^"]*)"/g;
  266. const updatedText = content.replace(regex, `<image src="${config.staticUrl}$1"`);
  267. return updatedText
  268. }
  269. })
  270. /**
  271. * 校验是否已登录
  272. * redirect : 是否需要重定向,true时直接跳转登录页面,false 则不跳转,通过Promise.reject返回错误,页面自行处理,
  273. * 默认为true
  274. */
  275. Object.defineProperty(Vue.prototype, '$VerifyLogin', {
  276. value: (redirect = true) => {
  277. return Promise.resolve()
  278. // return Promise.reject(345)
  279. }
  280. });
  281. // export const ApiCallback = async (Api, params) => {
  282. // const {
  283. // path,
  284. // method
  285. // } = Api;
  286. // let data = null
  287. // switch (method) {
  288. // case 'get':
  289. // data = await http.get(path, params);
  290. // break;
  291. // case 'post':
  292. // data = await http.post(path, params);
  293. // break;
  294. // }
  295. // if (data && data.code === 200) {
  296. // return Promise.resolve(data)
  297. // } else {
  298. // return Promise.reject(2)
  299. // }
  300. // }
  301. /**
  302. * 消息跳转
  303. * type
  304. * applicationId 第三方应用 appid
  305. * linkAddress 第三方应用地址
  306. */
  307. Object.defineProperty(Vue.prototype, '$openMessage', {
  308. value: (messageInfo) => {
  309. if (messageInfo.type == 1) {
  310. getMessageAppLocation(messageInfo.applicationId, messageInfo.linkAddress)
  311. }
  312. }
  313. });
  314. /**
  315. * 唤醒小程序
  316. * 人脸识别
  317. * typeValue 0 纯唤醒小程序 1 授权登录 2 人脸识别
  318. */
  319. Object.defineProperty(Vue.prototype, '$openWXminiprogram', {
  320. value: (data = {}, typeValue = 0, auth = false) => {
  321. return new Promise((resolve, reject) => {
  322. plus.share.getServices(result => {
  323. let sweixin = '';
  324. for (var i = 0; i < result.length; i++) {
  325. let t = result[i];
  326. if (t.id == 'weixin') {
  327. sweixin = t;
  328. }
  329. }
  330. if (sweixin) {
  331. let id = ''
  332. let path = ''
  333. let type = 0
  334. switch (typeValue) {
  335. case 0:
  336. id = data.appletOriginalId
  337. path = data.address
  338. break;
  339. case 1:
  340. id = 'gh_9b86ebba08b0'
  341. type = 0
  342. path = `/pages/authLogin/authLogin?appName=宜格服务`
  343. break;
  344. case 2:
  345. id = 'gh_9b86ebba08b0'
  346. type = 0
  347. path =
  348. `/pages/identify/face_indentify?name=${data.realName}&idCardNumber=${data.identityCard}&channel=app`
  349. break;
  350. }
  351. sweixin.launchMiniProgram({
  352. // id:"gh_9b86ebba08b0", // 要跳转小程序的原始ID
  353. // type: 2, // 微信小程序版本类型可取值: 0-正式版; 1-测试版; 2-体验版。 默认值为0。
  354. // path: `pages/identify/face_indentify?name=${data.realName}&idCardNumber=${data.identityCard}&channel=app`
  355. id: id,
  356. type: type,
  357. path: path
  358. },
  359. // 目标小程序点击返回App后执行的回调,在此接收微信小程序传递的参数
  360. res => {
  361. let resData = JSON.parse(res)
  362. if (typeValue == 1) {
  363. resolve(resData)
  364. } else if (typeValue == 2) {
  365. if (auth) {
  366. resolve(resData)
  367. } else {
  368. facRedirectHandle(data, resData)
  369. }
  370. }
  371. },
  372. err => {
  373. console.log(err)
  374. uni.showToast({
  375. icon: 'none',
  376. title: '当前环境不支持微信操作!'
  377. })
  378. }
  379. );
  380. } else {
  381. // 没有获取到微信分享服务
  382. uni.showToast({
  383. icon: 'none',
  384. title: '当前环境不支持微信操作!'
  385. })
  386. }
  387. }, error => {
  388. // 获取分享服务列表失败
  389. console.log(error)
  390. })
  391. })
  392. }
  393. });
  394. /**
  395. * 任务状态说明
  396. * checkStatus 审状态(0未提交,1待审核,2驳回,3审核通过)
  397. */
  398. Object.defineProperty(Vue.prototype, '$TaskStatusText', {
  399. value: (status) => {
  400. let text = "";
  401. if (status >= 0) {
  402. switch (status) {
  403. case 0:
  404. text = "未提交";
  405. break;
  406. case 1:
  407. text = "待审核";
  408. break;
  409. case 2:
  410. text = "驳回";
  411. break;
  412. case 3:
  413. text = "审核通过";
  414. break;
  415. }
  416. }
  417. return text
  418. }
  419. })
  420. // 判断图片是否 大于 20kb
  421. Object.defineProperty(Vue.prototype, '$getImageSize', {
  422. value: (url) => {
  423. return new Promise((resolve, reject) => {
  424. uni.downloadFile({
  425. url: url, //仅为示例,并非真实的资源
  426. success: (res) => {
  427. if (res.statusCode === 200) {
  428. console.log('下载成功==>', res);
  429. const tempFilePath = res.tempFilePath
  430. uni.getFileInfo({
  431. filePath: tempFilePath,
  432. success: (resFile) => {
  433. console.log('获取文件信息成功==>', resFile)
  434. const sizeKB = resFile && resFile
  435. .size / 1024 || 0
  436. console.log('sizeKB==>', sizeKB)
  437. if (sizeKB > 20) {
  438. resolve(true)
  439. } else {
  440. resolve(false)
  441. }
  442. },
  443. fail: (fail) => {
  444. resolve(false)
  445. }
  446. })
  447. }
  448. },
  449. fail: (fail) => {
  450. resolve(false)
  451. }
  452. })
  453. })
  454. }
  455. })
  456. }
  457. }