utils.js 7.7 KB

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