quantify.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. const {
  2. default: api
  3. } = require("../../js/api");
  4. const utils = require('../../utils/util');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. nozzleId: '',
  11. nozzleInfo: null,
  12. quantify: '元',
  13. fastInputs: [10, 20, 50, 100, 200, 500],
  14. type: [{
  15. name: '金额',
  16. checked: true
  17. },
  18. {
  19. name: '升数',
  20. checked: false
  21. }
  22. ],
  23. inputValue: '',
  24. inputTip: '请输入金额',
  25. amount: '',
  26. latitude: 0, //用户当前经度
  27. longitude: 0, //用户当前纬度
  28. stationLatitude: 0, //站点经度
  29. stationLongitude: 0, //站点纬度
  30. },
  31. /**
  32. * 生命周期函数--监听页面加载
  33. */
  34. onLoad(options) {
  35. const link = decodeURIComponent(options.q) // 获取到二维码原始链接
  36. var id = link.split('yuwxapp?id=')[1];
  37. if (id == undefined) {
  38. id = options.id;
  39. }
  40. this.setData({
  41. nozzleId: id
  42. })
  43. wx.showToast({
  44. title: id,
  45. })
  46. },
  47. /** 获取站点信息 */
  48. getStationData() {
  49. console.log("获取站点")
  50. api.request_GetSiteInfo().then(res => {
  51. if (res.data.statusCode == 203) {
  52. //若为203,证明还未登录,跳转到登录页,这里可能刚从主页跳转过来,频繁的跳转可能会跳转页面超时,故而加上延时
  53. setTimeout(() => {
  54. wx.navigateTo({
  55. url: '../login/login'
  56. })
  57. }, 500)
  58. return
  59. }
  60. console.log("站点信息", res)
  61. const stationLocation = res.data.data.site.gpsCoordinates.split(",");
  62. if (stationLocation.length == 2) {
  63. this.setData({
  64. stationLatitude: stationLocation[0],
  65. stationLongitude: stationLocation[1]
  66. })
  67. }
  68. this.getLocation();
  69. })
  70. },
  71. /** 获取用户经纬度 */
  72. getLocation() {
  73. const that = this;
  74. wx.getLocation({
  75. type: 'wgs84',
  76. success(res) {
  77. console.log("获取经纬度结果", res)
  78. that.setData({
  79. latitude: res.latitude,
  80. longitude: res.longitude
  81. })
  82. }
  83. })
  84. this.getNozzleInfo();
  85. },
  86. /** 获取油枪信息 */
  87. getNozzleInfo() {
  88. const that = this;
  89. api.request_GetNozzleInfo(this.data.nozzleId).then(res => {
  90. console.log("获取油枪信息", res)
  91. if (res.data.content.length != 0) {
  92. var nozzle = res.data.content[0];
  93. nozzle.productPrice = utils.formatDiNumber(nozzle.productPrice);
  94. that.setData({
  95. nozzleInfo: nozzle
  96. })
  97. }
  98. })
  99. },
  100. /** 输入框监听 */
  101. onInputChange(event) {
  102. console.log("输入框变化", event)
  103. this.setData({
  104. inputValue: event.detail.value
  105. })
  106. },
  107. /**
  108. * 处理快速输入金额的点击事件
  109. */
  110. onFastInputClick(event) {
  111. console.log("点击", event)
  112. const amount = event.currentTarget.dataset.id;
  113. this.setData({
  114. inputValue: amount
  115. });
  116. },
  117. /** 授权 */
  118. toAuthorization() {
  119. console.log(this.data)
  120. // const distance = utils.haversine(this.data.stationLatitude,this.data.stationLongitude,this.data.latitude,this.data.longitude);
  121. // if(distance > 500) {
  122. // wx.showToast({
  123. // title: '您不在油站范围内,请到油站时进行下单',
  124. // })
  125. // return
  126. // }
  127. this.createOrder()
  128. },
  129. /** 创建订单 */
  130. createOrder() {
  131. const nozzle = this.data.nozzleInfo;
  132. const value = this.data.inputValue;
  133. console.log("要授权的油枪信息", nozzle, value)
  134. if(value <= 0){
  135. wx.showToast({
  136. title: '定量值非法',
  137. })
  138. return
  139. }
  140. var requestData = null
  141. if (this.data.quantify == '元') {
  142. requestData = {
  143. externalGunNumber: nozzle.externalGunNumber,
  144. originalAmount: value,
  145. product: nozzle.productName,
  146. price: parseFloat(nozzle.productPrice),
  147. transactionNumber: ''
  148. };
  149. } else {
  150. requestData = {
  151. externalGunNumber: nozzle.externalGunNumber,
  152. qty: value,
  153. product: nozzle.productName,
  154. price: nozzle.productPrice,
  155. transactionNumber: ''
  156. };
  157. }
  158. api.request_createOrder(requestData).then(res => {
  159. console.log("创建订单", res)
  160. if (res.data.statusCode != 200) {
  161. wx.showToast({
  162. icon: "none",
  163. title: res.data.message == undefined ? res.data.notification : res.data.message,
  164. })
  165. return
  166. }
  167. utils.subAndsendMessage(res.data.data.id, "下单").then(response => {
  168. this.toPayPage(res)
  169. }).catch(err => {
  170. this.toPayPage(res)
  171. });
  172. })
  173. },
  174. toPayPage(res) {
  175. const order = res.data.data;
  176. const time = utils.formatDateNotSecond(order.createTime)
  177. setTimeout(() => {
  178. wx.navigateTo({
  179. url: '../AuthorizationTransactionPage/AuthorizationTransactionPage?nozzleId=' + this.data.nozzleId,
  180. success: function (res) {
  181. res.eventChannel.emit('acceptDataFromQuantify', {
  182. oilName: order.productName,
  183. nozzle: order.nozzleId,
  184. volume: order.qty,
  185. amount: order.originalAmount,
  186. payAmount: order.actualPaymentAmount,
  187. discount: order.originalAmount - order.actualPaymentAmount,
  188. orderId: order.id,
  189. time: time
  190. })
  191. }
  192. })
  193. }, 500)
  194. },
  195. /**
  196. * 生命周期函数--监听页面初次渲染完成
  197. */
  198. onReady() {
  199. },
  200. /**
  201. * 生命周期函数--监听页面显示
  202. */
  203. onShow() {
  204. console.log("页面显示")
  205. this.getStationData();
  206. },
  207. /**
  208. * 生命周期函数--监听页面隐藏
  209. */
  210. onHide() {
  211. },
  212. /**
  213. * 生命周期函数--监听页面卸载
  214. */
  215. onUnload() {
  216. },
  217. /**
  218. * 页面相关事件处理函数--监听用户下拉动作
  219. */
  220. onPullDownRefresh() {
  221. },
  222. /**
  223. * 页面上拉触底事件的处理函数
  224. */
  225. onReachBottom() {
  226. },
  227. /**
  228. * 用户点击右上角分享
  229. */
  230. onShareAppMessage() {
  231. },
  232. /**
  233. * 当选择定量类型
  234. */
  235. onTypeChange(event) {
  236. var type = event.detail.value;
  237. if (type == '升数') {
  238. this.setData({
  239. quantify: '升',
  240. inputTip: '请输入升数'
  241. });
  242. } else {
  243. this.setData({
  244. quantify: '元',
  245. inputTip: '请输入金额'
  246. });
  247. }
  248. }
  249. })