index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <template>
  2. <view class="start-index">
  3. <view v-if="tabberPageLoadFlag[1]" :style="{display: currentIndex === 1 ? '' : 'none'}">
  4. <tn-nav-bar customBack>
  5. <view slot="back" class='tn-custom-nav-bar__back' @click="reFetchData()">
  6. <text v-if="!loading" class="tn-icon-circle-arrow" style="font-size: 30px;"></text>
  7. <view style="margin-top:6px" v-if="loading"><tn-loading :size="46"></tn-loading></view>
  8. </view>
  9. <view slot="default" style="display: flex;">
  10. <view style="flex:1;margin-left:25px;">
  11. <text>我要什么</text>
  12. </view>
  13. <view>
  14. <text class="tn-icon-add-fill" style="font-size: 30px;margin-right: 4px;"
  15. @click="addButton()"></text>
  16. </view>
  17. </view>
  18. </tn-nav-bar>
  19. <scroll-view class="custom-tabbar-page" :style="{marginTop: vuex_custom_bar_height + 'px'}" lower-threshold="60" scroll-y :refresher-enabled="true" :refresher-triggered="triggered" @refresherpulling="onPulling" @refresherrestore="onRestore" @refresherrefresh="onRefresh" enable-back-to-top @scrolltolower="tabbarPageScrollLower">
  20. <Home ref="home"></Home>
  21. </scroll-view>
  22. </view>
  23. <view v-if="tabberPageLoadFlag[2]" :style="{display: currentIndex === 2 ? '' : 'none'}">
  24. <tn-nav-bar customBack>
  25. <view slot="back" class='tn-custom-nav-bar__back' @click="reFetchData()">
  26. <text v-if="!loading" class="tn-icon-circle-arrow" style="font-size: 30px;"></text>
  27. <view style="margin-top:6px" v-if="loading" ><tn-loading :size="46"></tn-loading></view>
  28. </view>
  29. <view slot="default" style="display: flex;">
  30. <view style="flex:1;margin-left:25px">
  31. <text>我有什么</text>
  32. </view>
  33. <view>
  34. <text class="tn-icon-add-fill" style="font-size: 30px;margin-right: 4px;"
  35. @click="addButton()"></text>
  36. </view>
  37. </view>
  38. </tn-nav-bar>
  39. <scroll-view class="custom-tabbar-page" :style="{marginTop: vuex_custom_bar_height + 'px'}" scroll-y :refresher-enabled="true" :refresher-triggered="triggered" lower-threshold="60" @refresherrestore="onRestore" @refresherrefresh="onRefresh" @refresherpulling="onPulling" enable-back-to-top @scrolltolower="tabbarPageScrollLower">
  40. <Comm ref="comm"></Comm>
  41. </scroll-view>
  42. </view>
  43. <view v-if="tabberPageLoadFlag[0]" :style="{display: currentIndex === 0 ? '' : 'none'}">
  44. <scroll-view class="custom-tabbar-page" scroll-y enable-back-to-top @scrolltolower="tabbarPageScrollLower">
  45. <Discovery ref="discovery"></Discovery>
  46. </scroll-view>
  47. </view>
  48. <view v-if="tabberPageLoadFlag[3]" :style="{display: currentIndex === 3 ? '' : 'none'}">
  49. <scroll-view class="custom-tabbar-page" scroll-y enable-back-to-top @scrolltolower="tabbarPageScrollLower">
  50. <Mine ref="mine"></Mine>
  51. </scroll-view>
  52. </view>
  53. <Auth ref="authRef"></Auth>
  54. <tn-tabbar v-model="currentIndex" :list="tabbarList" activeColor="#1d60b1" inactiveColor="#AAAAAA"
  55. activeIconColor="#1d60b1" :animation="true" :safeAreaInsetBottom="true" @change="switchTabbar"></tn-tabbar>
  56. <canvas type="2d" id="myCanvas" style="width: 450px; height: 450px;left:9000px;position:fixed;"></canvas>
  57. </view>
  58. </template>
  59. <script>
  60. import Auth from '../index/autoLogin.vue'
  61. import Home from '../home/home.vue'
  62. import Comm from '../comm/comm.vue'
  63. import Discovery from '../discovery/discovery.vue'
  64. import Mine from '../mine/mine.vue'
  65. import request from '../../utils/request'
  66. export default {
  67. components: {
  68. Auth,
  69. Home,
  70. Comm,
  71. Discovery,
  72. Mine
  73. },
  74. data() {
  75. return {
  76. triggered: false,
  77. _freshing: false,
  78. // 底部tabbar菜单数据
  79. tabbarList: [
  80. {
  81. title: '供需发布平台',
  82. activeIcon: 'add-fill',
  83. inactiveIcon: 'add-circle',
  84. // count: 12
  85. },
  86. {
  87. title: '我要什么',
  88. activeIcon: 'trust-fill',
  89. inactiveIcon: 'trust'
  90. },
  91. {
  92. title: '我有什么',
  93. activeIcon: 'team-fill',
  94. inactiveIcon: 'team'
  95. },
  96. {
  97. title: '我的',
  98. activeIcon: 'my-fill',
  99. inactiveIcon: 'my'
  100. }
  101. ],
  102. // tabbar当前被选中的序号
  103. currentIndex: 0,
  104. // 自定义底栏对应页面的加载情况
  105. tabberPageLoadFlag: [],
  106. shareData:{}
  107. }
  108. },
  109. onLoad(options) {
  110. const index = Number(options.index || 0)
  111. // 根据底部tabbar菜单列表设置对应页面的加载情况
  112. this.tabberPageLoadFlag = this.tabbarList.map((item, tabbar_index) => {
  113. return index === tabbar_index
  114. })
  115. this._freshing = false;
  116. this.switchTabbar(index);
  117. let that = this;
  118. uni.hideShareMenu();
  119. },
  120. onShow(options){
  121. if (this.currentIndex === 3&&this.$refs.mine) {
  122. this.$refs.mine.fetchData();
  123. }
  124. if (this.currentIndex === 1&&this.$refs.comm) {
  125. this.$refs.comm.fetchData();
  126. }
  127. if (this.currentIndex === 2&&this.$refs.discovery) {
  128. this.$refs.discovery.fetchData();
  129. }
  130. if(uni.getStorageSync('lastTime')){
  131. //5*60s过期
  132. if(new Date().getTime()-uni.getStorageSync('lastTime')>5*60*1000||!uni.getStorageSync('userNo')){
  133. uni.setStorageSync('lastTime',new Date().getTime());
  134. if(this.$refs.authRef&&!uni.getStorageSync('loginOut')){
  135. // this.$refs.authRef.reLogin();
  136. }
  137. }
  138. }else{
  139. uni.setStorageSync('lastTime',new Date().getTime())
  140. }
  141. if(uni.getStorageSync('version')!='2.0.6'){
  142. uni.showModal({
  143. title: '更新提示',
  144. content: '新版本已经准备好,是否重启应用?\r\n 更新内容:优化产品体验,修复已知问题',
  145. success: (res) => {
  146. if (res.confirm) {
  147. uni.setStorageSync('version','2.0.6');
  148. uni.reLaunch({
  149. url:'/pages/index/index'
  150. })
  151. }
  152. }
  153. })
  154. }
  155. if(uni.getStorageSync('nextPage')&&uni.getStorageSync('userNo')){
  156. let nextPage = uni.getStorageSync('nextPage')
  157. uni.removeStorageSync('nextPage');
  158. uni.navigateTo({
  159. url: nextPage
  160. })
  161. }else{
  162. uni.removeStorageSync('nextPage')
  163. }
  164. if (wx.canIUse('getUpdateManager')) {
  165. const updateManager = wx.getUpdateManager();
  166. updateManager && updateManager.onCheckForUpdate((res) => {
  167. if (res.hasUpdate) {
  168. updateManager.onUpdateReady(() => {
  169. uni.showModal({
  170. title: '更新提示',
  171. content: '新版本已经准备好,是否重启应用?',
  172. success: (res) => {
  173. if (res.confirm) {
  174. uni.clearStorageSync() // 更新完成后刷新storage的数据
  175. updateManager.applyUpdate()
  176. }
  177. }
  178. })
  179. })
  180. updateManager.onUpdateFailed(() => {
  181. // uni.showModal({
  182. // title: '已有新版本上线',
  183. // content: '小程序自动更新失败,请删除该小程序后重新搜索打开哟~~~',
  184. // showCancel: false
  185. // })
  186. uni.reLaunch({
  187. url:'/pages/index/index'
  188. })
  189. })
  190. } else {
  191. //没有更新
  192. }
  193. })
  194. }
  195. },
  196. methods: {
  197. // 切换导航
  198. switchTabbar(index) {
  199. this._switchTabbarPage(index)
  200. },
  201. // 瀑布流导航页面滚动到底部
  202. tabbarPageScrollLower(e) {
  203. if (this.currentIndex === 1) {
  204. this.$refs.home.loadMore();
  205. }
  206. if (this.currentIndex === 2) {
  207. this.$refs.comm.loadMore();
  208. }
  209. },
  210. onPulling(e) {
  211. console.log("onpulling", e);
  212. if(e.detail.deltaY<0){
  213. return
  214. }
  215. this.triggered = true
  216. },
  217. onRefresh(){
  218. if (this._freshing) return;
  219. this._freshing = true;
  220. if (this.currentIndex === 1) {
  221. this.$refs.home.reFetchData();
  222. }
  223. if (this.currentIndex === 2) {
  224. this.$refs.comm.reFetchData();
  225. }
  226. setTimeout(() => {
  227. this.triggered = false;
  228. this._freshing = false;
  229. }, 2000)
  230. },
  231. onRestore() {
  232. this.triggered = false; // 需要重置
  233. },
  234. addButton() {
  235. if (!uni.getStorageSync('userNo')) {
  236. uni.showToast({
  237. title: '请登录',
  238. icon:'none'
  239. })
  240. uni.setStorageSync('nextPage',this.currentIndex === 0?'/circlePages/circle':'/circlePages/addShare');
  241. uni.navigateTo({
  242. url: '/pages/login/login'
  243. })
  244. return false;
  245. } else {
  246. uni.navigateTo({
  247. url: this.currentIndex === 0?'/circlePages/circle':'/circlePages/addShare'
  248. })
  249. }
  250. //未登录,去登录
  251. //已登录,去新增第一步
  252. },
  253. reFetchData() {
  254. if (this.currentIndex === 1) {
  255. console.error('this');
  256. this.$refs.home.reFetchData();
  257. }
  258. if (this.currentIndex === 2) {
  259. console.error('this');
  260. this.$refs.comm.reFetchData();
  261. }
  262. },
  263. // 切换导航页面
  264. _switchTabbarPage(index) {
  265. const selectPageFlag = this.tabberPageLoadFlag[index]
  266. if (selectPageFlag === undefined) {
  267. return
  268. }
  269. if (selectPageFlag === false) {
  270. this.tabberPageLoadFlag[index] = true
  271. }
  272. this.currentIndex = index
  273. if (this.currentIndex === 0&&this.$refs.discovery) {
  274. this.$refs.discovery.fetchData();
  275. }
  276. if (this.currentIndex === 3&&this.$refs.mine) {
  277. this.$refs.mine.fetchData();
  278. }
  279. },
  280. // 获取canvas实例和ctx画笔
  281. getMyCanvasAndCtx(id) {
  282. //id canvas 2d的id
  283. return new Promise((resolve) => {
  284. const query = wx.createSelectorQuery();
  285. query
  286. .select(`#${id}`)
  287. .fields({
  288. node: true,
  289. size: true,
  290. })
  291. .exec((res) => {
  292. const canvas = res[0].node;
  293. const ctx = canvas.getContext("2d");
  294. const dpr = wx.getSystemInfoSync().pixelRatio;
  295. canvas.width = res[0].width * dpr;
  296. canvas.height = res[0].height * dpr;
  297. ctx.scale(dpr, dpr);
  298. const data = {
  299. canvas,
  300. ctx,
  301. };
  302. resolve(data);
  303. });
  304. });
  305. },
  306. sleep(time) {
  307. return new Promise((resolve) => setTimeout(resolve, time));
  308. },
  309. async initCanvas(){
  310. // 保存画布
  311. const { ctx, canvas } = await this.getMyCanvasAndCtx("myCanvas");
  312. ctx.save();
  313. ctx.font = "24px";
  314. // ctx.textAlign = "center";
  315. ctx.fillText(this.shareData.company||this.shareData.contactPerson, 0, 32);
  316. // ctx.textAlign = "center";
  317. ctx.fillText(this.shareData.content, 0, 72);
  318. // sleep 500毫秒,等待图片加载完成 不然有可能圆形头像没有加载出来
  319. await this.sleep(500);
  320. let url = null;
  321. // 需要自行封装 canvasToTempFilePath
  322. const res = await wx.canvasToTempFilePath({ canvas });
  323. url = res.tempFilePath;
  324. return url;
  325. },
  326. addLog(shareData){
  327. const that = this;
  328. let params= {
  329. bisNo:shareData.bisNo,
  330. bisType:this.currentIndex === 0?'1':'2',
  331. userNo:uni.getStorageSync('userNo')
  332. };
  333. request.post('/slbForward/add',{slbForward:JSON.stringify(params)},{
  334. headers: {
  335. 'Content-Type': 'application/json', // 默认值
  336. },
  337. }).then(res=>{
  338. })
  339. },
  340. onShareAppMessage: function(option) {
  341. if(option.from=="button"){
  342. if (this.currentIndex === 1) {
  343. this.$refs.home.hideSheet();
  344. }
  345. if (this.currentIndex === 2) {
  346. this.$refs.comm.hideSheet();
  347. }
  348. this.shareData=option.target.dataset.coupon;
  349. this.addLog(this.shareData);
  350. const promise = new Promise(async (resolve) => {
  351. const url = await this.initCanvas();
  352. resolve({
  353. // title: `速立保`,
  354. path: `/pages/index/detail/detail?id=`+this.shareData.bisNo+'&isNeed='+(this.shareData.isNeed?1:0),
  355. // imageUrl: url,
  356. });
  357. });
  358. // 默认参数
  359. return {
  360. // title: `速立保`,
  361. path: `/pages/index/detail/detail?id=`+this.shareData.bisNo+'&isNeed='+(this.shareData.isNeed?1:0),
  362. promise,
  363. };
  364. }else{
  365. return {
  366. // title: '速立保',
  367. path: '/pages/index/index',
  368. };
  369. }
  370. }
  371. }
  372. }
  373. </script>
  374. <style lang="scss" scoped>
  375. </style>