基于vue技术栈-移动端外卖项目实战问题总结

雨点打透心脏的1/2处 2022-03-12 13:12 234阅读 0赞

前言

为了深入学习vue全家桶及前后台交互技术,包括vuex,vue-router,ajax等,模仿某平台写一个简化版的移动端项目出来

技术栈

vue2 + vuex + vue-router + webpack + ES6/7 + axios + stylus + mint-ui + mock模拟数据

项目链接及预览

点此进入github

此项目github地址: https://github.com/nopapername/vue-oyshop-project

关于接口数据

在github上找的别人做好的一个超大型外卖平台项目api文档里的其中几项
引用大佬的api接口文档地址
用到以下接口:
1、获取城市列表
2、根据经纬度详细定位
3、食品分类列表
4、获取商铺列表
5、获取验证码
6、获取用户信息
7、登录
8、退出

遇到的一些问题

  • 在组件上绑定点击事件@click=”$router.back()“无法返回上一页?
    解决办法:组件上绑定事件时需要加上native修饰符
    (即:@click.native=”$router.back())
  • 移动端页面的响应性如何解决?
    解决办法:使用媒体查询器和em,rem进行适配。
    (即:媒体查询适配范围自行百度)
  • 跨域发送ajax请求的方法?
    解决办法:配置webpack-dev-server,代理某一个路径到目标路径,同时更改源和重写

    // 修改config里的配置文件index.js中以下部分
    // Paths

    1. assetsSubDirectory: 'static',
    2. assetsPublicPath: '/',
    3. proxyTable: {
    4. '/api': { // 匹配所有以'/api'开头的请求路径
    5. target: 'https://elm.cangdu.org', // 代理目标的基础路径
    6. changeOrigin: true, // 支持跨域
    7. pathRewrite: { // 重写路径:删除路径中开头的'/api'
    8. '^/api': ''
    9. }
    10. }
    11. },
  • 如何使用从后台获取了数据更新后的state中的数据再次请求数据(此项目中用在使用获得的经纬度再来确定具体位置以及用在界面更新之后再创建swiper对象)?
    解决办法: 使用watch监听并利用vue提供的方法this.$nextTick(() => {})来等待数据更新后再进行后台请求。第二种方法是使用定时器设置一定的间隔时间再请求数据。

    watch: {

    1. cityPosition (value) {
    2. this.$nextTick(() => {
    3. this.getCityDetailedInfo()
    4. this.getShopList()
    5. })
    6. }

    }

  • 手机短信验证码登录时手机号格式输入正确 发送验证码按钮亮起,点击后显示已发送并倒计时 功能?
    解决办法:使用计算属性返回判断手机号格式的正则表达式的布尔值来进行动态添加类,并添加点击事件方法设置定时器实现倒计时效果。
    html代码




js中:

  1. data () {
  2. return {
  3. loginWay: true, // 切换登录方式,true显示当前登录界面
  4. computeTime: 0, // 验证码倒计时
  5. computedFlag: false, // 计数标志,当计数时切换到不可点击的按钮颜色
  6. phone: '' // 手机号
  7. }
  8. },
  9. computed: {
  10. rightPhone () {
  11. return /^1[345789]\d{9}$/.test(this.phone)
  12. }
  13. },
  14. methods: {
  15. getCode () {
  16. if (!this.computeTime) {
  17. this.computedFlag = true
  18. this.computeTime = 30
  19. const computedNum = setInterval(() => {
  20. this.computeTime -= 1
  21. if (this.computeTime === 0) {
  22. clearInterval(computedNum)
  23. this.computedFlag = false
  24. }
  25. }, 1000)
  26. }
  27. }
  28. }
  • 按需引入mint-ui 的一些坑点?
    网上搜集到了一篇知识点博客 点击进入
  • 发ajax请求在调试器network中出现错误404一些原因?
    1 请求的路径是不是对
    2 代理是否生效(配置和重启)
    3 服务器应用是否运行
  • vuex报错: [vuex] Expects string as the type, but found object 什么原因???
    解决:actions中代码写错了。仔细检查!!!(百度了下mutation和mutation-types中写错也有此类型错误提示)

    // 错误的
    async getCaptchas ({commit}) {

    1. const captchas = await reqCaptchas()
    2. commit([RECEIVE_CAPTCHAS], {captchas})

    }
    // 正确的
    async getCaptchas ({commit}) {

    1. const captchas = await reqCaptchas()
    2. commit(RECEIVE_CAPTCHAS, {captchas})

    }

