index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. const app = getApp()
  2. const CONFIG = require('../../config.js')
  3. const WXAPI = require('apifm-wxapi')
  4. const AUTH = require('../../utils/auth')
  5. const wxpay = require('../../utils/pay.js')
  6. Date.prototype.format = function(format) {
  7. var date = {
  8. "M+": this.getMonth() + 1,
  9. "d+": this.getDate(),
  10. "h+": this.getHours(),
  11. "m+": this.getMinutes(),
  12. "s+": this.getSeconds(),
  13. "q+": Math.floor((this.getMonth() + 3) / 3),
  14. "S+": this.getMilliseconds()
  15. };
  16. if (/(y+)/i.test(format)) {
  17. format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  18. }
  19. for (var k in date) {
  20. if (new RegExp("(" + k + ")").test(format)) {
  21. format = format.replace(RegExp.$1, RegExp.$1.length == 1
  22. ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
  23. }
  24. }
  25. return format;
  26. }
  27. Page({
  28. data: {
  29. totalScoreToPay: 0,
  30. goodsList: [],
  31. isNeedLogistics: 0, // 是否需要物流信息
  32. yunPrice: 0,
  33. allGoodsAndYunPrice: 0,
  34. goodsJsonStr: "",
  35. orderType: "", //订单类型,购物车下单或立即支付下单,默认是购物车, buyNow 说明是立即购买
  36. pingtuanOpenId: undefined, //拼团的话记录团号
  37. hasNoCoupons: true,
  38. coupons: [],
  39. couponAmount: 0, //优惠券金额
  40. curCoupon: null, // 当前选择使用的优惠券
  41. curCouponShowText: '请选择使用优惠券', // 当前选择使用的优惠券
  42. peisongType: 'kd', // 配送方式 kd,zq 分别表示快递/到店自取
  43. remark: '',
  44. shopIndex: -1,
  45. pageIsEnd: false,
  46. bindMobileStatus: 0, // 0 未判断 1 已绑定手机号码 2 未绑定手机号码
  47. userScore: 0, // 用户可用积分
  48. deductionScore: '0', // 本次交易抵扣的积分数
  49. shopCarType: 0, //0自营购物车,1云货架购物车
  50. dyopen: 0, // 是否开启订阅
  51. dyunit: 0, // 按天
  52. dyduration: 1, // 订阅间隔
  53. dytimes: 1, // 订阅次数
  54. dateStart: undefined, // 订阅首次扣费时间
  55. minDate: new Date().getTime(),
  56. maxDate: new Date(2030, 10, 1).getTime(),
  57. currentDate: new Date().getTime(),
  58. formatter: (type, value) => {
  59. if (type === 'year') {
  60. return `${value}年`;
  61. }
  62. if (type === 'month') {
  63. return `${value}月`;
  64. }
  65. if (type === 'day') {
  66. return `${value}日`;
  67. }
  68. if (type === 'hour') {
  69. return `${value}点`;
  70. }
  71. if (type === 'minute') {
  72. return `${value}分`;
  73. }
  74. return value;
  75. },
  76. cardId: '0' // 使用的次卡ID
  77. },
  78. onShow() {
  79. if (this.data.pageIsEnd) {
  80. return
  81. }
  82. AUTH.checkHasLogined().then(isLogined => {
  83. if (isLogined) {
  84. this.doneShow()
  85. } else {
  86. AUTH.authorize().then(res => {
  87. AUTH.bindSeller()
  88. this.doneShow()
  89. })
  90. }
  91. })
  92. AUTH.wxaCode().then(code => {
  93. this.data.code = code
  94. })
  95. },
  96. async doneShow() {
  97. let goodsList = []
  98. let shopList = []
  99. const token = wx.getStorageSync('token')
  100. //立即购买下单
  101. if ("buyNow" == this.data.orderType) {
  102. var buyNowInfoMem = wx.getStorageSync('buyNowInfo');
  103. this.data.kjId = buyNowInfoMem.kjId;
  104. if (buyNowInfoMem && buyNowInfoMem.shopList) {
  105. goodsList = buyNowInfoMem.shopList
  106. }
  107. } else {
  108. //购物车下单
  109. if (this.data.shopCarType == 0) {//自营购物车
  110. var res = await WXAPI.shippingCarInfo(token)
  111. shopList = res.data.shopList
  112. } else if (this.data.shopCarType == 1) {//云货架购物车
  113. var res = await WXAPI.jdvopCartInfo(token)
  114. shopList = [{
  115. id: 0,
  116. name: '其他',
  117. hasNoCoupons: true,
  118. serviceDistance: 99999999
  119. }]
  120. }
  121. if (res.code == 0) {
  122. goodsList = res.data.items.filter(ele => {
  123. return ele.selected
  124. })
  125. const shopIds = []
  126. goodsList.forEach(ele => {
  127. if (this.data.shopCarType == 1) {
  128. ele.shopId = 0
  129. }
  130. shopIds.push(ele.shopId)
  131. })
  132. shopList = shopList.filter(ele => {
  133. return shopIds.includes(ele.id)
  134. })
  135. }
  136. }
  137. shopList.forEach(ele => {
  138. ele.hasNoCoupons = true
  139. })
  140. this.setData({
  141. shopList,
  142. goodsList,
  143. peisongType: this.data.peisongType
  144. });
  145. this.initShippingAddress()
  146. this.userAmount()
  147. },
  148. onLoad(e) {
  149. const nowDate = new Date();
  150. let _data = {
  151. isNeedLogistics: 1,
  152. dateStart: nowDate.format('yyyy-MM-dd h:m:s'),
  153. orderPeriod_open: wx.getStorageSync('orderPeriod_open'),
  154. order_pay_user_balance: wx.getStorageSync('order_pay_user_balance')
  155. }
  156. if (e.orderType) {
  157. _data.orderType = e.orderType
  158. }
  159. if (e.pingtuanOpenId) {
  160. _data.pingtuanOpenId = e.pingtuanOpenId
  161. }
  162. if (e.shopCarType) {
  163. _data.shopCarType = e.shopCarType
  164. }
  165. this.setData(_data)
  166. this.getUserApiInfo()
  167. this.cardMyList()
  168. },
  169. async userAmount() {
  170. const res = await WXAPI.userAmount(wx.getStorageSync('token'))
  171. const order_pay_user_balance = wx.getStorageSync('order_pay_user_balance')
  172. if (res.code == 0) {
  173. this.setData({
  174. balance: order_pay_user_balance == '1' ? res.data.balance : 0,
  175. userScore: res.data.score
  176. })
  177. }
  178. },
  179. getDistrictId: function (obj, aaa) {
  180. if (!obj) {
  181. return "";
  182. }
  183. if (!aaa) {
  184. return "";
  185. }
  186. return aaa;
  187. },
  188. remarkChange(e) {
  189. this.data.remark = e.detail.value
  190. },
  191. async goCreateOrder() {
  192. this.setData({
  193. btnLoading: true
  194. })
  195. // 检测实名认证状态
  196. if (wx.getStorageSync('needIdCheck') == 1) {
  197. const res = await WXAPI.userDetail(wx.getStorageSync('token'))
  198. if (res.code == 0 && !res.data.base.isIdcardCheck) {
  199. wx.navigateTo({
  200. url: '/pages/idCheck/index',
  201. })
  202. this.setData({
  203. btnLoading: false
  204. })
  205. return
  206. }
  207. }
  208. const subscribe_ids = wx.getStorageSync('subscribe_ids')
  209. if (subscribe_ids) {
  210. wx.requestSubscribeMessage({
  211. tmplIds: subscribe_ids.split(','),
  212. success(res) {
  213. },
  214. fail(e) {
  215. console.error(e)
  216. },
  217. complete: (e) => {
  218. this.createOrder(true)
  219. },
  220. })
  221. } else {
  222. this.createOrder(true)
  223. }
  224. },
  225. async createOrder(e) {
  226. // shopCarType: 0 //0自营购物车,1云货架购物车
  227. const loginToken = wx.getStorageSync('token') // 用户登录 token
  228. const postData = {
  229. token: loginToken,
  230. goodsJsonStr: this.data.goodsJsonStr,
  231. remark: this.data.remark,
  232. peisongType: this.data.peisongType,
  233. deductionScore: this.data.deductionScore,
  234. goodsType: this.data.shopCarType,
  235. cardId: this.data.cardId
  236. }
  237. if (this.data.cardId == '0') {
  238. postData.cardId = ''
  239. }
  240. if (this.data.dyopen == 1) {
  241. const orderPeriod = {
  242. unit: this.data.dyunit,
  243. duration: this.data.dyduration,
  244. dateStart: this.data.dateStart,
  245. times: this.data.dytimes,
  246. autoPay: true
  247. }
  248. postData.orderPeriod = JSON.stringify(orderPeriod)
  249. }
  250. if (this.data.kjId) {
  251. postData.kjid = this.data.kjId
  252. }
  253. if (this.data.pingtuanOpenId) {
  254. postData.pingtuanOpenId = this.data.pingtuanOpenId
  255. }
  256. if (postData.peisongType == 'kd' && this.data.curAddressData && this.data.curAddressData.provinceId) {
  257. postData.provinceId = this.data.curAddressData.provinceId;
  258. }
  259. if (postData.peisongType == 'kd' && this.data.curAddressData && this.data.curAddressData.cityId) {
  260. postData.cityId = this.data.curAddressData.cityId;
  261. }
  262. if (postData.peisongType == 'kd' && this.data.curAddressData && this.data.curAddressData.districtId) {
  263. postData.districtId = this.data.curAddressData.districtId;
  264. }
  265. if (postData.peisongType == 'kd' && this.data.curAddressData && this.data.curAddressData.streetId) {
  266. postData.streetId = this.data.curAddressData.streetId;
  267. }
  268. if (this.data.shopCarType == 1) {
  269. // vop 需要地址来计算运费
  270. postData.address = this.data.curAddressData.address;
  271. postData.linkMan = this.data.curAddressData.linkMan;
  272. postData.mobile = this.data.curAddressData.mobile;
  273. postData.code = this.data.curAddressData.code;
  274. }
  275. if (e && this.data.isNeedLogistics > 0 && postData.peisongType == 'kd') {
  276. if (!this.data.curAddressData) {
  277. wx.hideLoading();
  278. wx.showToast({
  279. title: '请设置收货地址',
  280. icon: 'none'
  281. })
  282. this.setData({
  283. btnLoading: false
  284. })
  285. return;
  286. }
  287. if (postData.peisongType == 'kd') {
  288. postData.address = this.data.curAddressData.address;
  289. postData.linkMan = this.data.curAddressData.linkMan;
  290. postData.mobile = this.data.curAddressData.mobile;
  291. postData.code = this.data.curAddressData.code;
  292. }
  293. }
  294. if (this.data.curCoupon) {
  295. postData.couponId = this.data.curCoupon.id;
  296. }
  297. if (!e) {
  298. postData.calculate = "true";
  299. } else {
  300. if (postData.peisongType == 'zq' && this.data.shops && this.data.shopIndex == -1) {
  301. wx.showToast({
  302. title: '请选择自提门店',
  303. icon: 'none'
  304. })
  305. this.setData({
  306. btnLoading: false
  307. })
  308. return;
  309. }
  310. const extJsonStr = {}
  311. if (postData.peisongType == 'zq') {
  312. if (!this.data.name) {
  313. wx.showToast({
  314. title: '请填写联系人',
  315. icon: 'none'
  316. })
  317. this.setData({
  318. btnLoading: false
  319. })
  320. return;
  321. }
  322. if (!this.data.mobile) {
  323. wx.showToast({
  324. title: '请填写联系电话',
  325. icon: 'none'
  326. })
  327. this.setData({
  328. btnLoading: false
  329. })
  330. return;
  331. }
  332. extJsonStr['联系人'] = this.data.name
  333. extJsonStr['联系电话'] = this.data.mobile
  334. }
  335. if (postData.peisongType == 'zq' && this.data.shops) {
  336. postData.shopIdZt = this.data.shops[this.data.shopIndex].id
  337. postData.shopNameZt = this.data.shops[this.data.shopIndex].name
  338. }
  339. postData.extJsonStr = JSON.stringify(extJsonStr)
  340. }
  341. const shopList = this.data.shopList
  342. let totalRes = {
  343. code: 0,
  344. msg: 'success',
  345. data: {
  346. score: 0,
  347. amountReal: 0,
  348. orderIds: []
  349. }
  350. }
  351. if (shopList && shopList.length > 1) {
  352. // 多门店的商品下单
  353. let totalScoreToPay = 0
  354. let isNeedLogistics = false
  355. let allGoodsAndYunPrice = 0
  356. let yunPrice = 0
  357. let deductionMoney = 0
  358. let couponAmount = 0
  359. for (let index = 0; index < shopList.length; index++) {
  360. const curShop = shopList[index]
  361. console.log(curShop);
  362. postData.filterShopId = curShop.id
  363. if (curShop.curCoupon) {
  364. postData.couponId = curShop.curCoupon.id
  365. } else {
  366. postData.couponId = ''
  367. }
  368. const res = await WXAPI.orderCreate(postData)
  369. this.data.pageIsEnd = true
  370. if (res.code != 0) {
  371. this.data.pageIsEnd = false
  372. wx.showModal({
  373. title: '错误',
  374. content: res.msg,
  375. showCancel: false
  376. })
  377. this.setData({
  378. btnLoading: false
  379. })
  380. return;
  381. }
  382. totalRes.data.score += res.data.score
  383. totalRes.data.amountReal += res.data.amountReal
  384. totalRes.data.orderIds.push(res.data.id)
  385. console.log('e:', e);
  386. if (!e) {
  387. curShop.hasNoCoupons = true
  388. console.log(curShop);
  389. if (res.data.couponUserList) {
  390. curShop.hasNoCoupons = false
  391. res.data.couponUserList.forEach(ele => {
  392. let moneyUnit = '元'
  393. if (ele.moneyType == 1) {
  394. moneyUnit = '%'
  395. }
  396. if (ele.moneyHreshold) {
  397. ele.nameExt = ele.name + ' [面值' + ele.money + moneyUnit + ',满' + ele.moneyHreshold + '元可用]'
  398. } else {
  399. ele.nameExt = ele.name + ' [面值' + ele.money + moneyUnit + ']'
  400. }
  401. })
  402. curShop.curCouponShowText = '请选择使用优惠券'
  403. curShop.coupons = res.data.couponUserList
  404. if (res.data.couponId && res.data.couponId.length > 0) {
  405. curShop.curCoupon = curShop.coupons.find(ele => { return ele.id == res.data.couponId[0] })
  406. curShop.curCouponShowText = curShop.curCoupon.nameExt
  407. }
  408. }
  409. shopList.splice(index, 1, curShop)
  410. // 计算积分抵扣规则 userScore
  411. let scoreDeductionRules = res.data.scoreDeductionRules
  412. if (scoreDeductionRules) {
  413. // 如果可叠加,计算可抵扣的最大积分数
  414. scoreDeductionRules.forEach(ele => {
  415. if (ele.loop) {
  416. let loopTimes = Math.floor(this.data.userScore / ele.score) // 按剩余积分取最大
  417. let loopTimesMax = Math.floor((res.data.amountTotle + res.data.deductionMoney) / ele.money) // 按金额取最大
  418. if (loopTimes > loopTimesMax) {
  419. loopTimes = loopTimesMax
  420. }
  421. ele.score = ele.score * loopTimes
  422. ele.money = ele.money * loopTimes
  423. }
  424. })
  425. // 剔除积分数为0的情况
  426. scoreDeductionRules = scoreDeductionRules.filter(ele => {
  427. return ele.score > 0
  428. })
  429. curShop.scoreDeductionRules = scoreDeductionRules
  430. shopList.splice(index, 1, curShop)
  431. }
  432. totalScoreToPay += res.data.score
  433. if (res.data.isNeedLogistics) {
  434. isNeedLogistics = true
  435. }
  436. allGoodsAndYunPrice += res.data.amountReal
  437. yunPrice += res.data.amountLogistics
  438. deductionMoney += res.data.deductionMoney
  439. couponAmount += res.data.couponAmount
  440. }
  441. }
  442. this.setData({
  443. shopList,
  444. totalScoreToPay,
  445. isNeedLogistics,
  446. allGoodsAndYunPrice,
  447. yunPrice,
  448. hasNoCoupons: true,
  449. deductionMoney,
  450. couponAmount
  451. });
  452. } else {
  453. // 单门店单商品下单
  454. if (shopList && shopList.length == 1) {
  455. if (shopList[0].curCoupon) {
  456. postData.couponId = shopList[0].curCoupon.id
  457. } else {
  458. postData.couponId = ''
  459. }
  460. }
  461. const res = await WXAPI.orderCreate(postData)
  462. this.data.pageIsEnd = true
  463. if (res.code != 0) {
  464. this.data.pageIsEnd = false
  465. wx.showModal({
  466. title: '错误',
  467. content: res.msg,
  468. showCancel: false
  469. })
  470. this.setData({
  471. btnLoading: false
  472. })
  473. return;
  474. }
  475. totalRes = res
  476. if (!e) {
  477. let hasNoCoupons = true
  478. let coupons = null
  479. if (res.data.couponUserList) {
  480. hasNoCoupons = false
  481. res.data.couponUserList.forEach(ele => {
  482. let moneyUnit = '元'
  483. if (ele.moneyType == 1) {
  484. moneyUnit = '%'
  485. }
  486. if (ele.moneyHreshold) {
  487. ele.nameExt = ele.name + ' [面值' + ele.money + moneyUnit + ',满' + ele.moneyHreshold + '元可用]'
  488. } else {
  489. ele.nameExt = ele.name + ' [面值' + ele.money + moneyUnit + ']'
  490. }
  491. })
  492. coupons = res.data.couponUserList
  493. if (shopList && shopList.length == 1 && !hasNoCoupons) {
  494. hasNoCoupons = true
  495. const curShop = shopList[0]
  496. curShop.hasNoCoupons = false
  497. curShop.curCouponShowText = '请选择使用优惠券'
  498. curShop.coupons = res.data.couponUserList
  499. if (res.data.couponId && res.data.couponId.length > 0) {
  500. curShop.curCoupon = curShop.coupons.find(ele => { return ele.id == res.data.couponId[0] })
  501. curShop.curCouponShowText = curShop.curCoupon.nameExt
  502. }
  503. shopList[0] = curShop
  504. }
  505. }
  506. // 计算积分抵扣规则 userScore
  507. let scoreDeductionRules = res.data.scoreDeductionRules
  508. if (scoreDeductionRules) {
  509. // 如果可叠加,计算可抵扣的最大积分数
  510. scoreDeductionRules.forEach(ele => {
  511. if (ele.loop) {
  512. let loopTimes = Math.floor(this.data.userScore / ele.score) // 按剩余积分取最大
  513. let loopTimesMax = Math.floor((res.data.amountTotle + res.data.deductionMoney) / ele.money) // 按金额取最大
  514. if (loopTimes > loopTimesMax) {
  515. loopTimes = loopTimesMax
  516. }
  517. ele.score = ele.score * loopTimes
  518. ele.money = ele.money * loopTimes
  519. }
  520. })
  521. // 剔除积分数为0的情况
  522. scoreDeductionRules = scoreDeductionRules.filter(ele => {
  523. return ele.score > 0
  524. })
  525. }
  526. this.setData({
  527. shopList,
  528. totalScoreToPay: res.data.score,
  529. isNeedLogistics: res.data.isNeedLogistics,
  530. allGoodsAndYunPrice: res.data.amountReal,
  531. yunPrice: res.data.amountLogistics,
  532. hasNoCoupons,
  533. coupons,
  534. deductionMoney: res.data.deductionMoney,
  535. couponAmount: res.data.couponAmount,
  536. scoreDeductionRules
  537. })
  538. }
  539. }
  540. if (!e) {
  541. this.data.pageIsEnd = false
  542. return
  543. }
  544. if (e && "buyNow" != this.data.orderType) {
  545. // 清空购物车数据
  546. const keyArrays = []
  547. this.data.goodsList.forEach(ele => {
  548. keyArrays.push(ele.key)
  549. })
  550. if (this.data.shopCarType == 0) { //自营购物车
  551. WXAPI.shippingCarInfoRemoveItem(loginToken, keyArrays.join())
  552. } else if (this.data.shopCarType == 1) {//云货架购物车
  553. WXAPI.jdvopCartRemove(loginToken, keyArrays.join())
  554. }
  555. }
  556. this.processAfterCreateOrder(totalRes)
  557. },
  558. async processAfterCreateOrder(res) {
  559. this.setData({
  560. btnLoading: false
  561. })
  562. if (res.data.status != 0) {
  563. wx.redirectTo({
  564. url: "/pages/order-list/index"
  565. })
  566. return
  567. }
  568. let orderId = ''
  569. if (res.data.orderIds && res.data.orderIds.length > 0) {
  570. orderId = res.data.orderIds.join()
  571. } else {
  572. orderId = res.data.id
  573. }
  574. // 直接弹出支付,取消支付的话,去订单列表
  575. const balance = this.data.balance
  576. const userScore = this.data.userScore
  577. if (userScore < res.data.score) {
  578. wx.showModal({
  579. title: '提示',
  580. content: '您当前可用积分不足,请稍后前往订单管理进行支付',
  581. showCancel: false,
  582. success: res2 => {
  583. wx.redirectTo({
  584. url: "/pages/order-list/index"
  585. })
  586. }
  587. })
  588. return
  589. }
  590. if (balance || res.data.amountReal * 1 == 0) {
  591. // 有余额
  592. const money = (res.data.amountReal * 1 - balance * 1).toFixed(2)
  593. if (money <= 0) {
  594. // 余额足够
  595. wx.showModal({
  596. title: '请确认支付',
  597. content: `您当前可用余额¥${balance},使用余额支付¥${res.data.amountReal}?`,
  598. confirmText: "确认支付",
  599. cancelText: "暂不付款",
  600. success: res2 => {
  601. if (res2.confirm) {
  602. // 使用余额支付
  603. WXAPI.orderPay(wx.getStorageSync('token'), orderId).then(res3 => {
  604. if (res3.code != 0) {
  605. wx.showToast({
  606. title: res3.msg,
  607. icon: 'none'
  608. })
  609. return
  610. }
  611. wx.redirectTo({
  612. url: "/pages/order-list/index"
  613. })
  614. })
  615. } else {
  616. wx.redirectTo({
  617. url: "/pages/order-list/index"
  618. })
  619. }
  620. }
  621. })
  622. } else {
  623. // 余额不够
  624. wx.showModal({
  625. title: '请确认支付',
  626. content: `您当前可用余额¥${balance},仍需支付¥${money}`,
  627. confirmText: "确认支付",
  628. cancelText: "暂不付款",
  629. success: res2 => {
  630. if (res2.confirm) {
  631. // 使用余额支付
  632. wxpay.wxpay('order', money, orderId, "/pages/order-list/index");
  633. } else {
  634. wx.redirectTo({
  635. url: "/pages/order-list/index"
  636. })
  637. }
  638. }
  639. })
  640. }
  641. } else {
  642. // 没余额
  643. wxpay.wxpay('order', res.data.amountReal, orderId, "/pages/order-list/index");
  644. }
  645. },
  646. async initShippingAddress() {
  647. const res = await WXAPI.defaultAddress(wx.getStorageSync('token'))
  648. if (res.code == 0) {
  649. this.setData({
  650. curAddressData: res.data.info
  651. });
  652. } else {
  653. this.setData({
  654. curAddressData: null
  655. });
  656. }
  657. this.processYunfei();
  658. },
  659. processYunfei() {
  660. var goodsList = this.data.goodsList
  661. if (goodsList.length == 0) {
  662. return
  663. }
  664. const goodsJsonStr = []
  665. var isNeedLogistics = 0;
  666. let inviter_id = 0;
  667. let inviter_id_storge = wx.getStorageSync('referrer');
  668. if (inviter_id_storge) {
  669. inviter_id = inviter_id_storge;
  670. }
  671. for (let i = 0; i < goodsList.length; i++) {
  672. let carShopBean = goodsList[i];
  673. if (carShopBean.logistics || carShopBean.logisticsId) {
  674. isNeedLogistics = 1;
  675. }
  676. const _goodsJsonStr = {
  677. propertyChildIds: carShopBean.propertyChildIds
  678. }
  679. if (carShopBean.sku && carShopBean.sku.length > 0) {
  680. let propertyChildIds = ''
  681. carShopBean.sku.forEach(option => {
  682. propertyChildIds = propertyChildIds + ',' + option.optionId + ':' + option.optionValueId
  683. })
  684. _goodsJsonStr.propertyChildIds = propertyChildIds
  685. }
  686. if (carShopBean.additions && carShopBean.additions.length > 0) {
  687. let goodsAdditionList = []
  688. carShopBean.additions.forEach(option => {
  689. goodsAdditionList.push({
  690. pid: option.pid,
  691. id: option.id
  692. })
  693. })
  694. _goodsJsonStr.goodsAdditionList = goodsAdditionList
  695. }
  696. _goodsJsonStr.goodsId = carShopBean.goodsId
  697. _goodsJsonStr.number = carShopBean.number
  698. _goodsJsonStr.logisticsType = 0
  699. _goodsJsonStr.inviter_id = inviter_id
  700. goodsJsonStr.push(_goodsJsonStr)
  701. }
  702. if (this.data.shopCarType == 1) {
  703. // vop 商品必须快递
  704. isNeedLogistics = 1
  705. }
  706. this.setData({
  707. isNeedLogistics: isNeedLogistics,
  708. goodsJsonStr: JSON.stringify(goodsJsonStr)
  709. });
  710. this.createOrder();
  711. },
  712. addAddress: function () {
  713. wx.navigateTo({
  714. url: "/pages/address-add/index"
  715. })
  716. },
  717. selectAddress: function () {
  718. wx.navigateTo({
  719. url: "/pages/select-address/index"
  720. })
  721. },
  722. bindChangeCoupon: function (e) {
  723. const selIndex = e.detail.value;
  724. this.setData({
  725. curCoupon: this.data.coupons[selIndex],
  726. curCouponShowText: this.data.coupons[selIndex].nameExt
  727. });
  728. this.processYunfei()
  729. },
  730. bindChangeCouponShop: function (e) {
  731. const selIndex = e.detail.value;
  732. const shopIndex = e.currentTarget.dataset.sidx
  733. const shopList = this.data.shopList
  734. const curshop = shopList[shopIndex]
  735. curshop.curCoupon = curshop.coupons[selIndex]
  736. curshop.curCouponShowText = curshop.coupons[selIndex].nameExt
  737. shopList.splice(shopIndex, 1, curshop)
  738. this.setData({
  739. shopList
  740. });
  741. this.processYunfei()
  742. },
  743. radioChange(e) {
  744. this.setData({
  745. peisongType: e.detail.value
  746. })
  747. this.processYunfei()
  748. if (e.detail.value == 'zq') {
  749. this.fetchShops()
  750. }
  751. },
  752. dyChange(e) {
  753. this.setData({
  754. dyopen: e.detail.value
  755. })
  756. },
  757. dyunitChange(e) {
  758. this.setData({
  759. dyunit: e.detail.value
  760. })
  761. },
  762. cancelLogin() {
  763. wx.navigateBack()
  764. },
  765. async fetchShops() {
  766. const res = await WXAPI.fetchShops()
  767. if (res.code == 0) {
  768. let shopIndex = this.data.shopIndex
  769. const shopInfo = wx.getStorageSync('shopInfo')
  770. if (shopInfo) {
  771. shopIndex = res.data.findIndex(ele => {
  772. return ele.id == shopInfo.id
  773. })
  774. }
  775. this.setData({
  776. shops: res.data,
  777. shopIndex
  778. })
  779. }
  780. },
  781. shopSelect(e) {
  782. this.setData({
  783. shopIndex: e.detail.value
  784. })
  785. },
  786. goMap() {
  787. const _this = this
  788. const shop = this.data.shops[this.data.shopIndex]
  789. const latitude = shop.latitude
  790. const longitude = shop.longitude
  791. wx.openLocation({
  792. latitude,
  793. longitude,
  794. scale: 18
  795. })
  796. },
  797. callMobile() {
  798. const shop = this.data.shops[this.data.shopIndex]
  799. wx.makePhoneCall({
  800. phoneNumber: shop.linkPhone,
  801. })
  802. },
  803. async getUserApiInfo() {
  804. const res = await WXAPI.userDetail(wx.getStorageSync('token'))
  805. if (res.code == 0) {
  806. this.setData({
  807. bindMobileStatus: res.data.base.mobile ? 1 : 2, // 账户绑定的手机号码状态
  808. mobile: res.data.base.mobile,
  809. })
  810. }
  811. },
  812. async getPhoneNumber(e) {
  813. if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
  814. wx.showToast({
  815. title: e.detail.errMsg,
  816. icon: 'none'
  817. })
  818. return;
  819. }
  820. let res
  821. const extConfigSync = wx.getExtConfigSync()
  822. if (extConfigSync.subDomain) {
  823. // 服务商模式
  824. res = await WXAPI.wxappServiceBindMobile({
  825. token: wx.getStorageSync('token'),
  826. code: this.data.code,
  827. encryptedData: e.detail.encryptedData,
  828. iv: e.detail.iv,
  829. })
  830. } else {
  831. res = await WXAPI.bindMobileWxapp(wx.getStorageSync('token'), this.data.code, e.detail.encryptedData, e.detail.iv)
  832. }
  833. AUTH.wxaCode().then(code => {
  834. this.data.code = code
  835. })
  836. if (res.code == 0) {
  837. wx.showToast({
  838. title: '读取成功',
  839. icon: 'success'
  840. })
  841. this.setData({
  842. mobile: res.data,
  843. bindMobileStatus: 1
  844. })
  845. } else {
  846. wx.showToast({
  847. title: res.msg,
  848. icon: 'none'
  849. })
  850. }
  851. },
  852. deductionScoreChange(event) {
  853. this.setData({
  854. deductionScore: event.detail,
  855. })
  856. this.processYunfei()
  857. },
  858. deductionScoreClick(event) {
  859. const {
  860. name
  861. } = event.currentTarget.dataset;
  862. this.setData({
  863. deductionScore: name,
  864. })
  865. this.processYunfei()
  866. },
  867. cardChange(event) {
  868. this.setData({
  869. cardId: event.detail,
  870. })
  871. this.processYunfei()
  872. },
  873. cardClick(event) {
  874. const {
  875. name
  876. } = event.currentTarget.dataset;
  877. this.setData({
  878. cardId: name,
  879. })
  880. this.processYunfei()
  881. },
  882. dateStartclick(e) {
  883. this.setData({
  884. dateStartpop: true
  885. })
  886. },
  887. dateStartconfirm(e) {
  888. const d = new Date(e.detail)
  889. this.setData({
  890. dateStart: d.format('yyyy-MM-dd h:m:s'),
  891. dateStartpop: false
  892. })
  893. console.log(e);
  894. },
  895. dateStartcancel(e) {
  896. this.setData({
  897. dateStartpop: false
  898. })
  899. },
  900. async cardMyList() {
  901. const res = await WXAPI.cardMyList(wx.getStorageSync('token'))
  902. if (res.code == 0) {
  903. const myCards = res.data.filter(ele => { return ele.status == 0 && ele.amount > 0 && ele.cardInfo.refs })
  904. if (myCards.length > 0) {
  905. this.setData({
  906. myCards: res.data
  907. })
  908. }
  909. }
  910. },
  911. })