quantify.js 8.0 KB

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