怎么快速上手一個(gè)微信小程序--(逆戰(zhàn)片)

在這個(gè)特殊時(shí)期的日子里,躲在家里敲代碼是最安全的。接下來(lái)為大家介紹下微信小程序的快速開發(fā)流程。

1.首先看一下app.json的全局配置

{
  "pages": [
    "pages/index/index",
    "pages/category/index",
    "pages/goods_list/index",
    "pages/goods_detail/index",
    "pages/cart/index",
    "pages/collect/index",
    "pages/order/index",
    "pages/search/index",
    "pages/user/index",
    "pages/feedback/index",
    "pages/login/index",
    "pages/auth/index",
    "pages/pay/index"
  ],
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#eb4450",
    "navigationBarTitleText": "阿偉嚴(yán)選",
    "navigationBarTextStyle": "white"
  },
  "tabBar": {
    "color": "#999",
    "selectedColor": "#ff2d4a",
    "backgroundColor": "#fafafa",
    "position": "bottom",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首頁(yè)",
        "iconPath": "icons/home.png",
        "selectedIconPath": "icons/home-o.png"
      },
      {
        "pagePath": "pages/category/index",
        "text": "分類",
        "iconPath": "icons/category.png",
        "selectedIconPath": "icons/category-o.png"
      },
      {
        "pagePath": "pages/cart/index",
        "text": "購(gòu)物車",
        "iconPath": "icons/cart.png",
        "selectedIconPath": "icons/cart-o.png"
      },
      {
        "pagePath": "pages/user/index",
        "text": "我的",
        "iconPath": "icons/my.png",
        "selectedIconPath": "icons/my-o.png"
      }
    ]
  },
  "style": "v2",
  "sitemapLocation": "sitemap.json"
}

tabBar效果圖如下:


1582440519(1).jpg

2.接下來(lái)其他的頁(yè)面寫法都大同小異,主要說(shuō)一下購(gòu)物車的頁(yè)面
js

/**
 * 1 獲取用戶的收貨地址
  1 綁定點(diǎn)擊事件
  2 調(diào)用小程序內(nèi)置 api  獲取用戶的收貨地址  wx.chooseAddress

  2 獲取 用戶 對(duì)小程序 所授予 獲取地址的  權(quán)限 狀態(tài) scope
    1 假設(shè) 用戶 點(diǎn)擊獲取收貨地址的提示框 確定  authSetting scope.address
      scope 值 true 直接調(diào)用 獲取收貨地址
    2 假設(shè) 用戶 從來(lái)沒(méi)有調(diào)用過(guò) 收貨地址的api
      scope undefined 直接調(diào)用 獲取收貨地址
    3 假設(shè) 用戶 點(diǎn)擊獲取收貨地址的提示框 取消
      scope 值 false
      1 誘導(dǎo)用戶 自己 打開 授權(quán)設(shè)置頁(yè)面(wx.openSetting) 當(dāng)用戶重新給與 獲取地址權(quán)限的時(shí)候
      2 獲取收貨地址
    4 把獲取到的收貨地址 存入到 本地存儲(chǔ)中
 */
