confirmOrder.js 31 KB

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