index.vue 12 KB

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