api.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import {
  2. baseUrl,
  3. baseApiUrl
  4. } from './config.js';
  5. import * as common from './common.js' //引入common
  6. import * as db from './db.js' //引入common
  7. // 需要登陆的,都写到这里,否则就是不需要登陆的接口
  8. let methodsToken = ['profile', 'refreshUser', 'wxLogin', 'changeMobile', 'getContentDetail',
  9. 'addCart', 'previewOrder', 'editMainBodyUser', 'like', 'unLike', 'getUserLike', 'getUserCollect', 'collect',
  10. 'uncollect', 'contribute'
  11. ];
  12. const post = (method, data, callback, type, orgurl) => {
  13. let userToken = '';
  14. let auth = '';
  15. // 判断token是否存在
  16. // console.log("method: ", method);
  17. if (methodsToken.indexOf(method) >= 0) {
  18. // 获取用户token
  19. let auth = db.get("auth");
  20. // console.log(auth);
  21. let nowdate = (new Date()) / 1000; //当前时间戳
  22. //新增用户判断是否登录逻辑begin
  23. common.isLogin();
  24. //新增用户判断是否登录逻辑end
  25. if (!auth || auth.createtime + auth.expires_in < nowdate) {
  26. common.toLogin();
  27. return false;
  28. } else {
  29. userToken = auth.token;
  30. }
  31. }
  32. if (type) {
  33. method = type + '/' + method
  34. } else {
  35. method = '/' + method
  36. }
  37. let realurl = baseApiUrl + method;
  38. if (orgurl) {
  39. realurl = baseUrl + orgurl;
  40. }
  41. uni.showLoading({
  42. title: '',
  43. icon: 'loading'
  44. });
  45. uni.request({
  46. url: realurl,
  47. data: data,
  48. header: {
  49. 'Accept': 'application/json',
  50. 'Content-Type': 'application/x-www-form-urlencoded',
  51. 'token': userToken,
  52. '__token__': userToken
  53. },
  54. method: 'POST',
  55. success: (response) => {
  56. uni.hideLoading();
  57. const result = response.data
  58. if (result.msg == 'Please login' || result.msg == '请登陆') {
  59. db.del("user");
  60. db.del("auth");
  61. console.log('未登陆')
  62. uni.showToast({
  63. title: result.msg,
  64. icon: 'none',
  65. duration: 2000,
  66. complete: function() {
  67. uni.reLaunch({
  68. url: '/pages/index/index',
  69. })
  70. }
  71. });
  72. }
  73. callback(result);
  74. },
  75. fail: (error) => {
  76. console.log(error)
  77. uni.hideLoading();
  78. if (error && error.response) {
  79. showError(error.response);
  80. }
  81. },
  82. });
  83. }
  84. // 上传图片
  85. export const uploadImage = (method, data = {}, callback, num = 9, type) => {
  86. if (type) {
  87. method = type + '/' + method
  88. } else {
  89. method = method
  90. }
  91. let userToken = '';
  92. let auth = db.get("auth");
  93. userToken = auth.token;
  94. uni.chooseImage({
  95. count: num,
  96. success: (res) => {
  97. uni.showLoading({
  98. title: '上传中...'
  99. });
  100. let tempFilePaths = res.tempFilePaths
  101. for (var i = 0; i < tempFilePaths.length; i++) {
  102. data.file = tempFilePaths[i]
  103. uni.uploadFile({
  104. url: baseApiUrl + method,
  105. filePath: tempFilePaths[i],
  106. fileType: 'image',
  107. name: 'file',
  108. headers: {
  109. 'Accept': 'application/json',
  110. 'Content-Type': 'multipart/form-data',
  111. 'token': userToken
  112. },
  113. formData: data,
  114. success: (uploadFileRes) => {
  115. callback(JSON.parse(uploadFileRes.data))
  116. },
  117. fail: (error) => {
  118. if (error && error.response) {
  119. common.showError(error.response);
  120. }
  121. },
  122. complete: () => {
  123. setTimeout(function() {
  124. uni.hideLoading();
  125. }, 250);
  126. },
  127. });
  128. }
  129. }
  130. });
  131. }
  132. const get = (url, callback) => {
  133. uni.showLoading({
  134. title: '加载中'
  135. });
  136. let realurl = baseUrl + url;
  137. if (url.indexOf('http:') >= 0 || url.indexOf('https:') >= 0) {
  138. let realurl = url;
  139. }
  140. uni.request({
  141. url: realurl,
  142. header: {
  143. 'Accept': 'application/json',
  144. 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
  145. },
  146. method: 'GET',
  147. success: (response) => {
  148. callback(response.data);
  149. },
  150. fail: (error) => {
  151. if (error && error.response) {
  152. showError(error.response);
  153. }
  154. },
  155. complete: () => {
  156. setTimeout(function() {
  157. uni.hideLoading();
  158. }, 250);
  159. }
  160. });
  161. }
  162. const showError = error => {
  163. let errorMsg = ''
  164. switch (error.status) {
  165. case 400:
  166. errorMsg = '请求参数错误'
  167. break
  168. case 401:
  169. errorMsg = '未授权,请登录'
  170. break
  171. case 403:
  172. errorMsg = '跨域拒绝访问'
  173. break
  174. case 404:
  175. errorMsg = `请求地址出错: ${error.config.url}`
  176. break
  177. case 408:
  178. errorMsg = '请求超时'
  179. break
  180. case 500:
  181. errorMsg = '服务器内部错误'
  182. break
  183. case 501:
  184. errorMsg = '服务未实现'
  185. break
  186. case 502:
  187. errorMsg = '网关错误'
  188. break
  189. case 503:
  190. errorMsg = '服务不可用'
  191. break
  192. case 504:
  193. errorMsg = '网关超时'
  194. break
  195. case 505:
  196. errorMsg = 'HTTP版本不受支持'
  197. break
  198. default:
  199. errorMsg = error.msg
  200. break
  201. }
  202. uni.showToast({
  203. title: errorMsg,
  204. icon: 'none',
  205. duration: 2000
  206. });
  207. }
  208. const syncget = (url, data) => {
  209. return new Promise(function(resolve, reject) {
  210. get(url + queryParams(data, true), (result) => {
  211. try {
  212. resolve(result);
  213. } catch (e) {
  214. reject(e);
  215. }
  216. })
  217. })
  218. }
  219. const syncpost = (f, m, d) => {
  220. return new Promise(function(resolve, reject) {
  221. post(f, d, (result) => {
  222. try {
  223. resolve(result);
  224. } catch (e) {
  225. reject(e);
  226. }
  227. }, m, m);
  228. })
  229. }
  230. // 登录
  231. export const third = (data, callback) => post('third', data, callback, 'content/main_body_user');
  232. // 用户绑定手机
  233. export const bindphone = (data, callback) => post('bind', data, callback, 'discover/User');
  234. // 修改用户信息
  235. export const profile = (data, callback) => post('profile', data, callback, 'discover/User');
  236. // 发送验证码
  237. export const sendSmsVerify = (data, callback) => post('sendSmsVerify', data, callback, 'discover/User');
  238. // 刷新用户
  239. export const refreshUser = (data, callback) => post('refreshUser', data, callback, 'content/main_body_user');
  240. // 注册
  241. // export const register = (data, callback) => post('register', data, callback, 'discover/User');
  242. // 登录
  243. export const login = (data, callback) => post('login', data, callback, 'discover/User');
  244. // 退出登录
  245. export const logout = (data, callback) => post('logout', data, callback, 'discover/User');
  246. // 上传头像
  247. export const upload = (data, callback) => post('upload', data, callback, 'discover/Ajax');
  248. // 绑定手机
  249. export const changeMobile = (data, callback) => post('changeMobile', data, callback, 'discover/User');
  250. //获取手机号前先登录
  251. export const wxLogin = (data, callback) => post('wxLogin', data, callback, 'user');
  252. //获取手机号
  253. export const getMobile = (data, callback) => post('getPhoneNumber', data, callback, 'user');
  254. //取CMS配置
  255. // export const getConfig = async (params = {}) => await syncget('addons/cms/api.common/init', params);
  256. function queryParams(data = {}, isPrefix = true, arrayFormat = 'brackets') {
  257. let prefix = isPrefix ? '?' : ''
  258. let _result = []
  259. if (['indices', 'brackets', 'repeat', 'comma'].indexOf(arrayFormat) == -1) arrayFormat = 'brackets';
  260. for (let key in data) {
  261. let value = data[key]
  262. // 去掉为空的参数
  263. if (['', undefined, null].indexOf(value) >= 0) {
  264. continue;
  265. }
  266. if (value.length > 0) {
  267. value = value.replace(/%/g, "%25");
  268. value = value.replace(/\&/g, "%26");
  269. value = value.replace(/\+/g, "%2B");
  270. }
  271. // 如果值为数组,另行处理
  272. if (value.constructor === Array) {
  273. // e.g. {ids: [1, 2, 3]}
  274. switch (arrayFormat) {
  275. case 'indices':
  276. // 结果: ids[0]=1&ids[1]=2&ids[2]=3
  277. for (let i = 0; i < value.length; i++) {
  278. _result.push(key + '[' + i + ']=' + value[i])
  279. }
  280. break;
  281. case 'brackets':
  282. // 结果: ids[]=1&ids[]=2&ids[]=3
  283. value.forEach(_value => {
  284. _result.push(key + '[]=' + _value)
  285. })
  286. break;
  287. case 'repeat':
  288. // 结果: ids=1&ids=2&ids=3
  289. value.forEach(_value => {
  290. _result.push(key + '=' + _value)
  291. })
  292. break;
  293. case 'comma':
  294. // 结果: ids=1,2,3
  295. let commaStr = "";
  296. value.forEach(_value => {
  297. commaStr += (commaStr ? "," : "") + _value;
  298. })
  299. _result.push(key + '=' + commaStr)
  300. break;
  301. default:
  302. value.forEach(_value => {
  303. _result.push(key + '[]=' + _value)
  304. })
  305. }
  306. } else {
  307. _result.push(key + '=' + value)
  308. }
  309. }
  310. return _result.length ? prefix + _result.join('&') : ''
  311. }
  312. //取轮播图
  313. export const getIndexBanner = (data, callback) => get('api/content/banner_function/getIndexBanner' + queryParams(
  314. data,
  315. true), callback);
  316. // export const getWuyuanUser = (data, callback) => post('getWuyuanUser', data, callback, 'wuyuan/user');
  317. // 学院底部课程
  318. export const getHomeData = (data, callback) => post('getHomeData', data, callback, '',
  319. 'addons/yuneducation/page/getHomeData');
  320. // 课程界面数据
  321. export const getCourseList = (data, callback) => post('getCourseList', data, callback, '',
  322. 'addons/yuneducation/course/getCourseList');
  323. // 课程详情
  324. export const getCourseDetail = (data, callback) => post('getCourseDetail', data, callback, '',
  325. 'addons/yuneducation/course/getCourseDetail');
  326. // 获取课程考核
  327. export const getCourseExamine = (data, callback) => post('getCourseExamine', data, callback, '',
  328. 'addons/yuneducation/course/getCourseExamine');
  329. // 课程开始学习
  330. export const startLearn = (data, callback) => post('startLearn', data, callback, '',
  331. 'addons/yuneducation/course/startLearn');
  332. // 试播
  333. export const getContentDetail = (data, callback) => post('getContentDetail', data, callback, '',
  334. 'addons/yuneducation/course/getContentDetail');
  335. // 加入购物车
  336. export const addCart = (data, callback) => post('addCart', data, callback, '',
  337. 'addons/yuneducation/order/addCart');
  338. // 预览订单
  339. export const previewOrder = (data, callback) => post('previewOrder', data, callback, '',
  340. 'addons/yuneducation/order/previewOrder');
  341. // 提交订单
  342. export const submitOrder = (data, callback) => post('submitOrder', data, callback, '',
  343. 'addons/yuneducation/order/submitOrder');
  344. // 移除购物车
  345. export const removeCart = (data, callback) => post('removeCart', data, callback, '',
  346. 'addons/yuneducation/order/removeCart');
  347. // 讲师详情
  348. export const getTeacherDetail = (data, callback) => post('getTeacherDetail', data, callback, '',
  349. 'addons/yuneducation/teacher/getTeacherDetail');
  350. // 培训分类列表
  351. export const getCategoryList = (data, callback) => post('getCategoryList', data, callback, '',
  352. 'addons/yuneducation/course/getCategoryList');
  353. // 获取培训列表
  354. export const getScheduleList = (data, callback) => post('getScheduleList', data, callback, '',
  355. 'addons/yuneducation/schedule/getScheduleList');
  356. // 获取培训详情
  357. export const getScheduleDetail = (data, callback) => post('getScheduleDetail', data, callback, '',
  358. 'addons/yuneducation/schedule/getScheduleDetail');
  359. // 培训开始学习
  360. export const scheduleStartLearn = (data, callback) => post('startLearn', data, callback, '',
  361. 'addons/yuneducation/schedule/startLearn');
  362. // 获取我的课程列表
  363. export const getMineCourseList = (data, callback) => post('getMineCourseList', data, callback, '',
  364. 'addons/yuneducation/course/getMineCourseList');
  365. // 城市主体列表
  366. export const getCity = (data, callback) => get('api/content/main_body/getCity' + queryParams(data, true),
  367. callback);
  368. //获取主体用户信息
  369. export const getMainBodyUser = (data, callback) => post('getMainBodyUser', data, callback,
  370. 'content/main_body_user');
  371. //修改主体用户信息
  372. export const editMainBodyUser = (data, callback) => post('editMainBodyUser', data, callback,
  373. 'content/main_body_user');
  374. // 首页功能集
  375. export const getIndexFunction = (data, callback) => get('api/content/banner_function/getIndexFunction' + queryParams(
  376. data,
  377. true), callback);
  378. // 获取带标志的内容列表
  379. export const getFlagList = (data, callback) => post('getFlagList', data, callback,
  380. 'content/content');
  381. // 模型的主体栏目列表
  382. export const getColumnList = (data, callback) => post('getColumnList', data, callback,
  383. 'content/main_body_column');
  384. // 模型内容列表
  385. export const getContentList = (data, callback) => post('getContentList', data, callback,
  386. 'content/content');
  387. // 模型主体栏目内容列表
  388. export const getMainBodyColumnContentList = (data, callback) => post('getMainBodyColumnContentList', data, callback,
  389. 'content/content');
  390. // 内容详情
  391. export const getContentDetails = (data, callback) => post('getContentDetail', data, callback,
  392. 'content/content');
  393. //史馆列表
  394. export const getHistoryMuseum = (data, callback) => post('getHistoryMuseum', data, callback,
  395. 'content/main_body');
  396. //史馆详情
  397. export const getDetail = (data, callback) => post('getDetail', data, callback,
  398. 'content/main_body');
  399. //史馆列表分类列表
  400. export const getCategoryLists = (data, callback) => post('getCategoryList', data, callback,
  401. 'content/category');
  402. //点赞
  403. export const like = (data, callback) => post('like', data, callback,
  404. 'content/main_body_user');
  405. //取消点赞
  406. export const unLike = (data, callback) => post('unLike', data, callback,
  407. 'content/main_body_user');
  408. //点赞内容列表
  409. export const getUserLike = (data, callback) => post('getUserLike', data, callback,
  410. 'content/main_body_user');
  411. //收藏
  412. export const collect = (data, callback) => post('collect', data, callback,
  413. 'content/main_body_user');
  414. //取消收藏
  415. export const uncollect = (data, callback) => post('uncollect', data, callback,
  416. 'content/main_body_user');
  417. //收藏内容列表
  418. export const getUserCollect = (data, callback) => post('getUserCollect', data, callback,
  419. 'content/main_body_user');
  420. //投稿
  421. export const contribute = (data, callback) => post('contribute', data, callback,
  422. 'content/content');
  423. // content