post.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // pages/post/post.js
  2. const app = getApp()
  3. const host = app.globalData.host;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. sexArray: ["男","女"], //性别
  10. recordArray: ["初中", "高中或同等学历", "中专或同等学历", "专科或同等学历", "本科或同等学历","研究生以上"], //学历
  11. categoryArray: [{ label: '电工', id: 0 }, { label: '水工', id: 1 }], //作业类别
  12. subjectArray: [{name:'请选择'}], //准操项目
  13. trainArray: [{ name: '新办', intro: '第一次取证;或者以前取证了没有按规定的时间复审、换证' },
  14. { name: '复审', intro: '已取证;复审日期一天都不超最好提前3个月' },
  15. { name: '换证', intro: '有效期满提前三个月' }], //培训类型
  16. receiveArray: ['自取','邮寄(将产生邮寄费用)'], //领取类型
  17. sex:0,
  18. record:0,
  19. category:-1,
  20. subject:-1,
  21. train:-1,
  22. receive:0,
  23. area: ['四川省', '成都市', '锦江区'],
  24. time:'2020-01-01',
  25. price:'0.00',
  26. classList:[],
  27. class_:-1,
  28. class_id:0,
  29. data:{},
  30. type:0,
  31. device_cats:[],
  32. dev:[],
  33. show_dev:false
  34. },
  35. /**
  36. * 生命周期函数--监听页面加载
  37. */
  38. onLoad: function (options) {
  39. this.setData({
  40. type: options.type
  41. })
  42. var sub_list=[],user={}
  43. //获取用户信息
  44. wx.request({
  45. url: host + '/api/wx/authinfo',
  46. header: {
  47. openid: app.globalData.openid
  48. },
  49. success: res => {
  50. user = res.data.data
  51. if (options.type != 2 && user){
  52. this.setData({
  53. name:user.name,
  54. company: user.company,
  55. phone:user.phone,
  56. idno: user.idno,
  57. sex:user.sex=='男'?0:1,
  58. record: this.data.recordArray.indexOf(user.education),
  59. })
  60. }
  61. }
  62. })
  63. wx.request({
  64. url: host + '/api/wx/sujectitem',
  65. success: res => {
  66. sub_list = res.data.data
  67. // options:s_name 分类名称 s_id 分类id class_id 班级id
  68. //新增报名
  69. if (options.type == 1){
  70. //班级列表
  71. wx.request({
  72. url: host + '/api/wx/class/list?signup=1&subject_item0=' + options.s_name,
  73. method: 'GET',
  74. success: res => {
  75. // 过滤分类
  76. let list = sub_list.filter(item => item.id == options.s_id)
  77. this.setData({
  78. categoryArray: list[0].children,
  79. z_type: list[0].name, //提交时拼接subject_item
  80. classList: res.data.data
  81. })
  82. }
  83. })
  84. }
  85. //修改报名
  86. if(options.type == 2){
  87. wx.showLoading({
  88. title: '',
  89. })
  90. wx.request({
  91. url: host+'/api/wx/signup',
  92. method:'GET',
  93. data:{id:options.id},
  94. success:res=>{
  95. wx.hideLoading()
  96. let data = res.data.data, subject_item = data.subject_item.split('|');
  97. let list = sub_list.filter(item => item.name == subject_item[0]);
  98. let categoryArray = list[0].children, subject = 0,
  99. subjectArray = [], category = 0, train=0;
  100. //定位分类下拉选项
  101. for(let i=0;i<categoryArray.length;i++){
  102. if (categoryArray[i].name == subject_item[1]){
  103. category=i
  104. subjectArray = categoryArray[i].children
  105. for(let j=0;j<subjectArray.length;j++){
  106. if (subjectArray[j].name == subject_item[2]){
  107. subject=j
  108. }
  109. }
  110. }
  111. }
  112. for(let i=0;i<this.data.trainArray.length;i++){
  113. if (this.data.trainArray[i].name == data.train_type){
  114. train=i
  115. }
  116. }
  117. //获取班级列表
  118. wx.request({
  119. url: host + '/api/wx/class/list?subject_item0=' + list[0].name,
  120. method: 'GET',
  121. success: res => {
  122. let classes = res.data.data[0].classes, class_=0
  123. for(let i=0;i<classes.length;i++){
  124. if (classes[i].cid == data.class_id){
  125. class_=i
  126. }
  127. }
  128. this.setData({
  129. classList: classes,
  130. class_: class_
  131. })
  132. }
  133. })
  134. this.setData({
  135. z_type: subject_item[0],
  136. sex:data.sex=='男' ? 0 :1,
  137. record: this.data.recordArray.indexOf(data.education),
  138. receive: data.receive_card == '自取' ? 0 : 1,
  139. categoryArray: categoryArray,
  140. subjectArray: subjectArray,
  141. category: category,
  142. subject: subject,
  143. train: train,
  144. area:data.area.split(' '),
  145. device_cats: categoryArray[category].device_cats,
  146. dev: data.device_cats ? data.device_cats.join(' , '):''
  147. })
  148. this.setData({
  149. data: res.data.data
  150. })
  151. }
  152. })
  153. }
  154. //班级报名
  155. if(options.type == 3){
  156. this.setData({
  157. parice_list: options.price.split('_'),
  158. class_id: options.class_id
  159. })
  160. }
  161. }
  162. })
  163. },
  164. getClass(){
  165. console.log()
  166. let category = this.data.category>=0 ? this.data.categoryArray[this.data.category].name:'',
  167. subject = this.data.subject >= 0 ? this.data.subjectArray[this.data.subject].name : ''
  168. wx.request({
  169. url: host + '/api/wx/class/list?signup=1',
  170. method: 'GET',
  171. data:{
  172. subject_item0: this.data.z_type,
  173. subject_item1: category,
  174. subject_item2: subject
  175. },
  176. success: res => {
  177. // 过滤分类
  178. this.setData({
  179. classList: res.data.data
  180. })
  181. }
  182. })
  183. },
  184. sure:function(){
  185. if(this.data.dev==''){
  186. wx.showToast({
  187. title: '请选择设备种类!',
  188. icon: 'none',
  189. duration: 2000
  190. })
  191. return false
  192. }
  193. this.setData({
  194. show_dev: false,
  195. })
  196. },
  197. checkboxChange:function(e){
  198. console.log('checkbox发生change事件,携带value值为:', e.detail.value)
  199. const values = e.detail.value
  200. this.setData({
  201. dev: e.detail.value.join(' , ')
  202. })
  203. },
  204. open:function(){
  205. this.setData({
  206. show_dev: true,
  207. })
  208. },
  209. pickerChange:function(e){
  210. if(e.target.dataset.name == 'sex'){
  211. this.setData({
  212. sex: e.detail.value
  213. })
  214. };
  215. if (e.target.dataset.name == 'record') {
  216. this.setData({
  217. record: e.detail.value
  218. })
  219. };
  220. if (e.target.dataset.name == 'category') {
  221. let list = this.data.categoryArray[e.detail.value].children
  222. this.setData({
  223. category: e.detail.value,
  224. subjectArray: list,
  225. subject:-1,
  226. train:-1,
  227. device_cats: this.data.categoryArray[e.detail.value].device_cats,
  228. })
  229. if (this.data.categoryArray[e.detail.value].device_cats.length>0){
  230. this.setData({
  231. show_dev: true
  232. })
  233. }else{
  234. this.setData({
  235. dev: ''
  236. })
  237. }
  238. this.getClass()
  239. };
  240. if (e.target.dataset.name == 'subject') {
  241. this.setData({
  242. subject: e.detail.value,
  243. train: -1
  244. })
  245. this.getClass()
  246. };
  247. if (e.target.dataset.name == 'time') {
  248. this.setData({
  249. time: e.detail.value
  250. })
  251. };
  252. if (e.target.dataset.name == 'train') {
  253. var price=this.data.price
  254. if(this.data.type == 3){
  255. price = this.data.parice_list[e.detail.value]
  256. }else if(this.data.category>=0 && this.data.subject>=0){
  257. if (e.detail.value == 0){
  258. price = this.data.subjectArray[this.data.subject].price_new
  259. }
  260. if (e.detail.value == 1) {
  261. price = this.data.subjectArray[this.data.subject].price_re
  262. }
  263. if (e.detail.value == 2) {
  264. price = this.data.subjectArray[this.data.subject].price_change
  265. }
  266. }
  267. this.setData({
  268. train: e.detail.value,
  269. price: price
  270. })
  271. };
  272. if (e.target.dataset.name == 'area') {
  273. this.setData({
  274. area: e.detail.value
  275. })
  276. };
  277. if (e.target.dataset.name == 'receive') {
  278. this.setData({
  279. receive: e.detail.value
  280. })
  281. };
  282. if (e.target.dataset.name == 'class_') {
  283. this.setData({
  284. class_: e.detail.value
  285. })
  286. };
  287. },
  288. next:function(e){
  289. var formData = e.detail.value,data=this.data
  290. if (this.data.dev != ''){
  291. formData.device_cats = this.data.dev.split(' , ')
  292. }
  293. if (formData.name == '') {
  294. wx.showToast({
  295. title: '姓名不能为空!',
  296. icon: 'none',
  297. duration: 2000
  298. })
  299. return false
  300. }
  301. if (formData.idno.length < 18) {
  302. wx.showToast({
  303. title: '请填写正确的身份证号',
  304. icon: 'none',
  305. duration: 2000
  306. })
  307. return false
  308. }
  309. if (!(/^1[3456789]\d{9}$/.test(formData.phone))) {
  310. wx.showToast({
  311. title: '请填写正确的手机号',
  312. icon: 'none',
  313. duration: 2000
  314. })
  315. return false
  316. }
  317. if(data.type != 3){
  318. if (this.data.category == -1) {
  319. wx.showToast({
  320. title: '请选择作业类别',
  321. icon: 'none',
  322. duration: 2000
  323. })
  324. return false
  325. }
  326. if (this.data.subject == -1) {
  327. wx.showToast({
  328. title: '请选择准操项目或资格类型',
  329. icon: 'none',
  330. duration: 2000
  331. })
  332. return false
  333. }
  334. // console.log(this.data.class_)
  335. // console.log(this.data.z_type)
  336. if (this.data.classList.length > 0 && this.data.class_<0) {
  337. wx.showToast({
  338. title: '请选择班级',
  339. icon: 'none',
  340. duration: 2000
  341. })
  342. return false
  343. }
  344. formData.subject_item = data.z_type + "|" + data.categoryArray[data.category].name + "|" + data.subjectArray[data.subject].name
  345. formData.subject_id = data.subjectArray[data.subject].id
  346. if (formData.subject_item.indexOf("特种作业人员")==-1){
  347. formData.class_id = data.classList[data.class_].id
  348. }else{
  349. formData.class_id = 0
  350. }
  351. }else{
  352. formData.class_id = data.class_id
  353. }
  354. if (this.data.train == -1) {
  355. wx.showToast({
  356. title: '请选择培训类型',
  357. icon: 'none',
  358. duration: 2000
  359. })
  360. return false
  361. }
  362. formData.sex=data.sexArray[data.sex]
  363. formData.education = data.recordArray[data.record]
  364. formData.receive_card = data.receiveArray[data.receive]
  365. formData.train_type = data.trainArray[data.train].name
  366. formData.price=this.data.price
  367. formData.area=this.data.area.join(' ')
  368. var method = 'POST', post_data = formData
  369. if (this.data.type == 2) {
  370. method = 'PUT'
  371. var new_data=this.data.data
  372. for(let key in formData){
  373. new_data[key]=formData[key]
  374. }
  375. post_data = new_data
  376. }
  377. wx.showLoading({
  378. title: '正在提交',
  379. })
  380. wx.request({
  381. url: host + '/api/wx/signup',
  382. method: method,
  383. header: {
  384. openid: app.globalData.openid
  385. },
  386. data: post_data,
  387. success: res => {
  388. wx.hideLoading()
  389. if(res.data.code == 0){
  390. let id = (data.type == 2) ? new_data.id : res.data.data.id
  391. wx.navigateTo({
  392. url: '../upload/upload?id='+id,
  393. })
  394. }else{
  395. wx.showToast({
  396. title: res.data.message,
  397. icon: 'none',
  398. duration: 2000
  399. })
  400. }
  401. },
  402. fail: res => {
  403. wx.hideLoading()
  404. wx.showToast({
  405. title: res.data.message,
  406. icon: 'none',
  407. duration: 2000
  408. })
  409. }
  410. })
  411. },
  412. /**
  413. * 生命周期函数--监听页面初次渲染完成
  414. */
  415. onReady: function () {
  416. },
  417. /**
  418. * 生命周期函数--监听页面显示
  419. */
  420. onShow: function () {
  421. },
  422. /**
  423. * 生命周期函数--监听页面隐藏
  424. */
  425. onHide: function () {
  426. },
  427. /**
  428. * 生命周期函数--监听页面卸载
  429. */
  430. onUnload: function () {
  431. },
  432. /**
  433. * 页面相关事件处理函数--监听用户下拉动作
  434. */
  435. onPullDownRefresh: function () {
  436. },
  437. /**
  438. * 页面上拉触底事件的处理函数
  439. */
  440. onReachBottom: function () {
  441. },
  442. /**
  443. * 用户点击右上角分享
  444. */
  445. onShareAppMessage: function () {
  446. }
  447. })