一个完整的项目示例:taro开发微信小程序

[复制链接]
发表于 2025-9-8 03:34:27 | 显示全部楼层 |阅读模式
前一周完成了一个项目,体测成绩转换的工具,没做纪录,。这次计划开发一个舆图应用小程序,纪录一下。方便给使用的人。
一、申请微信小程序,填写相应的信息,取得开发者ID。这个要给腾讯舆图使用的。

二、申请腾讯舆图API,创建应用,个人版,6500次调用 。并发5.也还行吧。







好了,我们开始。
三、新建一个文件夹,我的是win系统。

放在如下位置。D:\gitee\wechat-map
在目次处:cmd
四、创建环境相关。

我使用的是react+taro等,因为工程量小,不再搞复杂了。
参考 :安装及使用 | Taro 文档
看操纵纪录:
   npm install -g @tarojs/cli
    taro init wechat-map
  

   cd wechat-map
    pnpm install 
  五、 打开vscode,编译一下。

退出当前用户,重新登录一个用户。你懂的。
打开terminal,pnpm dev:weapp
我用的是taro 3.6.37,大概有点低,再说吧。
六,打开微信开发者工具。

直接导入项目即可,找到目次。其他主动生成。

留意清缓存,编译。
 

 七、正式开始吧,来个首页

  1. /*
  2. * @Author: DuYicheng
  3. * @Date: 2025-05-13 08:49:34
  4. * @LastEditors: DuYicheng
  5. * @LastEditTime: 2025-05-13 09:57:00
  6. * @Description:
  7. * @FilePath: \wechat-map\src\pages\index\index.tsx
  8. */
  9. import { View, Text, Image } from '@tarojs/components'
  10. import { useLoad } from '@tarojs/taro'
  11. import './index.scss'
  12. export default function Index() {
  13.   useLoad(() => {
  14.     console.log('Page loaded.')
  15.   })
  16.   return (
  17.     <View className='index'>
  18.       <View className='logo-container'>
  19.         <Image
  20.           className='medical-logo'
  21.           src='../../assets/images/medical-logo.png'
  22.           mode='aspectFit'
  23.         />
  24.         <Text className='title'>医疗服务</Text>
  25.       </View>
  26.       <View className='button-container'>
  27.         <View className='find-button clinic'>
  28.           找诊所
  29.         </View>
  30.         <View className='find-button pharmacy'>
  31.           找药店
  32.         </View>
  33.       </View>
  34.     </View>
  35.   )
  36. }
复制代码
  1. .index {
  2.   display: flex;
  3.   flex-direction: column;
  4.   align-items: center;
  5.   padding: 40px 20px;
  6. }
  7. .logo-container {
  8.   display: flex;
  9.   flex-direction: column;
  10.   align-items: center;
  11.   margin-bottom: 60px;
  12.   .medical-logo {
  13.     width: 120px;
  14.     height: 120px;
  15.     margin-bottom: 20px;
  16.   }
  17.   .title {
  18.     font-size: 24px;
  19.     color: #333;
  20.   }
  21. }
  22. .button-container {
  23.   width: 100%;
  24.   display: flex;
  25.   flex-direction: column;
  26.   gap: 20px;
  27.   padding: 0 40px; // 添加内边距使按钮更窄
  28. }
  29. .find-button {
  30.   width: 100%;
  31.   height: 80px; // 增加按钮高度
  32.   display: flex;
  33.   align-items: center;
  34.   justify-content: center;
  35.   border-radius: 8px;
  36.   font-size: 18px;
  37.   color: #fff;
  38.   
  39.   &.clinic {
  40.     background-color: #2D7DD2;
  41.   }
  42.   
  43.   &.pharmacy {
  44.     background-color: #45B649;
  45.   }
  46. }
复制代码


八、小程序sdk设置。




九、实现按钮的跳转。


 先实现诊所功能
1.布局图
  1. src/
  2.   ├── assets/
  3.   ├── pages/
  4.   │   ├── index/
  5.   │   └── clinic-map/
  6.   │       ├── index.tsx
  7.   │       ├── index.scss
  8.   │       └── index.config.ts
  9.   └── utils/
  10.       └── qqmap-wx-jssdk.js
复制代码
2.主页面调解代码如:
  1. import { View, Text, Image } from '@tarojs/components'
  2. import { useLoad, navigateTo } from '@tarojs/taro'
  3. import './index.scss'
  4. export default function Index() {
  5.   useLoad(() => {
  6.     console.log('Page loaded.')
  7.   })
  8.   const handleFindClinic = () => {
  9.     navigateTo({
  10.       url: '/pages/clinic-map/index'
  11.     })
  12.   }
  13.   return (
  14.     <View className='index'>
  15.       <View className='logo-container'>
  16.         <Image
  17.           className='medical-logo'
  18.           src='../../assets/medical-logo.png'
  19.           mode='aspectFit'
  20.         />
  21.         <Text className='title'>医疗服务</Text>
  22.       </View>
  23.       <View className='button-container'>
  24.         <View className='find-button clinic' onClick={handleFindClinic}>
  25.           找诊所
  26.         </View>
  27.         <View className='find-button pharmacy'>
  28.           找药店
  29.         </View>
  30.       </View>
  31.     </View>
  32.   )
  33. }
