| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- var requestUtils = require('./request.js')
- const apiHost = requestUtils.apiHost || 'https://xcx-api.fohowyc.com/'
- const appApiHost = requestUtils.apiHostX || 'https://app-api.fohowyc.com/'
- function getLanguageCode () {
- return 'zh'
- }
- function getToken () {
- return wx.getStorageSync('lbt_token_key') || ''
- }
- function getHeaders (extra) {
- var token = getToken()
- var headers = {
- 'Accept-Language': getLanguageCode(),
- terminal: 'mini-program'
- }
- if (token) {
- headers.Authorization = 'Bearer ' + token
- }
- if (extra) {
- for (var key in extra) {
- headers[key] = extra[key]
- }
- }
- return headers
- }
- function normalizeResponse (res) {
- return res && res.data ? res.data : res
- }
- function getSuccessCode (payload) {
- if (payload && payload.code !== undefined) {
- return payload.code
- }
- if (payload && payload.Code !== undefined) {
- return payload.Code
- }
- return undefined
- }
- function getResponseData (payload) {
- if (!payload) {
- return payload
- }
- if (payload.data !== undefined) {
- return payload.data
- }
- if (payload.Data !== undefined) {
- return payload.Data
- }
- return payload
- }
- function getErrorMessage (payload) {
- if (!payload) {
- return '请求失败'
- }
- return payload.msg || payload.Msg || payload.err_msg || payload.message || '请求失败'
- }
- function request (options) {
- var host = options.host === 'old' ? apiHost : appApiHost
- var method = options.method || 'GET'
- return new Promise(function (resolve, reject) {
- wx.request({
- url: host + options.path,
- method: method,
- data: options.data || {},
- header: getHeaders(options.header || {
- 'content-type': method === 'GET' ? 'application/json' : 'application/json'
- }),
- success: function (res) {
- var payload = normalizeResponse(res)
- var code = getSuccessCode(payload)
- if (res.statusCode >= 200 && res.statusCode < 300 && (code === undefined || code === 0)) {
- resolve(getResponseData(payload))
- return
- }
- reject({
- statusCode: res.statusCode,
- payload: payload,
- message: getErrorMessage(payload)
- })
- },
- fail: function (err) {
- reject({
- message: '网络异常,请稍后重试',
- error: err
- })
- }
- })
- })
- }
- function uploadObservationImage (filePath, kind) {
- return new Promise(function (resolve, reject) {
- wx.uploadFile({
- url: appApiHost + 'tcm/observation-images',
- filePath: filePath,
- name: 'file',
- formData: {
- kind: kind
- },
- header: getHeaders(),
- success: function (res) {
- var payload = {}
- try {
- payload = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
- } catch (e) {
- reject({
- message: '上传返回解析失败',
- error: e
- })
- return
- }
- if (res.statusCode >= 200 && res.statusCode < 300 && getSuccessCode(payload) === 0) {
- resolve(getResponseData(payload))
- return
- }
- reject({
- statusCode: res.statusCode,
- payload: payload,
- message: getErrorMessage(payload)
- })
- },
- fail: function (err) {
- reject({
- message: '上传失败,请稍后重试',
- error: err
- })
- }
- })
- })
- }
- function getCurrentUserInfo () {
- return request({
- host: 'old',
- path: 'v1/user/info'
- })
- }
- function getScaleUsers () {
- return getCurrentUserInfo().then(function (info) {
- var user = info && info.user ? info.user : {}
- var userId = user.user_id || user.id
- if (!userId) {
- return []
- }
- return request({
- path: 'scaleUsers/getScaleUsersList?ID=' + userId + '&page=1&pageSize=999'
- }).then(function (data) {
- if (Array.isArray(data)) {
- return data
- }
- if (data && Array.isArray(data.list)) {
- return data.list
- }
- return []
- })
- })
- }
- function getPhotoTips () {
- return request({
- path: 'tcm/photo-tips'
- })
- }
- function getQuestions () {
- return request({
- path: 'tcm/questions'
- })
- }
- function submitExam (data) {
- return request({
- path: 'tcm/exams',
- method: 'POST',
- data: data
- })
- }
- function getLatestExam (scaleUserId) {
- return request({
- path: 'tcm/exams/latest?scaleUserId=' + scaleUserId
- })
- }
- function getExamDetail (examId) {
- return request({
- path: 'tcm/exams/' + examId
- })
- }
- function createScaleUser (data) {
- return request({
- path: 'scaleUsers/createScaleUsers',
- method: 'POST',
- data: data
- })
- }
- function updateScaleUser (data) {
- return request({
- path: 'scaleUsers/updateScaleUsers',
- method: 'PUT',
- data: data
- })
- }
- function setDefaultScaleUser (id) {
- return request({
- path: 'scaleUsers/SetDefaultUser',
- method: 'PUT',
- data: {
- id: id
- }
- })
- }
- function deleteScaleUser (id) {
- return request({
- path: 'scaleUsers/deleteScaleUsers',
- method: 'DELETE',
- data: {
- ID: id
- }
- })
- }
- module.exports = {
- getLanguageCode: getLanguageCode,
- getCurrentUserInfo: getCurrentUserInfo,
- getScaleUsers: getScaleUsers,
- getPhotoTips: getPhotoTips,
- getQuestions: getQuestions,
- uploadObservationImage: uploadObservationImage,
- submitExam: submitExam,
- getLatestExam: getLatestExam,
- getExamDetail: getExamDetail,
- createScaleUser: createScaleUser,
- updateScaleUser: updateScaleUser,
- setDefaultScaleUser: setDefaultScaleUser,
- deleteScaleUser: deleteScaleUser
- }
|