index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. const app = getApp();
  2. const util = require("../../../utils/util.js")
  3. Page({
  4. data: {
  5. ctx: false,
  6. hidden: true,
  7. showcamara: false,
  8. type:'card',
  9. phone:'',
  10. studyUser :{
  11. cardId:"",
  12. cardImg:"",
  13. nickname:"",
  14. phone:'',
  15. openid:'',
  16. identify:0,
  17. session_key:'',
  18. cardBackImg:""
  19. }
  20. },
  21. wxLogin(){
  22. let that = this
  23. wx.login({
  24. success(wxres) {
  25. if( !wxres.code){
  26. util.showMsg("登入失败")
  27. }else{
  28. that.doLoginByCode( wxres.code )
  29. }
  30. }
  31. })
  32. },
  33. doLoginByCode( code ){
  34. app.formPost( "Auth.WxLoginByCode", {code}).then( (res)=>{
  35. if( res.code == 200){
  36. this.setData( {userInfo: res.data})
  37. app.setUserInfo( res.data )
  38. wx.navigateBack({
  39. delta: 1
  40. })
  41. }
  42. })
  43. },
  44. doIdentify( e ){
  45. let that = this
  46. wx.login({
  47. success(wxres) {
  48. if (wxres.code) {
  49. that.doRegister( wxres.code, e.detail.value )
  50. }
  51. }
  52. })
  53. },
  54. doRegister( code, info ){
  55. let param = {
  56. code: code,
  57. phone: info.phone,
  58. cardId: info.cardId,
  59. nickname: info.nickname,
  60. }
  61. app.formPost( 'Auth.Register', param).then( res =>{
  62. if( res.code == 200 ){
  63. let studyUser= res.data
  64. app.setUserInfo( studyUser )
  65. util.showSuccess("登入成功")
  66. this.setData({studyUser})
  67. }else{
  68. util.showMsg( res.msg )
  69. }
  70. })
  71. },
  72. onReady: function (res) {
  73. var ctx = wx.createCameraContext('myCamera')
  74. this.setData({ ctx: ctx })
  75. },
  76. previewImage: function (e) {
  77. var current = e.target.dataset.src;
  78. wx.previewImage({
  79. current: current,
  80. urls: [current]
  81. })
  82. } ,
  83. takeCard(){
  84. this.setData( {showcamara:true, type:'card'})
  85. },
  86. takeCardBack(){
  87. this.setData( {showcamara:true, type:'cardBack'})
  88. },
  89. identify( err, {url} ){
  90. if( err != 200 || !url ) {
  91. util.showMsg("图片上传失败")
  92. return
  93. }
  94. let type = this.data.type;
  95. util.showBusy("识别图片中")
  96. let studyUser = this.data.studyUser
  97. let showcamara= false
  98. app.formPost('Auth.wxUploadImg', {url, type}).then(res => {
  99. if( res.code == 200 ){
  100. util.showBusy("上传成功")
  101. if ( type =="card"){
  102. studyUser.cardImg = url
  103. studyUser.address = res.data.Address
  104. studyUser.nickname = res.data.Name
  105. studyUser.cardId = res.data.IDNumber
  106. studyUser.gender = res.data.Gender
  107. }else{
  108. studyUser.cardBackImg = url
  109. }
  110. wx.setStorageSync('@identify', studyUser)
  111. this.setData({studyUser,showcamara})
  112. }
  113. })
  114. },
  115. chooseImg: function () {
  116. var that = this
  117. wx.chooseImage({
  118. count: 1,
  119. sizeType: ['compressed'],
  120. sourceType: ['album'],
  121. success: function (res) {
  122. that.setData( {showcamara:false})
  123. util.showSuccess("图片上传中")
  124. util.uploadFile( res.tempFilePaths[0], that.identify )
  125. },
  126. fail:(res)=>{
  127. console.log( res)
  128. util.showMsg("图片选择失败")
  129. }
  130. })
  131. },
  132. takePhoto() {
  133. var that = this
  134. var ctx = this.data.ctx
  135. ctx.takePhoto({
  136. quality: 'low',
  137. success: (res) => {
  138. app.message("图片上传中")
  139. that.setData( {showcamara:false})
  140. var tempImagePath = res.tempImagePath
  141. util.uploadFile( tempImagePath, that.identify )
  142. },
  143. fail: (res)=> {
  144. app.message("图片上传中", "error")
  145. }
  146. })
  147. },
  148. onConfirm( e ){
  149. let {phone} = e.detail.value
  150. let info = this.data.info
  151. let {card_img, card_back_img} = this.data.info
  152. if( !card_img ) return util.showMsg( "身份证正面有误");
  153. if( !card_back_img ) return util.showMsg( "身份证背面有误");
  154. if( !phone || phone.length != 11 ) return util.showMsg( "手机号码有误");
  155. util.http( '/maker/identifyMaker', {phone}, (err,res)=>{
  156. if( err!= 0 ) return;
  157. info.identify = 1
  158. app.identifyUser( )
  159. this.setData({info})
  160. })
  161. },
  162. goBack() {
  163. this.setData({showcamara:false})
  164. }
  165. })