config.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // let api = "https://passport.lrlz.com/mobile/index.php";
  2. // let api = "http://www.xyzshops.cn/mobile/index.php";
  3. // let api = "http://192.168.1.200/mobile/index.php";
  4. // let api = "https://www.xyzshops.cn/mobile/index.php";
  5. let api = "http://www.xyzshops.cn/mobile/index.php";
  6. function getReq(app, data, callback, method) {
  7. let options = {
  8. client_type: 'mini'
  9. }
  10. data = Object.assign({}, options, data);
  11. let session_id = wx.getStorageSync('session_id');
  12. if (!app.globalData.fMinistart && data['op'] != 'ministart') {
  13. setTimeout(() => {
  14. getReq(app, data, callback, method)
  15. }, 0);
  16. return;
  17. }
  18. let act = data['act'];
  19. if (act.indexOf('member_') == 0) {
  20. if (app != null && !app.globalData.userInfo) {
  21. wx.navigateTo({
  22. url: '/pages/auth/auth',
  23. })
  24. return;
  25. }
  26. }
  27. console.log('act=' + act + '&' + 'op=' + data['op']);
  28. let header = {
  29. 'content-type': 'application/json', // 默认值
  30. 'Cookie': 'MPHPSESSID=' + session_id
  31. }
  32. if (method == 'POST') {
  33. header = {
  34. 'content-type': 'application/x-www-form-urlencoded', //数据转换成 query string
  35. 'Cookie': 'MPHPSESSID=' + session_id
  36. }
  37. }
  38. return wx.request({
  39. url: api,
  40. data,
  41. method: method || 'GET',
  42. header,
  43. success(res) {
  44. console.log('header:', res.header["Set-Cookie"])
  45. var cookies = res.header["Set-Cookie"];
  46. if (cookies) {
  47. var cookie = getCookieValue(cookies, 'MPHPSESSID');
  48. if (cookie) wx.setStorageSync('session_id', cookie);
  49. }
  50. if (res.data.code == 10014) { //ErrUnLogin = 10014;
  51. wx.navigateTo({
  52. url: '/pages/auth/auth',
  53. })
  54. } else {
  55. callback(res.data)
  56. }
  57. },
  58. fail() {
  59. wx.showModal({
  60. confirmText: '重试',
  61. content: '网络错误',
  62. success: function (res) {
  63. if (res.confirm) {
  64. wx.reLaunch({
  65. url: "/pages/index/index"
  66. })
  67. } else if (res.cancel) {
  68. console.log('用户点击取消')
  69. }
  70. }
  71. })
  72. }
  73. })
  74. }
  75. function getCookieValue(cookies, name) {
  76. var cookie_pos = cookies.indexOf(name, 0);
  77. if (cookie_pos != -1) {
  78. cookie_pos += name.length + 1;
  79. var cookie_end = cookies.indexOf(";", cookie_pos);
  80. if (cookie_end == -1) {
  81. cookie_end = cookies.length;
  82. }
  83. var value = unescape(cookies.substring(cookie_pos, cookie_end));
  84. return value;
  85. } else {
  86. return '';
  87. }
  88. }
  89. function buyVGoods(app, goods_id, goods_num, other, successCallback, failCallback) {
  90. let ifcart = 0;
  91. let cartids = goods_id + '|' + goods_num;
  92. let params = {
  93. act: 'member_buy',
  94. op: 'step_vsecond',
  95. payment: 'minipay',
  96. usebonus: 0,
  97. ifcart: 0,
  98. cart_id: cartids,
  99. invoice_id: 0,
  100. ...other
  101. }
  102. console.table(params)
  103. wx.showLoading({
  104. title: '加载中',
  105. mask: true
  106. });
  107. params = Object.assign({}, params);
  108. getReq(app, params, function (res) {
  109. wx.hideLoading()
  110. if (res.code == 200) {
  111. let param = res.datas.param.data
  112. let pay_sn = res.datas.pay_sn
  113. wx.requestPayment({
  114. timeStamp: param.timeStamp, //时间戳,自1970年以来的秒数
  115. nonceStr: param.nonceStr, //随机串
  116. package: param.package,
  117. signType: param.signType, //微信签名方式:
  118. paySign: param.paySign, //微信签名
  119. success: function (res) {
  120. wx.hideLoading()
  121. successCallback && successCallback(res)
  122. wx.aldPushSubscribeMessage({
  123. eventId: '5f38ac6790fcd68beee4d510',
  124. success(res) {
  125. // 成功后的回调函数
  126. console.log(res)
  127. },
  128. fail(res, e) {
  129. // 失败后的回调函数
  130. console.log(res)
  131. console.log(e)
  132. }
  133. });
  134. },
  135. fail: function (res) {
  136. wx.showToast({
  137. icon: 'none',
  138. title: '支付失败',
  139. duration: 2000
  140. })
  141. failCallback && failCallback(res)
  142. }
  143. });
  144. } else {
  145. wx.showToast({
  146. icon: 'none',
  147. title: '支付未完成,订单将在10分钟后取消...',
  148. duration: 2000
  149. })
  150. }
  151. })
  152. }
  153. function payOrder(app, pay_sn) {
  154. let params = {
  155. act: 'member_order',
  156. op: 'pay',
  157. pay_sn: pay_sn,
  158. payment: 'minipay'
  159. }
  160. getReq(app, params, function (res) {
  161. if (res.code == 200) {
  162. let param = res.datas.param.data;
  163. wx.requestPayment({
  164. timeStamp: param.timeStamp, //时间戳,自1970年以来的秒数
  165. nonceStr: param.nonceStr, //随机串
  166. package: param.package,
  167. signType: param.signType, //微信签名方式:
  168. paySign: param.paySign, //微信签名
  169. success: function (res) {
  170. wx.redirectTo({
  171. url: "/pages/order_tabs/orderTabs?state_type=state_pay"
  172. })
  173. wx.aldPushSubscribeMessage({
  174. eventId: '5f38ac6790fcd68beee4d510',
  175. success(res) {
  176. // 成功后的回调函数
  177. console.log(res)
  178. },
  179. fail(res, e) {
  180. // 失败后的回调函数
  181. console.log(res)
  182. console.log(e)
  183. }
  184. });
  185. console.log("支付成功:", res);
  186. },
  187. fail: function (res) {
  188. setTimeout(() => {
  189. wx.showToast({
  190. icon: 'none',
  191. title: '支付未完成,订单将在10分钟后取消...',
  192. duration: 2000
  193. })
  194. }, 200);
  195. console.log("失败:", res);
  196. }
  197. });
  198. }
  199. })
  200. }
  201. module.exports = {
  202. getReq: getReq,
  203. host: api,
  204. buyVGoods: buyVGoods,
  205. payOrder: payOrder
  206. }