index.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // index.ts
  2. import { api_executeOrder, api_queryByParamName, api_shopProduct } from "../../api/login";
  3. import { debounce } from '../../utils/util'
  4. // 获取应用实例
  5. const app = getApp<IAppOption>()
  6. // pages/orders/orders.ts
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. current: 0,
  13. autoplay: false,
  14. interval: 5000,
  15. duration: 500,
  16. popupShow: false,
  17. list: [] as any,
  18. isOverSaled: false, // 是否所有商品都没有库存
  19. selected: {} as any,
  20. isH5Pay: true, // 是否公众号支付
  21. paymentType: null as any, // 支付类型
  22. paymentChannel: null as any,
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad() {
  28. this.queryPayType()
  29. },
  30. /**
  31. * 获取基础信息
  32. */
  33. async onInit() {
  34. try {
  35. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  36. const list = data.data || []
  37. let selected: any = {}
  38. let isOverSaled = true // 是否销售完
  39. list.forEach((item: any) => {
  40. item.originalPrice = this.formatPrice(item.originalPrice, 'ALL');
  41. item.typeName = this.formatPeriod(item.num, item.period);
  42. const prices: any = this.formatPrice(item.salePrice)
  43. item.integerPart = prices.integerPart
  44. item.decimalPart = prices.decimalPart
  45. if(item.stockNum > 0) {
  46. isOverSaled = false
  47. if( !selected.id) {
  48. selected = item
  49. }
  50. }
  51. });
  52. if(isOverSaled) {
  53. // 没有可购买商品则默认选中第一个商品
  54. selected = list[0]
  55. }
  56. this.setData({
  57. list,
  58. isOverSaled,
  59. selected
  60. })
  61. } catch(e) {
  62. console.log(e, 'e')
  63. }
  64. },
  65. // 格式化价格
  66. formatPrice(price: number, type?: string) {
  67. const amountStr = price.toFixed(2)
  68. const [integerPart, decimalPart] = amountStr.split('.');
  69. if(type === 'ALL') {
  70. return amountStr
  71. }
  72. return {
  73. integerPart,
  74. decimalPart
  75. }
  76. },
  77. // 格式化类型
  78. formatPeriod(num: number, type: string) {
  79. const template: any = {
  80. DAY: "天卡",
  81. MONTH: "月卡",
  82. YEAR: "年卡"
  83. }
  84. if(type === "YEAR" && num >= 99) {
  85. return '终生卡'
  86. }
  87. return num + template[type]
  88. },
  89. // 选择
  90. onSelectGoods(e: any) {
  91. const { dataset } = e.currentTarget
  92. const item = this.data.list.find((item: any) => item.id === dataset.id)
  93. // 判断是否有库存
  94. if(item.stockNum <= 0) {
  95. return
  96. }
  97. this.setData({
  98. selected: item || {}
  99. })
  100. },
  101. // 事件处理函数
  102. changeSwiper(e: any) {
  103. const detail = e.detail;
  104. if(detail.source === 'touch' || detail.source == 'autoplay') {
  105. this.setData({
  106. current: detail.current
  107. })
  108. }
  109. },
  110. isLogin() {
  111. // 判断是否登录
  112. if(!app.globalData.isLogin) {
  113. wx.navigateTo({
  114. url: '../login/login',
  115. })
  116. return false
  117. }
  118. return true
  119. },
  120. /** 我的订单 */
  121. onOrder() {
  122. // 判断是否登录
  123. if(!this.isLogin()) {
  124. return
  125. }
  126. wx.navigateTo({
  127. url: '../orders/orders',
  128. })
  129. },
  130. onBuyShop() {
  131. // 判断是否登录
  132. if(!this.isLogin()) {
  133. return
  134. }
  135. this.setData({
  136. popupShow: true
  137. })
  138. },
  139. onClose() {
  140. this.setData({
  141. popupShow: false
  142. })
  143. },
  144. onSubmit() {
  145. // 判断是否登录
  146. const that = this
  147. debounce(function () {
  148. if(!that.isLogin()) {
  149. return
  150. }
  151. if(that.data.isH5Pay) {
  152. that.onH5Play(that.data.selected)
  153. } else {
  154. let info = JSON.stringify({
  155. ...that.data.selected
  156. });
  157. info = encodeURIComponent(info);
  158. wx.navigateTo({
  159. url: `../orders/order-detail?orderInfo=${info}`,
  160. })
  161. that.setData({
  162. popupShow: false
  163. })
  164. }
  165. }, 500)()
  166. },
  167. async onH5Play(goodsInfo: any) {
  168. wx.showLoading({
  169. mask: true,
  170. title: "加载中...",
  171. });
  172. try {
  173. const { salePrice, shopId, name, id, orderNo } = goodsInfo
  174. const { data } = await api_executeOrder({
  175. "orderType": "WECHAT_MINI",
  176. "paymentType": this.data.paymentType,
  177. "paymentCashAmount": salePrice,
  178. "paymentCouponAmount": 0,
  179. "shopId": shopId,
  180. "openId": app.globalData.userInfo?.liteOpenid,
  181. "goodsInfos": [{
  182. "goodsId": id,
  183. "goodsNum": 1,
  184. "goodsType": "ACTIVATION_CODE",
  185. "paymentCashAmount": salePrice,
  186. "paymentCouponAmount": 0
  187. }],
  188. "orderName": name,
  189. "orderDesc": name
  190. })
  191. if (data.code === 200) {
  192. const { paymentConfig, paymentType, orderNo } = data.data
  193. // 测试h5页面支付流程
  194. const params = {
  195. paymentType: paymentType, //'adapay-cooleshow-6560',
  196. pay_channel: 'wx_pub',
  197. wxAppId: paymentConfig.wxAppId, //'wxbde13f59d40cb4f2',
  198. body: paymentConfig.body, //'aaa',
  199. price: paymentConfig.price, // '0.01',
  200. orderNo: paymentConfig.merOrderNo,
  201. userId: paymentConfig.userId
  202. }
  203. wx.navigateTo({
  204. url: '../order-webview/order-webview?orderInfo=' + encodeURIComponent(JSON.stringify(params))
  205. })
  206. wx.hideLoading()
  207. } else {
  208. wx.hideLoading()
  209. wx.showToast({
  210. title: '购买失败',
  211. icon: 'none'
  212. })
  213. }
  214. } catch {
  215. wx.hideLoading()
  216. }
  217. },
  218. // 获取后台配置的支付方式
  219. async queryPayType() {
  220. try {
  221. // wxlite_payment_service_provider
  222. const { data } = await api_queryByParamName({
  223. paramName: 'wxlite_payment_service_provider'
  224. });
  225. if (data.code == 200) {
  226. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  227. this.setData({
  228. paymentType: paramValue.vendor,
  229. paymentChannel: paramValue.channel
  230. });
  231. }
  232. } catch (error) {
  233. console.log(error, "error");
  234. }
  235. },
  236. /**
  237. * 生命周期函数--监听页面显示
  238. */
  239. onShow() {
  240. this.onInit()
  241. },
  242. })