app.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import store from '../store'
  2. import * as util from './util'
  3. import { paginate } from '@/common/constant'
  4. import * as MessageApi from '@/api/message'
  5. /**
  6. * 获取当前运行的终端(App H5 小程序)
  7. * https://uniapp.dcloud.io/platform
  8. */
  9. export const getPlatform = () => {
  10. // #ifdef APP-PLUS
  11. const platform = 'App'
  12. // #endif
  13. // #ifdef APP-PLUS-NVUE
  14. const platform = 'App'
  15. // #endif
  16. // #ifdef H5
  17. const platform = 'H5'
  18. // #endif
  19. // #ifdef MP-WEIXIN
  20. const platform = 'MP-WEIXIN'
  21. // #endif
  22. // #ifdef MP-ALIPAY
  23. const platform = 'MP-ALIPAY'
  24. // #endif
  25. // #ifdef MP-BAIDU
  26. const platform = 'MP-BAIDU'
  27. // #endif
  28. // #ifdef MP-TOUTIAO
  29. const platform = 'MP-TOUTIAO'
  30. // #endif
  31. // #ifdef MP-QQ
  32. const platform = 'MP-QQ'
  33. // #endif
  34. // #ifdef MP-360
  35. const platform = 'MP-360'
  36. // #endif
  37. return platform;
  38. }
  39. /**
  40. * 显示成功提示框
  41. */
  42. export const showSuccess = (msg, callback) => {
  43. uni.showToast({
  44. title: msg,
  45. icon: 'success',
  46. mask: true,
  47. duration: 1500,
  48. success() {
  49. callback && callback()
  50. }
  51. })
  52. }
  53. /**
  54. * 显示失败提示框
  55. */
  56. export const showError = (msg, callback) => {
  57. uni.showModal({
  58. title: '友情提示',
  59. content: msg,
  60. showCancel: false,
  61. success(res) {
  62. callback && callback()
  63. }
  64. })
  65. }
  66. /**
  67. * 显示纯文字提示框
  68. */
  69. export const showToast = msg => {
  70. uni.showToast({
  71. title: msg,
  72. icon: 'none'
  73. })
  74. }
  75. /**
  76. * tabBar页面路径列表 (用于链接跳转时判断)
  77. * tabBarLinks为常量, 无需修改
  78. */
  79. export const getTabBarLinks = () => {
  80. const tabBarLinks = [
  81. 'pages/index/index',
  82. 'pages/category/index',
  83. 'pages/order/index',
  84. 'pages/user/index'
  85. ]
  86. return tabBarLinks
  87. }
  88. /**
  89. * 生成转发的url参数
  90. */
  91. export const getShareUrlParams = (params) => {
  92. return util.urlEncode({
  93. refereeId: store.getters.userId, // 推荐人ID
  94. ...params
  95. })
  96. }
  97. /**
  98. * 跳转到指定页面url
  99. * 支持tabBar页面
  100. * @param {string} url
  101. * @param {object} query
  102. */
  103. export const navTo = (url, query = {}) => {
  104. if (!url || url.length == 0) {
  105. return false
  106. }
  107. // tabBar页面, 使用switchTab
  108. if (util.inArray(url, getTabBarLinks())) {
  109. uni.switchTab({
  110. url: `/${url}`
  111. })
  112. return true
  113. }
  114. // 生成query参数
  115. const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
  116. // 普通页面, 使用navigateTo
  117. uni.navigateTo({
  118. url: `/${url}${queryStr}`
  119. })
  120. return true
  121. }
  122. /**
  123. * 记录购物车商品总数量
  124. * @param {*} value
  125. */
  126. export const setCartTotalNum = (value) => {
  127. uni.setStorageSync('cartTotalNum', Number(value))
  128. }
  129. /**
  130. * 设置购物车tabbar的角标
  131. * 该方法只能在tabbar页面中调用, 其他页面调用会报错
  132. */
  133. export const setCartTabBadge = () => {
  134. const cartTabbarIndex = 1
  135. const cartTotal = uni.getStorageSync('cartTotalNum') || 0;
  136. if (cartTotal > 0) {
  137. uni.setTabBarBadge({
  138. index: cartTabbarIndex,
  139. text: `${cartTotal}`
  140. })
  141. } else {
  142. uni.removeTabBarBadge({
  143. index: cartTabbarIndex
  144. })
  145. }
  146. return
  147. }
  148. /**
  149. * 验证是否已登录
  150. */
  151. export const checkLogin = () => {
  152. return !!store.getters.userId;
  153. }
  154. /**
  155. * 需要登录后操作
  156. */
  157. export const needLogin = () => {
  158. const isLogin = checkLogin();
  159. if (!isLogin) {
  160. uni.showModal({
  161. title: '温馨提示',
  162. content: '此时此刻需要您登录喔~',
  163. confirmText: "去登录",
  164. cancelText: "再逛会",
  165. success: res => {
  166. if (res.confirm) {
  167. uni.navigateTo({
  168. url: "/pages/login/index"
  169. })
  170. }
  171. }
  172. })
  173. }
  174. }
  175. /**
  176. * 发起支付请求
  177. * @param {Object} 参数
  178. */
  179. export const wxPayment = (option) => {
  180. const options = {
  181. timeStamp: '',
  182. nonceStr: '',
  183. prepay_id: '',
  184. paySign: '',
  185. ...option
  186. }
  187. // 微信内浏览器支付
  188. if (isWechat()) {
  189. return new Promise((resolve, reject) => {
  190. wxH5Payment(options,
  191. function(res) {
  192. resolve(res);
  193. },
  194. function(err) {
  195. reject(err);
  196. });
  197. })
  198. }
  199. // H5支付
  200. if (getPlatform() == 'H5' && option.mweb_url) {
  201. h5Pay(option.mweb_url, option.backUrl);
  202. return true;
  203. }
  204. // 微信小程序支付
  205. return new Promise((resolve, reject) => {
  206. uni.requestPayment({
  207. provider: 'wxpay',
  208. timeStamp: options.timeStamp,
  209. nonceStr: options.nonceStr,
  210. 'package': options.package,
  211. signType: 'MD5',
  212. paySign: options.paySign,
  213. success: res => resolve(res),
  214. fail: res => reject(res)
  215. })
  216. })
  217. }
  218. /**
  219. * 加载更多列表数据
  220. * @param {Object} resList 新列表数据
  221. * @param {Object} oldList 旧列表数据
  222. * @param {int} pageNo 当前页码
  223. */
  224. export const getEmptyPaginateObj = () => {
  225. return util.cloneObj(paginate)
  226. }
  227. /**
  228. * 加载更多列表数据
  229. * @param {Object} resList 新列表数据
  230. * @param {Object} oldList 旧列表数据
  231. * @param {int} pageNo 当前页码
  232. */
  233. export const getMoreListData = (resList, oldList, pageNo) => {
  234. // 如果是第一页需手动制空列表
  235. if (pageNo == 1) oldList.content = [];
  236. // 合并新数据
  237. return oldList.content.concat(resList.content);
  238. }
  239. /**
  240. * 弹框消息
  241. * @param {Object} 参数
  242. */
  243. export const showMessage = () => {
  244. const app = this
  245. if (!store.getters.userId) {
  246. return false
  247. }
  248. return new Promise((resolve, reject) => {
  249. MessageApi.getOne()
  250. .then(result => {
  251. if (result.data.content) {
  252. uni.showModal({
  253. title: result.data.title ? result.data.title : '友情提示',
  254. content: result.data.content,
  255. showCancel: false,
  256. success(res) {
  257. if (res.confirm) {
  258. // 将消息置为已读
  259. MessageApi.readed({'msgId' : result.data.msgId})
  260. .then(result => {
  261. //empty
  262. })
  263. }
  264. }
  265. })
  266. }
  267. })
  268. })
  269. }
  270. export const isWechat = () => {
  271. try {
  272. const ua = window.navigator.userAgent.toLowerCase();
  273. if (ua.match(/micromessenger/i) == 'micromessenger') {
  274. return true;
  275. } else {
  276. return false;
  277. }
  278. } catch (e) {
  279. return false;
  280. }
  281. }
  282. export const wxH5Payment = (data, callback_succ_func, callback_error_func) => {
  283. if (!isWechat()) {
  284. return false;
  285. }
  286. if (typeof WeixinJSBridge == "undefined") {
  287. if (document.addEventListener) {
  288. document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
  289. } else if (document.attachEvent) {
  290. document.attachEvent('WeixinJSBridgeReady', jsApiCall);
  291. document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
  292. }
  293. } else {
  294. jsApiCall(data, callback_succ_func, callback_error_func);
  295. }
  296. }
  297. export const jsApiCall = (data, callback_succ_func, callback_error_func) => {
  298. //使用原生的,避免初始化appid问题
  299. WeixinJSBridge.invoke('getBrandWCPayRequest', {
  300. appId:data['appId'],
  301. timeStamp: data['timeStamp'],
  302. nonceStr: data['nonceStr'], // 支付签名随机串,不长于 32 位
  303. package: data['package'], // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  304. signType: data['signType'], // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  305. paySign: data['paySign'], // 支付签名
  306. },
  307. function(res) {
  308. var msg = res.err_msg ?res.err_msg :res.errMsg;
  309. //WeixinJSBridge.log(msg);
  310. switch (msg) {
  311. case 'get_brand_wcpay_request:ok': //支付成功时
  312. if(callback_succ_func){
  313. callback_succ_func(res);
  314. }
  315. break;
  316. default: //支付失败时
  317. WeixinJSBridge.log('支付失败!'+msg+',请返回重试.');
  318. if(callback_error_func){
  319. callback_error_func({msg:msg});
  320. }
  321. break;
  322. }
  323. })
  324. }
  325. export const h5Pay = (url, backUrl) => {
  326. // 设置回跳地址,支付完成之后回跳到哪
  327. let redirectUrl ='&redirect_url=' + encodeURIComponent(backUrl);
  328. // 拼接上回跳地址
  329. let linkUrl = url + redirectUrl
  330. const system = uni.getSystemInfoSync()
  331. if (system.platform == 'ios') {
  332. // 如果是iOS平台,使用location.href,iOS里面限制了window.open的使用。
  333. window.location.href = linkUrl;
  334. } else {
  335. window.open(linkUrl);
  336. }
  337. }