123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- // app.js
- const api = require('./request/api');
- const http = require('./request/http');
- const utils = require('./utils/util.js');
- const storeKeys = require('./utils/storageKeys.js');
- App({
- onLaunch(options) {
- wx.getSystemInfo({
- success: (res) => {
- this.globalData.statusBarHeight = res.statusBarHeight + 46
- }
- })
- utils.removeStorageSync(storeKeys.SHAREID);
- utils.removeStorageSync(storeKeys.APPLESID);
- utils.removeStorageSync(storeKeys.ISCHECk);
- utils.removeStorageSync(storeKeys.ISPOPUPAD);
- utils.removeStorageSync(storeKeys.SOURECID);
- utils.removeStorageSync(storeKeys.CLICKID);
- if(options){
- console.log(options)
- // url参数中可以获取到gdt_vid、weixinadinfo参数值 let gdt_vid = options.gdt_vid
- let gdt_vid = options.query.gdt_vid || "";
- let weixinadinfo = options.query.weixinadinfo || "";
- /**
- * 对接地址:https://ad.weixin.qq.com/main.html#/docs/232
- * 参数说明地址:https://ad.weixin.qq.com/main.html#/docs/232?branch_id=2IhtO
- * weixinadinfo 存在则是来源微信广告
- */
- if(weixinadinfo){
- let weixinadinfoArr = weixinadinfo.split(".") || [];
- let aid = weixinadinfoArr[0] || "";
- aid && utils.setStorageSync(storeKeys.CLICKID,aid);
- }
- }
- /*
- 管理小程序更新
- */
- if (wx.canIUse('getUpdateManager')) {
- const updateManager = wx.getUpdateManager()
- updateManager.onCheckForUpdate(function (res) {
- // 请求完新版本信息的回调
- if (res.hasUpdate) {
- updateManager.onUpdateReady(function () {
- wx.showModal({
- title: '更新提示',
- content: '需要重启以使用最新功能',
- confirmColor: '#FD455D',
- confirmText: '知道了',
- showCancel: false,
- success: function (res) {
- if (res.confirm) {
- updateManager.applyUpdate()
- }
- }
- })
- })
- updateManager.onUpdateFailed(function () {
- // 新的版本下载失败
- wx.showModal({
- confirmColor: '#FD455D',
- showCancel: false,
- title: '已经有新版本了哟~',
- content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开哟'
- })
- })
- }
- })
- } else {
- //基础库低于 1.9.90不能使用getUpdateManager
- wx.showModal({
- confirmColor: '#FD455D',
- title: '提示',
- content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
- })
- }
- /*
- 获取系统状态栏信息
- */
- let _this = this;
- wx.getSystemInfo({
- success: res => {
- let modelmes = res.model;
- // res.safeArea.top > 20
- if (modelmes.search('iPhone X') != -1 || modelmes.search('iPhone 11') != -1 || modelmes.search('iPhone 12') != -1 || modelmes.search('iPhone 12 Pro') != -1 || modelmes.search('iPhone 13') != -1 || modelmes.search('iPhone 13 Pro') != -1) {
- _this.globalData.isIphoneX = true;
- }
- _this.globalData.phoneHeight = res.screenHeight;
- wx.setStorageSync('modelmes', modelmes)
- }
- })
- this.autoCoupon();
- },
- autoCoupon() {
- if (!utils.getStorageSync(storeKeys.TOKEN)) {
- return;
- }
- http.request({
- url: api.URL + '/api/auto_coupon',
- method: 'GET',
- noLogin: true,
- noToast: true,
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {},
- error: (res) => {}
- })
- },
- onShow(options) {
- // 存储渠道推广id
- if (options.query) {
- console.log("请看这里活动渠道id:" + options.query.sourceId)
- console.log(options.query)
- options.query.sourceId && utils.setStorageSync(storeKeys.SOURECID, options.query.sourceId);
- }
- //进入来源
- this.globalData.scene = options.scene;
- if (utils.getStorageSync(storeKeys.TOKEN)) {
- this.getCartNumber();
- } else {
- utils.removeStorageSync(storeKeys.CARETOTAl);
- utils.removeStorageSync(storeKeys.USERINFO);
- wx.removeTabBarBadge({
- index: 2,
- })
- }
- },
- globalData: {
- versionNumber: "v1.0.0", //版本号
- scene: null, //小程序进入场景 1047 扫码 https://developers.weixin.qq.com/miniprogram/dev/reference/scene-list.html
- userInfo: {},
- statusBarHeight: 0,
- shareImage: `${api.URL}/uploads/image/image_global_sharing.jpg`,
- shareTitle: "买正宗公明腊肠,就上官方体验店;正品保证 官方溯源",
- sharePath: "pages/tabBar/index/index?share=xcx",
- riceShareImage: `${api.URL}/uploads/image/image_global_sharing.jpg`,
- },
- //页面跳转
- navigateToUrl: function (url) {
-
- wx.navigateTo({
- url: url,
- })
- },
- //页面跳转
- navigateTo: function (e) {
- let url = e.currentTarget.dataset.url;
- wx.navigateTo({
- url: url,
- })
- },
- //获取购物车数据
- getCartNumber() {
- http.request({
- url: api.URL + '/api/cart/total',
- method: 'GET',
- noLogin: true,
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- this.changeTabBarBadge(res.data.data.cartTotal)
- },
- error: (res) => {},
- })
- },
- //改变购物车数字
- changeTabBarBadge(number) {
- number = number || "";
- if (number) {
- if (number >= 100) {
- number = "99+"
- }
- utils.setStorageSync(storeKeys.CARETOTAl, number);
- wx.setTabBarBadge({
- index: 2,
- text: (number).toString()
- })
- } else {
- utils.removeStorageSync(storeKeys.CARETOTAl);
- wx.removeTabBarBadge({
- index: 2,
- })
- }
- },
- //活动埋点
- buriedPoint(activityId, type) {
- let userInfo = utils.getStorageSync(storeKeys.USERINFO) || {}
- let userId = userInfo.user_id || 0;
- let data = {
- "coupon_activity_id": activityId, //20裂变活动ID
- "type": type, //埋点活动页面类型 1首页 2分享 3被分享页面 4被分享领取 5被分享人二次分享
- "user_id": userId //用户ID 0为未登录游客
- }
- http.request({
- url: api.URL + '/api/coupon.activity/statistics',
- token: utils.getStorageSync(storeKeys.TOKEN),
- method: 'POST',
- noToast: true,
- data,
- success: (res) => {},
- error: (res) => {}
- })
- },
- //添加数据统计(渠道统计分析)
- countRecord() {
- // client:0 客户端类型 默认0 0-微信小程序
- // visit_type:1 /页面来源 1-拼团活动 2-砍价活动 3-渠道推广 4商品详情
- // visit_type_id:1 /页面来源目标id 如:活动id、渠道推广id
- let visit_type = 3;
- let visit_type_id = utils.getStorageSync(storeKeys.SOURECID) || "";
- if (!visit_type_id) {
- return;
- }
- console.log("添加数据统计渠道id:" + visit_type_id)
- http.request({
- url: api.URL + '/api/visitRecord/countRecord?client=0&visit_type='+visit_type + '&visit_type_id=' + visit_type_id,
- token: utils.getStorageSync(storeKeys.TOKEN),
- method: 'get',
- noToast: true,
- success: (res) => {},
- error: (res) => {}
- })
- },
- //添加数据统计(渠道统计分析)(仅供拼团活动、砍价活动调用)
- countRecordActivity(visit_type, activity_id) {
- // client:0 客户端类型 默认0 0-微信小程序
- // visit_type:1 /页面来源 1-拼团活动 2-砍价活动 3-渠道推广 10拼团裂变
- // visit_type_id:1 /页面来源目标id 如:活动id、渠道推广id
- let visit_type_id = activity_id;
- // if(!utils.getStorageSync(storeKeys.SOURECID)){
- // return;
- // }
- console.log("添加数据统计(仅供拼团活动、砍价活动调用)渠道id:" + visit_type_id)
- http.request({
- url: api.URL + '/api/visitRecord/countRecord?client=0&visit_type=' + visit_type + '&visit_type_id=' + visit_type_id,
- token: utils.getStorageSync(storeKeys.TOKEN),
- method: 'get',
- noToast: true,
- success: (res) => {},
- error: (res) => {}
- })
- },
- //添加数据统计(公共页面调用)
- countRecordPublic(visit_type){
- // visit_type 0首页 5商品列表
- console.log("添加数据统计(公共页面调用)",visit_type)
- http.request({
- url: api.URL + '/api/visitRecord/countRecord?client=0&visit_type=' + visit_type,
- token: utils.getStorageSync(storeKeys.TOKEN),
- method: 'get',
- noToast: true,
- success: (res) => {},
- error: (res) => {}
- })
- },
- //分享统计(公共页面调用)
- shareRecordPublic(target_type,target_id){
- // target_type 1:商品分享
- // target_id target_type为1时-商品id
- http.request({
- url: api.URL + '/api/user_share/add?target_type='+target_type+'&target_id=' + target_id,
- token: utils.getStorageSync(storeKeys.TOKEN),
- method: 'get',
- noToast: true,
- success: (res) => {},
- error: (res) => {}
- })
- },
- // 调用接口通知后端推送授权成功
- noticePush({template_id},callback) {
- let data = {
- template_id //模版id
- }
- http.request({
- url: api.URL + '/api/index/subscribe',
- method: 'POST',
- token: utils.getStorageSync(storeKeys.TOKEN),
- data,
- success: (res) => {
- console.log('订阅返回----')
- console.log(res)
- callback && callback();
- },
- error: (res) => {}
- })
- },
- // 调用接口通知后端取消推送
- cancelPush({template_id},callback) {
- let data = {
- template_id //模版id
- }
- http.request({
- url: api.URL + '/api/index/cancelSubscribe',
- method: 'POST',
- token: utils.getStorageSync(storeKeys.TOKEN),
- data,
- success: (res) => {
- console.log(res)
- callback && callback();
- },
- error: (res) => {}
- })
- }
- })
|