leasing.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. const utilMd5 = require('../../../utils/md5.js')
  2. const app = getApp()
  3. Page({
  4. data: {
  5. xa_consignee: '',
  6. xa_city: '',
  7. et_type: '',
  8. xa_county: '',
  9. xa_address: '',
  10. xa_phone: '',
  11. startdate: '',
  12. enddate: '',
  13. et_url: '',
  14. et_rent: '',
  15. et_mortagage: '',
  16. et_name: '',
  17. rent: '',
  18. discount: 0,
  19. num: 30,
  20. total: '',
  21. minusStatus: 'disabled' ,
  22. userInfo: {},
  23. hasUserInfo: false,
  24. canIUse: wx.canIUse('button.open-type.getUserInfo')
  25. },
  26. hrefAddress: function(e) {
  27. if (this.data.xa_consignee == null || this.data.xa_consignee == ''){
  28. wx.navigateTo({
  29. url: '/pages/mine/myaddress/createaddress/createaddress?type=bill'
  30. })
  31. } else {
  32. wx.navigateTo({
  33. url: '/pages/mine/myaddress/myaddress?type=bill'
  34. })
  35. }
  36. },
  37. //事件处理函数
  38. bindViewTap: function() {
  39. wx.navigateTo({
  40. url: '../logs/logs'
  41. })
  42. },
  43. onLoad: function (options) {
  44. var that = this
  45. wx.request({
  46. url: app.globalData.url + '/getEqUserAddress',
  47. method: 'get',
  48. data: {
  49. xu_openid: app.globalData.openid
  50. },
  51. success: function (res) {
  52. if (res.data.length != 0) {
  53. that.setData({
  54. xa_consignee: res.data.xa_consignee,
  55. xa_city: res.data.xa_city,
  56. xa_county: res.data.xa_county,
  57. xa_address: res.data.xa_address,
  58. xa_phone: res.data.xa_phone
  59. })
  60. }
  61. },
  62. fail() {
  63. app.timeOut()
  64. }
  65. })
  66. this.setData ({
  67. et_type: options.para
  68. })
  69. var date = new Date();
  70. var date2 = new Date(date);
  71. date2 = +date2 + 1000 * 60 * 60 * 24 * 30;
  72. date2 = new Date(date2);
  73. var seperator1 = "-";
  74. var year = date.getFullYear();
  75. var month = ("0" + (date.getMonth() + 1)).slice(-2);
  76. var strDate = ("0" + (date.getDate())).slice(-2);
  77. var year2 = date2.getFullYear();
  78. var month2 = ("0" + (date2.getMonth() + 1)).slice(-2);
  79. var strDate2 = ("0" + (date2.getDate())).slice(-2);
  80. this.setData({
  81. startdate: year + seperator1 + month + seperator1 + strDate,
  82. enddate: year2 + seperator1 + month2 + seperator1 + strDate2
  83. })
  84. var that = this
  85. wx.request({
  86. url: app.globalData.url +'/getEquipmentAmount',
  87. data: {
  88. et_type: this.data.et_type
  89. },
  90. method: 'get',
  91. success: function (res) {
  92. that.setData({
  93. et_url: res.data.et_url,
  94. et_rent: res.data.et_rent.toFixed(2),
  95. et_mortagage: res.data.et_mortagage.toFixed(2),
  96. et_name: res.data.et_name,
  97. rent: (res.data.et_rent * 30).toFixed(2),
  98. total: (res.data.et_rent * 30 + res.data.et_mortagage).toFixed(2)
  99. })
  100. },
  101. fail() {
  102. app.timeOut()
  103. }
  104. });
  105. let _this = this;
  106. _this.setData({
  107. navH: app.globalData.navHeight
  108. })
  109. },
  110. getUserInfo: function(e) {
  111. app.globalData.userInfo = e.detail.userInfo
  112. this.setData({
  113. userInfo: e.detail.userInfo,
  114. hasUserInfo: true
  115. })
  116. },
  117. bindDateChange: function (e) {
  118. var endDate = new Date(e.detail.value);
  119. endDate = +endDate + 1000 * 60 * 60 * 24 * 30;
  120. endDate = new Date(endDate);
  121. this.setData({
  122. startdate: e.detail.value,
  123. enddate: endDate.getFullYear() + "-" + ("0" + ((endDate.getMonth() + 1))).slice(-2) + "-" + ("0" + (endDate.getDate())).slice(-2)
  124. })
  125. this.compute();
  126. },
  127. bindMinus: function () {
  128. var num = this.data.num;
  129. // 如果大于1时,才可以减
  130. if (num > 30) {
  131. num--;
  132. }
  133. // 只有大于一件的时候,才能normal状态,否则disable状态
  134. var minusStatus = num <= 30 ? 'disabled' : 'normal';
  135. // 将数值与状态写回
  136. this.setData({
  137. num: num,
  138. minusStatus: minusStatus
  139. });
  140. this.compute();
  141. },
  142. /* 点击加号 */
  143. bindPlus: function () {
  144. var num = this.data.num;
  145. // 不作过多考虑自增1
  146. num++;
  147. // 只有大于一件的时候,才能normal状态,否则disable状态
  148. var minusStatus = num < 30 ? 'disabled' : 'normal';
  149. // 将数值与状态写回
  150. this.setData({
  151. num: num,
  152. minusStatus: minusStatus
  153. });
  154. this.compute();
  155. },
  156. /* 输入框事件 */
  157. bindManual: function (e) {
  158. var num = e.detail.value;
  159. // 将数值与状态写回
  160. this.setData({
  161. num: num
  162. });
  163. this.compute();
  164. },
  165. compute: function() {
  166. if(this.data.num <= 30) {
  167. var startDate = new Date(this.data.startdate);
  168. startDate = +startDate + 1000 * 60 * 60 * 24 * 30;
  169. startDate = new Date(startDate);
  170. var temp_rent = Number(30 * this.data.et_rent)
  171. var temp_total = Number(temp_rent) + Number(this.data.et_mortagage)
  172. this.setData({
  173. enddate: startDate.getFullYear() + "-" + ("0" + (startDate.getMonth() + 1)).slice(-2) + "-" + ("0" + (startDate.getDate())).slice(-2),
  174. num: 30,
  175. rent: temp_rent.toFixed(2),
  176. total: temp_total.toFixed(2)
  177. })
  178. } else {
  179. var startDate = new Date(this.data.startdate);
  180. startDate = +startDate + 1000 * 60 * 60 * 24 * this.data.num;
  181. startDate = new Date(startDate);
  182. var temp_rent = Number(this.data.num * this.data.et_rent)
  183. var temp_total = Number(temp_rent) + Number(this.data.et_mortagage)
  184. this.setData({
  185. enddate: startDate.getFullYear() + "-" + ("0" + (startDate.getMonth() + 1)).slice(-2) + "-" + ("0" + (startDate.getDate())).slice(-2),
  186. rent: temp_rent.toFixed(2),
  187. total: temp_total.toFixed(2)
  188. })
  189. }
  190. },
  191. orderSign: function(e){
  192. //验证输入
  193. if (this.data.xa_consignee == null || this.data.xa_consignee == '') {
  194. wx.showToast({
  195. title: '请选择您的收货地址!',
  196. icon: 'none',
  197. duration: 1500
  198. })
  199. return false;
  200. }
  201. if (this.data.total == null || this.data.total == '') {
  202. wx.showToast({
  203. title: '没有合计金额!',
  204. icon: 'none',
  205. duration: 1500
  206. })
  207. return false;
  208. }
  209. //生成订单号
  210. var date = new Date();
  211. var year = date.getFullYear();
  212. var month = ("0" + (date.getMonth() + 1)).slice(-2);;
  213. var strDate = ("0" + (date.getDate())).slice(-2);
  214. var hours = ("0" + (date.getHours())).slice(-2);
  215. var minutes = ("0" + (date.getMinutes())).slice(-2);
  216. var seconds = ("0" + (date.getSeconds())).slice(-2);
  217. var rnd = "";
  218. for (var i = 0; i < 4; i++){
  219. rnd += Math.floor(Math.random() * 10);
  220. }
  221. //head
  222. var orderno = 'YS' + year + month + strDate + this.data.xa_phone.substring(this.data.xa_phone.length - 4, this.data.xa_phone.length) + rnd;
  223. var eh_mortagage = Number(this.data.et_mortagage)
  224. var eh_rental = Number(this.data.rent)
  225. var eh_cmname = app.globalData.nickName
  226. var eh_cmphone = app.globalData.userphone
  227. var eh_cmidcard = app.globalData.openid
  228. var eh_cmaddress = this.data.xa_consignee + ',' + this.data.xa_phone + ',' + this.data.xa_city + this.data.xa_county + this.data.xa_address
  229. var eh_usercode = 'onLine'
  230. var eh_date = year + '-' + month + '-' + strDate
  231. var eh_time = year + '-' + month + '-' + strDate + ' ' + hours + ':' + minutes + ':' + seconds;
  232. var eh_status = '1'
  233. var eh_source = '1'
  234. var total = Number(this.data.total)
  235. var cp_discount = Number(this.data.discount)
  236. //detail
  237. var et_type = this.data.et_type
  238. var ed_startdate = this.data.startdate
  239. var ed_enddate = this.data.enddate
  240. // 支付test
  241. // this.setData({
  242. // total: 0.01
  243. // })
  244. //获取支付参数
  245. var that = this
  246. wx.request({
  247. url: app.globalData.payurl,
  248. method: 'post',
  249. data: {
  250. orderNo: orderno,
  251. channel: "wx_mini_ys",
  252. price: this.data.total,
  253. originPrice: (Number(this.data.et_mortagage) + Number(this.data.rent)).toFixed(2),
  254. openId: app.globalData.openid,
  255. title: this.data.et_name +"*1",
  256. productName: this.data.et_name,
  257. productId: this.data.et_type
  258. },
  259. success: function (res) {
  260. that.addEquipmenteHireHeadOnline(orderno, eh_mortagage, eh_rental, eh_cmname, eh_cmphone, eh_cmidcard, eh_cmaddress, eh_usercode, eh_date, eh_status, eh_source, that.data.total, cp_discount)
  261. that.addEquipmenteHireDetail(orderno, et_type, ed_startdate, ed_enddate)
  262. // 调用支付接口
  263. var timeStamp = String(parseInt(new Date().getTime() / 1000));
  264. var nonceStr = that.randomString();
  265. var newpaySign = "appId=" + res.data.body.appId + "&nonceStr=" + nonceStr + "&package=" + res.data.body.package + "&signType=MD5" + "&timeStamp=" + timeStamp + "&key=" + app.globalData.key;
  266. var paySign = utilMd5.hexMD5(newpaySign).toUpperCase();
  267. wx.requestPayment(
  268. {
  269. 'timeStamp': timeStamp,
  270. 'nonceStr': nonceStr,
  271. 'package': res.data.body.package,
  272. 'signType': 'MD5',
  273. 'paySign': paySign,
  274. 'success': function (res) {
  275. //发送消息
  276. wx.showToast({
  277. title: '付款成功!',
  278. icon: 'none',
  279. duration: 1500
  280. })
  281. var pages = getCurrentPages();
  282. var currPage = pages[pages.length - 1];
  283. var prevPage = pages[pages.length - 2];
  284. wx.navigateBack({
  285. delta: 1
  286. })
  287. wx.request({
  288. url: app.globalData.url + '/updateEquipmenteHireHeadOnline',
  289. method: 'post',
  290. header: { "Content-Type": "application/x-www-form-urlencoded" },
  291. data: {
  292. eh_code: orderno,
  293. eh_mortagage: eh_mortagage,
  294. eh_rental: eh_rental,
  295. eh_cmname: eh_cmname,
  296. eh_cmphone: eh_cmphone,
  297. eh_cmidcard: eh_cmidcard,
  298. eh_cmaddress: eh_cmaddress,
  299. eh_usercode: eh_usercode,
  300. eh_date: eh_date,
  301. eh_status: eh_status,
  302. eh_source: eh_source,
  303. total: that.data.total,
  304. cp_discount: cp_discount
  305. },
  306. success: function (res) {
  307. wx.hideLoading()
  308. wx.showToast({
  309. title: '租赁成功!',
  310. icon: 'success',
  311. duration: 1500
  312. })
  313. wx.request({
  314. url: app.globalData.url + '/insertEqCustomerBooking',
  315. method: 'post',
  316. header: { "Content-Type": "application/x-www-form-urlencoded" },
  317. data: {
  318. bk_phone: app.globalData.userphone,
  319. bk_date: year + '-' + month + '-' + strDate,
  320. bk_type: 3,
  321. bk_desc: orderno
  322. }
  323. })
  324. that.sendMessage(orderno, that.data.total, eh_time)
  325. },
  326. fail() {
  327. app.timeOut()
  328. }
  329. })
  330. },
  331. 'fail': function (res) {
  332. //删除订单 deleteEquipmenteHireHead
  333. wx.request({
  334. url: app.globalData.url + '/deleteEquipmenteHireHead',
  335. method: 'get',
  336. data: {
  337. eh_code: orderno
  338. },
  339. success: function (resbill) {
  340. wx.showToast({
  341. title: '付款失败!',
  342. icon: 'none',
  343. duration: 1500
  344. })
  345. },
  346. fail() {
  347. app.timeOut()
  348. }
  349. })
  350. },
  351. 'complete': function (res) {
  352. }
  353. })
  354. },
  355. fail() {
  356. app.timeOut()
  357. }
  358. })
  359. },
  360. addEquipmenteHireHeadOnline: function (orderno, eh_mortagage, eh_rental, eh_cmname, eh_cmphone, eh_cmidcard, eh_cmaddress, eh_usercode, eh_date, eh_status, eh_source, total, cp_discount) {
  361. wx.request({
  362. url: app.globalData.url + '/addEquipmenteHireHeadOnline',
  363. method: 'post',
  364. header: { "Content-Type": "application/x-www-form-urlencoded" },
  365. data: {
  366. eh_code: orderno,
  367. eh_mortagage: eh_mortagage,
  368. eh_rental: eh_rental,
  369. eh_cmname: eh_cmname,
  370. eh_cmphone: eh_cmphone,
  371. eh_cmidcard: eh_cmidcard,
  372. eh_cmaddress: eh_cmaddress,
  373. eh_usercode: eh_usercode,
  374. eh_date: eh_date,
  375. eh_status: eh_status,
  376. eh_source: eh_source,
  377. total: total,
  378. cp_discount: cp_discount
  379. }
  380. })
  381. },
  382. sendMessage: function (orderno, total, eh_time){
  383. wx.request({
  384. url: app.globalData.url + '/WXSendOrderMessage',
  385. method: 'get',
  386. data: {
  387. orderno: orderno,
  388. total: total,
  389. date: eh_time
  390. }
  391. })
  392. },
  393. addEquipmenteHireDetail: function (orderno, et_type, ed_startdate, ed_enddate) {
  394. var date = new Date();
  395. var year = date.getFullYear();
  396. var month = ("0" + (date.getMonth() + 1)).slice(-2);;
  397. var strDate = ("0" + (date.getDate())).slice(-2);
  398. var seperator1 = "-";
  399. wx.request({
  400. url: app.globalData.url + '/addEquipmenteHireDetail',
  401. method: 'post',
  402. header: { "Content-Type": "application/x-www-form-urlencoded" },
  403. data: {
  404. eh_code: orderno,
  405. et_type: et_type,
  406. ed_startdate: ed_startdate,
  407. ed_enddate: ed_enddate
  408. },
  409. success: function (res) {
  410. },
  411. fail() {
  412. app.timeOut()
  413. }
  414. })
  415. },
  416. randomString: function () {
  417. var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  418. var maxPos = chars.length;
  419. var pwd = '';
  420. for(var i = 0; i< 32; i++) {
  421. pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  422. }
  423. return pwd;
  424. },
  425. onShareAppMessage: function () {
  426. return {
  427. title: '悦所-设备租赁(' + this.data.et_name+')',
  428. path: '/pages/index/index?skip=1&path=/pages/equipment/leasing/leasing&para=' + this.data.et_type
  429. }
  430. }
  431. })