index.js 4.2 KB

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