net.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. const app = getApp()
  2. var url = require('url.js')
  3. var util = require('md5.js')
  4. var isShowFailModal = false
  5. var prefix = 'a'
  6. var suffix = prefix
  7. //封装微信request请求
  8. function connect(obj, flag) {
  9. var now = new Date().getTime()
  10. signature(obj)
  11. wx.request({
  12. url: obj.url,
  13. data: obj.data,
  14. method: obj.method,
  15. header: {
  16. 'content-type': 'application/x-www-form-urlencoded',
  17. 'My-User-Agent': app.globalData.userAgent
  18. },
  19. success: res => {
  20. app.log('time : ' + (new Date().getTime() - now) + ' : ' + obj.url)
  21. app.log(res.data)
  22. if (res.data.code == 200) {
  23. obj.success(res)
  24. } else if (res.data.code == 601 || res.data.code == 10004) {
  25. //KEY失效,重新获取KEY,重跑接口
  26. login(obj, flag)
  27. } else {
  28. //若返回500错误码
  29. if (flag) {
  30. //不作操作,返回给初始函数处理
  31. obj.success(res)
  32. } else {
  33. //默认操作,弹出错误提示
  34. wx.hideLoading()
  35. wx.showToast({
  36. duration: 2000,
  37. icon: 'none',
  38. title: res.data.msg || '',
  39. })
  40. }
  41. }
  42. },
  43. fail: e => {
  44. fail(obj, flag)
  45. },
  46. complete: obj.complete
  47. })
  48. }
  49. function signature(obj){
  50. obj.data = obj.data || {}
  51. obj.data.key = app.server.key
  52. var time = new Date().getTime() + app.globalData.timeDifference
  53. var uri = obj.url
  54. var len = uri.indexOf('?')
  55. len = len >= 0 ? len : uri.length
  56. uri = uri.substring(uri.indexOf('/bnl'), len)
  57. var signature = prefix + uri + obj.data.key + time + suffix
  58. signature = util.md5(signature)
  59. obj.data.mytimestamp = time
  60. obj.data.mysignature = signature
  61. }
  62. function fail(obj, flag) {
  63. wx.hideLoading()
  64. if (isShowFailModal){
  65. return
  66. }
  67. isShowFailModal = true
  68. wx.showModal({
  69. title: '',
  70. content: '网络连接不稳定,请稍后重试',
  71. cancelText: '忽略',
  72. success: function (res) {
  73. isShowFailModal = false
  74. if (res.confirm) {
  75. connect(obj, flag)
  76. }
  77. }
  78. })
  79. }
  80. function login(obj, flag) {
  81. wx.login({
  82. success: res => {
  83. app.globalData.code = res.code
  84. getUserInfo(obj, flag)
  85. },
  86. fail: e => {
  87. fail(obj, flag)
  88. }
  89. })
  90. }
  91. function getUserInfo(obj, flag) {
  92. wx.getUserInfo({
  93. success: res => {
  94. app.globalData.userInfo = res.userInfo
  95. getKey(obj, res, flag)
  96. },
  97. fail: e => {
  98. fail(obj, flag)
  99. }
  100. })
  101. }
  102. function getKey(obj, res, flag) {
  103. var state = ''
  104. if (app.globalData.identify.recomCode) {
  105. state = app.globalData.identify.shareIdentify + '#' + app.globalData.identify.recomCode
  106. }
  107. connect({
  108. url: url.host + url.login,
  109. data: {
  110. code: app.globalData.code,
  111. rawData: res.rawData,
  112. signature: res.signature,
  113. state: state
  114. },
  115. success: res => {
  116. if (res.data.code == 200) {
  117. var _result = res.data.data
  118. var nowTime = res.data.data.nowTime || new Date().getTime()
  119. app.globalData.timeDifference = nowTime - new Date().getTime()
  120. app.server = _result
  121. connect(obj, flag)
  122. } else {
  123. fail(obj, flag)
  124. }
  125. }
  126. }, true)
  127. }
  128. function a(p, s){
  129. prefix = p
  130. suffix = s
  131. }
  132. function b(p) {
  133. prefix = p
  134. }
  135. function c(s) {
  136. suffix = s
  137. }
  138. function e(p){
  139. return prefix
  140. }
  141. function f(s){
  142. return suffix
  143. }
  144. //微信下单支付流程
  145. function wxpay(obj) {
  146. getOrder(obj)
  147. }
  148. //获取订单接口
  149. function getOrder(obj) {
  150. connect({
  151. url: obj.url,
  152. data: obj.data,
  153. method: 'POST',
  154. success: res => {
  155. order(obj, res.data.data)
  156. }
  157. })
  158. }
  159. //微信支付统一调用接口
  160. function order(obj, order) {
  161. if (app.globalData.debug) {
  162. connect({
  163. url: url.host + url.testPay,
  164. data: { out_trade_no: order },
  165. success: res => {
  166. obj.success(res)
  167. }
  168. })
  169. return
  170. }
  171. connect({
  172. url: url.host + url.unified,
  173. data: { out_trade_no: order },
  174. method: 'POST',
  175. success: res => {
  176. pay(obj, order, res.data.data)
  177. }
  178. })
  179. }
  180. //微信支付
  181. function pay(obj, no, order) {
  182. wx.requestPayment({
  183. timeStamp: order.timeStamp,
  184. nonceStr: order.nonceStr,
  185. package: order.package,
  186. signType: 'MD5',
  187. paySign: order.paySign,
  188. success: res => {
  189. wx.showLoading({
  190. title: '',
  191. })
  192. setTimeout(function () {
  193. query(obj, no)
  194. }.bind(this), 2000);
  195. },
  196. fail: e => {
  197. wx.hideLoading()
  198. wx.showModal({
  199. title: '',
  200. content: '支付失败',
  201. showCancel: false
  202. })
  203. }
  204. })
  205. }
  206. //查询返回结果
  207. function query(obj, no, end) {
  208. connect({
  209. url: url.host + url.wx_query,
  210. method: 'POST',
  211. data: {
  212. out_trade_no: no
  213. },
  214. success: res => {
  215. if (res.data.code == 200 && res.data.data == 1) {
  216. obj.success(res)
  217. } else {
  218. if (end) {
  219. wx.hideLoading()
  220. wx.showModal({
  221. title: '',
  222. content: res.data.msg,
  223. showCancel: false,
  224. success: res => {
  225. }
  226. })
  227. } else {
  228. setTimeout(function () {
  229. query(obj, no, true)
  230. }.bind(this), 2000);
  231. }
  232. }
  233. }
  234. }, false)
  235. }
  236. module.exports = {
  237. a: a,
  238. b: b,
  239. c: c,
  240. e: e,
  241. f: f,
  242. connect: connect,
  243. wxpay: wxpay,
  244. }