index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. const Parse = getApp().Parse;
  2. const company = getApp().globalData.company
  3. import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. realName: '',
  10. mobile: '',
  11. address: '',
  12. id: '',
  13. order: [],
  14. goods: [],
  15. show: false,
  16. activeColor:''
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: async function (options) {
  22. this.setData({
  23. activeColor:getApp().globalData.activeColor || '#229293'
  24. })
  25. let id = options.id
  26. this.setData({
  27. id
  28. })
  29. await this.getOrder()
  30. await this.getGoods()
  31. },
  32. // 根据订单id获取当前订单
  33. getOrder: async function () {
  34. let Order = new Parse.Query('Order')
  35. let order = await Order.get(this.data.id)
  36. let orderJSON = order.toJSON()
  37. orderJSON.totalPrice = parseFloat(orderJSON.totalPrice).toFixed(2)
  38. orderJSON.totalPrice = parseFloat(orderJSON.totalPrice)
  39. let { info } = orderJSON
  40. if (info) {
  41. let { name, mobile, address } = info
  42. this.setData({
  43. realName: name,
  44. mobile: mobile,
  45. address: address
  46. })
  47. }
  48. this.setData({
  49. order: orderJSON,
  50. })
  51. },
  52. // 根据订单中specMap字段获取商品数据
  53. getGoods: async function () {
  54. let { specMap } = this.data.order
  55. let list = []
  56. for (let k in specMap) {
  57. let goods = await this.getShopGoods(k)
  58. if (goods.total < specMap[k] && this.data.order.status == '100') {
  59. wx.showToast({
  60. title: goods.name + '商品库存不足, 已自动调整购买数量',
  61. icon: 'none',
  62. duration: 1500,
  63. });
  64. goods.count = goods.total
  65. } else if (!goods.total && this.data.order.status == '100') {
  66. return wx.showToast({
  67. title: goods.name + '商品库存不足, 已自动移出订单',
  68. icon: 'none',
  69. duration: 1500,
  70. });
  71. } else {
  72. goods.count = specMap[k]
  73. }
  74. goods.price = parseFloat(goods.price).toFixed(2)
  75. goods.originalPrice = parseFloat(goods.originalPrice).toFixed(2)
  76. list.push(goods)
  77. }
  78. this.setData({
  79. goods: list
  80. })
  81. },
  82. // 获取ShopGoods商品数据
  83. getShopGoods: async function (ID) {
  84. let ShopGoods = new Parse.Query('ShopGoods')
  85. let shopGoods = await ShopGoods.get(ID)
  86. let shopGoodsJSON = shopGoods.toJSON()
  87. return shopGoodsJSON
  88. },
  89. // 调起支付弹窗
  90. payMoney() {
  91. let reg_user = /^[\u4e00-\u9fa5]{2,4}$/;
  92. let reg_tel = /^((\+|00)86)?1((3[\d])|(4[5,6,7,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[\d])|(9[1,8,9]))\d{8}$/
  93. if (!reg_tel.test(this.data.mobile)) {
  94. return wx.showToast({
  95. title: '请正确填写手机号码!',
  96. icon: 'none'
  97. })
  98. }
  99. if (!reg_user.test(this.data.realName)) {
  100. return wx.showToast({
  101. title: '请正确填写姓名!',
  102. icon: 'none'
  103. })
  104. }
  105. if (!this.data.realName || !this.data.mobile || !this.data.address) {
  106. return wx.showToast({
  107. title: '请检查配送信息填写,不得为空!',
  108. icon: 'none',
  109. });
  110. }
  111. let money = this.data.order.totalPrice
  112. this.setData({
  113. tradeNo:this.data.order.orderNum,
  114. show: true,
  115. money: money
  116. })
  117. },
  118. // 支付当前订单
  119. acceptResult: async function (e) {
  120. // 同时储存一条AccountLog记录
  121. let user = Parse.User.current()
  122. let userJSON = user.toJSON()
  123. // 1. 查询Account账号
  124. let Account = new Parse.Query('Account')
  125. Account.equalTo('company', company)
  126. Account.equalTo('user', userJSON.objectId)
  127. let account = await Account.first()
  128. // 获取当前订单的Parse对象
  129. let Order = new Parse.Query('Order')
  130. let order = await Order.get(this.data.id)
  131. let {
  132. params,
  133. no
  134. } = e.detail;
  135. this.setData({
  136. show: false
  137. })
  138. try {
  139. if (params == "ok") {
  140. // 支付完成后将订单状态改变
  141. order.set('info', {
  142. 'name': this.data.realName,
  143. 'mobile': this.data.mobile,
  144. 'address': this.data.address
  145. })
  146. order.set("status", '200')
  147. order.set("isPay", true)
  148. order.save().then(() => {
  149. // 减去购买量的店铺商品库存
  150. let { specMap } = this.data.order
  151. for (let k in specMap) {
  152. this.subGoodsStock(k, specMap[k])
  153. }
  154. let AccountJSON = account.toJSON()
  155. let AccountLog = Parse.Object.extend('AccountLog')
  156. let accountLog = new AccountLog()
  157. accountLog.set('fromAccount', {
  158. '__type': 'Pointer',
  159. 'className': 'Account',
  160. 'objectId': AccountJSON.objectId
  161. })
  162. accountLog.set('orderType', 'shopgoods-balance')
  163. accountLog.set('assetType', 'balance')
  164. accountLog.set('desc', '余额支付')
  165. accountLog.set('fromName', userJSON.nickname)
  166. accountLog.set('orderNumber', this.data.order.orderNum)
  167. accountLog.set('targetName', 'system')
  168. accountLog.set('company', {
  169. '__type': 'Pointer',
  170. 'className': 'Company',
  171. 'objectId': company
  172. })
  173. accountLog.set('assetCount', this.data.order.totalPrice)
  174. accountLog.set('isVerified', false)
  175. accountLog.save().then((res) => {
  176. res.set('isVerified', true)
  177. res.save().then(() => {
  178. // 发送打印订单请求
  179. this.printOrder()
  180. // 返回上一级页面
  181. setTimeout(() => {
  182. wx.navigateBack({
  183. delta: 1,
  184. });
  185. }, 300)
  186. // 刷新数据
  187. const Page = getCurrentPages();
  188. const prePage = Page[Page.length - 2]
  189. let store = prePage.selectComponent("#comp3")
  190. store.data.goods = []
  191. store.getmy()
  192. store.getBindStore()
  193. store.getCategory()
  194. store.getGoods()
  195. })
  196. }).catch(error => {
  197. wx.showToast({
  198. title: "支付失败",
  199. icon: "error",
  200. duration: 1500,
  201. });
  202. wx.hideLoading()
  203. })
  204. })
  205. } else {
  206. wx.showToast({
  207. title: '支付失败',
  208. icon: 'none',
  209. image: '',
  210. duration: 1500,
  211. mask: false,
  212. });
  213. }
  214. } catch (error) {
  215. console.log(error)
  216. wx.showToast({
  217. title: "支付失败",
  218. icon: "error",
  219. duration: 1500,
  220. });
  221. wx.hideLoading()
  222. }
  223. },
  224. // 打印订单的处理函数
  225. printOrder: async function() {
  226. // 获取打印设备终端编号
  227. let code = ''
  228. let query = new Parse.Query('PrintDevice')
  229. query.equalTo('company', '1AiWpTEDH9')
  230. let result = await query.first()
  231. if(result && result.id) {
  232. code = result.get('code')
  233. }
  234. // 处理订单商品数组
  235. let goods = this.data.goods
  236. let orders = []
  237. goods.forEach(val => {
  238. let item = {
  239. name: val.name,
  240. num: val.count,
  241. price: val.price
  242. }
  243. orders.push(item)
  244. })
  245. // 定义打印订单所需数据数组
  246. let data = {
  247. dev_code : code,
  248. cid: company,
  249. order_id: this.data.order.objectId,
  250. orderNumber: this.data.order.orderNum,
  251. price: this.data.order.totalPrice,
  252. orders: orders,
  253. name: this.data.realName,
  254. mobile: this.data.mobile,
  255. address: this.data.address,
  256. }
  257. console.log(data);
  258. // 发送打印请求
  259. wx.request({
  260. url: 'https://test.fmode.cn/api/print-dev/dev',
  261. data: data,
  262. header: {'content-type':'application/json'},
  263. method: 'POST',
  264. dataType: 'json',
  265. responseType: 'text',
  266. success: (result)=>{
  267. console.log('----订单打印成功----');
  268. },
  269. fail: ()=>{},
  270. complete: ()=>{}
  271. });
  272. },
  273. // 订单支付完成后减去当前店铺商品的库存
  274. subGoodsStock: async function (ID, num) {
  275. let ShopGoods = new Parse.Query('ShopGoods')
  276. let shopGoods = await ShopGoods.get(ID)
  277. let shopGoodsJSON = shopGoods.toJSON()
  278. shopGoods.set('total', shopGoodsJSON.total - num)
  279. shopGoods.set('sales', shopGoodsJSON.sales + num)
  280. shopGoods.save().then((res) => {
  281. })
  282. },
  283. // 订单退款
  284. refund: async function () {
  285. wx.showModal({
  286. title: '发起退款',
  287. content: '确认对该笔订单发起退款吗',
  288. showCancel: true,
  289. cancelText: '取消',
  290. cancelColor: '#000000',
  291. confirmText: '确定',
  292. confirmColor: '#fe4a4a',
  293. success: async (result) => {
  294. if (result.confirm) {
  295. // 将订单状态改变为 500
  296. let Order = new Parse.Query('Order')
  297. let order = await Order.get(this.data.order.objectId)
  298. order.set('status', '500')
  299. order.save().then(() => {
  300. Toast.loading({
  301. message: '提交申请...',
  302. forbidClick: true,
  303. onClose: () => {
  304. this.setData({
  305. order: []
  306. })
  307. this.getOrder()
  308. }
  309. })
  310. })
  311. }
  312. },
  313. });
  314. },
  315. /**
  316. * 生命周期函数--监听页面初次渲染完成
  317. */
  318. onReady: function () {
  319. },
  320. /**
  321. * 生命周期函数--监听页面显示
  322. */
  323. onShow: function () {
  324. },
  325. /**
  326. * 生命周期函数--监听页面隐藏
  327. */
  328. onHide: function () {
  329. },
  330. /**
  331. * 生命周期函数--监听页面卸载
  332. */
  333. onUnload: function () {
  334. },
  335. /**
  336. * 页面相关事件处理函数--监听用户下拉动作
  337. */
  338. onPullDownRefresh: function () {
  339. },
  340. /**
  341. * 页面上拉触底事件的处理函数
  342. */
  343. onReachBottom: function () {
  344. },
  345. /**
  346. * 用户点击右上角分享
  347. */
  348. onShareAppMessage: function () {
  349. }
  350. })