utils.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import $http from '@/config/requestConfig'
  2. import store from '@/store';
  3. import base from '@/config/baseUrl';
  4. import {
  5. getAppWxLatLon
  6. } from '@/plugins/utils';
  7. import {
  8. getStaffStatus
  9. } from '@/config/login';
  10. // #ifdef H5
  11. import {
  12. getLatLonH5,
  13. publicShareFun,
  14. wxPublicPay,
  15. getBrowser,
  16. appMutual
  17. } from '@/config/html5Utils';
  18. // 公众号分享
  19. export const publicShare = publicShareFun;
  20. // #endif
  21. // #ifdef APP-PLUS
  22. import appShareFun, {
  23. closeShare
  24. } from '@/plugins/share';
  25. // APP分享
  26. export const appShare = function(data, callbcak) {
  27. return appShareFun({
  28. shareTitle: data.shareTitle || base.share.title,
  29. shareUrl: data.shareUrl || base.share.link,
  30. shareContent: data.shareContent || base.share.desc,
  31. shareImg: data.shareImg || base.share.imgUrl,
  32. }, callbcak);
  33. };
  34. export const closeAppShare = closeShare;
  35. // #endif
  36. // #ifdef MP-WEIXIN
  37. // 微信小程序分享
  38. export const wxShare = function(data = {}) {
  39. let shareInfo = {
  40. title: data.title || base.share.title,
  41. };
  42. if (data.path && typeof(data.path) == "string") {
  43. shareInfo.path = data.path;
  44. } else if (data.path != 1) {
  45. shareInfo.path = "pages/home/home";
  46. }
  47. if (data.imageUrl) {
  48. shareInfo.imageUrl = data.imageUrl;
  49. }
  50. let userInfo = store.state.userInfo;
  51. if (!(userInfo && userInfo.uid)) {
  52. userInfo = uni.getStorageSync("userInfo");
  53. }
  54. if (userInfo && userInfo.uid) {
  55. if (data.query && typeof(data.query) == "object") {
  56. data.query.recommendCode = userInfo.uid;
  57. } else {
  58. data.query = {
  59. recommendCode: userInfo.uid
  60. };
  61. }
  62. }
  63. if (data.query && typeof(data.query) == "object") {
  64. Object.keys(data.query).forEach((key, index) => {
  65. if (index > 0 && shareInfo.query) {
  66. shareInfo.query += "&" + key + "=" + data.query[key];
  67. shareInfo.path += "&" + key + "=" + data.query[key];
  68. } else {
  69. shareInfo.query = key + "=" + data.query[key];
  70. shareInfo.path += "?" + key + "=" + data.query[key];
  71. }
  72. });
  73. }
  74. return shareInfo;
  75. }
  76. // #endif
  77. //支付(APP微信支付、APP支付宝支付、微信小程序支付)
  78. export const setPay = function(payInfo, callback) {
  79. let httpUrl = "";
  80. if (payInfo.type == 'wxpay') {
  81. // APP微信支付
  82. httpUrl = 'api/pay/v1/pay_sign_wx'
  83. } else if (payInfo.type == 'alipay') {
  84. // APP支付宝支付
  85. httpUrl = 'api/pay/v1/pay_sign_ali'
  86. } else if (payInfo.type == 'smallPay') {
  87. // 微信小程序支付
  88. httpUrl = 'api/pay/v1/small_pay_sign_wx'
  89. }
  90. $http.get(httpUrl, {
  91. orderNo: payInfo.orderNo
  92. }).then(data => {
  93. let payData = {
  94. success: function(res) {
  95. callback && callback({
  96. success: true,
  97. data: res
  98. });
  99. console.log('success:' + JSON.stringify(res));
  100. },
  101. fail: function(err) {
  102. callback && callback({
  103. success: false,
  104. data: err
  105. });
  106. console.log('fail:' + JSON.stringify(err));
  107. }
  108. };
  109. if (payInfo.type == 'smallPay') {
  110. // 小程序
  111. payData.provider = 'wxpay';
  112. payData.timeStamp = data.timeStamp;
  113. payData.nonceStr = data.nonceStr;
  114. payData.package = data.package;
  115. // payData.package = "prepay_id=" + data.prepayid;
  116. payData.signType = "MD5";
  117. payData.paySign = data.sign;
  118. } else if (payInfo.type == 'wxpay') {
  119. // app微信
  120. payData.provider = 'wxpay';
  121. payData.orderInfo = data;
  122. } else if (payInfo.type == 'alipay') {
  123. // app 支付宝
  124. payData.provider = 'alipay';
  125. payData.orderInfo = data;
  126. } else if (payInfo.type == 'baidu') {
  127. payData.provider = 'baidu';
  128. payData.orderInfo = data;
  129. }
  130. console.log("支付参数", payData);
  131. uni.requestPayment(payData);
  132. }, err => {
  133. callback && callback({
  134. success: false,
  135. data: err
  136. });
  137. });
  138. }
  139. // 支付统一分配
  140. export const setPayAssign = function(orderInfo, callback) {
  141. orderInfo.price = orderInfo.price || orderInfo.pricePay;
  142. orderInfo.title = orderInfo.title || orderInfo.orderTitle;
  143. //支付
  144. // #ifdef APP-PLUS
  145. uni.navigateTo({
  146. url: '/pages/home/weChatPay?orderNo=' + orderInfo.orderNo + '&price=' + orderInfo.price +
  147. '&title=' + orderInfo.title
  148. });
  149. // #endif
  150. // #ifdef MP-WEIXIN
  151. setPay({
  152. ...orderInfo,
  153. type: "smallPay"
  154. }, res => {
  155. if (res.success) {
  156. uni.redirectTo({
  157. url: "/pages/shopCar/paySuccess?orderNo=" + orderInfo.orderNo
  158. });
  159. }
  160. });
  161. // #endif
  162. // #ifdef H5
  163. if (getBrowser() === '微信') {
  164. wxPublicPay({
  165. orderNo: orderInfo.orderNo
  166. }, function() {
  167. uni.redirectTo({
  168. url: "/pages/shopCar/paySuccess?orderNo=" + orderInfo.orderNo
  169. });
  170. });
  171. } else {
  172. appMutual('setJumpPay', orderInfo);
  173. }
  174. // #endif
  175. }
  176. // 获取地址信息 (微信小程序、APP、公众号)
  177. export const getLatLon = function(tip) {
  178. return new Promise((resolve, reject) => {
  179. const successProcess = function(res) {
  180. store.commit("setCurrentAddress", {
  181. latitude: res.latitude,
  182. longitude: res.longitude
  183. });
  184. resolve(res);
  185. };
  186. const errProcess = function(err) {
  187. reject(err);
  188. if (tip) {
  189. uni.showToast({
  190. title: err,
  191. icon: "none"
  192. });
  193. }
  194. };
  195. // #ifdef H5
  196. getLatLonH5(successProcess, errProcess);
  197. // #endif
  198. // #ifndef H5
  199. getAppWxLatLon(successProcess, errProcess);
  200. // #endif
  201. });
  202. }
  203. // 页面跳转
  204. export const navigate = async function(options, type = "navigateTo", isRequireLogin = false) {
  205. uni.hideKeyboard();
  206. jumpPage(options, type);
  207. // 是否登录验证
  208. // if (isRequireLogin == true) {
  209. // // 进行判断是否用户已登录,如果未登录,则弹出要求登录框,并return false进行截断
  210. // if (!!store.state.token) {
  211. // if((store.state.userStatus==='1') || (store.state.userStatus==='2') || (store.state.userStatus==='3') || (store.state.userStatus==='4') ){
  212. // await getStaffStatus(store.state.userLoginId).then(res => {
  213. // if(store.state.userStatus==='1'){
  214. // return uni.showModal({
  215. // content: '您的帐号未完成认证,暂无相关权限。',
  216. // cancelText:'关闭',
  217. // confirmText:'去认证',
  218. // success:(res)=> {
  219. // if (res.confirm) {
  220. // uni.navigateTo({ url:"/pages/user/infoInput" })
  221. // }
  222. // }
  223. // });
  224. // }else if((store.state.userStatus==='2') || (store.state.userStatus==='3') || (store.state.userStatus==='4')){
  225. // let title="";
  226. // if(store.state.userStatus==='2'){
  227. // title = '您的认证信息正在审核,请耐心等待。'
  228. // }else if(store.state.userStatus==='3'){
  229. // title = '您的认证信息已被管理人员驳回,无法进行认证,暂无相关权限。如有疑问请联系管理员或推荐人。'
  230. // }else if(store.state.userStatus==='4'){
  231. // title = '您的认证信息已被管理人员退回,请按照要求修改后重新提交。'
  232. // }
  233. // return uni.showModal({
  234. // content: title,
  235. // cancelText:'关闭',
  236. // confirmText:'去查看',
  237. // success:(res)=> {
  238. // if (res.confirm) {
  239. // uni.navigateTo({ url:"/pages/user/infoExamine" })
  240. // }
  241. // }
  242. // });
  243. // }else{
  244. // jumpPage(options,type)
  245. // }
  246. // })
  247. // }else{
  248. // // 跳转
  249. // jumpPage(options,type)
  250. // }
  251. // }else{
  252. // uni.showModal({
  253. // title: "登录提示",
  254. // content: "此时此刻需要您登录喔~",
  255. // confirmText: "去登录",
  256. // cancelText: "再逛会",
  257. // showCancel:false,
  258. // success: (res) => {
  259. // if (res.confirm) {
  260. // return uni.reLaunch({
  261. // url: "/pages/login/login"
  262. // });
  263. // }
  264. // }
  265. // });
  266. // }
  267. // }else{
  268. // // 跳转
  269. // jumpPage(options,type)
  270. // }
  271. }
  272. // 跳转页面
  273. function jumpPage(options, type) {
  274. switch (type) {
  275. case "navigateTo":
  276. uni.navigateTo(options);
  277. break;
  278. case "redirectTo":
  279. uni.redirectTo(options);
  280. break;
  281. case "reLaunch":
  282. uni.reLaunch(options);
  283. break;
  284. case "switchTab":
  285. uni.switchTab(options);
  286. break;
  287. }
  288. }