常用知识总结

git使用

廖雪峰的官网
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

克隆项目:git clone "目标仓库"

1、创建版本库:cd到项目目录下执行 git init
2、查看一下状态:git status
3、添加到版本库:git add .
4、提交说明:git commit . -m "some message"
5、更新:git pull
6、提交: git push
7、撤销修改:git checkout .
8、提交记录:git log
9、版本回退: git reset --hard 3628164
10、回退到上一个版本:git reset --hard HEAD^

flex布局

Flex 布局教程:语法篇
http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
Flex 布局教程:实例篇
http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

目录结构

文件结构
https://mp.weixin.qq.com/debug/wxadoc/dev/framework/structure.html

注册页面

注册页面
https://mp.weixin.qq.com/debug/wxadoc/dev/framework/app-service/page.html

常用方法

data() 页面的初始数据
onShow() 监听页面显示
setData() 参数格式接受一个对象,以 key,value 的形式表示将 this.data 中的 key 对应的值改变成 value。

路由

页面路由
https://mp.weixin.qq.com/debug/wxadoc/dev/framework/app-service/route.html
路由跳转页面
https://mp.weixin.qq.com/debug/wxadoc/dev/api/ui-navigate.html
 wx.navigateTo({
    url: '../identify/identify'
 })

事件处理函数

事件处理函数
https://mp.weixin.qq.com/debug/wxadoc/dev/framework/app-service/page.html
点击事件
bindtap()

传参获取跳转

设置参数名data-title:
<view bindtap="toDetail" data-title="hello">

toDetail:function(e){
    var gettitle=e.currentTarget.dataset.title;(获取传过来的参数)

    带参数跳转:
     wx.navigateTo({
         url:"../detail/detail?title="+gettitle
     })
   }

获取传递的参数

Page({
  data:{
    title:''
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
        title:options.title
    })
  },
})

点击获取分类信息切换背景

绑定切换id
<view class="classify-left-list {{touchId == item.id ? 'classify-touch' : ''}}" data-classifyId="{{item.id}}" bindtap="getClassList">

 // 获取分类下的课程
  getClassList: function (event) {
    let that = this;
    var classId = event.currentTarget.dataset.classifyid;
    util.myrequest({course_class_id: classId},'/course/class/and/price/info','POST',function (res) {
      if(res.result) {
        that.setData({
          price: res.datas.price,
          course: res.datas.course,
          touchId: classId
        })
      }else{
        // console.log(res.result); 
      }
    })
  },

传递对象

传递:
var arr = {
            courseTitle: courseTitle,
            courseId: courseId
        };
        wx.navigateTo({
            url: "../detail/detail?title=" + JSON.stringify(arr)
        })

获取:
onLoad: function(options) {
        let that = this;
        let str = JSON.parse(options.title);
        let courseId = str.courseId;
        this.getDetail(that, courseId);
        wx.setNavigationBarTitle({
            title: str.courseTitle
        })
    },

模块化

模块化
https://mp.weixin.qq.com/debug/wxadoc/dev/framework/app-service/module.html

(util文件)
1、定义方法
function firstLogin() {}
2、导出方法
module.exports = {
    firstlogin: firstLogin,
}
3、引入方法
var util = require('../../utils/util.js');
4、使用方法
 util.firstlogin();

请求接口

发起请求
https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html#wxrequestobject
例子
wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {
     x: '' ,
     y: ''
  },
  header: {
      'content-type': 'application/json'
  },
  success: function(res) {
    console.log(res.data)
  }
封装方法
function myrequest(data, path, method, cb) {
    wx.request({
        url:  path,
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        data: data,
        method: method,
        success: function(res) {
            if (typeof cb == "function") {
                cb(res.data);
            }
        },

        // 登入失败
        fail: function() {
            wx.showModal({
                title: '警告',
                content: '网络异常!',
                success: function(res) {
                    if (res.confirm) {
                        // console.log('用户点击确定')
                    }
                }
            })
        },
    })
}
使用封装的方法请求接口
util.myrequest('', '/home/title ', 'GET', function(res) {
            if (res.result) {
                e.setData({
                    title: res.datas
                })
            }
        })

动态获取图片宽高适应屏幕

