confirmOrder.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. }else{
  405. i -=1;
  406. var com_id = that.data.carts[i].com_id;
  407. var name = that.data.carts[i].name;//商品名称
  408. //检测商品是否下架
  409. wx.request({
  410. url: host + "commodityapi/commoditview",
  411. data: {
  412. com_id: com_id
  413. },
  414. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  415. header: {
  416. 'Accept': 'application/json'
  417. },
  418. success: function (res) {
  419. //有此商品
  420. if (res.data.total > 0) {
  421. if (res.data.commodity.commodity_flag != 0) {//此商品为下架商品
  422. wx.hideLoading();
  423. that.setData({
  424. maskBg: false,
  425. hiddenmodal: false,
  426. modalCont: name + '已下架,请重新选购!'
  427. })
  428. setTimeout(function () {
  429. that.setData({
  430. hiddenmodal: true
  431. })
  432. }, 2000)
  433. //删除当前订单
  434. that.deleteOrder(un_id, timestamp, coupon_id);
  435. wx.navigateBack({
  436. delta:1
  437. })
  438. return false;
  439. } else {
  440. //检测库存
  441. wx.request({
  442. url: host + "api/commodityGroup/checkCommodityGroupRepertory",
  443. data: {
  444. datasheetGroupId: that.data.carts[i].com_group_id
  445. },
  446. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  447. header: {
  448. 'Accept': 'application/json'
  449. },
  450. success: function (res) {
  451. if (res.data.code == '200') {
  452. var canBuy = res.data.data.canBuy;
  453. //库存充足【当前选择的规格库存不为0,且库存数量大于起订量】
  454. if (res.data.data.canBuy == true) {
  455. if (res.data.data.repertory < that.data.carts[i].num) {
  456. wx.hideLoading();
  457. that.setData({
  458. maskBg: false,
  459. hiddenmodal: false,
  460. modalCont: '"' + that.data.carts[i].name + '"的库存不足,请重新选择购买数量!'
  461. })
  462. setTimeout(function () {
  463. that.setData({
  464. hiddenmodal: true
  465. })
  466. //删除当前订单
  467. that.deleteOrder(un_id, timestamp, coupon_id);
  468. wx.navigateBack({
  469. delta: 1
  470. })
  471. }, 3000);
  472. return false;
  473. }else{
  474. var sumNum = parseInt(that.data.carts[i].num);
  475. for (var t = 0; t < i; t++) {
  476. 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) {
  477. sumNum += parseInt(that.data.carts[t].num);
  478. }
  479. }
  480. console.log("立即购买-sumNum:" + sumNum);
  481. console.log("立即购买-repertory:" + res.data.data.repertory);
  482. if (sumNum > res.data.data.repertory) {
  483. wx.hideLoading();
  484. that.setData({
  485. maskBg: false,
  486. hiddenmodal: false,
  487. modalCont: '“' + that.data.carts[i].name + '”库存不足,请重新选择!'
  488. })
  489. setTimeout(function () {
  490. that.setData({
  491. hiddenmodal: true
  492. })
  493. //删除当前订单
  494. that.deleteOrder(un_id, timestamp, coupon_id);
  495. wx.navigateBack({
  496. delta: 1
  497. })
  498. }, 3000);
  499. return false;
  500. } else {
  501. that.insertEachGoods(i, timestamp, timestamp1, coupon_id, un_id, formId);
  502. }
  503. }
  504. }else{
  505. wx.hideLoading();
  506. that.setData({
  507. maskBg: false,
  508. hiddenmodal: false,
  509. modalCont: '“' + that.data.carts[i].name + '”已售罄,请重新选购!'
  510. })
  511. setTimeout(function () {
  512. that.setData({
  513. hiddenmodal: true
  514. })
  515. //删除当前订单
  516. that.deleteOrder(un_id, timestamp, coupon_id);
  517. wx.navigateBack({
  518. delta: 1
  519. })
  520. }, 3000);
  521. return false;
  522. }
  523. }
  524. }
  525. })
  526. }
  527. } else {
  528. wx.hideLoading();
  529. that.setData({
  530. maskBg: false,
  531. hiddenmodal: false,
  532. modalCont: '“' + that.data.carts[i].name + '”已下架,请重新选购!'
  533. })
  534. setTimeout(function () {
  535. that.setData({
  536. hiddenmodal: true
  537. })
  538. wx.navigateBack({
  539. delta: 1
  540. })
  541. //删除当前订单
  542. that.deleteOrder(un_id, timestamp, coupon_id);
  543. }, 3000);
  544. return false;
  545. }
  546. }
  547. })
  548. }
  549. },
  550. //插入订单中的每一样商品
  551. insertEachGoods: function (i, timestamp, timestamp1, coupon_id, un_id, formId){
  552. var that = this;
  553. var host = getApp().globalData.servsers;
  554. wx.request({
  555. url: host + "orderaffapi/insertOrderAff",
  556. data: {
  557. order_number: timestamp,
  558. cost: that.data.carts[i].cost,
  559. com_id: that.data.carts[i].com_id,
  560. logo: that.data.carts[i].logo,
  561. name: that.data.carts[i].name,
  562. num: that.data.carts[i].num,
  563. process: that.data.carts[i].process,
  564. giftbox_name: that.data.carts[i].giftbox_name,
  565. giftbox_id: that.data.carts[i].giftbox,
  566. box_price: that.data.carts[i].box_price,
  567. process_name: that.data.carts[i].process_name,
  568. process_id: that.data.carts[i].process,
  569. logo: that.data.carts[i].logo,
  570. style1: that.data.carts[i].style1,
  571. style1_name: that.data.carts[i].style1_name,
  572. style2: that.data.carts[i].style2,
  573. style2_name: that.data.carts[i].style2_name,
  574. style3: that.data.carts[i].style3,
  575. style3_name: that.data.carts[i].style3_name,
  576. style4: that.data.carts[i].style4,
  577. style4_name: that.data.carts[i].style4_name,
  578. style5: that.data.carts[i].style5,
  579. style5_name: that.data.carts[i].style5_name,
  580. comGroupId: that.data.carts[i].com_group_id
  581. },
  582. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  583. header: {
  584. 'Accept': 'application/json'
  585. },
  586. success: function (res) {
  587. console.log("插入");
  588. console.log(res);
  589. if ((that.data.carts[i].process_name != '') && (that.data.carts[i].process != -1)) {
  590. that.setData({
  591. stateNum: that.data.stateNum + 1
  592. })
  593. }
  594. //插入订单
  595. that.insertOrder(i, timestamp, timestamp1, coupon_id, un_id, formId);
  596. console.log("插入2");
  597. if (res == null || res.data == null || res.data == '') {
  598. //删除当前订单
  599. that.deleteOrder(un_id, timestamp, coupon_id);
  600. that.setData({
  601. maskBg: false,
  602. allowPay: 0,
  603. firstClick: 0
  604. })
  605. console.error('插入订单失败');
  606. return;
  607. }
  608. }
  609. })
  610. },
  611. //删除订单
  612. deleteOrder: function (un_id, timestamp, coupon_id){
  613. var that = this;
  614. var host = getApp().globalData.servsers;
  615. wx.request({
  616. url: host + "orderapi/deleteorder",
  617. data: {
  618. order_userid: un_id,
  619. order_number: timestamp,
  620. coupon_id: coupon_id,
  621. },
  622. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  623. header: {
  624. 'Accept': 'application/json'
  625. },
  626. success: function (res) {
  627. if (res == null || res.data == null) {
  628. console.error('已下架-删除订单:失败');
  629. return;
  630. }
  631. }
  632. });
  633. },
  634. //调起付款
  635. payOrder:function(timestamp, timestamp1, coupon_id, un_id, formId){
  636. console.log("pay");
  637. console.log("pay-formId:" + formId);
  638. var that = this;
  639. var pages = getCurrentPages();
  640. var currPage = pages[pages.length - 1]; //当前页面
  641. var prevPage = pages[pages.length - 2]; //上一个页面
  642. var prevPageUrl = prevPage.route;
  643. var host = getApp().globalData.servsers;
  644. var carts = that.data.carts;
  645. var timestamp = String(Date.parse(new Date())); //时间戳
  646. //插入用于模板消息的formId
  647. wx.request({
  648. url: host + "userapi/insertadduserwxsend",
  649. data: {
  650. user_id: un_id,
  651. form_id_fh: formId,
  652. order_number: timestamp,
  653. },
  654. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  655. header: {
  656. 'Accept': 'application/json'
  657. },
  658. success: function (res) {
  659. console.log("formid");
  660. console.log(res);
  661. if (res == null || res.data == null) {
  662. console.error('插入formId失败');
  663. return;
  664. }
  665. }
  666. })
  667. //获取用户登录状态
  668. wx.login({
  669. success: function (res) {
  670. //发起网络请求,发起的是HTTPS请求,向服务端请求预支付
  671. var code = res.code;
  672. var title = that.data.carts[0].name;
  673. var price = that.data.total2 * 100;
  674. price = 1;
  675. wx.request({
  676. url: host + 'prepay',
  677. data: {
  678. code: code,
  679. price: price,
  680. title: title,
  681. order_number: timestamp1,
  682. userid: un_id
  683. },
  684. success: function (res) {
  685. console.log("pay:");
  686. console.log(res);
  687. if (res.data.result) {
  688. wx.hideLoading();
  689. that.setData({
  690. maskBg: false
  691. })
  692. if (res.data.result == true) {
  693. var nonceStr = res.data.nonceStr;
  694. var prepayId = res.data.prepayId;
  695. // 按照字段首字母排序组成新字符串
  696. var payDataA = "appId=wx9e3f68fa2172f1c7&nonceStr=" + res.data.nonceStr + "&package=prepay_id=" + res.data.prepayId + "&signType=MD5&timeStamp=" + timestamp;
  697. var payDataB = payDataA + "&key=MlxMZWpZidKxCPPaOOztMP84XvQzqSOh";
  698. // 使用MD5加密算法计算加密字符串
  699. paySign = MD5Util.MD5(payDataB).toUpperCase();
  700. // 使用MD5加密算法计算加密字符串
  701. var paySign = MD5Util.MD5(payDataB).toUpperCase();
  702. // 发起微信支付
  703. wx.requestPayment({
  704. 'timeStamp': timestamp,
  705. 'nonceStr': nonceStr,
  706. 'package': 'prepay_id=' + prepayId,
  707. 'signType': 'MD5',
  708. 'paySign': paySign,
  709. 'success': function (res) {
  710. console.log(111);
  711. console.log(res);
  712. wx.request({
  713. url: host + "orderapi/update",
  714. data: {
  715. order_sta: that.data.orderState1,
  716. order_number: timestamp1,
  717. orderState: that.data.orderState1,
  718. order_userid: un_id
  719. },
  720. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  721. header: {
  722. 'Accept': 'application/json'
  723. },
  724. success: function (res) {
  725. console.log("支付");
  726. console.log(res);
  727. that.setData({
  728. maskBg: false,
  729. hiddenmodal: false,
  730. modalCont: '支付成功'
  731. })
  732. setTimeout(function () {
  733. that.setData({
  734. hiddenmodal: true
  735. })
  736. wx.redirectTo({
  737. url: '/pages/orderList/orderList?isSelect=0'
  738. })
  739. }, 2000);
  740. if (res == null || res.data == null) {
  741. //删除当前订单
  742. that.deleteOrder(un_id, timestamp, that.data.coupon_id);
  743. that.setData({
  744. maskBg: false,
  745. allowPay: 0,
  746. firstClick: 0
  747. })
  748. return false;
  749. }
  750. }
  751. });
  752. },
  753. 'fail': function (res) {
  754. that.setData({
  755. maskBg: false,
  756. hiddenmodal: false,
  757. modalCont: '取消支付'
  758. })
  759. setTimeout(function () {
  760. that.setData({
  761. hiddenmodal: true
  762. })
  763. wx.redirectTo({
  764. url: '/pages/orderList/orderList?isSelect=0'
  765. })
  766. }, 2000);
  767. }
  768. })
  769. //如果是从购物车进入支付,则清空对应购物车商品
  770. if (prevPageUrl == 'pages/car/index' || prevPageUrl == 'pages/carInsert/carInsert') {
  771. for (var i = 0; i < that.data.carts.length; i++) {
  772. wx.request({
  773. url: host + "shoppingcartapi/deleteshoppingcart",
  774. data: {
  775. id: that.data.carts[i].id
  776. },
  777. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  778. header: {
  779. 'Accept': 'application/json'
  780. },
  781. success: function (res) {
  782. if (res == null || res.data == null) {
  783. console.error('清空对应购物车商品失败');
  784. return;
  785. }
  786. }
  787. });
  788. }
  789. }
  790. } else {
  791. that.setData({
  792. maskBg: false,
  793. allowPay: 0,
  794. firstClick: 0
  795. })
  796. //删除当前订单
  797. that.deleteOrder(un_id, timestamp, coupon_id);
  798. return false;
  799. }
  800. } else {
  801. that.setData({
  802. maskBg: false,
  803. allowPay: 0,
  804. firstClick: 0,
  805. hiddenmodal: false,
  806. modalCont: '网络错误,请重新操作!'
  807. })
  808. setTimeout(function () {
  809. that.setData({
  810. hiddenmodal: true,
  811. allowPay: 0
  812. })
  813. //删除当前订单
  814. that.deleteOrder(un_id, timestamp, coupon_id);
  815. }, 3000);
  816. return false;
  817. }
  818. }
  819. })
  820. }
  821. })
  822. },
  823. })
  824. var that;
  825. var Util = require('../../utils/util.js');