create.js 22 KB

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