common.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // 不含icon提示框
  2. export const toast = str => {
  3. return new Promise((resolve, reject) => {
  4. if (str.length < 20) {
  5. uni.showToast({
  6. title: str,
  7. icon: "none",
  8. duration: 1500,
  9. success: () => {
  10. setTimeout(() => {
  11. resolve
  12. }, 1500)
  13. }
  14. })
  15. } else {
  16. uni.showModal({
  17. title: "提示",
  18. content: String(str),
  19. showCancel: false,
  20. confirmText: "我知道了",
  21. success(res) {
  22. if (res.confirm) {
  23. resolve(res);
  24. } else {
  25. reject();
  26. }
  27. }
  28. });
  29. }
  30. })
  31. };
  32. // 成功提示框
  33. export const successToast = (str) => {
  34. return new Promise((resolve, reject) => {
  35. uni.showToast({
  36. title: str || '请求成功',
  37. icon: 'success',
  38. duration: 1500,
  39. success: () => {
  40. setTimeout(() => {
  41. resolve();
  42. }, 1500);
  43. },
  44. });
  45. });
  46. };
  47. // loading
  48. export const showLoading = () => {
  49. return new Promise((resolve, reject) => {
  50. uni.showLoading({
  51. success: () => {
  52. resolve();
  53. },
  54. });
  55. });
  56. };
  57. // 提示loading
  58. export const tipLoading = (str) => {
  59. return new Promise((resolve, reject) => {
  60. uni.showLoading({
  61. title: str,
  62. success: () => {
  63. resolve();
  64. },
  65. });
  66. });
  67. };
  68. // 隐藏loading
  69. export const hideLoading = () => {
  70. return new Promise((resolve, reject) => {
  71. uni.hideLoading({
  72. success: () => {
  73. resolve();
  74. },
  75. });
  76. });
  77. };
  78. // 模态弹窗
  79. export const modal = (options = {}) => {
  80. if (!options) return;
  81. const {
  82. title,
  83. content,
  84. showCancel,
  85. cancelText,
  86. cancelColor,
  87. confirmText,
  88. confirmColor,
  89. } = Object.assign({}, options.content ? options : {
  90. content: options
  91. });
  92. return new Promise((resolve, reject) => {
  93. uni.showModal({
  94. title: title || '提示',
  95. content: String(content),
  96. showCancel: typeof showCancel == 'boolean' ? showCancel : true,
  97. cancelText: cancelText || '取消',
  98. cancelColor: cancelColor || '#323233',
  99. confirmText: confirmText || '确定',
  100. confirmColor: confirmColor || '#3D8FFD',
  101. complete(res) {
  102. if (res.confirm) {
  103. resolve(res);
  104. } else {
  105. reject();
  106. }
  107. },
  108. });
  109. });
  110. };
  111. /**
  112. * 跳转页面 navigateTo
  113. * 保留当前页面,跳转到应用内的某个页面
  114. * @param {String} url 需要跳转的页面
  115. * @param {Boolean} isAuth 是否需要鉴权
  116. * @returns
  117. */
  118. export const navPage = (url, isAuth = 0) => {
  119. if ((isAuth && store.state.isLogin) || !isAuth) {
  120. uni.navigateTo({
  121. url,
  122. fail: (err) => {
  123. console.log('页面跳转失败', url, err);
  124. },
  125. });
  126. } else {
  127. uni.navigateTo({
  128. url: '/pages/login/index',
  129. });
  130. }
  131. };
  132. /**
  133. * 跳转页面 redirectTo
  134. * 关闭当前页面,跳转到应用内的某个页面
  135. * @param {String} url 需要跳转的页面
  136. * @param {Boolean} isAuth 是否需要鉴权
  137. * @returns
  138. */
  139. export const redPage = (url, isAuth = 0) => {
  140. if ((isAuth && store.state.isLogin) || !isAuth) {
  141. uni.redirectTo({
  142. url,
  143. fail: (err) => {
  144. console.log('页面跳转失败', url, err);
  145. },
  146. });
  147. } else {
  148. uni.navigateTo({
  149. url: '/pages/login/index',
  150. });
  151. }
  152. };
  153. /**
  154. * 返回页面
  155. * @param {Number} num 返回的页面数
  156. * @param {Number} time 延时返回
  157. * @returns
  158. */
  159. export const backPage = (num = 1, time = 0) => {
  160. if (!num) return false;
  161. setTimeout(() => {
  162. uni.navigateBack({
  163. delta: num,
  164. });
  165. }, time);
  166. };
  167. /**
  168. * 拨打电话
  169. * @param {String} phone 用户隐私号
  170. * @param {String} orderId 服务单id,如果需要请求记录接口
  171. */
  172. export const callPhone = (phone) => {
  173. if (!phone)
  174. return modal({
  175. content: '手机号码不存在',
  176. showCancel: false,
  177. })
  178. .then(() => {})
  179. .catch(() => {});
  180. uni.makePhoneCall({
  181. phoneNumber: phone,
  182. success: () => {
  183. // logCallPhone(orderId);
  184. },
  185. });
  186. };
  187. /**
  188. * 复制
  189. * @param {String} val 复制内容
  190. */
  191. export const copy = (val) => {
  192. uni.setClipboardData({
  193. data: val,
  194. success: () => {
  195. successToast('复制成功');
  196. },
  197. });
  198. };
  199. // 获取当前定位
  200. export const getLocation = async function() {
  201. return new Promise((resolve, reject) => {
  202. uni.getLocation({
  203. type: 'gcj02',
  204. geocode: true,
  205. success: (res) => {
  206. resolve(res);
  207. },
  208. fail: (err) => {
  209. console.log('获取当前定位失败', err);
  210. uni.authorize({
  211. scope: 'scope.userLocation',
  212. success: () => {
  213. // 允许授权
  214. uni.getLocation({
  215. type: 'gcj02',
  216. geocode: true,
  217. success: (res) => {
  218. resolve(res);
  219. },
  220. fail: (err) => {
  221. modal({
  222. title: '提示',
  223. content: '定位失败,请重试',
  224. showCancel: false,
  225. }).then(() => {});
  226. },
  227. });
  228. },
  229. fail: () => {
  230. // 拒绝授权
  231. modal({
  232. title: '提示',
  233. content: '定位失败,请重试',
  234. showCancel: false,
  235. // confirmText: '重新定位',
  236. }).then(() => {
  237. // getLocation();
  238. });
  239. },
  240. });
  241. },
  242. });
  243. });
  244. };
  245. // 获取地址
  246. export const getAddress = async function() {
  247. const location = await getLocation();
  248. return new Promise((resolve, reject) => {
  249. uni.request({
  250. url: 'https://restapi.amap.com/v3/geocode/regeo',
  251. method: 'GET',
  252. data: {
  253. location: location.longitude + ',' + location.latitude,
  254. key: 'b772f8b0ace6bc96c04ae8e48f241e36',
  255. },
  256. success: (res) => {
  257. resolve({
  258. longitude: location.longitude,
  259. latitude: location.latitude,
  260. address: res.data.regeocode.formatted_address,
  261. province: res.data.regeocode.addressComponent.province,
  262. city: res.data.regeocode.addressComponent.city,
  263. area: res.data.regeocode.addressComponent.district,
  264. street: res.data.regeocode.addressComponent.township,
  265. });
  266. },
  267. fail: function(err) {
  268. console.log('地址解析失败' + err);
  269. },
  270. });
  271. });
  272. };
  273. // 判断微信环境
  274. export function isWeixin() {
  275. if (navigator && navigator.userAgent) {
  276. var ua = navigator.userAgent.toLowerCase();
  277. if (ua.indexOf('micromessenger') != -1) {
  278. return true;
  279. } else {
  280. return false;
  281. }
  282. } else {
  283. return false
  284. }
  285. }
  286. // 解析地址栏参数
  287. export function getQueryVariable(variable) {
  288. // 从?开始获取后面的所有数据
  289. var query = window.location.search.substring(1);
  290. // 从字符串&开始分隔成数组split
  291. var vars = query.split('&');
  292. // 遍历该数组
  293. for (var i = 0; i < vars.length; i++) {
  294. // 从等号部分分割成字符
  295. var pair = vars[i].split('=');
  296. // 如果第一个元素等于 传进来的参的话 就输出第二个元素
  297. if (pair[0] == variable) {
  298. return pair[1];
  299. }
  300. }
  301. return undefined;
  302. }
  303. export default {
  304. toast,
  305. successToast,
  306. showLoading,
  307. tipLoading,
  308. hideLoading,
  309. modal,
  310. navPage,
  311. redPage,
  312. backPage,
  313. callPhone,
  314. copy,
  315. getLocation,
  316. getAddress,
  317. };