index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. import {
  5. VueLikePage
  6. } from '../../utils/page'
  7. import { CDN_URL,API_BASE_URL,TYPE } from '../../config/index'
  8. import {isPhoneX} from './../../utils/tools'
  9. import Router from '../../utils/routes'
  10. import Toast from '../../components/toast/toast';
  11. VueLikePage([],{
  12. data:{
  13. cdn_url:CDN_URL,
  14. isBackCamera:false,
  15. current:'1',
  16. currentImg:'',
  17. isSelect: false,
  18. form:''
  19. },
  20. methods:{
  21. onLoad:function (options) {
  22. let {id} = options
  23. this.setData({
  24. current:id
  25. })
  26. let left = app.globalData.deviceInfo.windowWidth - app.globalData.top.right
  27. isPhoneX().then(res => {
  28. this.setData({
  29. isIphoneX: res,
  30. top:app.globalData.top,
  31. left
  32. })
  33. })
  34. },
  35. onShow:function () {
  36. // this.reTake()
  37. if (this.data.form == 'work') {
  38. this.setData({
  39. form:''
  40. })
  41. this.reTake()
  42. }
  43. },
  44. loadcompele(){
  45. this.setData({
  46. loadCompele: true
  47. })
  48. },
  49. back(){
  50. if (this.data.currentImg) {
  51. this.reTake()
  52. }
  53. else{
  54. wx.navigateBack()
  55. }
  56. },
  57. changPosition(){
  58. this.setData({
  59. isBackCamera:!this.data.isBackCamera
  60. })
  61. },
  62. reTake(){
  63. this.setData({
  64. currentImg:'',
  65. isSelect:false
  66. })
  67. },
  68. submit(){
  69. let toast = Toast.loading({
  70. message:'正在生成中,请耐心等候',
  71. selector: '#loading-toast'
  72. });
  73. wx.uploadFile({
  74. url: `${API_BASE_URL}/api/uploadFaceSwap/${TYPE[this.data.current]}`,
  75. filePath: this.data.currentImg,
  76. name: 'file',
  77. success: (res)=>{
  78. let data = JSON.parse(res.data)
  79. if (data.code==0) {
  80. this.setData({
  81. work:data.msg
  82. })
  83. Router.push({
  84. url: 'work',
  85. query: {
  86. vr_link:data.msg,
  87. id: this.data.current
  88. }
  89. })
  90. }
  91. else{
  92. Toast.fail({
  93. message:this.data.isSelect?'无法识别人脸,请选取正脸照片':'无法识别人脸,请正对摄像头拍照',
  94. selector: '#fail-toast',
  95. callBack:()=>{
  96. if (this.data.isSelect) {
  97. this.back()
  98. wx.chooseImage({
  99. sourceType:["album"],
  100. count: 1,
  101. sizeType: ['original', 'compressed'],
  102. success:res =>{
  103. this.setData({
  104. currentImg: res.tempFilePaths[0],
  105. isSelect:true
  106. })
  107. },
  108. fail:error =>{
  109. console.log(error);
  110. }
  111. })
  112. }
  113. else{
  114. this.reTake()
  115. }
  116. }
  117. });
  118. }
  119. },
  120. fail:()=>{
  121. Toast.fail({
  122. message:this.data.isSelect?'无法识别人脸,请选取正脸照片':'无法识别人脸,请正对摄像头拍照',
  123. selector: '#fail-toast',
  124. callBack:()=>{
  125. if (this.data.isSelect) {
  126. this.back()
  127. wx.chooseImage({
  128. sourceType:["album"],
  129. count: 1,
  130. sizeType: ['original', 'compressed'],
  131. success:res =>{
  132. this.setData({
  133. currentImg: res.tempFilePaths[0],
  134. isSelect:true
  135. })
  136. },
  137. fail:error =>{
  138. console.log(error);
  139. }
  140. })
  141. }
  142. else{
  143. this.reTake()
  144. }
  145. }
  146. });
  147. },
  148. complete(){
  149. toast.hideLoading()
  150. }
  151. })
  152. },
  153. selectImage(){
  154. Toast.message({
  155. message:'请选取正脸照片',
  156. selector: '#tips-toast',
  157. callBack:()=>{
  158. wx.chooseImage({
  159. sourceType:["album"],
  160. count: 1,
  161. sizeType: ['original', 'compressed'],
  162. success:res =>{
  163. this.setData({
  164. currentImg: res.tempFilePaths[0],
  165. isSelect:true
  166. })
  167. console.log(this.data.currentImg,this.data.isSelect)
  168. },
  169. fail:error =>{
  170. console.log(error);
  171. }
  172. })
  173. }
  174. });
  175. },
  176. takePhoto() {
  177. const ctx = wx.createCameraContext()
  178. ctx.takePhoto({
  179. quality: 'high',
  180. success: (res) => {
  181. this.setData({
  182. currentImg: res.tempImagePath,
  183. isSelect:false
  184. })
  185. }
  186. })
  187. }
  188. }
  189. })