index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // nova-tourism/pages/collect/collect-detail/index.js
  2. let Parse = getApp().Parse;
  3. const company = getApp().globalData.company
  4. const uid = Parse.User.current()?.id
  5. let sev = require('../../../service/request')
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. statusBarHeight: 0,
  12. screenHeight: 0,
  13. customHeight: 0,
  14. bottomNavHeight: 0,
  15. contentHeight: 0,
  16. imageUrls: ['https://img95.699pic.com/photo/60072/5330.jpg_wh860.jpg',
  17. 'https://c-ssl.dtstatic.com/uploads/blog/201409/27/20140927230814_4QfVC.thumb.1000_0.jpeg',
  18. 'https://ts1.cn.mm.bing.net/th/id/R-C.37f4077f8eaca918401b84b4ee23af7c?rik=7HYWC88a9q4sZw&riu=http%3a%2f%2fpic.616pic.com%2fphotoone%2f00%2f06%2f03%2f618e27ad7ce8e1124.jpg&ehk=jkc6iZWAg%2bAEjAk6%2br0VAAy1lfffqslT6n%2fMWl293Yc%3d&risl=&pid=ImgRaw&r=0'
  19. ],
  20. gid: null, //商品id
  21. good: null, //商品详情
  22. spec: null, //商品规格
  23. checkSpec: null, //选中的规格
  24. totalPrice: 0, //订单总价
  25. specBox: false, //规格弹框
  26. address: null, //收货地址
  27. tradeNo: null, //订单编号
  28. order: null, //当前订单-parse
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad: function (options) {
  34. const systemInfo = wx.getSystemInfoSync();
  35. const statusBarHeight = systemInfo.statusBarHeight || 0;
  36. const screenHeight = systemInfo.screenHeight || 0;
  37. const custom = wx.getMenuButtonBoundingClientRect();
  38. const customHeight = custom.height + 10 + 2 || 0;
  39. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  40. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  41. this.setData({
  42. statusBarHeight,
  43. screenHeight,
  44. customHeight,
  45. bottomNavHeight,
  46. contentHeight
  47. });
  48. this.setData({
  49. gid: options?.gid
  50. })
  51. this.refersh()
  52. },
  53. async refersh() {
  54. this.getGood()
  55. },
  56. /**获取商品 */
  57. async getGood() {
  58. let {
  59. gid
  60. } = this.data
  61. let query = new Parse.Query('ShopGoods')
  62. let d = await query.get(gid)
  63. let good = d?.toJSON()
  64. console.log(good)
  65. let spec = good?.specMap?.specList.map(item => {
  66. let price = good.specMap[item][0].price
  67. let subName = price || good.price
  68. let obj = {
  69. name: item,
  70. subname: `售价¥${subName}`,
  71. color: 'gray',
  72. price: subName
  73. }
  74. return obj
  75. })
  76. let checkSpec = {}
  77. if (spec?.length) {
  78. checkSpec = spec[0]
  79. spec[0].color = 'red'
  80. }
  81. let totalPrice = checkSpec.price || good.price
  82. this.setData({
  83. good,
  84. spec,
  85. checkSpec,
  86. totalPrice
  87. })
  88. },
  89. /** 打开规格弹框*/
  90. openSpec() {
  91. let {spec} = this.data
  92. console.log(spec,'‘hhh')
  93. if(spec?.length<=0)return
  94. this.setData({
  95. specBox: true
  96. })
  97. },
  98. /**关闭规格弹框 */
  99. closeSpec() {
  100. this.setData({
  101. specBox: false
  102. })
  103. },
  104. /**选择规格 */
  105. getCheckSpec(e) {
  106. let {
  107. spec,
  108. checkSpec,
  109. totalPrice
  110. } = this.data
  111. checkSpec = e.detail
  112. spec = spec.map(item => {
  113. item.color = 'gray'
  114. if (item.name == checkSpec.name) {
  115. item.color = 'red'
  116. totalPrice = item.price
  117. }
  118. return item
  119. })
  120. this.setData({
  121. spec,
  122. checkSpec,
  123. totalPrice
  124. })
  125. },
  126. /** 跳转*/
  127. tourl(e) {
  128. const url = e.currentTarget.dataset.url
  129. wx.navigateTo({
  130. url: `${url}` // 目标页面的路径
  131. });
  132. },
  133. /** 判断是否可创建订单*/
  134. judgeOrder() {
  135. let {
  136. address
  137. } = this.data
  138. if (!address?.objectId) {
  139. wx.showToast({
  140. title: '请确认收货地址',
  141. icon: 'none',
  142. });
  143. return false
  144. }
  145. return true
  146. },
  147. /**创建订单 */
  148. async submitOrder() {
  149. let isPass = this.judgeOrder()
  150. if (!isPass) return
  151. let {
  152. good,
  153. totalPrice,
  154. checkSpec,
  155. address,
  156. order,
  157. tradeNo
  158. } = this.data
  159. let o = order
  160. if (!order?.id) {
  161. let now = new Date()
  162. tradeNo = "C" +
  163. String(now.getFullYear()) +
  164. (now.getMonth() + 1) +
  165. now.getDate() +
  166. now.getHours() +
  167. now.getMinutes() +
  168. now.getSeconds() +
  169. Math.random().toString().slice(-6);
  170. let Order = Parse.Object.extend("Order");
  171. o = new Order()
  172. o.set("company", {
  173. __type: 'Pointer',
  174. className: 'Company',
  175. objectId: company
  176. })
  177. o.set("user", {
  178. __type: 'Pointer',
  179. className: '_User',
  180. objectId: uid
  181. })
  182. o.set("orderNum", tradeNo);
  183. o.set('status', '100') //待支付
  184. o.set('type', 'scenery')
  185. o.set("targetObject", [{
  186. "__type": "Pointer",
  187. "className": "ShopGoods",
  188. "objectId": good?.objectId
  189. }])
  190. }
  191. o.set("totalPrice", totalPrice || 0);
  192. o.set("address", {
  193. __type: 'Pointer',
  194. className: 'ShopAddress',
  195. objectId: address?.objectId
  196. });
  197. o.set("info", {
  198. name: address?.name,
  199. mobile: address?.mobile,
  200. address: address?.full_region + address?.address
  201. });
  202. let specMap = {}
  203. specMap[good?.objectId] = {
  204. count: 1,
  205. price: good.price,
  206. spec: checkSpec.name
  207. }
  208. o.set("specMap", specMap);
  209. try {
  210. // order = await o.save()
  211. this.setData({
  212. tradeNo,
  213. order:o,
  214. showPay: true,
  215. })
  216. // wx.showToast({
  217. // title: '已创建订单',
  218. // icon: 'none'
  219. // })
  220. } catch (error) {
  221. wx.showToast({
  222. title: '出错了请检查后重试',
  223. icon: 'error'
  224. })
  225. }
  226. },
  227. /**支付回调 */
  228. async acceptResult(e) {
  229. let {
  230. order
  231. } = this.data
  232. let {
  233. params
  234. } = e.detail
  235. if (params == 'ok') {
  236. wx.showLoading({
  237. title: '加载中',
  238. })
  239. order.set('status', '200')
  240. order.set('payType', 'wx')
  241. order.set('isPay', true)
  242. try {
  243. await order.save()
  244. setTimeout(() => {
  245. wx.hideLoading()
  246. wx.redirectTo({
  247. url: '/nova-tourism/pages/my/my-order/index?active=0',
  248. })
  249. }, 1000);
  250. } catch (error) {
  251. wx.showToast({
  252. title: '保存订单出错,请联系客服核对订单',
  253. icon: 'none'
  254. })
  255. }
  256. } else {
  257. wx.showToast({
  258. title: '支付失败',
  259. icon: 'none'
  260. })
  261. }
  262. },
  263. /**
  264. * 生命周期函数--监听页面初次渲染完成
  265. */
  266. onReady: function () {
  267. },
  268. /**
  269. * 生命周期函数--监听页面显示
  270. */
  271. async onShow() {
  272. let address = await sev.getAddress()
  273. console.log(address)
  274. this.setData({
  275. address
  276. })
  277. },
  278. /**
  279. * 生命周期函数--监听页面隐藏
  280. */
  281. onHide: function () {
  282. },
  283. /**
  284. * 生命周期函数--监听页面卸载
  285. */
  286. onUnload: function () {
  287. },
  288. /**
  289. * 页面相关事件处理函数--监听用户下拉动作
  290. */
  291. onPullDownRefresh: function () {
  292. },
  293. /**
  294. * 页面上拉触底事件的处理函数
  295. */
  296. onReachBottom: function () {
  297. },
  298. /**
  299. * 用户点击右上角分享
  300. */
  301. onShareAppMessage: function () {
  302. }
  303. })