uni-id-pages-x-agreements.uvue 6.0 KB
Newer Older
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
1 2 3
<template>
	<view class="agreements-root" v-if="agreements.length != 0">
		<template v-if="needAgreements">
DCloud_JSON's avatar
DCloud_JSON 已提交
4 5 6 7
			<checkbox-group @change="setAgree" class="checkbox-group">
        <checkbox class="checkbox" :checked="!pendingAgreements" value="agreement">
          <text class="text">同意</text>
        </checkbox>
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
			</checkbox-group>
			<view class="content">
				<view class="agreements-item" v-for="(agreement,index) in agreements" :key="index">
					<text class="agreement text" @click="navigateTo(index)">{{agreement.title}}</text>
					<text class="text and" v-if="hasAnd(index)" space="nbsp"> 和 </text>
				</view>
			</view>
		</template>

		<!-- 弹出式 -->
		<uni-popup ref="popup">
			<view class="popup-content">
				<view class="popup-header">
					<uni-id-pages-x-icons :size="18"  color="#E6A23C" type="advert" />
					<text class="popup-title">请先阅读并同意</text>
				</view>
				<view class="content">
					<view class="agreements-item" v-for="(agreement,index) in agreements" :key="index">
						<text class="agreement popup-text" @click="navigateTo(index)">{{agreement.title}}</text>
						<text class="popup-text and" v-if="hasAnd(index)" space="nbsp"> 和 </text>
					</view>
				</view>
				<view class="btn-group">
					<text class="btn" @click="cancel">取消</text>
					<text class="btn confirm" @click="confirm">同意</text>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
  import config from '@/uni_modules/uni-id-pages-x/config.uts';
  import {state} from '@/uni_modules/uni-id-pages-x/store.uts';
  type Agreement = {url:string,title:string};
  let confirmCallBack = ():void=>{} //console.log('未传入回调函数,value:')
  export default {
    name: "uni-id-pages-x-agreements",
    computed: {
    	agreements():Agreement[]{
A
Anne_LXM 已提交
48
        const agreements = config.getJSON('agreements')
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    		if(agreements === null){
    			return []
    		}else{
          return [
          	{
          		"url":agreements.getString('serviceUrl') as string,
          		"title":"用户服务协议"
          	},
          	{
          		"url":agreements.getString('privacyUrl') as string,
          		"title":"隐私政策条款"
          	}
          ] as Agreement[]
        }
    	},
      pendingAgreements():boolean{
        return state.pendingAgreements
      }
    },
    props: {
      scope: {
        type: String,
        default: "login"
      },
    },
    data() {
      return {
        needAgreements:true,
A
Anne_LXM 已提交
77
        popupComponent:null as null | UniPopupComponentPublicInstance
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
78 79 80
      }
    },
    mounted() {
A
Anne_LXM 已提交
81

DCloud_JSON's avatar
init  
DCloud_JSON 已提交
82 83 84 85 86 87 88
      const scopeList = config.getArray<string>('agreements.scopeList')
      if(scopeList == null){
        this.needAgreements = false
      }else{
        this.needAgreements = scopeList.includes(this.scope)
      }
      state.pendingAgreements = this.needAgreements;
A
Anne_LXM 已提交
89 90

      this.popupComponent = (this.$refs['popup'] as UniPopupComponentPublicInstance)
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
      // this.showPopupAgreements(()=>{
      //   console.log('showPopupAgreements after');
      // })
      uni.$on("uni-id-pages-x-agreements-shake",()=>{
        // console.log('弹出申请同意隐私协议的框');
        (this.$refs['popup-box'] as INode).style.setProperty("top",0)
      })
    },
    methods: {
      hasAnd(index:number) : boolean{
      	return this.agreements.length - 1 > index
      },
      navigateTo(index:number){
        const title = this.agreements[index].title
        const url = this.agreements[index].url
        uni.navigateTo({
          "url":"/uni_modules/uni-id-pages-x/pages/common/webview/webview?url="+url+"&title="+title,
          fail:e=>{
            console.error(e);
          }
        })
      },
      setAgree(event: CheckboxGroupChangeEvent){
        state.pendingAgreements =  event.detail.value.length == 0
      },
      showPopupAgreements(callback: () => void){
        confirmCallBack = callback;
A
Anne_LXM 已提交
118
        this.popupComponent!.open()
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
119 120 121
      },
      cancel(){
        state.pendingAgreements = true;
A
Anne_LXM 已提交
122
        this.popupComponent!.close()
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
123 124 125 126 127
      },
      confirm(){
        state.pendingAgreements = false;
        // console.log('state.pendingAgreements',state.pendingAgreements);
        confirmCallBack();
A
Anne_LXM 已提交
128
        this.popupComponent!.close()
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141
      }
    },
  }
</script>

<style lang="scss" scoped>
	.agreements-root {
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		margin-left: -9px;
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
142 143 144 145
  .checkbox-group {
    transform: scale(0.6);
    margin-left: -6px;
    margin-right: -18px;
146 147 148
    /* #ifdef WEB */
    margin-right: -30px;
    /* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    padding-right: 10px;
    .checkbox {
      margin-left: 5px;
      padding-right: 15px;
      .text {
        margin-left: 8px;
        margin-right: 10px;
        transform: scale(1.8);
      	color: #8a8f8b;
      	font-size: 12px;
        line-height: 24px;
      }
    }

    /* #ifdef MP-WEIXIN */
    // 解决微信小程序中外层有transform: scale内层再设置transform: scale无效的问题
    .checkbox {
      padding-right: 40px;
      .text {
        position: absolute;
        margin-right: 0;
        top: 0;
      }
    }
    /* #endif */
  }
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	.agreements-item {
		flex-direction: row;
	}

	.popup-text{
		line-height: 30px;
		font-size:14px;
	}

	.agreement {
		color: #04498c;
		/* #ifdef WEB */
		cursor: pointer;
		/* #endif */
	}

	.content {
		padding: 5px 0;
		flex-wrap: wrap;
		flex-direction: row;
	}

	.popup-content {
		background-color: #FFF;
		padding-bottom: 0;
		border-radius: 10px;
		width: 600rpx;
    padding: 15px;
204
    padding-bottom: 0;
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	}
	.popup-header {
		display: flex;
		flex-direction: row;
		align-items: center;
		padding-top: 5px;
	}
	.popup-title {
		font-size: 16px;
		// text-align: center;
		margin: 15px 0;
		padding-left: 5px;
		// color: #E6A23C;
	}

	.popup-content .btn-group {
		border-top: 1px solid #eee;
		flex-direction: row;
		justify-content: space-around;
		margin-top: 10px;
	}

	.popup-content .btn-group .btn {
		flex: 1;
		text-align: center;
		color: #666;
		height: 45px;
		line-height: 45px;
		font-size: 14px;
	}

	.popup-content .btn-group .btn.confirm {
		color: #115ff7;
		border-left: solid 1px #eee;
	}
A
Anne_LXM 已提交
240
</style>