create.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. if (res.retCode == 0) {
  104. var gasArr = [];
  105. for (let i = 0; i < res.data.length; i++) {
  106. gasArr.push(res.data[i]);
  107. }
  108. this.setData({
  109. gasallarr: res.data,
  110. gasarr: gasArr
  111. })
  112. app.globalData.stationName = res.data[0].stationName;
  113. }
  114. // console.log("gasArr lengh:"+gasArr.length);
  115. }).catch(error => { // handle error
  116. console.log(error);
  117. })
  118. },
  119. gup: function (name, url) {
  120. if (!url) url = location.href;
  121. name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  122. var regexS = "[\\?&]" + name + "=([^&#]*)";
  123. var regex = new RegExp(regexS);
  124. var results = regex.exec(url);
  125. return results == null ? null : results[1];
  126. },
  127. /**
  128. * 生命周期函数--监听页面显示
  129. */
  130. onShow: function () {},
  131. onClose() {
  132. this.setData({
  133. showPopup: false
  134. });
  135. },
  136. clickMore: function () {
  137. this.setData({
  138. showPopup: true
  139. });
  140. },
  141. selectGas: function (event) {
  142. let gunNo = event.target.dataset.index;
  143. let gunName = event.target.dataset.oilname;
  144. let oilPrice = event.target.dataset.oilprice;
  145. let gunType = event.target.dataset.guntype;
  146. app.oilGunNum = gunNo;
  147. app.oilGunName = gunName;
  148. app.oilPrice = oilPrice;
  149. app.gunType = gunType;
  150. // console.log("gunType="+event.target.dataset.guntype);
  151. this.setData({
  152. gas: event.target.dataset.index,
  153. showPopup: false
  154. });
  155. // console.log("gas is :" + event.target.dataset.index);
  156. },
  157. selectGasAll: function (event) {
  158. var gasarr = [];
  159. var arrLength = this.data.gasallarr.length;
  160. if (event.target.dataset.index > 5) {
  161. for (let index = event.target.dataset.index - 5; index <= event.target.dataset.index; index++) {
  162. gasarr.push(this.data.gasallarr[index]);
  163. }
  164. } else {
  165. for (let index = 0; index < arrLength; index++) {
  166. gasarr.push(this.data.gasallarr[index]);
  167. }
  168. }
  169. let gunNo = event.target.dataset.index;
  170. let gunName = event.target.dataset.oilname;
  171. let oilPrice = event.target.dataset.oilprice;
  172. let gunType = event.target.dataset.guntype;
  173. app.oilGunNum = gunNo;
  174. app.oilGunName = gunName;
  175. app.oilPrice = oilPrice;
  176. app.gunType = gunType;
  177. // console.log("选定的油枪号:" + event.target.dataset.num);
  178. // console.log("gasarr size:" + gasarr.length);
  179. this.setData({
  180. gasarr: gasarr,
  181. gas: event.target.dataset.num,
  182. showPopup: false
  183. });
  184. },
  185. clickAccount: function (event) {
  186. app.orderAmount = event.target.dataset.money;
  187. this.setData({
  188. account: event.target.dataset.money
  189. });
  190. // console.log("account is :" + event.target.dataset.money);
  191. },
  192. toastMsg: function () {
  193. if (this.data.gas <= 0) {
  194. Toast.fail('请选择油枪');
  195. } else if (this.data.account <= 0) {
  196. Toast.fail('请输入金额');
  197. }
  198. },
  199. toConfirm: function () {
  200. var userInfoValue = wx.getStorageSync('userInfo')
  201. try {
  202. if (userInfoValue) {
  203. // Do something with return value
  204. // console.log("本地缓存中的userInfo:" + userInfoValue);
  205. try {
  206. var mobileValue = wx.getStorageSync('mobilePhone')
  207. if (mobileValue) {
  208. // Do something with return value
  209. // console.log("本地缓存中的phoneNumber:" + mobileValue);
  210. let orderAmt = app.orderAmount;
  211. let oilPrice = app.oilPrice;
  212. let discountPrice = null; //优惠价格
  213. let gasoilDiscountAmount = null; //优惠金额-满减,立减,直降
  214. let discountAmount = null; //优惠起点金额
  215. let totalMoney = null;
  216. let youHuiAmount = null;
  217. let gunType = app.gunType;
  218. if (gunType === '1') { //油品订单
  219. console.log("**************计算优惠单开始****************");
  220. console.log(app)
  221. /**************计算优惠单开始****************/
  222. getCustomerGradeInfo({
  223. "openId": app.globalData.openId,
  224. "stationId": app.globalData.stationId,
  225. "userType": "2",
  226. "oilName": app.oilGunName,
  227. "mobilePhone": mobileValue
  228. }).then(res => { // handle success
  229. if (res.retCode == 0) { //获取优惠信息成功
  230. if (res.data.length > 0) {
  231. if ((res.data[0].discountWay === '直降')) { //优惠方式为等级直降
  232. discountPrice = res.data[0].gasoilDiscountLitre; //直降方案,按会员等级、按油品单价直降
  233. totalMoney = (orderAmt / oilPrice) * (oilPrice - discountPrice);
  234. // console.log("totalMonry1:"+totalMoney);
  235. totalMoney = totalMoney.toFixed(2);
  236. // console.log("totalMonry2:"+totalMoney);
  237. youHuiAmount = (orderAmt / oilPrice) * discountPrice;
  238. youHuiAmount = youHuiAmount.toFixed(2);
  239. } else if (res.data[0].discountWay === '1') { //优惠方式为满减
  240. gasoilDiscountAmount = Number(res.data[0].gasoilDiscountAmt); //满减方案,价格不变,总额满足条件优惠满减
  241. discountAmount = Number(res.data[0].discountAmt);
  242. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  243. if (orderAmt >= discountAmount) {
  244. let discountTimes = Math.floor(orderAmt / discountAmount);
  245. totalMoney = Number(orderAmt - (gasoilDiscountAmount * discountTimes));
  246. totalMoney = totalMoney.toFixed(2);
  247. youHuiAmount = Number(gasoilDiscountAmount * discountTimes);
  248. youHuiAmount = youHuiAmount.toFixed(2);
  249. } else {
  250. totalMoney = orderAmt;
  251. youHuiAmount = 0;
  252. }
  253. } else if (res.data[0].discountWay === '2') { //优惠方式为立减
  254. gasoilDiscountAmount = Number(res.data[0].gasoilDiscountAmt); //立减方案,价格不变,总额满足条件优惠立减
  255. discountAmount = Number(res.data[0].discountAmt);
  256. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  257. if (orderAmt >= discountAmount) {
  258. totalMoney = Number(orderAmt - gasoilDiscountAmount);
  259. totalMoney = totalMoney.toFixed(2);
  260. youHuiAmount = Number(gasoilDiscountAmount);
  261. youHuiAmount = youHuiAmount.toFixed(2);
  262. } else {
  263. totalMoney = orderAmt;
  264. youHuiAmount = 0;
  265. }
  266. } else if ((res.data[0].discountWay === '3')) { //优惠方式为独立直降
  267. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  268. if (res.data[0].discountTerm === '1') { //按总金额直降
  269. if (orderAmt >= res.data[0].discountAmt) {
  270. discountPrice = Number(res.data[0].gasoilDiscountAmt); //独立直降方案,按油品升数或者总价直降
  271. totalMoney = (orderAmt / oilPrice) * (oilPrice - discountPrice);
  272. // console.log("totalMonry1:"+totalMoney);
  273. totalMoney = totalMoney.toFixed(2);
  274. // console.log("totalMonry2:"+totalMoney);
  275. youHuiAmount = (orderAmt / oilPrice) * discountPrice;
  276. youHuiAmount = youHuiAmount.toFixed(2);
  277. } else {
  278. totalMoney = orderAmt;
  279. youHuiAmount = 0;
  280. }
  281. } else if (res.data[0].discountTerm === '2') { //按升数直降
  282. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  283. //取条件升数
  284. let dislQty = Number(res.data[0].discountAmt);
  285. //计算升数
  286. let lQty = orderAmt / oilPrice;
  287. lQty = lQty.toFixed(2);
  288. if (lQty >= dislQty) {
  289. totalMoney = (orderAmt / oilPrice) * (oilPrice - discountPrice);
  290. // console.log("totalMonry1:"+totalMoney);
  291. totalMoney = totalMoney.toFixed(2);
  292. // console.log("totalMonry2:"+totalMoney);
  293. youHuiAmount = (orderAmt / oilPrice) * discountPrice;
  294. youHuiAmount = youHuiAmount.toFixed(2);
  295. } else {
  296. totalMoney = orderAmt;
  297. youHuiAmount = 0;
  298. }
  299. } else {
  300. totalMoney = orderAmt;
  301. youHuiAmount = 0;
  302. }
  303. } else { //没有以上三种优惠方式
  304. totalMoney = orderAmt;
  305. youHuiAmount = 0;
  306. }
  307. } else { //没有查询到优惠信息,新用户没有优惠
  308. totalMoney = orderAmt;
  309. youHuiAmount = 0;
  310. }
  311. /**************计算优惠结束****************/
  312. //请求后台创建订单接口,获取订单号
  313. if (isDemo) {
  314. console.log("演示项目,已经为您修改实收金额");
  315. totalMoney = "0.01"
  316. }
  317. postOrderInfo({
  318. "stationId": app.globalData.stationId,
  319. "oilGun": app.oilGunNum,
  320. "oilName": app.oilGunName,
  321. "oilPrice": app.oilPrice,
  322. "receivableAmt": app.orderAmount, //应收金额
  323. "discountAmt": youHuiAmount, //优惠金额
  324. "amt": totalMoney, //实收金额
  325. "openId": app.globalData.openId,
  326. "userType": 2,
  327. "orderType": "1"
  328. }).then(res => { // handle success
  329. // console.log(res.data);
  330. if (res.retCode == 0) {
  331. app.orderNo = res.data;
  332. wx.navigateTo({
  333. url: "/pages/order/confirm"
  334. });
  335. } else {
  336. Toast.fail(res.message);
  337. }
  338. }).catch(error => { // handle error
  339. // console.log(error);
  340. Toast.fail(error);
  341. })
  342. } else {
  343. Toast.fail(res.message);
  344. }
  345. }).catch(error => { // handle error
  346. Toast.fail(error);
  347. })
  348. } else { //非油品订单
  349. totalMoney = orderAmt;
  350. youHuiAmount = 0;
  351. if (isDemo) {
  352. console.log("演示项目,已经为您修改实收金额");
  353. totalMoney = "0.01"
  354. }
  355. //请求后台创建订单接口,获取订单号
  356. postOrderInfo({
  357. "stationId": app.globalData.stationId,
  358. "oilGun": app.oilGunNum,
  359. "oilName": app.oilGunName,
  360. "oilPrice": app.oilPrice,
  361. "receivableAmt": app.orderAmount, //应收金额
  362. "discountAmt": youHuiAmount, //优惠金额
  363. "amt": totalMoney, //实收金额
  364. "openId": app.globalData.openId,
  365. "userType": 2,
  366. "orderType": "2"
  367. }).then(res => { // handle success
  368. // console.log(res.data);
  369. if (res.retCode == 0) {
  370. app.orderNo = res.data;
  371. wx.navigateTo({
  372. url: "/pages/order/confirm"
  373. });
  374. } else {
  375. Toast.fail(res.message);
  376. }
  377. }).catch(error => { // handle error
  378. // console.log(error);
  379. Toast.fail(error);
  380. })
  381. }
  382. } else {
  383. this.showDialogPhone(); //调用手机号登录弹窗
  384. }
  385. } catch (e) {
  386. // Do something when catch error
  387. Toast.fail(e);
  388. }
  389. } else {
  390. this.showDialogLogin(); //调用登录弹窗
  391. }
  392. } catch (e) {
  393. // Do something when catch error
  394. Toast.fail(e);
  395. }
  396. },
  397. //金额输入框获取实时输入的值
  398. bindKeyInput: function (e) {
  399. // const reg = /^[1-9]{1,}(\.)?([0-9]{0,2})?/ // 最小数额1
  400. const reg = /^[0-9]{1,}(\.)?([0-9]{0,2})?/ // 不设置最小数额
  401. const res = e.detail.value.match(reg, "");
  402. if (res !== null) {
  403. app.orderAmount = res[0]
  404. this.setData({
  405. account: res[0]
  406. })
  407. } else {
  408. app.orderAmount = ""
  409. this.setData({
  410. account: ""
  411. })
  412. }
  413. },
  414. // 显示登录弹窗
  415. showDialogLogin: function () {
  416. this.setData({
  417. showModal: true //修改弹窗状态为true,即显示
  418. })
  419. },
  420. // 隐藏登录弹窗
  421. hideDialogLogin: function () {
  422. this.setData({
  423. showModal: false //修改弹窗状态为false,即隐藏
  424. });
  425. },
  426. getUserInfo: function (e) {
  427. let that = this;
  428. wx.getUserProfile({
  429. lang: "zh_CN",
  430. desc: "测试一下",
  431. success(res) {
  432. app.globalData.userInfo = res.userInfo;
  433. wx.setStorage({
  434. data: res.userInfo,
  435. key: 'userInfo',
  436. })
  437. that.showDialogPhone(); //调用一键获取手机号弹窗
  438. that.setData({
  439. userInfo: res.userInfo,
  440. hasUserInfo: true
  441. })
  442. },
  443. fail(err) {
  444. wx.showToast({
  445. title: '您拒绝了授权',
  446. icon: 'error',
  447. duration: 2000
  448. })
  449. }
  450. })
  451. // 获取用户信息
  452. wx.getSetting({
  453. success(res) {
  454. if (res.authSetting['scope.userInfo']) {
  455. wx.getUserInfo({
  456. success(res) {
  457. app.globalData.userInfo = res.userInfo;
  458. wx.setStorage({
  459. data: res.userInfo,
  460. key: 'userInfo',
  461. })
  462. that.showDialogPhone(); //调用一键获取手机号弹窗
  463. that.setData({
  464. userInfo: res.userInfo,
  465. hasUserInfo: true
  466. })
  467. },
  468. fail(res) {
  469. Toast.fail(res);
  470. console.log("获取用户信息失败", res)
  471. }
  472. })
  473. } else {
  474. that.setData({
  475. showModal: false
  476. });
  477. wx.showToast({
  478. title: '拒绝授权!',
  479. icon: 'error',
  480. duration: 2000
  481. })
  482. }
  483. }
  484. })
  485. },
  486. //授权用户信息按钮
  487. bindAuthUserInfo: function (e) {
  488. var that = this;
  489. //获取授权结果查看是否已授权用户信息
  490. wx.getSetting({
  491. success: (res) => {
  492. if (res.authSetting['scope.userInfo'] == undefined && !res.authSetting['scope.userInfo']) //未授权获取用户信息(首次进来页面)
  493. that.getLocationInfo(); //获取当前用户信息
  494. else if (res.authSetting['scope.userInfo'] === false) //未授权获取用户信息(点击官方授权弹框取消按钮后)
  495. that.setData({
  496. isShowUserInfoLayer: true
  497. }) //显示自定义授权框
  498. else //已授权
  499. that.getUserInfo(); //获取当前用户信息
  500. }
  501. })
  502. },
  503. // 显示一键获取手机号弹窗
  504. showDialogPhone: function () {
  505. this.setData({
  506. showModalPhone: true //修改弹窗状态为true,即显示
  507. })
  508. },
  509. // 隐藏一键获取手机号弹窗
  510. hideDialogPhone: function () {
  511. this.setData({
  512. showModalPhone: false //修改弹窗状态为false,即隐藏
  513. });
  514. },
  515. getPhoneNumber: function (e) {
  516. let that = this;
  517. let app = getApp();
  518. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  519. that.setData({
  520. showModalPhone: false,
  521. showModal: false
  522. });
  523. wx.showToast({
  524. title: '拒绝授权!',
  525. icon: 'error',
  526. duration: 2000
  527. })
  528. } else {
  529. let iv = e.detail.iv;
  530. let encryptedData = e.detail.encryptedData;
  531. let mobilePhone = null;
  532. wx.login({
  533. success: function (res) {
  534. that.setData({
  535. hasPhoneNumber: true
  536. });
  537. if (res.code) {
  538. let code = res.code;
  539. //发起网络请求
  540. getMpOpenId({
  541. code: code,
  542. stationId: app.globalData.stationId
  543. }).then(res => { // handle success
  544. // console.log("sessionKey:" + res.session_key);
  545. decryptEncryptedData({
  546. sessionKey: res.sessionKey,
  547. encryptedData: encryptedData,
  548. iv: iv
  549. }).then(res => { // handle success
  550. mobilePhone = res.phoneNumber;
  551. //保存appUserInfo信息
  552. saveAppUserInfo({
  553. "userType": "2",
  554. "openId": app.globalData.openId,
  555. "blogNickName": app.globalData.userInfo.nickName,
  556. "sexFlag": app.globalData.userInfo.gender === 1 ? 'M' : 'F',
  557. "blogProfilePhoto": app.globalData.userInfo.avatarUrl,
  558. "mobilePhone": mobilePhone,
  559. "stationId": app.globalData.stationId, //油站Id
  560. "stationName": app.globalData.stationName, //油站名称
  561. "unionId": app.globalData.unionId
  562. }).then(res => { // handle success
  563. // console.log(res.data);
  564. if (res.retCode == 0) {
  565. // console.log("保存app用户信息成功:"+app.globalData.userInfo);
  566. // console.log("保存app用户信息成功。。。");
  567. app.globalData.phoneNumber = mobilePhone;
  568. wx.setStorage({
  569. data: mobilePhone,
  570. key: 'mobilePhone',
  571. });
  572. // console.log("mobilePhone:" + mobilePhone);
  573. ///////////////////////
  574. that.setData({
  575. showModalPhone: false,
  576. showModal: false,
  577. phoneNumber: mobilePhone
  578. });
  579. wx.showToast({
  580. title: '登录成功',
  581. icon: 'success',
  582. duration: 2000
  583. })
  584. }
  585. }).catch(error => { // handle error
  586. wx.showToast({
  587. title: error,
  588. })
  589. console.log(error);
  590. })
  591. }).catch(error => { // handle error
  592. wx.showToast({
  593. title: error,
  594. })
  595. console.log(error);
  596. })
  597. }).catch(error => { // handle error
  598. wx.showToast({
  599. title: error,
  600. })
  601. console.log(error);
  602. })
  603. } else {
  604. wx.showToast({
  605. title: res.errMsg,
  606. })
  607. console.log('获取用户登录态失败!' + res.errMsg)
  608. }
  609. }
  610. })
  611. }
  612. },
  613. test() {
  614. wx.login({
  615. success(res) {
  616. console.log(res)
  617. }
  618. })
  619. console.log(111111)
  620. wx.getUserProfile({
  621. lang: "zh_CN",
  622. desc: "测试一下",
  623. success(res) {
  624. console.log(res)
  625. }
  626. })
  627. }
  628. })