student_certification.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. var app = getApp();
  2. Page({
  3. data: {
  4. name:'',
  5. xuehao:'',
  6. school:'',
  7. time:'',
  8. imgs:[],
  9. imgarr:[],
  10. index:0
  11. },
  12. onLoad: function (options) {
  13. },
  14. select:function(e){
  15. this.setData({index:e.currentTarget.dataset.index})
  16. },
  17. name:function(e){
  18. this.setData({name:e.detail.value})
  19. },
  20. xuehao:function(e){
  21. this.setData({xuehao:e.detail.value})
  22. },
  23. school:function(e){
  24. this.setData({school:e.detail.value})
  25. },
  26. time:function(e){
  27. this.setData({time:e.detail.value})
  28. },
  29. submit:function(){
  30. var that = this;
  31. console.log(that.data.imgarr)
  32. var nameReg = /^[\u4E00-\u9FA5]{2,4}$/;
  33. var reg=/^[\u0391-\uFFE5]+$/;
  34. var dateFormat =/^(\d{4})-(\d{2})$/;
  35. if(!that.data.name){
  36. wx.showToast({
  37. title: '请输入姓名',
  38. icon:'none'
  39. })
  40. return;
  41. }else if(!nameReg.test(that.data.name)){
  42. wx.showToast({
  43. title: '请输入正确姓名',
  44. icon:'none'
  45. })
  46. } else if(!that.data.xuehao){
  47. wx.showToast({
  48. title: that.data.index==0 ? '请输入学号':'请输入工号',
  49. icon:'none'
  50. })
  51. return;
  52. }else if(!that.data.school && that.data.index==0){
  53. wx.showToast({
  54. title: '请输入学校',
  55. icon:'none'
  56. })
  57. return;
  58. }else if(!reg.test(that.data.school) && that.data.index==0){
  59. wx.showToast({
  60. title: '请输入正确学校名称',
  61. icon:'none'
  62. })
  63. return;
  64. }else if(!that.data.time && that.data.index==0){
  65. wx.showToast({
  66. title: '请输入入学时间',
  67. icon:'none'
  68. })
  69. return;
  70. }else if(!dateFormat.test(that.data.time) && that.data.index==0){
  71. wx.showToast({
  72. title: '入学时间输入有误',
  73. icon:'none'
  74. })
  75. return;
  76. }else if(that.data.imgarr.length<1){
  77. wx.showToast({
  78. title: '上传证件照片',
  79. icon:'none'
  80. })
  81. return;
  82. }else{
  83. var data = {
  84. 'name':that.data.name,
  85. 'no':that.data.xuehao,
  86. 'type':that.data.index,
  87. 'year':that.data.time,
  88. 'imgs': that.data.imgarr,
  89. 'school':that.data.school
  90. }
  91. console.log(data)
  92. app.request('/user/student', data, 'POST').then(res => {
  93. console.log(res)
  94. if(res.statusCode==200){
  95. wx.navigateTo({
  96. url: '../student_success/student_success',
  97. })
  98. }
  99. })
  100. }
  101. },
  102. upload:function(){
  103. var that = this;
  104. wx.chooseImage({
  105. count: 1, // 默认9
  106. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  107. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  108. success: function (res) {
  109. console.log(res)
  110. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  111. var tempFilePaths = res.tempFilePaths;
  112. that.setData({
  113. imgs: tempFilePaths
  114. })
  115. that.uploadimg({
  116. url: app.globalData.url + '/upload/image',
  117. path: tempFilePaths
  118. });
  119. }
  120. })
  121. },
  122. xieyi:function(){
  123. wx.navigateTo({
  124. url: '/pages/personal/privacy/privacy',
  125. })
  126. },
  127. uploadimg: function (data) {
  128. wx.showLoading({
  129. title: '上传中...',
  130. mask:true
  131. })
  132. var that = this,
  133. i = data.i ? data.i : 0, //当前上传的哪张图片
  134. success = data.success ? data.success : 0, //上传成功的个数
  135. fail = data.fail ? data.fail : 0; //上传失败的个数
  136. wx.uploadFile({
  137. url: data.url,
  138. filePath: data.path[i],
  139. header: {
  140. 'content-type': 'application/x-www-form-urlencoded',
  141. 'Authorization': wx.getStorageSync('token'),
  142. 'merchant-id': app.globalData.merchant_id
  143. },
  144. name: 'file', //这里根据自己的实际情况改
  145. formData: {
  146. type: 'auth'
  147. }, //这里是上传图片时一起上传的数据
  148. success: (resp) => {
  149. success++; //图片上传成功,图片上传成功的变量+1
  150. console.log(resp)
  151. var data = JSON.parse(resp.data)
  152. // var photo1 = [];
  153. // photo1.push(data.id)
  154. that.setData({
  155. imgarr: data.id
  156. })
  157. wx.hideLoading()
  158. //这里可能有BUG,失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时,这里的success才+1
  159. },
  160. fail: (res) => {
  161. fail++; //图片上传失败,图片上传失败的变量+1
  162. console.log('fail:' + i + "fail:" + fail);
  163. },
  164. complete: () => {
  165. i++; //这个图片执行完上传后,开始上传下一张
  166. if (i == data.path.length) { //当图片传完时,停止调用
  167. //console.log('执行完毕');
  168. //console.log('成功:' + success + " 失败:" + fail);
  169. } else { //若图片还没有传完,则继续调用函数
  170. //console.log(i);
  171. data.i = i;
  172. data.success = success;
  173. data.fail = fail;
  174. that.uploadimg(data);
  175. }
  176. }
  177. });
  178. },
  179. onReady: function () {
  180. },
  181. onShow: function () {
  182. },
  183. onHide: function () {
  184. },
  185. onUnload: function () {
  186. },
  187. onPullDownRefresh: function () {
  188. },
  189. onReachBottom: function () {
  190. },
  191. onShareAppMessage: function () {
  192. }
  193. })