app.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import store from '@/store'
  2. import * as util from '@/utils/util'
  3. /**
  4. * 打开地图查看位置
  5. * @param {Object} = options
  6. * options.lat 经度
  7. * options.lon 纬度
  8. * options.name 名字
  9. * options.address 地址
  10. */
  11. export const openAddressMap = (options, cb) => {
  12. if (!options.lat || !options.lon) return console.warn('请传入经纬度');
  13. uni.openLocation({
  14. latitude: Number(options.lat),
  15. longitude: Number(options.lon),
  16. name: options.name || '',
  17. address: options.address || '',
  18. complete: res => cb ? cb() : null,
  19. });
  20. }
  21. /**
  22. * 富文本图片格式化
  23. * * @param {string} 路径 content:我是富文本内容
  24. */
  25. export const formatRich = (content) => {
  26. let res = ''
  27. if (content) {
  28. res = content.replace(new RegExp('<img', 'gi'),
  29. "<img style='width:100%;vertical-align: middle;'")
  30. res = res.replace(new RegExp('<p', 'gi'),
  31. "<img style='word-break: break-all;'")
  32. }
  33. return res
  34. }
  35. /**
  36. * 非tabbar页面自定义事件跳转传参(注:刷新丢失)
  37. * @param {boolean} 是否需要登录 isLogin:false
  38. * @param {string} 路径 url:'pages/home/index'
  39. * @param {string} 事件名 eventName:'sendData'
  40. * @param {object} 传参 eventData:{a:1}
  41. */
  42. export const navToPageParams = (isLogin = false, url, eventName = '', eventData = {}) => {
  43. if (isLogin) {
  44. if (checkLogin()) {
  45. uni.navigateTo({
  46. url: `/${url}`,
  47. success: res => {
  48. res.eventChannel.emit(eventName, eventData)
  49. }
  50. })
  51. } else {
  52. navTo('pages/login/index')
  53. }
  54. } else {
  55. uni.navigateTo({
  56. url: `/${url}`,
  57. success: res => {
  58. res.eventChannel.emit(eventName, eventData)
  59. }
  60. })
  61. }
  62. }
  63. /**
  64. * 跳转传参
  65. * @param {boolean} isLogin:true
  66. * @param {string} path:''
  67. * @param {object} params:{}
  68. * @param {Function} cellback:()=>{}
  69. */
  70. export const navToPage = (isLogin = false, path = '', params = {}, cellback) => {
  71. if (isLogin) {
  72. if (checkLogin()) {
  73. if (cellback) cellback
  74. else navTo(path, params)
  75. } else {
  76. navTo('pages/login/index')
  77. }
  78. } else {
  79. if (cellback) cellback
  80. else navTo(path, params)
  81. }
  82. return {
  83. isLogin,
  84. path,
  85. params,
  86. cellback
  87. }
  88. }
  89. /**
  90. * 图片预览,传入current:当前索引和urls图片列表
  91. * @param {Number} current:0
  92. * @param {Array<string>|string} urls:['']|''
  93. */
  94. export const previewImage = async (current = 0, urls) => {
  95. if (typeof urls == 'string') urls = [urls]
  96. uni.previewImage({
  97. current,
  98. urls,
  99. })
  100. }
  101. /**
  102. * 文本复制
  103. * @param {Number} data:'要复制的文本'
  104. */
  105. export const copyText = (data, showToast = true) => {
  106. uni.setClipboardData({
  107. data,
  108. showToast,
  109. });
  110. }
  111. /**
  112. * 电话拨号
  113. * @param {string} phoneNumber:'15011111111'
  114. */
  115. export const cellPhone = (phoneNumber, cb) => {
  116. uni.makePhoneCall({
  117. phoneNumber,
  118. success: (res) => {
  119. if (cb) cb(res)
  120. }
  121. })
  122. }
  123. /**
  124. * 二次确认框
  125. * @param {String} title - "标题"
  126. * @param {String} content - "内容"
  127. * @param {Function} confirm - 确认
  128. * @param {Function} cancel - 取消
  129. */
  130. export const showModel = (options) => {
  131. uni.showModal({
  132. title: options.title || '温馨提示',
  133. content: options.content,
  134. showCancel: options.showCancel || true,
  135. success(res) {
  136. if (res.confirm) {
  137. options.confirm && options.confirm();
  138. }
  139. if (res.cancel) {
  140. options.cancel && options.cancel();
  141. }
  142. }
  143. })
  144. }
  145. /**
  146. * 显示成功提示框
  147. */
  148. export const showSuccess = (msg, callback) => {
  149. uni.showToast({
  150. title: msg,
  151. icon: 'success',
  152. mask: true,
  153. duration: 1500,
  154. success() {
  155. callback && callback()
  156. }
  157. })
  158. }
  159. /**
  160. * 显示失败提示框
  161. */
  162. export const showError = (msg, callback) => {
  163. uni.showModal({
  164. title: '友情提示',
  165. content: msg,
  166. showCancel: false,
  167. success(res) {
  168. callback && callback()
  169. }
  170. })
  171. }
  172. /**
  173. * 显示纯文字提示框
  174. */
  175. export const showToast = (msg, icon = 'none', duration = 1500, mask = true) => {
  176. uni.showToast({
  177. title: msg, // 提示的内容
  178. icon,
  179. mask, // 是否显示透明蒙层,防止触摸穿透
  180. duration // 提示的延迟时间,单位毫秒,默认:1500
  181. })
  182. }
  183. /**
  184. * tabBar页面路径列表 (用于链接跳转时判断)
  185. * tabBarLinks为常量, 无需修改
  186. */
  187. export const getTabBarLinks = () => {
  188. const tabBarLinks = [
  189. 'pages/home/index',
  190. 'pages/camp/index',
  191. 'pages/games/index',
  192. 'pages/user/index',
  193. ]
  194. return tabBarLinks
  195. }
  196. /**
  197. * 生成转发的url参数(string格式)
  198. */
  199. export const getShareUrlParams = (params) => {
  200. return util.urlEncode({
  201. refereeId: store.getters.userId, // 推荐人ID
  202. ...params
  203. })
  204. }
  205. /**
  206. * 跳转到指定页面url
  207. * 支持tabBar页面
  208. * @param {string} url 页面路径
  209. * @param {object} query 页面参数
  210. * @param {string} modo 跳转类型 默认:navigateTo) 可选:redirectTo
  211. */
  212. export const navTo = (url, query = {}, modo = 'navigateTo') => {
  213. if (!url || url.length == 0) {
  214. return false
  215. }
  216. // tabBar页面, 使用switchTab
  217. if (util.inArray(url, getTabBarLinks())) {
  218. uni.switchTab({
  219. url: `/${url}`
  220. })
  221. return true
  222. }
  223. // 生成query参数
  224. const queryStr = query ? '?' + util.urlEncode(query) : ''
  225. // 普通页面, 使用navigateTo
  226. modo === 'navigateTo' && uni.navigateTo({
  227. url: `/${url}${queryStr}`,
  228. })
  229. // 特殊指定, 使用redirectTo
  230. modo === 'redirectTo' && uni.redirectTo({
  231. url: `/${url}${queryStr}`,
  232. })
  233. return true
  234. }
  235. /**
  236. * 退回页面
  237. * @param {integer} page 页面数量
  238. */
  239. export const navBack = (page = 1) => {
  240. uni.navigateBack({
  241. delta: page
  242. });
  243. }
  244. /**
  245. * 验证是否已登录
  246. */
  247. export const checkLogin = () => {
  248. return !!store.getters.userId
  249. }
  250. /**
  251. * 发起支付请求 微信小支付
  252. * @param {Object} 参数
  253. */
  254. export const wxPayment = (option) => {
  255. const options = {
  256. timeStamp: '',
  257. nonceStr: '',
  258. prepay_id: '',
  259. paySign: '',
  260. ...option
  261. }
  262. return new Promise((resolve, reject) => {
  263. uni.requestPayment({
  264. provider: 'wxpay',
  265. timeStamp: options.timeStamp,
  266. nonceStr: options.nonceStr,
  267. 'package': `prepay_id=${options.prepay_id}`,
  268. signType: 'MD5',
  269. paySign: options.paySign,
  270. success: res => resolve(res),
  271. fail: res => reject(res)
  272. })
  273. })
  274. }