post.js 13 KB

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