微信/支付宝小程序开发之路

一、微信小程序

1、开发之前

使用的工具:微信开发者工具 + VSCode
开发的时候并没有给上线时真正的appkey,是使用自己注册的的appkey(好像也可以用测试账号)进行开发。
小程序注册流程就不记录了。(以下只针对个人当前这个小程序)

2、开发中的问题

2.1、代码差异(与html的区别)

常用标签

  • text

文字都要写在<text>标签中

  • view
<view class="" hover-class="none" hover-stop-propagation="false"></view>
//hover-class   指定按下去的样式类。当 hover-class="none" 时,没有点击态效果
//hover-stop-propagation 指定是否阻止本节点的祖先节点出现点击态
//就个人而言:以上两个属性都没有使用过  还是按正常HTML、css样式来写的,
//具体见 https://developers.weixin.qq.com/miniprogram/dev/component/view.html
  • image
<image class="" src="" mode="aspectFit|aspectFill|widthFix" lazy-load="false" binderror="" bindload=""></image>
//mode属性有很多  自己理解: aspectFit相当于contain ,aspectFill相当于cover
//lazy-load  图片懒加载,在即将进入一定范围(上下三屏)时才开始加载
//第一个项目 只是用了这两个属性
  • cover-view
<cover-view></cover-view>
//可覆盖的原生组件包括 map、video、canvas、camera、live-player、live-pusher
//只支持嵌套 cover-view、cover-image,可在 cover-view 中使用 button。组件属性的长度单位默认为px,2.4.0起支持传入单位(rpx/px)。

注意:
之前想要实现一个在直播中的视频图层上显示评论列表,评论列表是要有滚动条的,最新评论显示在最下方,保证滚动条一直滚到最下方,我这边使用的是<cover-view></cover-view>,然后设置了scroll-top属性,文档中描述说的是“设置顶部滚动偏移量,仅在设置了 overflow-y: scroll 成为滚动元素后生效”,但是我这边设置一直没有成功,后面又将<cover-view></cover-view>换成了<view></<view>,在<view></<view>标签中嵌套了<scroll-view></scroll-view>

  • scroll-view
<scroll-view></scroll-view>
//可滚动视图区域。使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置 height。
//属性值有很多,具体见文档
  • cover-image
<cover-image class="" src="" bindload="" binderror=""></cover-image>
  • swiper + swiper-item
<swiper indicator-dots="true" autoplay="true" interval="5000" duration="100" circular='true' indicator-active-color="#fff" indicator-color='#ccc'>
 <block wx:for="{{swiperList}}" wx:key wx:for-index="index">
  <swiper-item class="swiperItem flexMiddle">
    <image src="{{item.host + item.dir + item.filepath + item.filename}}" class="slide-image" mode="aspectFill" />
  </swiper-item>
 </block>
</swiper>
  • live-player
<live-player class="" src="" mode="live" autoplay="false" muted="false" orientation="vertical" 
  object-fit="contain" min-cache="1" max-cache="3" 
  bindstatechange="" bindfullscreenchange="" bindnetstatus="">
  
</live-player>

2.2、图标

小程序中有一些自带的图标
写法如下:

 <icon type="success" size="" color=""/>
// type 是控制不同的图片,size 是图标大小,color 是图标颜色
//icon的类型,有效值:success, success_no_circle, info, warn, waiting, cancel, download, search, clear
//详情看文档

3、上传体验版

上传之后

  • 1、在【开发】-【开发设置】-【服务器域名】中配置在小程序中用到的所有域名(注意:一个月内可申请5次修改)
  • 2、若是直播的小程序需要在【开发】-【开发设置】-【接口设置】打开下图两个功能


    image.png

4、感受过的坑

标签:<cover-view>自身有个white-space: nowrap属性
1、当在使用这个标签并想实现文字两行省略号显示的时候,文字怎么都不换行,最后在发现自身属性的问题,后面将white-space设为normal
对于<cover-view>本来是不知道也不想使用的,但是同事说,单纯的定位可能后期会有问题,最好是用一下。但是在当前这个项目中,没有过多的使用这个标签,基本还是通过定位来实现。

二、支付宝小程序

1、开发之前

1.1、使用的工具: IDE + VSCode
下载IDE
注意:IDE请去官网下载最新版,不然有的在运行的时候 会有一些莫名的报错
1.2、开通支付宝小程序的后台
1.3、安装HerbJS

HerbJS文档地址
a.安装herbjs: npm install herbjs -g
b.初始化项目: herb init

image.png

以上命令若是不能正常执行,请加sudo

2、开发运行

在VSCode 打开项目文件夹 使用【npm install; npm run dev】运行代码 && IDE打开项目文件夹预览效果,二者均可编写代码

3、开发中的问题

4、总结

  • 4.1 支付宝小程序排序组件(基于herbjs+pinyin-pro)
