index.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. const Parse = getApp().Parse;
  2. const login = require("../../../utils/login");
  3. const getTabs = require("../../../utils/getTabs")
  4. const company = getApp().globalData.company
  5. const tabName = getApp().globalData.moduleTab
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. tabbarList: [],
  12. tabbarOption: null,
  13. active: 0,
  14. isInit: false,
  15. botHeight: 0,
  16. template: getApp().globalData.styleTemplate,
  17. bottomHeight: getApp().globalData.screenHeight - getApp().globalData.safeArea?.bottom
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: async function (options) {
  23. getApp().globalData.styleTemplate = {
  24. style: '1',
  25. themeColor: '',
  26. }
  27. this.setData({
  28. template: getApp().globalData.styleTemplate
  29. })
  30. let str = decodeURIComponent(options.q)
  31. let obj = this.getParaName(str)
  32. let tabbarOption = await getTabs.getDiyTabs(tabName ? tabName : null)
  33. this.setData({
  34. tabbarList: tabbarOption.list,
  35. tabbarOption: tabbarOption,
  36. })
  37. let active = options.active
  38. if (this.data.tabbarOption.activeColor) {
  39. getApp().globalData.activeColor = this.data.tabbarOption.activeColor
  40. }
  41. if (this.data.tabbarOption.activeColor == '#000000') {
  42. getApp().globalData.titleColor = "#999999"
  43. }
  44. let event = {
  45. detail: options.active || 0,
  46. currentTarget: {
  47. dataset: 1
  48. }
  49. }
  50. await this.onChange(event)
  51. this.getUserLocation()
  52. this.getScreenHeight()
  53. if (obj && obj.tableid && obj.storeid) {
  54. wx.navigateTo({
  55. url: `/nova-tourism/pages/gourmet/store-package/meal-order/index?storeid=${obj.storeid}&tableid=${obj.tableid}`,
  56. })
  57. }
  58. },
  59. getUserLocation: async function () {
  60. let _this = this
  61. await wx.getSetting({
  62. success: (res) => {
  63. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
  64. wx.showModal({
  65. title: '请求授权当前位置',
  66. content: '逛一逛需要你的地理位置,请确认授权',
  67. success: function (res) {
  68. if (res.cancel) {
  69. wx.showToast({
  70. title: '拒绝授权',
  71. icon: 'none',
  72. duration: 1000
  73. })
  74. } else if (res.confirm) {
  75. wx.openSetting({
  76. success: function (dataAu) {
  77. if (dataAu.authSetting["scope.userLocation"] == true) {
  78. wx.showToast({
  79. title: '授权成功',
  80. icon: 'success',
  81. duration: 1000
  82. })
  83. _this.getLocation();
  84. //再次授权,调用wx.getLocation的API
  85. } else {
  86. wx.showToast({
  87. title: '授权失败',
  88. icon: 'none',
  89. duration: 1000
  90. })
  91. }
  92. }
  93. })
  94. }
  95. }
  96. })
  97. return res.authSetting['scope.userLocation']
  98. } else if (res.authSetting['scope.userLocation'] == undefined) {
  99. //调用wx.getLocation的API
  100. this.getLocation();
  101. } else {
  102. //调用wx.getLocation的API
  103. this.getLocation();
  104. }
  105. }
  106. })
  107. },
  108. //授权后调用百度地图接口获取当前城市
  109. getLocation: function () {
  110. // 微信获得经纬度
  111. let _this = this;
  112. wx.getLocation({
  113. type: 'wgs84', //gcj02 // wgs84
  114. success: function (res) {
  115. let latitude = res.latitude
  116. let longitude = res.longitude
  117. let speed = res.speed
  118. let accuracy = res.accuracy
  119. },
  120. fail(err) {
  121. }
  122. })
  123. },
  124. getParaName(url) {
  125. if (!url || url.indexOf('?') == -1) {
  126. return
  127. }
  128. let paraname = url.split('?')[1]
  129. return this.setObject(paraname) //封装成对象
  130. },
  131. setObject(paraArr) {
  132. let obj = {}
  133. let arr1 = paraArr.split('&')
  134. arr1.forEach(item => {
  135. let str = item.split('=')
  136. let key = str[0]
  137. let val = str[1]
  138. obj[key] = val
  139. })
  140. return obj
  141. },
  142. async isLocal() {
  143. let that = this
  144. return await new Promise((resolve, reject) => {
  145. wx.getSetting({
  146. success: function (res) {
  147. let localtion = res.authSetting['scope.userLocation']
  148. if (!localtion) {
  149. that.getUserLocation()
  150. resolve(localtion)
  151. } else {
  152. resolve(localtion)
  153. }
  154. }
  155. })
  156. })
  157. },
  158. async onChange(event, id) {
  159. let currentIndex = event.detail;
  160. console.log(event);
  161. // let currentIndex = event.currentTarget.dataset.index
  162. let currentComp = this.selectComponent("#comp" + String(currentIndex));
  163. let tab = this.data.tabbarList[currentIndex];
  164. if (tab && tab.auth) { // auth设置有值时,校验登录
  165. let userLogin = wx.getStorageSync("userLogin");
  166. if (userLogin == "") {
  167. login.loginNow('/nova-tourism/pages/app-auth/index');
  168. return;
  169. }
  170. }
  171. if (currentIndex == 0) {
  172. currentComp.getData()
  173. }
  174. if (currentIndex == 1) {
  175. currentComp.setData({
  176. showTab: true
  177. })
  178. currentComp.getLocation()
  179. currentComp.getData()
  180. currentComp.getShopStore()
  181. }
  182. if (currentIndex == 2) {
  183. currentComp.setData({
  184. showTab: true,
  185. searchVal: ''
  186. })
  187. // currentComp.getRooms()/**!!!! */
  188. }
  189. if (currentIndex == 3) {
  190. currentComp.setData({
  191. goods: []
  192. })
  193. // currentComp.getmy()/**!!!! */
  194. // currentComp.getBindStore()/****** */
  195. // currentComp.getCategory()/****** */
  196. currentComp.getGoods()
  197. }
  198. if (currentIndex == 4) {
  199. // currentComp.getmy() /**!!!!! */
  200. }
  201. wx.pageScrollTo({ // 滚动至页面顶部
  202. scrollTop: 0,
  203. });
  204. this.setData({ // 切换Tab参数
  205. active: currentIndex,
  206. });
  207. },
  208. /**
  209. * 生命周期函数--监听页面初次渲染完成
  210. */
  211. onReady: async function () { },
  212. /**
  213. * 生命周期函数--监听页面显示
  214. */
  215. onShow: async function () {
  216. let component = this.selectComponent("#comp" + this.data.active);
  217. console.log(component);
  218. switch (Number(this.data.active)) {
  219. // case 0:
  220. // component.refresh()
  221. // break;
  222. // case 1:
  223. // component.refresh()
  224. // break;
  225. case 3:
  226. component.setData({
  227. goods: []
  228. })
  229. component.refresh()
  230. break;
  231. // default:
  232. // break;
  233. }
  234. },
  235. /**
  236. * 生命周期函数--监听页面隐藏
  237. */
  238. onHide: function () {
  239. },
  240. /**
  241. * 生命周期函数--监听页面卸载
  242. */
  243. onUnload: function () {
  244. },
  245. /**
  246. * 页面相关事件处理函数--监听用户下拉动作传递给子组件
  247. */
  248. onPullDownRefresh: function () {
  249. // 根据tab当前index,查询组件并触发组件内下拉刷新函数
  250. let comp = this.selectComponent("#comp" + this.data.active);
  251. if (comp) {
  252. comp.onPullDownRefresh && comp.onPullDownRefresh();
  253. }
  254. return
  255. },
  256. /**
  257. * 页面上拉触底事件的处理函数
  258. */
  259. onReachBottom: function () {
  260. },
  261. /**
  262. * 用户点击右上角分享
  263. */
  264. onShareAppMessage: function () {
  265. return {
  266. // title: '畅游山坡',
  267. }
  268. },
  269. onShareTimeline: async function (res) {
  270. await this.getTask('share')
  271. return {
  272. title: '转发到朋友圈',
  273. query: '我是携带的参数'
  274. }
  275. },
  276. //获取底部安全距离
  277. getScreenHeight() {
  278. let that = this
  279. wx.getSystemInfo({
  280. success: (res) => {
  281. let {
  282. screenHeight,
  283. safeArea
  284. } = res
  285. let botHeight = screenHeight - safeArea.bottom
  286. that.setData({
  287. botHeight
  288. })
  289. wx.setStorageSync('botHeight', botHeight);
  290. },
  291. fail: () => { },
  292. complete: () => { }
  293. });
  294. },
  295. async getTask(type) {
  296. let userid = Parse.User.current().id
  297. let Task = new Parse.Query('Task')
  298. Task.equalTo('company', company)
  299. Task.equalTo('taskType', type)
  300. Task.equalTo('isOpen', true)
  301. let task = await Task.first()
  302. if (task && task.id) {
  303. let start = new Date(new Date(new Date().toLocaleDateString()).getTime())
  304. let TaskLog = new Parse.Query('TaskLog')
  305. TaskLog.equalTo('user', userid)
  306. TaskLog.equalTo('task', task.id)
  307. TaskLog.equalTo('company', company)
  308. if (task.type == 'daily') {
  309. TaskLog.greaterThan('createdAt', start)
  310. }
  311. let log = await TaskLog.first()
  312. if (log && log.id) {
  313. return
  314. } else {
  315. this.createdLog(userid, task.id, task.get('credit'))
  316. }
  317. }
  318. },
  319. async createdLog(uid, tid, credit) {
  320. let TaskLog = Parse.Object.extend('TaskLog')
  321. let tasklog = new TaskLog()
  322. tasklog.set('user', {
  323. __type: "Pointer",
  324. className: '_User',
  325. objectId: uid
  326. })
  327. tasklog.set('task', {
  328. __type: "Pointer",
  329. className: 'Task',
  330. objectId: tid
  331. })
  332. tasklog.set('company', {
  333. __type: "Pointer",
  334. className: 'Company',
  335. objectId: company
  336. })
  337. tasklog.set('isReceive', false)
  338. tasklog.set('credit', credit)
  339. await tasklog.save()
  340. },
  341. })