猎豆优选小程序

App.vue 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <script>
  2. import {
  3. postJSON as $postJSON,
  4. get as $get,
  5. getUuid as $getUuid,
  6. } from "./config/util";
  7. import { api as $api } from "./config/api";
  8. const router = require("./utils/router");
  9. export default {
  10. globalData: {
  11. isIOS: false, // 判断是否是ios
  12. isIphoneX: false, // 是否为 iphoneX 及以上机型
  13. statusBarHeight: 44, // 状态栏高度
  14. navigationBarHeight: 44, // 导航栏高度 (自定义导航-导航烂高度默认44)
  15. SDKVersion: "", // 当前小程序基础库版本号
  16. WXVersion: "", // 当前小程序所在微信的版本号
  17. navbarBackground: {
  18. // 顶部导航背景图
  19. backgroundImage:
  20. "linear-gradient(45deg, rgb(244, 117, 42), rgb(239, 15, 42))",
  21. },
  22. newNavbarBackground: {
  23. background: `url(https://ld.726p.com/tbk/upload/c579f015a49a3f9e066f0ab00ab56f7.png) no-repeat`,
  24. backgroundSize: "cover",
  25. },
  26. $router: router,
  27. fun_version: "", //过审接口版本号
  28. },
  29. onLaunch: function (options) {
  30. console.log("App onLaunch options => ", options);
  31. // 判断当前设备是否为 iphoneX 及以上机型
  32. this.handleGetIsIphoneX();
  33. // 判断当前小程序是否为最新版本
  34. this.handleVersionUpdate();
  35. // 获取当前小程序基础库版本号
  36. this.handleGetVersion();
  37. // 小程序审核状态查询
  38. this.getVerifiedFun();
  39. // 微信登录
  40. this.handleWxLogin();
  41. // 设置用户首次启动小程序标识(用于在首页展示开屏广告)
  42. this.handleSetUserIsFirst(true);
  43. },
  44. onShow: function (options) {
  45. // 进来options参数处理
  46. this.onShowEntry(options);
  47. // 清除微信客服标识
  48. this.handleRemoveWxServiceFlag();
  49. },
  50. onHide: function () {
  51. const wxServiceDialogFlag = uni.getStorageSync("wxServiceDialogFlag");
  52. if (!wxServiceDialogFlag) {
  53. // 获取“微信客服”弹框标识,如果没触发过“微信客服”弹框,再初始化剪贴板(弹出猜你喜欢弹框)
  54. uni.setStorageSync("clipboard", ""); // 通过clipboard的内容来判断是否已经过弹出剪切板内容,弹出过的clipboard为no,否则clipboard为空
  55. }
  56. },
  57. methods: {
  58. // 初次进来
  59. onShowEntry(options) {
  60. if (options.path != "pages/subPackages/home/shareDetail") {
  61. // 从好友邀请过来,分享人的邀请码
  62. if (options.query && options.query.invite_code) {
  63. uni.setStorageSync("share_invite_code", options.query.invite_code);
  64. }
  65. }
  66. },
  67. // 判断当前设备是否为 iphoneX 及以上机型
  68. handleGetIsIphoneX() {
  69. const systemInfo = uni.getSystemInfoSync();
  70. const _app = this.$scope || getApp(); // 兼容方式
  71. _app.globalData.statusBarHeight = systemInfo.statusBarHeight;
  72. if (systemInfo.system.toUpperCase().includes("IOS")) {
  73. _app.globalData.isIOS = true; // 判断是否是ios
  74. if (systemInfo.safeArea.top > 20) {
  75. _app.globalData.isIphoneX = true;
  76. }
  77. }
  78. },
  79. // 判断当前小程序是否为最新版本
  80. handleVersionUpdate() {
  81. const that = this;
  82. // 判断应用的 getUpdateManager 是否在当前版本可用
  83. if (uni.canIUse("getUpdateManager")) {
  84. const updateManager = uni.getUpdateManager();
  85. // 向小程序后台请求完新版本信息
  86. updateManager.onCheckForUpdate(function (res) {
  87. if (res.hasUpdate) {
  88. // 小程序有新版本,静默下载新版本,新版本下载完成
  89. updateManager.onUpdateReady(function () {
  90. uni.showModal({
  91. title: "更新提示",
  92. content: "小程序已发布新版本,是否重启应用?",
  93. success: function (res) {
  94. if (res.confirm) {
  95. updateManager.applyUpdate();
  96. } else if (res.cancel) {
  97. // 强制用户更新,弹出第二次弹窗
  98. uni.showModal({
  99. title: "提示",
  100. content: "小程序已发布新版本,是否重启应用",
  101. showCancel: false, // 隐藏取消按钮
  102. success: function (res) {
  103. // 第二次提示后,强制更新
  104. if (res.confirm) {
  105. // 当新版本下载完成,调用该方法会强制当前小程序应用上新版本并重启
  106. updateManager.applyUpdate();
  107. } else if (res.cancel) {
  108. // 重新回到版本更新提示
  109. that.handleVersionUpdate();
  110. }
  111. },
  112. });
  113. }
  114. },
  115. });
  116. });
  117. // 当新版本下载失败
  118. updateManager.onUpdateFailed(function () {
  119. uni.showModal({
  120. title: "提示",
  121. content: "小程序版本更新失败,请您删除当前小程序并重新打开",
  122. });
  123. });
  124. }
  125. });
  126. } else {
  127. // 提示用户在最新版本的客户端上体验
  128. uni.showModal({
  129. title: "温馨提示",
  130. content:
  131. "当前微信版本过低,可能无法使用该功能,请升级到最新版本后重试。",
  132. });
  133. }
  134. },
  135. // 获取当前小程序基础库版本号
  136. handleGetVersion() {
  137. const systemInfo = uni.getSystemInfoSync();
  138. console.log("systemInfo => ", systemInfo);
  139. const _app = this.$scope || getApp(); // 兼容方式
  140. _app.globalData.SDKVersion = systemInfo.SDKVersion;
  141. _app.globalData.WXVersion = systemInfo.version;
  142. },
  143. // 显示获取用户信息确认弹框
  144. handleShowModal(content) {
  145. return new Promise((resolve) => {
  146. uni.showModal({
  147. title: "提示",
  148. content: content || "确定执行当前操作吗?",
  149. success: async (res) => {
  150. if (res.confirm) {
  151. resolve("确认");
  152. }
  153. },
  154. });
  155. });
  156. },
  157. // 根据token => 获取用户信息 => 更新至本地存储
  158. handleGetUserInfo(loadingFlag) {
  159. return new Promise(async (resolve, reject) => {
  160. try {
  161. const url = $api.login_userInfo;
  162. const params = {};
  163. const headers = {
  164. token: uni.getStorageSync("token") || "",
  165. };
  166. if (!loadingFlag) {
  167. // 我的页面刷新信息的时候,不需要loading
  168. uni.showLoading({ title: "加载中...", mask: true });
  169. }
  170. const { data: res } = await $postJSON(url, params, headers);
  171. if (res && res.errno == 0) {
  172. uni.setStorageSync("userInfo", { ...res.rst });
  173. resolve({ ...res.rst });
  174. } else {
  175. uni.showToast({ title: res.err || "操作失败", icon: "none" });
  176. reject(res.err);
  177. }
  178. } catch (error) {
  179. console.log("error => ", error);
  180. reject(error);
  181. } finally {
  182. uni.hideLoading();
  183. }
  184. });
  185. },
  186. // 获取渠道版本信息 - 接口header中使用
  187. channelGetVersion() {
  188. return new Promise(async (resolve, reject) => {
  189. try {
  190. const url = $api.channel_getVersion;
  191. const params = {
  192. channel_name: "猎豆优选小程序",
  193. };
  194. const { data: res } = await $get(url, params);
  195. if (res && res.errno == 0) {
  196. const { source, version } = res.rst || {};
  197. uni.setStorageSync("channel_source", source || "");
  198. uni.setStorageSync("channel_version", version || "");
  199. resolve(res.rst);
  200. } else {
  201. reject(res.err);
  202. }
  203. } catch (error) {
  204. reject(error);
  205. }
  206. });
  207. },
  208. // 小程序审核状态接口
  209. async getVerifiedFun() {
  210. try {
  211. const url = $api.wx_verified;
  212. const params = {
  213. version: "v1.0",
  214. };
  215. const { data: res } = await $get(url, params);
  216. if (res && res.errno == 0) {
  217. const _app = this.$scope || getApp(); // 兼容方式
  218. _app.globalData.fun_version = res.rst.version;
  219. } else {
  220. _app.globalData.fun_version = "";
  221. }
  222. } catch (error) {
  223. } finally {
  224. uni.hideLoading();
  225. }
  226. },
  227. // 用户打开详情页行为数据上报categoryNew_countBuy
  228. async categoryNew_countBuy({ shop_type, type, path, category_id }) {
  229. let user_label = "";
  230. // #ifdef H5
  231. // h5生成随机数 为个统计游客数
  232. user_label = uni.getStorageSync("uuid_user_label") || "";
  233. if (!user_label || user_label == "") {
  234. user_label = $getUuid();
  235. uni.setStorageSync("uuid_user_label", user_label);
  236. }
  237. // #endif
  238. try {
  239. const url = $api.categoryNew_countBuy;
  240. const params = {
  241. shop_type,
  242. type,
  243. path,
  244. category_id,
  245. user_label: user_label,
  246. };
  247. const { data: res } = await $postJSON(url, params);
  248. if (res && res.errno == 0) {
  249. } else {
  250. }
  251. } catch (error) {
  252. } finally {
  253. }
  254. },
  255. // 获取当前用户等级
  256. handleGetUserLevel() {
  257. const userInfo = uni.getStorageSync("userInfo") || {};
  258. return userInfo.user_level || "";
  259. },
  260. // 清空用户登录信息缓存
  261. handleClearUserInfo() {
  262. const USER_INFO_KEYS = [
  263. "token",
  264. "user_id",
  265. "userInfo",
  266. "nickname",
  267. "sex",
  268. "default_invite_code",
  269. "openid",
  270. "session_key",
  271. "unionid",
  272. ];
  273. USER_INFO_KEYS.forEach((key) => {
  274. uni.removeStorageSync(key);
  275. });
  276. },
  277. // 执行微信登录
  278. async handleWxLogin() {
  279. const isUnionid = this.handleGetUnionid();
  280. if (!isUnionid) {
  281. // 没有unionid => 从后台获取
  282. const res = await this.handleGetWxLoginInfo();
  283. // 将当前用户微信登录信息存储至本地(供后续手机号登录及业务使用)
  284. // 新用户(未绑定过微信的用户)
  285. uni.setStorageSync("openid", res.openid || "");
  286. uni.setStorageSync("session_key", res.session_key || "");
  287. uni.setStorageSync("unionid", res.unionid || "");
  288. if (res.token) {
  289. // 老用户(当前微信绑定过手机号) => 根据token获取用户信息
  290. uni.setStorageSync("token", res.token);
  291. uni.setStorageSync("user_id", res.user_id);
  292. uni.setStorageSync("sex", res.sex);
  293. uni.setStorageSync("nickname", res.nickname);
  294. uni.setStorageSync("name", res.name);
  295. this.handleGetUserInfo(); // 获取用户信息
  296. }
  297. }
  298. },
  299. // 判断本地是否有微信唯一id unionid
  300. handleGetUnionid() {
  301. return uni.getStorageSync("unionid") || "";
  302. },
  303. // 获取用户微信登录信息
  304. handleGetWxLoginInfo() {
  305. return new Promise((resolve, reject) => {
  306. uni.login({
  307. success: async (res) => {
  308. const code = res.code;
  309. try {
  310. const { data: res = {} } = await $get($api.login_getUserInfo, {
  311. code,
  312. });
  313. if (res && res.errno == 0) {
  314. resolve(res.rst.user_info);
  315. } else {
  316. uni.showToast({ title: res.err, icon: "none" });
  317. reject(res.err);
  318. }
  319. } catch (error) {
  320. reject(error);
  321. console.log("error => ", error);
  322. }
  323. },
  324. });
  325. });
  326. },
  327. // 设置用户首次启动标识(用于在首页展示开屏广告)
  328. handleSetUserIsFirst(isFirst) {
  329. uni.setStorageSync("isFirstLaunch", isFirst || false);
  330. },
  331. // 获取用户首次启动标识
  332. handleGetUserIsFirst() {
  333. return uni.getStorageSync("isFirstLaunch") || false;
  334. },
  335. handleRemoveWxServiceFlag() {
  336. uni.removeStorageSync("wxServiceDialogFlag");
  337. },
  338. },
  339. };
  340. </script>
  341. <style lang="scss">
  342. @import "uview-ui/index.scss";
  343. /*每个页面公共css */
  344. // 去除u-navbar自定义导航栏下方分割线/底部下划线
  345. .u-border-bottom:after {
  346. border: none !important;
  347. }
  348. //统一背景颜色
  349. page {
  350. background: #f5f5f5;
  351. }
  352. // 无数据展示
  353. .no-data-blcok {
  354. text-align: center;
  355. height: 100%;
  356. display: flex;
  357. align-items: center;
  358. justify-content: center;
  359. flex-direction: column;
  360. flex: 1;
  361. .no-data-img {
  362. width: 160rpx;
  363. height: 160rpx;
  364. }
  365. .no-data-hint {
  366. color: #999999;
  367. font-size: 26rpx;
  368. line-height: 50rpx;
  369. margin-top: 40rpx;
  370. }
  371. }
  372. </style>