uni_api.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // 对uni中的交互反馈相关api组件进行封装 showToast、showModal
  2. export function uniShowToast(title = '提示语', icon = "none", args = {}) {
  3. const {
  4. image = '',
  5. mask = false,
  6. duration = 1500,
  7. position,
  8. success, fail, complete
  9. } = args;
  10. let toast = {
  11. title,
  12. icon,
  13. duration
  14. };
  15. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  16. if (image) {
  17. toast['image'] = image
  18. }
  19. // #endif
  20. // #ifdef H5 || MP-WEIXIN
  21. if (mask) {
  22. toast['mask'] = mask
  23. }
  24. // #endif
  25. // #ifdef APP-PLUS
  26. if (position) {
  27. toast["position"] = position
  28. }
  29. // #endif
  30. uni.showToast({ ...toast,
  31. ...callBack(toast, {
  32. success,
  33. fail,
  34. complete
  35. })
  36. })
  37. }
  38. export function uniHideToast() {
  39. uni.hideToast()
  40. }
  41. export function uniHideLoading() {
  42. uni.hideLoading()
  43. }
  44. export function successToast(title = '成功', args = {}) {
  45. uniShowToast(title, "success", args)
  46. }
  47. export function loadingToast(title = '加载中', args = {}) {
  48. const {
  49. mask = true,
  50. success, fail, complete
  51. } = args;
  52. let loading = {
  53. title,
  54. };
  55. if (mask) {
  56. loading['mask'] = mask
  57. }
  58. uni.showLoading({
  59. ...loading,
  60. mask: true,
  61. ...callBack(loading, {
  62. success,
  63. fail,
  64. complete
  65. })
  66. });
  67. }
  68. export function uniShowModal(title, content, args = {}) {
  69. const {
  70. showCancel = true, cancelText = "取消", cancelColor, confirmText = "确定", confirmColor, success, fail, complete
  71. } = args;
  72. let modal = {
  73. title,
  74. content,
  75. showCancel,
  76. cancelText,
  77. confirmText
  78. }
  79. // #ifdef H5 || MP-WEIXIN || MP-BAIDU
  80. if (cancelColor) {
  81. modal['cancelColor'] = cancelColor
  82. }
  83. if (confirmColor) {
  84. modal['confirmColor'] = confirmColor
  85. }
  86. // #endif
  87. uni.showModal({ ...modal,
  88. ...callBack(modal, {
  89. success,
  90. fail,
  91. complete
  92. })
  93. })
  94. }
  95. export function uniNavigator(url, type = 'navigateTo', args = {}) {
  96. type = Number(url) ? 'navigateBack' : (type || 'navigateTo');
  97. const {
  98. endtime = Number(url) ? 600 : 200,
  99. animationType,
  100. animationDuration,
  101. success,
  102. fail,
  103. complete
  104. } = args;
  105. let obj = {
  106. url
  107. };
  108. // #ifdef APP-PLUS
  109. if (animationType) {
  110. obj['animationType'] = animationType
  111. }
  112. if (animationDuration) {
  113. obj['animationDuration'] = animationDuration
  114. }
  115. // #endif
  116. obj = callBack(obj, {
  117. success,
  118. fail,
  119. complete
  120. })
  121. console.log(obj)
  122. switch (type) {
  123. case 'navigateTo':
  124. setTimeout(function() {
  125. uni.navigateTo(obj)
  126. }, endtime);
  127. break;
  128. case 'redirectTo':
  129. setTimeout(function() {
  130. uni.redirectTo(obj)
  131. }, endtime);
  132. break;
  133. case 'reLaunch':
  134. setTimeout(function() {
  135. uni.reLaunch(obj)
  136. }, endtime);
  137. break;
  138. case 'switchTab':
  139. setTimeout(function() {
  140. uni.switchTab(obj)
  141. }, endtime);
  142. break;
  143. case 'navigateBack':
  144. setTimeout(function() {
  145. const history = getCurrentPages();
  146. if (history.length > 1) {
  147. uni.navigateBack({
  148. delta: Number(url)
  149. })
  150. } else {
  151. uni.switchTab({
  152. url: '/pages/index/index'
  153. })
  154. }
  155. }, endtime);
  156. break;
  157. }
  158. }
  159. export function uniStartPullDownRefresh(args = {}) {
  160. uni.startPullDownRefresh(args);
  161. }
  162. export function uniStopPullDownRefresh(endtime = 1000) {
  163. setTimeout(function() {
  164. uni.stopPullDownRefresh();
  165. }, endtime);
  166. }
  167. // 获取图片信息
  168. export function uniGetImageInfo(path) {
  169. return new Promise((resolve, reject) => {
  170. uni.getImageInfo({
  171. src: path,
  172. success: function(res) {
  173. console.log(res)
  174. resolve(res)
  175. },
  176. fail: function(err) {
  177. console.log(err)
  178. reject(err)
  179. },
  180. })
  181. })
  182. }
  183. // 获取节点信息
  184. export function uniSelectorQueryInfo(selector, _this) {
  185. return new Promise((resolve, reject) => {
  186. const query = uni.createSelectorQuery().in(_this);
  187. query.select(selector).boundingClientRect(res => {
  188. // 获取节点坐标
  189. resolve(res)
  190. }).exec();
  191. })
  192. }
  193. // 设置导航栏颜色
  194. export function uniSetNavigationBarColor(backgroundColor = "#e93323", frontColor = "#ffffff") {
  195. uni.setNavigationBarColor({
  196. frontColor,
  197. backgroundColor,
  198. animation: {
  199. duration: 400,
  200. timingFunc: 'easeIn'
  201. }
  202. })
  203. }
  204. // 跳转小程序
  205. export function uniNavigateToMiniProgram(appId, args = {}) {
  206. // #ifdef MP
  207. const {
  208. path,
  209. extraData = {
  210. from: '跳转小程序要携带的信息'
  211. },
  212. envVersion,
  213. success,
  214. fail,
  215. complete
  216. } = args;
  217. let obj = {
  218. path,
  219. extraData,
  220. envVersion
  221. }
  222. obj = callBack(obj, {
  223. success,
  224. fail,
  225. complete
  226. })
  227. uni.navigateToMiniProgram({
  228. appId: '',
  229. ...obj
  230. })
  231. // #endif
  232. }
  233. // 返回上一个小程序
  234. export function uniNavigateBackMiniProgram(args = {}) {
  235. // #ifdef MP
  236. const {
  237. extraData = {
  238. from: '返回上一个小程序要携带的信息'
  239. },
  240. success,
  241. fail,
  242. complete
  243. } = args;
  244. let obj = {
  245. extraData
  246. }
  247. obj = callBack(obj, {
  248. success,
  249. fail,
  250. complete
  251. })
  252. uni.navigateBackMiniProgram({
  253. ...obj
  254. })
  255. // #endif
  256. }
  257. function callBack(name = {}, options = {}) {
  258. const {
  259. success,
  260. fail,
  261. complete
  262. } = options;
  263. if (success) {
  264. name['success'] = (res) => {
  265. success(res)
  266. }
  267. }
  268. if (fail) {
  269. name['fail'] = (err) => {
  270. fail(err)
  271. }
  272. }
  273. if (complete) {
  274. name['complete'] = (res) => {
  275. complete(res)
  276. }
  277. }
  278. return name
  279. }