confirmOrder.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. wx.showLoading({
  275. title: '订单合成中',
  276. })
  277. var pages = getCurrentPages();
  278. var currPage = pages[pages.length - 1]; //当前页面
  279. var prevPage = pages[pages.length - 2]; //上一个页面
  280. var prevPageUrl = prevPage.route;
  281. var len = that.data.carts.length;
  282. // wx.showLoading({//支付提示
  283. // title: '生成订单', //微信支付
  284. // icon: 'loading',
  285. // mask: true,
  286. // success: function () {
  287. // that.setData({
  288. // maskBg: true,
  289. // allowPay: 1
  290. // })
  291. // }
  292. // })
  293. that.setData({
  294. maskBg: true,
  295. allowPay: 1
  296. })
  297. if (that.data.value == 0) {
  298. that.setData({
  299. invoice_flag: 3,
  300. invoice_send_flag: 3,
  301. invoice_type_name: '',//发票类型名称
  302. invoice_name: '',//个人名字
  303. invoice_content: '',//纸质发票内容
  304. invoice_company_name: '',//发票公司名字(纸质单位和增值单位的公司名称相同)
  305. invoice_number: '', //纳税人识别号
  306. invoice_address: '',//注册地址
  307. invoice_telphone: '',//注册电话
  308. invoice_brandName: '',//开户银行
  309. invoice_brandNum: '',//银行账户
  310. invoice_company_content: '',//增值发票内容
  311. invoice_price: '',//发票金额
  312. invoice_title: '',//纸质个人发票抬头
  313. invoice_company_title: '',//纸质单位和增值发票抬头
  314. invoice_takeName: '',//收货姓名
  315. invoice_takeTelphone: '',//收货人联系方式
  316. invoice_takeAddress: ''//收货人地址
  317. })
  318. }
  319. var timestamp = Date.parse(new Date());
  320. timestamp = timestamp / 1000;
  321. var timestamp1 = timestamp;
  322. var host = getApp().globalData.servsers;
  323. var un_id = getApp().globalData.un_id;
  324. if (un_id != undefined && un_id != '' && un_id != null){
  325. wx.request({
  326. url: host + "orderapi/insertorder",
  327. data: {
  328. order_userid: un_id,
  329. order_sta: '1',
  330. order_number: timestamp,
  331. order_type: '0',
  332. order_Consignee: that.data.consignee,
  333. order_phone: that.data.phone,
  334. order_address: '',
  335. order_commodityid: that.data.carts[0].id,
  336. order_num: that.data.carts[0].num,
  337. order_Price: that.data.total2,//////
  338. orderr_Coupon: that.data.minNum,
  339. coupon_id: that.data.coupon_id,
  340. order_Specifications: '',
  341. order_gixbox: '',
  342. invoice_send_flag: that.data.invoice_send_flag,
  343. invoice_flag: that.data.invoice_flag, //发票标志位
  344. invoice_type_name: that.data.invoice_type_name,//发票类型名称
  345. invoice_name: that.data.invoice_name,//个人名字
  346. invoice_content: that.data.invoice_content,//纸质发票内容
  347. invoice_company_name: that.data.invoice_company_name,//发票公司名字(纸质单位和增值单位的公司名称相同)
  348. invoice_number: that.data.invoice_number, //纳税人识别号
  349. invoice_address: that.data.invoice_address,//注册地址
  350. invoice_telphone: that.data.invoice_telphone,//注册电话
  351. invoice_brandName: that.data.invoice_brandName,//开户银行
  352. invoice_brandNum: that.data.invoice_brandNum,//银行账户
  353. invoice_company_content: that.data.invoice_company_content,//增值发票内容
  354. invoice_price: that.data.invoice_price,//发票金额
  355. invoice_title: that.data.invoice_title,//纸质个人发票抬头
  356. invoice_company_title: that.data.invoice_company_title,//纸质单位和增值发票抬头
  357. invoice_takeName: that.data.invoice_takeName,//收货姓名
  358. invoice_takeTelphone: that.data.invoice_takeTelphone,//收货人联系方式
  359. invoice_takeAddress: that.data.invoice_takeAddress//收货人地址
  360. },
  361. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  362. header: {
  363. 'Accept': 'application/json'
  364. },
  365. success: function (res) {
  366. if (res.data.result == 1){
  367. //插入订单
  368. that.insertOrder(len, timestamp, timestamp1, that.data.coupon_id, un_id, formId);
  369. }else{
  370. //删除当前订单
  371. that.deleteOrder(un_id, timestamp, that.data.coupon_id);
  372. wx.hideLoading();
  373. that.setData({
  374. maskBg: false,
  375. allowPay: 0,
  376. firstClick: 0
  377. })
  378. }
  379. if (res == null || res.data == null || res.data == '') {
  380. console.error('插入订单失败');
  381. //删除当前订单
  382. that.deleteOrder(un_id, timestamp, that.data.coupon_id);
  383. that.setData({
  384. maskBg: false,
  385. allowPay: 0,
  386. firstClick: 0
  387. })
  388. return;
  389. }
  390. }
  391. });
  392. }
  393. },
  394. //插入订单
  395. insertOrder: function (i, timestamp, timestamp1, coupon_id, un_id, formId){
  396. var that = this;
  397. var host = getApp().globalData.servsers;
  398. var stateNum = that.data.stateNum;//记录带有定制的商品数量
  399. if(i == 0){
  400. if (stateNum > 0) {
  401. that.setData({
  402. orderState1: 2//待定制
  403. })
  404. } else {
  405. that.setData({
  406. orderState1: 10//待送礼 10
  407. })
  408. }
  409. //调起支付
  410. // that.payOrder(timestamp, timestamp1, coupon_id, un_id, formId);
  411. wx.showToast({
  412. title: '订单已生成,后面待上传支付凭证',
  413. icon:'none'
  414. })
  415. }else{
  416. i -=1;
  417. var com_id = that.data.carts[i].com_id;
  418. var name = that.data.carts[i].name;//商品名称
  419. //检测商品是否下架
  420. wx.request({
  421. url: host + "commodityapi/commoditview",
  422. data: {
  423. com_id: com_id
  424. },
  425. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  426. header: {
  427. 'Accept': 'application/json'
  428. },
  429. success: function (res) {
  430. //有此商品
  431. if (res.data.total > 0) {
  432. if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
  433. wx.hideLoading();
  434. that.setData({
  435. maskBg: false,
  436. hiddenmodal: false,
  437. modalCont: name + '已下架,请重新选购!'
  438. })
  439. setTimeout(function () {
  440. that.setData({
  441. hiddenmodal: true
  442. })
  443. }, 2000)
  444. //删除当前订单
  445. that.deleteOrder(un_id, timestamp, coupon_id);
  446. wx.navigateBack({
  447. delta:1
  448. })
  449. return false;
  450. } else {
  451. //检测库存
  452. wx.request({
  453. url: host + "api/commodityGroup/checkCommodityGroupRepertory",
  454. data: {
  455. datasheetGroupId: that.data.carts[i].com_group_id
  456. },
  457. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  458. header: {
  459. 'Accept': 'application/json'
  460. },
  461. success: function (res) {
  462. if (res.data.code == '200') {
  463. var canBuy = res.data.data.canBuy;
  464. //库存充足【当前选择的规格库存不为0,且库存数量大于起订量】
  465. if (res.data.data.canBuy == true) {
  466. if (res.data.data.repertory < that.data.carts[i].num) {
  467. wx.hideLoading();
  468. that.setData({
  469. maskBg: false,
  470. hiddenmodal: false,
  471. modalCont: '"' + that.data.carts[i].name + '"的库存不足,请重新选择购买数量!'
  472. })
  473. setTimeout(function () {
  474. that.setData({
  475. hiddenmodal: true
  476. })
  477. //删除当前订单
  478. that.deleteOrder(un_id, timestamp, coupon_id);
  479. wx.navigateBack({
  480. delta: 1
  481. })
  482. }, 3000);
  483. return false;
  484. }else{
  485. var sumNum = parseInt(that.data.carts[i].num);
  486. for (var t = 0; t < i; t++) {
  487. 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) {
  488. sumNum += parseInt(that.data.carts[t].num);
  489. }
  490. }
  491. console.log("立即购买-sumNum:" + sumNum);
  492. console.log("立即购买-repertory:" + res.data.data.repertory);
  493. if (sumNum > res.data.data.repertory) {
  494. wx.hideLoading();
  495. that.setData({
  496. maskBg: false,
  497. hiddenmodal: false,
  498. modalCont: '“' + that.data.carts[i].name + '”库存不足,请重新选择!'
  499. })
  500. setTimeout(function () {
  501. that.setData({
  502. hiddenmodal: true
  503. })
  504. //删除当前订单
  505. that.deleteOrder(un_id, timestamp, coupon_id);
  506. wx.navigateBack({
  507. delta: 1
  508. })
  509. }, 3000);
  510. return false;
  511. } else {
  512. that.insertEachGoods(i, timestamp, timestamp1, coupon_id, un_id, formId);
  513. }
  514. }
  515. }else{
  516. wx.hideLoading();
  517. that.setData({
  518. maskBg: false,
  519. hiddenmodal: false,
  520. modalCont: '“' + that.data.carts[i].name + '”已售罄,请重新选购!'
  521. })
  522. setTimeout(function () {
  523. that.setData({
  524. hiddenmodal: true
  525. })
  526. //删除当前订单
  527. that.deleteOrder(un_id, timestamp, coupon_id);
  528. wx.navigateBack({
  529. delta: 1
  530. })
  531. }, 3000);
  532. return false;
  533. }
  534. }
  535. }
  536. })
  537. }
  538. } else {
  539. wx.hideLoading();
  540. that.setData({
  541. maskBg: false,
  542. hiddenmodal: false,
  543. modalCont: '“' + that.data.carts[i].name + '”已下架,请重新选购!'
  544. })
  545. setTimeout(function () {
  546. that.setData({
  547. hiddenmodal: true
  548. })
  549. wx.navigateBack({
  550. delta: 1
  551. })
  552. //删除当前订单
  553. that.deleteOrder(un_id, timestamp, coupon_id);
  554. }, 3000);
  555. return false;
  556. }
  557. }
  558. })
  559. }
  560. setTimeout(()=>{
  561. wx.hideLoading();
  562. wx.navigateTo({
  563. url: '/pages/orderCanvas/orderCanavs',
  564. })
  565. },1000)
  566. },
  567. //插入订单中的每一样商品
  568. insertEachGoods: function (i, timestamp, timestamp1, coupon_id, un_id, formId){
  569. var that = this;
  570. var host = getApp().globalData.servsers;
  571. wx.request({
  572. url: host + "orderaffapi/insertOrderAff",
  573. data: {
  574. order_number: timestamp,
  575. cost: that.data.carts[i].cost,
  576. com_id: that.data.carts[i].com_id,
  577. logo: that.data.carts[i].logo,
  578. name: that.data.carts[i].name,
  579. num: that.data.carts[i].num,
  580. process: that.data.carts[i].process,
  581. giftbox_name: that.data.carts[i].giftbox_name,
  582. giftbox_id: that.data.carts[i].giftbox,
  583. box_price: that.data.carts[i].box_price,
  584. process_name: that.data.carts[i].process_name,
  585. process_id: that.data.carts[i].process,
  586. logo: that.data.carts[i].logo,
  587. style1: that.data.carts[i].style1,
  588. style1_name: that.data.carts[i].style1_name,
  589. style2: that.data.carts[i].style2,
  590. style2_name: that.data.carts[i].style2_name,
  591. style3: that.data.carts[i].style3,
  592. style3_name: that.data.carts[i].style3_name,
  593. style4: that.data.carts[i].style4,
  594. style4_name: that.data.carts[i].style4_name,
  595. style5: that.data.carts[i].style5,
  596. style5_name: that.data.carts[i].style5_name,
  597. comGroupId: that.data.carts[i].com_group_id
  598. },
  599. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  600. header: {
  601. 'Accept': 'application/json'
  602. },
  603. success: function (res) {
  604. console.log("插入");
  605. console.log(res);
  606. if ((that.data.carts[i].process_name != '') && (that.data.carts[i].process != -1)) {
  607. that.setData({
  608. stateNum: that.data.stateNum + 1
  609. })
  610. }
  611. //插入订单
  612. that.insertOrder(i, timestamp, timestamp1, coupon_id, un_id, formId);
  613. console.log("插入2");
  614. if (res == null || res.data == null || res.data == '') {
  615. //删除当前订单
  616. that.deleteOrder(un_id, timestamp, coupon_id);
  617. that.setData({
  618. maskBg: false,
  619. allowPay: 0,
  620. firstClick: 0
  621. })
  622. console.error('插入订单失败');
  623. return;
  624. }
  625. }
  626. })
  627. },
  628. //删除订单
  629. deleteOrder: function (un_id, timestamp, coupon_id){
  630. var that = this;
  631. var host = getApp().globalData.servsers;
  632. wx.request({
  633. url: host + "orderapi/deleteorder",
  634. data: {
  635. order_userid: un_id,
  636. order_number: timestamp,
  637. coupon_id: coupon_id,
  638. },
  639. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  640. header: {
  641. 'Accept': 'application/json'
  642. },
  643. success: function (res) {
  644. if (res == null || res.data == null) {
  645. console.error('已下架-删除订单:失败');
  646. return;
  647. }
  648. }
  649. });
  650. },
  651. //调起付款
  652. payOrder:function(timestamp, timestamp1, coupon_id, un_id, formId){
  653. console.log("pay");
  654. console.log("pay-formId:" + formId);
  655. var that = this;
  656. var pages = getCurrentPages();
  657. var currPage = pages[pages.length - 1]; //当前页面
  658. var prevPage = pages[pages.length - 2]; //上一个页面
  659. var prevPageUrl = prevPage.route;
  660. var host = getApp().globalData.servsers;
  661. var carts = that.data.carts;
  662. var timestamp = String(Date.parse(new Date())); //时间戳
  663. //插入用于模板消息的formId
  664. wx.request({
  665. url: host + "userapi/insertadduserwxsend",
  666. data: {
  667. user_id: un_id,
  668. form_id_fh: formId,
  669. order_number: timestamp,
  670. },
  671. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  672. header: {
  673. 'Accept': 'application/json'
  674. },
  675. success: function (res) {
  676. console.log("formid");
  677. console.log(res);
  678. if (res == null || res.data == null) {
  679. console.error('插入formId失败');
  680. return;
  681. }
  682. }
  683. })
  684. //获取用户登录状态
  685. wx.login({
  686. success: function (res) {
  687. //发起网络请求,发起的是HTTPS请求,向服务端请求预支付
  688. var code = res.code;
  689. var title = that.data.carts[0].name;
  690. var price = that.data.total2 * 100;
  691. price = 1;
  692. wx.request({
  693. url: host + 'prepay',
  694. data: {
  695. code: code,
  696. price: price,
  697. title: title,
  698. order_number: timestamp1,
  699. userid: un_id
  700. },
  701. success: function (res) {
  702. console.log("pay:");
  703. console.log(res);
  704. if (res.data.result) {
  705. wx.hideLoading();
  706. that.setData({
  707. maskBg: false
  708. })
  709. if (res.data.result == true) {
  710. var nonceStr = res.data.nonceStr;
  711. var prepayId = res.data.prepayId;
  712. // 按照字段首字母排序组成新字符串
  713. var payDataA = "appId=wx9e3f68fa2172f1c7&nonceStr=" + res.data.nonceStr + "&package=prepay_id=" + res.data.prepayId + "&signType=MD5&timeStamp=" + timestamp;
  714. var payDataB = payDataA + "&key=MlxMZWpZidKxCPPaOOztMP84XvQzqSOh";
  715. // 使用MD5加密算法计算加密字符串
  716. paySign = MD5Util.MD5(payDataB).toUpperCase();
  717. // 使用MD5加密算法计算加密字符串
  718. var paySign = MD5Util.MD5(payDataB).toUpperCase();
  719. // 发起微信支付
  720. wx.requestPayment({
  721. 'timeStamp': timestamp,
  722. 'nonceStr': nonceStr,
  723. 'package': 'prepay_id=' + prepayId,
  724. 'signType': 'MD5',
  725. 'paySign': paySign,
  726. 'success': function (res) {
  727. console.log(111);
  728. console.log(res);
  729. wx.request({
  730. url: host + "orderapi/update",
  731. data: {
  732. order_sta: that.data.orderState1,
  733. order_number: timestamp1,
  734. orderState: that.data.orderState1,
  735. order_userid: un_id
  736. },
  737. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  738. header: {
  739. 'Accept': 'application/json'
  740. },
  741. success: function (res) {
  742. console.log("支付");
  743. console.log(res);
  744. that.setData({
  745. maskBg: false,
  746. hiddenmodal: false,
  747. modalCont: '支付成功'
  748. })
  749. setTimeout(function () {
  750. that.setData({
  751. hiddenmodal: true
  752. })
  753. wx.redirectTo({
  754. url: '/pages/orderList/orderList?isSelect=0'
  755. })
  756. }, 2000);
  757. if (res == null || res.data == null) {
  758. //删除当前订单
  759. that.deleteOrder(un_id, timestamp, that.data.coupon_id);
  760. that.setData({
  761. maskBg: false,
  762. allowPay: 0,
  763. firstClick: 0
  764. })
  765. return false;
  766. }
  767. }
  768. });
  769. },
  770. 'fail': function (res) {
  771. that.setData({
  772. maskBg: false,
  773. hiddenmodal: false,
  774. modalCont: '取消支付'
  775. })
  776. setTimeout(function () {
  777. that.setData({
  778. hiddenmodal: true
  779. })
  780. wx.redirectTo({
  781. url: '/pages/orderList/orderList?isSelect=0'
  782. })
  783. }, 2000);
  784. }
  785. })
  786. //如果是从购物车进入支付,则清空对应购物车商品
  787. if (prevPageUrl == 'pages/car/index' || prevPageUrl == 'pages/carInsert/carInsert') {
  788. for (var i = 0; i < that.data.carts.length; i++) {
  789. wx.request({
  790. url: host + "shoppingcartapi/deleteshoppingcart",
  791. data: {
  792. id: that.data.carts[i].id
  793. },
  794. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  795. header: {
  796. 'Accept': 'application/json'
  797. },
  798. success: function (res) {
  799. if (res == null || res.data == null) {
  800. console.error('清空对应购物车商品失败');
  801. return;
  802. }
  803. }
  804. });
  805. }
  806. }
  807. } else {
  808. that.setData({
  809. maskBg: false,
  810. allowPay: 0,
  811. firstClick: 0
  812. })
  813. //删除当前订单
  814. that.deleteOrder(un_id, timestamp, coupon_id);
  815. return false;
  816. }
  817. } else {
  818. that.setData({
  819. maskBg: false,
  820. allowPay: 0,
  821. firstClick: 0,
  822. hiddenmodal: false,
  823. modalCont: '网络错误,请重新操作!'
  824. })
  825. setTimeout(function () {
  826. that.setData({
  827. hiddenmodal: true,
  828. allowPay: 0
  829. })
  830. //删除当前订单
  831. that.deleteOrder(un_id, timestamp, coupon_id);
  832. }, 3000);
  833. return false;
  834. }
  835. }
  836. })
  837. }
  838. })
  839. },
  840. })
  841. var that;
  842. var Util = require('../../utils/util.js');