post.js 13 KB

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