1.項目目錄javascript
2.代碼java
app/src/service/api.jsgit
import Taro from '@tarojs/taro' import { HTTP_STATUS } from '../const/status' import { base } from './config' import { logError } from '../utils' const token = '' export default { baseOptions(params, method = 'GET') { let { url, data } = params // let token = getApp().globalData.token // if (!token) login() console.log('params', params) let contentType = 'application/x-www-form-urlencoded' contentType = params.contentType || contentType const option = { isShowLoading: false, loadingText: '正在加載', url: base + url, data: data, method: method, header: { 'content-type': contentType, 'token': token }, success(res) { if (res.statusCode === HTTP_STATUS.NOT_FOUND) { return logError('api', '請求資源不存在') } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) { return logError('api', '服務端出現了問題') } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) { return logError('api', '沒有權限訪問') } else if (res.statusCode === HTTP_STATUS.SUCCESS) { return res.data } }, error(e) { logError('api', '請求接口出現問題', e) } } return Taro.request(option) }, get(url, data = '') { let option = { url, data } return this.baseOptions(option) }, post: function (url, data, contentType) { let params = { url, data, contentType } return this.baseOptions(params, 'POST') } }
app/src/service/config.jsgithub
// https://api.github.com/repos/NervJS/taro/issues?per_page=1&page=2 export const base = "https://api.github.com/repos/";
app/src/const/status.jsapi
export const HTTP_STATUS = { SUCCESS: 200, CLIENT_ERROR: 400, AUTHENTICATE: 401, FORBIDDEN: 403, NOT_FOUND: 404, SERVER_ERROR: 500, BAD_GATEWAY: 502, SERVICE_UNAVAILABLE: 503, GATEWAY_TIMEOUT: 504 } // promise status export const SUCCESS = { success:'success'} export const FAIL = { fail:'fail'} export const COMPLETE = { complete:'complete'} export const PROMISE_STATUS = { success: 'success', fail: 'fail', complete: 'complete' } export const RESULT_STATUS = { SUCCESS:0, SIGNATURE_FAILED: 1000 // 簽名失敗 }
app/src/utils/index.jspromise
export const promisify = (func, ctx) => { // 返回一個新的function return function () { // 初始化this做用域 var ctx = ctx || this; // 新方法返回的promise return new Promise((resolve, reject) => { // 調用原來的非promise方法func,綁定做用域,傳參,以及callback(callback爲func的最後一個參數) func.call(ctx, ...arguments, function () { // 將回調函數中的的第一個參數error單獨取出 var args = Array.prototype.map.call(arguments, item => item); var err = args.shift(); // 判斷是否有error if (err) { reject(err) } else { // 沒有error則將後續參數resolve出來 args = args.length > 1 ? args : args[0]; resolve(args); } }); }) }; }; export const promiseImage = (url) => { return new Promise(function (resolve, reject) { resolve(url) }) } export const isChinese = (str) => { if (escape(str).indexOf("%u") < 0) return false return true } export const handleName = (str) => { let res = emoj2str(str) if (isChinese(res)) { res = res.length > 4 ? res.slice(0, 4) + '...' : res } else { res = res.length > 7 ? res.slice(0, 7) + '...' : res } return res } export const emoj2str = (str) => { return unescape(escape(str).replace(/\%uD.{3}/g, '')) } /*獲取當前頁url*/ export const getCurrentPageUrl = () => { let pages = getCurrentPages() let currentPage = pages[pages.length - 1] let url = currentPage.route return url } export const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } export const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } export const logError = (name, action, info) => { if (!info) { info = 'empty' } try { let deviceInfo = wx.getSystemInfoSync() var device = JSON.stringify(deviceInfo) } catch (e) { console.error('not support getSystemInfoSync api', err.message) } let time = formatTime(new Date()) console.error(time, name, action, info, device) // if (typeof action !== 'object') { // fundebug.notify(name, action, info) // } // fundebug.notifyError(info, { name, action, device, time }) if (typeof info === 'object') { info = JSON.stringify(info) } }
.app