app.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // app.js
  2. const api = require('./request/api');
  3. const http = require('./request/http');
  4. const utils = require('./utils/util.js');
  5. const storeKeys = require('./utils/storageKeys.js');
  6. App({
  7. onLaunch(options) {
  8. wx.getSystemInfo({
  9. success: (res) => {
  10. this.globalData.statusBarHeight = res.statusBarHeight + 46
  11. }
  12. })
  13. utils.removeStorageSync(storeKeys.SHAREID);
  14. utils.removeStorageSync(storeKeys.APPLESID);
  15. utils.removeStorageSync(storeKeys.ISCHECk);
  16. utils.removeStorageSync(storeKeys.ISPOPUPAD);
  17. utils.removeStorageSync(storeKeys.SOURECID);
  18. utils.removeStorageSync(storeKeys.CLICKID);
  19. if(options){
  20. console.log(options)
  21. // url参数中可以获取到gdt_vid、weixinadinfo参数值 let gdt_vid = options.gdt_vid
  22. let gdt_vid = options.query.gdt_vid || "";
  23. let weixinadinfo = options.query.weixinadinfo || "";
  24. /**
  25. * 对接地址:https://ad.weixin.qq.com/main.html#/docs/232
  26. * 参数说明地址:https://ad.weixin.qq.com/main.html#/docs/232?branch_id=2IhtO
  27. * weixinadinfo 存在则是来源微信广告
  28. */
  29. if(weixinadinfo){
  30. let weixinadinfoArr = weixinadinfo.split(".") || [];
  31. let aid = weixinadinfoArr[0] || "";
  32. aid && utils.setStorageSync(storeKeys.CLICKID,aid);
  33. }
  34. }
  35. /*
  36. 管理小程序更新
  37. */
  38. if (wx.canIUse('getUpdateManager')) {
  39. const updateManager = wx.getUpdateManager()
  40. updateManager.onCheckForUpdate(function (res) {
  41. // 请求完新版本信息的回调
  42. if (res.hasUpdate) {
  43. updateManager.onUpdateReady(function () {
  44. wx.showModal({
  45. title: '更新提示',
  46. content: '需要重启以使用最新功能',
  47. confirmColor: '#FD455D',
  48. confirmText: '知道了',
  49. showCancel: false,
  50. success: function (res) {
  51. if (res.confirm) {
  52. updateManager.applyUpdate()
  53. }
  54. }
  55. })
  56. })
  57. updateManager.onUpdateFailed(function () {
  58. // 新的版本下载失败
  59. wx.showModal({
  60. confirmColor: '#FD455D',
  61. showCancel: false,
  62. title: '已经有新版本了哟~',
  63. content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开哟'
  64. })
  65. })
  66. }
  67. })
  68. } else {
  69. //基础库低于 1.9.90不能使用getUpdateManager
  70. wx.showModal({
  71. confirmColor: '#FD455D',
  72. title: '提示',
  73. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  74. })
  75. }
  76. /*
  77. 获取系统状态栏信息
  78. */
  79. let _this = this;
  80. wx.getSystemInfo({
  81. success: res => {
  82. let modelmes = res.model;
  83. // res.safeArea.top > 20
  84. 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) {
  85. _this.globalData.isIphoneX = true;
  86. }
  87. _this.globalData.phoneHeight = res.screenHeight;
  88. wx.setStorageSync('modelmes', modelmes)
  89. }
  90. })
  91. this.autoCoupon();
  92. },
  93. autoCoupon() {
  94. if (!utils.getStorageSync(storeKeys.TOKEN)) {
  95. return;
  96. }
  97. http.request({
  98. url: api.URL + '/api/auto_coupon',
  99. method: 'GET',
  100. noLogin: true,
  101. noToast: true,
  102. token: utils.getStorageSync(storeKeys.TOKEN),
  103. success: (res) => {},
  104. error: (res) => {}
  105. })
  106. },
  107. onShow(options) {
  108. // 存储渠道推广id
  109. if (options.query) {
  110. console.log("请看这里活动渠道id:" + options.query.sourceId)
  111. console.log(options.query)
  112. options.query.sourceId && utils.setStorageSync(storeKeys.SOURECID, options.query.sourceId);
  113. }
  114. //进入来源
  115. this.globalData.scene = options.scene;
  116. if (utils.getStorageSync(storeKeys.TOKEN)) {
  117. this.getCartNumber();
  118. } else {
  119. utils.removeStorageSync(storeKeys.CARETOTAl);
  120. utils.removeStorageSync(storeKeys.USERINFO);
  121. wx.removeTabBarBadge({
  122. index: 2,
  123. })
  124. }
  125. },
  126. globalData: {
  127. versionNumber: "v1.3.7", //版本号
  128. scene: null, //小程序进入场景 1047 扫码 https://developers.weixin.qq.com/miniprogram/dev/reference/scene-list.html
  129. userInfo: {},
  130. statusBarHeight: 0,
  131. shareImage: "https://cookhome-images.oss-cn-hangzhou.aliyuncs.com/file.jm/yscShareImage.png",
  132. shareTitle: "买正宗公明腊肠,就上官方体验店;正品保证 官方溯源",
  133. sharePath: "pages/tabBar/index/index?share=xcx",
  134. riceShareImage: "https://cookhome-images-1.oss-cn-hangzhou.aliyuncs.com/spokesman/nk6bBxk1lDEg1tjzocME5yGA6vXVvOW542SULr8f.png",
  135. },
  136. //页面跳转
  137. navigateToUrl: function (url) {
  138. wx.navigateTo({
  139. url: url,
  140. })
  141. },
  142. //页面跳转
  143. navigateTo: function (e) {
  144. let url = e.currentTarget.dataset.url;
  145. wx.navigateTo({
  146. url: url,
  147. })
  148. },
  149. //获取购物车数据
  150. getCartNumber() {
  151. http.request({
  152. url: api.URL + '/api/cart/total',
  153. method: 'GET',
  154. noLogin: true,
  155. token: utils.getStorageSync(storeKeys.TOKEN),
  156. success: (res) => {
  157. this.changeTabBarBadge(res.data.data.cartTotal)
  158. },
  159. error: (res) => {},
  160. })
  161. },
  162. //改变购物车数字
  163. changeTabBarBadge(number) {
  164. number = number || "";
  165. if (number) {
  166. if (number >= 100) {
  167. number = "99+"
  168. }
  169. utils.setStorageSync(storeKeys.CARETOTAl, number);
  170. wx.setTabBarBadge({
  171. index: 2,
  172. text: (number).toString()
  173. })
  174. } else {
  175. utils.removeStorageSync(storeKeys.CARETOTAl);
  176. wx.removeTabBarBadge({
  177. index: 2,
  178. })
  179. }
  180. },
  181. //活动埋点
  182. buriedPoint(activityId, type) {
  183. let userInfo = utils.getStorageSync(storeKeys.USERINFO) || {}
  184. let userId = userInfo.user_id || 0;
  185. let data = {
  186. "coupon_activity_id": activityId, //20裂变活动ID
  187. "type": type, //埋点活动页面类型 1首页 2分享 3被分享页面 4被分享领取 5被分享人二次分享
  188. "user_id": userId //用户ID 0为未登录游客
  189. }
  190. http.request({
  191. url: api.URL + '/api/coupon.activity/statistics',
  192. token: utils.getStorageSync(storeKeys.TOKEN),
  193. method: 'POST',
  194. noToast: true,
  195. data,
  196. success: (res) => {},
  197. error: (res) => {}
  198. })
  199. },
  200. //添加数据统计(渠道统计分析)
  201. countRecord() {
  202. // client:0 客户端类型 默认0 0-微信小程序
  203. // visit_type:1 /页面来源 1-拼团活动 2-砍价活动 3-渠道推广 4商品详情
  204. // visit_type_id:1 /页面来源目标id 如:活动id、渠道推广id
  205. let visit_type = 3;
  206. let visit_type_id = utils.getStorageSync(storeKeys.SOURECID) || "";
  207. if (!visit_type_id) {
  208. return;
  209. }
  210. console.log("添加数据统计渠道id:" + visit_type_id)
  211. http.request({
  212. url: api.URL + '/api/visitRecord/countRecord?client=0&visit_type='+visit_type + '&visit_type_id=' + visit_type_id,
  213. token: utils.getStorageSync(storeKeys.TOKEN),
  214. method: 'get',
  215. noToast: true,
  216. success: (res) => {},
  217. error: (res) => {}
  218. })
  219. },
  220. //添加数据统计(渠道统计分析)(仅供拼团活动、砍价活动调用)
  221. countRecordActivity(visit_type, activity_id) {
  222. // client:0 客户端类型 默认0 0-微信小程序
  223. // visit_type:1 /页面来源 1-拼团活动 2-砍价活动 3-渠道推广 10拼团裂变
  224. // visit_type_id:1 /页面来源目标id 如:活动id、渠道推广id
  225. let visit_type_id = activity_id;
  226. // if(!utils.getStorageSync(storeKeys.SOURECID)){
  227. // return;
  228. // }
  229. console.log("添加数据统计(仅供拼团活动、砍价活动调用)渠道id:" + visit_type_id)
  230. http.request({
  231. url: api.URL + '/api/visitRecord/countRecord?client=0&visit_type=' + visit_type + '&visit_type_id=' + visit_type_id,
  232. token: utils.getStorageSync(storeKeys.TOKEN),
  233. method: 'get',
  234. noToast: true,
  235. success: (res) => {},
  236. error: (res) => {}
  237. })
  238. },
  239. //添加数据统计(公共页面调用)
  240. countRecordPublic(visit_type){
  241. // visit_type 0首页 5商品列表
  242. console.log("添加数据统计(公共页面调用)",visit_type)
  243. http.request({
  244. url: api.URL + '/api/visitRecord/countRecord?client=0&visit_type=' + visit_type,
  245. token: utils.getStorageSync(storeKeys.TOKEN),
  246. method: 'get',
  247. noToast: true,
  248. success: (res) => {},
  249. error: (res) => {}
  250. })
  251. },
  252. //分享统计(公共页面调用)
  253. shareRecordPublic(target_type,target_id){
  254. // target_type 1:商品分享
  255. // target_id target_type为1时-商品id
  256. http.request({
  257. url: api.URL + '/api/user_share/add?target_type='+target_type+'&target_id=' + target_id,
  258. token: utils.getStorageSync(storeKeys.TOKEN),
  259. method: 'get',
  260. noToast: true,
  261. success: (res) => {},
  262. error: (res) => {}
  263. })
  264. },
  265. // 调用接口通知后端推送授权成功
  266. noticePush({template_id},callback) {
  267. let data = {
  268. template_id //模版id
  269. }
  270. http.request({
  271. url: api.URL + '/api/index/subscribe',
  272. method: 'POST',
  273. token: utils.getStorageSync(storeKeys.TOKEN),
  274. data,
  275. success: (res) => {
  276. console.log('订阅返回----')
  277. console.log(res)
  278. callback && callback();
  279. },
  280. error: (res) => {}
  281. })
  282. },
  283. // 调用接口通知后端取消推送
  284. cancelPush({template_id},callback) {
  285. let data = {
  286. template_id //模版id
  287. }
  288. http.request({
  289. url: api.URL + '/api/index/cancelSubscribe',
  290. method: 'POST',
  291. token: utils.getStorageSync(storeKeys.TOKEN),
  292. data,
  293. success: (res) => {
  294. console.log(res)
  295. callback && callback();
  296. },
  297. error: (res) => {}
  298. })
  299. }
  300. })