request.js 11 KB

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