| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- //app.js
- var util = require('./utils/utils.js');
- var api;
- App({
- data: {
- deviceInfo: {}
- },
- is_on_launch: true,
- onLaunch: function () {
- let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
- var uniacid = JSON.stringify(extConfig) != "{}" ? extConfig.uniacid : '20236';
- this.siteInfo.uniacid = uniacid;
- this.siteInfo.acid = uniacid;
- wx.setStorageSync("store_id", uniacid);
- console.log('uniacid:'+uniacid);
-
- this.setApi();
- api = this.api;
- this.getNavigationBarColor();
- this.getStoreData();
- this.getCatList();
- },
- getStoreData: function () {
- var page = this;
- this.request({
- url: api.default.store,
- success: function (res) {
- if (res.code == 0) {
- wx.setStorageSync("store", res.data.store);
- wx.setStorageSync("store_name", res.data.store_name);
- wx.setStorageSync("show_customer_service", res.data.show_customer_service);
- wx.setStorageSync("contact_tel", res.data.contact_tel);
- wx.setStorageSync("share_setting", res.data.share_setting);
- }
- },
- complete: function () {
- page.login();
- }
- });
- },
- getCatList: function () {
- this.request({
- url: api.default.cat_list,
- success: function (res) {
- if (res.code == 0) {
- var cat_list = res.data.list || [];
- wx.setStorageSync("cat_list", cat_list);
- }
- }
- });
- },
- login: function () {
- var pages = getCurrentPages();
- var page = pages[(pages.length - 1)];
- wx.showLoading({
- title: "正在登录",
- mask: true,
- });
- wx.login({
- success: function (res) {
- if (res.code) {
- var code = res.code;
- wx.getUserInfo({
- success: function (res) {
- getApp().request({
- url: api.passport.login,
- method: "post",
- data: {
- code: code,
- user_info: res.rawData,
- encrypted_data: res.encryptedData,
- iv: res.iv,
- signature: res.signature
- },
- success: function (res) {
- wx.hideLoading();
- if (res.code == 0) {
- wx.setStorageSync("access_token", res.data.access_token);
- wx.setStorageSync("user_info", res.data);
- var p = getCurrentPages();
- var parent_id = 0;
- if (p[0].options.user_id != undefined) {
- var parent_id = p[0].options.user_id;
- }
- else if (p[0].options.scene != undefined) {
- var parent_id = p[0].options.scene;
- }
- getApp().bindParent({
- parent_id: parent_id || 0
- });
- if (page == undefined) {
- return;
- }
- var loginNoRefreshPage = getApp().loginNoRefreshPage;
- for (var i in loginNoRefreshPage) {
- if (loginNoRefreshPage[i] === page.route)
- return;
- }
- wx.redirectTo({
- url: "/" + page.route + "?" + util.objectToUrlParams(page.options),
- fail: function () {
- wx.switchTab({
- url: "/" + page.route,
- });
- },
- });
- } else {
- wx.showToast({
- title: res.msg
- });
- }
- }
- });
- },
- fail: function (res) {
- wx.hideLoading();
- getApp().getauth({
- content: '需要获取您的用户信息授权,请到小程序设置中打开授权',
- cancel: true,
- success: function (e) {
- if (e) {
- getApp().login();
- }
- },
- });
- }
- });
- } else {
- //console.log(res);
- }
- }
- });
- },
- request: function (object) {
- if (!object.data)
- object.data = {};
- var access_token = wx.getStorageSync("access_token");
- if (access_token) {
- object.data.access_token = access_token;
- }
- object.data._uniacid = this.siteInfo.uniacid;
- object.data._acid = this.siteInfo.acid;
- wx.request({
- url: object.url,
- header: object.header || {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- data: object.data || {},
- method: object.method || "GET",
- dataType: object.dataType || "json",
- success: function (res) {
- if (res.data.code == -1) {
- getApp().login();
- } else {
- if (object.success)
- object.success(res.data);
- }
- },
- fail: function (res) {
- console.warn('--- request fail >>>');
- console.warn(res);
- console.warn('<<< request fail ---');
- var app = getApp();
- if (app.is_on_launch) {
- app.is_on_launch = false;
- wx.showModal({
- title: "网络请求出错",
- content: res.errMsg,
- showCancel: false,
- success: function (res) {
- if (res.confirm) {
- if (object.fail)
- object.fail(res);
- }
- }
- });
- } else {
- wx.showToast({
- title: res.errMsg,
- image: "/images/icon-warning.png",
- });
- if (object.fail)
- object.fail(res);
- }
- },
- complete: function (res) {
- if (res.statusCode != 200) {
- console.log('--- request http error >>>');
- console.log(res.statusCode);
- console.log(res.data);
- console.log('<<< request http error ---');
- }
- if (object.complete)
- object.complete(res);
- }
- });
- },
- saveFormId: function (form_id) {
- this.request({
- url: api.user.save_form_id,
- data: {
- form_id: form_id,
- }
- });
- },
- loginBindParent: function (object) {
- var access_token = wx.getStorageSync("access_token");
- if (access_token == '') {
- return true;
- }
- getApp().bindParent(object);
- },
- bindParent: function (object) {
- if (object.parent_id == "undefined" || object.parent_id == 0)
- return;
- console.log("Try To Bind Parent With User Id:" + object.parent_id);
- var user_info = wx.getStorageSync("user_info");
- var share_setting = wx.getStorageSync("share_setting");
- if (share_setting.level > 0) {
- var parent_id = object.parent_id;
- if (parent_id != 0) {
- getApp().request({
- url: api.share.bind_parent,
- data: {parent_id: object.parent_id},
- success: function (res) {
- if (res.code == 0) {
- user_info.parent = res.data
- wx.setStorageSync('user_info', user_info);
- }
- }
- });
- }
- }
- },
- /**
- * 分享送优惠券
- * */
- shareSendCoupon: function (page) {
- wx.showLoading({
- mask: true,
- });
- if (!page.hideGetCoupon) {
- page.hideGetCoupon = function (e) {
- var url = e.currentTarget.dataset.url || false;
- page.setData({
- get_coupon_list: null,
- });
- if (url) {
- wx.navigateTo({
- url: url,
- });
- }
- };
- }
- this.request({
- url: api.coupon.share_send,
- success: function (res) {
- if (res.code == 0) {
- page.setData({
- get_coupon_list: res.data.list
- });
- }
- },
- complete: function () {
- wx.hideLoading();
- }
- });
- },
- getauth: function (object) {
- wx.showModal({
- title: '是否打开设置页面重新授权',
- content: object.content,
- confirmText: '去设置',
- success: function (e) {
- if (e.confirm) {
- wx.openSetting({
- success: function (res) {
- if (object.success) {
- object.success(res);
- }
- },
- fail: function (res) {
- if (object.fail) {
- object.fail(res);
- }
- },
- complete: function (res) {
- if (object.complete)
- object.complete(res);
- }
- })
- } else {
- if (object.cancel) {
- getApp().getauth(object);
- }
- }
- }
- })
- },
- api: require('api.js'),
- setApi: function () {
- var siteroot = this.siteInfo.siteroot;
- siteroot = siteroot.replace('app/index.php', '');
- siteroot += 'addons/zjhj_mall/core/web/index.php?store_id=-1&r=api/';
- function getNewApiUri(api) {
- for (var i in api) {
- if (typeof api[i] === 'string') {
- api[i] = api[i].replace('{$_api_root}', siteroot);
- } else {
- api[i] = getNewApiUri(api[i]);
- }
- }
- return api;
- }
- this.api = getNewApiUri(this.api);
- var _index_api_url = this.api.default.index;
- var _web_root = _index_api_url.substr(0, _index_api_url.indexOf('/index.php'));
- this.webRoot = _web_root;
- },
- webRoot: null,
- siteInfo : {
- 'uniacid': '-1',
- 'acid': '-1',
- 'version': '1.0.0',
- 'siteroot': 'https://u5.9026.com/app/index.php',
- },
- currentPage: null,
- pageOnLoad: function (page) {
- this.currentPage = page;
- console.log('--------pageOnLoad----------');
- if (typeof page.openWxapp === 'undefined') {
- page.openWxapp = this.openWxapp;
- }
- if (typeof page.showToast === 'undefined') {
- page.showToast = this.pageShowToast;
- }
- this.setNavigationBarColor();
- this.setPageNavbar(page);
- var app = this;
- this.currentPage.naveClick = function(e){
- var page = this;
- app.navigatorClick(e,page);
- }
- wx.getSystemInfo({
- success: res => {
- let modelmes = res.model;
- if (modelmes.search('iPhone X') != -1) {
- page.setData({
- isIphoneX: true
- })
- }
- }
- })
- },
- pageOnReady: function (page) {
- console.log('--------pageOnReady----------');
- },
- pageOnShow: function (page) {
- console.log('--------pageOnShow----------');
- },
- pageOnHide: function (page) {
- console.log('--------pageOnHide----------');
- },
- pageOnUnload: function (page) {
- console.log('--------pageOnUnload----------');
- },
- setPageNavbar: function (page) {
- console.log('----setPageNavbar----');
- console.log(page);
- var navbar = wx.getStorageSync('_navbar');
- if (navbar) {
- setNavbar(navbar);
- }
- this.request({
- url: api.default.navbar,
- success: function (res) {
- if (res.code == 0) {
- setNavbar(res.data);
- wx.setStorageSync('_navbar', res.data);
- }
- }
- });
- function setNavbar(navbar) {
- var in_navs = false;
- var route = page.route || (page.__route__ || null);
- for (var i in navbar.navs) {
- if (navbar.navs[i].url === "/" + route) {
- navbar.navs[i].active = true;
- in_navs = true;
- } else {
- navbar.navs[i].active = false;
- }
- }
- if (!in_navs)
- return;
- page.setData({_navbar: navbar});
- }
- },
- getNavigationBarColor: function () {
- var app = this;
- app.request({
- url: api.default.navigation_bar_color,
- success: function (res) {
- if (res.code == 0) {
- wx.setStorageSync('_navigation_bar_color', res.data);
- app.setNavigationBarColor();
- }
- }
- });
- },
- setNavigationBarColor: function () {
- var navigation_bar_color = wx.getStorageSync('_navigation_bar_color');
- if (navigation_bar_color) {
- wx.setNavigationBarColor(navigation_bar_color);
- }
- },
- //登录成功后不刷新的页面
- loginNoRefreshPage: [
- 'pages/index/index',
- //'pages/fxhb/open/open',
- //'pages/fxhb/detail/detail',
- ],
- openWxapp: function (e) {
- console.log('--openWxapp---');
- if (!e.currentTarget.dataset.url)
- return;
- var url = e.currentTarget.dataset.url;
- url = parseQueryString(url);
- url.path = url.path ? decodeURIComponent(url.path) : "";
- console.log("Open New App");
- console.log(url);
- wx.navigateToMiniProgram({
- appId: url.appId,
- path: url.path,
- complete: function (e) {
- console.log(e);
- }
- });
- function parseQueryString(url) {
- var reg_url = /^[^\?]+\?([\w\W]+)$/,
- reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
- arr_url = reg_url.exec(url),
- ret = {};
- if (arr_url && arr_url[1]) {
- var str_para = arr_url[1], result;
- while ((result = reg_para.exec(str_para)) != null) {
- ret[result[1]] = result[2];
- }
- }
- return ret;
- }
- },
- pageShowToast: function (e) {
- console.log('--- pageToast ---');
- var page = this.currentPage;
- var duration = e.duration || 2500;
- var title = e.title || '';
- var success = e.success || null;
- var fail = e.fail || null;
- var complete = e.complete || null;
- if (page._toast_timer) {
- clearTimeout(page._toast_timer);
- }
- page.setData({
- _toast: {
- title: title,
- },
- });
- page._toast_timer = setTimeout(function () {
- var _toast = page.data._toast;
- _toast.hide = true;
- page.setData({
- _toast: _toast,
- });
- if (typeof complete == 'function') {
- complete();
- }
- }, duration);
- },
- navigatorClick: function (e,page) {
- var open_type = e.currentTarget.dataset.open_type;
- if(open_type == 'redirect'){
- return true;
- }
- if (open_type == 'wxapp') {
- var path = e.currentTarget.dataset.path;
- var str = path.substr(0,1);
- if(str != '/'){
- path = '/' + path;
- }
- wx.navigateToMiniProgram({
- appId: e.currentTarget.dataset.appid,
- path: path,
- complete: function (e) {
- console.log(e);
- }
- });
- }
- if(open_type == 'tel'){
- var contact_tel = e.currentTarget.dataset.tel;
- wx.makePhoneCall({
- phoneNumber: contact_tel
- })
- }
- return false;
- function parseQueryString(url) {
- var reg_url = /^[^\?]+\?([\w\W]+)$/,
- reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
- arr_url = reg_url.exec(url),
- ret = {};
- if (arr_url && arr_url[1]) {
- var str_para = arr_url[1], result;
- while ((result = reg_para.exec(str_para)) != null) {
- ret[result[1]] = result[2];
- }
- }
- return ret;
- }
- },
- });
|