confirmOrder.js 32 KB

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