config.js 6.4 KB

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