import {
  getSetting,
  chooseAddress,
  openSetting,
  showModal,
  showToast
} from "../../utils/asyncWx";
import regeneratorRuntime from "../../lib/runtime/runtime";
Page({
  data: {
    address: {},
    cart: [],
    allChecked: false,
    totalPrice: 0,
    totalNum: 0
  },
  // onShow()頁(yè)面加載就觸發(fā)
  onShow() {
    // 獲取緩存中的收貨地址信息
    const address = wx.getStorageSync("address");
    // ***************獲取緩存中的購(gòu)物車數(shù)據(jù)***************//
    const cart = wx.getStorageSync("cart") || [];
    this.setData({ address });
    this.setCart(cart);
    // ***************計(jì)算全選**********************//
    // every() 數(shù)組方法會(huì)遍歷 會(huì)接受一個(gè)回調(diào)函數(shù) 那么每一個(gè)回調(diào)函數(shù)都會(huì)返回true 那么every方法的返回值為true,
    // 只要有一個(gè)回調(diào)函數(shù)返回了false 那么不在循環(huán)執(zhí)行, 直接返回false
    // const allChecked = cart.length ? cart.every(v => v.checked) : false;
  },

  // 點(diǎn)擊 收貨地址
  async handleChooseAddress() {
    try {
      // 1 獲取 權(quán)限狀態(tài)
      const res1 = await getSetting();
      const scopeAddress = res1.authSetting["scope.address"];
      // 2 判斷 權(quán)限狀態(tài)
      if (scopeAddress === false) {
        await openSetting();
      }
      // 4 調(diào)用獲取收貨地址的 api
      let address = await chooseAddress();
      // 5 存入到緩存中
      wx.setStorageSync("address", address);
    } catch (error) {
      console.log(error);
    }
  },
  // 商品的選中
  handleItemChange(e) {
    // 1. 獲取被修改的商品id
    const goods_id = e.currentTarget.dataset.id;
    // 2. 獲取購(gòu)物車數(shù)組
    let { cart } = this.data;
    // 3.找到被修改的商品對(duì)象
    let index = cart.findIndex(v => v.goods_id === goods_id);
    // 4. 選中狀態(tài)取反
    cart[index].checked = !cart[index].checked;
    this.setCart(cart);
  },
  // **(重點(diǎn)封裝)設(shè)置購(gòu)物車狀態(tài)同時(shí) 重新計(jì)算 底部工具欄的數(shù)據(jù) 全選 總價(jià)格 購(gòu)買的數(shù)量
  setCart(cart) {
    wx.setStorageSync("cart", cart);
    let allChecked = true;
    // 1 總價(jià)格 總數(shù)量
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.num * v.goods_price;
        totalNum += v.num;
      } else {
        allChecked = false;
      }
    });
    // 判斷數(shù)組是否為空
    allChecked = cart.length != 0 ? allChecked : false;
    this.setData({
      cart,
      totalPrice,
      totalNum,
      allChecked
    });
    wx.setStorageSync("cart", cart);
  },
  // 全選 反選
  handleItemAllCheck() {
    // 1.獲取data中的數(shù)據(jù)
    let { cart, allChecked } = this.data;
    // 2. 修改值
    allChecked = !allChecked;
    // 3. 循環(huán)修改cart數(shù)組中的商品選中狀態(tài)
    cart.forEach(v => (v.checked = allChecked));
    // 4. 把修改后的值重新填充回data或者緩存中
    this.setCart(cart);
  },
  // 數(shù)量加減
  async handleItemNumEdit(e) {
    console.log(e);
    const { operation, id } = e.currentTarget.dataset;
    // 2 獲取購(gòu)物車數(shù)組
    let { cart } = this.data;
    // 3 找到需要修改的商品的索引
    const index = cart.findIndex(v => v.goods_id === id);
    // 4 進(jìn)行修改數(shù)量
    if (cart[index].num === 1 && operation === -1) {
      //彈窗提示
      const res = await showModal({ content: "您是否要?jiǎng)h除?" });
      if (res.confirm) {
        cart.splice(index, 1);
        this.setCart(cart);
      }
      // wx.showModal({
      //   title: "提示",
      //   content: "您是否要?jiǎng)h除?",
      //   success: res => {
      //     if (res.confirm) {
      //       cart.splice(index, 1);
      //       this.setCart(cart);
      //     } else if (res.cancel) {
      //       console.log(222);
      //     }
      //   }
      // });
    } else {
      cart[index].num += operation;
    }
    // 5 設(shè)置回緩存和data中
    this.setCart(cart);
  },
  //*************結(jié)算***************/
  async handlePay() {
    // 1 判斷是否有收貨地址
    const { address, totalNum } = this.data;
    if (!address.userName) {
      await showToast({ title: "您還沒(méi)有收貨地址!" });
      return;
    }
    // 判斷用戶有沒(méi)有選購(gòu)商品
    if (totalNum === 0) {
      await showToast({ title: "您還沒(méi)有選購(gòu)商品呢!" });
    }
    // 3 跳轉(zhuǎn)到支付頁(yè)面
    wx.navigateTo({
      url: "/pages/pay/index"
    });
  }
});

html

<!-- 收貨地址 -->
<view class="revice_address_row">
  <!-- 當(dāng)收貨地址 不存在 按鈕顯示 -->
  <view class="address_btn" wx:if="{{!address.userName}}">
    <button type="primary" plain="{{true}}" bind:tap="handleChooseAddress">獲取收貨地址</button>
  </view>
  <!-- 當(dāng)收貨地址 存在 按鈕顯示 -->
  <view wx:else class="user_info_row">
    <view class="user_info">
      <view>收貨人:{{address.userName}}</view>
      <view>{{address.provinceName+address.cityName+address.countyName+address.detailInfo}}</view>
    </view>
    <view class="user_phone">{{address.telNumber}}</view>
  </view>
