首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序前端function封装

微信小程序前端function封装

作者头像
达达前端
发布2019-08-25 09:59:35
6710
发布2019-08-25 09:59:35
举报
文章被收录于专栏:达达前端达达前端

funtion的封装 utils =>http.js

var tips = {
    1: "没有网络",
    999: "无效的请求",
    5000: "没有这些电影"
}

function http(params) {
    /*params 对象包含 url,success,method,data */
    if(!params.method){
        params.method ="GET";
    }
    wx.request({
        url:params.url,
        method:params.method,
        data:params.data,
        header: {
            'Content-Type': 'json'
        },
        success: function (res) {
            var statusCode = res.statusCode.toString();
            if (statusCode.startsWith("2")) {
                params.success(res.data);
            } else {

                var errorCode = res.data.code;
                _show_error(errorCode);
            }
        },
        fail: err => {
            _show_error(1);
        }
    })
}

function _show_error(errorCode) {
    wx.showToast({
        title: tips[errorCode],
        icon: "none",
        duration: 2000
    })
}
export {
    http
};

index.js

import {
http} from "../../utils/http";
const app = getApp()

Page({
  data: {
    movies: []
  },
  onLoad() {
      var url = "https://douban.uieee.com/v2/movie/top250";
      wx.showNavigationBarLoading();
      http({
        url,
        success: res => {
          this.handleData(res);
        }
      })
    },
})

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.08.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档