handleMonthly.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <view class="handle-monthly">
  3. <view class="handle-monthly-item">
  4. <view>车牌选择</view>
  5. <view class="choose-license" @click="isShowCarLicense = true">
  6. <view>{{form.carLicense.label}}</view>
  7. <u-icon name="arrow-down" color="#7B7B7B" size="30"></u-icon>
  8. </view>
  9. </view>
  10. <u-select v-model="isShowCarLicense" :list="carLicenseList" @confirm="carLicenseListConfirm"></u-select>
  11. <view class="handle-monthly-item">
  12. <view>车辆信息</view>
  13. <view>{{form.carLicense.value | verifyStatusFilter}}</view>
  14. </view>
  15. <view class="handle-monthly-item">
  16. <view>包月金额</view>
  17. <view class="handle-monthly-money">{{form.monthAmount}}元</view>
  18. </view>
  19. <view class="handle-monthly-item">
  20. <view>包月时长</view>
  21. <view class="handle-monthly-time-long">
  22. <button @click="reduceMonthNum()">-</button>
  23. <view>{{form.month}}个月</view>
  24. <button @click="addMonthNum()">+</button>
  25. </view>
  26. </view>
  27. <view class="handle-monthly-item">
  28. <view>有效期限</view>
  29. <view>{{dateRange}}</view>
  30. </view>
  31. <view class="handle-monthly-explain">
  32. <view>包月说明</view>
  33. <view>1、停车不足30分钟,免费;</view>
  34. <view>2、停车 超过20分钟,按2元/小时收费;</view>
  35. <!-- <view>3、月卡会员在有效期内停车免费</view> -->
  36. </view>
  37. <view class="handle-monthly-confirm-button">
  38. <button type="default"@click="submit(roadNo)">确认包月</button>
  39. </view>
  40. <u-modal
  41. v-model="payWayPop"
  42. :title-style="{color: '#404040'}"
  43. title="缴费方式"
  44. :show-confirm-button="false"
  45. :show-cancel-button="false">
  46. <view class="slot-content">
  47. <view class="pay-way">
  48. <view class="pay-way-item" @click="gyBankPay">
  49. <image src="../../static/img/gy-bank-pay-icon.png" mode=""></image>
  50. <view>贵州银行</view>
  51. </view>
  52. <view class="pay-way-item" @click="wechatPay">
  53. <image src="../../static/img/wechat-pay-icon.png" mode=""></image>
  54. <view>微信支付</view>
  55. </view>
  56. </view>
  57. <button class="pay-way-close-btn" @click="payWayPop = false">关闭</button>
  58. </view>
  59. </u-modal>
  60. <u-toast ref="uToast" />
  61. </view>
  62. </template>
  63. <script>
  64. import getUrlParams from "../../utils/getUrlParams.js";
  65. export default {
  66. data() {
  67. return {
  68. startTime:'',
  69. endTime:'',
  70. payUrl:'',
  71. monthId: '',
  72. vehicleNo:'',
  73. monthEndTime:'',
  74. monthStartTime:'',
  75. lastActiveDate:null,
  76. roadNo:null,
  77. carLicenseList: [],
  78. isShowCarLicense: false,
  79. form: {
  80. energyType:[],
  81. monthAmount: [],
  82. carLicense: {},
  83. month: 1,
  84. dateRange:""
  85. },
  86. label:"",
  87. payWayPop: false,
  88. jumpUrl: undefined
  89. }
  90. },
  91. onLoad (page) {
  92. if (page.roadNo) {
  93. this.roadNo = page.roadNo;
  94. this.getMonthInfo(this.roadNo);
  95. }
  96. if (page.vehicleNo) {
  97. this.vehicleNo = page.vehicleNo
  98. }
  99. const baseUrl = window.location.href.split('#')[0]
  100. let jumpUrl = baseUrl + '#' + '/pages/center/monthly/monthly'
  101. this.jumpUrl = jumpUrl
  102. },
  103. mounted(){
  104. // console.log(this.lastActiveDate)
  105. // this.form.dateRange=this.getMonthRange(new Date(this.lastActiveDate),1)
  106. },
  107. methods: {
  108. /**
  109. * 获取几个月的日期范围
  110. * {date} Date 起始日期,往后推一天
  111. * {monthNum} Number 往后月数
  112. * */
  113. getMonthRange (date, monthNum) {
  114. let Date1 = this.lastActiveDate;
  115. // Date1 = Date1.valueOf() + 24 * 60 * 60 * 1000
  116. Date1 = new Date(Date1)
  117. const year = Date1.getFullYear()
  118. const month = Date1.getMonth()+1
  119. const day = Date1.getDate()
  120. const hours = Date1.getHours();
  121. const minutes = Date1.getMinutes();
  122. const seconds = Date1.getSeconds();
  123. let days = new Date(year, month, 0)
  124. days = days.getDate() //获取当前日期中的月的天数
  125. let year2 = year;
  126. let month2 = parseInt(month) + parseInt(monthNum)
  127. if (month2 > 12) {
  128. year2 = parseInt(year2) + parseInt((parseInt(month2) / 12 == 0 ? 1 : parseInt(month2) / 12))
  129. month2 = parseInt(month2) % 12;
  130. }
  131. let day2 = day;
  132. let days2 = new Date(year2, month2, 0)
  133. days2 = days2.getDate()
  134. if (day2 > days2) {
  135. day2 = days2
  136. }
  137. if (month2 < 10) {
  138. month2 = '0' + month2;
  139. }
  140. const t1 = year + '.' + (month > 9 ? month : '0' + month) + '.' + (day > 9 ? day : '0' + day)
  141. const t2 = year2 + '.' + month2 + '.' + (day2 > 9 ? day2 : '0' + day2)
  142. this.startTime=t1
  143. this.endTime=t2
  144. this.monthStartTime=year + '-' + (month > 9 ? month : '0' + month) + '-' + (day > 9 ? day : '0' + day) + ' ' + hours + ':' + minutes + ':' +seconds
  145. this.monthEndTime=year2 + '-' + month2 + '-' + day2 + ' ' + hours + ':' + minutes + ':' +seconds
  146. return t1 + '-' + t2
  147. },
  148. /**
  149. * 月操作 减1
  150. * */
  151. reduceMonthNum () {
  152. if (this.form.month > 1) {
  153. this.form.month -= 1
  154. this.form.dateRange = this.getMonthRange(new Date, this.form.month)
  155. }
  156. },
  157. /**
  158. * 月操作 加1
  159. * */
  160. addMonthNum () {
  161. if(this.form.month >=24){
  162. this.$refs.uToast.show({
  163. title: '最多24月',
  164. type: 'warning',
  165. })
  166. return
  167. }
  168. this.form.month += 1
  169. this.form.dateRange = this.getMonthRange(new Date, this.form.month)
  170. },
  171. carLicenseListConfirm (item) {
  172. console.log('this.carLicenseList',this.carLicenseList);
  173. console.log('item',item);
  174. this.form.carLicense = item[0]
  175. },
  176. getMonthInfo(roadNo){
  177. this.$u.api.monthInfo({roadNo: this.roadNo})
  178. .then(res => {
  179. if (res.code === 200){
  180. this.lastActiveDate = res.data.lastActiveDate;
  181. this.form.monthAmount=res.data.monthAmount;
  182. this.carLicenseList = [];
  183. let vehicleNoItem = null
  184. res.data.vehicleList.forEach(item => {
  185. const obj = {
  186. value: item.energyType,
  187. label: item.vehicleNo,
  188. energyType:item.energyType
  189. }
  190. if (this.vehicleNo == item.vehicleNo) {
  191. vehicleNoItem = obj
  192. }
  193. this.carLicenseList.push(obj)
  194. });
  195. // 判断是否url存在车牌号,存在则选中项默认选中
  196. if (vehicleNoItem) {
  197. this.form.carLicense = vehicleNoItem
  198. } else {
  199. this.form.carLicense = this.carLicenseList[0]
  200. }
  201. }
  202. })
  203. },
  204. submit(roadNo){
  205. this.$u.api.createMonth({
  206. roadNo:this.roadNo,
  207. vehicleNo:this.form.carLicense.label,
  208. energyType:this.form.carLicense.energyType,
  209. monthStartTime:this.monthStartTime,
  210. monthEndTime:this.monthEndTime,
  211. monthCount:this.form.month})
  212. .then(res => {
  213. console.log("createMonth",res)
  214. if(res.code === 200){
  215. this.monthId = res.data.monthId
  216. console.log(this.monthId)
  217. this.payWayPop = true
  218. }else{
  219. this.$refs.uToast.show({
  220. title: res.msg,
  221. type: 'warning',
  222. });
  223. }
  224. }).catch(err=>{
  225. });
  226. },
  227. gyBankPay() {
  228. this.$u.api.monthPay({
  229. monthId: this.monthId,
  230. jumpUrl: this.jumpUrl
  231. }).then(res => {
  232. if(res.code === 200){
  233. window.location.href = res.data.url
  234. } else {
  235. this.$refs.uToast.show({
  236. title: res.msg,
  237. type: 'error',
  238. });
  239. }
  240. })
  241. },
  242. /**
  243. * 微信支付
  244. * 判断vuex中是否存在openId
  245. * 存在直接调起微信支付
  246. * 不存在则通过微信登录去获取用户的code
  247. * 完成后通过code去获取用户的openId等信息
  248. * 最后再调起微信支付
  249. * */
  250. wechatPay() {
  251. const openId = this.$store.state.vuex_wxinfo.openId
  252. if (openId) {
  253. this.getWXPay()
  254. } else {
  255. this.getCode()
  256. }
  257. },
  258. /**
  259. * 调起微信支付接口
  260. * @param {Array} list 需要支付的订单组合数组
  261. * @param {Number} deviceNo 设备编号(在停车锁部分需要)
  262. * */
  263. async getWXPay(){
  264. // 支付成功跳转到包月页面
  265. let params = {
  266. monthId: this.monthId,
  267. openid: this.$store.state.vuex_wxinfo.openId
  268. };
  269. await this.$wxApi.config();
  270. this.$pay.wechatPay(params, '/client/monthpay/wechat', this.jumpUrl).then(res =>{
  271. switch (Number(res.code)) {
  272. case 0: // 成功
  273. //#ifdef H5
  274. window.location.reload();
  275. //#endif
  276. break;
  277. case 1: // 取消
  278. this.$refs.uToast.show({
  279. title: '已取消支付',
  280. type: 'info',
  281. });
  282. break;
  283. case 2: // 支付失败
  284. this.$refs.uToast.show({
  285. title: '支付失败,请检查!',
  286. type: 'error',
  287. });
  288. break;
  289. }
  290. });
  291. },
  292. /**
  293. * 获取code
  294. * 1 微信登录获取code
  295. * 2 url中截取
  296. * */
  297. getCode () {
  298. // 获取页面完整url
  299. const local = window.location.href
  300. // 获取url后面的参数
  301. const locationLocaturl = window.location.search;
  302. // 截取url中的code
  303. this.code = getUrlParams(locationLocaturl, "code");
  304. // 如果没有code,则去请求
  305. if (this.code == null || this.code === '') {
  306. window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&#wechat_redirect`
  307. } else {
  308. // 把code传给后台获取用户信息
  309. this.handleGetWXInfo(this.code)
  310. }
  311. },
  312. /**
  313. * 通过code获取openId等用户信息
  314. * 拿到用户信息后再调起微信支付
  315. * */
  316. handleGetWXInfo (code) {
  317. let _this = this
  318. this.$u.api.getWXInfo(code).then((res) => {
  319. if (res.code === 200 ) {
  320. this.$u.vuex('vuex_wxinfo', res.data);
  321. this.getWXPay(this.currentItem)
  322. }
  323. }).catch((err) => {
  324. this.$refs.uToast.show({
  325. title: err.msg,
  326. type: 'error',
  327. });
  328. })
  329. }
  330. },
  331. computed:{
  332. dateRange:function(){
  333. return this.getMonthRange(this.lastActiveDate,this.form.month)
  334. },
  335. },
  336. filters:{
  337. verifyStatusFilter(value) {
  338. if (value === 0) {
  339. return '';
  340. } else if(value === 1){
  341. return '汽油车';
  342. } else if(value === 2){
  343. return '新能源';
  344. } else {
  345. return '';
  346. }
  347. },
  348. }
  349. }
  350. </script>
  351. <style lang="scss" scoped>
  352. @import './handleMonthly.scss';
  353. @import '../paymentMethod/paymentMethod.scss'
  354. </style>