• uniapp 学习笔记三十四 加载用户地址列表和默认地址的在线数据修改


    uniapp 学习笔记三十四 加载用户地址列表和默认地址的在线数据修改

    uniapp 学习笔记三十四 加载用户地址列表和默认地址的在线数据修改

     

     

    order.vue

    1. <script>
    2. import {mapGetters} from 'vuex'
    3. export default {
    4. data() {
    5. return {
    6. };
    7. },
    8. computed:{
    9. ...mapGetters({
    10. 'orderAddress':'address/orderAddress'
    11. })
    12. },
    13. methods:{
    14. handleAddress(){
    15. uni.navigateTo({
    16. url:'../address/address'
    17. })
    18. }
    19. }
    20. }
    21. script>
    22. <style lang="scss">
    23. .poster{
    24. width: 180upx;
    25. height: 180upx;
    26. background-color: #d8d8d8;
    27. }
    28. .info{
    29. width: 60%;
    30. .edit{
    31. width: 80upx;
    32. height: 80upx;
    33. text-align: center;
    34. line-height: 80upx;
    35. background-color: #e6e6e6;
    36. border-radius: 50%;
    37. }
    38. }
    39. .flex.align-end{
    40. min-width: 210upx;
    41. }
    42. .icon-youxiajiaogouxuan{
    43. color: #e7e7e7;
    44. }
    45. .cu-btn.lg{
    46. width: 50%;
    47. }
    48. .fixed {
    49. position: fixed;
    50. bottom: 0;
    51. left: 0;
    52. width: 100%;
    53. box-shadow: 0 0 10upx 2upx rgba(0, 0, 0, 0.2);
    54. }
    55. style>

    address.vue

    1. <script>
    2. import {mapState,mapMutations} from 'vuex'
    3. export default {
    4. data() {
    5. return {
    6. };
    7. },
    8. computed: {
    9. ...mapState({
    10. addressList:state=>state.address.addressList,
    11. checkedIdx:state=>state.address.checkedIdx,
    12. userInfo:state=>state.user.userInfo
    13. })
    14. },
    15. methods:{
    16. ...mapMutations({
    17. 'handleDefault':'address/addressDefaultMut',
    18. 'handleCheckAddress':'address/addressCheckMut'
    19. }),
    20. handleAddAddress(){
    21. uni.navigateTo({
    22. url:'address-detail'
    23. })
    24. },
    25. handleDefault(idx){
    26. let obj = {"requests":[]};
    27. this.addressList.forEach((item,i)=>{
    28. let bool = i===idx;
    29. obj.requests.push({
    30. "method":"PUT",
    31. "path":`/1.1/classes/address/${item.objectId}`,
    32. "body":{
    33. "isdefault":bool
    34. }
    35. })
    36. })
    37. //批量操作在线数据
    38. this.$post('/1.1/batch',obj).then(res=>{
    39. this.$store.commit('address/addressDefaultMut',idx)
    40. })
    41. }
    42. }
    43. }
    44. script>
    45. <style lang="scss">
    46. .edit{
    47. width: 80upx;
    48. height: 80upx;
    49. text-align: center;
    50. line-height: 80upx;
    51. background-color: #e6e6e6;
    52. border-radius: 50%;
    53. }
    54. .cu-btn.lg{
    55. width: 50%;
    56. }
    57. .fixed {
    58. position: fixed;
    59. bottom: 0;
    60. left: 0;
    61. width: 100%;
    62. box-shadow: 0 0 10upx 2upx rgba(0, 0, 0, 0.2);
    63. }
    64. .icon-checkbox-xuanzhong{
    65. width: 50upx;
    66. height: 50upx;
    67. font-size: 50upx;
    68. text-align: center;
    69. line-height: 50upx;
    70. color: #e6e6e6;
    71. background-color: #e6e6e6;
    72. border-radius: 20%;
    73. }
    74. .default{
    75. position: relative;
    76. overflow: hidden;
    77. .default-cont{
    78. padding: 20upx 10upx 10upx;
    79. width: 100upx;
    80. background-color: #fae456;
    81. font-size: 12upx;
    82. text-align: center;
    83. position: absolute;
    84. top: -20upx;
    85. right: -40upx;
    86. transform: rotate(45deg);
    87. display: block;
    88. }
    89. }
    90. .default-cont{
    91. display: none;
    92. }
    93. style>

    address.js

    1. import {$post,$get} from '@/utils/request.js'
    2. export default{
    3. namespaced:true,
    4. state(){
    5. return{
    6. checkedIdx:-1, //记录用户勾选的地址
    7. addressList:[
    8. /* {
    9. username:'曹国舅',
    10. phone:'13010101010',
    11. city:'北京',
    12. region:'朝阳区',
    13. detail:'朝阳路周家井',
    14. isdefault:true
    15. },{
    16. username:'何仙姑',
    17. phone:'13010101010',
    18. city:'北京',
    19. region:'海淀区',
    20. detail:'中关村二街',
    21. isdefault:false
    22. },{
    23. username:'吕洞宾',
    24. phone:'13010101010',
    25. city:'北京',
    26. region:'门头沟',
    27. detail:'黑山大街',
    28. isdefault:false
    29. } */
    30. ]
    31. }
    32. },
    33. getters:{
    34. orderAddress(state){ // 订单中的地址
    35. // 根据 checkedIdx 与 isdefault 共同得到一个地址对象
    36. let {checkedIdx,addressList} = state
    37. if (checkedIdx!=-1) {
    38. return addressList[checkedIdx]
    39. }
    40. let len = addressList.length
    41. for(let i=0;i
    42. if(addressList[i].isdefault){
    43. return addressList[i]
    44. }
    45. }
    46. }
    47. },
    48. mutations:{
    49. addressDefaultMut(state,idx){ //设为默认
    50. state.addressList.forEach((item,i)=>{
    51. if(i==idx){
    52. item.isdefault = true
    53. }else{
    54. item.isdefault = false
    55. }
    56. })
    57. },
    58. addressCheckMut(state,idx){
    59. //选取地址
    60. state.checkedIdx = idx
    61. //点选返回
    62. uni.navigateBack({
    63. delta:1
    64. })
    65. },
    66. addressAddMut(state,addressObj){
    67. //新增地址
    68. state.addressList.push(addressObj)
    69. },
    70. addressInitMut(state,addressArr){
    71. //初始化地址列表
    72. state.addressList = addressArr
    73. }
    74. },
    75. actions:{
    76. addressAddAct(context,addressObj){
    77. $post('/1.1/classes/address',addressObj).then(({objectId})=>{
    78. // console.log(res);
    79. context.commit('addressAddMut',{
    80. ...addressObj,
    81. objectId
    82. })
    83. uni.navigateBack({
    84. delta:1
    85. })
    86. })
    87. },
    88. addressInitAct(context,userid){
    89. let url = `/1.1/classes/address?where={"userid":"${userid}"}`
    90. $get(url).then(({results})=>{
    91. // console.log(url,res);
    92. context.commit('addressInitMut',results)
    93. })
    94. }
    95. }
    96. }

    APP.vue

    1. <style lang="scss">
    2. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
    3. @import "@/uni_modules/uview-ui/index.scss";
    4. /*每个页面公共css */
    5. @import url("@/common/uni.css");
    6. style>

     user.js

    1. import {$post} from '../utils/request.js'
    2. export default {
    3. namespaced:true, //开启命名空间后,访问所有属性都需要带模块名
    4. state(){
    5. return {
    6. userInfo:null
    7. }
    8. },
    9. mutations:{
    10. initInfo(state,info){
    11. // 存入状态机变量
    12. state.userInfo = info
    13. }
    14. },
    15. actions:{
    16. userLoginAct(context,info){
    17. $post('/1.1/login',info).then(res=>{
    18. console.log('PostUser:',res)
    19. let {code} = res
    20. if (code) {
    21. let title = code == 211 ? '账号不存在' : '密码错误'
    22. uni.showToast({
    23. title,
    24. icon:'none'
    25. })
    26. return
    27. }
    28. context.commit('initInfo',res)
    29. // 存入本地存储
    30. uni.setStorage({
    31. key:'userInfo',
    32. data:res
    33. })
    34. // 返回上一页
    35. uni.navigateBack({
    36. delta:1
    37. })
    38. })
    39. }
    40. }
    41. }

    address-detail.vue

    1. <script>
    2. import {mapState} from 'vuex'
    3. export default {
    4. data() {
    5. return {
    6. regionArr:[
    7. '东城区',
    8. '西城区',
    9. '朝阳区',
    10. '海淀区',
    11. '石景山区',
    12. '大兴区',
    13. '通州区',
    14. '顺义区',
    15. '房山区'
    16. ],
    17. regionIdx:-1
    18. };
    19. },
    20. computed:{
    21. ...mapState({
    22. userInfo:state=>state.user.userInfo
    23. })
    24. },
    25. methods:{
    26. handleRegion(ev){
    27. console.log(ev);
    28. let {value} = ev.detail;
    29. this.regionIdx = value
    30. },
    31. handleSubmit(ev){
    32. let {value} = ev.detail
    33. let {regionArr,regionIdx} = this
    34. value.city = '北京市'
    35. value.region = regionArr[regionIdx]
    36. value.isdefault = false
    37. value.userid = this.userInfo.objectId
    38. this.$store.dispatch('address/addressAddAct',value)
    39. }
    40. }
    41. }
    42. script>
    43. <style lang="scss">
    44. .map{
    45. height: 400upx;
    46. width: 100%;
    47. }
    48. style>

  • 相关阅读:
    1.01_pdf转word项目_项目需求分析和技术架构
    Metabase学习教程:视图-3
    Flume配置3——拦截器过滤
    【英雄哥六月集训】第 30天: 拓扑排序
    基于Python的汉语分词系统
    关于如何编写好金融科技客户端SDK的思考
    MyBatis-Plus之ActiveRecord[基础增删改查操作]
    音视频封装demo:将h264数据和aac数据封装(mux)成TS文件(纯手工,不依赖第三方开源库)
    操作系统期末复习整理知识点
    微服务项目架构演变过程
  • 原文地址:https://blog.csdn.net/gixome/article/details/126834867