</view>
<!-- 購(gòu)物車 -->
<view class="cart_content">
  <view class="cart_title">購(gòu)物車</view>
  <view class="cart_main">
    <block wx:if="{{cart.length!==0}}">
      <view class="cart_item" wx:for="{{cart}}" wx:key="goods_id">
        <!-- 單選框 -->
        <view class="cart_chk_wrap">
          <checkbox-group data-id="{{item.goods_id}}" bindchange="handleItemChange">
            <checkbox checked="{{item.checked}}"></checkbox>
          </checkbox-group>
        </view>
        <!-- 圖片 -->
        <navigator class="cart_img_wrap">
          <image src="{{item.goods_small_logo}}" mode="widthFix"></image>
        </navigator>
        <!-- 商品信息 -->
        <view class="cart_info_wrap">
          <view class="goods_name">{{item.goods_name}}</view>
          <view class="goods_price_wrap">
            <view class="goods_price">¥{{item.goods_price}}</view>
            <view class="cart_num_tool">
              <view class="num_edit" bindtap="handleItemNumEdit" data-id="{{item.goods_id}}" data-operation="{{-1}}">
                -
              </view>
              <view class="goods_num">{{item.num}}</view>
              <view class="num_edit" bindtap="handleItemNumEdit" data-id="{{item.goods_id}}" data-operation="{{1}}">
                +
              </view>
            </view>
          </view>
        </view>
      </view>
    </block>
    <block wx:else>
      <image mode="widthFix" src="http://hbimg.b0.upaiyun.com/e1b1467beea0a9c7d6a56b32bac6d7e5dcd914f7c3e6-YTwUd6_fw658"></image>
      <view class="del_white">主人趕快去買件商品吧~.~</view>
    </block>
  </view>
</view>
<!-- 底部工具欄 -->
<view class="footer_tool">
  <!-- 全選 -->
  <view class="all_chk_wrap">
    <checkbox-group bindchange="handleItemAllCheck">
      全選
      <checkbox checked="{{allChecked}}"></checkbox>
    </checkbox-group>
  </view>
  <!-- 總價(jià)格 -->
  <view class="total_price_wrap">
    <view class="total_price">
      合計(jì):
      <text class="total_price_text">¥{{totalPrice}}</text>
    </view>
    <view>包含運(yùn)費(fèi)</view>
  </view>
  <!-- 結(jié)算 -->
  <view class="order_pay_wrap" bindtap="handlePay">結(jié)算({{totalNum}})</view>
</view>

購(gòu)物車效果圖如下:


1582440826(1).jpg

購(gòu)物車基礎(chǔ)邏輯代碼這些基本夠用了,看到這里是不是小伙伴們都按耐不住自己的雙手了。
那就開始敲代碼吧。最后謝謝大家能夠?yàn)槲尹c(diǎn)個(gè)贊!感謝鐵鐵們。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 因新工作主要負(fù)責(zé)微信小程序這一塊,最近的重心就移到這一塊,該博客是對(duì)微信小程序整體的整理歸納以及標(biāo)明一些細(xì)節(jié)點(diǎn),初...
    majun00閱讀 7,643評(píng)論 0 9
  • 今天開始談?wù)摼嚯x的奧秘?,F(xiàn)在走路,過(guò)程中都。距離有心的距離,也有現(xiàn)實(shí)生活中的距離!今天來(lái)談一談,在現(xiàn)實(shí)生活中距離的...
    踏雪無(wú)痕Sunny閱讀 661評(píng)論 0 0
  • 1.小西是個(gè)普通女孩,普通的不能再普通,長(zhǎng)相是看了好幾眼也記不住,身材瘦瘦的像沒(méi)發(fā)育完全,也就是皮膚還白點(diǎn),梳著一...
    清醒過(guò)來(lái)的貓閱讀 616評(píng)論 0 1
  • 伯伯發(fā)來(lái)微信,催我趕快回去,最好是明天。 晚上,姑姑用微信與我視頻通話,她說(shuō):“你爺爺?shù)纳眢w狀況越來(lái)越差了,今天一...
    鹿宥宥閱讀 704評(píng)論 0 2
  • 之所以用重啟,是事先在此思考一下自己的2019,然后活出自己的期盼,那么在從容度過(guò)2019時(shí),我就會(huì)收獲雙倍的富足...
    紅葉女神閱讀 316評(píng)論 0 2

友情鏈接更多精彩內(nèi)容