index.vue 12 KB

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