dopay.js 6.4 KB

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