quantify.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. stationName:'',//油站名
  15. fastInputs: [10, 20, 50, 100, 200, 500],
  16. selectIndex:-1,
  17. type: [{
  18. name: '金额',
  19. checked: true
  20. },
  21. {
  22. name: '升数',
  23. checked: false
  24. }
  25. ],
  26. inputValue: '',
  27. inputTip: '请输入金额',
  28. amount: '',
  29. latitude: 0, //用户当前经度
  30. longitude: 0, //用户当前纬度
  31. stationLatitude: 0, //站点经度
  32. stationLongitude: 0, //站点纬度
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad(options) {
  38. const link = decodeURIComponent(options.q) // 获取到二维码原始链接
  39. var id = link.split('yuwxapp?id=')[1];
  40. if (id == undefined) {
  41. id = options.id;
  42. }
  43. this.setData({
  44. nozzleId: 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 name = res.data.data.site.name;
  62. this.setData({
  63. stationName:name,
  64. })
  65. const stationLocation = res.data.data.site.gpsCoordinates.split(",");
  66. if (stationLocation.length == 2) {
  67. this.setData({
  68. stationLatitude: stationLocation[0],
  69. stationLongitude: stationLocation[1]
  70. })
  71. }
  72. this.getLocation();
  73. })
  74. },
  75. /** 获取用户经纬度 */
  76. getLocation() {
  77. const that = this;
  78. wx.getLocation({
  79. type: 'wgs84',
  80. isHighAccuracy:true,
  81. highAccuracyExpireTime:5000,
  82. success(res) {
  83. console.log("获取经纬度结果", res)
  84. that.setData({
  85. latitude: res.latitude,
  86. longitude: res.longitude
  87. })
  88. }
  89. })
  90. this.getNozzleInfo();
  91. },
  92. /** 获取油枪信息 */
  93. getNozzleInfo() {
  94. const that = this;
  95. api.request_GetNozzleInfo(this.data.nozzleId).then(res => {
  96. console.log("获取油枪信息", res)
  97. if (res.data.content.length != 0) {
  98. var nozzle = res.data.content[0];
  99. nozzle.productPrice = utils.formatDiNumber(nozzle.productPrice);
  100. that.setData({
  101. nozzleInfo: nozzle
  102. });
  103. } else {
  104. console.log('获取油枪信息失败,无内容返回');
  105. }
  106. }).catch(err => {
  107. console.log('获取油枪信息接口请求失败', err);
  108. });
  109. },
  110. /** 输入框监听 */
  111. onInputChange(event) {
  112. console.log("输入框变化", event)
  113. let value = event.detail.value;
  114. // 1. 过滤非法字符(只允许数字和小数点)
  115. value = value.replace(/[^\d.]/g, "");
  116. // 2. 限制小数点只能出现一次
  117. const dotIndex = value.indexOf(".");
  118. if (dotIndex !== -1) {
  119. value = value.substring(0, dotIndex + 1) + value.substring(dotIndex).replace(/\./g, "");
  120. }
  121. // 3. 限制小数点后最多两位
  122. if (dotIndex !== -1 && value.length > dotIndex + 3) {
  123. value = value.substring(0, dotIndex + 3);
  124. }
  125. // 4. 更新输入框值
  126. this.setData({ inputValue: value });
  127. },
  128. /**
  129. * 处理快速输入金额的点击事件
  130. */
  131. onFastInputClick(event) {
  132. console.log("点击", event)
  133. const value = event.currentTarget.dataset.id;
  134. const index = event.currentTarget.dataset.index;
  135. this.setData({
  136. inputValue: value,
  137. selectIndex:index
  138. });
  139. },
  140. /** 授权 */
  141. toAuthorization() {
  142. if(this.data.inputValue == '') {
  143. wx.showToast({
  144. title: '请输入定量值',
  145. icon:'none'
  146. })
  147. return
  148. }
  149. wx.showLoading({
  150. title: '下单中',
  151. })
  152. console.log('进入 toAuthorization 方法');
  153. console.log(this.data)
  154. const distance = utils.haversine(this.data.stationLatitude,this.data.stationLongitude,this.data.latitude,this.data.longitude);
  155. if(distance > 500) {
  156. wx.showToast({
  157. title: '您不在油站范围内,请到油站时进行下单',
  158. })
  159. return
  160. }
  161. this.createOrder();
  162. },
  163. /** 创建订单 */
  164. createOrder() {
  165. const nozzle = this.data.nozzleInfo;
  166. if (!nozzle) {
  167. wx.hideLoading()
  168. wx.showToast({
  169. title: '油枪信息未获取',
  170. icon: 'error',
  171. });
  172. return;
  173. }
  174. const value = parseFloat(this.data.inputValue);
  175. console.log("要授权的油枪信息", nozzle, value);
  176. if (this.data.quantify == '元' && (value < 2.00 || value > 9900.00)) {
  177. wx.showToast({
  178. title: '金额须在2~9900之间',
  179. });
  180. return;
  181. }
  182. const price = parseFloat(nozzle.productPrice);
  183. if (this.data.quantify == '升' && (value < 1.00 || (value * price) > 9900.00)) {
  184. wx.showToast({
  185. title: '升数不合法',
  186. });
  187. return;
  188. }
  189. var requestData = null;
  190. if (this.data.quantify == '元') {
  191. requestData = {
  192. externalGunNumber: nozzle.externalGunNumber,
  193. originalAmount: value,
  194. product: nozzle.productName,
  195. price: parseFloat(nozzle.productPrice),
  196. transactionNumber: ''
  197. };
  198. } else {
  199. requestData = {
  200. externalGunNumber: nozzle.externalGunNumber,
  201. qty: value,
  202. product: nozzle.productName,
  203. price: nozzle.productPrice,
  204. transactionNumber: ''
  205. };
  206. }
  207. api.request_createOrder(requestData).then(res => {
  208. console.log("创建订单", res);
  209. if (res.data.statusCode == 200) {
  210. utils.subAndsendMessage(res.data.data.id, "下单").then(response => {
  211. this.toPayPage(res);
  212. }).catch(err => {
  213. this.toPayPage(res);
  214. });
  215. } else {
  216. wx.showToast({
  217. icon: "none",
  218. title: '授权失败',
  219. });
  220. }
  221. }).catch(err => {
  222. console.log("创建订单失败", err);
  223. wx.showToast({
  224. icon: "none",
  225. title: '授权失败',
  226. });
  227. });
  228. },
  229. toPayPage(res) {
  230. const that = this;
  231. const order = res.data.data;
  232. const time = utils.formatDateNotSecond(order.createTime);
  233. setTimeout(() => {
  234. wx.navigateTo({
  235. url: '../AuthorizationTransactionPage/AuthorizationTransactionPage?nozzleId=' + this.data.nozzleId,
  236. success: function (res) {
  237. res.eventChannel.emit('acceptDataFromQuantify', {
  238. oilName: order.productName,
  239. nozzle: order.nozzleId,
  240. volume: order.qty,
  241. amount: order.originalAmount,
  242. payAmount: order.actualPaymentAmount,
  243. discount: order.originalAmount - order.actualPaymentAmount,
  244. orderId: order.id,
  245. time: time,
  246. price:order.price,
  247. station:that.data.stationName
  248. })
  249. }
  250. })
  251. }, 500);
  252. wx.hideLoading()
  253. },
  254. /**
  255. * 生命周期函数--监听页面初次渲染完成
  256. */
  257. onReady() {
  258. },
  259. /**
  260. * 生命周期函数--监听页面显示
  261. */
  262. onShow() {
  263. console.log("页面显示")
  264. this.getStationData();
  265. },
  266. /**
  267. * 生命周期函数--监听页面隐藏
  268. */
  269. onHide() {
  270. },
  271. /**
  272. * 生命周期函数--监听页面卸载
  273. */
  274. onUnload() {
  275. },
  276. /**
  277. * 页面相关事件处理函数--监听用户下拉动作
  278. */
  279. onPullDownRefresh() {
  280. },
  281. /**
  282. * 页面上拉触底事件的处理函数
  283. */
  284. onReachBottom() {
  285. },
  286. /**
  287. * 用户点击右上角分享
  288. */
  289. onShareAppMessage() {
  290. },
  291. /**
  292. * 当选择定量类型
  293. */
  294. onTypeChange(event) {
  295. var type = event.detail.value;
  296. if (type == '升数') {
  297. this.setData({
  298. quantify: '升',
  299. inputTip: '请输入升数'
  300. });
  301. } else {
  302. this.setData({
  303. quantify: '元',
  304. inputTip: '请输入金额'
  305. });
  306. }
  307. }
  308. });