confirmOrder.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. var app = getApp() //实例化小程序,从而获取全局数据或者使用全局函数
  2. var MD5Util = require('../../utils/md5.js');
  3. // let URLINDEX=util.prefix();
  4. var addressList = [];
  5. var addressAll = [];
  6. Page({
  7. data: {
  8. user_phone: '',//绑定的手机号码
  9. hidden: 'hidden',
  10. addressAll: addressAll,
  11. addressList: addressList,
  12. addressBg: "../../images/address_line.png",
  13. addressVal: "../../images/address_logo.png",
  14. addressAddImg: "../../images/add_address.png",
  15. addressEditImg: "../../images/gray_back.png",
  16. checkState: true,
  17. carts: [],
  18. gotJson2: {},
  19. total: 0,
  20. value: 0,
  21. checked: false,//发票默认不选中
  22. servalue: 1,
  23. serchecked: true,//服务协议默认选中
  24. isShow: false,
  25. address: '',
  26. total1: 0,
  27. total2: 0,
  28. province: '',
  29. city: '',
  30. area: '',
  31. adress: '',
  32. consignee: '',
  33. phone: '',
  34. hiddenmodal: true,
  35. minNum: 0,//优惠金额
  36. coupon_id: '',
  37. couponName: '', //优惠劵名称
  38. invoice: false,
  39. invCont: '我要开发票',
  40. //发票信息
  41. invoice_send_flag: 3,
  42. invoice_flag: 3, //发票标志位,如果为3,则为无需发票
  43. invoice_type_name: '',//发票类型名称
  44. invoice_company_name: '',//发票公司名字(纸质单位和增值单位的公司名称相同)
  45. invoice_number: '', //纳税人识别号
  46. invoice_address: '',//注册地址
  47. invoice_telphone: '',//注册电话
  48. invoice_brandName: '',//开户银行
  49. invoice_brandNum: '',//银行账户
  50. invoice_company_content: '',//增值发票内容
  51. invoice_price: '',//发票金额
  52. invoice_title: '',//纸质个人发票抬头
  53. invoice_company_title: '',//纸质单位和增值发票抬头
  54. invoice_takeName: '',//收货姓名
  55. invoice_takeTelphone: '',//收货人联系方式
  56. invoice_takeAddress: '',//收货人地址
  57. invoiceTypeFlag: 0,
  58. orderState: 0,
  59. orderState1: 0,
  60. stateNum: 0,
  61. maskBg: false,//支付提示蒙层
  62. agrService: true,//同意服务协议
  63. stateNum:0,//记录定制商品数量
  64. allowPay : 0//避免重复点击付款
  65. },
  66. onLoad: function (options) {
  67. wx.showNavigationBarLoading();
  68. var that = this;
  69. //转义&符
  70. var carts = options.carts;
  71. carts = carts.replace(/zss/g, "&");
  72. carts = JSON.parse(carts);
  73. console.log(carts);
  74. that.setData({
  75. carts: carts
  76. })
  77. var total = 0;
  78. var total2 = 0;
  79. for (var i = 0; i < carts.length; i++) {
  80. var boxPrice = carts[i].box_price;
  81. console.log("boxPrice:" + boxPrice);
  82. var goodsPrice = carts[i].cost;
  83. console.log("goodsPrice:" + goodsPrice);
  84. if (boxPrice == '' && boxPrice == null) {
  85. boxPrice = 0;
  86. }
  87. total += ((parseFloat(goodsPrice) + parseFloat(boxPrice)) * parseInt(carts[i].num));
  88. }
  89. total = total.toFixed(2)
  90. total2 = total - that.data.minNum;
  91. if (total == 0) {
  92. total = 0.01;
  93. }
  94. if (total2 == 0) {
  95. total2 = 0.01;
  96. }
  97. that.setData({
  98. carts: carts,
  99. total1: total,
  100. total2: total2
  101. })
  102. var host = getApp().globalData.servsers;
  103. var un_id = getApp().globalData.un_id;
  104. var openid = getApp().globalData.openid;
  105. wx.login({
  106. success: function (res) {
  107. //判断开始
  108. if ((un_id == undefined || un_id == '' || un_id == null) || (openid == undefined || openid == '' || openid == null)) {
  109. wx.navigateTo({
  110. url: '/pages/authorize/authorize?link=confirmOrder',
  111. })
  112. }else{
  113. //查询是否绑定手机号
  114. wx.request({
  115. url: host + "userapi/userGetPhone",
  116. data: {
  117. user_id: un_id,
  118. },
  119. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  120. header: {
  121. 'Accept': 'application/json'
  122. },
  123. success: function (res) {
  124. console.log("phone:"+res.data.result);
  125. that.setData({
  126. user_phone: res.data.result
  127. })
  128. if (res == null || res.data == null) {
  129. console.error('网络请求失败');
  130. return;
  131. }
  132. }
  133. });
  134. //查询是否有发票信息
  135. wx.request({
  136. url: host + "invoiceapi/adressview",//发票
  137. data: {
  138. user_id: un_id
  139. },
  140. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  141. header: {
  142. 'Accept': 'application/json'
  143. },
  144. success: function (res) {
  145. var flag = res.data.rows;
  146. if (res.data.total > 0) {
  147. if (res.data.rows[0].invoice_flag == 0) {
  148. that.setData({
  149. invoice_flag: 0,
  150. invoice_send_flag: 0,
  151. invCont: '纸质普通发票',
  152. invoice: true,
  153. invoice_type_name: '纸质普通发票',
  154. invoice_title: '个人',
  155. invoice_name: res.data.rows[0].invoice_name,//个人姓名
  156. invoice_content: res.data.rows[0].invoice_content,//发票内容
  157. invoice_price: res.data.rows[0].invoice_price//金额
  158. })
  159. } else if (res.data.rows[0].invoice_flag == 1) {
  160. that.setData({
  161. invoice_flag: 1,
  162. invoice_send_flag: 1,
  163. invCont: '纸质普通发票',
  164. invoice: true,
  165. invoice_type_name: '纸质普通发票',
  166. invoice_company_title: '单位',
  167. invoice_company_name: res.data.rows[0].invoice_company_name,//单位名称
  168. invoice_number: res.data.rows[0].invoice_number,//纳税人识别号
  169. invoice_content: res.data.rows[0].invoice_content,//发票内容
  170. invoice_price: res.data.rows[0].invoice_price //金额
  171. })
  172. } else {
  173. that.setData({
  174. invoice_flag: 2,
  175. invoice_send_flag: 2,
  176. invCont: '增值税专用发票',
  177. invoice: true,
  178. invoice_type_name: '增值税专用发票',
  179. invoice_company_title: '单位',
  180. invoice_company_name: res.data.rows[0].invoice_company_name,//单位名称
  181. invoice_number: res.data.rows[0].invoice_number,//纳税人识别号
  182. invoice_address: res.data.rows[0].invoice_address,//注册地址
  183. invoice_telphone: res.data.rows[0].invoice_telphone,//注册电话
  184. invoice_brandName: res.data.rows[0].invoice_brandName,//开户银行
  185. invoice_brandNum: res.data.rows[0].invoice_brandNum,//银行账户
  186. invoice_company_content: '明细',//发票内容
  187. invoice_price: res.data.rows[0].invoice_price, //金额
  188. invoice_takeName: res.data.rows[0].invoice_takeName,
  189. invoice_takeTelphone: res.data.rows[0].invoice_takeTelphone,
  190. invoice_takeAddress: res.data.rows[0].invoice_takeAddress
  191. })
  192. }
  193. }
  194. wx.hideNavigationBarLoading(//加载完成后显示页面
  195. that.setData({
  196. hidden: ''
  197. })
  198. )
  199. if (res == null || res.data == null) {
  200. console.error('网络请求失败');
  201. return;
  202. }
  203. }
  204. })
  205. }
  206. //判断结束
  207. }
  208. })
  209. },
  210. onShow:function(){
  211. var that = this;
  212. var host = getApp().globalData.servsers;
  213. var un_id = getApp().globalData.un_id;
  214. },
  215. coupon: function () {//选择优惠劵
  216. wx.navigateTo({//把总价带过去,直接返回优惠之后的总价
  217. url: '/pages/mineCoupon/coupon?total2=' + this.data.total1
  218. })
  219. },
  220. invoice: function () {//我要开发票,把商品价钱带过来
  221. wx.navigateTo({
  222. url: '/pages/invoiceInfor/invoiceInfor?total1=' + this.data.total1
  223. })
  224. },
  225. checkboxChange: function (e) {
  226. var that = this;
  227. var datavalue = e.currentTarget.dataset.value;
  228. var invoice = this.data.invoice;
  229. if (invoice) {
  230. if (datavalue == 0) {
  231. that.setData({
  232. 'value': 1,
  233. 'checked': true,
  234. 'isShow': true,
  235. invCont: that.data.invoice_type_name
  236. })
  237. } else {
  238. that.setData({
  239. 'value': 0,
  240. 'checked': false,
  241. 'isShow': true,
  242. invCont: that.data.invoice_type_name
  243. })
  244. }
  245. }
  246. },
  247. agrService: function (e) {
  248. var that = this;
  249. var datavalue = e.currentTarget.dataset.value;
  250. if (datavalue == 0) {
  251. that.setData({
  252. servalue: 1,
  253. 'serchecked': true,
  254. allowPay : 0
  255. })
  256. } else {
  257. that.setData({
  258. servalue: 0,
  259. 'serchecked': false
  260. })
  261. }
  262. },
  263. save: function (e) {
  264. var that = this;
  265. var user_phone = that.data.user_phone;
  266. var formId = e.detail.formId;
  267. console.log("mineGiftList - formId1:" + formId);
  268. if (user_phone == '' || user_phone == null || user_phone == undefined){
  269. wx.navigateTo({
  270. url: '/pages/bindingPhone/bindingPhone',
  271. })
  272. return false;
  273. }
  274. var pages = getCurrentPages();
  275. var currPage = pages[pages.length - 1]; //当前页面
  276. var prevPage = pages[pages.length - 2]; //上一个页面
  277. var prevPageUrl = prevPage.route;
  278. var len = that.data.carts.length;
  279. wx.showLoading({//支付提示
  280. title: '生成订单', //微信支付
  281. icon: 'loading',
  282. mask: true,
  283. success: function () {
  284. that.setData({
  285. maskBg: true,
  286. allowPay : 1
  287. })
  288. }
  289. })
  290. if (that.data.value == 0) {
  291. that.setData({
  292. invoice_flag: 3,
  293. invoice_send_flag: 3,
  294. invoice_type_name: '',//发票类型名称
  295. invoice_name: '',//个人名字
  296. invoice_content: '',//纸质发票内容
  297. invoice_company_name: '',//发票公司名字(纸质单位和增值单位的公司名称相同)
  298. invoice_number: '', //纳税人识别号
  299. invoice_address: '',//注册地址
  300. invoice_telphone: '',//注册电话
  301. invoice_brandName: '',//开户银行
  302. invoice_brandNum: '',//银行账户
  303. invoice_company_content: '',//增值发票内容
  304. invoice_price: '',//发票金额
  305. invoice_title: '',//纸质个人发票抬头
  306. invoice_company_title: '',//纸质单位和增值发票抬头
  307. invoice_takeName: '',//收货姓名
  308. invoice_takeTelphone: '',//收货人联系方式
  309. invoice_takeAddress: ''//收货人地址
  310. })
  311. }
  312. var timestamp = Date.parse(new Date());
  313. timestamp = timestamp / 1000;
  314. var timestamp1 = timestamp;
  315. var host = getApp().globalData.servsers;
  316. var un_id = getApp().globalData.un_id;
  317. if (un_id != undefined && un_id != '' && un_id != null){
  318. wx.request({
  319. url: host + "orderapi/insertorder",
  320. data: {
  321. order_userid: un_id,
  322. order_sta: '1',
  323. order_number: timestamp,
  324. order_type: '0',
  325. order_Consignee: that.data.consignee,
  326. order_phone: that.data.phone,
  327. order_address: '',
  328. order_commodityid: that.data.carts[0].id,
  329. order_num: that.data.carts[0].num,
  330. order_Price: that.data.total2,//////
  331. orderr_Coupon: that.data.minNum,
  332. coupon_id: that.data.coupon_id,
  333. order_Specifications: '',
  334. order_gixbox: '',
  335. invoice_send_flag: that.data.invoice_send_flag,
  336. invoice_flag: that.data.invoice_flag, //发票标志位
  337. invoice_type_name: that.data.invoice_type_name,//发票类型名称
  338. invoice_name: that.data.invoice_name,//个人名字
  339. invoice_content: that.data.invoice_content,//纸质发票内容
  340. invoice_company_name: that.data.invoice_company_name,//发票公司名字(纸质单位和增值单位的公司名称相同)
  341. invoice_number: that.data.invoice_number, //纳税人识别号
  342. invoice_address: that.data.invoice_address,//注册地址
  343. invoice_telphone: that.data.invoice_telphone,//注册电话
  344. invoice_brandName: that.data.invoice_brandName,//开户银行
  345. invoice_brandNum: that.data.invoice_brandNum,//银行账户
  346. invoice_company_content: that.data.invoice_company_content,//增值发票内容
  347. invoice_price: that.data.invoice_price,//发票金额
  348. invoice_title: that.data.invoice_title,//纸质个人发票抬头
  349. invoice_company_title: that.data.invoice_company_title,//纸质单位和增值发票抬头
  350. invoice_takeName: that.data.invoice_takeName,//收货姓名
  351. invoice_takeTelphone: that.data.invoice_takeTelphone,//收货人联系方式
  352. invoice_takeAddress: that.data.invoice_takeAddress//收货人地址
  353. },
  354. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  355. header: {
  356. 'Accept': 'application/json'
  357. },
  358. success: function (res) {
  359. if (res.data.result == 1){
  360. //插入订单
  361. that.insertOrder(len, timestamp, timestamp1, that.data.coupon_id, un_id, formId);
  362. }else{
  363. //删除当前订单
  364. that.deleteOrder(un_id, timestamp, that.data.coupon_id);
  365. wx.hideLoading();
  366. that.setData({
  367. maskBg: false,
  368. allowPay: 0,
  369. firstClick: 0
  370. })
  371. }
  372. if (res == null || res.data == null || res.data == '') {
  373. console.error('插入订单失败');
  374. //删除当前订单
  375. that.deleteOrder(un_id, timestamp, that.data.coupon_id);
  376. that.setData({
  377. maskBg: false,
  378. allowPay: 0,
  379. firstClick: 0
  380. })
  381. return;
  382. }
  383. }
  384. });
  385. }
  386. },
  387. //插入订单
  388. insertOrder: function (i, timestamp, timestamp1, coupon_id, un_id, formId){
  389. var that = this;
  390. var host = getApp().globalData.servsers;
  391. var stateNum = that.data.stateNum;//记录带有定制的商品数量
  392. if(i == 0){
  393. if (stateNum > 0) {
  394. that.setData({
  395. orderState1: 2//待定制
  396. })
  397. } else {
  398. that.setData({
  399. orderState1: 10//待送礼 10
  400. })
  401. }
  402. //调起支付
  403. // that.payOrder(timestamp, timestamp1, coupon_id, un_id, formId);
  404. wx.showModal({
  405. title: '提示',
  406. content: '订单已生成,后面待上传支付凭证',
  407. })
  408. }else{
  409. i -=1;
  410. var com_id = that.data.carts[i].com_id;
  411. var name = that.data.carts[i].name;//商品名称
  412. //检测商品是否下架
  413. wx.request({
  414. url: host + "commodityapi/commoditview",
  415. data: {
  416. com_id: com_id
  417. },
  418. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  419. header: {
  420. 'Accept': 'application/json'
  421. },
  422. success: function (res) {
  423. //有此商品
  424. if (res.data.total > 0) {
  425. if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
  426. wx.hideLoading();
  427. that.setData({
  428. maskBg: false,
  429. hiddenmodal: false,
  430. modalCont: name + '已下架,请重新选购!'
  431. })
  432. setTimeout(function () {
  433. that.setData({
  434. hiddenmodal: true
  435. })
  436. }, 2000)
  437. //删除当前订单
  438. that.deleteOrder(un_id, timestamp, coupon_id);
  439. wx.navigateBack({
  440. delta:1
  441. })
  442. return false;
  443. } else {
  444. //检测库存
  445. wx.request({
  446. url: host + "api/commodityGroup/checkCommodityGroupRepertory",
  447. data: {
  448. datasheetGroupId: that.data.carts[i].com_group_id
  449. },
  450. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  451. header: {
  452. 'Accept': 'application/json'
  453. },
  454. success: function (res) {
  455. if (res.data.code == '200') {
  456. var canBuy = res.data.data.canBuy;
  457. //库存充足【当前选择的规格库存不为0,且库存数量大于起订量】
  458. if (res.data.data.canBuy == true) {
  459. if (res.data.data.repertory < that.data.carts[i].num) {
  460. wx.hideLoading();
  461. that.setData({
  462. maskBg: false,
  463. hiddenmodal: false,
  464. modalCont: '"' + that.data.carts[i].name + '"的库存不足,请重新选择购买数量!'
  465. })
  466. setTimeout(function () {
  467. that.setData({
  468. hiddenmodal: true
  469. })
  470. //删除当前订单
  471. that.deleteOrder(un_id, timestamp, coupon_id);
  472. wx.navigateBack({
  473. delta: 1
  474. })
  475. }, 3000);
  476. return false;
  477. }else{
  478. var sumNum = parseInt(that.data.carts[i].num);
  479. for (var t = 0; t < i; t++) {
  480. if (that.data.carts[t].com_id == that.data.carts[i].com_id && that.data.carts[t].com_group_id == that.data.carts[i].com_group_id) {
  481. sumNum += parseInt(that.data.carts[t].num);
  482. }
  483. }
  484. console.log("立即购买-sumNum:" + sumNum);
  485. console.log("立即购买-repertory:" + res.data.data.repertory);
  486. if (sumNum > res.data.data.repertory) {
  487. wx.hideLoading();
  488. that.setData({
  489. maskBg: false,
  490. hiddenmodal: false,
  491. modalCont: '“' + that.data.carts[i].name + '”库存不足,请重新选择!'
  492. })
  493. setTimeout(function () {
  494. that.setData({
  495. hiddenmodal: true
  496. })
  497. //删除当前订单
  498. that.deleteOrder(un_id, timestamp, coupon_id);
  499. wx.navigateBack({
  500. delta: 1
  501. })
  502. }, 3000);
  503. return false;
  504. } else {
  505. that.insertEachGoods(i, timestamp, timestamp1, coupon_id, un_id, formId);
  506. }
  507. }
  508. }else{
  509. wx.hideLoading();
  510. that.setData({
  511. maskBg: false,
  512. hiddenmodal: false,
  513. modalCont: '“' + that.data.carts[i].name + '”已售罄,请重新选购!'
  514. })
  515. setTimeout(function () {
  516. that.setData({
  517. hiddenmodal: true
  518. })
  519. //删除当前订单
  520. that.deleteOrder(un_id, timestamp, coupon_id);
  521. wx.navigateBack({
  522. delta: 1
  523. })
  524. }, 3000);
  525. return false;
  526. }
  527. }
  528. }
  529. })
  530. }
  531. } else {
  532. wx.hideLoading();
  533. that.setData({
  534. maskBg: false,
  535. hiddenmodal: false,
  536. modalCont: '“' + that.data.carts[i].name + '”已下架,请重新选购!'
  537. })
  538. setTimeout(function () {
  539. that.setData({
  540. hiddenmodal: true
  541. })
  542. wx.navigateBack({
  543. delta: 1
  544. })
  545. //删除当前订单
  546. that.deleteOrder(un_id, timestamp, coupon_id);
  547. }, 3000);
  548. return false;
  549. }
  550. }
  551. })
  552. }
  553. },
  554. //插入订单中的每一样商品
  555. insertEachGoods: function (i, timestamp, timestamp1, coupon_id, un_id, formId){
  556. var that = this;
  557. var host = getApp().globalData.servsers;
  558. wx.request({
  559. url: host + "orderaffapi/insertOrderAff",
  560. data: {
  561. order_number: timestamp,
  562. cost: that.data.carts[i].cost,
  563. com_id: that.data.carts[i].com_id,
  564. logo: that.data.carts[i].logo,
  565. name: that.data.carts[i].name,
  566. num: that.data.carts[i].num,
  567. process: that.data.carts[i].process,
  568. giftbox_name: that.data.carts[i].giftbox_name,
  569. giftbox_id: that.data.carts[i].giftbox,
  570. box_price: that.data.carts[i].box_price,
  571. process_name: that.data.carts[i].process_name,
  572. process_id: that.data.carts[i].process,
  573. logo: that.data.carts[i].logo,
  574. style1: that.data.carts[i].style1,
  575. style1_name: that.data.carts[i].style1_name,
  576. style2: that.data.carts[i].style2,
  577. style2_name: that.data.carts[i].style2_name,
  578. style3: that.data.carts[i].style3,
  579. style3_name: that.data.carts[i].style3_name,
  580. style4: that.data.carts[i].style4,
  581. style4_name: that.data.carts[i].style4_name,
  582. style5: that.data.carts[i].style5,
  583. style5_name: that.data.carts[i].style5_name,
  584. comGroupId: that.data.carts[i].com_group_id
  585. },
  586. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  587. header: {
  588. 'Accept': 'application/json'
  589. },
  590. success: function (res) {
  591. console.log("插入");
  592. console.log(res);
  593. if ((that.data.carts[i].process_name != '') && (that.data.carts[i].process != -1)) {
  594. that.setData({
  595. stateNum: that.data.stateNum + 1
  596. })
  597. }
  598. //插入订单
  599. that.insertOrder(i, timestamp, timestamp1, coupon_id, un_id, formId);
  600. console.log("插入2");
  601. if (res == null || res.data == null || res.data == '') {
  602. //删除当前订单
  603. that.deleteOrder(un_id, timestamp, coupon_id);
  604. that.setData({
  605. maskBg: false,
  606. allowPay: 0,
  607. firstClick: 0
  608. })
  609. console.error('插入订单失败');
  610. return;
  611. }
  612. }
  613. })
  614. },
  615. //删除订单
  616. deleteOrder: function (un_id, timestamp, coupon_id){
  617. var that = this;
  618. var host = getApp().globalData.servsers;
  619. wx.request({
  620. url: host + "orderapi/deleteorder",
  621. data: {
  622. order_userid: un_id,
  623. order_number: timestamp,
  624. coupon_id: coupon_id,
  625. },
  626. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  627. header: {
  628. 'Accept': 'application/json'
  629. },
  630. success: function (res) {
  631. if (res == null || res.data == null) {
  632. console.error('已下架-删除订单:失败');
  633. return;
  634. }
  635. }
  636. });
  637. },
  638. //调起付款
  639. payOrder:function(timestamp, timestamp1, coupon_id, un_id, formId){
  640. console.log("pay");
  641. console.log("pay-formId:" + formId);
  642. var that = this;
  643. var pages = getCurrentPages();
  644. var currPage = pages[pages.length - 1]; //当前页面
  645. var prevPage = pages[pages.length - 2]; //上一个页面
  646. var prevPageUrl = prevPage.route;
  647. var host = getApp().globalData.servsers;
  648. var carts = that.data.carts;
  649. var timestamp = String(Date.parse(new Date())); //时间戳
  650. //插入用于模板消息的formId
  651. wx.request({
  652. url: host + "userapi/insertadduserwxsend",
  653. data: {
  654. user_id: un_id,
  655. form_id_fh: formId,
  656. order_number: timestamp,
  657. },
  658. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  659. header: {
  660. 'Accept': 'application/json'
  661. },
  662. success: function (res) {
  663. console.log("formid");
  664. console.log(res);
  665. if (res == null || res.data == null) {
  666. console.error('插入formId失败');
  667. return;
  668. }
  669. }
  670. })
  671. //获取用户登录状态
  672. wx.login({
  673. success: function (res) {
  674. //发起网络请求,发起的是HTTPS请求,向服务端请求预支付
  675. var code = res.code;
  676. var title = that.data.carts[0].name;
  677. var price = that.data.total2 * 100;
  678. price = 1;
  679. wx.request({
  680. url: host + 'prepay',
  681. data: {
  682. code: code,
  683. price: price,
  684. title: title,
  685. order_number: timestamp1,
  686. userid: un_id
  687. },
  688. success: function (res) {
  689. console.log("pay:");
  690. console.log(res);
  691. if (res.data.result) {
  692. wx.hideLoading();
  693. that.setData({
  694. maskBg: false
  695. })
  696. if (res.data.result == true) {
  697. var nonceStr = res.data.nonceStr;
  698. var prepayId = res.data.prepayId;
  699. // 按照字段首字母排序组成新字符串
  700. var payDataA = "appId=wx9e3f68fa2172f1c7&nonceStr=" + res.data.nonceStr + "&package=prepay_id=" + res.data.prepayId + "&signType=MD5&timeStamp=" + timestamp;
  701. var payDataB = payDataA + "&key=MlxMZWpZidKxCPPaOOztMP84XvQzqSOh";
  702. // 使用MD5加密算法计算加密字符串
  703. paySign = MD5Util.MD5(payDataB).toUpperCase();
  704. // 使用MD5加密算法计算加密字符串
  705. var paySign = MD5Util.MD5(payDataB).toUpperCase();
  706. // 发起微信支付
  707. wx.requestPayment({
  708. 'timeStamp': timestamp,
  709. 'nonceStr': nonceStr,
  710. 'package': 'prepay_id=' + prepayId,
  711. 'signType': 'MD5',
  712. 'paySign': paySign,
  713. 'success': function (res) {
  714. console.log(111);
  715. console.log(res);
  716. wx.request({
  717. url: host + "orderapi/update",
  718. data: {
  719. order_sta: that.data.orderState1,
  720. order_number: timestamp1,
  721. orderState: that.data.orderState1,
  722. order_userid: un_id
  723. },
  724. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  725. header: {
  726. 'Accept': 'application/json'
  727. },
  728. success: function (res) {
  729. console.log("支付");
  730. console.log(res);
  731. that.setData({
  732. maskBg: false,
  733. hiddenmodal: false,
  734. modalCont: '支付成功'
  735. })
  736. setTimeout(function () {
  737. that.setData({
  738. hiddenmodal: true
  739. })
  740. wx.redirectTo({
  741. url: '/pages/orderList/orderList?isSelect=0'
  742. })
  743. }, 2000);
  744. if (res == null || res.data == null) {
  745. //删除当前订单
  746. that.deleteOrder(un_id, timestamp, that.data.coupon_id);
  747. that.setData({
  748. maskBg: false,
  749. allowPay: 0,
  750. firstClick: 0
  751. })
  752. return false;
  753. }
  754. }
  755. });
  756. },
  757. 'fail': function (res) {
  758. that.setData({
  759. maskBg: false,
  760. hiddenmodal: false,
  761. modalCont: '取消支付'
  762. })
  763. setTimeout(function () {
  764. that.setData({
  765. hiddenmodal: true
  766. })
  767. wx.redirectTo({
  768. url: '/pages/orderList/orderList?isSelect=0'
  769. })
  770. }, 2000);
  771. }
  772. })
  773. //如果是从购物车进入支付,则清空对应购物车商品
  774. if (prevPageUrl == 'pages/car/index' || prevPageUrl == 'pages/carInsert/carInsert') {
  775. for (var i = 0; i < that.data.carts.length; i++) {
  776. wx.request({
  777. url: host + "shoppingcartapi/deleteshoppingcart",
  778. data: {
  779. id: that.data.carts[i].id
  780. },
  781. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  782. header: {
  783. 'Accept': 'application/json'
  784. },
  785. success: function (res) {
  786. if (res == null || res.data == null) {
  787. console.error('清空对应购物车商品失败');
  788. return;
  789. }
  790. }
  791. });
  792. }
  793. }
  794. } else {
  795. that.setData({
  796. maskBg: false,
  797. allowPay: 0,
  798. firstClick: 0
  799. })
  800. //删除当前订单
  801. that.deleteOrder(un_id, timestamp, coupon_id);
  802. return false;
  803. }
  804. } else {
  805. that.setData({
  806. maskBg: false,
  807. allowPay: 0,
  808. firstClick: 0,
  809. hiddenmodal: false,
  810. modalCont: '网络错误,请重新操作!'
  811. })
  812. setTimeout(function () {
  813. that.setData({
  814. hiddenmodal: true,
  815. allowPay: 0
  816. })
  817. //删除当前订单
  818. that.deleteOrder(un_id, timestamp, coupon_id);
  819. }, 3000);
  820. return false;
  821. }
  822. }
  823. })
  824. }
  825. })
  826. },
  827. })
  828. var that;
  829. var Util = require('../../utils/util.js');