tcm.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. var requestUtils = require('./request.js')
  2. const apiHost = requestUtils.apiHost || 'https://xcx-api.fohowyc.com/'
  3. const appApiHost = requestUtils.apiHostX || 'https://app-api.fohowyc.com/'
  4. function getLanguageCode () {
  5. return 'zh'
  6. }
  7. function getToken () {
  8. return wx.getStorageSync('lbt_token_key') || ''
  9. }
  10. function getHeaders (extra) {
  11. var token = getToken()
  12. var headers = {
  13. 'Accept-Language': getLanguageCode(),
  14. terminal: 'mini-program'
  15. }
  16. if (token) {
  17. headers.Authorization = 'Bearer ' + token
  18. }
  19. if (extra) {
  20. for (var key in extra) {
  21. headers[key] = extra[key]
  22. }
  23. }
  24. return headers
  25. }
  26. function normalizeResponse (res) {
  27. return res && res.data ? res.data : res
  28. }
  29. function getSuccessCode (payload) {
  30. if (payload && payload.code !== undefined) {
  31. return payload.code
  32. }
  33. if (payload && payload.Code !== undefined) {
  34. return payload.Code
  35. }
  36. return undefined
  37. }
  38. function getResponseData (payload) {
  39. if (!payload) {
  40. return payload
  41. }
  42. if (payload.data !== undefined) {
  43. return payload.data
  44. }
  45. if (payload.Data !== undefined) {
  46. return payload.Data
  47. }
  48. return payload
  49. }
  50. function getErrorMessage (payload) {
  51. if (!payload) {
  52. return '请求失败'
  53. }
  54. return payload.msg || payload.Msg || payload.err_msg || payload.message || '请求失败'
  55. }
  56. function request (options) {
  57. var host = options.host === 'old' ? apiHost : appApiHost
  58. var method = options.method || 'GET'
  59. return new Promise(function (resolve, reject) {
  60. wx.request({
  61. url: host + options.path,
  62. method: method,
  63. data: options.data || {},
  64. header: getHeaders(options.header || {
  65. 'content-type': method === 'GET' ? 'application/json' : 'application/json'
  66. }),
  67. success: function (res) {
  68. var payload = normalizeResponse(res)
  69. var code = getSuccessCode(payload)
  70. if (res.statusCode >= 200 && res.statusCode < 300 && (code === undefined || code === 0)) {
  71. resolve(getResponseData(payload))
  72. return
  73. }
  74. reject({
  75. statusCode: res.statusCode,
  76. payload: payload,
  77. message: getErrorMessage(payload)
  78. })
  79. },
  80. fail: function (err) {
  81. reject({
  82. message: '网络异常,请稍后重试',
  83. error: err
  84. })
  85. }
  86. })
  87. })
  88. }
  89. function uploadObservationImage (filePath, kind) {
  90. return new Promise(function (resolve, reject) {
  91. wx.uploadFile({
  92. url: appApiHost + 'tcm/observation-images',
  93. filePath: filePath,
  94. name: 'file',
  95. formData: {
  96. kind: kind
  97. },
  98. header: getHeaders(),
  99. success: function (res) {
  100. var payload = {}
  101. try {
  102. payload = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
  103. } catch (e) {
  104. reject({
  105. message: '上传返回解析失败',
  106. error: e
  107. })
  108. return
  109. }
  110. if (res.statusCode >= 200 && res.statusCode < 300 && getSuccessCode(payload) === 0) {
  111. resolve(getResponseData(payload))
  112. return
  113. }
  114. reject({
  115. statusCode: res.statusCode,
  116. payload: payload,
  117. message: getErrorMessage(payload)
  118. })
  119. },
  120. fail: function (err) {
  121. reject({
  122. message: '上传失败,请稍后重试',
  123. error: err
  124. })
  125. }
  126. })
  127. })
  128. }
  129. function getCurrentUserInfo () {
  130. return request({
  131. host: 'old',
  132. path: 'v1/user/info'
  133. })
  134. }
  135. function getScaleUsers () {
  136. return getCurrentUserInfo().then(function (info) {
  137. var user = info && info.user ? info.user : {}
  138. var userId = user.user_id || user.id
  139. if (!userId) {
  140. return []
  141. }
  142. return request({
  143. path: 'scaleUsers/getScaleUsersList?ID=' + userId + '&page=1&pageSize=999'
  144. }).then(function (data) {
  145. if (Array.isArray(data)) {
  146. return data
  147. }
  148. if (data && Array.isArray(data.list)) {
  149. return data.list
  150. }
  151. return []
  152. })
  153. })
  154. }
  155. function getPhotoTips () {
  156. return request({
  157. path: 'tcm/photo-tips'
  158. })
  159. }
  160. function getQuestions () {
  161. return request({
  162. path: 'tcm/questions'
  163. })
  164. }
  165. function submitExam (data) {
  166. return request({
  167. path: 'tcm/exams',
  168. method: 'POST',
  169. data: data
  170. })
  171. }
  172. function getLatestExam (scaleUserId) {
  173. return request({
  174. path: 'tcm/exams/latest?scaleUserId=' + scaleUserId
  175. })
  176. }
  177. function getExamDetail (examId) {
  178. return request({
  179. path: 'tcm/exams/' + examId
  180. })
  181. }
  182. function createScaleUser (data) {
  183. return request({
  184. path: 'scaleUsers/createScaleUsers',
  185. method: 'POST',
  186. data: data
  187. })
  188. }
  189. function updateScaleUser (data) {
  190. return request({
  191. path: 'scaleUsers/updateScaleUsers',
  192. method: 'PUT',
  193. data: data
  194. })
  195. }
  196. function setDefaultScaleUser (id) {
  197. return request({
  198. path: 'scaleUsers/SetDefaultUser',
  199. method: 'PUT',
  200. data: {
  201. id: id
  202. }
  203. })
  204. }
  205. function deleteScaleUser (id) {
  206. return request({
  207. path: 'scaleUsers/deleteScaleUsers',
  208. method: 'DELETE',
  209. data: {
  210. ID: id
  211. }
  212. })
  213. }
  214. module.exports = {
  215. getLanguageCode: getLanguageCode,
  216. getCurrentUserInfo: getCurrentUserInfo,
  217. getScaleUsers: getScaleUsers,
  218. getPhotoTips: getPhotoTips,
  219. getQuestions: getQuestions,
  220. uploadObservationImage: uploadObservationImage,
  221. submitExam: submitExam,
  222. getLatestExam: getLatestExam,
  223. getExamDetail: getExamDetail,
  224. createScaleUser: createScaleUser,
  225. updateScaleUser: updateScaleUser,
  226. setDefaultScaleUser: setDefaultScaleUser,
  227. deleteScaleUser: deleteScaleUser
  228. }