index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import { orderConfirm, getCouponsOrderPrice, orderCreate, postOrderComputed} from '../../api/order.js';
  2. import { getAddressDefault, getAddressDetail } from '../../api/user.js';
  3. import { openPaySubscribe } from '../../utils/SubscribeMessage.js';
  4. import { storeListApi } from '../../api/store.js';
  5. import { CACHE_LONGITUDE, CACHE_LATITUDE } from '../../config.js';
  6. const app = getApp();
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. textareaStatus:true,
  13. parameter: {
  14. 'navbar': '1',
  15. 'return': '1',
  16. 'title': '提交订单',
  17. 'color': true,
  18. 'class': '0'
  19. },
  20. //支付方式
  21. cartArr: [
  22. { "name": "微信支付", "icon": "icon-weixin2", value: 'weixin', title: '微信快捷支付' },
  23. { "name": "余额支付", "icon": "icon-icon-test", value: 'yue',title:'可用余额:'},
  24. { "name": "线下支付", "icon": "icon-yinhangqia", value: 'offline', title: '线下支付'},
  25. ],
  26. payType:'weixin',//支付方式
  27. openType:1,//优惠券打开方式 1=使用
  28. active:0,//支付方式切换
  29. coupon: { coupon: false, list: [], statusTile:'立即使用'},//优惠券组件
  30. address: {address: false},//地址组件
  31. addressInfo:{},//地址信息
  32. pinkId:0,//拼团id
  33. addressId:0,//地址id
  34. couponId:0,//优惠券id
  35. cartId:'',//购物车id
  36. userInfo:{},//用户信息
  37. mark:'',//备注信息
  38. couponTitle:'请选择',//优惠券
  39. coupon_price:0,//优惠券抵扣金额
  40. useIntegral:false,//是否使用积分
  41. integral_price:0,//积分抵扣金额
  42. ChangePrice:0,//使用积分抵扣变动后的金额
  43. formIds:[],//收集formid
  44. status:0,
  45. is_address:false,
  46. isClose:false,
  47. toPay:false,//修复进入支付时页面隐藏从新刷新页面
  48. shippingType:0,
  49. system_store:{},
  50. storePostage:0,
  51. contacts:'',
  52. contactsTel:'',
  53. mydata: {},
  54. storeList: []
  55. },
  56. /**
  57. * 授权回调事件
  58. *
  59. */
  60. onLoadFun:function(){
  61. this.getaddressInfo();
  62. this.getConfirm();
  63. //调用子页面方法授权后执行获取地址列表
  64. this.selectComponent('#address-window').getAddressList();
  65. },
  66. /**
  67. * 生命周期函数--监听页面显示
  68. */
  69. onShow: function () {
  70. this.setData({ textareaStatus: true });
  71. if (app.globalData.isLog && this.data.isClose && this.data.toPay==false) {
  72. this.getaddressInfo();
  73. this.selectComponent('#address-window').getAddressList();
  74. }
  75. let pages = getCurrentPages();
  76. let currPage = pages[pages.length - 1]; //当前页面
  77. if (currPage.data.storeItem){
  78. let json = currPage.data.storeItem;
  79. this.setData({
  80. system_store: json,
  81. });
  82. }
  83. },
  84. /**
  85. * 获取门店列表数据
  86. */
  87. getList: function () {
  88. let longitude = wx.getStorageSync(CACHE_LONGITUDE); //经度
  89. let latitude = wx.getStorageSync(CACHE_LATITUDE); //纬度
  90. let data = {
  91. latitude: latitude, //纬度
  92. longitude: longitude, //经度
  93. page: 1,
  94. limit: 10
  95. }
  96. storeListApi(data).then(res => {
  97. let list = res.data.list || [];
  98. this.setData({
  99. storeList: list,
  100. system_store: list[0],
  101. });
  102. }).catch(err => {
  103. })
  104. },
  105. /*
  106. * 跳转门店列表
  107. */
  108. showStoreList: function () {
  109. wx.navigateTo({
  110. url: '/pages/goods_details_store/index?go=order'
  111. })
  112. },
  113. computedPrice:function(){
  114. let shippingType = this.data.shippingType;
  115. postOrderComputed(this.data.orderKey,{
  116. addressId: this.data.addressId,
  117. useIntegral: this.data.useIntegral ? 1 : 0,
  118. couponId: this.data.couponId,
  119. shipping_type: parseInt(shippingType) + 1,
  120. payType: this.data.payType
  121. }).then(res=>{
  122. let result = res.data.result;
  123. if (result){
  124. this.setData({
  125. totalPrice: result.pay_price,
  126. integral_price: result.deduction_price,
  127. coupon_price: result.coupon_price,
  128. integral: this.data.useIntegral ? result.SurplusIntegral : this.data.userInfo.integral ,
  129. 'priceGroup.storePostage': shippingType == 1 ? 0 : result.pay_postage,
  130. });
  131. }
  132. })
  133. },
  134. addressType:function(e){
  135. let index = e.currentTarget.dataset.index;
  136. if (this.data.storeList.length>0){
  137. this.setData({ shippingType: parseInt(index) });
  138. }else{
  139. if(index==1){
  140. return app.Tips({ title: '暂无门店信息,你无法选择到店自提' });
  141. }
  142. }
  143. this.computedPrice();
  144. },
  145. bindPickerChange: function (e) {
  146. let value = e.detail.value;
  147. this.setData({shippingType: value})
  148. this.computedPrice();
  149. },
  150. /**
  151. * 生命周期函数--监听页面隐藏
  152. */
  153. onHide: function () {
  154. this.setData({ isClose: true });
  155. },
  156. ChangCouponsClone:function(){
  157. this.setData({'coupon.coupon':false});
  158. },
  159. changeTextareaStatus:function(){
  160. for (var i = 0, len = this.data.coupon.list.length; i < len;i++){
  161. this.data.coupon.list[i].use_title='';
  162. this.data.coupon.list[i].is_use = 0;
  163. }
  164. this.setData({ textareaStatus: true, status: 0, "coupon.list": this.data.coupon.list});
  165. },
  166. /**
  167. * 处理点击优惠券后的事件
  168. *
  169. */
  170. ChangCoupons:function(e){
  171. var index = e.detail, list = this.data.coupon.list, couponTitle = '请选择', couponId = 0;
  172. for (var i = 0, len = list.length; i < len; i++) {
  173. if(i != index){
  174. list[i].use_title = '';
  175. list[i].is_use = 0;
  176. }
  177. }
  178. if (list[index].is_use) {
  179. //不使用优惠券
  180. list[index].use_title = '';
  181. list[index].is_use = 0;
  182. } else {
  183. //使用优惠券
  184. list[index].use_title = '不使用';
  185. list[index].is_use = 1;
  186. couponTitle = list[index].coupon_title;
  187. couponId = list[index].id;
  188. }
  189. this.setData({
  190. couponTitle: couponTitle,
  191. couponId: couponId,
  192. 'coupon.coupon': false,
  193. "coupon.list":list,
  194. });
  195. this.computedPrice();
  196. },
  197. /**
  198. * 使用积分抵扣
  199. */
  200. ChangeIntegral:function(){
  201. this.setData({useIntegral:!this.data.useIntegral});
  202. this.computedPrice();
  203. },
  204. /**
  205. * 选择地址后改变事件
  206. * @param object e
  207. */
  208. OnChangeAddress:function(e){
  209. this.setData({ textareaStatus:true,addressId: e.detail,'address.address':false});
  210. this.getaddressInfo();
  211. this.computedPrice();
  212. },
  213. /**
  214. * 生命周期函数--监听页面加载
  215. */
  216. onLoad: function (options) {
  217. if (!options.cartId) return app.Tips({ title:'请选择要购买的商品'},{tab:3,url:1});
  218. // if (options.shippingType) this.setData({ shippingType: options.shippingType, storeIndex: options.storeIndex });
  219. this.setData({
  220. couponId: options.couponId || 0,
  221. pinkId: options.pinkId ? parseInt(options.pinkId) : 0,
  222. addressId: options.addressId || 0,
  223. cartId: options.cartId,
  224. is_address: options.is_address ? true : false
  225. });
  226. },
  227. bindHideKeyboard: function (e) {
  228. this.setData({mark: e.detail.value});
  229. },
  230. /**
  231. * 获取当前订单详细信息
  232. *
  233. */
  234. getConfirm:function(){
  235. var that=this;
  236. orderConfirm(this.data.cartId).then(res=>{
  237. that.setData({
  238. userInfo: res.data.userInfo,
  239. integral: res.data.userInfo.integral,
  240. cartInfo: res.data.cartInfo,
  241. integralRatio: res.data.integralRatio,
  242. offlinePostage: res.data.offlinePostage,
  243. orderKey: res.data.orderKey,
  244. priceGroup: res.data.priceGroup,
  245. totalPrice: app.help().Add(parseFloat(res.data.priceGroup.totalPrice), parseFloat(res.data.priceGroup.storePostage)),
  246. seckillId: parseInt(res.data.seckill_id),
  247. usableCoupon: res.data.usableCoupon,
  248. // system_store: res.data.system_store,
  249. store_self_mention: res.data.store_self_mention
  250. });
  251. that.data.cartArr[1].title = '可用余额:' + res.data.userInfo.now_money;
  252. if (res.data.offline_pay_status == 2) that.data.cartArr.pop();
  253. that.setData({ cartArr: that.data.cartArr, ChangePrice: that.data.totalPrice });
  254. that.getBargainId();
  255. that.getCouponList();
  256. that.getList();
  257. }).catch(err=>{
  258. return app.Tips({ title: err }, { tab: 3, url: 1 });
  259. });
  260. },
  261. /*
  262. * 提取砍价和拼团id
  263. */
  264. getBargainId: function () {
  265. var that = this;
  266. var cartINfo = that.data.cartInfo;
  267. var BargainId = 0;
  268. var combinationId = 0;
  269. cartINfo.forEach(function (value, index, cartINfo) {
  270. BargainId = cartINfo[index].bargain_id,
  271. combinationId = cartINfo[index].combination_id
  272. })
  273. that.setData({ BargainId: parseInt(BargainId), combinationId: parseInt(combinationId)});
  274. if (that.data.cartArr.length == 3 && (BargainId || combinationId || that.data.seckillId)){
  275. that.data.cartArr.pop();
  276. that.setData({ cartArr: that.data.cartArr});
  277. }
  278. },
  279. /**
  280. * 获取当前金额可用优惠券
  281. *
  282. */
  283. getCouponList:function(){
  284. var that=this;
  285. let data = { cartId: this.data.cartId}
  286. getCouponsOrderPrice(this.data.totalPrice, data).then(res=>{
  287. that.setData({ 'coupon.list': res.data, openType: 1 });
  288. });
  289. },
  290. /*
  291. * 获取默认收货地址或者获取某条地址信息
  292. */
  293. getaddressInfo:function(){
  294. var that=this;
  295. if(that.data.addressId){
  296. getAddressDetail(that.data.addressId).then(res=>{
  297. res.data.is_default = parseInt(res.data.is_default);
  298. that.setData({ addressInfo: res.data || {}, addressId: res.data.id || 0, 'address.addressId': res.data.id || 0 });
  299. })
  300. }else{
  301. getAddressDefault().then(res=>{
  302. res.data.is_default = parseInt(res.data.is_default);
  303. that.setData({ addressInfo: res.data || {}, addressId: res.data.id || 0, 'address.addressId': res.data.id || 0 });
  304. })
  305. }
  306. },
  307. payItem:function(e){
  308. var that = this;
  309. var active = e.currentTarget.dataset.index;
  310. that.setData({
  311. active: active,
  312. animated: true,
  313. payType: that.data.cartArr[active].value,
  314. });
  315. that.computedPrice();
  316. setTimeout(function () {
  317. that.car();
  318. }, 500);
  319. },
  320. coupon: function () {
  321. this.setData({
  322. 'coupon.coupon': true
  323. })
  324. },
  325. car: function () {
  326. var that = this;
  327. that.setData({
  328. animated: false
  329. });
  330. },
  331. onAddress:function(){
  332. this.setData({
  333. textareaStatus:false,
  334. 'address.address': true,
  335. pagesUrl: '/pages/user_address_list/index?cartId=' + this.data.cartId + '&pinkId=' + this.data.pinkId + '&couponId=' + this.data.couponId
  336. });
  337. },
  338. realName:function(e){
  339. this.setData({
  340. contacts: e.detail.value
  341. })
  342. },
  343. phone: function (e) {
  344. this.setData({
  345. contactsTel: e.detail.value
  346. })
  347. },
  348. SubOrder:function(e){
  349. var that = this, data={};
  350. if (!this.data.payType) return app.Tips({title:'请选择支付方式'});
  351. if (!this.data.addressId && !this.data.shippingType) return app.Tips({ title:'请选择收货地址'});
  352. if (this.data.shippingType == 1){
  353. if (this.data.contacts == "" || this.data.contactsTel == "")
  354. return app.Tips({ title: '请填写联系人或联系人电话' });
  355. if (!/^1(3|4|5|7|8|9|6)\d{9}$/.test(this.data.contactsTel)) {
  356. return app.Tips({ title: '请填写正确的手机号' });
  357. }
  358. if (!/^[\u4e00-\u9fa5\w]{2,16}$/.test(this.data.contacts)) {
  359. return app.Tips({ title: '请填写您的真实姓名' });
  360. }
  361. }
  362. data={
  363. real_name: that.data.contacts,
  364. phone: that.data.contactsTel,
  365. addressId: that.data.addressId,
  366. formId: '',
  367. couponId: that.data.couponId,
  368. payType: that.data.payType,
  369. useIntegral: that.data.useIntegral,
  370. bargainId: that.data.BargainId,
  371. combinationId: that.data.combinationId,
  372. pinkId: that.data.pinkId,
  373. seckill_id: that.data.seckillId,
  374. mark: that.data.mark,
  375. store_id: that.data.system_store ? that.data.system_store.id : 0,
  376. 'from':'routine',
  377. shipping_type: app.help().Add(that.data.shippingType,1)
  378. };
  379. if (data.payType == 'yue' && parseFloat(that.data.userInfo.now_money) < parseFloat(that.data.totalPrice)) return app.Tips({title:'余额不足!'});
  380. wx.showLoading({ title: '订单支付中'});
  381. openPaySubscribe().then(()=>{
  382. orderCreate(this.data.orderKey ,data).then(res=>{
  383. var status = res.data.status, orderId = res.data.result.orderId, jsConfig = res.data.result.jsConfig,
  384. goPages = '/pages/order_pay_status/index?order_id=' + orderId + '&msg=' + res.msg;
  385. switch (status) {
  386. case 'ORDER_EXIST': case 'EXTEND_ORDER': case 'PAY_ERROR':
  387. wx.hideLoading();
  388. return app.Tips({ title: res.msg }, { tab: 5, url: goPages });
  389. break;
  390. case 'SUCCESS':
  391. wx.hideLoading();
  392. if (that.data.BargainId || that.data.combinationId || that.data.pinkId || that.data.seckillId)
  393. return app.Tips({ title: res.msg, icon: 'success' }, { tab: 4, url: goPages });
  394. return app.Tips({ title: res.msg, icon: 'success' }, { tab: 5, url: goPages });
  395. break;
  396. case 'WECHAT_PAY':
  397. that.setData({ toPay: true });
  398. wx.requestPayment({
  399. timeStamp: jsConfig.timestamp,
  400. nonceStr: jsConfig.nonceStr,
  401. package: jsConfig.package,
  402. signType: jsConfig.signType,
  403. paySign: jsConfig.paySign,
  404. success: function (res) {
  405. wx.hideLoading();
  406. if (that.data.BargainId || that.data.combinationId || that.data.pinkId || that.data.seckillId)
  407. return app.Tips({ title: '支付成功', icon: 'success' }, { tab: 4, url: goPages });
  408. return app.Tips({ title: '支付成功', icon: 'success' }, { tab: 5, url: goPages });
  409. },
  410. fail: function (e) {
  411. wx.hideLoading();
  412. return app.Tips({ title: '取消支付' }, { tab: 5, url: goPages + '&status=2' });
  413. },
  414. complete: function (e) {
  415. wx.hideLoading();
  416. //关闭当前页面跳转至订单状态
  417. if (res.errMsg == 'requestPayment:cancel') return app.Tips({ title: '取消支付' }, { tab: 5, url: goPages + '&status=2' });
  418. },
  419. })
  420. break;
  421. case 'PAY_DEFICIENCY':
  422. wx.hideLoading();
  423. //余额不足
  424. return app.Tips({ title: res.msg }, { tab: 5, url: goPages + '&status=1' });
  425. break;
  426. }
  427. }).catch(err=>{
  428. wx.hideLoading();
  429. return app.Tips({title:err});
  430. });
  431. });
  432. }
  433. })