Protocol.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <div class="protocol">
  3. <div class="agreeProtocol" v-show="isProtocol">
  4. <slot>
  5. <van-checkbox @click="onChange" :checked-color="checkedColor" v-model="checked" v-if="!exists">
  6. <template #icon="props">
  7. <!-- {{ props.checked }} -->
  8. <img :src="props.checked ? activeButtonIcon : inactiveButtonIcon" >
  9. </template>
  10. </van-checkbox>
  11. <i :style="leftStyle" @click="onClick" v-if="!exists">{{ leftString }}</i>
  12. <i :style="leftStyle" v-if="exists">查看</i>
  13. <span @click="onPopupClose" :style="rightStyle">《{{ rightString }}》</span>
  14. </slot>
  15. </div>
  16. <!-- 协议 -->
  17. <van-popup id="protocolPopup" v-model="popupStatus" position="bottom">
  18. <m-protocol :protocolHTML="protocolHTML" :popupStatus="popupStatus" :checked="checked" :tenantId="tenantId" @onClose="onPopupClose" @onPopupSure="onPopupSure" />
  19. </van-popup>
  20. </div>
  21. </template>
  22. <script>
  23. import MProtocol from './MProtocol'
  24. import {
  25. queryProduceContract,
  26. getContract
  27. } from '@/api/smallWeb'
  28. // import setLoading from '@/utils/loading'
  29. export default {
  30. name: 'mheader',
  31. components: {
  32. MProtocol
  33. },
  34. props: {
  35. value: {
  36. type: Boolean,
  37. default () {
  38. return false
  39. }
  40. },
  41. tenantId: [String, Number], // 机构签署协议
  42. type: {
  43. type: [String, Number],
  44. default: 0
  45. },
  46. musicGroupId: String,
  47. checkedColor: String,
  48. userId: String,
  49. activeIcon: String,
  50. inactiveIcon: String,
  51. leftStyle: String,
  52. leftString: {
  53. type: String,
  54. default () {
  55. return '我已阅读并同意'
  56. }
  57. },
  58. rightStyle: String,
  59. rightString: {
  60. type: String,
  61. default () {
  62. return '产品及服务协议'
  63. }
  64. }
  65. },
  66. data() {
  67. return {
  68. popupStatus: false,
  69. checked: this.value,
  70. exists: true, // 是否已经生成过协议
  71. protocolHTML: null, // 协议内容
  72. fullPath: null, //保存
  73. isProtocol: false // 是否有协议内容
  74. }
  75. },
  76. mounted() {
  77. this.__init()
  78. // window.onhashchange = () => {
  79. // this.popupStatus = false
  80. // }
  81. window.addEventListener('hashchange', this.onHash, false)
  82. },
  83. methods: {
  84. onHash() {
  85. this.popupStatus = false
  86. },
  87. async __init() {
  88. if(this.tenantId) {
  89. try {
  90. const res = await getContract({ id: this.tenantId, type: this.type })
  91. this.protocolHTML = res.data
  92. this.isProtocol = this.protocolHTML ? true : false
  93. this.exists = false
  94. this.checked = false
  95. this.$emit('input', this.exists)
  96. } catch(e) {
  97. //
  98. }
  99. } else {
  100. let params = {
  101. userId: this.userId ? this.userId : null,
  102. musicGroupId: this.musicGroupId ? this.musicGroupId : null
  103. }
  104. const token = sessionStorage.getItem('token')
  105. if(token) {
  106. localStorage.setItem('Authorization', token)
  107. localStorage.setItem('userInfo', token)
  108. }
  109. await queryProduceContract(params).then(res => {
  110. let result = res.data
  111. this.protocolHTML = '' // 重置协议
  112. if (result.code == 200) {
  113. this.protocolHTML = result.data.productContract
  114. this.isProtocol = this.protocolHTML ? true : false
  115. this.exists = result.data.exists
  116. this.checked = this.exists
  117. this.$emit('input', this.exists)
  118. }
  119. })
  120. }
  121. // 如果没有协议内容则不 显示协议
  122. if(!this.protocolHTML) {
  123. this.isProtocol = this.protocolHTML ? true : false
  124. this.checked = true // 默认选中,学生端则不用默认选中
  125. this.$emit('input', this.checked || this.exists)
  126. }
  127. },
  128. // onChange() {
  129. // this.$emit('input', this.checked)
  130. // },
  131. // onClick() {
  132. // this.checked = !this.checked
  133. // this.$emit('input', this.checked)
  134. // },
  135. onChange() {
  136. if(this.tenantId) {
  137. if(this.checked) {
  138. this.checked = false
  139. this.onPopupClose()
  140. } else {
  141. this.$emit('input', false)
  142. }
  143. } else {
  144. this.$emit('input', this.checked)
  145. }
  146. },
  147. onClick() {
  148. if(this.tenantId) {
  149. if(this.checked) {
  150. this.checked = false
  151. this.$emit('input', false)
  152. } else {
  153. this.onPopupClose()
  154. }
  155. } else {
  156. this.checked = !this.checked
  157. this.$emit('input', this.checked)
  158. }
  159. },
  160. onPopupSure() {
  161. this.$emit('input', true)
  162. this.checked = true
  163. this.onPopupClose()
  164. },
  165. onPopupClose() {
  166. // 判断是否有协议内容
  167. if(!this.protocolHTML) {
  168. return
  169. }
  170. this.popupStatus = !this.popupStatus
  171. // 打开弹窗
  172. if(this.popupStatus) {
  173. const route = this.$route
  174. let times = 0
  175. /* eslint-disable */
  176. for(let i in route.query) {
  177. times += 1
  178. }
  179. let fullPath = route.fullPath
  180. const origin = window.location.origin
  181. const url = times > 0 ? '&pto=1' : '?pto=1'
  182. history.pushState("", "", `${origin}/#${fullPath}${url}`)
  183. } else {
  184. window.history.go(-1)
  185. }
  186. let popup = document.querySelector('#protocolPopup')
  187. if (popup) {
  188. popup.scrollTop = 0
  189. }
  190. }
  191. },
  192. watch: {
  193. popupStatus(newValue) {
  194. this.$emit('changeCheck', newValue)
  195. },
  196. value(nowValue) {
  197. if (nowValue) {
  198. this.checked = nowValue
  199. }
  200. }
  201. },
  202. destroyed() {
  203. window.removeEventListener('hashchange', this.onHash)
  204. }
  205. }
  206. </script>
  207. <style lang="less" scoped>
  208. .agreeProtocol {
  209. display: flex;
  210. align-items: center;
  211. color: #333333;
  212. padding: .1rem .16rem;
  213. font-size: .14rem;
  214. line-height: 1;
  215. i {
  216. font-style: normal;
  217. }
  218. .van-checkbox {
  219. padding-right: .08rem;
  220. img {
  221. width: 20px;
  222. height: 20px;
  223. }
  224. }
  225. /deep/.van-checkbox__icon .van-icon {
  226. border-color: #e9eaef;
  227. background-color: #fff;
  228. }
  229. /deep/.van-checkbox__icon--checked .van-icon {
  230. color: #fff;
  231. background-color: #F85043;
  232. border-color: #F85043;
  233. }
  234. span {
  235. color: #01C1B5;
  236. }
  237. }
  238. @media screen and (max-width: 320px) {
  239. .agreeProtocol .van-checkbox img {
  240. width: 20px;
  241. height: 20px;
  242. }
  243. }
  244. </style>