index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 :style="{fontSize:(wxFontSize)+'px'}">我要什么</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 :style="{fontSize:(wxFontSize)+'px'}">我有什么</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" :iconSize="wxFontSize+23" :fontSize="wxFontSize+6" :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. wxFontSize:17
  108. }
  109. },
  110. onLoad(options) {
  111. const appBaseInfo = wx.getAppBaseInfo();
  112. this.wxFontSize = uni.getStorageSync('fontSize')||appBaseInfo.fontSizeSetting||17;
  113. const index = Number(options.index || 0)
  114. // 根据底部tabbar菜单列表设置对应页面的加载情况
  115. this.tabberPageLoadFlag = this.tabbarList.map((item, tabbar_index) => {
  116. return index === tabbar_index
  117. })
  118. this._freshing = false;
  119. this.switchTabbar(index);
  120. let that = this;
  121. uni.hideShareMenu();
  122. },
  123. onShow(options){
  124. if (this.currentIndex === 3&&this.$refs.mine) {
  125. this.$refs.mine.fetchData();
  126. }
  127. if (this.currentIndex === 2&&this.$refs.comm) {
  128. this.$refs.comm.fetchData();
  129. }
  130. if (this.currentIndex === 0&&this.$refs.discovery) {
  131. this.$refs.discovery.fetchData();
  132. }
  133. if(uni.getStorageSync('lastTime')){
  134. //5*60s过期
  135. if(new Date().getTime()-uni.getStorageSync('lastTime')>5*60*1000||!uni.getStorageSync('userNo')){
  136. uni.setStorageSync('lastTime',new Date().getTime());
  137. if(this.$refs.authRef&&!uni.getStorageSync('loginOut')){
  138. // this.$refs.authRef.reLogin();
  139. }
  140. }
  141. }else{
  142. uni.setStorageSync('lastTime',new Date().getTime())
  143. }
  144. if(uni.getStorageSync('nextPage')&&uni.getStorageSync('userNo')){
  145. let nextPage = uni.getStorageSync('nextPage')
  146. uni.removeStorageSync('nextPage');
  147. uni.navigateTo({
  148. url: nextPage
  149. })
  150. }else{
  151. uni.removeStorageSync('nextPage')
  152. }
  153. if (wx.canIUse('getUpdateManager')) {
  154. const updateManager = wx.getUpdateManager();
  155. updateManager && updateManager.onCheckForUpdate((res) => {
  156. if (res.hasUpdate) {
  157. updateManager.onUpdateReady(() => {
  158. uni.showModal({
  159. title: '更新提示',
  160. content: '新版本已经准备好,是否重启应用?',
  161. success: (res) => {
  162. if (res.confirm) {
  163. uni.clearStorageSync() // 更新完成后刷新storage的数据
  164. updateManager.applyUpdate()
  165. }
  166. }
  167. })
  168. })
  169. updateManager.onUpdateFailed(() => {
  170. // uni.showModal({
  171. // title: '已有新版本上线',
  172. // content: '小程序自动更新失败,请删除该小程序后重新搜索打开哟~~~',
  173. // showCancel: false
  174. // })
  175. uni.reLaunch({
  176. url:'/pages/index/index'
  177. })
  178. })
  179. } else {
  180. //没有更新
  181. }
  182. })
  183. }
  184. },
  185. methods: {
  186. // 切换导航
  187. switchTabbar(index) {
  188. this._switchTabbarPage(index)
  189. },
  190. // 瀑布流导航页面滚动到底部
  191. tabbarPageScrollLower(e) {
  192. if (this.currentIndex === 1) {
  193. this.$refs.home.loadMore();
  194. }
  195. if (this.currentIndex === 2) {
  196. this.$refs.comm.loadMore();
  197. }
  198. },
  199. onPulling(e) {
  200. console.log("onpulling", e);
  201. if(e.detail.deltaY<0){
  202. return
  203. }
  204. this.triggered = true
  205. },
  206. onRefresh(){
  207. if (this._freshing) return;
  208. this._freshing = true;
  209. if (this.currentIndex === 1) {
  210. this.$refs.home.reFetchData();
  211. }
  212. if (this.currentIndex === 2) {
  213. this.$refs.comm.reFetchData();
  214. }
  215. setTimeout(() => {
  216. this.triggered = false;
  217. this._freshing = false;
  218. }, 2000)
  219. },
  220. onRestore() {
  221. this.triggered = false; // 需要重置
  222. },
  223. addButton() {
  224. if (!uni.getStorageSync('userNo')) {
  225. uni.showToast({
  226. title: '请登录',
  227. icon:'none'
  228. })
  229. uni.setStorageSync('nextPage',this.currentIndex === 1?'/circlePages/circle':'/circlePages/addShare');
  230. uni.navigateTo({
  231. url: '/pages/login/login'
  232. })
  233. return false;
  234. } else {
  235. uni.navigateTo({
  236. url: this.currentIndex === 1?'/circlePages/circle':'/circlePages/addShare'
  237. })
  238. }
  239. //未登录,去登录
  240. //已登录,去新增第一步
  241. },
  242. reFetchData() {
  243. if (this.currentIndex === 1) {
  244. console.error('this');
  245. this.$refs.home.reFetchData();
  246. }
  247. if (this.currentIndex === 2) {
  248. console.error('this');
  249. this.$refs.comm.reFetchData();
  250. }
  251. },
  252. // 切换导航页面
  253. _switchTabbarPage(index) {
  254. const selectPageFlag = this.tabberPageLoadFlag[index]
  255. if (selectPageFlag === undefined) {
  256. return
  257. }
  258. if (selectPageFlag === false) {
  259. this.tabberPageLoadFlag[index] = true
  260. }
  261. this.currentIndex = index
  262. if (this.currentIndex === 0&&this.$refs.discovery) {
  263. this.$refs.discovery.fetchData();
  264. }
  265. if (this.currentIndex === 3&&this.$refs.mine) {
  266. this.$refs.mine.fetchData();
  267. }
  268. },
  269. // 获取canvas实例和ctx画笔
  270. getMyCanvasAndCtx(id) {
  271. //id canvas 2d的id
  272. return new Promise((resolve) => {
  273. const query = wx.createSelectorQuery();
  274. query
  275. .select(`#${id}`)
  276. .fields({
  277. node: true,
  278. size: true,
  279. })
  280. .exec((res) => {
  281. const canvas = res[0].node;
  282. const ctx = canvas.getContext("2d");
  283. const dpr = wx.getSystemInfoSync().pixelRatio;
  284. canvas.width = res[0].width * dpr;
  285. canvas.height = res[0].height * dpr;
  286. ctx.scale(dpr, dpr);
  287. const data = {
  288. canvas,
  289. ctx,
  290. };
  291. resolve(data);
  292. });
  293. });
  294. },
  295. sleep(time) {
  296. return new Promise((resolve) => setTimeout(resolve, time));
  297. },
  298. async initCanvas(){
  299. // 保存画布
  300. const { ctx, canvas } = await this.getMyCanvasAndCtx("myCanvas");
  301. ctx.save();
  302. ctx.font = "24px";
  303. // ctx.textAlign = "center";
  304. ctx.fillText(this.shareData.company||this.shareData.contactPerson, 0, 32);
  305. // ctx.textAlign = "center";
  306. ctx.fillText(this.shareData.content, 0, 72);
  307. // sleep 500毫秒,等待图片加载完成 不然有可能圆形头像没有加载出来
  308. await this.sleep(500);
  309. let url = null;
  310. // 需要自行封装 canvasToTempFilePath
  311. const res = await wx.canvasToTempFilePath({ canvas });
  312. url = res.tempFilePath;
  313. return url;
  314. },
  315. addLog(shareData){
  316. const that = this;
  317. let params= {
  318. bisNo:shareData.bisNo,
  319. bisType:this.currentIndex === 1?'1':'2',
  320. userNo:uni.getStorageSync('userNo')
  321. };
  322. request.post('/slbForward/add',{slbForward:JSON.stringify(params)},{
  323. headers: {
  324. 'Content-Type': 'application/json', // 默认值
  325. },
  326. }).then(res=>{
  327. })
  328. },
  329. onShareAppMessage: function(option) {
  330. if(option.from=="button"){
  331. if (this.currentIndex === 1) {
  332. this.$refs.home.hideSheet();
  333. }
  334. if (this.currentIndex === 2) {
  335. this.$refs.comm.hideSheet();
  336. }
  337. this.shareData=option.target.dataset.coupon;
  338. this.addLog(this.shareData);
  339. const promise = new Promise(async (resolve) => {
  340. const url = await this.initCanvas();
  341. resolve({
  342. // title: `速立保`,
  343. path: `/pages/index/detail/detail?id=`+this.shareData.bisNo+'&isNeed='+(this.shareData.isNeed?1:0),
  344. // imageUrl: url,
  345. });
  346. });
  347. // 默认参数
  348. return {
  349. // title: `速立保`,
  350. path: `/pages/index/detail/detail?id=`+this.shareData.bisNo+'&isNeed='+(this.shareData.isNeed?1:0),
  351. promise,
  352. };
  353. }else{
  354. return {
  355. // title: '速立保',
  356. path: '/pages/index/index',
  357. };
  358. }
  359. }
  360. }
  361. }
  362. </script>
  363. <style lang="scss" scoped>
  364. </style>