| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- // app.js
- /**
- * 新版备注:
- * 1. 两个流程 fare_type 0外卖 1自提 默认自提
- * 2. 有会员价
- * 3. 商品分规格, 有规格商品和无规格商品同时存在
- * 4. 获取应用实例 一律直接用getApp()
- * 5. 无主动退出
- * 6. 用户右上角的分享,只开首页和个人中心,其他页面都关闭
- * 7. 如需在接口成功时弹出返回信息,请单独加一下toast,避免与微信的loading冲突(wx.showLoading 与wx.showToast不能同时存在)
- * 8. 如需当接口连续调用,共用一个loading时,请传参nextInvoke,且主动控制何时关闭loading
- *
- * **/
- let myPro = require("/utils/wxRequest.js");
- App({
- onLaunch() {
- let that = this;
- // 拿code换openid(先放onLaunch试一段)
- wx.login({
- success: function(res) {
- // console.log('获取wxcode',res)
- let params = {
- code: res.code
- };
- myPro.wxRequest("user/mini-auth","POST",params).then(res=>{
- let result = res.result;
- // 有token 是老用户
- if(result.token){
- that.globalData.token = result.token;
- wx.setStorage({
- key: "token",
- data: result.token
- });
- that.globalData.is_member = result.user.is_member; // 会员 0否 1是
- that.globalData.userInfo = {
- nickname: result.user.nickname,
- thumb: result.user.thumb,
- phone: result.user.phone
- };
- if(result.user.store){
- that.globalData.role = 1; // 有店铺,即为门店
- };
- // console.log('用户',that.globalData.userInfo)
- // 拿用户信息(供首页使用)
- }else{
- // 新用户
- that.globalData.openid = result.openid;
- that.globalData.session_key = result.session_key;
- }
- }).catch(err=>{
- console.log('报错信息',err);
- wx.showToast({
- title: err,
- icon: "none"
- })
- })
- },
- fail: function(res){
- wx.showToast({
- title: "微信未授权成功,请在设置中允许使用用户信息",
- icon: "none"
- });
- }
- });
- },
- onShow(){
- // console.log('onShow');
- let that = this;
- // 设备信息
- wx.getSystemInfo({
- success(res) {
- // 获取系统信息
- const systemInfo = wx.getSystemInfoSync();
- // 胶囊按钮位置信息
- const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
- // 胶囊位置(初始位置)
- that.globalData.customTop = menuButtonInfo.top;
- // 胶囊位置(中间位置)
- that.globalData.customCenter =
- menuButtonInfo.top + menuButtonInfo.height / 2;
- // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度
- that.globalData.setBgHeight =
- (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 +
- menuButtonInfo.height +
- systemInfo.statusBarHeight;
- },
- });
- // 定位
- // that.getLocal();
- },
- // 验证登录
- verifyLogin(callBack){
- const that = this;
- if(!callBack){
- callBack = function(){};
- };
- if(!that.globalData.token){
- let pages = getCurrentPages();
- if(pages.length > 0){
- wx.setStorage({
- key: 'loginCallBackPage',
- data: that.pageBackUrl(),
- success: function(){
- wx.reLaunch({
- url: '/pages/login/index/index'
- })
- },
- })
- };
- callBack(false);
- }else{
- callBack(true);
- }
- },
- // 回跳地址
- pageBackUrl: function(){
- let pages = getCurrentPages();
- let currentPage = pages[pages.length -1];
- let url= currentPage.route;
- let options = JSON.stringify(currentPage.options);
- options = options.replace(/[\{\}\"]/g,'');
- options = options.replace(/\:/g,'=');
- options = options.replace(/\,/g,'&');
- url = url + '?' + options;
- // console.log('回跳地址',url)
- return url
- },
- // 定位授权
- getLocal: function (callback) {
- var that = this;
- if (callback == undefined) {
- callback = function () {
- // console.log('定位授权::::默认回调函数')
- }
- }
- wx.getSetting({
- success(res) {
- if (!res.authSetting['scope.userLocation']) {
- // 未授权时,先授权
- wx.authorize({
- scope: 'scope.userLocation',
- success(auth) {
- console.log("auth:::::", auth);
- // 授权成功
- if (auth.errMsg == 'authorize:ok') {
- // 用户授权成功
- that.globalData.location_auth = 1;
- // 执行获取地理位置
- that.getLocation(callback);
- };
- },
- fail(auth){
- console.log("auth:::::", auth);
- callback();
- // 用户拒绝,添加标识
- that.globalData.location_auth = -1;
- }
- })
- } else {
- // 已授权,执行获取地理位置
- that.globalData.location_auth = 1;
- that.getLocation(callback);
- }
- },
- fail(res) {
- that.globalData.location_auth = -1;
- callback();
- console.log('设备信息调取失败',res);
- }
- })
- },
- // 执行获取地理位置
- getLocation(callback) {
- let that = this;
- // 用户已经同意小程序使用定位
- wx.getLocation({
- type: 'wgs84',
- success(res) {
- console.log('定位成功',res);
- that.globalData.lat = res.latitude;
- that.globalData.lng = res.longitude;
- // console.log('地理位置:', res)
- // 执行回调函数
- setTimeout(() => {
- callback();
- }, 0);
- },
- fail(res){
- console.log('定位失败',res);
- setTimeout(() => {
- callback();
- }, 0);
- }
- })
- },
- globalData: {
- // 测试 domain api imgUrl
- // 测试appid: wx3fa706377424c2c9
- // domain: "http://singlestore.shanmuzhi.com",
- // api: "http://singlestore.shanmuzhi.com/",
- // imgUrl: "http://singlestore.shanmuzhi.com",
- domain: "https://dandianone.bjycst.top/",
- api: "https://dandianone.bjycst.top/",
- imgUrl: "https://dandianone.bjycst.top/",
- // 正式 wx4ee6b1b6aa3d1403
- // domain: 'https://user.jinyutaoxiaoya.com/',
- // api: 'https://user.jinyutaoxiaoya.com/',
- // imgUrl: 'https://jinyu-txy.oss-cn-beijing.aliyuncs.com',
- location_auth: null, // 地理位置授权标识 1同意 -1拒绝
- lat: 0, // 衡水市火车站 37.743955
- lng: 0, // 衡水市火车站 115.691485
- token: wx.getStorageSync('token') ? wx.getStorageSync('token') : '', // ceshitoken
- openid: null,
- session_key: null,
- role: null, // 角色 1为门店 null
- userInfo: null,
- customTop: null, // 胶囊位置 顶部
- customCenter: null, // 胶囊位置 中间
- setBgHeight: null, // 导航栏高度
- logoTu: "/statics/img/logo.png",
- is_member: 0, // 是否为会员 1是 0否
- store_id: 1, // 单店(约定id是1)
- loadingCount: 0 // 声明一个对象用于存储请求个数
- },
- });
|