1 Star 0 Fork 2

落地 / 商城源码

forked from coyeking / 商城源码 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.js 13.43 KB
一键复制 编辑 原始数据 按行查看 历史
coyeking 提交于 2021-04-12 15:37 . 1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
import Vue from 'vue'
import App from './App'
const tabBarLinks = [
'pages/index/index',
'pages/category/index',
'pages/flow/index',
'pages/user/index'
];
// 工具类
import util from './utils/util.js';
Vue.prototype.isDubug = false
Vue.prototype.isDubugToken = 'b2c82c154fad61bf42b34912ed28410c'
Vue.config.productionTip = false
// #ifdef APP-PLUS
// 版本号
const currentVersion = {
id: plus.runtime.version
}
Vue.prototype.$current = currentVersion
// #endif
// 龙商秀
Vue.prototype.api_root = 'https://demo.store.xiaowo6.cn/index.php/api/';
Vue.prototype.siteInfo = {
name: "商城APP",
siteroot: "https://demo.store.xiaowo6.cn/", // 必填: api地址
wxapp_id: "10001", // 此处为商城ID, 可在超管后台-商城列表中查看
};
/**
* 获取商城ID
*/
Vue.prototype.getWxappId = function() {
return siteinfo.wxapp_id = 10001;
}
/**
* 获取tabBar页面路径列表
*/
Vue.prototype.getTabBarLinks = function() {
return tabBarLinks;
}
/**
* 获取客服
*/
Vue.prototype.getQQkefu = function() {
return uni.getStorageSync('qqkefu');
}
/**
* 记录推荐人id
*/
Vue.prototype.saveRefereeId = function(refereeId) {
if (!uni.getStorageSync('referee_id'))
uni.setStorageSync('referee_id', refereeId);
}
/**
* 获取场景值(scene)
*/
Vue.prototype.getSceneData = function(query) {
return query.scene ? util.scene_decode(query.scene) : {};
}
/**
* 生成转发的url参数
*/
Vue.prototype.getShareUrlParams = function(params) {
let _this = this;
return util.urlEncode(Object.assign({
referee_id: _this.getUserId()
}, params));
}
/**
* 设置顶部导航栏
*/
Vue.prototype.setCommonPageBar = function(page) {
uni.setStorageSync('titleTextColor',page.style.titleTextColor === 'white' ? '#ffffff' : '#000000');
uni.setStorageSync('titleBackgroundColor',page.style.titleBackgroundColor);
}
/**
* 获取顶部导航栏
*/
Vue.prototype.getCommonPageBar = function() {
return {
titleTextColor: uni.getStorageSync('titleTextColor'),
titleBackgroundColor: uni.getStorageSync('titleBackgroundColor'),
}
}
/**
* 小程序启动场景
*/
Vue.prototype.onStartupScene = function(query) {
// 获取场景值
let scene = this.getSceneData(query);
// 记录推荐人id
let refereeId = query.referee_id ? query.referee_id : scene.uid;
refereeId > 0 && (this.saveRefereeId(refereeId));
}
/**
* 验证登录
*/
Vue.prototype.getUserInfo = function(e, callback) {
let App = this;
if (e.detail.errMsg !== 'getUserInfo:ok') {
return false;
}
uni.showLoading({
title: "正在登录",
mask: true
});
// 执行微信登录
uni.login({
success(res) {
// 发送用户信息
App._post_form('user/login', {
code: res.code,
user_info: e.detail.rawData,
encrypted_data: e.detail.encryptedData,
iv: e.detail.iv,
signature: e.detail.signature,
referee_id: uni.getStorageSync('referee_id')
}, result => {
// 记录token user_id
uni.setStorageSync('token', result.data.token);
uni.setStorageSync('user_id', result.data.user_id);
// 执行回调函数
callback && callback();
}, false, () => {
uni.hideLoading();
});
}
});
}
/**
* 验证登录
*/
Vue.prototype.checkIsLogin = function() {
return uni.getStorageSync('token') != '' && uni.getStorageSync('user_id') != '';
}
/**
* 当前用户id
*/
Vue.prototype.getUserId = function() {
return uni.getStorageSync('user_id');
},
/**
* 对象转URL
*/
Vue.prototype.urlEncode = function(data) {
var _result = [];
for (var key in data) {
var value = data[key];
if (value.constructor == Array) {
value.forEach(function(_value) {
_result.push(key + "=" + _value);
});
} else {
_result.push(key + '=' + value);
}
}
return _result.join('&');
}
/**
* 记录formId
* (因微信模板消息已下线,所以formId取消不再收集)
*/
Vue.prototype.saveFormId = function(formId) {
return true;
}
// 模拟继承微信setData的方法
Vue.prototype.setData = function(obj) {
// console.log('这里是方法');
let that = this;
let keys = [];
let val, data;
Object.keys(obj).forEach(function(key) {
keys = key.split('.');
val = obj[key];
data = that.$data;
keys.forEach(function(key2, index) {
// console.log(key2)
if (index + 1 == keys.length) {
that.$set(data, key2, val);
} else {
if (!data[key2]) {
that.$set(data, key2, {});
}
}
data = data[key2];
})
});
}
// 显示失败提示框
Vue.prototype.showError = function(msg, callback) {
uni.showModal({
title: '友情提示',
content: msg,
showCancel: false,
success: function(res) {
// callback && (setTimeout(function() {
// callback();
// }, 1500));
callback && callback();
}
});
}
/**
* 执行用户登录
*/
Vue.prototype.doLogin = function(delta) {
// 保存当前页面
let pages = getCurrentPages();
if (pages.length) {
// 登录完成后跳转回原页面 有报错,暂时弃用
// let currentPage = pages[pages.length - 1];
// "pages/login/login" != currentPage.route && uni.setStorageSync("currentPage", currentPage);
}
// 跳转授权页面
uni.navigateTo({
url: "/pages/login/login?delta=" + (delta || 1)
});
}
/**
* 显示成功提示框
*/
Vue.prototype.showSuccess = function(msg, callback) {
uni.showToast({
title: msg,
icon: 'success',
mask: true,
duration: 1500,
success: function() {
callback && (setTimeout(function() {
callback();
}, 1500));
}
});
}
/**
* 验证是否存在user_info
*/
Vue.prototype.validateUserInfo = function() {
let user_info = uni.getStorageSync('user_info');
return !!uni.getStorageSync('user_info');
}
/**
* 跳转到指定页面
* 支持tabBar页面
*/
Vue.prototype.navigationTo = function(url) {
let urls = url.url
if (!urls || urls.length == 0) {
return false;
}
// console.log(urls);
let tabBarLinks = this.getTabBarLinks();
// tabBar页面
if (tabBarLinks.indexOf(urls) > -1) {
uni.switchTab({
url: '/' + urls
});
} else {
// 普通页面
uni.navigateTo({
url: '/' + urls
});
}
}
/**
* 发起微信支付
*/
Vue.prototype.wxPayment = function(option) {
let options = Object.assign({
payment: {},
success: () => {},
fail: () => {},
complete: () => {},
}, option);
// APP支付
// #ifdef APP-PLUS
let orderInfo = {
"appid": "wx314dcd5739d8f102", //appid
"noncestr": options.payment.nonceStr, //随机字符串
"package": "Sign=WXPay", //扩展字段
"partnerid": "1470210802", //商户号
"prepayid": options.payment.prepay_id, //预支付交易会话ID
"timestamp": Number(options.payment.timeStamp),
"sign": options.payment.paySign
}
//
console.log(JSON.stringify(orderInfo));
uni.requestPayment({
provider: "wxpay",
orderInfo: JSON.stringify(orderInfo), //微信、支付宝订单数据
success: function(res) {
// console.log('success:' + JSON.stringify(res));
options.success(res);
},
fail: function(res) {
// console.log('fail:' + JSON.stringify(err));
options.fail(res);
console.log('fail:' + JSON.stringify(res));
},
});
// #endif
// 微信小程序支付
// #ifdef MP-WEIXIN
uni.requestPayment({
provider: 'wxpay',
timeStamp: options.payment.timeStamp,
nonceStr: options.payment.nonceStr,
package: 'prepay_id=' + options.payment.prepay_id,
signType: 'MD5',
paySign: options.payment.paySign,
success(res) {
options.success(res);
},
fail(res) {
options.fail(res);
},
complete(res) {
options.complete(res);
}
});
// #endif
}
// 模拟继承get请求的方法
Vue.prototype._get = function(url, data, success, fail, complete, check_login) {
let App = this;
// 构造请求参数
data = data || {};
data.wxapp_id = App.siteInfo.wxapp_id;
// 构造get请求
let request = function() {
data.token = uni.getStorageSync('token');
// data.token = 'b632c206a4b12399578f34585d2c7769';
uni.request({
url: App.api_root + url,
header: {
'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
},
data: data,
success: function(res) {
if (res.statusCode !== 200 || typeof res.data !== 'object') {
console.log(res);
App.showError('网络请求出错');
return false;
}
if (res.data.code === -1) {
// 登录态失效, 重新登录
App.doLogin();
} else if (res.data.code === 0) {
App.showError(res.data.msg, function() {
fail && fail(res);
});
return false;
} else {
success && success(res.data);
}
},
fail: function(res) {
App.showError(res.errMsg, function() {
fail && fail(res);
});
},
complete: function(res) {
uni.hideNavigationBarLoading();
complete && complete(res);
},
});
};
// 判断是否需要验证登录
check_login ? App.doLogin(request) : request();
}
// 模拟继承post请求的方法
Vue.prototype._post_form = function(url, data, success, fail, complete) {
let App = this;
data.wxapp_id = App.siteInfo.wxapp_id;
data.token = uni.getStorageSync('token');
// data.token = 'b632c206a4b12399578f34585d2c7769';
uni.request({
url: App.api_root + url,
header: {
'content-type': 'application/x-www-form-urlencoded',
},
method: 'POST',
data: data,
success: function(res) {
if (res.statusCode !== 200 || typeof res.data !== 'object') {
App.showError('网络请求出错');
return false;
}
if (res.data.code === -1) {
// 登录态失效, 重新登录
App.doLogin();
return false;
} else if (res.data.code === 0) {
App.showError(res.data.msg, function() {
fail && fail(res);
});
return false;
}
success && success(res.data);
},
fail: function(res) {
// console.log(res);
App.showError(res.errMsg, function() {
fail && fail(res);
});
},
complete: function(res) {
// uni.hideLoading();
complete && complete(res);
}
});
}
// 参数number为毫秒时间戳,format为需要转换成的日期格式
Vue.prototype.transformTime = function(timestamp) {
// 获取年月日时分秒值 slice(-2)过滤掉大于10日期前面的0
var datetime = new Date(timestamp * 1000);
var year = datetime.getFullYear(),
month = ("0" + (datetime.getMonth() + 1)).slice(-2),
date = ("0" + datetime.getDate()).slice(-2),
hour = ("0" + datetime.getHours()).slice(-2),
minute = ("0" + datetime.getMinutes()).slice(-2),
second = ("0" + datetime.getSeconds()).slice(-2);
// 拼接
console.log('year====>' + datetime);
var result = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
// 返回
return result;
}
/*
*检测版本
*/
Vue.prototype.detectionVersion = function() {
let _this = this;
_this._post_form('page/appupdate', {
wxapp_id: _this.siteInfo.wxapp_id,
}, result => {
function toNum(a) {
var a = a.toString();
var c = a.split('.');
var num_place = ["", "0", "00", "000", "0000"],
r = num_place.reverse();
for (var i = 0; i < c.length; i++) {
var len = c[i].length;
c[i] = r[len] + c[i];
}
var res = c.join('');
return res;
}
function cpr_version(nweVersion, currentversion) {
var _nweVersion = toNum(nweVersion),
_currentversion = toNum(currentversion);
let status = 10
if (_nweVersion > _currentversion) {
status = 20
};
return status;
}
let content = ''; // 最新版版本信息
let downloadUrl = ''; // 下载地址
let nweVersion = '0'; // 最新版版本
let currentversion = '9.9.0' //当前版本
// #ifdef APP-PLUS
currentversion = plus.runtime.version; //当前版本
// #endif
if (_this._platform == 'android') {
content = result.data.data.AndroidText
downloadUrl = result.data.data.AndroidUrl
nweVersion = result.data.data.AndroidVersion
} else {
content = result.data.data.IOSText
downloadUrl = result.data.data.IOSUrl
nweVersion = result.data.data.IOSVersion
}
uni.setStorageSync('qqkefu', result.data.data.QQ);
let status = cpr_version(nweVersion, currentversion);
let versionInfo = {
"status": status, //升级标志,10无需升级 20需要升级
"content": content, //最新版版本信息
"url": downloadUrl, //更新包下载地址
"currentversion": currentversion, //当前版本
"edition": nweVersion, //最新版版本
};
uni.setStorageSync('versionInfo', versionInfo);
if (status == 20) {
uni.showModal({
title: '提示',
content: '有新的更新,请前往升级!',
showCancel: false,
success: function(res) {
if (res.confirm) {
uni.navigateTo({
url: '/pages/updataAPP/updataAPP'
})
}
}
});
}
return;
});
}
Vue.prototype.showModal = function(msg, callback) {
// #ifdef APP-PLUS
uni.showModal({
title: '友情提示',
content: msg,
cancelText: '确认',
confirmText: '取消',
success: function(res) {
if (res.cancel) callback && callback(res);
}
});
return;
// #endif
// #ifdef MP-WEIXIN
uni.showModal({
title: '友情提示',
content: msg,
success: function(res) {
if (res.confirm) callback && callback(res);
},
});
return;
// #endif
}
/* 打开外部浏览器 */
Vue.prototype.openBrowser = function(url){
// #ifdef MP-WEIXIN
return;
// #endif
// #ifdef APP-PLUS
plus.runtime.openURL(url);
return;
// #endif
// #ifdef H5
window.location.href = url;
return;
// #endif
}
Vue.prototype.showLoading = function(title = '加载中', callback) {
uni.showLoading({
title: title,
mask: true,
success: function(res) {
if (res.cancel) callback && callback(res);
}
});
return;
}
Vue.prototype.hideLoading = function() {
uni.hideLoading();
return;
}
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
1
https://gitee.com/locdee/mall-source-code.git
git@gitee.com:locdee/mall-source-code.git
locdee
mall-source-code
商城源码
master

搜索帮助