request.js 11 KB

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