index.js 12 KB

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