confirmOrder.js 33 KB

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