quantify.js 7.1 KB

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