makeOut.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. <template>
  2. <view class="makeOut">
  3. <view class="content signUp-info">
  4. <view class="title">
  5. {{activity.activityName}}
  6. </view>
  7. <view class="time">
  8. 活动时间:{{activity.activityStartTime}} 至 {{activity.activityEndTime}}
  9. </view>
  10. <view class="signUp-box">
  11. <view class="label-box">
  12. <view class="label">
  13. 报名人数
  14. </view>
  15. <view class="desc">
  16. {{activity.limited?`(最多${activity.limited}人)`:'不限制'}}
  17. </view>
  18. </view>
  19. <view class="num-box">
  20. <view class="btn" :class="[num == 1?'disabled':'']" @click="add(-1)">
  21. -
  22. </view>
  23. <view class="num">
  24. {{num}}
  25. </view>
  26. <view class="btn" :class="[num == activity.limited?'disabled':'']" @click="add(1)">
  27. +
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. <view class="content" v-for="(test,d) in modelQuery">
  33. <u--form
  34. labelPosition="left"
  35. :model="modelQuery[d]"
  36. :rules="rules"
  37. :ref="'uForm'+d"
  38. labelWidth="150rpx"
  39. >
  40. <template v-for="(item,index) in setQuery.data">
  41. <u-form-item
  42. :label="item.dataName"
  43. :prop="'data_'+index+'_value'"
  44. :borderBottom="index != (setQuery.data.length-1)"
  45. :required="item.requiredEnable == 1?true:false"
  46. ref="item1"
  47. >
  48. <u--input v-if="item.dataType == 1" :placeholder="'请选择'+item.dataName" :maxlength="getLength(item)" v-model="modelQuery[d][`data_${index}_value`]" border="none"></u--input>
  49. <u-textarea v-else-if="item.dataType == 2" v-model="modelQuery[d][`data_${index}_value`]" border="surround" :maxlength="400" count ></u-textarea>
  50. <u-radio-group v-model="modelQuery[d][`data_${index}_value`]" v-else-if="item.dataType == 3">
  51. <u-radio labelSize="34rpx" v-for="i in item.content" :key="i" :name="i" :label="i"></u-radio>
  52. </u-radio-group>
  53. <u-checkbox-group
  54. v-model="modelQuery[d][`data_${index}_value`]"
  55. v-else-if="item.dataType == 4"
  56. >
  57. <u-checkbox
  58. labelSize="34rpx"
  59. iconSize="34rpx"
  60. v-for="i in item.content"
  61. :key="index"
  62. :label="i"
  63. :name="i"
  64. >
  65. </u-checkbox>
  66. </u-checkbox-group>
  67. <u-upload
  68. width="200rpx"
  69. height="200rpx"
  70. v-else-if="item.dataType == 5"
  71. :fileList="modelQuery[d][`data_${index}_value`]"
  72. @afterRead="afterRead($event,d,index)"
  73. @delete="deletePic($event,modelQuery[d][`data_${index}_value`])"
  74. @oversize="oversize($event,item)"
  75. name="1"
  76. :maxSize="item.size*1024*1024"
  77. multiple
  78. :maxCount="item.num"
  79. ></u-upload>
  80. </u-form-item>
  81. </template>
  82. </u--form>
  83. </view>
  84. <view class="btn-box">
  85. <view class="left">
  86. <view class="num">
  87. 共{{num}}人
  88. </view>
  89. <view class="total">
  90. 合计 <view class="price-box">
  91. <view class="unit">
  92. </view>
  93. <view class="price">
  94. {{total}}
  95. </view>
  96. </view>
  97. </view>
  98. </view>
  99. <button class="btn" @click="handleSignUp" :loading="loading">
  100. 立即报名
  101. </button>
  102. </view>
  103. </view>
  104. </template>
  105. <script>
  106. import {getTemplateDetail,signUp,createOrder,calc} from '@/api/activity.js'
  107. import {upload} from '@/utils/upload.js'
  108. import {
  109. creatPayOrder,
  110. queryPayOrder,
  111. payDetails
  112. } from '@/api/payment.js'
  113. export default {
  114. data() {
  115. return {
  116. maxLength:40,
  117. num: 1,
  118. modelQuery:[{}],
  119. setQuery:{},
  120. activity:{},
  121. rules: {},
  122. loading:false,
  123. price:0,
  124. query: {
  125. "msgType": "wx.unifiedOrder",
  126. "orderDesc": "",
  127. "orderNo": "",
  128. "subOpenId": "",
  129. "userId": ""
  130. },
  131. // 支付信息
  132. payData: {}
  133. }
  134. },
  135. computed: {
  136. total() {
  137. return this.price || this.activity.price
  138. }
  139. },
  140. methods: {
  141. oversize(val,item){
  142. console.log(val,item);
  143. uni.showToast({
  144. title:`请上传${item.size}MB以内的图片`,
  145. icon:'none'
  146. })
  147. },
  148. getLength(item){
  149. if(item.validType == 1){
  150. return 11
  151. }else if(item.validType == 2){
  152. return 18
  153. }else{
  154. return 40
  155. }
  156. },
  157. add(val){
  158. if((val==-1&&this.num == 1)||(val==1&&this.num == this.activity.limited)) return
  159. this.num += val
  160. if(val == 1){
  161. let obj = {}
  162. this.setQuery.data.map((item,index)=>{
  163. if(item.dataType == 1){
  164. // this.$set(this.modelQuery[this.modelQuery.length], `data_${index}_value`,'')
  165. obj[`data_${index}_value`] = ''
  166. }else if(item.dataType == 2 ){
  167. // this.$set(this.modelQuery[this.modelQuery.length], `data_${index}_value`,'')
  168. obj[`data_${index}_value`] = ''
  169. }
  170. else if(item.dataType == 3 ){
  171. // this.$set(this.modelQuery[this.modelQuery.length], `data_${index}_value`,'')
  172. obj[`data_${index}_value`] = ''
  173. }
  174. else if(item.dataType == 4 ){
  175. // this.$set(this.modelQuery[this.modelQuery.length], `data_${index}_value`,[])
  176. obj[`data_${index}_value`] = []
  177. }else if( item.dataType == 5){
  178. // this.$set(this.modelQuery[this.modelQuery.length], `data_${index}_value`,[])
  179. obj[`data_${index}_value`] = []
  180. }
  181. })
  182. this.modelQuery.push(obj)
  183. this.$nextTick(() => {
  184. this.$refs['uForm'+(this.modelQuery.length-1)][0].setRules(this.rules);
  185. })
  186. }else{
  187. this.modelQuery.pop()
  188. }
  189. // 计算价格
  190. calc({activityId:this.activity.id,num:this.num}).then(res=>{
  191. if (res.state == 'Success') {
  192. this.price = res.content.price
  193. }
  194. })
  195. },
  196. getTemplateDetail(id){
  197. getTemplateDetail({id}).then(res=>{
  198. if (res.state == 'Success') {
  199. this.setQuery = res.content
  200. this.setQuery.data.map((item,index)=>{
  201. if(item.dataType == 1){
  202. // this.$set(item,'value','')
  203. this.$set(this.modelQuery[0], `data_${index}_value`,'')
  204. if(item.requiredEnable == 1){
  205. let obj; //校验规则
  206. if(item.validType == 0){
  207. console.log(11111);
  208. obj = {
  209. required: true,
  210. message: `请输入${item.dataName}`,
  211. trigger: ['blur']
  212. }
  213. }else if(item.validType == 1){
  214. console.log(22222);
  215. obj ={
  216. pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
  217. message: '请输入正确的手机号'
  218. }
  219. }else if(item.validType == 2){
  220. obj ={
  221. len:18,
  222. message: '请输入正确的身份证号'
  223. }
  224. }else if(item.validType == 3){
  225. obj ={
  226. pattern: /^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@"+"[A-Za-z0-9-]/,
  227. // 正则检验前先将值转为字符串
  228. // transform(value) {
  229. // return String(value);
  230. // },
  231. message: '请输入正确的邮箱'
  232. }
  233. }else if(item.validType == 4){
  234. obj ={
  235. pattern: /\\d+/,
  236. message: '只能输入数字'
  237. }
  238. }
  239. this.$set(this.rules, `data_${index}_value`, [{
  240. required: true,
  241. message: `请输入${item.dataName}`,
  242. trigger: ['blur']
  243. },obj])
  244. }
  245. }else if(item.dataType == 2 ){
  246. // this.$set(item,'value','')
  247. this.$set(this.modelQuery[0], `data_${index}_value`,'')
  248. if(item.requiredEnable == 1){
  249. this.$set(this.rules, `data_${index}_value`, [{
  250. required: true,
  251. message: `请输入${item.dataName}`,
  252. trigger: ['blur']
  253. }])
  254. }
  255. }
  256. else if(item.dataType == 3 ){
  257. // this.$set(item,'value','')
  258. this.$set(this.modelQuery[0], `data_${index}_value`,'')
  259. if(item.requiredEnable == 1){
  260. this.$set(this.rules, `data_${index}_value`, [{
  261. required: true,
  262. message: `请选择${item.dataName}`,
  263. trigger: ['blur','change']
  264. }])
  265. }
  266. }
  267. else if(item.dataType == 4 ){
  268. // this.$set(item,'value',[])
  269. this.$set(this.modelQuery[0], `data_${index}_value`,[])
  270. if(item.requiredEnable == 1){
  271. this.$set(this.rules, `data_${index}_value`, [{
  272. type: 'array',
  273. required: true,
  274. message: `请选择${item.dataName}`,
  275. trigger: ['change']
  276. }])
  277. }
  278. }else if( item.dataType == 5){
  279. // this.$set(item,'value',[])
  280. this.$set(this.modelQuery[0], `data_${index}_value`,[])
  281. if(item.requiredEnable == 1){
  282. this.$set(this.rules, `data_${index}_value`, [{
  283. type: 'array',
  284. required: true,
  285. message: `请上传${item.dataName}`,
  286. trigger: ['change']
  287. }])
  288. }
  289. }
  290. })
  291. this.$nextTick(() => {
  292. console.log( this.$refs.uForm0);
  293. this.$refs.uForm0[0].setRules(this.rules);
  294. })
  295. }
  296. })
  297. },
  298. // 删除图片
  299. deletePic(event,data) {
  300. data.splice(event.index, 1)
  301. },
  302. // 新增图片
  303. async afterRead(event,d,index) {
  304. console.log(event);
  305. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  306. let lists = [].concat(event.file)
  307. let fileListLen = this.modelQuery[d][`data_${index}_value`].length
  308. lists.map((item) => {
  309. console.log(this.setQuery.data[index],this.setQuery.data[index].dataName);
  310. this.modelQuery[d][`data_${index}_value`].push({
  311. ...item,
  312. status: 'uploading',
  313. message: '上传中'
  314. })
  315. })
  316. for (let i = 0; i < lists.length; i++) {
  317. const result = await this.uploadFilePromise(lists[i])
  318. let item = this.modelQuery[d][`data_${index}_value`][fileListLen]
  319. this.modelQuery[d][`data_${index}_value`].splice(fileListLen, 1, Object.assign(item, {
  320. status: 'success',
  321. message: '',
  322. url: result
  323. }))
  324. fileListLen++
  325. }
  326. },
  327. uploadFilePromise(file) {
  328. return new Promise((resolve, reject) => {
  329. upload(file,'SHOP_INFO').then(res=>{
  330. // this.form[key] = [{url:res}]
  331. resolve(res)
  332. })
  333. })
  334. },
  335. formatData(){
  336. console.log(this.modelQuery);
  337. },
  338. // 点击提交报名
  339. handleSignUp(){
  340. let obj = {
  341. "activitySignupId": this.activity.id,
  342. "dataInfos": [],
  343. "num": this.num,
  344. "userId": JSON.parse(uni.getStorageSync('userInfo')).userId
  345. }
  346. let arr = []
  347. new Promise((resolve,reject)=>{
  348. this.modelQuery.map((item,index)=>{
  349. let personData = {data:[]}//每个人的填写数据
  350. // 循环校验表单
  351. this['a'+index] = new Promise((resolve1,reject1)=>{
  352. this.$refs['uForm' + index][0].validate().then(res => {
  353. // 把每个人的资料全部放在personData的data属性里面 依次排列
  354. let dataItem
  355. for(let key in item){
  356. let i = key.split('_')[1]
  357. console.log(item,i,this.setQuery.data[i]);
  358. dataItem = JSON.parse(JSON.stringify(this.setQuery.data[i]))
  359. // 值是数组直接赋值
  360. if(Array.isArray(item[key])){
  361. if(dataItem.dataType == 5){//取出图片里面的地址
  362. dataItem.content = [item[key][0].url]
  363. }else{
  364. dataItem.content = item[key]
  365. }
  366. }else{
  367. dataItem.content = [item[key]]
  368. }
  369. personData.data.push(dataItem)
  370. }
  371. resolve1(1)
  372. }).catch(errors => {
  373. console.log(errors);
  374. })
  375. })
  376. obj.dataInfos.push(personData)
  377. // 校验的promise数组 用于获取全部校验成功
  378. arr.push(this['a'+index])
  379. })
  380. // 全部校验成功
  381. Promise.all(arr).then(res=>{
  382. console.log('结果',res);
  383. resolve()
  384. }).catch(e=>{
  385. console.log('报错',e);
  386. })
  387. }).then(()=>{
  388. console.log(this.modelQuery,this.setQuery,obj)
  389. this.signUp(obj).then(id=>{
  390. this.creat(id)
  391. })
  392. })
  393. },
  394. signUp(obj){
  395. return new Promise((resolve,reject)=>{
  396. this.loading = true
  397. signUp(obj).then(res=>{
  398. this.loading = false
  399. if (res.state == 'Success') {
  400. resolve(res.content.serviceId)
  401. // uni.hideLoading()
  402. // uni.showToast({
  403. // title:'报名成功',
  404. // icon:'success'
  405. // })
  406. // uni.reLaunch({
  407. // url: '/my/order/index'
  408. // })
  409. }
  410. })
  411. })
  412. },
  413. //创建订单
  414. creat(serviceId) {
  415. if (this.loading) return
  416. this.loading = true
  417. uni.showLoading({
  418. title: '支付中'
  419. })
  420. let that = this
  421. if (!this.payData.timeStamp) {
  422. createOrder({
  423. activityId: this.activity.id,
  424. serviceId,
  425. num:this.num,
  426. userId: JSON.parse(uni.getStorageSync('userInfo')).userId
  427. }).then(res => {
  428. this.loading = false
  429. if (res.state == 'Success') {
  430. if (!this.activity.price) { //价格为0
  431. uni.hideLoading()
  432. uni.reLaunch({
  433. url: '/my/order/index'
  434. })
  435. } else {
  436. this.query.orderNo = res.content.orderNo
  437. this.query.subOpenId = JSON.parse(uni.getStorageSync('userInfo')).openId
  438. this.query.orderDesc = this.activity.activityName
  439. creatPayOrder(this.query).then(data => {
  440. that.payData = JSON.parse(data.content.miniPayRequest)
  441. if (data.content.miniPayRequest == null) return uni.hideLoading()
  442. uni.requestPayment({
  443. "provider": "wxpay",
  444. "orderInfo": that.payData,
  445. "appid": that.payData
  446. .appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  447. "paySign": that.payData.paySign,
  448. "nonceStr": that.payData.nonceStr, // 随机字符串
  449. "package": that.payData.package, // 固定值
  450. // "prepayid": that.payData.package, // 统一下单订单号
  451. "timeStamp": that.payData.timeStamp, // 时间戳(单位:秒)
  452. "signType": that.payData.signType, //签名算法
  453. success(msg) {
  454. console.log('msg', msg, res);
  455. queryPayOrder(that.query.orderNo).then(res1 => {
  456. if (res1.state == 'Success') {
  457. uni.hideLoading()
  458. uni.reLaunch({
  459. url: '/my/order/index'
  460. })
  461. }
  462. })
  463. },
  464. fail(e) {
  465. console.log('err', e);
  466. that.loading = false
  467. uni.hideLoading()
  468. uni.showToast({
  469. title: '取消支付',
  470. icon: 'fail'
  471. })
  472. // 取消支付后,获取支付信息以备再次支付
  473. payDetails(that.query.orderNo).then(r => {
  474. if (r.state == 'Success') {
  475. that.payData = JSON.parse(r.content.miniPayRequest)
  476. }
  477. })
  478. }
  479. })
  480. })
  481. }
  482. }
  483. })
  484. } else { // 取消支付后再次支付
  485. uni.requestPayment({
  486. "provider": "wxpay",
  487. "orderInfo": that.payData,
  488. "appid": that.payData.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  489. "paySign": that.payData.paySign,
  490. "nonceStr": that.payData.nonceStr, // 随机字符串
  491. "package": that.payData.package, // 固定值
  492. // "prepayid": that.payData.package, // 统一下单订单号
  493. "timeStamp": that.payData.timeStamp, // 时间戳(单位:秒)
  494. "signType": that.payData.signType, //签名算法
  495. success(msg) {
  496. console.log('msg', msg);
  497. queryPayOrder(that.query.orderNo).then(res1 => {
  498. if (res1.state == 'Success') {
  499. uni.hideLoading()
  500. uni.reLaunch({
  501. url: '/my/order/index'
  502. })
  503. }
  504. })
  505. },
  506. fail(e) {
  507. that.loading = false
  508. uni.hideLoading()
  509. uni.showToast({
  510. title: '取消支付',
  511. icon: 'fail'
  512. })
  513. // 取消支付后,获取支付信息以备再次支付
  514. payDetails(that.query.orderNo).then(r => {
  515. if (r.state == 'Success') {
  516. that.payData = JSON.parse(r.content.miniPayRequest)
  517. }
  518. })
  519. console.log('err', e);
  520. }
  521. })
  522. }
  523. }
  524. },
  525. onLoad(options) {
  526. let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  527. this.query.userId = userInfo.userId
  528. this.activity = JSON.parse(uni.getStorageSync('activity'))
  529. this.getTemplateDetail(options.id)
  530. }
  531. }
  532. </script>
  533. <style lang="scss" >
  534. .makeOut{
  535. background: #F9F9F9;
  536. min-height: 100vh;
  537. padding-top: 20rpx;
  538. padding-bottom: 150rpx;
  539. .content{
  540. margin: 0 24rpx 20rpx;
  541. padding: 28rpx 24rpx;
  542. background: #fff;
  543. box-sizing: border-box;
  544. border-radius: 16rpx;
  545. }
  546. .signUp-info{
  547. .title{
  548. font-weight: 600;
  549. font-size: 32rpx;
  550. color: #181818;
  551. overflow: hidden;
  552. text-overflow: ellipsis;
  553. /* 弹性伸缩盒子模型显示 */
  554. display: -webkit-box;
  555. /* 限制在一个块元素显示的文本的行数 */
  556. -webkit-line-clamp: 2;
  557. /* 设置或检索伸缩盒对象的子元素的排列方式 */
  558. -webkit-box-orient: vertical;
  559. }
  560. .time{
  561. font-weight: 300;
  562. font-size: 24rpx;
  563. color: #AAAAAA;
  564. padding: 20rpx 0;
  565. border-bottom: 1rpx solid #F0F0F0;
  566. }
  567. .signUp-box{
  568. display: flex;
  569. justify-content: space-between;
  570. align-items: center;
  571. padding-top: 20rpx;
  572. .label-box{
  573. display: flex;
  574. align-items: center;
  575. .label{
  576. font-weight: 300;
  577. font-size: 24rpx;
  578. color: #181818;
  579. }
  580. .desc{
  581. font-weight: 300;
  582. font-size: 24rpx;
  583. color: #AAAAAA;
  584. }
  585. }
  586. .num-box{
  587. display: flex;
  588. align-items: center;
  589. .btn.disabled{
  590. background: #F0F0F0;
  591. color: #AAAAAA;
  592. }
  593. .btn{
  594. width: 36rpx;
  595. height: 36rpx;
  596. line-height: 36rpx;
  597. border-radius: 50%;
  598. background: #FFEDEB;
  599. color: $uni-color-primary;
  600. font-size: 30rpx;
  601. text-align: center;
  602. vertical-align: top;
  603. }
  604. .num{
  605. font-weight: 400;
  606. font-size: 24rpx;
  607. color: #222222;
  608. width: 50rpx;
  609. text-align: center;
  610. }
  611. }
  612. }
  613. }
  614. .btn-box {
  615. position: fixed;
  616. bottom: 0%;
  617. left: 0%;
  618. width: 100%;
  619. display: flex;
  620. align-items: center;
  621. justify-content: space-between;
  622. box-sizing: border-box;
  623. padding: 10rpx 24rpx env(safe-area-inset-bottom);
  624. background: #fff;
  625. border-top: 1rpx solid #EEEEEE;
  626. .left{
  627. display: flex;
  628. align-items: flex-end;
  629. .num{
  630. font-weight: 300;
  631. font-size: 24rpx;
  632. color: #AAAAAA;
  633. }
  634. .total{
  635. font-weight: 300;
  636. font-size: 24rpx;
  637. color: #222222;
  638. display: flex;
  639. align-items: flex-end;
  640. margin-left: 6rpx;
  641. .price-box{
  642. display: flex;
  643. align-items: flex-end;
  644. font-weight: 400;
  645. font-size: 32rpx;
  646. color: #222222;
  647. .unit{
  648. font-size: 20rpx;
  649. }
  650. }
  651. }
  652. }
  653. .btn {
  654. width: 280rpx;
  655. height: 80rpx;
  656. line-height: 80rpx;
  657. text-align: center;
  658. background: $uni-color-primary;
  659. border-radius: 40rpx;
  660. font-weight: 600;
  661. font-size: 28rpx;
  662. color: #FFFFFF;
  663. margin: 0;
  664. }
  665. }
  666. .u-form-item{
  667. display: flex;
  668. justify-content: flex-start;
  669. .u-form-item__body__left{
  670. align-self: flex-start !important;
  671. }
  672. .u-radio,.u-checkbox{
  673. margin-right: 20rpx;
  674. }
  675. .u-radio__icon-wrap--circle,.u-checkbox__icon-wrap--square{
  676. width: 28rpx!important;
  677. height: 28rpx!important;
  678. }
  679. }
  680. }
  681. </style>