post.js 11 KB

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