index.js 4.3 KB

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