index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. let Parse = getApp().Parse;
  2. const company = getApp().globalData.company
  3. const rechText = require('../../../../../../utils/rech-text');
  4. const login = require('../../../../../../utils/login')
  5. const dateF = require('../../../../../../utils/date')
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. id: "",
  12. goods: {},
  13. shopstore: {},
  14. number: 1,
  15. prices: null,
  16. gid: null,
  17. show: false,
  18. sid: null,
  19. oid: null
  20. },
  21. onChange(event) {
  22. let number = event.detail
  23. console.log(number);
  24. let prices = (this.data.goods.price * number)
  25. console.log(prices);
  26. this.setData({
  27. number: number,
  28. prices: prices
  29. })
  30. },
  31. async getorder() {
  32. let oid = this.data.oid
  33. let shopOrder
  34. let ShopOrder = new Parse.Query("ShopOrder")
  35. shopOrder = await ShopOrder.get(oid)
  36. shopOrder.set("status", 200)
  37. shopOrder.save().then(res => {
  38. console.log(res)
  39. })
  40. },
  41. submit() {
  42. let userLogin = wx.getStorageSync("userLogin");
  43. if (userLogin == "") {
  44. login.loginNow();
  45. return;
  46. }
  47. this.setData({
  48. show: true
  49. })
  50. let _this = this
  51. //生成订单号
  52. let now = new Date()
  53. let tradeNo = "C" +
  54. String(now.getFullYear()) +
  55. (now.getMonth() + 1) +
  56. now.getDate() +
  57. now.getHours() +
  58. now.getMinutes() +
  59. now.getSeconds() +
  60. Math.random().toString().slice(-6); //生成六位随机数
  61. this.setData({
  62. tradeNo: tradeNo
  63. })
  64. console.log(this.data.prices, this.data.shopstore.storeName, tradeNo, this.data.goods.name, this.data.gid, this.data.sid)
  65. let ShopOrder = Parse.Object.extend("ShopOrder")
  66. let shopOrder = new ShopOrder()
  67. shopOrder.set("orderNum", tradeNo)
  68. shopOrder.set("price", this.data.prices)
  69. shopOrder.set("type", 'meal')
  70. shopOrder.set("image", this.data.goods.image)
  71. shopOrder.set("status", 100)
  72. shopOrder.set("count", this.data.number)
  73. shopOrder.set("name", this.data.goods.name)
  74. shopOrder.set("goods", {
  75. __type: 'Pointer',
  76. className: 'ShopGoods',
  77. objectId: this.data.gid
  78. })
  79. shopOrder.set("shopStore", {
  80. __type: 'Pointer',
  81. className: 'ShopStore',
  82. objectId: this.data.sid
  83. })
  84. shopOrder.set("company", {
  85. __type: 'Pointer',
  86. className: 'Company',
  87. objectId: company
  88. })
  89. shopOrder.set("user", {
  90. __type: 'Pointer',
  91. className: '_User',
  92. objectId: Parse.User.current().id
  93. })
  94. shopOrder.save().then(res => {
  95. console.log(res)
  96. _this.setData({
  97. oid: res.id
  98. })
  99. })
  100. },
  101. async acceptResult(e) {
  102. let {
  103. activeOrder,
  104. tradeNo,
  105. oid
  106. } = this.data
  107. let that = this
  108. let {
  109. params,
  110. no
  111. } = e.detail;
  112. that.setData({
  113. show: false
  114. })
  115. try {
  116. if (params == "ok") {
  117. // activeOrder.set("status", 200)
  118. // activeOrder.set("isPay", true)
  119. await that.getorder()
  120. // await activeOrder.save()
  121. this.print_order(oid, tradeNo)
  122. wx.showToast({
  123. title: '购买成功',
  124. icon: 'none',
  125. image: '',
  126. duration: 1500,
  127. mask: false,
  128. });
  129. wx.navigateBack({
  130. delta: 2,
  131. })
  132. } else {
  133. wx.showToast({
  134. title: '支付失败,取消订单',
  135. icon: 'none',
  136. image: '',
  137. duration: 1500,
  138. mask: false,
  139. });
  140. }
  141. } catch (error) {
  142. console.log(error)
  143. wx.showToast({
  144. title: "支付失败",
  145. icon: "error",
  146. duration: 1500,
  147. });
  148. wx.hideLoading()
  149. }
  150. },
  151. //用户下单成功,打印订单
  152. async print_order(orderid, orderNumber) {
  153. let {
  154. prices,
  155. goods,
  156. sid
  157. } = this.data
  158. let orders = [{
  159. name: goods.name,
  160. num: 1,
  161. price: goods.price.toFixed(2)
  162. }]
  163. let servece = 0
  164. let mobile = '18866666666'
  165. let address = '堂食到店'
  166. let dev_code = await this.getPrint()
  167. let user = Parse.User.current()
  168. if(user.get('mobile')){
  169. mobile = user.get('mobile')
  170. }
  171. let data = {
  172. dev_code: dev_code,
  173. cid: company,
  174. order_id: orderid,
  175. orderNumber: orderNumber,
  176. price: prices,
  177. orders: orders,
  178. name: user.get('nickname'),
  179. mobile: mobile,
  180. address: address,
  181. servece: servece,
  182. desc: '用户下单',
  183. storeid:sid,
  184. }
  185. console.log(data);
  186. wx.request({
  187. url: 'https://test.fmode.cn/api/print-dev/dev',
  188. data: data,
  189. header: {
  190. 'content-type': 'application/json'
  191. },
  192. method: 'POST',
  193. dataType: 'json',
  194. responseType: 'text',
  195. success: (result) => {
  196. console.log(result);
  197. },
  198. fail: () => {},
  199. complete: () => {}
  200. });
  201. },
  202. //获取打印机设备
  203. async getPrint() {
  204. let query = new Parse.Query("PrintDevice")
  205. query.equalTo("company", company)
  206. let res = await query.first()
  207. if (res && res.id) {
  208. return res.get("code")
  209. }
  210. },
  211. async getShopgoods() {
  212. let id = this.data.id
  213. let query = new Parse.Query('ShopGoods')
  214. query.include('shopStore')
  215. let shopGoods = await query.get(id)
  216. let goods = shopGoods.toJSON()
  217. if (goods.details) {
  218. goods.details = rechText.formatRichText(goods.details)
  219. }
  220. let gid = shopGoods.id //套餐id
  221. let prices = goods.price * this.data.number
  222. this.setData({
  223. goods,
  224. prices,
  225. gid
  226. })
  227. this.getShopStore()
  228. },
  229. async getShopStore() {
  230. let storeId = this.data.goods.shopStore.objectId
  231. let ShopStore = new Parse.Query('ShopStore')
  232. let shopstore = await ShopStore.get(storeId)
  233. this.setData({
  234. shopstore: shopstore.toJSON(),
  235. sid: storeId
  236. })
  237. },
  238. taxi() {
  239. let latitude = this.data.shopstore.location.latitude;
  240. let longitude = this.data.shopstore.location.longitude;
  241. console.log(latitude, longitude);
  242. wx.getLocation({
  243. type: 'gcj02', //返回可以用于wx.openLocation的经纬度
  244. success() {
  245. wx.openLocation({
  246. latitude,
  247. longitude,
  248. scale: 18
  249. })
  250. }
  251. })
  252. },
  253. phone() {
  254. let phone = this.data.shopstore.mobile
  255. console.log(phone);
  256. wx.makePhoneCall({
  257. phoneNumber: phone
  258. })
  259. },
  260. /**
  261. * 生命周期函数--监听页面加载
  262. */
  263. onLoad: function (options) {
  264. this.setData({
  265. activeColor:getApp().globalData.activeColor || '#229293'
  266. })
  267. let id = options.id
  268. let events = {
  269. detail: 0
  270. }
  271. this.setData({
  272. id: id,
  273. events: events
  274. })
  275. this.getShopgoods()
  276. },
  277. /**
  278. * 生命周期函数--监听页面初次渲染完成
  279. */
  280. onReady: function () {
  281. },
  282. /**
  283. * 生命周期函数--监听页面显示
  284. */
  285. onShow: function () {
  286. },
  287. /**
  288. * 生命周期函数--监听页面隐藏
  289. */
  290. onHide: function () {
  291. },
  292. /**
  293. * 生命周期函数--监听页面卸载
  294. */
  295. onUnload: function () {
  296. },
  297. /**
  298. * 页面相关事件处理函数--监听用户下拉动作
  299. */
  300. onPullDownRefresh: function () {
  301. },
  302. /**
  303. * 页面上拉触底事件的处理函数
  304. */
  305. onReachBottom: function () {
  306. },
  307. /**
  308. * 用户点击右上角分享
  309. */
  310. onShareAppMessage: function () {
  311. }
  312. })