index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {
  2. getLoraList,
  3. createAiImg,
  4. getTaskResult,
  5. } from '~/api/avatar'
  6. import {
  7. userEvent
  8. } from '~/api/global'
  9. Page({
  10. data: {
  11. templates: [],
  12. loading: false,
  13. active: '',
  14. artistic: '',
  15. original: ''
  16. },
  17. onLoad() {
  18. getLoraList().then(res => {
  19. this.setData({
  20. templates: res.loras,
  21. active: res.loras[0] ? res.loras[0].lora_id : ''
  22. })
  23. })
  24. },
  25. async selectTemplate({
  26. currentTarget
  27. }) {
  28. if (this.data.loading) {
  29. return
  30. }
  31. this.setData({
  32. active: currentTarget.dataset.id
  33. })
  34. if (this.data.original) {
  35. this.createAiImg()
  36. }
  37. /* await userEvent({
  38. action: "style",
  39. id: currentTarget.dataset.name
  40. }) */
  41. },
  42. uploadImg() {
  43. if (this.data.loading) {
  44. return
  45. }
  46. wx.chooseMedia({
  47. count: 1,
  48. mediaType: ['image'],
  49. sizeType: ['compressed'], // original 原图;compressed 压缩图
  50. sourceType: ['album', 'camera'],
  51. camera: 'back',
  52. success: (res) => {
  53. this.cropper = this.selectComponent("#cropper");
  54. this.cropper.init({
  55. imgPath: res.tempFiles[0].tempFilePath, //imgPath是需要裁剪图片的图片路径,只支持本地或临时路径
  56. success: (imgUrl) => {
  57. wx.getFileSystemManager().readFile({
  58. filePath: imgUrl,
  59. encoding: "base64",
  60. success: res => {
  61. //返回base64格式
  62. var base64Str = 'data:image/png' + ';base64,' + res.data
  63. this.setData({
  64. artistic: base64Str,
  65. original: base64Str
  66. })
  67. this.createAiImg()
  68. /* userEvent({
  69. action: "photo"
  70. }) */
  71. },
  72. fail: err => {
  73. console.log(err)
  74. }
  75. })
  76. },
  77. fail(error) {
  78. console.log(error) //有两种:cancel代表点击了叉,fail代表wx.canvasToTempFilePath生成图片失败
  79. }
  80. });
  81. }
  82. })
  83. },
  84. createAiImg() {
  85. if (!this.data.original || this.data.loading) {
  86. return
  87. }
  88. this.setData({
  89. loading: true
  90. })
  91. createAiImg({
  92. "width": 512, //生成图片宽度
  93. "height": 512, //生成图片高度
  94. "n_samples": 1, //生成图片数量
  95. "controlnet_input_image_base64": this.data.original, //控制图片base64编码
  96. "style_id": this.data.active //控制风格id
  97. }).then(res => {
  98. this.getAiImg(res.uuid)
  99. }).catch(() => {
  100. this.setData({
  101. loading: false
  102. })
  103. wx.showToast({
  104. title: '网络异常请重试',
  105. icon: 'none',
  106. duration: 2500
  107. })
  108. })
  109. },
  110. async getAiImg(uuid) {
  111. console.log(uuid, 'uuuid');
  112. let res = await getTaskResult({
  113. uuid
  114. })
  115. if (res.data.status != 2) {
  116. setTimeout(() => {
  117. this.getAiImg(uuid)
  118. }, 2500)
  119. } else {
  120. this.setData({
  121. loading: false,
  122. artistic: res.data.generated_imgs[0]
  123. })
  124. }
  125. },
  126. downloadImg() {
  127. wx.getSetting({
  128. success: (res) => {
  129. if (res.authSetting['scope.writePhotosAlbum']) {
  130. this.base64ToImg()
  131. } else {
  132. wx.authorize({
  133. scope: 'scope.writePhotosAlbum',
  134. success: () => {
  135. this.base64ToImg()
  136. },
  137. fail(res) {
  138. wx.showModal({
  139. title: '无法保存到相册',
  140. content: '点击右上角浮点按钮->设置,进行授权',
  141. confirmText: '我知道了',
  142. showCancel: false,
  143. })
  144. }
  145. })
  146. }
  147. }
  148. })
  149. },
  150. base64ToImg() {
  151. const base64 = this.data.artistic; //base64格式图片
  152. if (!base64 || this.data.loading) {
  153. return
  154. }
  155. const time = new Date().getTime();
  156. const imgPath = wx.env.USER_DATA_PATH + "/poster" + time + "" + ".png";
  157. //如果图片字符串不含要清空的前缀,可以不执行下行代码.
  158. const imageData = base64.replace(/^data:image\/\w+;base64,/, "");
  159. const file = wx.getFileSystemManager();
  160. file.writeFileSync(imgPath, imageData, "base64");
  161. wx.saveImageToPhotosAlbum({
  162. filePath: imgPath,
  163. success: async (res) => {
  164. wx.showModal({
  165. title: '照片已保存至相册',
  166. content: '快去分享给小伙伴吧',
  167. confirmText: '我知道了',
  168. showCancel: false,
  169. })
  170. /* await userEvent({
  171. action: 'save'
  172. }) */
  173. }
  174. })
  175. },
  176. onShareAppMessage() {
  177. /* userEvent({
  178. action: 'share'
  179. }) */
  180. return {
  181. title: '玩转AI头像',
  182. path: '/pages/index/index',
  183. }
  184. }
  185. })