image.png
//index.axml
<view class="alphabeticSort">
  <scroll-view scroll-into-view="{{curKey}}" scroll-y="{{true}}" style="height: 100vh;" onScroll="onScroll" scroll-with-animation="{{true}}">
    <view a:for="{{indexList}}">
      <view class="code" id="{{item}}">
        <text>{{item}}</text></view>
      <view class="list" a:for="{{mapObj[item]}}" a:for-item="name">
        <view class="listItem" onTap="onClick" data-name="{{name}}">
          <view class="left">{{item}}</view>
          <view class="right">{{name}}</view>
        </view>
      </view>
    </view>
    <view class="alphabeticList" a:if="{{indexList && indexList.length}}">
      <view a:for="{{indexList}}">
        <view class="{{curKey ===  item || newKey === item ? 'active': '' }}" style="margin-bottom: 4rpx;text-align:center;" 
          onTap="getItem" data-item="{{item}}">{{item}}</view>
      </view>
    </view>
  </scroll-view>
</view>
//index.js
import { Component } from 'herbjs';
import { pinyin } from 'pinyin-pro';
interface IComponentData {}

interface IComponentProps {
  dataList: {
    name: string;
  }[];
  onClick?: (e) => void;
}

interface IComponentMethods {
  getItem(event: any): void;
  onScroll(event: any): void;
  onClick(event: any): void;
}
function getRectAll(selector) {
  return new Promise(function(resolve) {
    my.createSelectorQuery()
      .selectAll(selector)
      .boundingClientRect()
      .exec(function(rect) {
        if (rect === void 0) {
          rect = [];
        }
        return resolve(rect[0]);
      });
  });
}
function debounceHandle(fn) {
  let timer = null;
  return function(...args) {
    // @ts-ignore
    const that = this;
    clearTimeout(timer); // 如果存在事件就清除定时器
    // @ts-ignore
    const arg = args;
    timer = setTimeout(function() {
      // 如果不存在那么就开启定时器
      fn.apply(that, arg);
    }, 200);
  };
}
Component<IComponentData, IComponentProps, IComponentMethods>({
  data: {
    indexList: [],
    mapObj: {},
    curKey: '',
    heightList: [],
    newKey: '',
    flag: 0,
  },
  props: {
    dataList: [],
    // onClick: () => {},
  },
  didUpdate(prevProps) {
  //父组件传过来的数组
    if (prevProps.dataList === this.props.dataList) return;
    const py = /^[A-Z]$/;
    //mapObj为左侧需要渲染的列表数据{A: [],B: []}
    const mapObj = this.props.dataList.reduce((pre, cur) => {
      const eng = pinyin(cur.name, {
        pattern: 'first',
        toneType: 'none',
        type: 'array',
      })[0].toUpperCase();
      const key = py.test(eng) ? eng : '#';
      return {
        ...pre,
        [key]: (pre[key] || []).concat([ cur.name ]).sort(),
      };
    }, {});
    //indexList为右侧需要渲染的 A-Z 首字母列表
    const indexList = Object.keys(mapObj).sort();
    if (indexList[0] === '#') {
      indexList.push(indexList.shift());
    }
    getRectAll('.code').then((res: any) => {
      this.setData({
        heightList: res,
      });
    });
    this.setData({
      indexList,
      mapObj,
      curKey: indexList[0],
    });
  },
  methods: {
    getItem(e) {
      this.setData({
        curKey: e.target.dataset.item,
        newKey: '',
      });
    },
    onScroll: debounceHandle(function(e) {
      const scrollTop = e.detail.scrollTop;
      // @ts-ignore
      const index = this.data.heightList.findIndex((cur, index, arr) => {
        return scrollTop >= arr[index].top && scrollTop <= arr[index + 1].top;
      });
      // @ts-ignore
      const key = this.data.indexList[index];
      // @ts-ignore
      this.setData({
        newKey: key,
        curKey: '',
      });
    }),
    onClick(e) {
      this.props.onClick && this.props.onClick(e.target.dataset);
    },
  },
});

//index.less
/*
.alphabeticSort {
  .code {
    padding: 24rpx;
  }
  .list {
    background-color: #fff;
    .listItem {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      padding: 24rpx;
      .left {
        background-color: #ccc;
        width: 80rpx;
        height: 80rpx;
        border-radius: 50%;
        text-align: center;
        line-height: 80rpx;
        font-size: 48rpx;
        font-weight: 400;
        color: #fff;
        margin-right: 24rpx;
      }
      .right {
        color: #333;
      }
    }
  }
  .alphabeticList {
    position: fixed;
    right: 40rpx;
    top: 50%;
    background-color: #fff;
    border-radius: 10rpx;
    padding: 15rpx;
    color: #333;
    transform: translateY(-50%);
    font-size: 28rpx;
    box-shadow: #ccc 0rpx 0rpx 20rpx;
    .active {
      color: #1677ff;
    }
  }
}
*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,135评论 6 514
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,317评论 3 397
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,596评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,481评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,492评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,153评论 1 309
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,737评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,657评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,193评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,276评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,420评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,093评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,783评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,262评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,390评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,787评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,427评论 2 359

推荐阅读更多精彩内容