order.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import orderApi from '../../api/order'
  2. import api from '../../utils/api';
  3. import drawQrcode from '../../utils/weapp.qrcode.js'
  4. // pages/order/order.js
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. orderId: 0,
  11. order: {},
  12. //1显示订单详情,2显示确认支付,取消订单,3显示删除订单
  13. orderStatus: 1,
  14. timer: {},
  15. codeUrl: '',
  16. percent:'1',
  17. color:'#10AEFF'
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: async function (options) {
  23. var orderId = options.id;
  24. this.setData({
  25. orderId: orderId
  26. })
  27. // let order = await orderApi.getOrderById(orderId);
  28. },
  29. //生成二维码
  30. createQrcode: function () {
  31. const self = this
  32. drawById.call(wx, "qrCode", {
  33. text: self.data.order.writeOffCode, // 二维码内容
  34. })
  35. },
  36. createTimeForCode: async function (){
  37. const self = this
  38. const order = this.data.order
  39. console.log(order.writeOffOverTime)
  40. let endTime = new Date(order.writeOffOverTime);
  41. console.log(endTime.getTime())
  42. var timerID = setInterval(async () => {
  43. let nowTime = new Date();
  44. let timer = endTime.getTime() - nowTime.getTime();
  45. self.setData({
  46. percent: 100 - (timer/1200),
  47. })
  48. if (timer < 60000 && timer > 1 && self.data.color !== '#f52e46') {
  49. self.setData({
  50. color:"#f52e46"
  51. })
  52. }
  53. if (timer < 1) {
  54. clearInterval(timerID);
  55. await orderApi.refreshCode({orderId:order.id})
  56. await self.orderInfo()
  57. self.setData({
  58. color:'#10AEFF'
  59. })
  60. }
  61. },1000)
  62. },
  63. createTimeClock: function () {
  64. var self = this;
  65. let order = this.data.order;
  66. let orderId = this.data.orderId;
  67. console.log(order.createTime)
  68. let orderCreateTime = new Date(order.createTime);
  69. let endTime = orderCreateTime.getTime() + 15 * 60 * 1000;
  70. // let endTime = orderCreateTime.getTime() + 30 * 1000;
  71. var timerID = setInterval(() => {
  72. let nowTime = new Date();
  73. let timer = endTime - nowTime;
  74. if (timer < 1) {
  75. clearInterval(timerID);
  76. orderApi.closeOrder(orderId);
  77. order.status = 3;
  78. self.setData({
  79. order
  80. })
  81. }
  82. let minute = Math.floor(timer / (60 * 1000));
  83. minute = minute < 0 ? 0 : minute;
  84. let second = Math.round((timer % (60 * 1000)) / 1000);
  85. this.setData({
  86. timer: {
  87. minute: minute,
  88. second: second
  89. }
  90. })
  91. }, 500);
  92. },
  93. cancelOrder: function () {
  94. let orderId = this.data.orderId;
  95. let order = this.data.order;
  96. var self = this;
  97. wx.showModal({
  98. title: '是否确认取消订单?',
  99. success(res) {
  100. if (res.confirm) {
  101. orderApi.closeOrder(orderId);
  102. order.status = 3;
  103. self.setData({
  104. order
  105. })
  106. } else if (res.cancel) {}
  107. },
  108. })
  109. },
  110. deleteOrder: async function () {
  111. let id = this.data.order.id;
  112. await orderApi.deleteOrder(id);
  113. wx.showToast({
  114. title: '删除成功',
  115. success: function () {
  116. setTimeout(() => {
  117. wx.switchTab({
  118. url: '../personorder/personorder',
  119. })
  120. }, 1000);
  121. }
  122. })
  123. },
  124. drawCode: function (writeOffCode) {
  125. let width = this.rpxToPx(280);
  126. drawQrcode({
  127. width: width,
  128. height: width,
  129. canvasId: 'qrCode',
  130. text: writeOffCode,
  131. })
  132. },
  133. /**
  134. * 生命周期函数--监听页面初次渲染完成
  135. */
  136. onReady: function () {
  137. },
  138. /**
  139. * 生命周期函数--监听页面显示
  140. */
  141. onShow: async function () {
  142. await this.orderInfo()
  143. },
  144. orderInfo: async function(){
  145. var orderId = this.data.orderId;
  146. let order = await orderApi.getOrderById(orderId);
  147. this.setData({
  148. order: order.info,
  149. orderStatus: order.info.status,
  150. })
  151. if (order.info.status == 1) {
  152. this.createTimeClock()
  153. }
  154. if (order.info.status == 2 && order.info.writeOffStatus == 1) {
  155. this.drawCode(order.info.writeOffCode)
  156. await this.createTimeForCode()
  157. }
  158. },
  159. rpxToPx(data) {
  160. return data / 750 * wx.getSystemInfoSync().windowWidth
  161. },
  162. /**
  163. * 生命周期函数--监听页面隐藏
  164. */
  165. onHide: function () {
  166. },
  167. /**
  168. * 生命周期函数--监听页面卸载
  169. */
  170. onUnload: function () {
  171. },
  172. /**
  173. * 页面相关事件处理函数--监听用户下拉动作
  174. */
  175. onPullDownRefresh: function () {
  176. },
  177. /**
  178. * 页面上拉触底事件的处理函数
  179. */
  180. onReachBottom: function () {
  181. },
  182. /**
  183. * 用户点击右上角分享
  184. */
  185. onShareAppMessage: function () {
  186. },
  187. payOrder: async function(){
  188. const self = this
  189. let orderId = this.data.orderId
  190. const data = {
  191. orderId
  192. }
  193. const rs = await orderApi.payOrderAgain(data)
  194. wx.requestPayment({
  195. timeStamp: rs.timeStamp,
  196. nonceStr: rs.nonceStr,
  197. package: rs.package,
  198. signType: rs.signType,
  199. paySign: rs.paySign,
  200. success: async function (res) {
  201. console.log(res)
  202. await self.orderInfo()
  203. },
  204. fail: async function (res) {
  205. await self.orderInfo()
  206. }
  207. })
  208. },
  209. guide: function(e){
  210. const self = this;
  211. var lat = parseFloat(self.data.order.latitude);
  212. var lon = parseFloat(self.data.order.longitude);
  213. wx.getSetting({
  214. success: async (res) => {
  215. let authSetting = res.authSetting
  216. if (authSetting['scope.userLocation']) {
  217. console.log('已授权地理位置')
  218. wx.openLocation({
  219. latitude:lat,
  220. longitude: lon,
  221. })
  222. // 已授权
  223. } else {
  224. // 未授权
  225. console.log('未授权地理位置');
  226. wx.getLocation({
  227. type: 'wgs84',
  228. async success(res) {
  229. console.log(res)
  230. const latitude = res.latitude
  231. const longitude = res.longitude
  232. wx.setStorageSync('lon', longitude)
  233. wx.setStorageSync('lat', latitude)
  234. wx.openLocation({
  235. latitude:lat,
  236. longitude: lon,
  237. })
  238. }
  239. })
  240. }
  241. }
  242. })
  243. },
  244. call: function(e){
  245. let phone = this.data.order.mobile
  246. wx.makePhoneCall({
  247. phoneNumber: phone,
  248. })
  249. }
  250. })