config.js 6.5 KB

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