post.js 12 KB

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