post.js 11 KB

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