request.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // 新正式(未审批)
  2. // const apiHost = 'https://fohowapi.hiwavo.com/'
  3. // 测试
  4. const apiHost = 'https://tfohowapi.hiwavo.com/'
  5. function $get (url, params, success, fail) {
  6. wx.showNavigationBarLoading()
  7. var session = wx.getStorageSync('lbt_session_key')
  8. var token = wx.getStorageSync('lbt_token_key')
  9. // console.log('nowtoken',token);
  10. wx.request({
  11. url: apiHost + url,
  12. data: params,
  13. method: 'GET',
  14. header: {
  15. "content-type": "application/json",
  16. "terminal": 'mini-program',
  17. // "Cookie": 'fohow_sid=' + session
  18. "Authorization":"Bearer " + token
  19. },
  20. success: function (res) {
  21. callBack(res, success, fail)
  22. },
  23. fail: function (res) {
  24. callBack(res)
  25. },
  26. complete: function (res) {
  27. wx.hideNavigationBarLoading()
  28. }
  29. })
  30. }
  31. function $post (url, params, success, fail) {
  32. wx.showNavigationBarLoading()
  33. var session = wx.getStorageSync('lbt_session_key');
  34. var token = wx.getStorageSync('lbt_token_key');
  35. wx.request({
  36. url: apiHost + url,
  37. data: params,
  38. method: 'POST',
  39. header: {
  40. "content-type": "application/x-www-form-urlencoded",
  41. "terminal": 'mini-program',
  42. // "Cookie": 'fohow_sid=' + session
  43. "Authorization":"Bearer " + token
  44. },
  45. success: function (res) {
  46. callBack(res, success, fail)
  47. },
  48. fail: function (res) {
  49. },
  50. complete: function (res) {
  51. wx.hideNavigationBarLoading()
  52. }
  53. })
  54. }
  55. function $put (url, params, success, fail) {
  56. wx.showNavigationBarLoading()
  57. var session = wx.getStorageSync('lbt_session_key');
  58. var token = wx.getStorageSync('lbt_token_key');
  59. wx.request({
  60. url: apiHost + url,
  61. data: params,
  62. method: 'PUT',
  63. header: {
  64. "content-type": "application/x-www-form-urlencoded",
  65. "terminal": 'mini-program',
  66. // "Cookie": 'fohow_sid=' + session
  67. "Authorization":"Bearer " + token
  68. },
  69. success: function (res) {
  70. callBack(res, success, fail)
  71. },
  72. fail: function (res) {
  73. },
  74. complete: function (res) {
  75. wx.hideNavigationBarLoading()
  76. }
  77. })
  78. }
  79. function $del (url, params, success, fail) {
  80. wx.showNavigationBarLoading()
  81. var session = wx.getStorageSync('lbt_session_key');
  82. var token = wx.getStorageSync('lbt_token_key');
  83. wx.request({
  84. url: apiHost + url,
  85. data: params,
  86. method: 'DELETE',
  87. header: {
  88. "content-type": "application/x-www-form-urlencoded",
  89. "terminal": 'mini-program',
  90. // "Cookie": 'fohow_sid=' + session,
  91. "Authorization":"Bearer " + token
  92. },
  93. success: function (res) {
  94. callBack(res, success, fail)
  95. },
  96. fail: function (res) {
  97. },
  98. complete: function (res) {
  99. wx.hideNavigationBarLoading()
  100. }
  101. })
  102. }
  103. function callBack (res, success, fail) {
  104. if (res.statusCode === 200) {
  105. success(res)
  106. } else {
  107. if (fail) {
  108. fail(res)
  109. }
  110. switch (res.data.err_code) {
  111. case 'userNeedLogin':
  112. var pages = getCurrentPages()
  113. console.log(pages)
  114. var currentPage = pages[pages.length-1]
  115. var url = currentPage.route
  116. var options = currentPage.options
  117. var params = ''
  118. console.log(url)
  119. for (let a in options) {
  120. params += a + '=' +options[a] + '&'
  121. }
  122. if (params) {
  123. url = '/' + url + '?' + params
  124. } else {
  125. url = '/' + url
  126. }
  127. console.log(url)
  128. // wx.showToast({
  129. // title: 'getApp().globalData.try_login'+getApp().globalData.try_login,
  130. // icon: 'none',
  131. // duration: 2000
  132. // })
  133. // return false;
  134. if (getApp().globalData.try_login) {
  135. getApp().globalData.try_login = false
  136. // wx.showToast({
  137. // title: '尝试登陆失败,请到个人中心进行授权登陆',
  138. // icon: 'none',
  139. // duration: 2000
  140. // })
  141. // setTimeout(function(){
  142. // wx.switchTab({
  143. // url: '/pages/user/all/all'
  144. // })
  145. // },1000)
  146. //新token
  147. wx.login({
  148. success: res => {
  149. getApp().globalData.try_login = true;
  150. pageLogin(res, url)
  151. }
  152. })
  153. } else {
  154. wx.login({
  155. success: res => {
  156. getApp().globalData.try_login = true;
  157. pageLogin(res, url)
  158. }
  159. })
  160. }
  161. break
  162. default:
  163. if(res.data.err_msg.length > 20){
  164. wx.showModal({
  165. confirmColor: '#eab86a',
  166. content: res.data.err_msg,
  167. showCancel: false,//没有取消按钮的弹框
  168. success: function(res) {
  169. }
  170. })
  171. }else{
  172. wx.showToast({
  173. title: res.data.err_msg,
  174. icon: 'none',
  175. duration: 2000
  176. })
  177. }
  178. }
  179. }
  180. }
  181. // 登陆接口
  182. function login (res) {
  183. var url = 'xcx/login'
  184. var invite = wx.getStorageSync('invite');
  185. var params = {
  186. code: res.code,
  187. invite_id: invite//新token模式
  188. }
  189. var success = function (res) {
  190. wx.setStorageSync('lbt_session_key', res.data.session_key)
  191. wx.setStorageSync('lbt_session_time', Date.now())
  192. wx.setStorageSync('lbt_token_key', res.data.token)
  193. // check()
  194. console.log('login:id=' + res.data.wx_user.id)
  195. getApp().globalData.wx_id = res.data.wx_user.id;
  196. }
  197. $get(url, params, success)
  198. }
  199. // 获取授权信息
  200. function check () {
  201. var url = 'v1/user/check'
  202. var params = {
  203. }
  204. var success = function (res) {
  205. console.log('login:id=' + res.data.wx_user_id)
  206. getApp().globalData.wx_id = res.data.wx_user_id;
  207. }
  208. $get(url, params, success)
  209. }
  210. function getAuthorize () {
  211. wx.getSetting({
  212. success: res => {
  213. var auth = res.authSetting
  214. if (!auth['scope.userInfo']) {
  215. setAuthorize()
  216. }
  217. }
  218. })
  219. }
  220. // 主动请求授权
  221. function setAuthorize () {
  222. wx.authorize({
  223. scope: 'scope.userInfo',
  224. success: res => {
  225. sendInfo()
  226. },
  227. fail: res => {
  228. getAuthorizePopup()
  229. }
  230. })
  231. }
  232. // 发送个人信息给后端
  233. function sendInfo (a, channel, invite, that) {
  234. // var userInfo = a.detail;
  235. var userInfo = a;//新授权方式
  236. var str = JSON.stringify(userInfo)
  237. updateWxUserInfo(str, channel, invite, that)
  238. }
  239. // 显示未授权提示弹框
  240. function getAuthorizePopup () {
  241. wx.showModal({
  242. title: '用户未授权',
  243. content: '如需正常使用凤凰菁选商城,请按确定并在授权管理中选中“用户信息”,然后点按确定。最后再重新进入小程序即可正常使用。',
  244. showCancel: false,
  245. success: function (res) {
  246. if (res.confirm) {
  247. openAuthorize()
  248. }
  249. }
  250. })
  251. }
  252. // 进入授权界面
  253. function openAuthorize () {
  254. wx.openSetting({
  255. success: function (res) {
  256. var auth = res.authSetting
  257. if (!auth['scope.userInfo']) {
  258. // getAuthorizePopup()
  259. } else {
  260. sendInfo()
  261. }
  262. }
  263. })
  264. }
  265. // 更新个人信息
  266. function updateWxUserInfo (val, channel, invite, that) {
  267. var url = 'xcx/authorize'
  268. var params = {
  269. userinfo: val,
  270. channel: channel,
  271. invite_id: invite
  272. }
  273. var success = function (res) {
  274. wx.hideLoading()
  275. that.setData({
  276. popup: false
  277. })
  278. that.getVersion(500)
  279. }
  280. $post(url, params, success)
  281. }
  282. function pageLogin (res, str) {
  283. var url = 'xcx/login'
  284. var params = {
  285. code: res.code
  286. }
  287. var success = function (res) {
  288. console.log('pageLogin',res);
  289. res = res.data;
  290. if(res.wx_user){
  291. // console.log('newtoken---',res.token);
  292. wx.setStorageSync('lbt_session_key', res.session_key)
  293. wx.setStorageSync('lbt_token_key', res.token)
  294. wx.setStorageSync('lbt_session_time', Date.now())
  295. wx.redirectTo({
  296. url: str
  297. })
  298. }else{
  299. wx.showToast({
  300. title: '尝试登陆失败,请到个人中心进行授权登陆',
  301. icon: 'none',
  302. duration: 2000
  303. })
  304. }
  305. }
  306. $get(url, params, success)
  307. }
  308. function share (val, timline) {
  309. var url = ''
  310. var image = ''
  311. var title = val.title || '欢迎来到凤凰菁选商城'
  312. if (val.path) {
  313. if (val.path.indexOf('?') > -1) {
  314. url = val.path + '&invite=' + getApp().globalData.wx_id
  315. } else {
  316. url = val.path + '?invite=' + getApp().globalData.wx_id
  317. }
  318. } else {
  319. url = '/pages/start/start?invite=' + getApp().globalData.wx_id
  320. }
  321. if (getApp().globalData.wx_id === 0) {
  322. title = '·' + title
  323. }
  324. if (val.imageUrl === undefined) {
  325. image = 'http://fohow.oss-cn-shenzhen.aliyuncs.com/xcx/share/sharemain.jpg'
  326. } else {
  327. image = val.imageUrl
  328. }
  329. console.log(title)
  330. if(timline){
  331. var query = url.split('?')[1];
  332. return {
  333. title: title,
  334. path: url,
  335. imageUrl: image,
  336. query: query,
  337. success: function (res) {
  338. shareInfo(val)
  339. }
  340. }
  341. }else{
  342. return {
  343. title: title,
  344. path: url,
  345. imageUrl: image,
  346. success: function (res) {
  347. shareInfo(val)
  348. }
  349. }
  350. }
  351. }
  352. function $uploadFile(url, name, file, params, success, fail){
  353. wx.showNavigationBarLoading()
  354. var session = wx.getStorageSync('lbt_session_key');
  355. var token = wx.getStorageSync('lbt_token_key');
  356. wx.uploadFile({
  357. url: apiHost + url, //仅为示例,非真实的接口地址
  358. filePath: file,
  359. name: name,
  360. formData: params,
  361. header: {
  362. "content-type": "application/x-www-form-urlencoded",
  363. "terminal": 'mini-program',
  364. // "Cookie": 'fohow_sid=' + session,
  365. "Authorization":"Bearer " + token
  366. },
  367. success(res) {
  368. callBack(res, success, fail)
  369. },
  370. fail(res) {
  371. console.log('err',res)
  372. },
  373. complete(res) {
  374. wx.hideNavigationBarLoading()
  375. }
  376. })
  377. }
  378. function shareInfo (val) {
  379. var url = 'v1/share/info'
  380. var params = {
  381. sc: val.sc || 'xcx',
  382. ri: val.ri || 0,
  383. rp: val.rp || 0,
  384. to: 'group'
  385. }
  386. var success = function (val) {
  387. }
  388. $post(url, params, success)
  389. }
  390. module.exports = {
  391. $post: $post,
  392. $put: $put,
  393. $get: $get,
  394. $del: $del,
  395. login: login,
  396. getAuthorize: getAuthorize,
  397. updateWxUserInfo: updateWxUserInfo,
  398. sendInfo: sendInfo,
  399. share: share,
  400. $uploadFile:$uploadFile
  401. }