api.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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', 'addCart',
  9. 'previewOrder', 'getWuyuanUser', 'archives_post', 'get_channel_fields', 'get_channel', 'lists', 'details',
  10. 'logOffVolunteer', 'getCrProperty', 'joinTeam', 'getTeamMember', 'getPatrolTask', 'claimCr',
  11. 'getPatrolLog', 'claimCrList', 'claimCrDetails', 'editMainBodyUser', , 'contribute',
  12. 'getPatrolTaskDetails', 'getContentDetail', 'examineVolunteer', 'userManageRegionCrAuth', 'applyVolunteer',
  13. 'getPhoneNumber', 'editApplyVolunteer', 'mobileBindVolunteer', 'getTeamDetails', 'examineTask', 'removeMemberm',
  14. 'getScoreLog', 'rankingList', 'getMainBodyColumnContentList', 'regionData', 'cityData', 'checkIn',
  15. 'getMainBodyUser', 'getUserContribute', 'getUserHonor', 'submitTask', 'exportPatrolRecord'
  16. ];
  17. const post = (method, data, callback, type, orgurl) => {
  18. let userToken = '';
  19. let auth = '';
  20. // 判断token是否存在
  21. console.log("method: ", method);
  22. if (methodsToken.indexOf(method) >= 0) {
  23. // 获取用户token
  24. let auth = db.get("auth");
  25. // console.log(auth);
  26. let nowdate = (new Date()) / 1000; //当前时间戳
  27. //新增用户判断是否登录逻辑begin
  28. common.isLogin();
  29. //新增用户判断是否登录逻辑end
  30. if (!auth || auth.createtime + auth.expires_in < nowdate) {
  31. common.toLogin();
  32. return false;
  33. } else {
  34. userToken = auth.token;
  35. }
  36. }
  37. if (type) {
  38. method = type + '/' + method
  39. } else {
  40. method = '/' + method
  41. }
  42. let realurl = baseApiUrl + method;
  43. if (orgurl) {
  44. realurl = baseUrl + orgurl;
  45. }
  46. uni.showLoading({
  47. title: '',
  48. icon: 'loading'
  49. });
  50. uni.request({
  51. url: realurl,
  52. data: data,
  53. header: {
  54. 'Accept': 'application/json',
  55. 'Content-Type': 'application/x-www-form-urlencoded',
  56. 'token': userToken,
  57. '__token__': userToken
  58. },
  59. method: 'POST',
  60. success: (response) => {
  61. uni.hideLoading();
  62. const result = response.data
  63. if (result.msg == 'Please login' || result.msg == '请登陆') {
  64. db.del("user");
  65. db.del("auth");
  66. console.log('未登陆')
  67. uni.showToast({
  68. title: result.msg,
  69. icon: 'none',
  70. duration: 2000,
  71. complete: function() {
  72. uni.reLaunch({
  73. url: '/pages/index/index',
  74. })
  75. }
  76. });
  77. }
  78. callback(result);
  79. },
  80. fail: (error) => {
  81. uni.hideLoading();
  82. if (error && error.response) {
  83. showError(error.response);
  84. }
  85. },
  86. });
  87. }
  88. // 上传图片
  89. export const uploadImage = (method, data = {}, callback, num = 9, type) => {
  90. if (type) {
  91. method = type + '/' + method
  92. } else {
  93. method = method
  94. }
  95. let userToken = '';
  96. let auth = db.get("auth");
  97. userToken = auth.token;
  98. uni.chooseImage({
  99. count: num,
  100. success: (res) => {
  101. uni.showLoading({
  102. title: '上传中...'
  103. });
  104. let tempFilePaths = res.tempFilePaths
  105. for (var i = 0; i < tempFilePaths.length; i++) {
  106. data.file = tempFilePaths[i]
  107. uni.uploadFile({
  108. url: baseApiUrl + method,
  109. filePath: tempFilePaths[i],
  110. fileType: 'image',
  111. name: 'file',
  112. headers: {
  113. 'Accept': 'application/json',
  114. 'Content-Type': 'multipart/form-data',
  115. 'token': userToken
  116. },
  117. formData: data,
  118. success: (uploadFileRes) => {
  119. callback(JSON.parse(uploadFileRes.data))
  120. },
  121. fail: (error) => {
  122. if (error && error.response) {
  123. common.showError(error.response);
  124. }
  125. },
  126. complete: () => {
  127. setTimeout(function() {
  128. uni.hideLoading();
  129. }, 250);
  130. },
  131. });
  132. }
  133. }
  134. });
  135. }
  136. const get = (url, callback) => {
  137. uni.showLoading({
  138. title: '加载中'
  139. });
  140. let realurl = baseUrl + url;
  141. if (url.indexOf('http:') >= 0 || url.indexOf('https:') >= 0) {
  142. let realurl = url;
  143. }
  144. uni.request({
  145. url: realurl,
  146. header: {
  147. 'Accept': 'application/json',
  148. 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
  149. },
  150. method: 'GET',
  151. success: (response) => {
  152. callback(response.data);
  153. },
  154. fail: (error) => {
  155. if (error && error.response) {
  156. showError(error.response);
  157. }
  158. },
  159. complete: () => {
  160. setTimeout(function() {
  161. uni.hideLoading();
  162. }, 250);
  163. }
  164. });
  165. }
  166. const showError = error => {
  167. let errorMsg = ''
  168. switch (error.status) {
  169. case 400:
  170. errorMsg = '请求参数错误'
  171. break
  172. case 401:
  173. errorMsg = '未授权,请登录'
  174. break
  175. case 403:
  176. errorMsg = '跨域拒绝访问'
  177. break
  178. case 404:
  179. errorMsg = `请求地址出错: ${error.config.url}`
  180. break
  181. case 408:
  182. errorMsg = '请求超时'
  183. break
  184. case 500:
  185. errorMsg = '服务器内部错误'
  186. break
  187. case 501:
  188. errorMsg = '服务未实现'
  189. break
  190. case 502:
  191. errorMsg = '网关错误'
  192. break
  193. case 503:
  194. errorMsg = '服务不可用'
  195. break
  196. case 504:
  197. errorMsg = '网关超时'
  198. break
  199. case 505:
  200. errorMsg = 'HTTP版本不受支持'
  201. break
  202. default:
  203. errorMsg = error.msg
  204. break
  205. }
  206. uni.showToast({
  207. title: errorMsg,
  208. icon: 'none',
  209. duration: 2000
  210. });
  211. }
  212. const syncget = (url, data) => {
  213. return new Promise(function(resolve, reject) {
  214. get(url + queryParams(data, true), (result) => {
  215. try {
  216. resolve(result);
  217. } catch (e) {
  218. reject(e);
  219. }
  220. })
  221. })
  222. }
  223. const syncpost = (f, m, d) => {
  224. return new Promise(function(resolve, reject) {
  225. post(f, d, (result) => {
  226. try {
  227. resolve(result);
  228. } catch (e) {
  229. reject(e);
  230. }
  231. }, m, m);
  232. })
  233. }
  234. // 登录
  235. export const third = (data, callback) => post('third', data, callback, 'content/main_body_user');
  236. // 用户绑定手机
  237. export const bindphone = (data, callback) => post('bind', data, callback, 'discover/User');
  238. // 修改用户信息
  239. export const profile = (data, callback) => post('profile', data, callback, 'discover/User');
  240. // 发送验证码
  241. export const sendSmsVerify = (data, callback) => post('sendSmsVerify', data, callback, 'discover/User');
  242. // 刷新用户
  243. export const refreshUser = (data, callback) => post('refreshUser', data, callback, 'content/main_body_user');
  244. // 注册
  245. export const register = (data, callback) => post('register', data, callback, 'discover/User');
  246. // 登录
  247. export const login = (data, callback) => post('login', data, callback, 'discover/User');
  248. // 退出登录
  249. export const logout = (data, callback) => post('logout', data, callback, 'discover/User');
  250. // 上传头像
  251. export const upload = (data, callback) => post('upload', data, callback, 'discover/Ajax');
  252. // 绑定手机
  253. export const changeMobile = (data, callback) => post('changeMobile', data, callback, 'discover/User');
  254. //获取手机号前先登录
  255. export const wxLogin = (data, callback) => post('wxLogin', data, callback, 'user');
  256. //获取手机号
  257. // export const getMobile = (data, callback) => post('getPhoneNumber', data, callback, 'user');
  258. //取CMS配置
  259. export const getConfig = async (params = {}) => await syncget('addons/cms/api.common/init', params);
  260. function queryParams(data = {}, isPrefix = true, arrayFormat = 'brackets') {
  261. let prefix = isPrefix ? '?' : ''
  262. let _result = []
  263. if (['indices', 'brackets', 'repeat', 'comma'].indexOf(arrayFormat) == -1) arrayFormat = 'brackets';
  264. for (let key in data) {
  265. let value = data[key]
  266. // 去掉为空的参数
  267. if (['', undefined, null].indexOf(value) >= 0) {
  268. continue;
  269. }
  270. if (value.length > 0) {
  271. value = value.replace(/%/g, "%25");
  272. value = value.replace(/\&/g, "%26");
  273. value = value.replace(/\+/g, "%2B");
  274. }
  275. // 如果值为数组,另行处理
  276. if (value.constructor === Array) {
  277. // e.g. {ids: [1, 2, 3]}
  278. switch (arrayFormat) {
  279. case 'indices':
  280. // 结果: ids[0]=1&ids[1]=2&ids[2]=3
  281. for (let i = 0; i < value.length; i++) {
  282. _result.push(key + '[' + i + ']=' + value[i])
  283. }
  284. break;
  285. case 'brackets':
  286. // 结果: ids[]=1&ids[]=2&ids[]=3
  287. value.forEach(_value => {
  288. _result.push(key + '[]=' + _value)
  289. })
  290. break;
  291. case 'repeat':
  292. // 结果: ids=1&ids=2&ids=3
  293. value.forEach(_value => {
  294. _result.push(key + '=' + _value)
  295. })
  296. break;
  297. case 'comma':
  298. // 结果: ids=1,2,3
  299. let commaStr = "";
  300. value.forEach(_value => {
  301. commaStr += (commaStr ? "," : "") + _value;
  302. })
  303. _result.push(key + '=' + commaStr)
  304. break;
  305. default:
  306. value.forEach(_value => {
  307. _result.push(key + '[]=' + _value)
  308. })
  309. }
  310. } else {
  311. _result.push(key + '=' + value)
  312. }
  313. }
  314. return _result.length ? prefix + _result.join('&') : ''
  315. }
  316. // 模型的主体栏目列表
  317. export const getColumnList = (data, callback) => post('getColumnList', data, callback,
  318. 'content/main_body_column');
  319. // 内容详情
  320. export const getContentDetail = (data, callback) => post('getContentDetail', data, callback,
  321. 'content/content');
  322. // 模型内容列表
  323. export const getContentList = (data, callback) => post('getContentList', data, callback,
  324. 'content/content');
  325. // 模型主体栏目的内容列表
  326. export const getMainBodyColumnContentList = (data, callback) => post('getMainBodyColumnContentList', data, callback,
  327. 'content/content');
  328. // 培训分类列表
  329. export const getCategoryList = (data, callback) => post('getCategoryList', data, callback, '',
  330. 'addons/yuneducation/course/getCategoryList');
  331. // 学院底部课程
  332. export const getHomeData = (data, callback) => post('getHomeData', data, callback, '',
  333. 'addons/yuneducation/page/getHomeData');
  334. // 课程界面数据
  335. export const getCourseList = (data, callback) => post('getCourseList', data, callback, '',
  336. 'addons/yuneducation/course/getCourseList');
  337. // 课程详情
  338. export const getCourseDetail = (data, callback) => post('getCourseDetail', data, callback, '',
  339. 'addons/yuneducation/course/getCourseDetail');
  340. // 获取课程考核
  341. export const getCourseExamine = (data, callback) => post('getCourseExamine', data, callback, '',
  342. 'addons/yuneducation/course/getCourseExamine');
  343. // 课程开始学习
  344. export const startLearn = (data, callback) => post('startLearn', data, callback, '',
  345. 'addons/yuneducation/course/startLearn');
  346. // 加入购物车
  347. export const addCart = (data, callback) => post('addCart', data, callback, '',
  348. 'addons/yuneducation/order/addCart');
  349. // 预览订单
  350. export const previewOrder = (data, callback) => post('previewOrder', data, callback, '',
  351. 'addons/yuneducation/order/previewOrder');
  352. // 提交订单
  353. export const submitOrder = (data, callback) => post('submitOrder', data, callback, '',
  354. 'addons/yuneducation/order/submitOrder');
  355. // 移除购物车
  356. export const removeCart = (data, callback) => post('removeCart', data, callback, '',
  357. 'addons/yuneducation/order/removeCart');
  358. // 讲师详情
  359. export const getTeacherDetail = (data, callback) => post('getTeacherDetail', data, callback, '',
  360. 'addons/yuneducation/teacher/getTeacherDetail');
  361. // 获取培训列表
  362. export const getScheduleList = (data, callback) => post('getScheduleList', data, callback, '',
  363. 'addons/yuneducation/schedule/getScheduleList');
  364. // 获取培训详情
  365. export const getScheduleDetail = (data, callback) => post('getScheduleDetail', data, callback, '',
  366. 'addons/yuneducation/schedule/getScheduleDetail');
  367. // 培训开始学习
  368. export const scheduleStartLearn = (data, callback) => post('startLearn', data, callback, '',
  369. 'addons/yuneducation/schedule/startLearn');
  370. // 获取我的课程列表
  371. export const getMineCourseList = (data, callback) => post('getMineCourseList', data, callback, '',
  372. 'addons/yuneducation/course/getMineCourseList');
  373. // 试播
  374. // export const getContentDetail = (data, callback) => post('getContentDetail', data, callback, '',
  375. // 'addons/yuneducation/course/getContentDetail');
  376. //获取栏目
  377. export const getChannel = async (params = {}) => await syncpost('get_channel', '/addons/cms/api.archives/get_channel',
  378. params);
  379. //我发的文章
  380. export const myArchives = async (params = {}) => await syncpost('my', '/addons/cms/api.archives/my', params);
  381. // 文物管理相关
  382. //场馆详情页获取管理员权限
  383. export const adminCrAuth = (data, callback) => post('adminCrAuth', data, callback, 'wuyuan/admin');
  384. //取文巡查记录
  385. export const getPatrolLog = (data, callback) => post('getPatrolLog', data, callback, 'wuyuan/volunteer');
  386. //取文物详情
  387. export const getCulturalDetails = (data, callback) => get('api/wuyuan/cultural_relic/details' + queryParams(data, true),
  388. callback);
  389. // 文物管理相关end
  390. //取轮播图
  391. export const getBannerList = (data, callback) => get('/api/wuyuan/swiper_icon/lists' + queryParams(data, true),
  392. callback);
  393. // //人物列表 传 type参数类型:inheritor=非遗传承人,history=历史人物,folklore=民俗学者,specialist=文史专家
  394. // export const getPeopleList = (data, callback) => get('api/wuyuan/biographies/lists' + queryParams(data, true), callback);
  395. // //人物详情
  396. // export const getPeopleDetails = (data, callback) => get('api/wuyuan/biographies/details' + queryParams(data, true), callback);
  397. // 获取用户信息
  398. export const getMainBodyUser = (data, callback) => post('getMainBodyUser', data, callback,
  399. 'content/main_body_user/getMainBodyUser');
  400. //修改头像
  401. export const editMainBodyUser = (data, callback) => post('editMainBodyUser', data, callback,
  402. 'content/main_body_user');
  403. //取任务列表
  404. export const getTaskList = (data, callback) => post('lists', data, callback, 'wuyuan/task');
  405. //取法律文章列表
  406. export const getArchives = async (params = {}) => await syncget('https://huli-app.wenlvti.net/addons/cms/api', params);
  407. // 积分信息
  408. export const getWuyuanUser = (data, callback) => post('getWuyuanUser', data, callback, 'wuyuan/user');
  409. //取文物分类 参数传type类型
  410. export const getCulturalRelicCategory = (data, callback) => get('api/content/category/getCategoryList' + queryParams(
  411. data,
  412. true), callback);
  413. //取级联分类
  414. export const getCategoryCascadeList = (data, callback) => get('api/content/category/getCategoryCascadeList' +
  415. queryParams(
  416. data, true), callback);
  417. // 投稿
  418. export const contribute = (data, callback) => post('contribute', data, callback, '',
  419. 'api/content/style/contribute');
  420. //取文物场景列表
  421. export const getCulturalRelic = (data, callback) => get('api/content/cultural_relic/lists' + queryParams(data, true),
  422. callback);
  423. // 志愿报名
  424. export const applyVolunteer = (data, callback) => post('applyVolunteer', data, callback, '',
  425. 'api/volunteer/volunteer/applyVolunteer');
  426. // 志愿者列表
  427. export const lists = (data, callback) => post('lists', data, callback, '', 'api/volunteer/volunteer/lists');
  428. // 志愿者详情
  429. export const details = (data, callback) => post('details', data, callback, '', 'api/volunteer/volunteer/details');
  430. // 注销志愿者
  431. export const logOffVolunteer = (data, callback) => post('logOffVolunteer', data, callback, '',
  432. 'api/volunteer/volunteer/logOffVolunteer');
  433. // 志愿者巡查文物资产列表
  434. export const getCrProperty = (data, callback) => post('getCrProperty', data, callback, '',
  435. 'api/volunteer/patrol_property/getCrProperty');
  436. // 加入志愿者团队
  437. export const joinTeam = (data, callback) => post('joinTeam', data, callback, '',
  438. 'api/volunteer/team_member/joinTeam');
  439. // 获取团队成员列表
  440. export const getTeamMember = (data, callback) => post('getTeamMember', data, callback, '',
  441. 'api/volunteer/team/getTeamMember');
  442. // 移除成员
  443. export const removeMemberm = (data, callback) => post('removeMemberm', data, callback, '',
  444. 'api/volunteer/team_member/removeMember');
  445. // 获取志愿者任务列表
  446. export const getPatrolTask = (data, callback) => post('getPatrolTask', data, callback, '',
  447. 'api/volunteer/patrol_task/getPatrolTask');
  448. // 获取任务列详情
  449. export const getPatrolTaskDetails = (data, callback) => post('getPatrolTaskDetails', data, callback, '',
  450. 'api/volunteer/patrol_task/getPatrolTaskDetails');
  451. // 提交巡查任务
  452. export const submitTask = (data, callback) => post('submitTask', data, callback, '',
  453. 'api/volunteer/patrol_task/submitTask');
  454. // 计算志愿者与文物点的距离
  455. export const distanceCheck = (data, callback) => post('distanceCheck', data, callback, '',
  456. 'api/volunteer/patrol_task/distanceCheck');
  457. // 认领文物
  458. export const claimCr = (data, callback) => post('claimCr', data, callback, '',
  459. 'api/volunteer/claim_cr/claimCr');
  460. // 认领文物列表
  461. export const claimCrList = (data, callback) => post('claimCrList', data, callback, '',
  462. 'api/volunteer/claim_cr/claimCrList');
  463. // 认领文物详情暂未调用
  464. export const claimCrDetails = (data, callback) => post('claimCrDetails', data, callback, '',
  465. 'api/volunteer/claim_cr/claimCrDetails');
  466. // 最新公告
  467. export const getNotice = (data, callback) => post('getNotice', data, callback, '',
  468. 'api/system/notice/getNotice');
  469. // 区域
  470. export const getCategoryOnlyChildList = (data, callback) => post('getCategoryOnlyChildList', data, callback, '',
  471. 'api/content/category/getCategoryOnlyChildList');
  472. // 志愿者审核
  473. export const examineVolunteer = (data, callback) => post('examineVolunteer', data, callback, '',
  474. 'api/volunteer/volunteer/examineVolunteer');
  475. // 用户区域管理文物权限
  476. export const userManageRegionCrAuth = (data, callback) => post('userManageRegionCrAuth', data, callback, '',
  477. 'api/auth/user_manage_cr/userManageRegionCrAuth');
  478. // 修改驳回志愿者信息
  479. export const editApplyVolunteer = (data, callback) => post('editApplyVolunteer', data, callback, '',
  480. 'api/volunteer/volunteer/editApplyVolunteer');
  481. // 老志愿者手机号绑定志愿者
  482. export const getPhoneNumber = (data, callback) => post('getPhoneNumber', data, callback, '',
  483. 'api/volunteer/volunteer/getPhoneNumber');
  484. // 手动绑定志愿者手机号
  485. export const mobileBindVolunteer = (data, callback) => post('mobileBindVolunteer', data, callback, '',
  486. 'api/volunteer/volunteer/mobileBindVolunteer');
  487. // 提交任务审核
  488. export const examineTask = (data, callback) => post('examineTask', data, callback, '',
  489. 'api/volunteer/patrol_task/examineTask');
  490. // 获取用户积分列表
  491. export const getScoreLog = (data, callback) => post('getScoreLog', data, callback, '',
  492. 'api/content/main_body_user/getScoreLog');
  493. // 志愿者排行
  494. export const rankingList = (data, callback) => post('rankingList', data, callback, '',
  495. 'api/volunteer/volunteer/rankingList');
  496. // 活动列表
  497. export const activityLists = (data, callback) => get('api/activity/activity/lists' + queryParams(data, true),
  498. callback);
  499. // 活动详情
  500. export const activityDetails = (data, callback) => get('api/activity/activity/details' + queryParams(data, true),
  501. callback);
  502. // 志愿者排行详情
  503. export const rankingDetails = (data, callback) => get('api/volunteer/volunteer/rankingDetails' + queryParams(data,
  504. true),
  505. callback);
  506. // 区域数据统计
  507. export const regionData = (data, callback) => post('regionData', data, callback, '',
  508. 'api/volunteer/statistics/regionData');
  509. // 市级数据统计
  510. export const cityData = (data, callback) => post('cityData', data, callback, '',
  511. 'api/volunteer/statistics/cityData');
  512. // 新闻公告
  513. export const getNewsNotice = (data, callback) => post('getNewsNotice', data, callback, '',
  514. 'api/system/notice/getNewsNotice');
  515. // 打卡
  516. export const checkIn = (data, callback) => post('checkIn', data, callback, '',
  517. 'api/content/main_body_user/checkIn');
  518. // 活动报名
  519. export const activitySignup = (data, callback) => post('activitySignup', data, callback, '',
  520. 'api/activity/activity/activitySignup');
  521. // 我的投稿
  522. export const getUserContribute = (data, callback) => post('getUserContribute', data, callback, '',
  523. 'api/content/main_body_user/getUserContribute');
  524. // 我的荣誉
  525. export const getUserHonor = (data, callback) => post('getUserHonor', data, callback, '',
  526. 'api/content/main_body_user/getUserHonor');
  527. // 轮播
  528. export const getIndexBanner = (data, callback) => post('getIndexBanner', data, callback, '',
  529. 'api/content/banner_function/getIndexBanner');
  530. // 导出
  531. export const exportPatrolRecord = (data, callback) => post('exportPatrolRecord', data, callback, '',
  532. 'api/volunteer/patrol_task/exportPatrolRecord');