Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwei1994 committed May 6, 2020
1 parent 9e40619 commit 44b719a
Show file tree
Hide file tree
Showing 36 changed files with 1,016 additions and 690 deletions.
91 changes: 65 additions & 26 deletions config/html5Utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import base from '@/config/baseUrl';
import store from '@/config/store';
import $http from '@/config/requestConfig'
import { publicShare } from '@/config/share.js';
import {
modifyJson
} from '@/utils/utils';
import { getLocation, setShare } from '@/plugins/wxJsSDK';
/**
* 安卓IOS交互
*/
export const appMutual = (name, query = null, errCallback) => {
if (/android/i.test(navigator.userAgent)) {
if (window.shangChengView) {
if (typeof (query) == "object") {
if (typeof(query) == "object") {
query = JSON.stringify(query);
}
window.shangChengView[name](query);
Expand Down Expand Up @@ -54,8 +51,9 @@ export const weiXinPay = (data, callback) => {
signType: data.signType,
paySign: data.sign
};

function onBridgeReady() {
window.WeixinJSBridge.invoke("getBrandWCPayRequest", wxConfigObj, function (
window.WeixinJSBridge.invoke("getBrandWCPayRequest", wxConfigObj, function(
res
) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
Expand All @@ -64,11 +62,11 @@ export const weiXinPay = (data, callback) => {
if (res.err_msg == "get_brand_wcpay_request:cancel") {
// common.loadWarn('支付遇到问题,您取消了支付');
} else
if (res.err_msg == "get_brand_wcpay_request:fail") {
// common.myConfirm('支付遇到问题,您可能需要重新登录', '', function () {
// obj.wxLoginOAuth();
// });
}
if (res.err_msg == "get_brand_wcpay_request:fail") {
// common.myConfirm('支付遇到问题,您可能需要重新登录', '', function () {
// obj.wxLoginOAuth();
// });
}
});
}
if (typeof window.WeixinJSBridge == "undefined") {
Expand All @@ -90,6 +88,46 @@ export const getBrowser = () => {
}
return "其他";
};
// 获取地址信息
export const getLatLonH5 = function(successCallback, errCallback) {
if (getBrowser() == '微信') {
getLocation().then(res => {
successCallback(res);
}, err => {
console.log("位置信息错误", err);
errCallback("位置信息获取失败");
});
} else {
let clearTime = setTimeout(() => {
errCallback("获取经纬度超时");
}, 5000);
window.getAppLatLon = function(res) {
clearTimeout(clearTime);
successCallback(res);
}
appMutual("getAppLatLon", true);
}
};
// 公众号分享
export const publicShare = function (info = {}) {
if (getBrowser() == "微信") {
let shareInfo = {
title: info.shareTitle || info.title || base.share.title,
desc: info.desc || info.shareContent || base.share.desc,
imgUrl: info.imgUrl || info.shareImg || base.share.imgUrl,
link: info.link || info.shareUrl || base.share.link,
};
if (store.state.userInfo.token) {
if (shareInfo.link.indexOf("?") >= 0) {
shareInfo.link += "&recommendCode=" + store.state.userInfo.uid;
} else {
shareInfo.link += "?recommendCode=" + store.state.userInfo.uid;
}
}
return setShare(shareInfo);
}
}

//获取code
function getLogin(type) {
let urlNow = encodeURIComponent(window.location.href);
Expand All @@ -102,21 +140,21 @@ function getLogin(type) {

function getApiLogin(result, type, callback) {
$http.post("api/open/v1/login", {
wxPublicOpenId: result.openId,
unionid: result.unionid,
nickname: result.nickname,
headImg: result.headImg
})
wxPublicOpenId: result.openId,
unionid: result.unionid,
nickname: result.nickname,
headImg: result.headImg
})
.then(res => {
if (res.thirdLoginSuccess) {
store.commit('setUserInfo', modifyJson(res, result));
store.commit('setUserInfo', res);
callback && callback();
uni.showToast({
title: "欢迎回来",
icon: "none"
});
} else {
store.commit('setUserInfo', modifyJson(res, result));
store.commit('setUserInfo', res);
if (type == "judge") {
uni.showModal({
title: "提示",
Expand All @@ -141,6 +179,7 @@ function getApiLogin(result, type, callback) {
}
//判断是否登录,登录处理
let isGetOpenId = true;

function getRecommendCode() {
var url = window.location.href;
let codeIndex = url.indexOf("recommendCode=");
Expand Down Expand Up @@ -168,11 +207,11 @@ export const h5Login = function(type = "judge", callback) {
}
if (getBrowser() == "微信") {
if (store.state.userInfo.thirdLoginSuccess === false) {
getApiLogin(store.state.userInfo, type,() => {
getApiLogin(store.state.userInfo, type, () => {
callback && callback();
});
} else if (getRequest.code) {
if(isGetOpenId){
if (isGetOpenId) {
isGetOpenId = false;
let httpData = {
code: getRequest.code
Expand All @@ -194,7 +233,7 @@ export const h5Login = function(type = "judge", callback) {
title: "欢迎回来",
icon: "none"
});
},() => {
}, () => {
isGetOpenId = true;
});
}
Expand All @@ -216,14 +255,14 @@ export const h5Login = function(type = "judge", callback) {
uni.navigateTo({
url: "/pages/user/login"
});
}else{
} else {
uni.showModal({
title:"提示",
content:"您还未登录,请先登录~",
title: "提示",
content: "您还未登录,请先登录~",
confirmText: "去登录",
cancelText: "再逛会",
success: (res) => {
if(res.confirm){
if (res.confirm) {
uni.navigateTo({
url: "/pages/user/login"
});
Expand All @@ -234,4 +273,4 @@ export const h5Login = function(type = "judge", callback) {
});
}
}
}
}
54 changes: 27 additions & 27 deletions config/requestConfig.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import request from "@/utils/request";
import request from "@/plugins/request";
import store from '@/config/store';
// #ifdef H5
import { h5Login } from '@/config/html5Utils';
import {
h5Login
} from '@/config/html5Utils';
// #endif
import base from '@/config/baseUrl';
// #ifdef MP-WEIXIN
import { onLogin } from '@/config/login';
import {
onLogin
} from '@/config/login';
// #endif
//可以new多个request来支持多个域名请求
let $http = new request({
Expand All @@ -22,27 +26,24 @@ let $http = new request({
//当前接口请求数
let requestNum = 0;
//请求开始拦截器
$http.requestStart = function (options) {
console.log("请求开始",options);
uni.showNavigationBarLoading();
$http.requestStart = function(options) {
console.log("请求开始", options);
if (options.load) {
if (requestNum <= 0) {
//打开加载动画
// #ifndef APP-PLUS
uni.showLoading({
title: '加载中',
mask: true
});
// #endif
}
requestNum += 1;
}
// 图片上传大小限制
if(options.method == "FILE" && options.maxSize){
if (options.method == "FILE" && options.maxSize) {
// 文件最大字节: options.maxSize 可以在调用方法的时候加入参数
maxSize = options.maxSize;
for(let item of options.files){
if(item.size > maxSize){
for (let item of options.files) {
if (item.size > maxSize) {
setTimeout(() => {
uni.showToast({
title: "图片过大,请重新上传",
Expand All @@ -63,12 +64,11 @@ $http.requestStart = function (options) {
return options;
}
//请求结束
$http.requestEnd = function (options, resolve) {
$http.requestEnd = function(options, resolve) {
if (resolve.statusCode !== 200 && options.data && options.data.pageNo && options.loadMore) {
store.commit("setRequestState", 1200);
}
//判断当前接口是否需要加载动画
uni.hideNavigationBarLoading();
if (options.load) {
requestNum = requestNum - 1;
if (requestNum <= 0) {
Expand All @@ -86,19 +86,19 @@ $http.requestEnd = function (options, resolve) {
}
let loginPopupNum = 0;
//所有接口数据处理(此方法需要开发者根据各自的接口返回类型修改,以下只是模板)
$http.dataFactory = function (res) {
$http.dataFactory = function(res) {
console.log("接口请求数据", {
httpUrl:res.httpUrl,
resolve:res.response,
headers:res.headers,
data:res.data,
method:res.method,
url: res.url,
resolve: res.response,
headers: res.headers,
data: res.data,
method: res.method,
});
if (res.response.statusCode && res.response.statusCode == 200) {
let httpData = res.response.data;

/*********以下只是模板(及共参考),需要开发者根据各自的接口返回类型修改*********/

//判断数据是否请求成功
if (httpData.success) {
if (res.data.pageNo && res.loadMore && httpData.data.data) {
Expand Down Expand Up @@ -144,7 +144,7 @@ $http.dataFactory = function (res) {
content: content,
confirmText: "去登录",
cancelText: "再逛会",
success: function (res) {
success: function(res) {
loginPopupNum--;
if (res.confirm) {
uni.navigateTo({
Expand Down Expand Up @@ -180,12 +180,12 @@ $http.dataFactory = function (res) {
}
// 返回错误的结果(catch接受数据)
res.reject(res.response);
} else { //其他错误提示
} else { //其他错误提示
if (res.data.pageNo && res.loadMore) {
store.commit("setRequestState", 1200);
}
if (res.isPrompt) {
setTimeout(function () {
setTimeout(function() {
uni.showToast({
title: httpData.info,
icon: "none",
Expand All @@ -196,10 +196,10 @@ $http.dataFactory = function (res) {
// 返回错误的结果(catch接受数据)
res.reject(res.response);
}

/*********以上只是模板(及共参考),需要开发者根据各自的接口返回类型修改*********/
}else{

} else {
// 返回错误的结果(catch接受数据)
res.reject(res.response);
}
Expand Down
54 changes: 0 additions & 54 deletions config/share.js

This file was deleted.

7 changes: 2 additions & 5 deletions config/store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
import {
modifyJson
} from '@/utils/utils';
//变量
const state = {
//用户数据
Expand All @@ -25,7 +22,7 @@ const state = {
//缓存浏览器的数据名称
const cacheNameList = ["userInfo"];
const mutations = {
//取出缓存数据
//取出缓存数据(打开APP就取出)
setCacheData(state) {
for (let name of cacheNameList) {
// #ifndef H5
Expand All @@ -48,7 +45,7 @@ const mutations = {
//储存用户信息
setUserInfo(state, data) {
if (data) {
state.userInfo = modifyJson(data, state.userInfo);
state.userInfo = Object.assign(state.userInfo,data);
// #ifdef H5
window.sessionStorage.setItem('userInfo', JSON.stringify(state.userInfo));
// #endif
Expand Down
Loading

0 comments on commit 44b719a

Please sign in to comment.