接口列表:

1、获取城市列表

请求URL:

  1. https://elm.cangdu.org/v1/cities

示例:

https://elm.cangdu.org/v1/cities?type=guess

请求方式:

  1. GET

参数类型:query


















参数 是否必选 类型 说明
type Y string guess:定位城市, hot:热门城市, group:所有城市

返回示例:

  1. {
  2. id: 1,
  3. name: "上海",
  4. abbr: "SH",
  5. area_code: "021",
  6. sort: 1,
  7. latitude: 31.23037,
  8. longitude: 121.473701,
  9. is_map: true,
  10. pinyin: "shanghai"
  11. }

2、根据经纬度详细定位

请求URL:

  1. https://elm.cangdu.org/v2/pois/:geohash

示例:

https://elm.cangdu.org/v2/pois/31.22967,121.4762

请求方式:

  1. GET

参数类型:param


















参数 是否必选 类型 说明
geohash Y string 经纬度

返回示例:

  1. {
  2. address: "上海市黄浦区西藏中路",
  3. city: "上海市",
  4. geohash: "31.22967,121.4762",
  5. latitude: "31.22967",
  6. longitude: "121.4762",
  7. name: "黄浦区上海人民广场"
  8. }

3、食品分类列表

请求URL:

  1. https://elm.cangdu.org/v2/index_entry

示例:

https://elm.cangdu.org/v2/index_entry

请求方式:

  1. GET

参数类型:











参数 是否必选 类型 说明

返回示例:

  1. [
  2. {
  3. id: 1,
  4. is_in_serving: true,
  5. description: "0元早餐0起送,每天都有新花样。",
  6. title: "预订早餐",
  7. link: "",
  8. image_url: "/d/49/7757ff22e8ab28e7dfa5f7e2c2692jpeg.jpeg",
  9. icon_url: "",
  10. title_color: "",
  11. __v: 0
  12. },
  13. {
  14. id: 65,
  15. is_in_serving: true,
  16. description: "",
  17. title: "土豪推荐",
  18. image_url: "/d/49/7757ff22e8ab28e7dfa5f7e2c2692jpeg.jpeg",
  19. link: "",
  20. icon_url: "",
  21. title_color: "",
  22. __v: 0
  23. },
  24. ... n条数据
  25. ]

4、获取商铺列表

请求URL:

  1. https://elm.cangdu.org/shopping/restaurants

示例:

https://elm.cangdu.org/shopping/restaurants?latitude=31.22967&longitude=121.4762

请求方式:

  1. GET

参数类型:query


































































参数 是否必选 类型 说明
latitude Y string 纬度
longitude Y string 经度
offset N int 跳过多少条数据,默认0
limit N int 请求数据的数量,默认20
restaurant_category_id N int 餐馆分类id
order_by N int 排序方式id: 1:起送价、2:配送速度、3:评分、4: 智能排序(默认)、5:距离最近、6:销量最高
delivery_mode N array 配送方式id
support_ids N array 餐馆支持特权的id
restaurant_category_ids N array 餐馆分类id

