order.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. import {
  2. get,
  3. post
  4. } from '../../utils/http'
  5. const app = getApp()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. current: 0,
  12. list: [],
  13. total: 0,
  14. page: 1,
  15. pay_methon: 1,
  16. payNow: false,
  17. showImg: '',
  18. showLandScape: false,
  19. orderStr: ['未付款', '待使用', '进行中', '已结算', '已取消'],
  20. },
  21. onCloseLandscape: function () {
  22. this.setData({
  23. showLandScape: false
  24. })
  25. },
  26. pay_methonw() {
  27. this.setData({
  28. pay_methon: 1
  29. })
  30. },
  31. pay_methonc() {
  32. this.setData({
  33. pay_methon: 2
  34. })
  35. },
  36. /**
  37. * 生命周期函数--监听页面加载
  38. */
  39. onLoad: function (options) {
  40. if (options.current) {
  41. this.setData({
  42. current: options.current
  43. })
  44. }
  45. let lat1 = app.address.latitude
  46. let lng1 = app.address.longitude
  47. this.setData({
  48. lat1: lat1,
  49. lng1: lng1,
  50. })
  51. },
  52. /**
  53. * 生命周期函数--监听页面初次渲染完成
  54. */
  55. onReady: function () {},
  56. /**
  57. * 生命周期函数--监听页面显示
  58. */
  59. onShow: function () {
  60. this.getAmount()
  61. this.getOrder(1)
  62. },
  63. /**
  64. * 生命周期函数--监听页面隐藏
  65. */
  66. onHide: function () {},
  67. /**
  68. * 生命周期函数--监听页面卸载
  69. */
  70. onUnload: function () {},
  71. /**
  72. * 页面相关事件处理函数--监听用户下拉动作
  73. */
  74. onPullDownRefresh: function () {},
  75. /**
  76. * 页面上拉触底事件的处理函数
  77. */
  78. onReachBottom: function () {
  79. if (this.data.page * 10 < this.data.total) {
  80. this.getOrder(++this.data.page)
  81. }
  82. },
  83. /**
  84. * 用户点击右上角分享
  85. */
  86. onShareAppMessage: function () {},
  87. /**
  88. * 切换tabs
  89. */
  90. onTabsChange(e) {
  91. let current = e.currentTarget.dataset.id
  92. if (current == this.data.current) {
  93. return
  94. }
  95. this.setData({
  96. current,
  97. page: 1
  98. },
  99. () => {
  100. this.getOrder()
  101. }
  102. )
  103. },
  104. /**
  105. * 取消订单
  106. */
  107. onCancelOrder(e) {
  108. let that = this
  109. wx.showModal({
  110. title: '提示',
  111. content: '是否取消当前预约',
  112. success(res) {
  113. if (res.confirm) {
  114. console.log('用户点击确定')
  115. post(
  116. 'v2/api/order/cancel', {
  117. id: e.currentTarget.dataset.id
  118. },
  119. (res) => {
  120. wx.showToast({
  121. title: res.msg,
  122. icon: 'success',
  123. duration: 2000
  124. })
  125. that.getOrder(1)
  126. }
  127. )
  128. } else if (res.cancel) {
  129. console.log('用户点击取消')
  130. }
  131. }
  132. })
  133. },
  134. oneMore(e) {
  135. let current = e.currentTarget.dataset.id
  136. let price = e.currentTarget.dataset.price
  137. this.getOrderData(current, price)
  138. },
  139. /**
  140. * 获取订单详情
  141. */
  142. getOrderData(current, price) {
  143. get('v2/api/order/info', {
  144. id: current
  145. }, (res) => {
  146. let orderDetails = res.data.project_list
  147. for (let i = 0; i < orderDetails.length; i++) {
  148. if (orderDetails[i].product_name) {
  149. orderDetails[i].selectMaterial = {}
  150. orderDetails[i].selectMaterial.name = orderDetails[i].product_name
  151. orderDetails[i].selectMaterial.id = orderDetails[i].product_id
  152. }
  153. }
  154. console.log(orderDetails)
  155. wx.redirectTo({
  156. url: '../reserveCar/reserveCar?data=' + JSON.stringify(orderDetails) + '&&money=' + price + '&&type=project',
  157. })
  158. })
  159. },
  160. // 点击立即支付
  161. payNow: function (e) {
  162. let id = e.currentTarget.dataset.id
  163. },
  164. // 点击了美导师
  165. beautyImg: function (e) {
  166. let img = e.currentTarget.dataset.img
  167. this.setData({
  168. showImg: img,
  169. showLandScape: true
  170. })
  171. },
  172. onPay: function (e) {
  173. this.setData({
  174. order_id: e.currentTarget.dataset.id,
  175. payMoney: e.currentTarget.dataset.money,
  176. payNow: true
  177. })
  178. },
  179. /**
  180. * 支付
  181. */
  182. payNow() {
  183. let that = this
  184. that.methonPayOrder()
  185. },
  186. onClosePay() {
  187. this.setData({
  188. payNow: false
  189. })
  190. },
  191. /**
  192. * 去支付
  193. */
  194. methonPayOrder() {
  195. let that = this
  196. post(
  197. 'api/pay/project', {
  198. order_id: this.data.order_id,
  199. pay_way: this.data.pay_methon == 1 ? 'weixin' : 'amount',
  200. },
  201. (res) => {
  202. that.setData({
  203. payNow: false
  204. })
  205. if (that.data.pay_methon == 2) {
  206. setTimeout(() => {
  207. that.setData({
  208. current: 1
  209. }, () => {
  210. that.getOrder(1)
  211. that.getAmount()
  212. })
  213. }, 1000)
  214. get('api/message/ids', {}, (res) => {
  215. let ids = res.data.ids
  216. that.onSubmitCh(ids)
  217. })
  218. return
  219. }
  220. wx.requestPayment({
  221. timeStamp: res.data.pay_data.timeStamp,
  222. nonceStr: res.data.pay_data.nonceStr,
  223. package: res.data.pay_data.package,
  224. signType: res.data.pay_data.signType,
  225. paySign: res.data.pay_data.paySign,
  226. success(res) {
  227. if (res.errMsg == 'requestPayment:ok') {
  228. wx.showToast({
  229. title: '支付成功',
  230. icon: 'success'
  231. })
  232. setTimeout(() => {
  233. that.setData({
  234. current: 1
  235. }, () => {
  236. that.getOrder(1)
  237. })
  238. }, 1000)
  239. get('api/message/ids', {}, (res) => {
  240. let ids = res.data.ids
  241. wx.requestSubscribeMessage({
  242. tmplIds: ids,
  243. success(res) {
  244. console.log(res)
  245. that.messageAgree(ids, e.currentTarget.dataset.id)
  246. },
  247. fail(rej) {
  248. console.log(rej)
  249. that.messageAgree(ids)
  250. wx.showToast({
  251. title: JSON.stringify(rej),
  252. icon: 'error'
  253. })
  254. }
  255. })
  256. })
  257. }
  258. },
  259. fail(res) {
  260. wx.showToast({
  261. title: '支付失败',
  262. icon: 'error'
  263. })
  264. }
  265. })
  266. }
  267. )
  268. },
  269. onSubmitCh(ids) {
  270. let that = this
  271. wx.showModal({
  272. title: '提示',
  273. content: '支付成功! 是否进行消息提醒',
  274. success: function (res) {
  275. if (res.confirm) {
  276. that.onSubmitTrue(ids)
  277. } else if (res.cancel) {
  278. that.messageAgree(ids, this.data.order_id)
  279. }
  280. }
  281. });
  282. },
  283. /**
  284. * 获取我的储值
  285. * api/user
  286. */
  287. getAmount() {
  288. get('api/user/amount', {}, (res) => {
  289. if (res.data) {
  290. this.setData({
  291. inserllAmount: res.data.amount
  292. })
  293. }
  294. })
  295. },
  296. onSubmitTrue(ids) {
  297. let that = this
  298. wx.requestSubscribeMessage({
  299. tmplIds: ids,
  300. success(res) {
  301. console.log(res)
  302. that.messageAgree(ids, this.data.order_id)
  303. },
  304. fail(rej) {
  305. console.log(rej)
  306. that.messageAgree(ids, this.data.order_id)
  307. wx.showToast({
  308. title: JSON.stringify(rej),
  309. icon: 'error'
  310. })
  311. }
  312. })
  313. },
  314. /**
  315. * 获取订单列表
  316. * api/order
  317. * 订单分类,1已预约,2待使用,3进行中,4已结算,5已取消
  318. */
  319. getOrder(_page) {
  320. let {
  321. list,
  322. current,
  323. page
  324. } = this.data
  325. get(
  326. 'v2/api/order/list', {
  327. type: Number(current) + 1,
  328. page: _page || page,
  329. limit: 10
  330. },
  331. (res) => {
  332. if (_page == 1 || page == 1) {
  333. list = []
  334. this.data.page = 1
  335. }
  336. list.push(...res.data.list)
  337. this.setData({
  338. list,
  339. total: res.data.total
  340. })
  341. }
  342. )
  343. },
  344. /**
  345. * 用户允许消息通知
  346. * /api/message/agree
  347. */
  348. messageAgree(ids, order_id) {
  349. post(
  350. 'api/message/agree', {
  351. order_id,
  352. ids
  353. },
  354. () => {}
  355. )
  356. }
  357. })