index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <view class="content">
  3. <view class="item-camera" v-if="showHeader">
  4. <camera binderror="" :device-position="devicePosition" :flash="deviceLight" class="camera"
  5. v-if="authCamera">
  6. <cover-image src='../../static/images/head1.png' class="item-cover" v-if="cover">
  7. </cover-image>
  8. </camera>
  9. <view class="item-list" v-if="list_btn">
  10. <image src="../../static/images/light.png" class="image-light" @tap="takeLight"></image>
  11. <image src="../../static/images/take.png" class="image-btn" @tap="takePhotoByHead"></image>
  12. <image src="../../static/images/change.png" class="image-change" @tap="changeDevice"></image>
  13. </view>
  14. <view class="error-handler" v-if="!authCamera">
  15. <button class="nobtn" openType="openSetting">打开相机授权</button>
  16. </view>
  17. </view>
  18. <view class="item-camera" v-if="!showHeader">
  19. <view>
  20. <image :src="image" class="camera"></image>
  21. </view>
  22. <view class="item-btn-list">
  23. <view><button class="use-btn" @tap="resetImage">重拍</button></view>
  24. <view><button class="use-btn" @tap="checkImage">使用照片</button></view>
  25. </view>
  26. </view>
  27. <view v-if="showLogin" class="login-item">
  28. <login :ocode="ocode" :appkey="appkey" scope="snsapi_userinfo" :visible="visible" @success="callback()"
  29. @cancel="callback()" @fail="callback()" />
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. return {
  37. cover: false, //覆盖层
  38. visible: true, //是否可见
  39. authCamera: true, //控制camera
  40. showHeader: true, //控制camera所在的view
  41. list_btn: false, //控制三个按钮
  42. showLogin: true,
  43. ctxHeader: null,
  44. devicePosition: 'front', //摄像头
  45. deviceLight: 'off', //闪光灯
  46. image: '',
  47. card_number: '', //学生信息
  48. appkey: '8C41E2FECF2E9925',
  49. sub_appid: '1015730314_1941301045',
  50. appid: 'wxd6f090391d410534', // 获取用户信息
  51. ocode: '1015730314', // 获取用户信息
  52. app_secret: '9C5EC8E3B7ABBF9C0641804000D8F28E', // 获取用户信息
  53. wxcode: '',
  54. access_token: '',
  55. userinfo: {},
  56. }
  57. },
  58. onLoad(options) {
  59. // console.log(options)
  60. // this.card_number = options.card_number //获取后台传入参数
  61. // if (!this.card_number) {
  62. // this.get_wxcode() //授权
  63. // // uni.showToast({
  64. // // icon: 'false',
  65. // // title: '请重新授权',
  66. // // duration: 1000,
  67. // // success: (res) => {
  68. // // // this.my_display = true
  69. // // }
  70. // // })
  71. // } else {
  72. // this.list_btn = true
  73. // this.get_image()
  74. // this.show_Toast()
  75. //获取摄像头权限
  76. uni.authorize({
  77. scope: 'scope.camera',
  78. success: res => {
  79. }
  80. })
  81. //判断是否获取摄像头权限
  82. uni.getSetting({
  83. success: (res) => {
  84. // console.log(res)
  85. if (res.authSetting["scope.camera"]) {
  86. this.authCamera = true
  87. // this.takePhotoByHead()
  88. } else {
  89. // this.handleCameraError()
  90. this.authCamera = false
  91. }
  92. }
  93. })
  94. // console.log(this.authCamera)
  95. // this.get_image() //拍照获取人脸信息
  96. // }
  97. },
  98. methods: {
  99. //授权获取wxcode
  100. callback: function({
  101. detail
  102. }) {
  103. // console.log(detail)
  104. // {wxcode: "7f7Qi9rN8zQ1nifQiysTZ3WIeIWlgcGk"}
  105. if (detail === undefined) {
  106. // this.userinfo = {};
  107. uni.showToast({
  108. icon: 'success',
  109. title: '需要先授权'
  110. });
  111. } else {
  112. const {
  113. wxcode = ""
  114. } = detail
  115. if (wxcode.length == 0) {
  116. uni.showToast({
  117. title: '未获得wxcode',
  118. duration: 500
  119. })
  120. } else {
  121. // 获取wxcode
  122. this.wxcode = wxcode
  123. // console.log(this.wxcode)
  124. this.list_btn = true
  125. this.cover = true
  126. // 通过wxcode换取access_token
  127. this.get_access_token()
  128. }
  129. }
  130. },
  131. /**
  132. * 通过wxcode换取access_token
  133. */
  134. async get_access_token() {
  135. const res = await this.$myRequest({
  136. host: 'wecard',
  137. url: '/connect/oauth2/token',
  138. method: 'POST',
  139. header: {
  140. 'content-type': 'application/json'
  141. },
  142. data: {
  143. 'wxcode': this.wxcode,
  144. 'app_key': this.appkey,
  145. 'app_secret': this.app_secret,
  146. 'grant_type': 'authorization_code',
  147. 'redirect_uri': 'mnp://wxd6f090391d410534'
  148. }
  149. })
  150. if (res.statusCode == 200) {
  151. if (res.data.access_token == '' || res.data.access_token == 'undefined') {
  152. uni.showToast({
  153. title: '未获得token'
  154. })
  155. } else {
  156. // console.log(res)
  157. this.access_token = res.data.access_token;
  158. // 通过access_token换取用户信息
  159. this.get_user_info();
  160. }
  161. } else {
  162. uni.showToast({
  163. icon: 'none',
  164. title: '请求失败,未获得token'
  165. })
  166. }
  167. },
  168. /**
  169. * 通过access_token换取用户信息
  170. */
  171. async get_user_info() {
  172. const res = await this.$myRequest({
  173. host: 'wecard',
  174. url: '/connect/oauth/get-user-info',
  175. method: 'POST',
  176. header: {
  177. 'content-type': 'application/json'
  178. },
  179. data: {
  180. "access_token": this.access_token
  181. }
  182. });
  183. if (res.statusCode == 200) {
  184. if (res.data.errcode == 0 && res.data.errmsg == 'OK') {
  185. try {
  186. this.userinfo = res.data
  187. // // 存储用户信息
  188. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  189. // 提示授权成功
  190. uni.showToast({
  191. icon: 'success',
  192. title: '授权成功',
  193. duration: 800,
  194. success: (res) => {
  195. // this.my_display = true
  196. this.list_btn = true
  197. this.cover = true
  198. }
  199. })
  200. this.card_number = this.userinfo.card_number
  201. // console.log(this.userinfo)
  202. } catch (e) {
  203. console.log(e)
  204. }
  205. } else {
  206. uni.showToast({
  207. title: '未获得用户信息',
  208. duration: 1000
  209. });
  210. }
  211. } else {
  212. uni.showToast({
  213. icon: 'none',
  214. title: '请求失败,未授权成功'
  215. });
  216. }
  217. },
  218. // //拍照获取人脸相片
  219. // get_image() {
  220. // uni.chooseMedia({
  221. // count: 1, //默认9
  222. // mediaType: ['image'],
  223. // sourceType: ['camera'],
  224. // sizeType: ['original'], //可以指定是原图还是压缩图,默认二者都有
  225. // camera: 'front',
  226. // success: res => {
  227. // // console.log(res)
  228. // let imgFiles = res.tempFiles[0].tempFilePath
  229. // this.image = imgFiles
  230. // console.log('本地地址', this.image)
  231. // this.preview()
  232. // // this.uploadTheImg(imgFiles) //向后台传送文件
  233. // }
  234. // });
  235. // },
  236. //拍摄头像
  237. takePhotoByHead() {
  238. this.showHeader = true //开启拍照
  239. this.ctxHeader = uni.createCameraContext();
  240. this.ctxHeader.takePhoto({
  241. quality: 'high',
  242. success: (res) => {
  243. uni.compressImage({
  244. src: res.tempImagePath,
  245. quality: 90, //压缩比例
  246. success: ress => {
  247. this.image = ress.tempFilePath;
  248. console.log(this.image)
  249. // console.log(typeof(this.image))
  250. this.showHeader = false; //关闭拍照
  251. // this.uploadTheImg()
  252. }
  253. })
  254. }
  255. });
  256. },
  257. // //当摄像头无法启动时显示
  258. // handleCameraError() {
  259. // uni.showToast({
  260. // title: '用户拒绝使用摄像头',
  261. // icon: 'none'
  262. // })
  263. // },
  264. //刷新页面
  265. onPullDownRefresh: function() {
  266. uni.navigateTo({
  267. url: '../index/index',
  268. });
  269. // console.log('触发了下拉刷新')
  270. },
  271. //相片预览
  272. preview() {
  273. console.log('路径' + this.image)
  274. uni.previewImage({
  275. // 对选中的图片进行预览
  276. urls: [this.image, ],
  277. // urls:['',''] 图片的地址 是数组形式
  278. });
  279. },
  280. //重新拍摄
  281. resetImage() {
  282. this.showHeader = true
  283. },
  284. //调用另一个摄像头
  285. changeDevice() {
  286. if (this.devicePosition == 'front') {
  287. this.devicePosition = 'back'
  288. uni.showToast({
  289. title: '开启后摄像头',
  290. icon: 'none'
  291. })
  292. } else {
  293. this.devicePosition = 'front'
  294. uni.showToast({
  295. title: '开启前摄像头',
  296. icon: 'none'
  297. })
  298. }
  299. },
  300. //设置闪光灯
  301. takeLight() {
  302. if (this.deviceLight == 'off') {
  303. this.deviceLight = 'on'
  304. uni.showToast({
  305. title: '开启闪光灯',
  306. icon: 'success'
  307. })
  308. } else {
  309. this.deviceLight = 'off'
  310. uni.showToast({
  311. title: '关闭闪光灯',
  312. icon: 'false'
  313. })
  314. }
  315. },
  316. //确定使用图片
  317. checkImage() {
  318. // console.log(this.image)
  319. uni.showLoading({
  320. title: '图片正在上传中'
  321. });
  322. this.uploadTheImg() //向后台传送文件
  323. },
  324. //上传图片
  325. uploadTheImg() {
  326. uni.uploadFile({
  327. url: 'http://pwawgw.natappfree.cc', //后端用于处理图片并返回图片地址的接口
  328. // header: {
  329. // "Content-Type": "multipart/form-data" // formdata提交格式
  330. // },
  331. filePath: this.image,
  332. name: 'uploadFile', // 默认
  333. fileType: 'image',
  334. formData: { // 其他的formdata参数
  335. card_number: this.card_number,
  336. },
  337. success: (res) => {
  338. console.log(res)
  339. // let data = JSON.parse(res.data) //返回的是字符串,需要转成对象格式,打印data如下图
  340. // if (data.code == 200) {
  341. // console.log(data.msg) //图片地址
  342. // }
  343. uni.hideLoading();
  344. // var code = '200'
  345. // if (code == 200) {
  346. if (res.statusCode == 200) {
  347. uni.showToast({
  348. icon: 'success',
  349. title: '人脸采集成功',
  350. success: (res) => {
  351. setTimeout(function() {
  352. uni.navigateTo({
  353. url: '../confirm/confirm',
  354. })
  355. }, 1500);
  356. }
  357. })
  358. } else {
  359. uni.showModal({
  360. title: '人脸采集失败',
  361. content: '请重新拍照上传',
  362. confirmText: '重新拍照',
  363. cancelText: '直接跳转',
  364. // showCancel: false,
  365. success: res => {
  366. if (res.confirm) {
  367. this.showHeader = true
  368. } else if (res.cancel) {
  369. uni.navigateTo({
  370. url: '../confirm/confirm',
  371. })
  372. }
  373. }
  374. });
  375. }
  376. },
  377. fail: (res) => {
  378. console.log(res)
  379. }
  380. })
  381. },
  382. },
  383. }
  384. </script>
  385. <style>
  386. @import url("index.css");
  387. </style>