index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. let Parse = getApp().Parse;
  2. const company = getApp().globalData.company
  3. const util = require('../../../../../utils/util.js')
  4. const dateF = require('../../../../../utils/date')
  5. const req = require('../../../../../utils/request')
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. time: null,
  12. store: null,
  13. merchant: null,
  14. totalCount: 0,
  15. todayCount: 0,
  16. totalPrice: 0,
  17. todayPrice: 0,
  18. uid: null,
  19. profit: 0, //分润
  20. isShowPayouts: false
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. this.setData({
  27. uid: wx.getStorageSync('merchant')?.objectId
  28. })
  29. console.log(options)
  30. var TIME = util.formatTime(new Date()); //当前时间
  31. console.log(TIME);
  32. let time = dateF.formatTime("YYYY-mm-dd", TIME)
  33. this.setData({
  34. time: time
  35. });
  36. let merchant = wx.getStorageSync('merchant'); //用户
  37. this.setData({
  38. merchant: merchant
  39. })
  40. if (!merchant) {
  41. wx.redirectTo({
  42. url: '/nova-tourism/pages/my/merchant/login/index'
  43. });
  44. }
  45. this.refersh()
  46. },
  47. async refersh() {
  48. await this.getShopStore()
  49. if (!this.data.store?.objectId) return
  50. await this.getShopOrder(this.data.store?.objectId)
  51. await this.getTodayOrder(this.data.store?.objectId)
  52. this.getProfit()
  53. },
  54. /**获取店铺 */
  55. async getShopStore() {
  56. let ShopStore = new Parse.Query('ShopStore')
  57. ShopStore.equalTo('company', company)
  58. ShopStore.equalTo('user', this.data.merchant.objectId)
  59. let shopStore = await ShopStore.first()
  60. if (shopStore && shopStore.id) {
  61. let id = shopStore.toJSON().objectId
  62. await this.setData({
  63. store: shopStore.toJSON()
  64. })
  65. } else {
  66. wx.setStorageSync('store', undefined)
  67. wx.setStorageSync('merchant', undefined)
  68. }
  69. },
  70. /**提现弹框-开 */
  71. async openPayouts() {
  72. await this.refersh()
  73. this.setData({
  74. isShowPayouts: true
  75. })
  76. },
  77. /**提现弹框-关 */
  78. closePayouts() {
  79. this.setData({
  80. isShowPayouts: false
  81. })
  82. },
  83. /**获取当前商户分润金额 */
  84. async getProfit() {
  85. let store_uid = wx.getStorageSync('merchant')?.objectId
  86. if (!store_uid) return
  87. let sql = `WITH t1 AS(--当前商户所绑定的用户
  88. SELECT u."objectId" FROM "_User" u
  89. LEFT JOIN "ShopStore" s ON s."objectId" = u."store"
  90. WHERE s."user" = '${store_uid}'--当前店长uid
  91. ),
  92. room_o AS (
  93. SELECT SUM((spec.value::json->>'invite-1')::FLOAT)
  94. FROM "RoomOrder" o,
  95. jsonb_each(o."incomeMap") AS spec (key, value)
  96. WHERE o."company"='${company}'
  97. AND o."isDeleted" IS NOT TRUE
  98. AND o."isPay" IS TRUE
  99. AND o."user" IN (SELECT "objectId" FROM t1)
  100. ),
  101. order_o AS (
  102. SELECT SUM((spec.value::json->>'invite-1')::FLOAT)
  103. FROM "Order" o,
  104. jsonb_each(o."incomeMap") AS spec (key, value)
  105. WHERE o."company"='${company}'
  106. AND o."isDeleted" IS NOT TRUE
  107. AND o."isPay" IS TRUE
  108. AND o."user" IN (SELECT "objectId" FROM t1)
  109. ),
  110. sum_t2 AS (SELECT * FROM room_o UNION ALL SELECT * FROM order_o)
  111. SELECT SUM(sum_t2."sum") FROM sum_t2`
  112. let data = await req.customSQL(sql)
  113. // console.log(sql)
  114. // console.log(data)
  115. let profit = data[0]?.sum || 0
  116. await this.setData({
  117. profit
  118. })
  119. this.getWithdraw()
  120. },
  121. /**
  122. * 分润可提现金额 = 获取当前商户分润金额-申请提现金额
  123. * 营业额可提现金额 = 总营业额*0.7-申请提现金额
  124. */
  125. async getWithdraw() {
  126. let sql = `select sum("count") as "withdraw" from "UserAgentWithdraw"
  127. where "company" = '${company}' and "user" = '${this.data.merchant.objectId}'
  128. and "isDeleted" is not true
  129. and "channel" = 'sharing'
  130. group by "user"`
  131. let sql02 = `select sum("count") as "withdraw" from "UserAgentWithdraw"
  132. where "company" = '${company}' and "user" = '${this.data.merchant.objectId}'
  133. and "isDeleted" is not true
  134. and "channel" = 'turnover'
  135. group by "user"`
  136. let data = await req.customSQL(sql)
  137. let data02 = await req.customSQL(sql02)
  138. console.log(data, data02)
  139. console.log(this.data.profit)
  140. let overSharing= data[0]?.withdraw || 0
  141. let overTurnover = data02[0]?.withdraw||0
  142. this.setData({
  143. sharing: (Math.floor(this.data.profit - overSharing * 100) / 100)?.toFixed(2),
  144. overSharing,
  145. turnover: (Math.floor((this.data.payoutPrice - overTurnover ) * 100) / 100)?.toFixed(2),
  146. overTurnover,
  147. // earnings: (Math.floor((this.data.profit - data[0].withdraw + payoutPrice) * 100) / 100)?.toFixed(2)
  148. })
  149. // console.log(this.data.earnings)
  150. },
  151. /**获取累计订单数据 */
  152. async getShopOrder(id) {
  153. /**民宿订单sql */
  154. let sql = ''
  155. if (this.data.store.type == 'stay') {
  156. sql = `select count("objectId") as "count", sum("price") as "totalPrice" from "RoomOrder"
  157. where "company" = '${company}' and "shopStore" = '${id}' and "isPay" = true and "isDeleted" is not true
  158. group by "shopStore"`
  159. } else {
  160. sql = `select count("objectId") as "count", sum("totalPrice") as "totalPrice" from "ShopOrder"
  161. where "company" = '${company}' and "shopStore" = '${id}' and "status" <> 100
  162. group by "shopStore"`
  163. }
  164. let data = await req.customSQL(sql)
  165. let gift_sql = `select count("objectId") as "count", sum("totalPrice") as "totalPrice" from "Order"
  166. where "company" = '${company}' and "store" = '${id}' and "isPay" = true and "isDeleted" is not true
  167. group by "store"`
  168. let gift_data = await req.customSQL(gift_sql)
  169. let totalCount = parseFloat(data[0]?.count || 0) + parseFloat(gift_data[0]?.count || 0)
  170. let totalPrice = ((parseFloat(data[0]?.totalPrice || 0) * 1000) + (parseFloat(gift_data[0]?.totalPrice || 0) * 1000)) / 1000
  171. this.setData({
  172. totalCount: totalCount || 0,
  173. totalPrice: totalPrice || 0,
  174. payoutPrice: Math.floor(totalPrice * 0.7 * 100) / 100,
  175. room_count: parseFloat(data[0]?.count || 0),
  176. room_totalPrice: parseFloat(data[0]?.totalPrice || 0),
  177. gift_count: parseFloat(gift_data[0]?.count || 0),
  178. gift_totalPrice: parseFloat(gift_data[0]?.totalPrice || 0),
  179. })
  180. },
  181. /** 获取今日订单数据*/
  182. async getTodayOrder(id) {
  183. var TIME = util.formatTime(new Date()); //当前时间
  184. console.log(TIME);
  185. let time = dateF.formatTime("YYYY-mm-dd 00:00:10", TIME)
  186. console.log(time);
  187. let sql = ''
  188. if (this.data.store.type == 'stay') {
  189. sql = `select count("objectId") as "count", sum("price") as "totalPrice" from "RoomOrder"
  190. where "company" = '${company}' and "shopStore" = '${id}' and "isPay" = true and "createdAt" > '${time}'
  191. group by "shopStore"`
  192. } else {
  193. sql = `select count("objectId") as "count", sum("price") as "totalPrice" from "ShopOrder"
  194. where "company" = '${company}' and "shopStore" = '${id}' and "status" <> 100 and "createdAt" > '${time}'
  195. group by "shopStore"`
  196. }
  197. let data = await req.customSQL(sql)
  198. let gift_sql = `select count("objectId") as "count", sum("totalPrice") as "totalPrice" from "Order"
  199. where "company" = '${company}' and "store" = '${id}' and "isPay" = true and "createdAt" > '${time}' and "isDeleted" is not true
  200. group by "store"`
  201. // console.log(gift_sql)
  202. let gift_data = await req.customSQL(gift_sql)
  203. let todayCount = parseFloat(data[0]?.count || 0) + parseFloat(gift_data[0]?.count || 0)
  204. let todayPrice = ((parseFloat(data[0]?.totalPrice || 0) * 1000) + (parseFloat(gift_data[0]?.totalPrice || 0) * 1000)) / 1000
  205. this.setData({
  206. todayCount: todayCount || 0,
  207. todayPrice: todayPrice || 0,
  208. room_todayCount: parseFloat(data[0]?.count || 0),
  209. room_todayPrice: parseFloat(data[0]?.totalPrice || 0),
  210. gift_todayCount: parseFloat(gift_data[0]?.count || 0),
  211. gift_todayPrice: parseFloat(gift_data[0]?.totalPrice || 0),
  212. })
  213. },
  214. // orderlist() {
  215. // let storeId = this.data.store.objectId
  216. // wx.navigateTo({
  217. // url: '../merchant-home/order-list/index?storeId=' + storeId
  218. // });
  219. // },
  220. //民宿
  221. order2list() {
  222. let storeId = this.data.store.objectId
  223. wx.navigateTo({
  224. url: '../merchant-home/order2-list/index?storeId=' + storeId
  225. });
  226. },
  227. giftOrder2list() {
  228. let storeId = this.data.store.objectId
  229. wx.navigateTo({
  230. url: '../merchant-home/gift-order/index?storeId=' + storeId
  231. });
  232. },
  233. // order3list() {
  234. // let storeId = this.data.store.objectId
  235. // wx.navigateTo({
  236. // url: '../merchant-home/order3-list/index?storeId=' + storeId
  237. // });
  238. // },
  239. // goods() {
  240. // let id = this.data.store.objectId
  241. // wx.navigateTo({
  242. // url: '/nova-tourism/pages/my/merchant/merchant-home/goods/index?id=' + id
  243. // });
  244. // },
  245. logout() {
  246. wx.showModal({
  247. title: '确认要退出吗?',
  248. showCancel: true,
  249. success(res) {
  250. if (res.confirm) {
  251. wx.removeStorageSync('merchant')
  252. wx.removeStorageSync('store')
  253. wx.navigateTo({
  254. url: '../../../../pages/index/index'
  255. })
  256. } else {
  257. }
  258. }
  259. })
  260. },
  261. package() {
  262. wx.navigateTo({
  263. url: '/nova-tourism/pages/my/merchant/merchant-home/package/index'
  264. });
  265. },
  266. dishes() {
  267. let id = this.data.store.objectId
  268. wx.navigateTo({
  269. url: '/nova-tourism/pages/my/merchant/merchant-home/dishes/index?id=' + id
  270. });
  271. },
  272. tables() {
  273. let id = this.data.store.objectId
  274. wx.navigateTo({
  275. url: '/nova-tourism/pages/my/merchant/merchant-home/tables/index?id=' + id
  276. });
  277. },
  278. // category() {
  279. // let id = this.data.store.objectId
  280. // let type = this.data.store.type
  281. // wx.navigateTo({
  282. // url: `/nova-tourism/pages/my/merchant/merchant-home/category/index?id=${id}&&type=${type}`
  283. // });
  284. // },
  285. goodmanage() {
  286. wx.navigateTo({
  287. url: '../good/good-list/index'
  288. });
  289. },
  290. roommanage() {
  291. wx.navigateTo({
  292. url: '../room-manage/index'
  293. });
  294. },
  295. account() {
  296. wx.navigateTo({
  297. url: '../merchant-home/account/index'
  298. });
  299. },
  300. comments() {
  301. switch (this.data.store.type) {
  302. case 'stay':
  303. wx.navigateTo({
  304. url: '../../merchant/comments/hotel/index'
  305. });
  306. break;
  307. case 'catering':
  308. wx.navigateTo({
  309. url: '/nova-tourism/pages/my/merchant/comments/setmeal/index'
  310. });
  311. break;
  312. case 'shop':
  313. wx.navigateTo({
  314. url: '/nova-tourism/pages/my/merchant/comments/setmeal/index'
  315. });
  316. break;
  317. default:
  318. break;
  319. }
  320. },
  321. async codeVerify() {
  322. console.log(this.data.store.type);
  323. let that = this
  324. wx.scanCode({
  325. success(res) {
  326. let content = res.result
  327. console.log(content)
  328. let code1 = content || content.match(/\id=(.*)/)[1]
  329. console.log(code1);
  330. switch (that.data.store.type) {
  331. case 'stay':
  332. wx.navigateTo({
  333. url: `../code-verify/index?id=${code1}`
  334. });
  335. break;
  336. case 'catering':
  337. wx.navigateTo({
  338. url: `/nova-tourism/pages/shop-admin/index?id=${code1}`
  339. });
  340. break;
  341. case 'shop':
  342. wx.navigateTo({
  343. url: `/nova-tourism/pages/shop-admin/index?id=${code1}`
  344. });
  345. break;
  346. default:
  347. break;
  348. }
  349. // wx.navigateTo({
  350. // url: `/nova-tourism/pages/my/merchant/code-verify/index?id=${res.id}`
  351. // });
  352. },
  353. fail(err) {
  354. console.log(err)
  355. wx.showToast({
  356. title: '扫码失败,请确认二维码信息正确',
  357. icon: 'none'
  358. })
  359. }
  360. })
  361. },
  362. toWithdraw(e) {
  363. let {price,channel} = e.currentTarget.dataset
  364. let id = this.data.store.storeName
  365. wx.navigateTo({
  366. url: `../../merchant/merchant-home/store-withdraw/index?earnings=${price||0}&&channel=${channel}&&id=${id}`,
  367. })
  368. },
  369. gourl(e) {
  370. const url = e.currentTarget.dataset.url;
  371. wx.navigateTo({
  372. url: `${url}`,
  373. });
  374. },
  375. /**
  376. * 生命周期函数--监听页面初次渲染完成
  377. */
  378. onReady: function () {
  379. },
  380. /**
  381. * 生命周期函数--监听页面显示
  382. */
  383. onShow: function () {
  384. // this.getShopStore()
  385. this.refersh()
  386. },
  387. /**
  388. * 生命周期函数--监听页面隐藏
  389. */
  390. onHide: function () {
  391. },
  392. /**
  393. * 生命周期函数--监听页面卸载
  394. */
  395. onUnload: function () {
  396. },
  397. /**
  398. * 页面相关事件处理函数--监听用户下拉动作
  399. */
  400. onPullDownRefresh: function () {
  401. },
  402. /**
  403. * 页面上拉触底事件的处理函数
  404. */
  405. onReachBottom: function () {
  406. },
  407. /**
  408. * 用户点击右上角分享
  409. */
  410. onShareAppMessage: function () {
  411. }
  412. })