复制代码
3.诊所舆图
  1. import { View, Map } from '@tarojs/components'
  2. import Taro, { useLoad } from '@tarojs/taro'
  3. import { useState } from 'react'
  4. import './index.scss'
  5. const QQMapWX = require('../../utils/qqmap-wx-jssdk.js')
  6. let qqmapsdk
  7. export default function ClinicMap() {
  8.   const [latitude, setLatitude] = useState(0)
  9.   const [longitude, setLongitude] = useState(0)
  10.   const [clinics, setClinics] = useState([])
  11.   useLoad(() => {
  12.     // 初始化地图SDK
  13.     qqmapsdk = new QQMapWX({
  14.       key: 'YOUR_KEY_HERE' // 替换成你的腾讯地图key
  15.     })
  16.     // 获取当前位置权限
  17.     Taro.getLocation({
  18.       type: 'gcj02',
  19.       success: function(res) {
  20.         setLatitude(res.latitude)
  21.         setLongitude(res.longitude)
  22.         searchNearbyClinic(res.latitude, res.longitude)
  23.       },
  24.       fail: function() {
  25.         Taro.showToast({
  26.           title: '需要授权位置信息',
  27.           icon: 'none'
  28.         })
  29.       }
  30.     })
  31.   })
  32.   const searchNearbyClinic = (lat, lng) => {
  33.     qqmapsdk.search({
  34.       keyword: '诊所',
  35.       location: `${lat},${lng}`,
  36.       success: function(res) {
  37.         console.log(res)
  38.         setClinics(res.data)
  39.       },
  40.       fail: function(res) {
  41.         console.error(res)
  42.         Taro.showToast({
  43.           title: '搜索诊所失败',
  44.           icon: 'none'
  45.         })
  46.       }
  47.     })
  48.   }
  49.   return (
  50.     <View className='clinic-map'>
  51.       <Map
  52.         className='map'
  53.         latitude={latitude}
  54.         longitude={longitude}
  55.         markers={clinics.map((clinic, index) => ({
  56.           id: index,
  57.           latitude: clinic.location.lat,
  58.           longitude: clinic.location.lng,
  59.           title: clinic.title
  60.         }))}
  61.         show-location
  62.       />
  63.       <View className='clinic-list'>
  64.         {clinics.map((clinic, index) => (
  65.           <View key={index} className='clinic-item'>
  66.             <View className='clinic-name'>{clinic.title}</View>
  67.             <View className='clinic-address'>{clinic.address}</View>
  68.             <View className='clinic-distance'>{clinic._distance}米</View>
  69.           </View>
  70.         ))}
  71.       </View>
  72.     </View>
  73.   )
  74. }
复制代码
4.舆图样式
  1. .clinic-map {
  2.   height: 100vh;
  3.   display: flex;
  4.   flex-direction: column;
  5.   .map {
  6.     width: 100%;
  7.     height: 50vh;
  8.   }
  9.   .clinic-list {
  10.     flex: 1;
  11.     overflow-y: auto;
  12.     padding: 10px;
  13.     background: #f5f5f5;
  14.     .clinic-item {
  15.       background: #fff;
  16.       margin-bottom: 10px;
  17.       padding: 15px;
  18.       border-radius: 8px;
  19.       .clinic-name {
  20.         font-size: 16px;
  21.         font-weight: bold;
  22.         color: #333;
  23.         margin-bottom: 5px;
  24.       }
  25.       .clinic-address {
  26.         font-size: 14px;
  27.         color: #666;
  28.         margin-bottom: 5px;
  29.       }
  30.       .clinic-distance {
  31.         font-size: 12px;
  32.         color: #999;
  33.       }
  34.     }
  35.   }
  36. }
复制代码
5.创建页面设置
  1. export default {
  2.   navigationBarTitleText: '找诊所',
  3.   navigationBarBackgroundColor: '#ffffff',
  4.   navigationBarTextStyle: 'black'
  5. }
复制代码
6.修改:app.config.ts
  1. export default defineAppConfig({
  2.   pages: [
  3.     'pages/index/index',
  4.     'pages/clinic-map/index'
  5.   ],
  6.   window: {
  7.     backgroundTextStyle: 'light',
  8.     navigationBarBackgroundColor: '#fff',
  9.     navigationBarTitleText: 'WeChat',
  10.     navigationBarTextStyle: 'black'
  11.   },
  12.   permission: {
  13.     'scope.userLocation': {
  14.       desc: '你的位置信息将用于小程序位置接口的效果展示'
  15.     }
  16.   }
  17. })
复制代码
7.修改project.config.json
  1. {
  2.   // ...existing code...
  3.   "permission": {
  4.     "scope.userLocation": {
  5.       "desc": "你的位置信息将用于小程序位置接口的效果展示"
  6.     }
  7.   }
  8.   // ...existing code...
  9. }
复制代码
其他建议:
建议在开发者工具的详情设置中,确保以下设置:

  • 勾选"使用加强编译"
  • 勾选"不校验正当域名"(开发阶段)
  • 确保在"API 权限设置"中已经开启了位置相关接口权限
 
测试成功,但过了一会,说用量多了。vscode,重新编译。

 
 根本可以了,下一步就可以上传代码等待考核了。以上是一个简朴的教程,你可以根据现实修改。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表