dopay.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. const app = getApp();
  2. Page({
  3. data: {
  4. loading: false,
  5. item: [],
  6. amount: [],
  7. address: [],
  8. address_isnull: 0,
  9. actions: [],
  10. orderParams: {}, // 支付参数
  11. chickPayBtn: false, //点击了支付按钮(订单信息交由古德云组件)
  12. chickOnPay: false, // 用户是否已经点击了「支付」并成功跳转到 古德云收银台 小程序
  13. },
  14. /*
  15. 生命周期函数--监听页面初次载入
  16. */
  17. onLoad: function () {
  18. this.getAddress();
  19. this.getCart();
  20. },
  21. /**
  22. * 获取购物车中的订单
  23. */
  24. getCart: function () {
  25. let that = this;
  26. var cart = wx.getStorageSync('group');
  27. app.api().Post('api/v3/fastshop/group/cartItem', {
  28. cart: cart
  29. }, function (result) {
  30. if (result.code == 200) {
  31. that.setData({
  32. item: result.data.item,
  33. amount: result.data.amount
  34. })
  35. } else {
  36. wx.removeStorageSync('store')
  37. wx.navigateBack()
  38. }
  39. })
  40. },
  41. /**
  42. * 唤起微信支付
  43. */
  44. wchatPayment: function (buytype) {
  45. let that = this;
  46. var ids = app.util().clearArray(wx.getStorageSync('group'));
  47. var param = {
  48. ids: ids,
  49. address: that.data.address.id,
  50. buytype: buytype,
  51. ucode: app.globalData.loginuser.ucode
  52. }
  53. app.api().Post("api/v3/fastshop/group/doPay", param, function (rel) {
  54. if (200 == rel.code) {
  55. wx.removeStorageSync('group')
  56. if (rel.data.type == 1) {
  57. that.setData({
  58. chickPayBtn: true,
  59. orderParams: rel.data.order
  60. })
  61. } else {
  62. app.doWechatPay(rel.data.order, function (res) {
  63. wx.navigateTo({
  64. url: '../order/index'
  65. })
  66. }, function (res) {
  67. wx.showModal({
  68. content: '支付失败或取消', showCancel: false, complete: function () {
  69. wx.navigateTo({
  70. url: '../order/index'
  71. });
  72. }
  73. })
  74. })
  75. }
  76. }
  77. })
  78. },
  79. /**
  80. * 读取微信地址
  81. */
  82. getAddress: function () {
  83. let that = this;
  84. app.api().Get("openapi/v1/user/getaddress", { 'signkey': 'dopay' }, function (rel) {
  85. if (rel.code == 200) {
  86. that.setData({
  87. address: rel.data,
  88. address_isnull: Object.keys(rel.data).length,
  89. })
  90. }
  91. })
  92. },
  93. /**
  94. * 读取微信地址
  95. */
  96. address: function () {
  97. let that = this;
  98. wx.getSetting({
  99. success(res) {
  100. if (res.authSetting['scope.address'] == false) {
  101. wx.openSetting({
  102. success: (res) => {
  103. res.authSetting = {
  104. "scope.address": true,
  105. }
  106. }
  107. })
  108. } else {
  109. wx.chooseAddress({
  110. success: function (res) {
  111. var name = res.userName;
  112. var telNumber = res.telNumber;
  113. var city = res.provinceName + res.cityName + res.countyName;
  114. var address = res.detailInfo;
  115. app.api().Post("openapi/v1/user/createaddress", {
  116. name: name,
  117. telphone: telNumber,
  118. city: city,
  119. address: address
  120. }, function (rel) {
  121. that.setData({
  122. address: rel.data,
  123. address_isnull: Object.keys(rel.data).length,
  124. })
  125. });
  126. }
  127. })
  128. }
  129. }
  130. })
  131. },
  132. /**
  133. * 支付方式
  134. */
  135. payTypes: function () {
  136. let that = this;
  137. var actions = that.data.actions;
  138. var address_isnull = that.data.address_isnull
  139. if (address_isnull == 0) {
  140. wx.showModal({
  141. content: '必须选择收货地址',
  142. })
  143. } else {
  144. this.setData({
  145. show: true
  146. });
  147. }
  148. if (that.data.show) {
  149. app.api().Get('api/v3/fastshop/index/shopBuyTypes', { 'signkey': 'shopBuyTypes' }, function (result) {
  150. that.setData({
  151. actions: result.data
  152. });
  153. })
  154. }
  155. },
  156. /**
  157. * 是否弹出支付菜单
  158. */
  159. onClose() {
  160. this.setData({
  161. show: false
  162. });
  163. },
  164. onSelect(event) {
  165. let that = this;
  166. var address_isnull = that.data.address_isnull
  167. var addressId = that.data.address.id;
  168. var payTypes = event.detail.types;
  169. wx.showLoading({ title: '支付中' })
  170. switch (payTypes) {
  171. case 1:
  172. that.wchatPayment("wepay");
  173. break;
  174. case 2:
  175. that.wchatPayment("point");
  176. break;
  177. default:
  178. that.onClose();
  179. }
  180. },
  181. /**
  182. * 支付成功的事件处理函数
  183. * res.detail 为 payjs 小程序返回的订单信息
  184. * 可通过 res.detail.payjsOrderId 拿到 payjs 订单号
  185. * 可通过 res.detail.responseData 拿到详细支付信息
  186. */
  187. goodPaySuccess: function (res) {
  188. if (res.detail.return_code = "SUCCESS") {
  189. wx.navigateTo({
  190. url: '/pages/order/index'
  191. })
  192. }
  193. },
  194. /**
  195. * 支付失败的事件处理函数
  196. * res.detail.error 为 true 代表传入小组件的参数存在问题
  197. * res.detail.navigateSuccess 代表了是否成功跳转到 PAYJS 小程序
  198. * res.detail.event 可能存有失败的原因
  199. * 如果下单成功但是用户取消支付则 res.detail.event.return_code == FAIL
  200. */
  201. goodPayFail: function (res) {
  202. this.setData({
  203. chickPayBtn: false,
  204. cart_number: 0
  205. })
  206. },
  207. /**
  208. * 支付完毕的事件处理函数
  209. * 无论支付成功或失败均会执行
  210. */
  211. goodPayComplete: function () {
  212. this.setData({
  213. chickPayBtn: false,
  214. cart_number: 0
  215. })
  216. },
  217. /**
  218. * 组件内部数据被修改时的事件
  219. * 当用户跳转到 云收银台 小程序并等待返回的过程中 chickOnPay 值为 true
  220. */
  221. goodPayChange(res) {
  222. if (res.detail.chickOnPay) {
  223. this.setData({
  224. chickOnPay: res.detail.chickOnPay
  225. })
  226. }
  227. }
  228. })