confirmOrder.js 31 KB

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