request.js 10 KB

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