request.js 10 KB

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