post.js 12 KB

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