// 设置轮播图自适应高度
function imageLoad(e, that, w) {
    wx.getSystemInfo({
        success: function(res) {
            var widths = res.windowWidth; //屏幕宽度
            let originalWidth = e.detail.width;
            let originalHeight = e.detail.height;
            var lunboimg = widths * w / 2.35
            that.setData({
                lunboimg: lunboimg
            })
        }
    })
}

// 精品课程宽高比16:9
function autoheight(e, that, x, y, z) {
    //获取图片的原始宽度和高度  
    wx.getSystemInfo({
        success: function(res) {
            var autowidth = res.windowWidth; //屏幕宽度
            var swiperHeight = (x * autowidth * y) / z
            that.setData({
                imgheight: swiperHeight
            })
        }
    })
}
.js文件中使用方法

    // 轮播图2.35:1
    getImgHeight: function(e) {
        var that = this;
        util.imgauto(e, that, 1)
    },
    // 获取轮播图
    getLunbo: function(e) {
        var that = this;
        util.myrequest('', '/carousel/info', 'GET', function(res) {
            if (res.result) {
                e.setData({
                    reImg: res.datas
                })
            }
        })
    },
![OL~MA2P9ZI2AO6(EJ(PON09.png](http://upload-images.jianshu.io/upload_images/2605497-8263d861a8b004de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

.wxml中使用
<!-- 轮播图 -->
<view style="height:{{lunboimg}}px!important;">
    <swiper indicator-dots="{{indicatorDots}}" style="height:{{lunboimg}}px!important;" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" circular="{{circular}}">
        <block wx:for="{{reImg}}" wx:key="{{reImg}}">
            <swiper-item data-port="1" data-courseTitle="贝斯塔" data-courseId="{{item.url}}" bindtap="toDetails">
                <image bindload="getImgHeight" style="height:{{lunboimg}}px!important;" src="http://beisita.anasit.com:8888{{item.image}}" />
            </swiper-item>
        </block>
    </swiper>
</view>
<!-- 轮播图结束 -->

判断登陆

https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html#wxloginobject

// 第一次使用小程序调用登录注册信息
// 是否是第一次注册
function firstLogin() {
    let that = this;
    wx.checkSession({
        success: function() {
            //session 未过期,并且在本生命周期一直有效
            wx.login({
                success: function(res) {
                    // console.log(res.code)
                    if (res.code) {
                        var code = res.code;
                        that.myrequest({ code: code }, '/user/come/again', 'POST', function(res) {
                            if (res.result) {
                                wx.setStorageSync('account', res.datas.account);
                                wx.setStorageSync('token', res.datas.token);
                            } else {
                                wx.navigateTo({
                                    url: '../identify/identify'
                                })

                            }
                        })
                    } else {
                        console.log('获取用户登录态失败!' + res.errMsg)
                    }
                }
            })
        },

        fail: function() {
            //登录态过期
            wx.login({
                success: function(res) {
                    if (res.code) {
                        var code = res.code;
                        // console.log(code);
                        that.myrequest({ code: code }, '/user/come/again', 'POST', function(res) {
                            if (res.result) {

                            } else {
                                wx.navigateTo({
                                    url: '../identify/identify?code=' + code
                                })
                            }
                        })
                    } else {
                        console.log('获取用户登录态失败!' + res.errMsg)
                    }
                }
            })
        }
    })
}
// 检查是否有token account
function checklogin() {
    console.log('token')
    var token = wx.getStorageSync('token');
    if (!token) {
        wx.navigateTo({
            url: '../identify/identify'
        })
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,050评论 25 707
  • 1、开发常用网址 开源项目及源码解析 泡在网上的日子 Android技术持续更新 stackoverflow 2、...
    赫丹阅读 355评论 0 2
  • 1.隐藏导航栏上的返回字体 2.去掉tableView多余的线条 3.去掉tableView的线条 4.去掉cel...
    Frank_chun阅读 1,112评论 4 16
  • 不知道从何说起呢。 放假的这段时间里打了两份寒假工,这也算是一种缘分吧,让我们见识了世间冷暖。很多事情对于每...
    宁宁晓心阅读 285评论 2 0
  • 半个月后。 “小姐,怎么还不睡啊?” 郦歌一手拿着烛台,一手护着灯火不被风吹灭,来到雁生的床边,将手里的灯搁下,问...
    成桑阅读 257评论 0 0