返回示例:

  1. [
  2. {
  3. name: "肯德基",
  4. address: "上海市宝山区淞宝路155弄18号星月国际商务广场1层",
  5. id: 1,
  6. latitude: 31.38098,
  7. longitude: 121.50146,
  8. location: [
  9. 121.50146,
  10. 31.38098
  11. ],
  12. phone: "1232313124324",
  13. category: "快餐便当/简餐",
  14. supports: [
  15. {
  16. description: "已加入“外卖保”计划,食品安全有保障",
  17. icon_color: "999999",
  18. icon_name: "保",
  19. id: 7,
  20. name: "外卖保",
  21. _id: "591bec73c2bbc84a6328a1e5"
  22. },
  23. {
  24. description: "准时必达,超时秒赔",
  25. icon_color: "57A9FF",
  26. icon_name: "准",
  27. id: 9,
  28. name: "准时达",
  29. _id: "591bec73c2bbc84a6328a1e4"
  30. },
  31. {
  32. description: "该商家支持开发票,请在下单时填写好发票抬头",
  33. icon_color: "999999",
  34. icon_name: "票",
  35. id: 4,
  36. name: "开发票",
  37. _id: "591bec73c2bbc84a6328a1e3"
  38. }
  39. ],
  40. status: 0,
  41. recent_order_num: 615,
  42. rating_count: 389,
  43. rating: 1.6,
  44. promotion_info: "他依然有人有人有人有人有人",
  45. piecewise_agent_fee: {
  46. tips: "配送费约¥5"
  47. },
  48. opening_hours: [
  49. "8:30/20:30"
  50. ],
  51. license: {
  52. catering_service_license_image: "",
  53. business_license_image: ""
  54. },
  55. is_new: true,
  56. is_premium: true,
  57. image_path: "/img/shop/15c1513a00615.jpg",
  58. identification: {
  59. registered_number: "",
  60. registered_address: "",
  61. operation_period: "",
  62. licenses_scope: "",
  63. licenses_number: "",
  64. licenses_date: "",
  65. legal_person: "",
  66. identificate_date: null,
  67. identificate_agency: "",
  68. company_name: ""
  69. },
  70. float_minimum_order_amount: 20,
  71. float_delivery_fee: 5,
  72. distance: "19.5公里",
  73. order_lead_time: "40分钟",
  74. description: "好吃的",
  75. delivery_mode: {
  76. color: "57A9FF",
  77. id: 1,
  78. is_solid: true,
  79. text: "蜂鸟专送"
  80. },
  81. activities: [
  82. {
  83. icon_name: "减",
  84. name: "满减优惠",
  85. description: "满30减5,满60减8",
  86. icon_color: "f07373",
  87. id: 1,
  88. _id: "591bec73c2bbc84a6328a1e7"
  89. },
  90. {
  91. icon_name: "特",
  92. name: "优惠大酬宾",
  93. description: "是对冯绍峰的上市房地产",
  94. icon_color: "EDC123",
  95. id: 2,
  96. _id: "591bec73c2bbc84a6328a1e6"
  97. }
  98. ],
  99. }
  100. ... 20条数据
  101. ]

5、获取验证码

请求URL:

  1. https://elm.cangdu.org/v1/captchas

示例:

请求方式:

  1. POST

参数类型:











参数 是否必选 类型 说明

返回示例:

  1. {
  2. status: 1,
  3. code: base64
  4. }

6、获取用户信息

请求URL:

  1. https://elm.cangdu.org/v1/user

示例:

请求方式:

  1. GET

参数类型:











参数 是否必选 类型 说明

返回示例:

  1. {
  2. username: "1",
  3. user_id: 2,
  4. id: 2,
  5. point: 0,
  6. mobile: "",
  7. is_mobile_valid: true,
  8. is_email_valid: false,
  9. is_active: 1,
  10. gift_amount: 3,
  11. email: "",
  12. delivery_card_expire_days: 0,
  13. current_invoice_id: 0,
  14. current_address_id: 0,
  15. brand_member_new: 0,
  16. balance: 0,
  17. avatar: "/img/default/default.jpg",
  18. __v: 0
  19. }

7、登录

请求URL:

  1. https://elm.cangdu.org/v2/login

示例:

请求方式:

  1. POST

参数类型:






























参数 是否必选 类型 说明
username Y string 用户名
password Y string 密码
captcha_code Y string 验证码

返回示例:

  1. {
  2. username: "1",
  3. user_id: 2,
  4. id: 2,
  5. point: 0,
  6. mobile: "",
  7. is_mobile_valid: true,
  8. is_email_valid: false,
  9. is_active: 1,
  10. gift_amount: 3,
  11. email: "",
  12. delivery_card_expire_days: 0,
  13. current_invoice_id: 0,
  14. current_address_id: 0,
  15. brand_member_new: 0,
  16. balance: 0,
  17. avatar: "/img/default/default.jpg",
  18. __v: 0
  19. }

8、退出

请求URL:

  1. https://elm.cangdu.org/v2/signout

示例:

请求方式:

  1. GET

参数类型:











参数 是否必选 类型 说明

返回示例:

  1. {
  2. status: 1,
  3. message: '退出成功'
  4. }

发表评论

表情:
评论列表 (有 0 条评论,234人围观)

还没有评论,来说两句吧...

相关阅读