create.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // pages/order/index.js
  2. import Toast from '@vant/weapp/toast/toast';
  3. import {
  4. isDemo,
  5. stationId
  6. } from '../../setting'
  7. import {
  8. getOilGunList,
  9. postOrderInfo,
  10. saveAppUserInfo,
  11. getMpOpenId,
  12. decryptEncryptedData,
  13. getCustomerGradeInfo,
  14. getStationPicList,
  15. getStationNoticeInfo
  16. } from "../../api/home"
  17. const app = getApp();
  18. Page({
  19. /**
  20. * 页面的初始数据
  21. */
  22. data: {
  23. showPopup: false,
  24. images: [
  25. 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2684369054,623698254&fm=26&gp=0.jpg',
  26. 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2684369054,623698254&fm=26&gp=0.jpg',
  27. 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2684369054,623698254&fm=26&gp=0.jpg',
  28. ],
  29. gasarr: [],
  30. gasallarr: [],
  31. money: [100, 200, 300, 400],
  32. gas: 0,
  33. account: '',
  34. showModal: false, //是否弹出登录弹窗
  35. showModalPhone: false, //是否弹出手机号绑定弹窗
  36. isShowUserInfoLayer: false, //是否手弹出获取用户信息弹窗
  37. userInfo: {},
  38. phoneNumber: null,
  39. hasUserInfo: false,
  40. hasPhoneNumber: false,
  41. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  42. iconSize: [20, 30, 40, 50, 60, 70],
  43. iconColor: [
  44. 'red', 'orange', 'yellow', 'green', 'rgb(0,255,255)', 'blue', 'purple'
  45. ],
  46. iconType: [
  47. 'success', 'success_no_circle', 'info', 'warn', 'waiting', 'cancel', 'download', 'search', 'clear'
  48. ],
  49. notice: ""
  50. },
  51. /**
  52. * 生命周期函数--监听页面加载
  53. */
  54. onLoad: function (options) {
  55. this.init()
  56. var userInfoValue = wx.getStorageSync('userInfo')
  57. try {
  58. if (userInfoValue) {
  59. try {
  60. var mobileValue = wx.getStorageSync('mobilePhone')
  61. if (mobileValue) {
  62. // Do something with return value
  63. // console.log("本地缓存中的phoneNumber:" + mobileValue);
  64. } else {
  65. this.showDialogPhone(); //调用手机号登录弹窗
  66. }
  67. } catch (e) {
  68. // Do something when catch error
  69. }
  70. } else {
  71. // console.log("app.globalData中的userInfo:" + app.globalData.userInfo);
  72. this.showDialogLogin(); //调用登录弹窗
  73. }
  74. } catch (e) {
  75. // Do something when catch error
  76. }
  77. },
  78. init() {
  79. // 加载页面所需资源
  80. //获取轮播图
  81. getStationPicList({
  82. stationId: app.globalData.stationId
  83. }).then((res) => {
  84. this.setData({
  85. images: res.data.map((ele) => {
  86. return ele.url
  87. })
  88. })
  89. })
  90. //获取通知信息
  91. getStationNoticeInfo({
  92. stationId: app.globalData.stationId
  93. }).then((res) => {
  94. this.setData({
  95. notice: res.data.notice
  96. })
  97. })
  98. getOilGunList({
  99. stationId: app.globalData.stationId
  100. }).then(res => { // handle success
  101. // console.log(res.data);
  102. // res.data.length
  103. console.log(res)
  104. if (res.retCode == 0) {
  105. var gasArr = [];
  106. for (let i = 0; i < res.data.length; i++) {
  107. gasArr.push(res.data[i]);
  108. }
  109. this.setData({
  110. gasallarr: res.data,
  111. gasarr: gasArr
  112. })
  113. app.globalData.stationName = res.data[0].stationName;
  114. }
  115. // console.log("gasArr lengh:"+gasArr.length);
  116. }).catch(error => { // handle error
  117. console.log(error);
  118. })
  119. },
  120. gup: function (name, url) {
  121. if (!url) url = location.href;
  122. name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  123. var regexS = "[\\?&]" + name + "=([^&#]*)";
  124. var regex = new RegExp(regexS);
  125. var results = regex.exec(url);
  126. return results == null ? null : results[1];
  127. },
  128. /**
  129. * 生命周期函数--监听页面显示
  130. */
  131. onShow: function () {},
  132. onClose() {
  133. this.setData({
  134. showPopup: false
  135. });
  136. },
  137. clickMore: function () {
  138. this.setData({
  139. showPopup: true
  140. });
  141. },
  142. selectGas: function (event) {
  143. let gunNo = event.target.dataset.index;
  144. let gunName = event.target.dataset.oilname;
  145. let oilPrice = event.target.dataset.oilprice;
  146. let gunType = event.target.dataset.guntype;
  147. app.oilGunNum = gunNo;
  148. app.oilGunName = gunName;
  149. app.oilPrice = oilPrice;
  150. app.gunType = gunType;
  151. // console.log("gunType="+event.target.dataset.guntype);
  152. this.setData({
  153. gas: event.target.dataset.index,
  154. showPopup: false
  155. });
  156. // console.log("gas is :" + event.target.dataset.index);
  157. },
  158. selectGasAll: function (event) {
  159. var gasarr = [];
  160. var arrLength = this.data.gasallarr.length;
  161. if (event.target.dataset.index > 5) {
  162. for (let index = event.target.dataset.index - 5; index <= event.target.dataset.index; index++) {
  163. gasarr.push(this.data.gasallarr[index]);
  164. }
  165. } else {
  166. for (let index = 0; index < arrLength; index++) {
  167. gasarr.push(this.data.gasallarr[index]);
  168. }
  169. }
  170. let gunNo = event.target.dataset.index;
  171. let gunName = event.target.dataset.oilname;
  172. let oilPrice = event.target.dataset.oilprice;
  173. let gunType = event.target.dataset.guntype;
  174. app.oilGunNum = gunNo;
  175. app.oilGunName = gunName;
  176. app.oilPrice = oilPrice;
  177. app.gunType = gunType;
  178. // console.log("选定的油枪号:" + event.target.dataset.num);
  179. // console.log("gasarr size:" + gasarr.length);
  180. this.setData({
  181. gasarr: gasarr,
  182. gas: event.target.dataset.num,
  183. showPopup: false
  184. });
  185. },
  186. clickAccount: function (event) {
  187. app.orderAmount = event.target.dataset.money;
  188. this.setData({
  189. account: event.target.dataset.money
  190. });
  191. // console.log("account is :" + event.target.dataset.money);
  192. },
  193. toastMsg: function () {
  194. if (this.data.gas <= 0) {
  195. Toast.fail('请选择油枪');
  196. } else if (this.data.account <= 0) {
  197. Toast.fail('请输入金额');
  198. }
  199. },
  200. toConfirm: function () {
  201. var userInfoValue = wx.getStorageSync('userInfo')
  202. try {
  203. if (userInfoValue) {
  204. // Do something with return value
  205. // console.log("本地缓存中的userInfo:" + userInfoValue);
  206. try {
  207. var mobileValue = wx.getStorageSync('mobilePhone')
  208. if (mobileValue) {
  209. // Do something with return value
  210. // console.log("本地缓存中的phoneNumber:" + mobileValue);
  211. let orderAmt = app.orderAmount;
  212. let oilPrice = app.oilPrice;
  213. let discountPrice = null; //优惠价格
  214. let gasoilDiscountAmount = null; //优惠金额-满减,立减,直降
  215. let discountAmount = null; //优惠起点金额
  216. let totalMoney = null;
  217. let youHuiAmount = null;
  218. let gunType = app.gunType;
  219. if (gunType === '1') { //油品订单
  220. console.log("**************计算优惠单开始****************");
  221. console.log(app)
  222. /**************计算优惠单开始****************/
  223. getCustomerGradeInfo({
  224. "openId": app.globalData.openId,
  225. "stationId": app.globalData.stationId,
  226. "userType": "2",
  227. "oilName": app.oilGunName,
  228. "mobilePhone": mobileValue
  229. }).then(res => { // handle success
  230. if (res.retCode == 0) { //获取优惠信息成功
  231. if (res.data.length > 0) {
  232. if ((res.data[0].discountWay === '直降')) { //优惠方式为等级直降
  233. discountPrice = res.data[0].gasoilDiscountLitre; //直降方案,按会员等级、按油品单价直降
  234. totalMoney = (orderAmt / oilPrice) * (oilPrice - discountPrice);
  235. // console.log("totalMonry1:"+totalMoney);
  236. totalMoney = totalMoney.toFixed(2);
  237. // console.log("totalMonry2:"+totalMoney);
  238. youHuiAmount = (orderAmt / oilPrice) * discountPrice;
  239. youHuiAmount = youHuiAmount.toFixed(2);
  240. } else if (res.data[0].discountWay === '1') { //优惠方式为满减
  241. gasoilDiscountAmount = Number(res.data[0].gasoilDiscountAmt); //满减方案,价格不变,总额满足条件优惠满减
  242. discountAmount = Number(res.data[0].discountAmt);
  243. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  244. if (orderAmt >= discountAmount) {
  245. let discountTimes = Math.floor(orderAmt / discountAmount);
  246. totalMoney = Number(orderAmt - (gasoilDiscountAmount * discountTimes));
  247. totalMoney = totalMoney.toFixed(2);
  248. youHuiAmount = Number(gasoilDiscountAmount * discountTimes);
  249. youHuiAmount = youHuiAmount.toFixed(2);
  250. } else {
  251. totalMoney = orderAmt;
  252. youHuiAmount = 0;
  253. }
  254. } else if (res.data[0].discountWay === '2') { //优惠方式为立减
  255. gasoilDiscountAmount = Number(res.data[0].gasoilDiscountAmt); //立减方案,价格不变,总额满足条件优惠立减
  256. discountAmount = Number(res.data[0].discountAmt);
  257. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  258. if (orderAmt >= discountAmount) {
  259. totalMoney = Number(orderAmt - gasoilDiscountAmount);
  260. totalMoney = totalMoney.toFixed(2);
  261. youHuiAmount = Number(gasoilDiscountAmount);
  262. youHuiAmount = youHuiAmount.toFixed(2);
  263. } else {
  264. totalMoney = orderAmt;
  265. youHuiAmount = 0;
  266. }
  267. } else if ((res.data[0].discountWay === '3')) { //优惠方式为独立直降
  268. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  269. if (res.data[0].discountTerm === '1') { //按总金额直降
  270. if (orderAmt >= res.data[0].discountAmt) {
  271. discountPrice = Number(res.data[0].gasoilDiscountAmt); //独立直降方案,按油品升数或者总价直降
  272. totalMoney = (orderAmt / oilPrice) * (oilPrice - discountPrice);
  273. // console.log("totalMonry1:"+totalMoney);
  274. totalMoney = totalMoney.toFixed(2);
  275. // console.log("totalMonry2:"+totalMoney);
  276. youHuiAmount = (orderAmt / oilPrice) * discountPrice;
  277. youHuiAmount = youHuiAmount.toFixed(2);
  278. } else {
  279. totalMoney = orderAmt;
  280. youHuiAmount = 0;
  281. }
  282. } else if (res.data[0].discountTerm === '2') { //按升数直降
  283. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  284. //取条件升数
  285. let dislQty = Number(res.data[0].discountAmt);
  286. //计算升数
  287. let lQty = orderAmt / oilPrice;
  288. lQty = lQty.toFixed(2);
  289. if (lQty >= dislQty) {
  290. totalMoney = (orderAmt / oilPrice) * (oilPrice - discountPrice);
  291. // console.log("totalMonry1:"+totalMoney);
  292. totalMoney = totalMoney.toFixed(2);
  293. // console.log("totalMonry2:"+totalMoney);
  294. youHuiAmount = (orderAmt / oilPrice) * discountPrice;
  295. youHuiAmount = youHuiAmount.toFixed(2);
  296. } else {
  297. totalMoney = orderAmt;
  298. youHuiAmount = 0;
  299. }
  300. } else {
  301. totalMoney = orderAmt;
  302. youHuiAmount = 0;
  303. }
  304. } else { //没有以上三种优惠方式
  305. totalMoney = orderAmt;
  306. youHuiAmount = 0;
  307. }
  308. } else { //没有查询到优惠信息,新用户没有优惠
  309. totalMoney = orderAmt;
  310. youHuiAmount = 0;
  311. }
  312. /**************计算优惠结束****************/
  313. //请求后台创建订单接口,获取订单号
  314. if (isDemo) {
  315. console.log("演示项目,已经为您修改实收金额");
  316. totalMoney = "0.01"
  317. }
  318. postOrderInfo({
  319. "stationId": app.globalData.stationId,
  320. "oilGun": app.oilGunNum,
  321. "oilName": app.oilGunName,
  322. "oilPrice": app.oilPrice,
  323. "receivableAmt": app.orderAmount, //应收金额
  324. "discountAmt": youHuiAmount, //优惠金额
  325. "amt": totalMoney, //实收金额
  326. "openId": app.globalData.openId,
  327. "userType": 2,
  328. "orderType": "1"
  329. }).then(res => { // handle success
  330. // console.log(res.data);
  331. if (res.retCode == 0) {
  332. app.orderNo = res.data;
  333. wx.navigateTo({
  334. url: "/pages/order/confirm"
  335. });
  336. } else {
  337. Toast.fail(res.message);
  338. }
  339. }).catch(error => { // handle error
  340. // console.log(error);
  341. Toast.fail(error);
  342. })
  343. } else {
  344. Toast.fail(res.message);
  345. }
  346. }).catch(error => { // handle error
  347. Toast.fail(error);
  348. })
  349. } else { //非油品订单
  350. totalMoney = orderAmt;
  351. youHuiAmount = 0;
  352. if (isDemo) {
  353. console.log("演示项目,已经为您修改实收金额");
  354. totalMoney = "0.01"
  355. }
  356. //请求后台创建订单接口,获取订单号
  357. postOrderInfo({
  358. "stationId": app.globalData.stationId,
  359. "oilGun": app.oilGunNum,
  360. "oilName": app.oilGunName,
  361. "oilPrice": app.oilPrice,
  362. "receivableAmt": app.orderAmount, //应收金额
  363. "discountAmt": youHuiAmount, //优惠金额
  364. "amt": totalMoney, //实收金额
  365. "openId": app.globalData.openId,
  366. "userType": 2,
  367. "orderType": "2"
  368. }).then(res => { // handle success
  369. // console.log(res.data);
  370. if (res.retCode == 0) {
  371. app.orderNo = res.data;
  372. wx.navigateTo({
  373. url: "/pages/order/confirm"
  374. });
  375. } else {
  376. Toast.fail(res.message);
  377. }
  378. }).catch(error => { // handle error
  379. // console.log(error);
  380. Toast.fail(error);
  381. })
  382. }
  383. } else {
  384. this.showDialogPhone(); //调用手机号登录弹窗
  385. }
  386. } catch (e) {
  387. // Do something when catch error
  388. Toast.fail(e);
  389. }
  390. } else {
  391. this.showDialogLogin(); //调用登录弹窗
  392. }
  393. } catch (e) {
  394. // Do something when catch error
  395. Toast.fail(e);
  396. }
  397. },
  398. //金额输入框获取实时输入的值
  399. bindKeyInput: function (e) {
  400. // const reg = /^[1-9]{1,}(\.)?([0-9]{0,2})?/ // 最小数额1
  401. const reg = /^[0-9]{1,}(\.)?([0-9]{0,2})?/ // 不设置最小数额
  402. const res = e.detail.value.match(reg, "");
  403. if (res !== null) {
  404. app.orderAmount = res[0]
  405. this.setData({
  406. account: res[0]
  407. })
  408. } else {
  409. app.orderAmount = ""
  410. this.setData({
  411. account: ""
  412. })
  413. }
  414. },
  415. // 显示登录弹窗
  416. showDialogLogin: function () {
  417. this.setData({
  418. showModal: true //修改弹窗状态为true,即显示
  419. })
  420. },
  421. // 隐藏登录弹窗
  422. hideDialogLogin: function () {
  423. this.setData({
  424. showModal: false //修改弹窗状态为false,即隐藏
  425. });
  426. },
  427. getUserInfo: function (e) {
  428. let that = this;
  429. // 获取用户信息
  430. wx.getSetting({
  431. success(res) {
  432. console.log(res)
  433. // console.log("res", res)
  434. if (res.authSetting['scope.userInfo']) {
  435. // console.log("已授权=====")
  436. // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  437. wx.getUserInfo({
  438. success(res) {
  439. // console.log("获取用户信息成功", res.userInfo);
  440. app.globalData.userInfo = res.userInfo;
  441. wx.setStorage({
  442. data: res.userInfo,
  443. key: 'userInfo',
  444. })
  445. that.showDialogPhone(); //调用一键获取手机号弹窗
  446. that.setData({
  447. userInfo: res.userInfo,
  448. hasUserInfo: true
  449. })
  450. },
  451. fail(res) {
  452. Toast.fail(res);
  453. console.log("获取用户信息失败", res)
  454. }
  455. })
  456. } else {
  457. that.setData({
  458. showModal: false
  459. });
  460. wx.showToast({
  461. title: '拒绝授权!',
  462. icon: 'error',
  463. duration: 2000
  464. })
  465. }
  466. }
  467. })
  468. },
  469. //授权用户信息按钮
  470. bindAuthUserInfo: function (e) {
  471. var that = this;
  472. //获取授权结果查看是否已授权用户信息
  473. wx.getSetting({
  474. success: (res) => {
  475. if (res.authSetting['scope.userInfo'] == undefined && !res.authSetting['scope.userInfo']) //未授权获取用户信息(首次进来页面)
  476. that.getLocationInfo(); //获取当前用户信息
  477. else if (res.authSetting['scope.userInfo'] === false) //未授权获取用户信息(点击官方授权弹框取消按钮后)
  478. that.setData({
  479. isShowUserInfoLayer: true
  480. }) //显示自定义授权框
  481. else //已授权
  482. that.getUserInfo(); //获取当前用户信息
  483. }
  484. })
  485. },
  486. // 显示一键获取手机号弹窗
  487. showDialogPhone: function () {
  488. this.setData({
  489. showModalPhone: true //修改弹窗状态为true,即显示
  490. })
  491. },
  492. // 隐藏一键获取手机号弹窗
  493. hideDialogPhone: function () {
  494. this.setData({
  495. showModalPhone: false //修改弹窗状态为false,即隐藏
  496. });
  497. },
  498. getPhoneNumber: function (e) {
  499. let that = this;
  500. let app = getApp();
  501. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  502. that.setData({
  503. showModalPhone: false,
  504. showModal: false
  505. });
  506. wx.showToast({
  507. title: '拒绝授权!',
  508. icon: 'error',
  509. duration: 2000
  510. })
  511. } else {
  512. let iv = e.detail.iv;
  513. let encryptedData = e.detail.encryptedData;
  514. let mobilePhone = null;
  515. wx.login({
  516. success: function (res) {
  517. that.setData({
  518. hasPhoneNumber: true
  519. });
  520. if (res.code) {
  521. let code = res.code;
  522. //发起网络请求
  523. getMpOpenId({
  524. code: code,
  525. stationId: app.globalData.stationId
  526. }).then(res => { // handle success
  527. // console.log("sessionKey:" + res.session_key);
  528. decryptEncryptedData({
  529. sessionKey: res.sessionKey,
  530. encryptedData: encryptedData,
  531. iv: iv
  532. }).then(res => { // handle success
  533. mobilePhone = res.phoneNumber;
  534. //保存appUserInfo信息
  535. saveAppUserInfo({
  536. "userType": "2",
  537. "openId": app.globalData.openId,
  538. "blogNickName": app.globalData.userInfo.nickName,
  539. "sexFlag": app.globalData.userInfo.gender === 1 ? 'M' : 'F',
  540. "blogProfilePhoto": app.globalData.userInfo.avatarUrl,
  541. "mobilePhone": mobilePhone,
  542. "stationId": app.globalData.stationId, //油站Id
  543. "stationName": app.globalData.stationName, //油站名称
  544. "unionId": app.globalData.unionId
  545. }).then(res => { // handle success
  546. // console.log(res.data);
  547. if (res.retCode == 0) {
  548. // console.log("保存app用户信息成功:"+app.globalData.userInfo);
  549. // console.log("保存app用户信息成功。。。");
  550. app.globalData.phoneNumber = mobilePhone;
  551. wx.setStorage({
  552. data: mobilePhone,
  553. key: 'mobilePhone',
  554. });
  555. // console.log("mobilePhone:" + mobilePhone);
  556. ///////////////////////
  557. that.setData({
  558. showModalPhone: false,
  559. showModal: false,
  560. phoneNumber: mobilePhone
  561. });
  562. wx.showToast({
  563. title: '登录成功',
  564. icon: 'success',
  565. duration: 2000
  566. })
  567. }
  568. }).catch(error => { // handle error
  569. wx.showToast({
  570. title: error,
  571. })
  572. console.log(error);
  573. })
  574. }).catch(error => { // handle error
  575. wx.showToast({
  576. title: error,
  577. })
  578. console.log(error);
  579. })
  580. }).catch(error => { // handle error
  581. wx.showToast({
  582. title: error,
  583. })
  584. console.log(error);
  585. })
  586. } else {
  587. wx.showToast({
  588. title: res.errMsg,
  589. })
  590. console.log('获取用户登录态失败!' + res.errMsg)
  591. }
  592. }
  593. })
  594. }
  595. }
  596. })